content: package top.niunaijun.blackbox.fake.service;
  
  import android.content.ComponentName;
  import android.content.Intent;
  import android.content.pm.PackageInfo;
  import android.content.pm.ResolveInfo;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.IBinder;
  
  import java.io.File;
  import java.lang.reflect.Method;
  
  import top.niunaijun.blackbox.BlackBoxCore;
  import top.niunaijun.blackbox.app.BActivityThread;
  import top.niunaijun.blackbox.fake.hook.MethodHook;
  import top.niunaijun.blackbox.fake.hook.ProxyMethod;
  import top.niunaijun.blackbox.fake.provider.FileProviderHandler;
  import top.niunaijun.blackbox.utils.ComponentUtils;
  import top.niunaijun.blackbox.utils.MethodParameterUtils;
  import top.niunaijun.blackbox.utils.Slog;
  import top.niunaijun.blackbox.utils.compat.BuildCompat;
  import top.niunaijun.blackbox.utils.compat.StartActivityCompat;
  
  import static android.content.pm.PackageManager.GET_META_DATA;
  
  /**
   * updated by alex5402 on 4/21/21.
   * * ∧＿∧
   * (`･ω･∥
   * 丶　つ０
   * しーＪ
   * TFNQw5HgWUS33Ke1eNmSFTwoQySGU7XNsK (USDT TRC20)
   */
  public class ActivityManagerCommonProxy {
      public static final String TAG = "CommonStub";
  
      @ProxyMethod("startActivity")
      public static class StartActivity extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              MethodParameterUtils.replaceFirstAppPkg(args);
              Intent intent = getIntent(args);
              Slog.d(TAG, "Hook in : " + intent);
              assert intent != null;
              // Allow PermissionController to run so apps receive onRequestPermissionsResult
              // (granting is still handled by our Package/AppOps hooks).
              if (intent.getParcelableExtra("_B_|_target_") != null) {
                  return method.invoke(who, args);
              }
              if (ComponentUtils.isRequestInstall(intent)) {
                  File file = FileProviderHandler.convertFile(BActivityThread.getApplication(), intent.getData());
                  
                  // Check if this is an attempt to install BlackBox app
                  if (file != null && file.exists()) {
                      try {
                          PackageInfo packageInfo = BlackBoxCore.getPackageManager().getPackageArchiveInfo(file.getAbsolutePath(), 0);
                          if (packageInfo != null) {
                              String packageName = packageInfo.packageName;
                              String hostPackageName = BlackBoxCore.getHostPkg();
                              if (packageName.equals(hostPackageName)) {
                                  Slog.w(TAG, "Blocked attempt to install BlackBox app from within BlackBox: " + packageName);
                                  // Return success but don't actually install
                                  return 0;
                              }
                          }
                      } catch (Exception e) {
                          Slog.w(TAG, "Could not verify if this is BlackBox app: " + e.getMessage());
                      }
                  }
                  
                  if (BlackBoxCore.get().requestInstallPackage(file, BActivityThread.getUserId())) {
                      return 0;
                  }
                  intent.setData(FileProviderHandler.convertFileUri(BActivityThread.getApplication(), intent.getData()));
                  return method.invoke(who, args);
              }
              String dataString = intent.getDataString();
              if (dataString != null && dataString.equals("package:" + BActivityThread.getAppPackageName())) {
                  intent.setData(Uri.parse("package:" + BlackBoxCore.getHostPkg()));
              }
  
              ResolveInfo resolveInfo = BlackBoxCore.getBPackageManager().resolveActivity(
                      intent,
                      GET_META_DATA,
                      StartActivityCompat.getResolvedType(args),
                      BActivityThread.getUserId());
              if (resolveInfo == null) {
                  String origPackage = intent.getPackage();
                  if (intent.getPackage() == null && intent.getComponent() == null) {
                      intent.setPackage(BActivityThread.getAppPackageName());
                  } else {
                      origPackage = intent.getPackage();
                  }
                  resolveInfo = BlackBoxCore.getBPackageManager().resolveActivity(
                          intent,
                          GET_META_DATA,
                          StartActivityCompat.getResolvedType(args),
                          BActivityThread.getUserId());
                  if (resolveInfo == null) {
                      intent.setPackage(origPackage);
                      return method.invoke(who, args);
                  }
              }
  
  
              intent.setExtrasClassLoader(who.getClass().getClassLoader());
              intent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name));
              BlackBoxCore.getBActivityManager().startActivityAms(BActivityThread.getUserId(),
                      StartActivityCompat.getIntent(args),
                      StartActivityCompat.getResolvedType(args),
                      StartActivityCompat.getResultTo(args),
                      StartActivityCompat.getResultWho(args),
                      StartActivityCompat.getRequestCode(args),
                      StartActivityCompat.getFlags(args),
                      StartActivityCompat.getOptions(args));
              return 0;
          }
  
          private Intent getIntent(Object[] args) {
              int index;
              if (BuildCompat.isR()) {
                  index = 3;
              } else {
                  index = 2;
              }
              if (args[index] instanceof Intent) {
                  return (Intent) args[index];
              }
              for (Object arg : args) {
                  if (arg instanceof Intent) {
                      return (Intent) arg;
                  }
              }
              return null;
          }
      }
  
      @ProxyMethod("startActivities")
      public static class StartActivities extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              int index = getIntents();
              Intent[] intents = (Intent[]) args[index++];
              String[] resolvedTypes = (String[]) args[index++];
              IBinder resultTo = (IBinder) args[index++];
              Bundle options = (Bundle) args[index];
              // todo ??
              if (!ComponentUtils.isSelf(intents)) {
                  return method.invoke(who, args);
              }
  
              for (Intent intent : intents) {
                  intent.setExtrasClassLoader(who.getClass().getClassLoader());
              }
              return BlackBoxCore.getBActivityManager().startActivities(BActivityThread.getUserId(),
                      intents, resolvedTypes, resultTo, options);
          }
  
          public int getIntents() {
              if (BuildCompat.isR()) {
                  return 3;
              }
              return 2;
          }
      }
  
      @ProxyMethod("startIntentSenderForResult")
      public static class StartIntentSenderForResult extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("activityResumed")
      public static class ActivityResumed extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              BlackBoxCore.getBActivityManager().onActivityResumed((IBinder) args[0]);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("activityDestroyed")
      public static class ActivityDestroyed extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              BlackBoxCore.getBActivityManager().onActivityDestroyed((IBinder) args[0]);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("finishActivity")
      public static class FinishActivity extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              BlackBoxCore.getBActivityManager().onFinishActivity((IBinder) args[0]);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("getAppTasks")
      public static class GetAppTasks extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              MethodParameterUtils.replaceFirstAppPkg(args);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("getCallingPackage")
      public static class getCallingPackage extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              return BlackBoxCore.getBActivityManager().getCallingPackage((IBinder) args[0], BActivityThread.getUserId());
          }
      }
  
      @ProxyMethod("getCallingActivity")
      public static class getCallingActivity extends MethodHook {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              return BlackBoxCore.getBActivityManager().getCallingActivity((IBinder) args[0], BActivityThread.getUserId());
          }
      }
  }
  