Merge "Always include uid as part of recentApp key" into qt-dev
am: 08ef7bd52b

Change-Id: I2f30889666946880c3331b1727ce7b8e0de5658d
diff --git a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
index 1fe7e7d..d5e1723 100644
--- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
+++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
@@ -79,7 +79,6 @@
     private final PackageManager mPm;
     private final NotificationBackend mNotificationBackend;
     private IUsageStatsManager mUsageStatsManager;
-    private final int mUserId;
     private final IconDrawableFactory mIconDrawableFactory;
 
     private Calendar mCal;
@@ -104,7 +103,6 @@
             ApplicationsState appState, Fragment host) {
         super(context);
         mIconDrawableFactory = IconDrawableFactory.newInstance(context);
-        mUserId = UserHandle.myUserId();
         mPm = context.getPackageManager();
         mHost = host;
         mApplicationsState = appState;
@@ -177,7 +175,6 @@
                 e.printStackTrace();
             }
             if (events != null) {
-
                 ArrayMap<String, NotifyingApp> aggregatedStats = new ArrayMap<>();
 
                 UsageEvents.Event event = new UsageEvents.Event();
@@ -205,7 +202,8 @@
         }
     }
 
-    private static String getKey(int userId, String pkg) {
+    @VisibleForTesting
+    static String getKey(int userId, String pkg) {
         return userId + "|" + pkg;
     }
 
@@ -252,12 +250,13 @@
             }
 
             boolean rebindPref = true;
-            NotificationAppPreference pref = appPreferences.remove(pkgName);
+            NotificationAppPreference pref = appPreferences.remove(getKey(app.getUserId(),
+                    pkgName));
             if (pref == null) {
                 pref = new NotificationAppPreference(prefContext);
                 rebindPref = false;
             }
-            pref.setKey(pkgName);
+            pref.setKey(getKey(app.getUserId(), pkgName));
             pref.setTitle(appEntry.label);
             pref.setIcon(mIconDrawableFactory.getBadgedIcon(appEntry.info));
             pref.setIconSize(TwoTargetPreference.ICON_SIZE_SMALL);
@@ -267,11 +266,11 @@
             Bundle args = new Bundle();
             args.putString(AppInfoBase.ARG_PACKAGE_NAME, pkgName);
             args.putInt(AppInfoBase.ARG_PACKAGE_UID, appEntry.info.uid);
-
             pref.setIntent(new SubSettingLauncher(mHost.getActivity())
                     .setDestination(AppNotificationSettings.class.getName())
                     .setTitleRes(R.string.notifications_title)
                     .setArguments(args)
+                    .setUserHandle(new UserHandle(UserHandle.getUserId(appEntry.info.uid)))
                     .setSourceMetricsCategory(
                             SettingsEnums.MANAGE_APPLICATIONS_NOTIFICATIONS)
                     .toIntent());
@@ -301,11 +300,11 @@
         int count = 0;
         for (NotifyingApp app : mApps) {
             final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(
-                    app.getPackage(), mUserId);
+                    app.getPackage(), app.getUserId());
             if (appEntry == null) {
                 continue;
             }
-            if (!shouldIncludePkgInRecents(app.getPackage())) {
+            if (!shouldIncludePkgInRecents(app.getPackage(), app.getUserId())) {
                 continue;
             }
             displayableApps.add(app);
@@ -321,14 +320,14 @@
     /**
      * Whether or not the app should be included in recent list.
      */
-    private boolean shouldIncludePkgInRecents(String pkgName) {
+    private boolean shouldIncludePkgInRecents(String pkgName, int userId) {
         final Intent launchIntent = new Intent().addCategory(Intent.CATEGORY_LAUNCHER)
                 .setPackage(pkgName);
 
         if (mPm.resolveActivity(launchIntent, 0) == null) {
             // Not visible on launcher -> likely not a user visible app, skip if non-instant.
             final ApplicationsState.AppEntry appEntry =
-                    mApplicationsState.getEntry(pkgName, mUserId);
+                    mApplicationsState.getEntry(pkgName, userId);
             if (appEntry == null || appEntry.info == null || !AppUtils.isInstant(appEntry.info)) {
                 Log.d(TAG, "Not a user visible or instant app, skipping " + pkgName);
                 return false;
diff --git a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
index d47d125..93bd8dc 100644
--- a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
@@ -168,7 +168,6 @@
 
     @Test
     public void display_showRecents() throws Exception {
-
         List<Event> events = new ArrayList<>();
         Event app = new Event();
         app.mEventType = Event.NOTIFICATION_INTERRUPTION;
@@ -262,8 +261,12 @@
         ArgumentCaptor<Preference> prefCaptor = ArgumentCaptor.forClass(Preference.class);
         verify(mCategory, times(2)).addPreference(prefCaptor.capture());
         List<Preference> prefs = prefCaptor.getAllValues();
-        assertThat(prefs.get(1).getKey()).isEqualTo(app.getPackageName());
-        assertThat(prefs.get(0).getKey()).isEqualTo(app1.getPackageName());
+        assertThat(prefs.get(1).getKey()).isEqualTo(
+                RecentNotifyingAppsPreferenceController.getKey(UserHandle.myUserId(),
+                        app.getPackageName()));
+        assertThat(prefs.get(0).getKey()).isEqualTo(
+                RecentNotifyingAppsPreferenceController.getKey(UserHandle.myUserId(),
+                        app1.getPackageName()));
     }
 
     @Test