Search in sources :

Example 1 with LoadedPlugin

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

the class MainActivity method onButtonClick.

public void onButtonClick(View v) {
    if (v.getId() == R.id.button) {
        final String pkg = "com.didi.virtualapk.demo";
        if (PluginManager.getInstance(this).getLoadedPlugin(pkg) == null) {
            Toast.makeText(this, "plugin [com.didi.virtualapk.demo] not loaded", Toast.LENGTH_SHORT).show();
            return;
        }
        // test Activity and Service
        Intent intent = new Intent();
        intent.setClassName(this, "com.didi.virtualapk.demo.aidl.BookManagerActivity");
        startActivity(intent);
        // test ContentProvider
        Uri bookUri = Uri.parse("content://com.didi.virtualapk.demo.book.provider/book");
        LoadedPlugin plugin = PluginManager.getInstance(this).getLoadedPlugin(pkg);
        bookUri = PluginContentResolver.wrapperUri(plugin, bookUri);
        Cursor bookCursor = getContentResolver().query(bookUri, new String[] { "_id", "name" }, null, null, null);
        while (bookCursor.moveToNext()) {
            int bookId = bookCursor.getInt(0);
            String bookName = bookCursor.getString(1);
            Log.d("ryg", "query book:" + bookId + ", " + bookName);
        }
        bookCursor.close();
    } else if (v.getId() == R.id.about) {
        showAbout();
    }
}
Also used : LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 2 with LoadedPlugin

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

the class RemoteService method onStartCommand.

@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) {
                e.printStackTrace();
            }
        }
    }
    return super.onStartCommand(intent, flags, startId);
}
Also used : LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) Intent(android.content.Intent) ComponentName(android.content.ComponentName) File(java.io.File)

Example 3 with LoadedPlugin

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

the class PluginManager method loadPlugin.

/**
 * load a plugin into memory, then invoke it's Application.
 * @param apk the file of plugin, should end with .apk
 * @throws Exception
 */
public void loadPlugin(File apk) throws Exception {
    if (null == apk) {
        throw new IllegalArgumentException("error : apk is null.");
    }
    if (!apk.exists()) {
        throw new FileNotFoundException(apk.getAbsolutePath());
    }
    LoadedPlugin plugin = LoadedPlugin.create(this, this.mContext, apk);
    if (null != plugin) {
        this.mPlugins.put(plugin.getPackageName(), plugin);
        // try to invoke plugin's application
        plugin.invokeApplication();
    } else {
        throw new RuntimeException("Can't load plugin which is invalid: " + apk.getAbsolutePath());
    }
}
Also used : LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with LoadedPlugin

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

the class RemoteContentProvider method getContentProvider.

private ContentProvider getContentProvider(final Uri uri) {
    final PluginManager pluginManager = PluginManager.getInstance(getContext());
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    final String auth = pluginUri.getAuthority();
    ContentProvider cachedProvider = sCachedProviders.get(auth);
    if (cachedProvider != null) {
        return cachedProvider;
    }
    synchronized (sCachedProviders) {
        LoadedPlugin plugin = pluginManager.getLoadedPlugin(uri.getQueryParameter(KEY_PKG));
        if (plugin == null) {
            try {
                pluginManager.loadPlugin(new File(uri.getQueryParameter(KEY_PLUGIN)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        final ProviderInfo providerInfo = pluginManager.resolveContentProvider(auth, 0);
        if (providerInfo != null) {
            RunUtil.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        LoadedPlugin loadedPlugin = pluginManager.getLoadedPlugin(uri.getQueryParameter(KEY_PKG));
                        ContentProvider contentProvider = (ContentProvider) Class.forName(providerInfo.name).newInstance();
                        contentProvider.attachInfo(loadedPlugin.getPluginContext(), providerInfo);
                        sCachedProviders.put(auth, contentProvider);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, true);
            return sCachedProviders.get(auth);
        }
    }
    return null;
}
Also used : PluginManager(com.didi.virtualapk.PluginManager) LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) ProviderInfo(android.content.pm.ProviderInfo) ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) File(java.io.File) OperationApplicationException(android.content.OperationApplicationException)

Example 5 with LoadedPlugin

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

the class PluginManager method loadPlugin.

/**
 * load a plugin into memory, then invoke it's Application.
 *
 * @param apk the file of plugin, should end with .apk
 * @throws Exception Exception
 *
 * 加载 Apk 文件
 *
 * 将 Apk 文件加载成 VA 识别的 LoadedPlugin
 */
public void loadPlugin(File apk) throws Exception {
    if (null == apk) {
        throw new IllegalArgumentException("error : apk is null.");
    }
    if (!apk.exists()) {
        throw new FileNotFoundException(apk.getAbsolutePath());
    }
    LoadedPlugin plugin = LoadedPlugin.create(this, this.mContext, apk);
    if (null != plugin) {
        this.mPlugins.put(plugin.getPackageName(), plugin);
        // try to invoke plugin's application
        plugin.invokeApplication();
    } else {
        throw new RuntimeException("Can't load plugin which is invalid: " + apk.getAbsolutePath());
    }
}
Also used : LoadedPlugin(com.didi.virtualapk.internal.LoadedPlugin) FileNotFoundException(java.io.FileNotFoundException)

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