Use SDK Sandbox process UserHandle for SDK context creation.
In some scenarios application.uid could be not set for RE-SDKs.
For thouse use cases current process (sdk_sandbox) UserHandle should be used.
Without this change SYSTEM (0) user will be used (-1 / PER_USER_RANGE)
Bug: 323267234
Test: CustomizedSdkContextTest from secondary user
Change-Id: I04527c8b89341020460cae1adb1b80269b68fbcd
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index b0f6c46..af56cb4 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -2626,12 +2626,18 @@
@Override
public Context createApplicationContext(ApplicationInfo application, int flags)
throws NameNotFoundException {
+ final UserHandle user = new UserHandle(UserHandle.getUserId(application.uid));
+ return createApplicationContextAsUser(application, flags, user);
+ }
+
+ private Context createApplicationContextAsUser(ApplicationInfo application, int flags,
+ UserHandle user) throws NameNotFoundException {
LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
flags | CONTEXT_REGISTER_PACKAGE);
if (pi != null) {
ContextImpl c = new ContextImpl(this, mMainThread, pi, ContextParams.EMPTY,
mAttributionSource.getAttributionTag(), mAttributionSource.getNext(), null,
- mToken, new UserHandle(UserHandle.getUserId(application.uid)), flags, null,
+ mToken, user, flags, null,
null, mDeviceId, mIsExplicitDeviceId);
final int displayId = getDisplayId();
@@ -2656,7 +2662,9 @@
throw new SecurityException("API can only be called from SdkSandbox process");
}
- ContextImpl ctx = (ContextImpl) createApplicationContext(sdkInfo, flags);
+ final UserHandle user = sdkInfo.uid >= 0
+ ? new UserHandle(UserHandle.getUserId(sdkInfo.uid)) : Process.myUserHandle();
+ ContextImpl ctx = (ContextImpl) createApplicationContextAsUser(sdkInfo, flags, user);
// Set sandbox app's context as the application context for sdk context
ctx.mPackageInfo.makeApplicationInner(/*forceDefaultAppClass=*/false,