Search in sources :

Example 11 with ContentProvider

use of android.content.ContentProvider 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 12 with ContentProvider

use of android.content.ContentProvider in project VirtualAPK by didi.

the class RemoteContentProvider method insert.

@Override
public Uri insert(Uri uri, ContentValues values) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.insert(pluginUri, values);
    }
    return uri;
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri)

Example 13 with ContentProvider

use of android.content.ContentProvider in project VirtualAPK by didi.

the class RemoteContentProvider method getType.

@Override
public String getType(Uri uri) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.getType(pluginUri);
    }
    return null;
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri)

Example 14 with ContentProvider

use of android.content.ContentProvider in project AndroidLife by CaMnter.

the class RemoteContentProvider method applyBatch.

/**
 * 反射替换 ContentProviderOperation 集合中所有的 ContentProviderOperation # Uri mUri
 *
 * 在 ContentProviderOperation 集合存在数据的情况下
 * 取出每个 ContentProviderOperation 对应的插件 ContentProvider
 * 手动调用 ContentProvider # applyBatch(...)
 *
 * @param operations operations
 * @return ContentProviderResult[]
 * @throws OperationApplicationException OperationApplicationException
 */
@NonNull
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    try {
        Field uriField = ContentProviderOperation.class.getDeclaredField("mUri");
        uriField.setAccessible(true);
        for (ContentProviderOperation operation : operations) {
            Uri pluginUri = Uri.parse(operation.getUri().getQueryParameter(KEY_URI));
            uriField.set(operation, pluginUri);
        }
    } catch (Exception e) {
        return new ContentProviderResult[0];
    }
    if (operations.size() > 0) {
        ContentProvider provider = getContentProvider(operations.get(0).getUri());
        if (provider != null) {
            return provider.applyBatch(operations);
        }
    }
    return new ContentProviderResult[0];
}
Also used : Field(java.lang.reflect.Field) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) OperationApplicationException(android.content.OperationApplicationException) NonNull(android.support.annotation.NonNull)

Example 15 with ContentProvider

use of android.content.ContentProvider in project AndroidLife by CaMnter.

the class RemoteContentProvider method getType.

/**
 * 获取插件 ContentProvider 后
 * 解析复合 Uri 协议,获取其中包含的插件 Uri 协议
 *
 * 手动调用 插件 ContentProvider # getType(...)
 *
 * @param uri uri
 * @return String
 */
@Override
public String getType(Uri uri) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.getType(pluginUri);
    }
    return null;
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri)

Aggregations

ContentProvider (android.content.ContentProvider)53 Uri (android.net.Uri)28 IContentProvider (android.content.IContentProvider)16 Test (org.junit.Test)12 Implementation (org.robolectric.annotation.Implementation)11 ContentValues (android.content.ContentValues)8 Context (android.content.Context)7 ApplicationInfo (android.content.pm.ApplicationInfo)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)7 RemoteException (android.os.RemoteException)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 ComponentName (android.content.ComponentName)6 IBinder (android.os.IBinder)6 BaseCursor (org.robolectric.fakes.BaseCursor)6 ContentProviderOperation (android.content.ContentProviderOperation)4 OperationApplicationException (android.content.OperationApplicationException)4 ProviderInfo (android.content.pm.ProviderInfo)3 Cursor (android.database.Cursor)3 MatrixCursor (android.database.MatrixCursor)3 ContentProviderResult (android.content.ContentProviderResult)2