Preference mAllAppPref;
@VisibleForTesting
Preference mDivider;
+ @VisibleForTesting
+ boolean mIsFirstLaunch;
private final PackageManager mPm;
private final UsageStatsManager mUsageStatsManager;
private Fragment mHost;
private Calendar mCal;
private List<UsageStats> mStats;
+ private List<UsageStats> mRecentApps;
private boolean mHasRecentApps;
static {
mIconDrawableFactory = IconDrawableFactory.newInstance(mContext);
mPowerManager = mContext.getSystemService(PowerManager.class);
mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class);
+ mRecentApps = new ArrayList<>();
+ mIsFirstLaunch = true;
+ reloadData();
}
public void setFragment(Fragment fragment) {
@Override
public int getAvailabilityStatus() {
- reloadData();
- return getDisplayableRecentAppList().isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE;
+ return mRecentApps.isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE;
}
@Override
@Override
public void updateState(Preference preference) {
super.updateState(preference);
- refreshUi();
+ // In order to improve launch time, we don't load data again at first launch.
+ if (!mIsFirstLaunch) {
+ reloadData();
+ refreshUi();
+ }
// Show total number of installed apps as See all's summary.
new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
mContext.getPackageManager()) {
}
}
}.execute();
+ mIsFirstLaunch = false;
}
@Override
@VisibleForTesting
void refreshUi() {
- reloadData();
- final List<UsageStats> recentApps = getDisplayableRecentAppList();
- if (recentApps != null && !recentApps.isEmpty()) {
+ if (mRecentApps != null && !mRecentApps.isEmpty()) {
mHasRecentApps = true;
- displayRecentApps(recentApps);
+ displayRecentApps();
} else {
mHasRecentApps = false;
displayOnlyAppInfo();
: mUsageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(),
System.currentTimeMillis());
+
+ updateDisplayableRecentAppList();
}
private void displayOnlyAppInfo() {
mRecentAppsPreference.setVisible(false);
}
- private void displayRecentApps(List<UsageStats> recentApps) {
+ private void displayRecentApps() {
int showAppsCount = 0;
- for (UsageStats stat : recentApps) {
+ for (UsageStats stat : mRecentApps) {
final AppEntityInfo appEntityInfoInfo = createAppEntity(stat);
if (appEntityInfoInfo != null) {
mAppEntitiesController.setAppEntity(showAppsCount++, appEntityInfoInfo);
.build();
}
- private List<UsageStats> getDisplayableRecentAppList() {
- final List<UsageStats> recentApps = new ArrayList<>();
+ private void updateDisplayableRecentAppList() {
+ mRecentApps.clear();
final Map<String, UsageStats> map = new ArrayMap<>();
final int statCount = mStats.size();
for (int i = 0; i < statCount; i++) {
if (appEntry == null) {
continue;
}
- recentApps.add(stat);
+ mRecentApps.add(stat);
count++;
if (count >= AppEntitiesHeaderController.MAXIMUM_APPS) {
break;
}
}
- return recentApps;
}
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.argThat;
-import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
mAppEntry.info = mApplicationInfo;
+ mController.reloadData();
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
}
@Test
- public void displayPreferenceAndUpdateState_shouldRefreshUi() {
- doNothing().when(mController).refreshUi();
+ public void displayPreference_shouldNotReloadData() {
+ mController.displayPreference(mScreen);
+
+ verify(mController, never()).reloadData();
+ }
+ @Test
+ public void displayPreference_shouldRefreshUi() {
mController.displayPreference(mScreen);
- mController.updateState(mScreen);
- verify(mController, times(2)).refreshUi();
+ verify(mController).refreshUi();
}
@Test
}
@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
public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() {
final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats();
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
mAppEntry.info = mApplicationInfo;
+ mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
mAppEntry.info = mApplicationInfo;
+ mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
mAppEntry.info = mApplicationInfo;
+ mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
// Make sure stat2 is considered an instant app.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info);
+ mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
.thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
+ mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);