Java Code Examples for android.content.ContentProvider

The following examples show how to use android.content.ContentProvider. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: AndroidComponentPlugin   Source File: ActivityThread.java    License: Apache License 2.0 6 votes vote down vote up
private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
        ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
    String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
    ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
            localProvider, holder);
    for (int i = 0; i < names.length; i++) {
        ProviderClientRecord existing = mProviderMap.get(names[i]);
        if (existing != null) {
            Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
                    + " already published as " + names[i]);
        } else {
            mProviderMap.put(names[i], pcr);
        }
    }
    return pcr;
}
 
Example 2
Source Project: android_9.0.0_r45   Source File: GrantedUriPermissions.java    License: Apache License 2.0 6 votes vote down vote up
private static GrantedUriPermissions grantUri(IActivityManager am, Uri uri,
        int sourceUid, String targetPackage, int targetUserId, int grantFlags, String tag,
        GrantedUriPermissions curPerms) {
    try {
        int sourceUserId = ContentProvider.getUserIdFromUri(uri,
                UserHandle.getUserId(sourceUid));
        uri = ContentProvider.getUriWithoutUserId(uri);
        if (curPerms == null) {
            curPerms = new GrantedUriPermissions(am, grantFlags, sourceUid, tag);
        }
        am.grantUriPermissionFromOwner(curPerms.mPermissionOwner, sourceUid, targetPackage,
                uri, grantFlags, sourceUserId, targetUserId);
        curPerms.mUris.add(uri);
    } catch (RemoteException e) {
        Slog.e("JobScheduler", "AM dead");
    }
    return curPerms;
}
 
Example 3
Source Project: android_9.0.0_r45   Source File: ClipboardService.java    License: Apache License 2.0 6 votes vote down vote up
private final void checkUriOwnerLocked(Uri uri, int sourceUid) {
    if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;

    final long ident = Binder.clearCallingIdentity();
    try {
        // This will throw SecurityException if caller can't grant
        mAm.checkGrantUriPermission(sourceUid, null,
                ContentProvider.getUriWithoutUserId(uri),
                Intent.FLAG_GRANT_READ_URI_PERMISSION,
                ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
    } catch (RemoteException ignored) {
        // Ignored because we're in same process
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example 4
Source Project: Spork   Source File: DefaultContextResolver.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public Context resolveContext(Object object) throws Exception {
	if (object instanceof View) {
		return ((View) object).getContext();
	} else if (object instanceof Fragment) {
		return ((Fragment) object).getActivity();
	} else if (object instanceof Context) {
		return (Context) object;
	} else if (object instanceof ContentProvider) {
		return ((ContentProvider) object).getContext();
	} else if (object instanceof ContextProvider) {
		return ((ContextProvider) object).getContext();
	} else if (object instanceof ViewProvider) {
		return ((ViewProvider) object).getView().getContext();
	} else {
		return null;
	}
}
 
Example 5
Source Project: android_9.0.0_r45   Source File: SlicePermissionManager.java    License: Apache License 2.0 5 votes vote down vote up
public void grantSliceAccess(String pkg, int userId, String providerPkg, int providerUser,
        Uri uri) {
    PkgUser pkgUser = new PkgUser(pkg, userId);
    PkgUser providerPkgUser = new PkgUser(providerPkg, providerUser);

    SliceClientPermissions client = getClient(pkgUser);
    client.grantUri(uri, providerPkgUser);

    SliceProviderPermissions provider = getProvider(providerPkgUser);
    provider.getOrCreateAuthority(ContentProvider.getUriWithoutUserId(uri).getAuthority())
            .addPkg(pkgUser);
}
 
Example 6
Source Project: issue-reporter-android   Source File: IssueReporterFragment.java    License: MIT License 5 votes vote down vote up
private String getAuthority(Context context, Class<? extends ContentProvider> providerClass) {
    PackageManager manager = context.getApplicationContext().getPackageManager();
    try {
        ProviderInfo providerInfo = manager.getProviderInfo(
                new ComponentName(context, providerClass), PackageManager.GET_META_DATA);
        return providerInfo.authority;
    } catch (PackageManager.NameNotFoundException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 7
Source Project: Neptune   Source File: ContentProviderProxy1.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Bundle call(@NonNull String method, String arg, Bundle extras) {
    if (extras == null || TextUtils.isEmpty(extras.getString(IntentConstant.EXTRA_WRAPPER_URI_KEY))) {
        return null;
    }
    Uri uri = Uri.parse(extras.getString(IntentConstant.EXTRA_WRAPPER_URI_KEY));
    ContentProvider provider = getContentProvider(uri);
    if (provider != null) {
        return provider.call(method, arg, extras);
    }
    return null;
}
 
Example 8
Source Project: springreplugin   Source File: PluginPitProviderBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@TargetApi(16)
public Cursor query(Uri uri, String[] projection,
                    String selection, String[] selectionArgs, String sortOrder,
                    CancellationSignal cancellationSignal) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.query(pu.transferredUri, projection, selection, selectionArgs, sortOrder, cancellationSignal);
}
 
Example 9
Source Project: Neptune   Source File: PluginLoadedApk.java    License: 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 10
Source Project: ACDD   Source File: ProviderProxy.java    License: MIT License 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.insert(uri, contentValues);
    }
    return null;
}
 
Example 11
Source Project: Ollie   Source File: OllieTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void initialize() {
	ContentProvider contentProvider = new OllieSampleProvider();
	contentProvider.onCreate();

	ShadowLog.stream = System.out;
	ShadowContentResolver.registerProvider("com.example.ollie", contentProvider);

	Ollie.with(Robolectric.application)
			.setName("OllieSample.db")
			.setLogLevel(LogLevel.FULL)
			.init();
}
 
Example 12
Source Project: VirtualAPK   Source File: RemoteContentProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.delete(pluginUri, selection, selectionArgs);
    }

    return 0;
}
 
