Expose create uri as user as system api.

Bug: 179659160
Test: atest ContentProviderTest
Change-Id: I86edc2d4fa0911366a91170819140b274f5d6480
This commit is contained in:
Corina
2021-03-04 10:29:28 +00:00
parent 9ce91b84e9
commit 3fcaa5218c
3 changed files with 54 additions and 0 deletions

View File

@@ -55,6 +55,10 @@ package android.app.usage {
package android.content {
public abstract class ContentProvider implements android.content.ComponentCallbacks2 {
method @NonNull public static android.net.Uri createContentUriAsUser(@NonNull android.net.Uri, @NonNull android.os.UserHandle);
}
public abstract class Context {
method @NonNull public android.os.UserHandle getUser();
}

View File

@@ -2621,6 +2621,48 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
return !TextUtils.isEmpty(uri.getUserInfo());
}
/**
* Returns the given content URI explicitly associated with the given {@link UserHandle}.
*
* @param contentUri The content URI to be associated with a user handle.
* @param userHandle The user handle with which to associate the URI.
*
* @throws IllegalArgumentException if
* <ul>
* <li>the given URI is not content URI (a content URI has {@link Uri#getScheme} equal to
* {@link ContentResolver.SCHEME_CONTENT}) or</li>
* <li>the given URI is already explicitly associated with a {@link UserHandle}, which is
* different than the given one.</li>
* </ul>
*
* @hide
*/
@NonNull
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static Uri createContentUriAsUser(
@NonNull Uri contentUri, @NonNull UserHandle userHandle) {
if (!ContentResolver.SCHEME_CONTENT.equals(contentUri.getScheme())) {
throw new IllegalArgumentException(String.format(
"Given URI [%s] is not a content URI: ", contentUri));
}
int userId = userHandle.getIdentifier();
if (uriHasUserId(contentUri)) {
if (String.valueOf(userId).equals(contentUri.getUserInfo())) {
return contentUri;
}
throw new IllegalArgumentException(String.format(
"Given URI [%s] already has a user ID, different from given user handle [%s]",
contentUri,
userId));
}
Uri.Builder builder = contentUri.buildUpon();
builder.encodedAuthority(
"" + userHandle.getIdentifier() + "@" + contentUri.getEncodedAuthority());
return builder.build();
}
/** @hide */
@UnsupportedAppUsage
public static Uri maybeAddUserId(Uri uri, int userId) {

View File

@@ -23,6 +23,7 @@ import static org.mockito.Mockito.withSettings;
import android.content.pm.ApplicationInfo;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import android.os.UserHandle;
import androidx.test.runner.AndroidJUnit4;
@@ -86,4 +87,11 @@ public class ContentProviderTest {
mCp.validateIncomingUri(
Uri.parse("content://com.example/foo/bar?foo=b//ar#foo=b//ar")));
}
@Test
public void testCreateContentUriAsUser() {
Uri uri = Uri.parse("content://com.example/foo/bar");
Uri expectedUri = Uri.parse("content://7@com.example/foo/bar");
assertEquals(expectedUri, ContentProvider.createContentUriAsUser(uri, UserHandle.of(7)));
}
}