Files
packages_apps_Settings/src/com/android/settings/applications/managedomainurls/DomainAppPreferenceController.java
Yi-Ling Chuang 93f945d516 Fix the flashing list on Opening link page
The bottom list flashes because it takes some time to create a
DomainAppPreference, where each perference takes around 6ms to load an
icon. When there are, that say, 30 apps installed, then it will take
around 180ms to be ready, which introduces the flashing issue.

In order to tackle icon loading, this CL applies the icon cache
mechanism to speed up the loading time.

Fixes: 222981986
Test: robotest
Change-Id: Ia8f7cbf5af5ea9f694c352ea40ccca47bedb175c
2022-04-29 19:29:02 +08:00

180 lines
5.8 KiB
Java

/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.managedomainurls;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.applications.intentpicker.AppLaunchSettings;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import java.util.ArrayList;
import java.util.Map;
public class DomainAppPreferenceController extends BasePreferenceController implements
ApplicationsState.Callbacks {
// constant value that can be used to check return code from sub activity.
private static final int INSTALLED_APP_DETAILS = 1;
private int mMetricsCategory;
private ApplicationsState mApplicationsState;
private ApplicationsState.Session mSession;
private ManageDomainUrls mFragment;
private PreferenceGroup mDomainAppList;
private Map<String, Preference> mPreferenceCache;
public DomainAppPreferenceController(Context context, String key) {
super(context, key);
mApplicationsState = ApplicationsState.getInstance(
(Application) mContext.getApplicationContext());
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mDomainAppList = screen.findPreference(getPreferenceKey());
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (preference instanceof DomainAppPreference) {
ApplicationsState.AppEntry entry = ((DomainAppPreference) preference).getEntry();
AppInfoBase.startAppInfoFragment(AppLaunchSettings.class,
mContext.getString(R.string.auto_launch_label),
entry.info.packageName, entry.info.uid, mFragment,
INSTALLED_APP_DETAILS, mMetricsCategory);
return true;
}
return false;
}
public void setFragment(ManageDomainUrls fragment) {
mFragment = fragment;
mMetricsCategory = fragment.getMetricsCategory();
mSession = mApplicationsState.newSession(this, mFragment.getSettingsLifecycle());
}
@Override
public void onRunningStateChanged(boolean running) {
}
@Override
public void onPackageListChanged() {
}
@Override
public void onRebuildComplete(ArrayList<AppEntry> apps) {
if (mContext == null) {
return;
}
// Preload top visible icons of app list.
AppUtils.preloadTopIcons(mContext, apps,
mContext.getResources().getInteger(R.integer.config_num_visible_app_icons));
rebuildAppList(mDomainAppList, apps);
}
@Override
public void onPackageIconChanged() {
}
@Override
public void onPackageSizeChanged(String packageName) {
}
@Override
public void onAllSizesComputed() {
}
@Override
public void onLauncherInfoChanged() {
}
@Override
public void onLoadEntriesCompleted() {
rebuild();
}
private void cacheAllPrefs(PreferenceGroup group) {
mPreferenceCache = new ArrayMap();
final int count = group.getPreferenceCount();
for (int i = 0; i < count; i++) {
Preference p = group.getPreference(i);
if (TextUtils.isEmpty(p.getKey())) {
continue;
}
mPreferenceCache.put(p.getKey(), p);
}
}
private Preference getCachedPreference(String key) {
return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
}
private void removeCachedPrefs(PreferenceGroup group) {
for (Preference p : mPreferenceCache.values()) {
group.removePreference(p);
}
mPreferenceCache = null;
}
private void rebuild() {
final ArrayList<AppEntry> apps = mSession.rebuild(
ApplicationsState.FILTER_WITH_DOMAIN_URLS, ApplicationsState.ALPHA_COMPARATOR);
if (apps != null) {
onRebuildComplete(apps);
}
}
private void rebuildAppList(PreferenceGroup group, ArrayList<AppEntry> apps) {
cacheAllPrefs(group);
final int size = apps.size();
final Context context = group.getContext();
for (int i = 0; i < size; i++) {
final AppEntry entry = apps.get(i);
final String key = entry.info.packageName + "|" + entry.info.uid;
DomainAppPreference preference = (DomainAppPreference) getCachedPreference(key);
if (preference == null) {
preference = new DomainAppPreference(context, entry);
preference.setKey(key);
group.addPreference(preference);
} else {
preference.reuse();
}
preference.setOrder(i);
}
removeCachedPrefs(group);
}
}