Example 13
Source Project: Neptune   Source File: PluginLoadedApk.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 在插件中查找可以处理这个Uri的Provider
 */
public ContentProvider getContentProvider(Uri uri) {
    if (uri == null || TextUtils.isEmpty(uri.getAuthority())) {
        return null;
    }
    return mProviderMaps.get(uri.getAuthority());
}
 
Example 14
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) {
    try {
        ActivityManager.getService().revokeUriPermission(
                mMainThread.getApplicationThread(), targetPackage,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 15
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
    try {
        return ActivityManager.getService().checkUriPermission(
                ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
                resolveUserId(uri), null);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 16
Source Project: android-test   Source File: ProviderTestRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Allows to add additional ContentProvider and the corresponding authority for testing.
 * Similarly, {@link #setDatabaseFile}, {@link #setDatabaseCommands}, {@link
 * #setDatabaseCommandsFile}, and {@link #runDatabaseCommands} can be used for this
 * ContentProvider.
 *
 * @param providerClass The class of the added ContentProvider under test.
 * @param providerAuth The authority defined for the added ContentProvider under test.
 */
public <T extends ContentProvider> Builder addProvider(
    @NonNull Class<T> providerClass, @NonNull String providerAuth) {
  checkNotNull(providerClass);
  checkNotNull(providerAuth);
  checkState(providerClasses.size() > 0, "No existing provider yet while trying to add more");
  checkState(
      !providerClasses.containsKey(providerAuth),
      String.format("ContentProvider with authority %s already exists.", providerAuth));
  providerClasses.put(providerAuth, providerClass);
  return this;
}
 
Example 17
Source Project: android_9.0.0_r45   Source File: MmsServiceBroker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Modifies the Uri to contain the caller's userId, if necessary.
 * Grants the phone package on primary user permission to access the contentUri,
 * even if the caller is not in the primary user.
 *
 * @param contentUri The Uri to adjust
 * @param action The intent action used to find the associated carrier app
 * @param permission The permission to add
 * @return The adjusted Uri containing the calling userId.
 */
private Uri adjustUriForUserAndGrantPermission(Uri contentUri, String action,
        int permission) {
    final Intent grantIntent = new Intent();
    grantIntent.setData(contentUri);
    grantIntent.setFlags(permission);

    final int callingUid = Binder.getCallingUid();
    final int callingUserId = UserHandle.getCallingUserId();
    if (callingUserId != UserHandle.USER_SYSTEM) {
        contentUri = ContentProvider.maybeAddUserId(contentUri, callingUserId);
    }

    long token = Binder.clearCallingIdentity();
    try {
        LocalServices.getService(ActivityManagerInternal.class)
                .grantUriPermissionFromIntent(callingUid, PHONE_PACKAGE_NAME,
                        grantIntent, UserHandle.USER_SYSTEM);

        // Grant permission for the carrier app.
        Intent intent = new Intent(action);
        TelephonyManager telephonyManager =
            (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        List<String> carrierPackages = telephonyManager.getCarrierPackageNamesForIntent(
                intent);
        if (carrierPackages != null && carrierPackages.size() == 1) {
            LocalServices.getService(ActivityManagerInternal.class)
                    .grantUriPermissionFromIntent(callingUid, carrierPackages.get(0),
                            grantIntent, UserHandle.USER_SYSTEM);
        }
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    return contentUri;
}
 
Example 18
Source Project: springreplugin   Source File: PluginPitProviderBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onLowMemory() {
    for (ContentProvider cp : mHelper.mProviderAuthorityMap.values()) {
        cp.onLowMemory();
    }
    super.onLowMemory();
}
 
Example 19
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
     try {
        ActivityManagerNative.getDefault().grantUriPermission(
                mMainThread.getApplicationThread(), toPackage,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
    }
}
 
Example 20
Source Project: Android-Plugin-Framework   Source File: PluginLauncher.java    License: MIT License 5 votes vote down vote up
private static void removeProvider(String authority, Map map) {
	if (map == null || authority == null) {
		return;
	}
	Iterator<Map.Entry> iterator = map.entrySet().iterator();
	while(iterator.hasNext()) {
		Map.Entry entry = iterator.next();
		ContentProvider contentProvider = new HackActivityThreadProviderClientRecord(entry.getValue()).getProvider();
		if (contentProvider != null && authority.equals(new HackContentProvider(contentProvider).getAuthority())) {
			iterator.remove();
			LogUtil.e("remove plugin contentprovider from map for " + authority);
			break;
		}
	}
}
 
Example 21
Source Project: springreplugin   Source File: PluginPitProviderBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues values) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.insert(pu.transferredUri, values);
}
 
Example 22
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** @hide */
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
    try {
        return ActivityManagerNative.getDefault().checkUriPermission(
                ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
                resolveUserId(uri), callerToken);
    } catch (RemoteException e) {
        return PackageManager.PERMISSION_DENIED;
    }
}
 
Example 23
Source Project: android-test   Source File: ProviderArgs.java    License: Apache License 2.0 5 votes vote down vote up
/** Pass in the provider instance to hold a weak reference to it. */
public void setProviderRef(ContentProvider provider) {
  if (providerRef != null) {
    Log.w(
        TAG,
        String.format(
            "Reference to Provider instance " + "with authority %s already set", authority));
  }
  providerRef = new WeakReference<>(provider);
}
 
Example 24
Source Project: VirtualAPK   Source File: RemoteContentProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.query(pluginUri, projection, selection, selectionArgs, sortOrder);
    }

    return null;
}
 
Example 25
Source Project: VirtualAPK   Source File: RemoteContentProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.update(pluginUri, values, selection, selectionArgs);
    }

    return 0;
}
 
Example 26
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
     try {
        ActivityManager.getService().grantUriPermission(
                mMainThread.getApplicationThread(), toPackage,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 27
Source Project: android_9.0.0_r45   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
    try {
        return ActivityManager.getService().checkUriPermission(
                ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
                resolveUserId(uri), null);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 28
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
    try {
        return ActivityManager.getService().checkUriPermission(
                ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
                resolveUserId(uri), null);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 29
Source Project: AndroidComponentPlugin   Source File: ContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** @hide */
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
    try {
        return ActivityManager.getService().checkUriPermission(
                ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
                resolveUserId(uri), callerToken);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 30
Source Project: springreplugin   Source File: PluginPitProviderBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String getType(Uri uri) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.getType(pu.transferredUri);
}