CategoryManager.java revision 22a56d775d047a1926f2a6d9c855c802d561d487
1/** 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.settingslib.drawer; 17 18import android.content.ComponentName; 19import android.content.Context; 20import android.util.ArrayMap; 21import android.util.Log; 22import android.util.Pair; 23 24import com.android.settingslib.applications.InterestingConfigChanges; 25 26import java.util.List; 27import java.util.Map; 28import java.util.Set; 29 30public class CategoryManager { 31 32 private static final String TAG = "CategoryManager"; 33 34 private static CategoryManager sInstance; 35 36 private final InterestingConfigChanges mInterestingConfigChanges; 37 38 // Tile cache (key: <packageName, activityName>, value: tile) 39 private final Map<Pair<String, String>, Tile> mTileByComponentCache; 40 41 // Tile cache (key: category key, value: category) 42 private final Map<String, DashboardCategory> mCategoryByKeyMap; 43 44 private List<DashboardCategory> mCategories; 45 46 public static CategoryManager get() { 47 if (sInstance == null) { 48 sInstance = new CategoryManager(); 49 } 50 return sInstance; 51 } 52 53 CategoryManager() { 54 mInterestingConfigChanges = new InterestingConfigChanges(); 55 mTileByComponentCache = new ArrayMap<>(); 56 mCategoryByKeyMap = new ArrayMap<>(); 57 } 58 59 public DashboardCategory getTilesByCategory(Context context, String categoryKey) { 60 tryInitCategories(context); 61 62 final DashboardCategory category = mCategoryByKeyMap.get(categoryKey); 63 if (category == null) { 64 throw new IllegalStateException("Can't find category with key " + categoryKey); 65 } 66 return category; 67 } 68 69 public List<DashboardCategory> getCategories(Context context) { 70 tryInitCategories(context); 71 return mCategories; 72 } 73 74 public void reloadAllCategoriesForConfigChange(Context context) { 75 if (mInterestingConfigChanges.applyNewConfig(context.getResources())) { 76 mCategories = null; 77 tryInitCategories(context); 78 } 79 } 80 81 public void updateCategoryFromBlacklist(Set<ComponentName> tileBlacklist) { 82 if (mCategories == null) { 83 Log.w(TAG, "Category is null, skipping blacklist update"); 84 } 85 for (int i = 0; i < mCategories.size(); i++) { 86 DashboardCategory category = mCategories.get(i); 87 for (int j = 0; j < category.tiles.size(); j++) { 88 Tile tile = category.tiles.get(j); 89 if (tileBlacklist.contains(tile.intent.getComponent())) { 90 category.tiles.remove(j--); 91 } 92 } 93 } 94 } 95 96 private void tryInitCategories(Context context) { 97 if (mCategories == null) { 98 mTileByComponentCache.clear(); 99 mCategoryByKeyMap.clear(); 100 mCategories = TileUtils.getCategories(context, mTileByComponentCache, 101 false /* categoryDefinedInManifest */); 102 for (DashboardCategory category : mCategories) { 103 mCategoryByKeyMap.put(category.key, category); 104 } 105 } 106 } 107 108} 109