com.didi.virtualapk.PluginManager Java Examples

The following examples show how to use com.didi.virtualapk.PluginManager. 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: PluginUtil.java    From VirtualAPK with Apache License 2.0 6 votes vote down vote up
public static int getTheme(Context context, ComponentName component) {
    LoadedPlugin loadedPlugin = PluginManager.getInstance(context).getLoadedPlugin(component);

    if (null == loadedPlugin) {
        return 0;
    }

    ActivityInfo info = loadedPlugin.getActivityInfo(component);
    if (null == info) {
        return 0;
    }

    if (0 != info.theme) {
        return info.theme;
    }

    ApplicationInfo appInfo = info.applicationInfo;
    if (null != appInfo && appInfo.theme != 0) {
        return appInfo.theme;
    }

    return selectDefaultTheme(0, Build.VERSION.SDK_INT);
}
 
Example #2
Source File: MainActivity.java    From VirtualAPKDemo with Apache License 2.0 6 votes vote down vote up
private void loadPlugin(Context base) {
    PluginManager pluginManager = PluginManager.getInstance(base);
    File apk = new File(getExternalStorageDirectory(), "plugin.apk");
    if (apk.exists()) {
        try {
            pluginManager.loadPlugin(apk);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(getApplicationContext(),
                "SDcard根目录未检测到plugin.apk插件", Toast.LENGTH_SHORT).show();
    }
}
 
Example #3
Source File: RemoteService.java    From VirtualAPK with Apache License 2.0 6 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null) {
        return super.onStartCommand(intent, flags, startId);
    }

    Intent target = intent.getParcelableExtra(EXTRA_TARGET);
    if (target != null) {
        String pluginLocation = intent.getStringExtra(EXTRA_PLUGIN_LOCATION);
        ComponentName component = target.getComponent();
        LoadedPlugin plugin = PluginManager.getInstance(this).getLoadedPlugin(component);
        if (plugin == null && pluginLocation != null) {
            try {
                PluginManager.getInstance(this).loadPlugin(new File(pluginLocation));
            } catch (Exception e) {
                Log.w(TAG, e);
            }
        }
    }

    return super.onStartCommand(intent, flags, startId);
}
 
Example #4
Source File: MainActivity.java    From VirtualAPKDemo with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    if (PluginManager.getInstance(this).getLoadedPlugin(PLUGIN_PKG_NAME) == null) {
        Toast.makeText(getApplicationContext(),
                "插件未加载,请尝试重启APP", Toast.LENGTH_SHORT).show();
        return;
    }

    Intent intent = new Intent();
    intent.setClassName("com.virtualapk.imageplugin", "com.virtualapk.imageplugin.ImageBrowserActivity");
    startActivity(intent);
}
 
Example #5
Source File: BaseApplication.java    From BlogDemo with Apache License 2.0 5 votes vote down vote up
@Override
protected void attachBaseContext(Context base) {
  super.attachBaseContext(base);
  long start = System.currentTimeMillis();
  PluginManager.getInstance(base).init();
  Log.d("ryg", "use time:" + (System.currentTimeMillis() - start));
}
 
Example #6
Source File: MainActivity.java    From BlogDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 加载插件
 *
 * @param pkg 插件包名
 * @param pluginFile 根目录插件文件名
 */
private void loadPlugin(String pkg, String pluginFile) {
  PluginManager pluginManager = PluginManager.getInstance(this);
  File apk = new File(Environment.getExternalStorageDirectory(), pluginFile);
  if (apk.exists()) {
    try {
      unInstallPlugin(pkg);
      pluginManager.loadPlugin(apk);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
Example #7
Source File: PluginUtil.java    From VirtualAPK with Apache License 2.0 5 votes vote down vote up
public static void hookActivityResources(Activity activity, String packageName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isVivo(activity.getResources())) {
        // for 5.0+ vivo
        return;
    }

    // designed for 5.0 - only, but some bad phones not work, eg:letv
    try {
        Context base = activity.getBaseContext();
        final LoadedPlugin plugin = PluginManager.getInstance(activity).getLoadedPlugin(packageName);
        final Resources resources = plugin.getResources();
        if (resources != null) {
            Reflector.with(base).field("mResources").set(resources);

            // copy theme
            Resources.Theme theme = resources.newTheme();
            theme.setTo(activity.getTheme());
            Reflector reflector = Reflector.with(activity);
            int themeResource = reflector.field("mThemeResource").get();
            theme.applyStyle(themeResource, true);
            reflector.field("mTheme").set(theme);

            reflector.field("mResources").set(resources);
        }
    } catch (Exception e) {
        Log.w(Constants.TAG, e);
    }
}
 
Example #8
Source File: MainActivity.java    From BlogDemo with Apache License 2.0 5 votes vote down vote up
private void startPluginActivity(String pkg, String activityPath) {
  if (PluginManager.getInstance(this).getLoadedPlugin(pkg) == null) {
    Toast.makeText(this, "plugin [com.didi.virtualapk.demo] not loaded", Toast.LENGTH_SHORT)
        .show();
    return;
  }
  Intent intent = new Intent();
  intent.setClassName(this, activityPath);
  startActivity(intent);
}
 
Example #9
Source File: App.java    From android-open-framework-analysis with Apache License 2.0 4 votes vote down vote up
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    PluginManager.getInstance(base).init();
}
 
Example #10
Source File: PluginContentResolver.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public PluginContentResolver(Context context) {
    super(context);
    mPluginManager = PluginManager.getInstance(context);
}
 
Example #11
Source File: MyApplication.java    From VirtualAPKDemo with Apache License 2.0 4 votes vote down vote up
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    PluginManager.getInstance(base).init();
}
 
