Improve the latency of first entering Apps page

Root cause:
AppsPreferenceController will query the recent app usages twice on
the main thread.

Solution:
Set flag to ensure only triggering refreshUi() once when entering
the Apps page.

Also correct the preference key of apps.xml since we don’t have
duplicated preferences now.

Fixes: 183176038
Test: robotests & visual
Change-Id: Ia41ee1e4a1946b8122deca63e318d3915afcc426
This commit is contained in:
Yanting Yang
2021-06-25 03:43:14 +08:00
parent 54c1ff8489
commit 4124284bb7
3 changed files with 25 additions and 4 deletions

View File

@@ -55,7 +55,7 @@
settings:searchable="false"/>
<Preference
android:key="default_apps_v2"
android:key="default_apps"
android:title="@string/app_default_dashboard_title"
android:order="-996"
settings:controller="com.android.settings.applications.DefaultAppsPreferenceController">
@@ -85,7 +85,7 @@
</Preference>
<Preference
android:key="special_access_v2"
android:key="special_access"
android:fragment="com.android.settings.applications.specialaccess.SpecialAccessSettings"
android:title="@string/special_access"
android:order="20"

View File

@@ -69,6 +69,7 @@ public class AppDashboardFragment extends DashboardFragment {
use(SpecialAppAccessPreferenceController.class).setSession(getSettingsLifecycle());
mAppsPreferenceController = use(AppsPreferenceController.class);
mAppsPreferenceController.setFragment(this /* fragment */);
getSettingsLifecycle().addObserver(mAppsPreferenceController);
final HibernatedAppsPreferenceController hibernatedAppsPreferenceController =
use(HibernatedAppsPreferenceController.class);

View File

@@ -26,6 +26,9 @@ import android.util.ArrayMap;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@@ -45,7 +48,8 @@ import java.util.Map;
* This controller displays up to four recently used apps.
* If there is no recently used app, we only show up an "App Info" preference.
*/
public class AppsPreferenceController extends BasePreferenceController {
public class AppsPreferenceController extends BasePreferenceController implements
LifecycleObserver {
public static final int SHOW_RECENT_APP_COUNT = 4;
@@ -73,6 +77,7 @@ public class AppsPreferenceController extends BasePreferenceController {
Preference mSeeAllPref;
private Fragment mHost;
private boolean mInitialLaunch = false;
public AppsPreferenceController(Context context) {
super(context, KEY_RECENT_APPS_CATEGORY);
@@ -95,13 +100,24 @@ public class AppsPreferenceController extends BasePreferenceController {
super.displayPreference(screen);
initPreferences(screen);
refreshUi();
mInitialLaunch = true;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (!mInitialLaunch) {
refreshUi();
}
}
/**
* Called when the apps page pauses.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
mInitialLaunch = false;
}
@VisibleForTesting
void refreshUi() {
@@ -109,11 +125,15 @@ public class AppsPreferenceController extends BasePreferenceController {
mRecentApps = loadRecentApps();
if (!mRecentApps.isEmpty()) {
displayRecentApps();
mAllAppsInfoPref.setVisible(false);
mRecentAppsCategory.setVisible(true);
mGeneralCategory.setVisible(true);
mSeeAllPref.setVisible(true);
} else {
mAllAppsInfoPref.setVisible(true);
mRecentAppsCategory.setVisible(false);
mGeneralCategory.setVisible(false);
mSeeAllPref.setVisible(false);
}
}