Java Code Examples for android.content.ContentProvider#attachInfo()

The following examples show how to use android.content.ContentProvider#attachInfo() . 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: PluginLoadedApk.java    From Neptune with Apache License 2.0 5 votes vote down vote up
/**
 * 安装插件的Provider
 */
private void installContentProviders() {
    if (!isSupportProvider()) {
        PluginDebugLog.runtimeLog(TAG, "Not support provider for plugin " + mPluginPackageName);
        return;
    }

    mPluginContentResolver = new PluginContentResolver(mHostContext);
    Map<String, PluginPackageInfo.ProviderIntentInfo> mProviderIntentInfos =
            mPluginPackageInfo.getProviderIntentInfos();
    if (mProviderIntentInfos != null) {
        Set<Map.Entry<String, PluginPackageInfo.ProviderIntentInfo>> mEntries =
                mProviderIntentInfos.entrySet();
        for (Map.Entry<String, PluginPackageInfo.ProviderIntentInfo> mEntry : mEntries) {
            PluginPackageInfo.ProviderIntentInfo mProviderInfo = mEntry.getValue();
            if (mProviderInfo != null) {
                try {
                    ContentProvider provider = ContentProvider.class.cast(mPluginClassLoader.
                            loadClass(mProviderInfo.mInfo.name).newInstance());
                    if (provider != null) {
                        // 调用attachInfo方法
                        provider.attachInfo(mPluginAppContext, mProviderInfo.mInfo);
                        mProviderMaps.put(mProviderInfo.mInfo.authority, provider);
                    }
                } catch (Exception e) {
                    ErrorUtil.throwErrorIfNeed(e);
                }
            }
        }
    }
}
 
Example 2
Source File: ProviderTestRule.java    From android-test with Apache License 2.0 4 votes vote down vote up
private ContentProvider createProvider(
    String auth,
    Class<? extends ContentProvider> clazz,
    MockContentResolver resolver,
    Context context) {
  ContentProvider provider;

  try {
    provider = clazz.getConstructor().newInstance();
  } catch (NoSuchMethodException me) {
    Log.e(
        TAG,
        "NoSuchMethodException occurred when trying create new Instance for "
            + clazz.toString());
    throw new RuntimeException(me);
  } catch (InvocationTargetException ite) {
    Log.e(
        TAG,
        "InvocationTargetException occurred when trying create new Instance for "
            + clazz.toString());
    throw new RuntimeException(ite);
  } catch (IllegalAccessException iae) {
    Log.e(
        TAG,
        "IllegalAccessException occurred when trying create new Instance for "
            + clazz.toString());
    throw new RuntimeException(iae);
  } catch (InstantiationException ie) {
    Log.e(
        TAG,
        "InstantiationException occurred when trying create new Instance for "
            + clazz.toString());
    throw new RuntimeException(ie);
  }

  ProviderInfo providerInfo = new ProviderInfo();
  providerInfo.authority = auth;
  // attachInfo will call ContentProvider.onCreate(), so will refresh the context
  // used by ContentProvider.
  provider.attachInfo(context, providerInfo);
  resolver.addProvider(providerInfo.authority, provider);
  return provider;
}