diff --git a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java index bd45884c87e..8e0807db89e 100644 --- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java +++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java @@ -29,7 +29,6 @@ import android.os.UserManager; import android.service.notification.NotifyingApp; import android.text.TextUtils; import android.util.ArrayMap; -import android.util.ArraySet; import android.util.IconDrawableFactory; import android.util.Slog; @@ -56,8 +55,6 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.Set; /** * This controller displays a list of recently used apps and a "See all" button. If there is @@ -149,9 +146,12 @@ public class RecentNotifyingAppsPreferenceController extends AbstractPreferenceC @VisibleForTesting void refreshUi(Context prefContext) { - ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 1)).setChecked(true); - ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 2)).setChecked(true); - ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 3)).setChecked(true); + for (int i = 1; i <= SHOW_RECENT_APP_COUNT; i++) { + PrimarySwitchPreference app = mCategory.findPreference(KEY_PLACEHOLDER + i); + if (app != null) { + app.setChecked(true); + } + } ThreadUtils.postOnBackgroundThread(() -> { reloadData(); final List recentApps = getDisplayableRecentAppList(); diff --git a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java index 983bf535bdd..f6eb93b7f29 100644 --- a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java @@ -220,6 +220,42 @@ public class RecentNotifyingAppsPreferenceControllerTest { verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp); } + @Test + public void display_noCrashIfLessThan3() throws Exception { + List events = new ArrayList<>(); + Event app = new Event(); + app.mEventType = Event.NOTIFICATION_INTERRUPTION; + app.mPackage = "a"; + app.mTimeStamp = System.currentTimeMillis(); + events.add(app); + ApplicationsState.AppEntry app1Entry = mock(ApplicationsState.AppEntry.class); + app1Entry.info = mApplicationInfo; + app1Entry.label = "app 1"; + + when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId())) + .thenReturn(app1Entry); + when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn( + new ResolveInfo()); + + UsageEvents usageEvents = getUsageEvents( + new String[] {app.getPackageName()}, + events); + when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString())) + .thenReturn(usageEvents); + + mAppEntry.info = mApplicationInfo; + + mController.displayPreference(mScreen); + + verify(mCategory).setTitle(R.string.recent_notifications); + // Only add app1 & app2. app3 skipped because it's invalid app. + assertThat(mApp1.getTitle()).isEqualTo(app1Entry.label); + + verify(mCategory).removePreferenceRecursively("app2"); + + mController.refreshUi(mContext); + } + @Test public void display_showRecentsWithInstantApp() throws Exception { List events = new ArrayList<>(); @@ -267,35 +303,12 @@ public class RecentNotifyingAppsPreferenceControllerTest { mController.displayPreference(mScreen); assertThat(mApp1.getTitle()).isEqualTo(appEntry.label); + assertThat(mApp1.getSummary()).isEqualTo("Just now"); assertThat(mApp2.getTitle()).isEqualTo(app1Entry.label); verify(mCategory).removePreferenceRecursively(mApp3.getKey()); } - @Test - public void display_showRecents_formatSummary() throws Exception { - List events = new ArrayList<>(); - Event app = new Event(); - app.mEventType = Event.NOTIFICATION_INTERRUPTION; - app.mPackage = "pkg.class"; - app.mTimeStamp = System.currentTimeMillis(); - events.add(app); - UsageEvents usageEvents = getUsageEvents(new String[] {"pkg.class"}, events); - when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString())) - .thenReturn(usageEvents); - - when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId())) - .thenReturn(mAppEntry); - when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn( - new ResolveInfo()); - - mAppEntry.info = mApplicationInfo; - - mController.displayPreference(mScreen); - - assertThat(mApp1.getSummary()).isEqualTo("Just now"); - } - @Test public void reloadData() throws Exception { when(mUserManager.getProfileIdsWithDisabled(0)).thenReturn(new int[] {0, 10});