Search in sources :

Example 11 with LoadedPlugin

use of com.didi.virtualapk.internal.LoadedPlugin in project AndroidLife by CaMnter.

the class PluginUtil method hookActivityResources.

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) {
            ReflectUtil.setField(base.getClass(), base, "mResources", resources);
            // copy theme
            Resources.Theme theme = resources.newTheme();
            theme.setTo(activity.getTheme());
            int themeResource = (int) ReflectUtil.getField(ContextThemeWrapper.class, activity, "mThemeResource");
            theme.applyStyle(themeResource, true);
            ReflectUtil.setField(ContextThemeWrapper.class, activity, "mTheme", theme);
            ReflectUtil.setField(ContextThemeWrapper.class, activity, "mResources", resources);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Context(android.content.Context) LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) ContextThemeWrapper(android.view.ContextThemeWrapper) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 12 with LoadedPlugin

use of com.didi.virtualapk.internal.LoadedPlugin in project VirtualAPK by didi.

the class IContentProviderProxy method wrapperUri.

private void wrapperUri(Method method, Object[] args) {
    Uri uri = null;
    int index = 0;
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof Uri) {
                uri = (Uri) args[i];
                index = i;
                break;
            }
        }
    }
    Bundle bundleInCallMethod = null;
    if (method.getName().equals("call")) {
        bundleInCallMethod = getBundleParameter(args);
        if (bundleInCallMethod != null) {
            String uriString = bundleInCallMethod.getString(KEY_WRAPPER_URI);
            if (uriString != null) {
                uri = Uri.parse(uriString);
            }
        }
    }
    if (uri == null) {
        return;
    }
    PluginManager pluginManager = PluginManager.getInstance(mContext);
    ProviderInfo info = pluginManager.resolveContentProvider(uri.getAuthority(), 0);
    if (info != null) {
        String pkg = info.packageName;
        LoadedPlugin plugin = pluginManager.getLoadedPlugin(pkg);
        String pluginUri = Uri.encode(uri.toString());
        StringBuilder builder = new StringBuilder(PluginContentResolver.getUri(mContext));
        builder.append("/?plugin=" + plugin.getLocation());
        builder.append("&pkg=" + pkg);
        builder.append("&uri=" + pluginUri);
        Uri wrapperUri = Uri.parse(builder.toString());
        if (method.getName().equals("call")) {
            bundleInCallMethod.putString(KEY_WRAPPER_URI, wrapperUri.toString());
        } else {
            args[index] = wrapperUri;
        }
    }
}
Also used : PluginManager(com.didi.virtualapk.PluginManager) LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) ProviderInfo(android.content.pm.ProviderInfo) Bundle(android.os.Bundle) Uri(android.net.Uri)

Example 13 with LoadedPlugin

use of com.didi.virtualapk.internal.LoadedPlugin in project VirtualAPK by didi.

