summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff DeCew <jeffdq@google.com>2021-08-16 14:32:13 -0400
committerJeff DeCew <jeffdq@google.com>2021-09-14 13:39:45 -0400
commit9d1e05c6b345a48f45afd451f602a52337047689 (patch)
treee3dc034ae497bc76e101485db6d9e870ed0decb8
parentc20398b8d1335dd3c78c44caa47864a54d06cd67 (diff)
Ensure package context used by System UI has a valid userId.
Fixes: 189924598 Bug: 194356170 Test: atest SystemUITests Test: atest android.service.notification.StatusBarNotificationTest#testGetPackageContext_worksWithUserAll Change-Id: Ic43a0301cb62ab27e276c2938012a27c3a9a6fff
-rw-r--r--core/java/android/service/notification/StatusBarNotification.java2
-rw-r--r--core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java18
2 files changed, 19 insertions, 1 deletions
diff --git a/core/java/android/service/notification/StatusBarNotification.java b/core/java/android/service/notification/StatusBarNotification.java
index 8e4a68e52697..40041486f6a6 100644
--- a/core/java/android/service/notification/StatusBarNotification.java
+++ b/core/java/android/service/notification/StatusBarNotification.java
@@ -436,7 +436,7 @@ public class StatusBarNotification implements Parcelable {
try {
ApplicationInfo ai = context.getPackageManager()
.getApplicationInfoAsUser(pkg, PackageManager.MATCH_UNINSTALLED_PACKAGES,
- getUserId());
+ getNormalizedUserId());
mContext = context.createApplicationContext(ai,
Context.CONTEXT_RESTRICTED);
} catch (PackageManager.NameNotFoundException e) {
diff --git a/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java b/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
index a5a98a98f0be..109b7ab73d6f 100644
--- a/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
+++ b/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
@@ -17,6 +17,8 @@
package android.service.notification;
import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNotSame;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertFalse;
@@ -51,6 +53,7 @@ public class StatusBarNotificationTest {
private final Context mMockContext = mock(Context.class);
@Mock
+ private Context mRealContext;
private PackageManager mPm;
private static final String PKG = "com.example.o";
@@ -75,6 +78,8 @@ public class StatusBarNotificationTest {
InstrumentationRegistry.getContext().getResources());
when(mMockContext.getPackageManager()).thenReturn(mPm);
when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
+
+ mRealContext = InstrumentationRegistry.getContext();
}
@Test
@@ -199,6 +204,19 @@ public class StatusBarNotificationTest {
}
+ @Test
+ public void testGetPackageContext_worksWithUserAll() {
+ String pkg = "com.android.systemui";
+ int uid = 1000;
+ Notification notification = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID).build();
+ StatusBarNotification sbn = new StatusBarNotification(
+ pkg, pkg, ID, TAG, uid, uid, notification, UserHandle.ALL, null, UID);
+ Context resultContext = sbn.getPackageContext(mRealContext);
+ assertNotNull(resultContext);
+ assertNotSame(mRealContext, resultContext);
+ assertEquals(pkg, resultContext.getPackageName());
+ }
+
private StatusBarNotification getNotification(String pkg, String group, String channelId) {
return getNotification(pkg, getNotificationBuilder(group, channelId));
}