Example #12
Source File: ActivityManagerProxy.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public ActivityManagerProxy(PluginManager pluginManager, IActivityManager activityManager) {
    this.mPluginManager = pluginManager;
    this.mActivityManager = activityManager;
}
 
Example #13
Source File: LocalService.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    mPluginManager = PluginManager.getInstance(this);
}
 
Example #14
Source File: ComponentsHandler.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public ComponentsHandler(PluginManager pluginManager) {
    mPluginManager = pluginManager;
    mContext = pluginManager.getHostContext();
}
 
Example #15
Source File: ResourcesManager.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
/**
 * Use System Apis to update all existing resources.
 * <br/>
 * 1. Update ApplicationInfo.splitSourceDirs and LoadedApk.mSplitResDirs
 * <br/>
 * 2. Replace all keys of ResourcesManager.mResourceImpls to new ResourcesKey
 * <br/>
 * 3. Use ResourcesManager.appendLibAssetForMainAssetPath(appInfo.publicSourceDir, "${packageName}.vastub") to update all existing resources.
 * <br/>
 *
 * see android.webkit.WebViewDelegate.addWebViewAssetPath(Context)
 */
@TargetApi(Build.VERSION_CODES.N)
private static Resources createResourcesForN(Context context, String packageName, File apk) throws Exception {
    long startTime = System.currentTimeMillis();
    String newAssetPath = apk.getAbsolutePath();
    ApplicationInfo info = context.getApplicationInfo();
    String baseResDir = info.publicSourceDir;
    
    info.splitSourceDirs = append(info.splitSourceDirs, newAssetPath);
    LoadedApk loadedApk = Reflector.with(context).field("mPackageInfo").get();

    Reflector rLoadedApk = Reflector.with(loadedApk).field("mSplitResDirs");
    String[] splitResDirs = rLoadedApk.get();
    rLoadedApk.set(append(splitResDirs, newAssetPath));

    final android.app.ResourcesManager resourcesManager = android.app.ResourcesManager.getInstance();
    ArrayMap<ResourcesKey, WeakReference<ResourcesImpl>> originalMap = Reflector.with(resourcesManager).field("mResourceImpls").get();

    synchronized (resourcesManager) {
        HashMap<ResourcesKey, WeakReference<ResourcesImpl>> resolvedMap = new HashMap<>();

        if (Build.VERSION.SDK_INT >= 28
            || (Build.VERSION.SDK_INT == 27 && Build.VERSION.PREVIEW_SDK_INT != 0)) { // P Preview
            ResourcesManagerCompatForP.resolveResourcesImplMap(originalMap, resolvedMap, context, loadedApk);

        } else {
            ResourcesManagerCompatForN.resolveResourcesImplMap(originalMap, resolvedMap, baseResDir, newAssetPath);
        }

        originalMap.clear();
        originalMap.putAll(resolvedMap);
    }

    android.app.ResourcesManager.getInstance().appendLibAssetForMainAssetPath(baseResDir, packageName + ".vastub");

    Resources newResources = context.getResources();

    // lastly, sync all LoadedPlugin to newResources
    for (LoadedPlugin plugin : PluginManager.getInstance(context).getAllLoadedPlugins()) {
        plugin.updateResources(newResources);
    }

    Log.d(TAG, "createResourcesForN cost time: +" + (System.currentTimeMillis() - startTime) + "ms");
    return newResources;
}
 
Example #16
Source File: LoadedPlugin.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public PluginManager getPluginManager() {
    return this.mPluginManager;
}
 
