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

The following examples show how to use android.content.ContentProvider#maybeAddUserId() . 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: MmsServiceBroker.java    From android_9.0.0_r45 with 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 2
Source File: InputContentInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Content URI with which the content can be obtained.
 */
@NonNull
public Uri getContentUri() {
    // Fix up the content URI when and only when the caller's user ID does not match the owner's
    // user ID.
    if (mContentUriOwnerUserId != UserHandle.myUserId()) {
        return ContentProvider.maybeAddUserId(mContentUri, mContentUriOwnerUserId);
    }
    return mContentUri;
}
 
Example 3
Source File: ContentProviderResult.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** @hide */
public ContentProviderResult(ContentProviderResult cpr, int userId) {
    uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
    count = cpr.count;
}
 
Example 4
Source File: CallLog.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static Uri addEntryAndRemoveExpiredEntries(Context context, UserManager userManager,
        UserHandle user, ContentValues values) {
    final ContentResolver resolver = context.getContentResolver();

    // Since we're doing this operation on behalf of an app, we only
    // want to use the actual "unlocked" state.
    final Uri uri = ContentProvider.maybeAddUserId(
            userManager.isUserUnlocked(user) ? CONTENT_URI : SHADOW_CONTENT_URI,
            user.getIdentifier());

    if (VERBOSE_LOG) {
        Log.v(LOG_TAG, String.format("Inserting to %s", uri));
    }

    try {
        // When cleaning up the call log, try to delete older call long entries on a per
        // PhoneAccount basis first.  There can be multiple ConnectionServices causing
        // the addition of entries in the call log.  With the introduction of Self-Managed
        // ConnectionServices, we want to ensure that a misbehaving self-managed CS cannot
        // spam the call log with its own entries, causing entries from Telephony to be
        // removed.
        final Uri result = resolver.insert(uri, values);
        if (values.containsKey(PHONE_ACCOUNT_ID)
                && !TextUtils.isEmpty(values.getAsString(PHONE_ACCOUNT_ID))
                && values.containsKey(PHONE_ACCOUNT_COMPONENT_NAME)
                && !TextUtils.isEmpty(values.getAsString(PHONE_ACCOUNT_COMPONENT_NAME))) {
            // Only purge entries for the same phone account.
            resolver.delete(uri, "_id IN " +
                    "(SELECT _id FROM calls"
                    + " WHERE " + PHONE_ACCOUNT_COMPONENT_NAME + " = ?"
                    + " AND " + PHONE_ACCOUNT_ID + " = ?"
                    + " ORDER BY " + DEFAULT_SORT_ORDER
                    + " LIMIT -1 OFFSET 500)", new String[] {
                    values.getAsString(PHONE_ACCOUNT_COMPONENT_NAME),
                    values.getAsString(PHONE_ACCOUNT_ID)
            });
        } else {
            // No valid phone account specified, so default to the old behavior.
            resolver.delete(uri, "_id IN " +
                    "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
                    + " LIMIT -1 OFFSET 500)", null);
        }

        return result;
    } catch (IllegalArgumentException e) {
        Log.w(LOG_TAG, "Failed to insert calllog", e);
        // Even though we make sure the target user is running and decrypted before calling
        // this method, there's a chance that the user just got shut down, in which case
        // we'll still get "IllegalArgumentException: Unknown URL content://call_log/calls".
        return null;
    }
}