the class LocalService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (null == intent || !intent.hasExtra(EXTRA_TARGET) || !intent.hasExtra(EXTRA_COMMAND)) {
        return START_STICKY;
    }
    Intent target = intent.getParcelableExtra(EXTRA_TARGET);
    int command = intent.getIntExtra(EXTRA_COMMAND, 0);
    if (null == target || command <= 0) {
        return START_STICKY;
    }
    ComponentName component = target.getComponent();
    LoadedPlugin plugin = mPluginManager.getLoadedPlugin(component);
    // ClassNotFoundException when unmarshalling in Android 5.1
    target.setExtrasClassLoader(plugin.getClassLoader());
    switch(command) {
        case EXTRA_COMMAND_START_SERVICE:
            {
                ActivityThread mainThread = (ActivityThread) ReflectUtil.getActivityThread(getBaseContext());
                IApplicationThread appThread = mainThread.getApplicationThread();
                Service service;
                if (this.mPluginManager.getComponentsHandler().isServiceAvailable(component)) {
                    service = this.mPluginManager.getComponentsHandler().getService(component);
                } else {
                    try {
                        service = (Service) plugin.getClassLoader().loadClass(component.getClassName()).newInstance();
                        Application app = plugin.getApplication();
                        IBinder token = appThread.asBinder();
                        Method attach = service.getClass().getMethod("attach", Context.class, ActivityThread.class, String.class, IBinder.class, Application.class, Object.class);
                        IActivityManager am = mPluginManager.getActivityManager();
                        attach.invoke(service, plugin.getPluginContext(), mainThread, component.getClassName(), token, app, am);
                        service.onCreate();
                        this.mPluginManager.getComponentsHandler().rememberService(component, service);
                    } catch (Throwable t) {
                        return START_STICKY;
                    }
                }
                service.onStartCommand(target, 0, this.mPluginManager.getComponentsHandler().getServiceCounter(service).getAndIncrement());
                break;
            }
        case EXTRA_COMMAND_BIND_SERVICE:
            {
                ActivityThread mainThread = (ActivityThread) ReflectUtil.getActivityThread(getBaseContext());
                IApplicationThread appThread = mainThread.getApplicationThread();
                Service service = null;
                if (this.mPluginManager.getComponentsHandler().isServiceAvailable(component)) {
                    service = this.mPluginManager.getComponentsHandler().getService(component);
                } else {
                    try {
                        service = (Service) plugin.getClassLoader().loadClass(component.getClassName()).newInstance();
                        Application app = plugin.getApplication();
                        IBinder token = appThread.asBinder();
                        Method attach = service.getClass().getMethod("attach", Context.class, ActivityThread.class, String.class, IBinder.class, Application.class, Object.class);
                        IActivityManager am = mPluginManager.getActivityManager();
                        attach.invoke(service, plugin.getPluginContext(), mainThread, component.getClassName(), token, app, am);
                        service.onCreate();
                        this.mPluginManager.getComponentsHandler().rememberService(component, service);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
                try {
                    IBinder binder = service.onBind(target);
                    IBinder serviceConnection = PluginUtil.getBinder(intent.getExtras(), "sc");
                    IServiceConnection iServiceConnection = IServiceConnection.Stub.asInterface(serviceConnection);
                    if (Build.VERSION.SDK_INT >= 26) {
                        ReflectUtil.invokeNoException(IServiceConnection.class, iServiceConnection, "connected", new Class[] { ComponentName.class, IBinder.class, boolean.class }, new Object[] { component, binder, false });
                    } else {
                        iServiceConnection.connected(component, binder);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        case EXTRA_COMMAND_STOP_SERVICE:
            {
                Service service = this.mPluginManager.getComponentsHandler().forgetService(component);
                if (null != service) {
                    try {
                        service.onDestroy();
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to stop service " + service + ": " + e.toString());
                    }
                } else {
                    Log.i(TAG, component + " not found");
                }
                break;
            }
        case EXTRA_COMMAND_UNBIND_SERVICE:
            {
                Service service = this.mPluginManager.getComponentsHandler().forgetService(component);
                if (null != service) {
                    try {
                        service.onUnbind(target);
                        service.onDestroy();
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to unbind service " + service + ": " + e.toString());
                    }
                } else {
                    Log.i(TAG, component + " not found");
                }
                break;
            }
    }
    return START_STICKY;
}
Also used : Context(android.content.Context) IServiceConnection(android.app.IServiceConnection) Service(android.app.Service) Intent(android.content.Intent) ActivityThread(android.app.ActivityThread) Method(java.lang.reflect.Method) LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) IBinder(android.os.IBinder) IApplicationThread(android.app.IApplicationThread) ComponentName(android.content.ComponentName) Application(android.app.Application) IActivityManager(android.app.IActivityManager)

Example 14 with LoadedPlugin

use of com.didi.virtualapk.internal.LoadedPlugin in project VirtualAPK by didi.

the class PluginUtil method hookActivityResources.

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) {
            ReflectUtil.setField(base.getClass(), base, "mResources", resources);
            // copy theme
            Resources.Theme theme = resources.newTheme();
            theme.setTo(activity.getTheme());
            int themeResource = (int) ReflectUtil.getField(ContextThemeWrapper.class, activity, "mThemeResource");
            theme.applyStyle(themeResource, true);
            ReflectUtil.setField(ContextThemeWrapper.class, activity, "mTheme", theme);
            ReflectUtil.setField(ContextThemeWrapper.class, activity, "mResources", resources);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Context(android.content.Context) LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) ContextThemeWrapper(android.view.ContextThemeWrapper) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 15 with LoadedPlugin

use of com.didi.virtualapk.internal.LoadedPlugin in project VirtualAPK by didi.

the class PluginUtil method getTheme.

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 PluginUtil.selectDefaultTheme(0, Build.VERSION.SDK_INT);
}
Also used : LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) ActivityInfo(android.content.pm.ActivityInfo) ApplicationInfo(android.content.pm.ApplicationInfo)

Aggregations

LoadedPlugin (com.didi.virtualapk.internal.LoadedPlugin)15 Intent (android.content.Intent)5 Uri (android.net.Uri)5 ComponentName (android.content.ComponentName)4 Context (android.content.Context)4 ProviderInfo (android.content.pm.ProviderInfo)4 PluginManager (com.didi.virtualapk.PluginManager)4 File (java.io.File)4 ActivityThread (android.app.ActivityThread)2 Application (android.app.Application)2 IActivityManager (android.app.IActivityManager)2 IApplicationThread (android.app.IApplicationThread)2 IServiceConnection (android.app.IServiceConnection)2 Service (android.app.Service)2 ContentProvider (android.content.ContentProvider)2 OperationApplicationException (android.content.OperationApplicationException)2 ActivityInfo (android.content.pm.ActivityInfo)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 Resources (android.content.res.Resources)2 Bundle (android.os.Bundle)2