Example #17
Source File: LoadedPlugin.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public LoadedPlugin(PluginManager pluginManager, Context context, File apk) throws Exception {
    this.mPluginManager = pluginManager;
    this.mHostContext = context;
    this.mLocation = apk.getAbsolutePath();
    this.mPackage = PackageParserCompat.parsePackage(context, apk, PackageParser.PARSE_MUST_BE_APK);
    this.mPackage.applicationInfo.metaData = this.mPackage.mAppMetaData;
    this.mPackageInfo = new PackageInfo();
    this.mPackageInfo.applicationInfo = this.mPackage.applicationInfo;
    this.mPackageInfo.applicationInfo.sourceDir = apk.getAbsolutePath();

    if (Build.VERSION.SDK_INT >= 28
        || (Build.VERSION.SDK_INT == 27 && Build.VERSION.PREVIEW_SDK_INT != 0)) { // Android P Preview
        try {
            this.mPackageInfo.signatures = this.mPackage.mSigningDetails.signatures;
        } catch (Throwable e) {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
            this.mPackageInfo.signatures = info.signatures;
        }
    } else {
        this.mPackageInfo.signatures = this.mPackage.mSignatures;
    }
    
    this.mPackageInfo.packageName = this.mPackage.packageName;
    if (pluginManager.getLoadedPlugin(mPackageInfo.packageName) != null) {
        throw new RuntimeException("plugin has already been loaded : " + mPackageInfo.packageName);
    }
    this.mPackageInfo.versionCode = this.mPackage.mVersionCode;
    this.mPackageInfo.versionName = this.mPackage.mVersionName;
    this.mPackageInfo.permissions = new PermissionInfo[0];
    this.mPackageManager = createPluginPackageManager();
    this.mPluginContext = createPluginContext(null);
    this.mNativeLibDir = getDir(context, Constants.NATIVE_DIR);
    this.mPackage.applicationInfo.nativeLibraryDir = this.mNativeLibDir.getAbsolutePath();
    this.mResources = createResources(context, getPackageName(), apk);
    this.mClassLoader = createClassLoader(context, apk, this.mNativeLibDir, context.getClassLoader());

    tryToCopyNativeLib(apk);

    // Cache instrumentations
    Map<ComponentName, InstrumentationInfo> instrumentations = new HashMap<ComponentName, InstrumentationInfo>();
    for (PackageParser.Instrumentation instrumentation : this.mPackage.instrumentation) {
        instrumentations.put(instrumentation.getComponentName(), instrumentation.info);
    }
    this.mInstrumentationInfos = Collections.unmodifiableMap(instrumentations);
    this.mPackageInfo.instrumentation = instrumentations.values().toArray(new InstrumentationInfo[instrumentations.size()]);

    // Cache activities
    Map<ComponentName, ActivityInfo> activityInfos = new HashMap<ComponentName, ActivityInfo>();
    for (PackageParser.Activity activity : this.mPackage.activities) {
        activity.info.metaData = activity.metaData;
        activityInfos.put(activity.getComponentName(), activity.info);
    }
    this.mActivityInfos = Collections.unmodifiableMap(activityInfos);
    this.mPackageInfo.activities = activityInfos.values().toArray(new ActivityInfo[activityInfos.size()]);

    // Cache services
    Map<ComponentName, ServiceInfo> serviceInfos = new HashMap<ComponentName, ServiceInfo>();
    for (PackageParser.Service service : this.mPackage.services) {
        serviceInfos.put(service.getComponentName(), service.info);
    }
    this.mServiceInfos = Collections.unmodifiableMap(serviceInfos);
    this.mPackageInfo.services = serviceInfos.values().toArray(new ServiceInfo[serviceInfos.size()]);

    // Cache providers
    Map<String, ProviderInfo> providers = new HashMap<String, ProviderInfo>();
    Map<ComponentName, ProviderInfo> providerInfos = new HashMap<ComponentName, ProviderInfo>();
    for (PackageParser.Provider provider : this.mPackage.providers) {
        providers.put(provider.info.authority, provider.info);
        providerInfos.put(provider.getComponentName(), provider.info);
    }
    this.mProviders = Collections.unmodifiableMap(providers);
    this.mProviderInfos = Collections.unmodifiableMap(providerInfos);
    this.mPackageInfo.providers = providerInfos.values().toArray(new ProviderInfo[providerInfos.size()]);

    // Register broadcast receivers dynamically
    Map<ComponentName, ActivityInfo> receivers = new HashMap<ComponentName, ActivityInfo>();
    for (PackageParser.Activity receiver : this.mPackage.receivers) {
        receivers.put(receiver.getComponentName(), receiver.info);

        BroadcastReceiver br = BroadcastReceiver.class.cast(getClassLoader().loadClass(receiver.getComponentName().getClassName()).newInstance());
        for (PackageParser.ActivityIntentInfo aii : receiver.intents) {
            this.mHostContext.registerReceiver(br, aii);
        }
    }
    this.mReceiverInfos = Collections.unmodifiableMap(receivers);
    this.mPackageInfo.receivers = receivers.values().toArray(new ActivityInfo[receivers.size()]);

    // try to invoke plugin's application
    invokeApplication();
}
 
Example #18
Source File: VAInstrumentation.java    From VirtualAPK with Apache License 2.0 4 votes vote down vote up
public VAInstrumentation(PluginManager pluginManager, Instrumentation base) {
    this.mPluginManager = pluginManager;
    this.mBase = base;
}