Merge "Improve launch time for Apps & notifications page"

This commit is contained in:
TreeHugger Robot
2019-02-26 08:50:20 +00:00
committed by Android (Google) Code Review
2 changed files with 57 additions and 19 deletions

View File

@@ -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;
@@ -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 {
@@ -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) {
@@ -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
@@ -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()) {
@@ -167,6 +176,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController
} }
} }
}.execute(); }.execute();
mIsFirstLaunch = false;
} }
@Override @Override
@@ -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();
@@ -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() {
@@ -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);
@@ -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++) {
@@ -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;
} }

View File

@@ -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;
@@ -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);
} }
@@ -157,13 +158,17 @@ public class RecentAppsPreferenceControllerTest {
} }
@Test @Test
public void displayPreferenceAndUpdateState_shouldRefreshUi() { public void displayPreference_shouldNotReloadData() {
doNothing().when(mController).refreshUi();
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateState(mScreen);
verify(mController, times(2)).refreshUi(); verify(mController, never()).reloadData();
}
@Test
public void displayPreference_shouldRefreshUi() {
mController.displayPreference(mScreen);
verify(mController).refreshUi();
} }
@Test @Test
@@ -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<>();
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);