Java Code Examples for android.app.ActivityThread#currentActivityThread()

The following examples show how to use android.app.ActivityThread#currentActivityThread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLiteCompatibilityWalFlags.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static void initIfNeeded() {
    if (sInitialized || sCallingGlobalSettings) {
        return;
    }
    ActivityThread activityThread = ActivityThread.currentActivityThread();
    Application app = activityThread == null ? null : activityThread.getApplication();
    String flags = null;
    if (app == null) {
        Log.w(TAG, "Cannot read global setting "
                + Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS + " - "
                + "Application state not available");
    } else {
        try {
            sCallingGlobalSettings = true;
            flags = Settings.Global.getString(app.getContentResolver(),
                    Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS);
        } finally {
            sCallingGlobalSettings = false;
        }
    }

    init(flags);
}
 
Example 2
Source File: ResourcesManager.java    From VirtualAPK with Apache License 2.0 6 votes vote down vote up
public static void hookResources(Context base, Resources resources) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return;
    }
    try {
        Reflector reflector = Reflector.with(base);
        reflector.field("mResources").set(resources);
        Object loadedApk = reflector.field("mPackageInfo").get();
        Reflector.with(loadedApk).field("mResources").set(resources);

        Object activityThread = ActivityThread.currentActivityThread();
        Object resManager;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            resManager = android.app.ResourcesManager.getInstance();
        } else {
            resManager = Reflector.with(activityThread).field("mResourcesManager").get();
        }
        Map<Object, WeakReference<Resources>> map = Reflector.with(resManager).field("mActiveResources").get();
        Object key = map.keySet().iterator().next();
        map.put(key, new WeakReference<>(resources));
    } catch (Exception e) {
        Log.w(TAG, e);
    }
}
 
Example 3
Source File: PluginManager.java    From VirtualAPK with Apache License 2.0 6 votes vote down vote up
protected void hookInstrumentationAndHandler() {
        try {
            ActivityThread activityThread = ActivityThread.currentActivityThread();
            Instrumentation baseInstrumentation = activityThread.getInstrumentation();
//            if (baseInstrumentation.getClass().getName().contains("lbe")) {
//                // reject executing in paralell space, for example, lbe.
//                System.exit(0);
//            }
    
            final VAInstrumentation instrumentation = createInstrumentation(baseInstrumentation);
            
            Reflector.with(activityThread).field("mInstrumentation").set(instrumentation);
            Handler mainHandler = Reflector.with(activityThread).method("getHandler").call();
            Reflector.with(mainHandler).field("mCallback").set(instrumentation);
            this.mInstrumentation = instrumentation;
            Log.d(TAG, "hookInstrumentationAndHandler succeed : " + mInstrumentation);
        } catch (Exception e) {
            Log.w(TAG, e);
        }
    }
 
Example 4
Source File: RuntimeInit.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void uncaughtException(Thread t, Throwable e) {
    try {
        ensureLogging(t, e);

        // Don't re-enter -- avoid infinite loops if crash-reporting crashes.
        if (mCrashing) return;
        mCrashing = true;

        // Try to end profiling. If a profiler is running at this point, and we kill the
        // process (below), the in-memory buffer will be lost. So try to stop, which will
        // flush the buffer. (This makes method trace profiling useful to debug crashes.)
        if (ActivityThread.currentActivityThread() != null) {
            ActivityThread.currentActivityThread().stopProfiling();
        }

        // Bring up crash dialog, wait for it to be dismissed
        ActivityManager.getService().handleApplicationCrash(
                mApplicationObject, new ApplicationErrorReport.ParcelableCrashInfo(e));
    } catch (Throwable t2) {
        if (t2 instanceof DeadObjectException) {
            // System process is dead; ignore
        } else {
            try {
                Clog_e(TAG, "Error reporting crash", t2);
            } catch (Throwable t3) {
                // Even Clog_e() fails!  Oh well.
            }
        }
    } finally {
        // Try everything to make sure this process goes away.
        Process.killProcess(Process.myPid());
        System.exit(10);
    }
}
 
Example 5
Source File: UserData.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static int getInt(String settings, int defaultValue) {
    ContentResolver cr = null;
    final ActivityThread at = ActivityThread.currentActivityThread();
    if (at != null) {
        cr = at.getApplication().getContentResolver();
    }

    if (cr == null) {
        Log.w(TAG, "Could not read from " + settings + "; hardcoding " + defaultValue);
        return defaultValue;
    }
    return Settings.Secure.getInt(cr, settings, defaultValue);
}
 
