android.content.ContentProvider Java Examples

The following examples show how to use android.content.ContentProvider. 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: ActivityThread.java    From AndroidComponentPlugin with 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 File: DefaultContextResolver.java    From Spork with 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 #3
Source File: ClipboardService.java    From android_9.0.0_r45 with 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 File: GrantedUriPermissions.java    From android_9.0.0_r45 with 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 #5
Source File: PluginPitProviderBase.java    From springreplugin with Apache License 2.0 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    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);
}
 
Example #6
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
@UnsupportedAppUsage
protected IContentProvider acquireProvider(Context context, String auth) {
    return mMainThread.acquireProvider(context,
            ContentProvider.getAuthorityWithoutUserId(auth),
            resolveUserIdFromAuthority(auth), true);
}
 
Example #7
Source File: HackActivityThreadProviderClientRecord.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
public ContentProvider getProvider() {
    Object o = RefInvoker.getField(instance, ClassName, Field_mProvider);
    if (o instanceof ContentProvider) {//maybe ContentProviderProxy
        return (ContentProvider) o;
    }
    return null;
}
 
Example #8
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
     try {
        ActivityManager.getService().revokeUriPermission(
                mMainThread.getApplicationThread(), null,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #9
Source File: AppListProvider.java    From island with Apache License 2.0 5 votes vote down vote up
protected static @NonNull <T extends AppListProvider> T getInstance(final Context context) {
	final String authority = context.getPackageName() + AUTHORITY_SUFFIX;		// Do not use BuildConfig.APPLICATION_ID
	final ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(authority);
	if (client == null) throw new IllegalStateException("AppListProvider not associated with authority: " + authority);
	try {
		final ContentProvider provider = client.getLocalContentProvider();
		if (provider == null)
			throw new IllegalStateException("android:multiprocess=\"true\" is required for this provider.");
		if (! (provider instanceof AppListProvider)) throw new IllegalArgumentException("");
		@SuppressWarnings("unchecked") final T casted = (T) provider;
		return casted;
	} finally { //noinspection deprecation
		client.release();
	}
}
 
Example #10
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 #11
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 #12
Source File: PluginPitProviderBase.java    From springreplugin with Apache License 2.0 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return -1;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return -1;
    }
    return cp.delete(pu.transferredUri, selection, selectionArgs);
}
 
Example #13
Source File: IssueReporterFragment.java    From issue-reporter-android with 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 #14
Source File: ContentProviderProxy1.java    From Neptune with Apache License 2.0 5 votes vote down vote up
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
    ContentProvider provider = getContentProvider(uri);
    if (provider != null) {
        Uri pluginUri = Uri.parse(uri.getQueryParameter(IntentConstant.EXTRA_TARGET_URI_KEY));
        return provider.delete(pluginUri, selection, selectionArgs);
    }
    return 0;
}
 
Example #15
Source File: ProviderProxy.java    From ACDD with MIT License 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection,
                    String selection, String[] selectionArgs, String sortOrder) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.query(uri, projection, selection, selectionArgs, sortOrder);
    }
    return null;
}
 
Example #16
Source File: ContentProviderProxy1.java    From Neptune with Apache License 2.0 5 votes vote down vote up
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
    ContentProvider provider = getContentProvider(uri);
    if (provider != null) {
        Uri pluginUri = Uri.parse(uri.getQueryParameter(IntentConstant.EXTRA_TARGET_URI_KEY));
        return provider.insert(pluginUri, values);
    }
    return null;
}
 
Example #17
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
     try {
        ActivityManager.getService().revokeUriPermission(
                mMainThread.getApplicationThread(), null,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #18
Source File: ProviderTestRule.java    From android-test with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
ProviderTestRule(
    Set<WeakReference<ContentProvider>> providersRef,
    Set<DatabaseArgs> databaseArgsSet,
    ContentResolver resolver,
    DelegatingContext context) {
  this.providersRef = providersRef;
  this.databaseArgsSet = databaseArgsSet;
  this.resolver = resolver;
  this.context = context;
}
 
Example #19
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 File: ContextImpl.java    From AndroidComponentPlugin with 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 #21
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 #22
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 #23
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
     try {
        ActivityManager.getService().revokeUriPermission(
                mMainThread.getApplicationThread(), null,
                ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #24
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 #25
Source File: ProviderProxy.java    From ACDD with MIT License 5 votes vote down vote up
/*****验证ContentProvider实现类是否加载*****/
protected ContentProvider getContentProvider() {
    if (this.mContentProvider != null) {
        return this.mContentProvider;
    }
    try {
        Class<?> loadClass = Globals.getClassLoader().loadClass(mTargetProvider);
        if (loadClass != null) {
            Constructor<?> constructor = loadClass.getConstructor();
            constructor.setAccessible(true);
            this.mContentProvider = (ContentProvider) constructor.newInstance();
            Field declaredField = ContentProvider.class.getDeclaredField("mContext");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getContext());
            declaredField = ContentProvider.class.getDeclaredField("mReadPermission");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getReadPermission());
            declaredField = ContentProvider.class.getDeclaredField("mWritePermission");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getWritePermission());
            declaredField = ContentProvider.class.getDeclaredField("mPathPermissions");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getPathPermissions());
            this.mContentProvider.onCreate();
            return this.mContentProvider;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #26
Source File: ContextImpl.java    From android_9.0.0_r45 with 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 File: NotificationRecord.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Note the presence of a {@link Uri} that should have permission granted to
 * whoever will be rendering it.
 * <p>
 * If the enqueuing app has the ability to grant access, it will be added to
 * {@link #mGrantableUris}. Otherwise, this will either log or throw
 * {@link SecurityException} depending on target SDK of enqueuing app.
 */
private void visitGrantableUri(Uri uri, boolean userOverriddenUri) {
    if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;

    // We can't grant Uri permissions from system
    final int sourceUid = sbn.getUid();
    if (sourceUid == android.os.Process.SYSTEM_UID) 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)));

        if (mGrantableUris == null) {
            mGrantableUris = new ArraySet<>();
        }
        mGrantableUris.add(uri);
    } catch (RemoteException ignored) {
        // Ignored because we're in same process
    } catch (SecurityException e) {
        if (!userOverriddenUri) {
            if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
                throw e;
            } else {
                Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
            }
        }
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #28
Source File: PluginPitProviderBase.java    From springreplugin with 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);
}
 
Example #29
Source File: ContextImpl.java    From AndroidComponentPlugin with 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 File: ContextImpl.java    From AndroidComponentPlugin with 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();
    }
}