Skip to content
Snippets Groups Projects
Commit b844001d authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Add Context.createContextAsUser()

Without it, apps (mainline modules) will need to use createPackageContext...,
which is a bit painful.

Bug: 142472686
Test: atest android.content.cts.ContextTest#testCreateContextAsUser
Change-Id: Id640e03862462724df1a4a3101f0b08faafba22f
parent b71657a6
No related branches found
No related tags found
No related merge requests found
...@@ -1358,8 +1358,9 @@ package android.content { ...@@ -1358,8 +1358,9 @@ package android.content {
   
public abstract class Context { public abstract class Context {
method @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) public boolean bindServiceAsUser(@RequiresPermission android.content.Intent, android.content.ServiceConnection, int, android.os.UserHandle); method @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) public boolean bindServiceAsUser(@RequiresPermission android.content.Intent, android.content.ServiceConnection, int, android.os.UserHandle);
method @NonNull public android.content.Context createContextAsUser(@NonNull android.os.UserHandle);
method public abstract android.content.Context createCredentialProtectedStorageContext(); method public abstract android.content.Context createCredentialProtectedStorageContext();
method public android.content.Context createPackageContextAsUser(String, int, android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException; method @NonNull public android.content.Context createPackageContextAsUser(@NonNull String, int, @NonNull android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException;
method @Nullable public abstract java.io.File getPreloadsFileCache(); method @Nullable public abstract java.io.File getPreloadsFileCache();
method public abstract boolean isCredentialProtectedStorage(); method public abstract boolean isCredentialProtectedStorage();
method public abstract void sendBroadcast(android.content.Intent, @Nullable String, @Nullable android.os.Bundle); method public abstract void sendBroadcast(android.content.Intent, @Nullable String, @Nullable android.os.Bundle);
......
...@@ -659,7 +659,8 @@ package android.content { ...@@ -659,7 +659,8 @@ package android.content {
} }
public abstract class Context { public abstract class Context {
method public android.content.Context createPackageContextAsUser(String, int, android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException; method @NonNull public android.content.Context createContextAsUser(@NonNull android.os.UserHandle);
method @NonNull public android.content.Context createPackageContextAsUser(@NonNull String, int, @NonNull android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException;
method public abstract android.view.Display getDisplay(); method public abstract android.view.Display getDisplay();
method public abstract int getDisplayId(); method public abstract int getDisplayId();
method public android.os.UserHandle getUser(); method public android.os.UserHandle getUser();
......
...@@ -2202,6 +2202,15 @@ class ContextImpl extends Context { ...@@ -2202,6 +2202,15 @@ class ContextImpl extends Context {
"Application package " + packageName + " not found"); "Application package " + packageName + " not found");
} }
@Override
public Context createContextAsUser(UserHandle user) {
try {
return createPackageContextAsUser(getPackageName(), mFlags, user);
} catch (NameNotFoundException e) {
throw new IllegalStateException("Own package not found: package=" + getPackageName());
}
}
@Override @Override
public Context createContextForSplit(String splitName) throws NameNotFoundException { public Context createContextForSplit(String splitName) throws NameNotFoundException {
if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) { if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
......
...@@ -5217,8 +5217,9 @@ public abstract class Context { ...@@ -5217,8 +5217,9 @@ public abstract class Context {
*/ */
@SystemApi @SystemApi
@TestApi @TestApi
@NonNull
public Context createPackageContextAsUser( public Context createPackageContextAsUser(
String packageName, @CreatePackageOptions int flags, UserHandle user) @NonNull String packageName, @CreatePackageOptions int flags, @NonNull UserHandle user)
throws PackageManager.NameNotFoundException { throws PackageManager.NameNotFoundException {
if (Build.IS_ENG) { if (Build.IS_ENG) {
throw new IllegalStateException("createPackageContextAsUser not overridden!"); throw new IllegalStateException("createPackageContextAsUser not overridden!");
...@@ -5226,6 +5227,23 @@ public abstract class Context { ...@@ -5226,6 +5227,23 @@ public abstract class Context {
return this; return this;
} }
/**
* Similar to {@link #createPackageContext(String, int)}, but for the own package with a
* different {@link UserHandle}. For example, {@link #getContentResolver()}
* will open any {@link Uri} as the given user.
*
* @hide
*/
@SystemApi
@TestApi
@NonNull
public Context createContextAsUser(@NonNull UserHandle user) {
if (Build.IS_ENG) {
throw new IllegalStateException("createContextAsUser not overridden!");
}
return this;
}
/** /**
* Creates a context given an {@link android.content.pm.ApplicationInfo}. * Creates a context given an {@link android.content.pm.ApplicationInfo}.
* *
......
...@@ -883,6 +883,12 @@ public class ContextWrapper extends Context { ...@@ -883,6 +883,12 @@ public class ContextWrapper extends Context {
return mBase.createPackageContextAsUser(packageName, flags, user); return mBase.createPackageContextAsUser(packageName, flags, user);
} }
/** @hide */
@Override
public Context createContextAsUser(UserHandle user) {
return mBase.createContextAsUser(user);
}
/** @hide */ /** @hide */
@Override @Override
@UnsupportedAppUsage @UnsupportedAppUsage
......
...@@ -756,6 +756,12 @@ public class MockContext extends Context { ...@@ -756,6 +756,12 @@ public class MockContext extends Context {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** {@hide} */
@Override
public Context createContextAsUser(UserHandle user) {
throw new UnsupportedOperationException();
}
/** {@hide} */ /** {@hide} */
@Override @Override
public int getUserId() { public int getUserId() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment