Loading src/com/android/settings/applications/RecentAppsPreferenceController.java +22 −13 Original line number Original line Diff line number Diff line Loading @@ -82,6 +82,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController Preference mAllAppPref; Preference mAllAppPref; @VisibleForTesting @VisibleForTesting Preference mDivider; Preference mDivider; @VisibleForTesting boolean mIsFirstLaunch; private final PackageManager mPm; private final PackageManager mPm; private final UsageStatsManager mUsageStatsManager; private final UsageStatsManager mUsageStatsManager; Loading @@ -93,6 +95,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController private Fragment mHost; private Fragment mHost; private Calendar mCal; private Calendar mCal; private List<UsageStats> mStats; private List<UsageStats> mStats; private List<UsageStats> mRecentApps; private boolean mHasRecentApps; private boolean mHasRecentApps; static { static { Loading @@ -115,6 +118,9 @@ public class RecentAppsPreferenceController extends BasePreferenceController mIconDrawableFactory = IconDrawableFactory.newInstance(mContext); mIconDrawableFactory = IconDrawableFactory.newInstance(mContext); mPowerManager = mContext.getSystemService(PowerManager.class); mPowerManager = mContext.getSystemService(PowerManager.class); mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); mRecentApps = new ArrayList<>(); mIsFirstLaunch = true; reloadData(); } } public void setFragment(Fragment fragment) { public void setFragment(Fragment fragment) { Loading @@ -123,8 +129,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController @Override @Override public int getAvailabilityStatus() { public int getAvailabilityStatus() { reloadData(); return mRecentApps.isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE; return getDisplayableRecentAppList().isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE; } } @Override @Override Loading Loading @@ -152,7 +157,11 @@ public class RecentAppsPreferenceController extends BasePreferenceController @Override @Override public void updateState(Preference preference) { public void updateState(Preference preference) { super.updateState(preference); super.updateState(preference); // In order to improve launch time, we don't load data again at first launch. if (!mIsFirstLaunch) { reloadData(); refreshUi(); refreshUi(); } // Show total number of installed apps as See all's summary. // Show total number of installed apps as See all's summary. new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON, new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON, mContext.getPackageManager()) { mContext.getPackageManager()) { Loading @@ -167,6 +176,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController } } } } }.execute(); }.execute(); mIsFirstLaunch = false; } } @Override @Override Loading @@ -177,11 +187,9 @@ public class RecentAppsPreferenceController extends BasePreferenceController @VisibleForTesting @VisibleForTesting void refreshUi() { void refreshUi() { reloadData(); if (mRecentApps != null && !mRecentApps.isEmpty()) { final List<UsageStats> recentApps = getDisplayableRecentAppList(); if (recentApps != null && !recentApps.isEmpty()) { mHasRecentApps = true; mHasRecentApps = true; displayRecentApps(recentApps); displayRecentApps(); } else { } else { mHasRecentApps = false; mHasRecentApps = false; displayOnlyAppInfo(); displayOnlyAppInfo(); Loading @@ -197,6 +205,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController : mUsageStatsManager.queryUsageStats( : mUsageStatsManager.queryUsageStats( UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(), UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(), System.currentTimeMillis()); System.currentTimeMillis()); updateDisplayableRecentAppList(); } } private void displayOnlyAppInfo() { private void displayOnlyAppInfo() { Loading @@ -206,10 +216,10 @@ public class RecentAppsPreferenceController extends BasePreferenceController mRecentAppsPreference.setVisible(false); mRecentAppsPreference.setVisible(false); } } private void displayRecentApps(List<UsageStats> recentApps) { private void displayRecentApps() { int showAppsCount = 0; int showAppsCount = 0; for (UsageStats stat : recentApps) { for (UsageStats stat : mRecentApps) { final AppEntityInfo appEntityInfoInfo = createAppEntity(stat); final AppEntityInfo appEntityInfoInfo = createAppEntity(stat); if (appEntityInfoInfo != null) { if (appEntityInfoInfo != null) { mAppEntitiesController.setAppEntity(showAppsCount++, appEntityInfoInfo); mAppEntitiesController.setAppEntity(showAppsCount++, appEntityInfoInfo); Loading Loading @@ -246,8 +256,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController .build(); .build(); } } private List<UsageStats> getDisplayableRecentAppList() { private void updateDisplayableRecentAppList() { final List<UsageStats> recentApps = new ArrayList<>(); mRecentApps.clear(); final Map<String, UsageStats> map = new ArrayMap<>(); final Map<String, UsageStats> map = new ArrayMap<>(); final int statCount = mStats.size(); final int statCount = mStats.size(); for (int i = 0; i < statCount; i++) { for (int i = 0; i < statCount; i++) { Loading @@ -273,13 +283,12 @@ public class RecentAppsPreferenceController extends BasePreferenceController if (appEntry == null) { if (appEntry == null) { continue; continue; } } recentApps.add(stat); mRecentApps.add(stat); count++; count++; if (count >= AppEntitiesHeaderController.MAXIMUM_APPS) { if (count >= AppEntitiesHeaderController.MAXIMUM_APPS) { break; break; } } } } return recentApps; } } Loading tests/robotests/src/com/android/settings/applications/RecentAppsPreferenceControllerTest.java +34 −5 Original line number Original line Diff line number Diff line Loading @@ -25,9 +25,9 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; Loading Loading @@ -146,6 +146,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.reloadData(); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); } } Loading @@ -157,13 +158,17 @@ public class RecentAppsPreferenceControllerTest { } } @Test @Test public void displayPreferenceAndUpdateState_shouldRefreshUi() { public void displayPreference_shouldNotReloadData() { doNothing().when(mController).refreshUi(); mController.displayPreference(mScreen); verify(mController, never()).reloadData(); } @Test public void displayPreference_shouldRefreshUi() { mController.displayPreference(mScreen); mController.displayPreference(mScreen); mController.updateState(mScreen); verify(mController, times(2)).refreshUi(); verify(mController).refreshUi(); } } @Test @Test Loading @@ -173,6 +178,25 @@ public class RecentAppsPreferenceControllerTest { assertThat(mController.mAppEntitiesController).isNotNull(); assertThat(mController.mAppEntitiesController).isNotNull(); } } @Test public void updateState_firstLaunch_shouldNotReloadData() { mController.mIsFirstLaunch = true; mController.updateState(mRecentAppsPreference); verify(mController, never()).reloadData(); } @Test public void updateState_afterFirstLaunch_shouldReloadDataAndRefreshUi() { mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); verify(mController).reloadData(); verify(mController).refreshUi(); } @Test @Test public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() { public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() { final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>(); Loading Loading @@ -203,6 +227,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -243,6 +268,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -274,6 +300,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -314,6 +341,7 @@ public class RecentAppsPreferenceControllerTest { // Make sure stat2 is considered an instant app. // Make sure stat2 is considered an instant app. ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", (InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info); (InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info); mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -389,6 +417,7 @@ public class RecentAppsPreferenceControllerTest { .thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo()); when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading
src/com/android/settings/applications/RecentAppsPreferenceController.java +22 −13 Original line number Original line Diff line number Diff line Loading @@ -82,6 +82,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController Preference mAllAppPref; Preference mAllAppPref; @VisibleForTesting @VisibleForTesting Preference mDivider; Preference mDivider; @VisibleForTesting boolean mIsFirstLaunch; private final PackageManager mPm; private final PackageManager mPm; private final UsageStatsManager mUsageStatsManager; private final UsageStatsManager mUsageStatsManager; Loading @@ -93,6 +95,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController private Fragment mHost; private Fragment mHost; private Calendar mCal; private Calendar mCal; private List<UsageStats> mStats; private List<UsageStats> mStats; private List<UsageStats> mRecentApps; private boolean mHasRecentApps; private boolean mHasRecentApps; static { static { Loading @@ -115,6 +118,9 @@ public class RecentAppsPreferenceController extends BasePreferenceController mIconDrawableFactory = IconDrawableFactory.newInstance(mContext); mIconDrawableFactory = IconDrawableFactory.newInstance(mContext); mPowerManager = mContext.getSystemService(PowerManager.class); mPowerManager = mContext.getSystemService(PowerManager.class); mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); mRecentApps = new ArrayList<>(); mIsFirstLaunch = true; reloadData(); } } public void setFragment(Fragment fragment) { public void setFragment(Fragment fragment) { Loading @@ -123,8 +129,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController @Override @Override public int getAvailabilityStatus() { public int getAvailabilityStatus() { reloadData(); return mRecentApps.isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE; return getDisplayableRecentAppList().isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE; } } @Override @Override Loading Loading @@ -152,7 +157,11 @@ public class RecentAppsPreferenceController extends BasePreferenceController @Override @Override public void updateState(Preference preference) { public void updateState(Preference preference) { super.updateState(preference); super.updateState(preference); // In order to improve launch time, we don't load data again at first launch. if (!mIsFirstLaunch) { reloadData(); refreshUi(); refreshUi(); } // Show total number of installed apps as See all's summary. // Show total number of installed apps as See all's summary. new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON, new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON, mContext.getPackageManager()) { mContext.getPackageManager()) { Loading @@ -167,6 +176,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController } } } } }.execute(); }.execute(); mIsFirstLaunch = false; } } @Override @Override Loading @@ -177,11 +187,9 @@ public class RecentAppsPreferenceController extends BasePreferenceController @VisibleForTesting @VisibleForTesting void refreshUi() { void refreshUi() { reloadData(); if (mRecentApps != null && !mRecentApps.isEmpty()) { final List<UsageStats> recentApps = getDisplayableRecentAppList(); if (recentApps != null && !recentApps.isEmpty()) { mHasRecentApps = true; mHasRecentApps = true; displayRecentApps(recentApps); displayRecentApps(); } else { } else { mHasRecentApps = false; mHasRecentApps = false; displayOnlyAppInfo(); displayOnlyAppInfo(); Loading @@ -197,6 +205,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController : mUsageStatsManager.queryUsageStats( : mUsageStatsManager.queryUsageStats( UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(), UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(), System.currentTimeMillis()); System.currentTimeMillis()); updateDisplayableRecentAppList(); } } private void displayOnlyAppInfo() { private void displayOnlyAppInfo() { Loading @@ -206,10 +216,10 @@ public class RecentAppsPreferenceController extends BasePreferenceController mRecentAppsPreference.setVisible(false); mRecentAppsPreference.setVisible(false); } } private void displayRecentApps(List<UsageStats> recentApps) { private void displayRecentApps() { int showAppsCount = 0; int showAppsCount = 0; for (UsageStats stat : recentApps) { for (UsageStats stat : mRecentApps) { final AppEntityInfo appEntityInfoInfo = createAppEntity(stat); final AppEntityInfo appEntityInfoInfo = createAppEntity(stat); if (appEntityInfoInfo != null) { if (appEntityInfoInfo != null) { mAppEntitiesController.setAppEntity(showAppsCount++, appEntityInfoInfo); mAppEntitiesController.setAppEntity(showAppsCount++, appEntityInfoInfo); Loading Loading @@ -246,8 +256,8 @@ public class RecentAppsPreferenceController extends BasePreferenceController .build(); .build(); } } private List<UsageStats> getDisplayableRecentAppList() { private void updateDisplayableRecentAppList() { final List<UsageStats> recentApps = new ArrayList<>(); mRecentApps.clear(); final Map<String, UsageStats> map = new ArrayMap<>(); final Map<String, UsageStats> map = new ArrayMap<>(); final int statCount = mStats.size(); final int statCount = mStats.size(); for (int i = 0; i < statCount; i++) { for (int i = 0; i < statCount; i++) { Loading @@ -273,13 +283,12 @@ public class RecentAppsPreferenceController extends BasePreferenceController if (appEntry == null) { if (appEntry == null) { continue; continue; } } recentApps.add(stat); mRecentApps.add(stat); count++; count++; if (count >= AppEntitiesHeaderController.MAXIMUM_APPS) { if (count >= AppEntitiesHeaderController.MAXIMUM_APPS) { break; break; } } } } return recentApps; } } Loading
tests/robotests/src/com/android/settings/applications/RecentAppsPreferenceControllerTest.java +34 −5 Original line number Original line Diff line number Diff line Loading @@ -25,9 +25,9 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify; Loading Loading @@ -146,6 +146,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.reloadData(); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); } } Loading @@ -157,13 +158,17 @@ public class RecentAppsPreferenceControllerTest { } } @Test @Test public void displayPreferenceAndUpdateState_shouldRefreshUi() { public void displayPreference_shouldNotReloadData() { doNothing().when(mController).refreshUi(); mController.displayPreference(mScreen); verify(mController, never()).reloadData(); } @Test public void displayPreference_shouldRefreshUi() { mController.displayPreference(mScreen); mController.displayPreference(mScreen); mController.updateState(mScreen); verify(mController, times(2)).refreshUi(); verify(mController).refreshUi(); } } @Test @Test Loading @@ -173,6 +178,25 @@ public class RecentAppsPreferenceControllerTest { assertThat(mController.mAppEntitiesController).isNotNull(); assertThat(mController.mAppEntitiesController).isNotNull(); } } @Test public void updateState_firstLaunch_shouldNotReloadData() { mController.mIsFirstLaunch = true; mController.updateState(mRecentAppsPreference); verify(mController, never()).reloadData(); } @Test public void updateState_afterFirstLaunch_shouldReloadDataAndRefreshUi() { mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); verify(mController).reloadData(); verify(mController).refreshUi(); } @Test @Test public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() { public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() { final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>(); Loading Loading @@ -203,6 +227,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -243,6 +268,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -274,6 +300,7 @@ public class RecentAppsPreferenceControllerTest { when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo; mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -314,6 +341,7 @@ public class RecentAppsPreferenceControllerTest { // Make sure stat2 is considered an instant app. // Make sure stat2 is considered an instant app. ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", (InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info); (InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info); mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading Loading @@ -389,6 +417,7 @@ public class RecentAppsPreferenceControllerTest { .thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo()); when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) .thenReturn(stats); .thenReturn(stats); mController.mIsFirstLaunch = false; mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference); Loading