From e04101be07b2225ee51f474d8f254d892fc4c6bc Mon Sep 17 00:00:00 2001 From: Antony Sargent Date: Wed, 24 Jan 2018 16:14:16 -0800 Subject: [PATCH] Prevent crash in recent app list In some situations if you have recently used an app and then uninstalled it, the display of recent apps in Settings->Apps & notifications would crash. This is due to a bug in recent CL that fixed b/71591298. Test: make -j64 RunSettingsRoboTests Change-Id: Id04b073b2617eeff0e188b10d3743496a9f70d5e Fixes: 72340364 --- .../RecentAppsPreferenceController.java | 2 +- ...centNotifyingAppsPreferenceController.java | 2 +- .../RecentAppsPreferenceControllerTest.java | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/applications/RecentAppsPreferenceController.java b/src/com/android/settings/applications/RecentAppsPreferenceController.java index 20c5581ae8e..38590817ae7 100644 --- a/src/com/android/settings/applications/RecentAppsPreferenceController.java +++ b/src/com/android/settings/applications/RecentAppsPreferenceController.java @@ -325,7 +325,7 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController // Not visible on launcher -> likely not a user visible app, skip if non-instant. final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId); - if (!AppUtils.isInstant(appEntry.info)) { + 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/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java index ef34a9b65e6..dbffc550d5e 100644 --- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java +++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java @@ -283,7 +283,7 @@ public class RecentNotifyingAppsPreferenceController extends AbstractPreferenceC // Not visible on launcher -> likely not a user visible app, skip if non-instant. final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId); - if (!AppUtils.isInstant(appEntry.info)) { + 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/applications/RecentAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/RecentAppsPreferenceControllerTest.java index bd57211a231..ed97fe774b6 100644 --- a/tests/robotests/src/com/android/settings/applications/RecentAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/applications/RecentAppsPreferenceControllerTest.java @@ -247,6 +247,33 @@ public class RecentAppsPreferenceControllerTest { assertThat(prefs.get(0).getKey()).isEqualTo(stat2.mPackageName); } + @Test + public void display_showRecentsWithNullAppEntryOrInfo() { + final List stats = new ArrayList<>(); + final UsageStats stat1 = new UsageStats(); + final UsageStats stat2 = new UsageStats(); + stat1.mLastTimeUsed = System.currentTimeMillis(); + stat1.mPackageName = "pkg.class"; + stats.add(stat1); + + stat2.mLastTimeUsed = System.currentTimeMillis(); + stat2.mPackageName = "pkg.class2"; + stats.add(stat2); + + // app1 has AppEntry with null info, app2 has null AppEntry. + mAppEntry.info = null; + when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) + .thenReturn(mAppEntry); + when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())) + .thenReturn(null); + + when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) + .thenReturn(stats); + + // We should not crash here. + mController.displayPreference(mScreen); + } + @Test public void display_hasRecentButNoneDisplayable_showAppInfo() { final List stats = new ArrayList<>();