Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5adc2669 authored by Fan Zhang's avatar Fan Zhang
Browse files

Do not clear category tile cache in every reload.

Bug: 31781480
Test: manual

The cache is useful when setting summary text on tiles. If tile cache is
cleared and rebuilt, summaryloader won't find existing tile to update
summary.

The cache should only be cleared when entire resource config changes,
tracked by InterestingConfigChnages.

Change-Id: I3afcaba30b835f59ffaad9c27564f0345af00d66
parent ad64a12b
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.util.ArrayMap;
import android.util.Log;
import android.util.Pair;

import com.android.settingslib.applications.InterestingConfigChanges;

import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -30,6 +32,7 @@ public class CategoryManager {
    private static final String TAG = "CategoryManager";

    private static CategoryManager sInstance;
    private final InterestingConfigChanges mInterestingConfigChanges;

    // Tile cache (key: <packageName, activityName>, value: tile)
    private final Map<Pair<String, String>, Tile> mTileByComponentCache;
@@ -39,16 +42,18 @@ public class CategoryManager {

    private List<DashboardCategory> mCategories;

    public static CategoryManager get() {
    public static CategoryManager get(Context context) {
        if (sInstance == null) {
            sInstance = new CategoryManager();
            sInstance = new CategoryManager(context);
        }
        return sInstance;
    }

    CategoryManager() {
    CategoryManager(Context context) {
        mTileByComponentCache = new ArrayMap<>();
        mCategoryByKeyMap = new ArrayMap<>();
        mInterestingConfigChanges = new InterestingConfigChanges();
        mInterestingConfigChanges.applyNewConfig(context.getResources());
    }

    public synchronized DashboardCategory getTilesByCategory(Context context, String categoryKey) {
@@ -67,8 +72,10 @@ public class CategoryManager {
    }

    public synchronized void reloadAllCategories(Context context) {
        final boolean forceClearCache = mInterestingConfigChanges.applyNewConfig(
                context.getResources());
        mCategories = null;
        tryInitCategories(context);
        tryInitCategories(context, forceClearCache);
    }

    public synchronized void updateCategoryFromBlacklist(Set<ComponentName> tileBlacklist) {
@@ -87,8 +94,16 @@ public class CategoryManager {
    }

    private synchronized void tryInitCategories(Context context) {
        // Keep cached tiles by default. The cache is only invalidated when InterestingConfigChange
        // happens.
        tryInitCategories(context, false /* forceClearCache */);
    }

    private synchronized void tryInitCategories(Context context, boolean forceClearCache) {
        if (mCategories == null) {
            if (forceClearCache) {
                mTileByComponentCache.clear();
            }
            mCategoryByKeyMap.clear();
            mCategories = TileUtils.getCategories(context, mTileByComponentCache,
                    false /* categoryDefinedInManifest */);
+2 −2
Original line number Diff line number Diff line
@@ -185,7 +185,7 @@ public class SettingsDrawerActivity extends Activity {
            return false;
        }
        if (isDashboardFeatureEnabled()) {
            final DashboardCategory homepageCategories = CategoryManager.get()
            final DashboardCategory homepageCategories = CategoryManager.get(this)
                    .getTilesByCategory(this, CategoryKey.CATEGORY_HOMEPAGE);
            return homepageCategories.containsComponent(componentName);
        } else {
@@ -429,7 +429,7 @@ public class SettingsDrawerActivity extends Activity {
        private final CategoryManager mCategoryManager;

        public CategoriesUpdateTask() {
            mCategoryManager = CategoryManager.get();
            mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this);
        }

        @Override
+2 −2
Original line number Diff line number Diff line
@@ -69,8 +69,8 @@ public class SettingsDrawerAdapter extends BaseAdapter {
    }

    public void updateHomepageCategories() {
        DashboardCategory category =
                CategoryManager.get().getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE);
        final DashboardCategory category = CategoryManager.get(mActivity)
                        .getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE);
        mItems.clear();
        // Spacer.
        mItems.add(null);