diff options
| author | Makoto Onuki <omakoto@google.com> | 2019-10-09 15:33:11 -0700 |
|---|---|---|
| committer | Makoto Onuki <omakoto@google.com> | 2019-10-10 15:58:20 +0000 |
| commit | b844001d6a8f0b10b2f379fd5bcdea8e896e7de7 (patch) | |
| tree | 03e5c008cb229f2d37240ed0b78d4223d1615b73 /core/java | |
| parent | b71657a60aaedf7771daeca015bf48d87cf81230 (diff) | |
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
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/app/ContextImpl.java | 9 | ||||
| -rw-r--r-- | core/java/android/content/Context.java | 20 | ||||
| -rw-r--r-- | core/java/android/content/ContextWrapper.java | 6 |
3 files changed, 34 insertions, 1 deletions
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index ef23d5e7a424..2381514a5e3a 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -2203,6 +2203,15 @@ class ContextImpl extends Context { } @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 public Context createContextForSplit(String splitName) throws NameNotFoundException { if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) { // All Splits are always loaded. diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 08817e05e0a5..2dde3ae5909c 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -5217,8 +5217,9 @@ public abstract class Context { */ @SystemApi @TestApi + @NonNull public Context createPackageContextAsUser( - String packageName, @CreatePackageOptions int flags, UserHandle user) + @NonNull String packageName, @CreatePackageOptions int flags, @NonNull UserHandle user) throws PackageManager.NameNotFoundException { if (Build.IS_ENG) { throw new IllegalStateException("createPackageContextAsUser not overridden!"); @@ -5227,6 +5228,23 @@ public abstract class Context { } /** + * 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}. * * @hide diff --git a/core/java/android/content/ContextWrapper.java b/core/java/android/content/ContextWrapper.java index 0859f97e81a1..f7cd51e7ffbc 100644 --- a/core/java/android/content/ContextWrapper.java +++ b/core/java/android/content/ContextWrapper.java @@ -885,6 +885,12 @@ public class ContextWrapper extends Context { /** @hide */ @Override + public Context createContextAsUser(UserHandle user) { + return mBase.createContextAsUser(user); + } + + /** @hide */ + @Override @UnsupportedAppUsage public Context createApplicationContext(ApplicationInfo application, int flags) throws PackageManager.NameNotFoundException { |