Example 6
Source File: Neptune.java    From Neptune with Apache License 2.0 5 votes vote down vote up
/**
 * 反射替换ActivityThread的mInstrumentation
 */
private static void hookInstrumentation() {

    PluginDebugLog.runtimeLog(TAG, "need to hook Instrumentation for plugin framework");
    ActivityThread activityThread = ActivityThread.currentActivityThread();
    Instrumentation hostInstr = getHostInstrumentation();

    if (hostInstr != null) {
        String hostInstrName = hostInstr.getClass().getName();
        PluginDebugLog.runtimeLog(TAG, "host Instrument name: " + hostInstrName);

        if (hostInstrName.startsWith("com.chaozhuo.superme")
                || hostInstrName.startsWith("com.lody.virtual")) {
            // warning: 特殊case,VirtualApp环境,暂不Hook
            PluginDebugLog.runtimeLog(TAG, "reject hook instrument, run in VirtualApp Environment");
        } else if (hostInstr instanceof NeptuneInstrument) {
            // already hooked
            PluginDebugLog.runtimeLog(TAG, "ActivityThread Instrumentation already hooked");
        } else {
            PluginInstrument pluginInstrument = new NeptuneInstrument(hostInstr);
            ReflectionUtils.on(activityThread).set("mInstrumentation", pluginInstrument);
            PluginDebugLog.runtimeLog(TAG, "init hook ActivityThread Instrumentation success");
        }
    } else {
        PluginDebugLog.runtimeLog(TAG, "init hook ActivityThread Instrumentation failed, hostInstr==null");
    }
}
 
Example 7
Source File: Neptune.java    From Neptune with Apache License 2.0 5 votes vote down vote up
/**
 * 获取ActivityThread的Instrumentation对象
 */
public static Instrumentation getHostInstrumentation() {

    if (mHostInstr == null) {
        ActivityThread activityThread = ActivityThread.currentActivityThread();
        Instrumentation hostInstr = activityThread.getInstrumentation();
        mHostInstr = PluginInstrument.unwrap(hostInstr);
    }

    return mHostInstr;
}
 
Example 8
Source File: PluginManager.java    From VirtualAPK with Apache License 2.0 5 votes vote down vote up
protected void hookIContentProviderAsNeeded() {
    Uri uri = Uri.parse(RemoteContentProvider.getUri(mContext));
    mContext.getContentResolver().call(uri, "wakeup", null, null);
    try {
        Field authority = null;
        Field provider = null;
        ActivityThread activityThread = ActivityThread.currentActivityThread();
        Map providerMap = Reflector.with(activityThread).field("mProviderMap").get();
        Iterator iter = providerMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            String auth;
            if (key instanceof String) {
                auth = (String) key;
            } else {
                if (authority == null) {
                    authority = key.getClass().getDeclaredField("authority");
                    authority.setAccessible(true);
                }
                auth = (String) authority.get(key);
            }
            if (auth.equals(RemoteContentProvider.getAuthority(mContext))) {
                if (provider == null) {
                    provider = val.getClass().getDeclaredField("mProvider");
                    provider.setAccessible(true);
                }
                IContentProvider rawProvider = (IContentProvider) provider.get(val);
                IContentProvider proxy = IContentProviderProxy.newInstance(mContext, rawProvider);
                mIContentProvider = proxy;
                Log.d(TAG, "hookIContentProvider succeed : " + mIContentProvider);
                break;
            }
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }
}
 
Example 9
Source File: DodoApplication.java    From DeepInVirtualApp with MIT License 5 votes vote down vote up
@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //从PackageManager里拿取全部的Activity桩
        initStub();
        mainThread = ActivityThread.currentActivityThread();
// 主进程名
        mainProcessName = base.getApplicationInfo().processName;
        // 当前进程名
        processName = mainThread.getProcessName();
        if (processName.equals(mainProcessName)) {
            processType = ProcessType.Main;
        } else if (isAppProcess(processName)) {
            processType = ProcessType.VAppClient;
        } else {
            processType = ProcessType.CHILD;
        }
        mOriginPackageManager = this.getPackageManager();
        //此处注入补丁
        try {
            PatchManager.getInstance().injectAll();
            PatchManager.getInstance().checkEnv();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }
 
Example 10
Source File: PluginContentResolver.java    From Neptune with Apache License 2.0 4 votes vote down vote up
private static void hookIContentProviderAsNeeded(Context context) {
    Uri uri = Uri.parse(ContentProviderProxy1.getUri(context));
    context.getContentResolver().call(uri, "wakeup", null, null);
    try {
        Field authority = null;
        Field provider = null;
        ActivityThread activityThread = ActivityThread.currentActivityThread();
        Map providerMap = ReflectionUtils.on(activityThread).get("mProviderMap");
        Iterator iterator = providerMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            String auth;
            if (key instanceof String) {
                // 4.2以下版本
                auth = (String) key;
            } else {
                // ProviderKey
                if (authority == null) {
                    authority = key.getClass().getDeclaredField("authority");
                    authority.setAccessible(true);
                }
                auth = (String) authority.get(key);
            }
            // 找到了代理ContentProvider的authority
            if (TextUtils.equals(auth, ContentProviderProxy1.getAuthority(context))) {
                if (provider == null) {
                    provider = value.getClass().getDeclaredField("mProvider");
                    provider.setAccessible(true);
                }
                IContentProvider rawProvider = (IContentProvider) provider.get(value);
                IContentProvider proxy = IContentProviderProxy.newInstance(context, rawProvider);
                sIContentProvider = proxy;
                PluginDebugLog.runtimeLog(TAG, "hookIContentProvider succeed : " + sIContentProvider);
                break;
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: RemoteHandler.java    From AppOpsX with MIT License 4 votes vote down vote up
private CallerResult callClass(ClassCaller caller){
  CallerResult result = new CallerResult();
  try {
    ActivityThread activityThread = ActivityThread.currentActivityThread();
    Context context = activityThread.getSystemContext();
    Context packageContext = null;

    //create or from cache get context
    WeakReference<Context> contextWeakReference = sLocalContext.get(caller.getPackageName());
    if (contextWeakReference != null && contextWeakReference.get() != null) {
      packageContext = contextWeakReference.get();
    }
    if (packageContext == null) {
      packageContext = context.createPackageContext(caller.getPackageName(), Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
      sLocalContext.put(caller.getPackageName(), new WeakReference<Context>(packageContext));
    }

    //load class
    Class<?> aClass = sClassCache.get(caller.getClassName());
    Constructor<?> aConstructor = sConstructorCache.get(caller.getClassName());
    if (aClass == null || aConstructor == null) {

      aClass = Class.forName(caller.getClassName(), false, packageContext.getClassLoader());
      Class<?> processer=Class.forName(ClassCallerProcessor.class.getName(),false,packageContext.getClassLoader());

      if (processer.isAssignableFrom(aClass)) {
        sClassCache.put(caller.getClassName(), aClass);
        sConstructorCache.put(caller.getClassName(),aClass.getConstructor(Context.class,Context.class,byte[].class));
      }else {
        throw new ClassCastException("class "+aClass.getName()+"  need extends ClassCallerProcessor !");
      }
    }

    //if found class,invoke proxyInvoke method
    if (aClass != null) {

      Object o = null;
      if(aConstructor != null){

        o = aConstructor.newInstance(packageContext,context,ParcelableUtil.marshall(LifecycleAgent.serverRunInfo));
      }

      Object[] params = caller.getParams();
      if(params != null){
        for (Object param : params) {
          if(param instanceof Bundle){
            ((Bundle) param).setClassLoader(packageContext.getClassLoader());
          }
        }
      }

      FLog.log("------new object "+o+"  params "+Arrays.toString(params)+"    "+aClass);

      Object ret = MethodUtils.invokeExactMethod(o, "proxyInvoke", params,new Class[]{Bundle.class});
      if (ret != null && ret instanceof Bundle) {
        writeResult(result, ret);
      } else {
        writeResult(result, Bundle.EMPTY);
      }

    } else {
      throw new ClassNotFoundException("not found class " + caller.getClassName() + "  in package: " + caller.getPackageName());
    }

  } catch (Throwable e) {
    e.printStackTrace();
    FLog.log(e);
    result.setThrowable(e);
  }

  return result;
}