| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.settings.dashboard; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| Doris Ling | 990460b | 2017-01-12 17:01:28 -0800 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 21 | import android.os.Bundle; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
| 23 | import android.util.ArrayMap; |
| Fan Zhang | 7f1a4b5 | 2016-10-12 12:35:52 -0700 | [diff] [blame] | 24 | import android.util.ArraySet; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| Fan Zhang | 1c61a58 | 2018-07-26 11:26:11 -0700 | [diff] [blame] | 27 | import androidx.annotation.VisibleForTesting; |
| 28 | import androidx.preference.Preference; |
| 29 | import androidx.preference.PreferenceManager; |
| 30 | import androidx.preference.PreferenceScreen; |
| 31 | |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 32 | import com.android.settings.SettingsPreferenceFragment; |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 33 | import com.android.settings.core.BasePreferenceController; |
| 34 | import com.android.settings.core.PreferenceControllerListHelper; |
| tmfang | d97fba5 | 2018-06-14 14:48:35 +0800 | [diff] [blame] | 35 | import com.android.settings.core.SettingsBaseActivity; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 36 | import com.android.settings.overlay.FeatureFactory; |
| 37 | import com.android.settings.search.Indexable; |
| Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 38 | import com.android.settingslib.core.AbstractPreferenceController; |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 39 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 40 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 41 | import com.android.settingslib.drawer.DashboardCategory; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 42 | import com.android.settingslib.drawer.Tile; |
| Doris Ling | 7087fd9 | 2017-07-17 15:04:36 -0700 | [diff] [blame] | 43 | import com.android.settingslib.drawer.TileUtils; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 44 | |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 46 | import java.util.Collection; |
| 47 | import java.util.List; |
| 48 | import java.util.Map; |
| Fan Zhang | 7f1a4b5 | 2016-10-12 12:35:52 -0700 | [diff] [blame] | 49 | import java.util.Set; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Base fragment for dashboard style UI containing a list of static and dynamic setting items. |
| 53 | */ |
| 54 | public abstract class DashboardFragment extends SettingsPreferenceFragment |
| tmfang | d97fba5 | 2018-06-14 14:48:35 +0800 | [diff] [blame] | 55 | implements SettingsBaseActivity.CategoryListener, Indexable, |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 56 | SummaryLoader.SummaryConsumer { |
| Fan Zhang | db1112a | 2016-10-18 12:58:31 -0700 | [diff] [blame] | 57 | private static final String TAG = "DashboardFragment"; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 58 | |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 59 | private final Map<Class, List<AbstractPreferenceController>> mPreferenceControllers = |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 60 | new ArrayMap<>(); |
| Fan Zhang | 7f1a4b5 | 2016-10-12 12:35:52 -0700 | [diff] [blame] | 61 | private final Set<String> mDashboardTilePrefKeys = new ArraySet<>(); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 62 | |
| Fan Zhang | b297abc | 2018-02-21 09:37:10 -0800 | [diff] [blame] | 63 | private DashboardFeatureProvider mDashboardFeatureProvider; |
| Fan Zhang | 9dc9c61 | 2016-11-11 13:23:21 -0800 | [diff] [blame] | 64 | private DashboardTilePlaceholderPreferenceController mPlaceholderPreferenceController; |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 65 | private boolean mListeningToCategoryChange; |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 66 | private SummaryLoader mSummaryLoader; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 67 | |
| 68 | @Override |
| 69 | public void onAttach(Context context) { |
| 70 | super.onAttach(context); |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 71 | mDashboardFeatureProvider = FeatureFactory.getFactory(context). |
| 72 | getDashboardFeatureProvider(context); |
| 73 | final List<AbstractPreferenceController> controllers = new ArrayList<>(); |
| 74 | // Load preference controllers from code |
| 75 | final List<AbstractPreferenceController> controllersFromCode = |
| Fan Zhang | f7843ad | 2018-02-22 13:51:41 -0800 | [diff] [blame] | 76 | createPreferenceControllers(context); |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 77 | // Load preference controllers from xml definition |
| 78 | final List<BasePreferenceController> controllersFromXml = PreferenceControllerListHelper |
| 79 | .getPreferenceControllersFromXml(context, getPreferenceScreenResId()); |
| 80 | // Filter xml-based controllers in case a similar controller is created from code already. |
| 81 | final List<BasePreferenceController> uniqueControllerFromXml = |
| 82 | PreferenceControllerListHelper.filterControllers( |
| 83 | controllersFromXml, controllersFromCode); |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 84 | |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 85 | // Add unique controllers to list. |
| 86 | if (controllersFromCode != null) { |
| 87 | controllers.addAll(controllersFromCode); |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 88 | } |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 89 | controllers.addAll(uniqueControllerFromXml); |
| 90 | |
| 91 | // And wire up with lifecycle. |
| tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 92 | final Lifecycle lifecycle = getSettingsLifecycle(); |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 93 | uniqueControllerFromXml |
| 94 | .stream() |
| 95 | .filter(controller -> controller instanceof LifecycleObserver) |
| 96 | .forEach( |
| 97 | controller -> lifecycle.addObserver((LifecycleObserver) controller)); |
| 98 | |
| Fan Zhang | 9dc9c61 | 2016-11-11 13:23:21 -0800 | [diff] [blame] | 99 | mPlaceholderPreferenceController = |
| 100 | new DashboardTilePlaceholderPreferenceController(context); |
| 101 | controllers.add(mPlaceholderPreferenceController); |
| Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 102 | for (AbstractPreferenceController controller : controllers) { |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 103 | addPreferenceController(controller); |
| 104 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 108 | public void onCreate(Bundle icicle) { |
| 109 | super.onCreate(icicle); |
| 110 | // Set ComparisonCallback so we get better animation when list changes. |
| 111 | getPreferenceManager().setPreferenceComparisonCallback( |
| 112 | new PreferenceManager.SimplePreferenceComparisonCallback()); |
| jeffreyhuang | bf234af | 2017-11-14 15:09:12 -0800 | [diff] [blame] | 113 | if (icicle != null) { |
| 114 | // Upon rotation configuration change we need to update preference states before any |
| 115 | // editing dialog is recreated (that would happen before onResume is called). |
| 116 | updatePreferenceStates(); |
| 117 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 121 | public void onCategoriesChanged() { |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 122 | final DashboardCategory category = |
| 123 | mDashboardFeatureProvider.getTilesForCategory(getCategoryKey()); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 124 | if (category == null) { |
| 125 | return; |
| 126 | } |
| Fan Zhang | 7f1a4b5 | 2016-10-12 12:35:52 -0700 | [diff] [blame] | 127 | refreshDashboardTiles(getLogTag()); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 132 | refreshAllPreferences(getLogTag()); |
| 133 | } |
| 134 | |
| 135 | @Override |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 136 | public void onStart() { |
| 137 | super.onStart(); |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 138 | final DashboardCategory category = |
| 139 | mDashboardFeatureProvider.getTilesForCategory(getCategoryKey()); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 140 | if (category == null) { |
| 141 | return; |
| 142 | } |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 143 | if (mSummaryLoader != null) { |
| 144 | // SummaryLoader can be null when there is no dynamic tiles. |
| 145 | mSummaryLoader.setListening(true); |
| 146 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 147 | final Activity activity = getActivity(); |
| tmfang | d97fba5 | 2018-06-14 14:48:35 +0800 | [diff] [blame] | 148 | if (activity instanceof SettingsBaseActivity) { |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 149 | mListeningToCategoryChange = true; |
| tmfang | d97fba5 | 2018-06-14 14:48:35 +0800 | [diff] [blame] | 150 | ((SettingsBaseActivity) activity).addCategoryListener(this); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 155 | public void notifySummaryChanged(Tile tile) { |
| 156 | final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile); |
| Doris Ling | 8b14a1a | 2017-08-17 14:45:12 -0700 | [diff] [blame] | 157 | final Preference pref = getPreferenceScreen().findPreference(key); |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 158 | if (pref == null) { |
| 159 | Log.d(getLogTag(), |
| 160 | String.format("Can't find pref by key %s, skipping update summary %s/%s", |
| 161 | key, tile.title, tile.summary)); |
| 162 | return; |
| 163 | } |
| 164 | pref.setSummary(tile.summary); |
| 165 | } |
| 166 | |
| 167 | @Override |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 168 | public void onResume() { |
| 169 | super.onResume(); |
| 170 | updatePreferenceStates(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 174 | public boolean onPreferenceTreeClick(Preference preference) { |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 175 | Collection<List<AbstractPreferenceController>> controllers = |
| 176 | mPreferenceControllers.values(); |
| Fan Zhang | ee45943 | 2017-02-23 10:59:47 -0800 | [diff] [blame] | 177 | // If preference contains intent, log it before handling. |
| 178 | mMetricsFeatureProvider.logDashboardStartIntent( |
| 179 | getContext(), preference.getIntent(), getMetricsCategory()); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 180 | // Give all controllers a chance to handle click. |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 181 | for (List<AbstractPreferenceController> controllerList : controllers) { |
| 182 | for (AbstractPreferenceController controller : controllerList) { |
| 183 | if (controller.handlePreferenceTreeClick(preference)) { |
| 184 | return true; |
| 185 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | return super.onPreferenceTreeClick(preference); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void onStop() { |
| 193 | super.onStop(); |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 194 | if (mSummaryLoader != null) { |
| 195 | // SummaryLoader can be null when there is no dynamic tiles. |
| 196 | mSummaryLoader.setListening(false); |
| 197 | } |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 198 | if (mListeningToCategoryChange) { |
| 199 | final Activity activity = getActivity(); |
| tmfang | d97fba5 | 2018-06-14 14:48:35 +0800 | [diff] [blame] | 200 | if (activity instanceof SettingsBaseActivity) { |
| 201 | ((SettingsBaseActivity) activity).remCategoryListener(this); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 202 | } |
| 203 | mListeningToCategoryChange = false; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| Fan Zhang | 9679dba | 2017-10-31 14:27:18 -0700 | [diff] [blame] | 207 | @Override |
| 208 | protected abstract int getPreferenceScreenResId(); |
| 209 | |
| Fan Zhang | f7843ad | 2018-02-22 13:51:41 -0800 | [diff] [blame] | 210 | protected <T extends AbstractPreferenceController> T use(Class<T> clazz) { |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 211 | List<AbstractPreferenceController> controllerList = mPreferenceControllers.get(clazz); |
| 212 | if (controllerList != null) { |
| 213 | if (controllerList.size() > 1) { |
| 214 | Log.w(TAG, "Multiple controllers of Class " + clazz.getSimpleName() |
| 215 | + " found, returning first one."); |
| 216 | } |
| 217 | return (T) controllerList.get(0); |
| 218 | } |
| 219 | |
| 220 | return null; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 223 | protected void addPreferenceController(AbstractPreferenceController controller) { |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 224 | if (mPreferenceControllers.get(controller.getClass()) == null) { |
| 225 | mPreferenceControllers.put(controller.getClass(), new ArrayList<>()); |
| 226 | } |
| 227 | mPreferenceControllers.get(controller.getClass()).add(controller); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 230 | /** |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 231 | * Returns the CategoryKey for loading {@link DashboardCategory} for this fragment. |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 232 | */ |
| Fan Zhang | 26a1def | 2017-07-26 10:58:32 -0700 | [diff] [blame] | 233 | @VisibleForTesting |
| Fan Zhang | 7e6df83 | 2017-01-24 14:02:17 -0800 | [diff] [blame] | 234 | public String getCategoryKey() { |
| 235 | return DashboardFragmentRegistry.PARENT_TO_CATEGORY_KEY_MAP.get(getClass().getName()); |
| 236 | } |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 239 | * Get the tag string for logging. |
| 240 | */ |
| 241 | protected abstract String getLogTag(); |
| 242 | |
| 243 | /** |
| Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 244 | * Get a list of {@link AbstractPreferenceController} for this fragment. |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 245 | */ |
| Fan Zhang | f7843ad | 2018-02-22 13:51:41 -0800 | [diff] [blame] | 246 | protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { |
| Fan Zhang | 917f101 | 2018-02-21 15:22:25 -0800 | [diff] [blame] | 247 | return null; |
| 248 | } |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 249 | |
| 250 | /** |
| Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 251 | * Returns true if this tile should be displayed |
| 252 | */ |
| 253 | protected boolean displayTile(Tile tile) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| Doris Ling | 7087fd9 | 2017-07-17 15:04:36 -0700 | [diff] [blame] | 257 | @VisibleForTesting |
| 258 | boolean tintTileIcon(Tile tile) { |
| Fan Zhang | 1c61a58 | 2018-07-26 11:26:11 -0700 | [diff] [blame] | 259 | final Context context = getContext(); |
| 260 | if (tile.getIcon(context) == null) { |
| Fan Zhang | 1ca6b25 | 2017-09-07 16:06:24 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| Doris Ling | 7087fd9 | 2017-07-17 15:04:36 -0700 | [diff] [blame] | 263 | // First check if the tile has set the icon tintable metadata. |
| 264 | final Bundle metadata = tile.metaData; |
| 265 | if (metadata != null |
| 266 | && metadata.containsKey(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE)) { |
| 267 | return metadata.getBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE); |
| 268 | } |
| Fan Zhang | 1c61a58 | 2018-07-26 11:26:11 -0700 | [diff] [blame] | 269 | final String pkgName = context.getPackageName(); |
| Doris Ling | 7087fd9 | 2017-07-17 15:04:36 -0700 | [diff] [blame] | 270 | // If this drawable is coming from outside Settings, tint it to match the color. |
| 271 | return pkgName != null && tile.intent != null |
| 272 | && !pkgName.equals(tile.intent.getComponent().getPackageName()); |
| 273 | } |
| 274 | |
| Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 275 | /** |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 276 | * Displays resource based tiles. |
| 277 | */ |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 278 | private void displayResourceTiles() { |
| 279 | final int resId = getPreferenceScreenResId(); |
| 280 | if (resId <= 0) { |
| 281 | return; |
| 282 | } |
| 283 | addPreferencesFromResource(resId); |
| 284 | final PreferenceScreen screen = getPreferenceScreen(); |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 285 | mPreferenceControllers.values().stream().flatMap(Collection::stream).forEach( |
| 286 | controller -> controller.displayPreference(screen)); |
| Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 287 | } |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 290 | * Update state of each preference managed by PreferenceController. |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 291 | */ |
| Fan Zhang | beddff8 | 2016-11-18 11:10:05 -0800 | [diff] [blame] | 292 | protected void updatePreferenceStates() { |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 293 | final PreferenceScreen screen = getPreferenceScreen(); |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 294 | Collection<List<AbstractPreferenceController>> controllerLists = |
| 295 | mPreferenceControllers.values(); |
| 296 | for (List<AbstractPreferenceController> controllerList : controllerLists) { |
| 297 | for (AbstractPreferenceController controller : controllerList) { |
| 298 | if (!controller.isAvailable()) { |
| 299 | continue; |
| 300 | } |
| 301 | final String key = controller.getPreferenceKey(); |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 302 | |
| Ben Lin | 9275177 | 2017-12-21 17:32:34 -0800 | [diff] [blame] | 303 | final Preference preference = screen.findPreference(key); |
| 304 | if (preference == null) { |
| 305 | Log.d(TAG, String.format("Cannot find preference with key %s in Controller %s", |
| 306 | key, controller.getClass().getSimpleName())); |
| 307 | continue; |
| 308 | } |
| 309 | controller.updateState(preference); |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 310 | } |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Refresh all preference items, including both static prefs from xml, and dynamic items from |
| 316 | * DashboardCategory. |
| 317 | */ |
| 318 | private void refreshAllPreferences(final String TAG) { |
| 319 | // First remove old preferences. |
| 320 | if (getPreferenceScreen() != null) { |
| 321 | // Intentionally do not cache PreferenceScreen because it will be recreated later. |
| 322 | getPreferenceScreen().removeAll(); |
| 323 | } |
| 324 | |
| 325 | // Add resource based tiles. |
| 326 | displayResourceTiles(); |
| 327 | |
| 328 | refreshDashboardTiles(TAG); |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Refresh preference items backed by DashboardCategory. |
| 333 | */ |
| Fan Zhang | 701b65e | 2018-07-24 12:49:30 -0700 | [diff] [blame] | 334 | @VisibleForTesting |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 335 | void refreshDashboardTiles(final String TAG) { |
| Fan Zhang | 8b5bca5 | 2016-10-19 12:00:32 -0700 | [diff] [blame] | 336 | final PreferenceScreen screen = getPreferenceScreen(); |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 337 | |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 338 | final DashboardCategory category = |
| 339 | mDashboardFeatureProvider.getTilesForCategory(getCategoryKey()); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 340 | if (category == null) { |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 341 | Log.d(TAG, "NO dashboard tiles for " + TAG); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
| Doris Ling | bcb7635 | 2017-11-22 17:29:21 -0800 | [diff] [blame] | 344 | final List<Tile> tiles = category.getTiles(); |
| Fan Zhang | e6c60c2 | 2016-10-04 17:48:32 -0700 | [diff] [blame] | 345 | if (tiles == null) { |
| Fan Zhang | 9a1928e | 2018-08-02 12:59:55 -0700 | [diff] [blame^] | 346 | Log.d(TAG, "tile list is empty, skipping category " + category.key); |
| Fan Zhang | e6c60c2 | 2016-10-04 17:48:32 -0700 | [diff] [blame] | 347 | return; |
| 348 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 349 | // Create a list to track which tiles are to be removed. |
| 350 | final List<String> remove = new ArrayList<>(mDashboardTilePrefKeys); |
| 351 | |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 352 | // There are dashboard tiles, so we need to install SummaryLoader. |
| 353 | if (mSummaryLoader != null) { |
| 354 | mSummaryLoader.release(); |
| 355 | } |
| Doris Ling | fd83b2e | 2017-01-18 17:03:26 -0800 | [diff] [blame] | 356 | final Context context = getContext(); |
| 357 | mSummaryLoader = new SummaryLoader(getActivity(), getCategoryKey()); |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 358 | mSummaryLoader.setSummaryConsumer(this); |
| Fan Zhang | 9a1928e | 2018-08-02 12:59:55 -0700 | [diff] [blame^] | 359 | final TypedArray a = context.obtainStyledAttributes(new int[]{ |
| Doris Ling | f2cf2ae | 2017-03-03 17:12:47 -0800 | [diff] [blame] | 360 | android.R.attr.colorControlNormal}); |
| Doris Ling | fd83b2e | 2017-01-18 17:03:26 -0800 | [diff] [blame] | 361 | final int tintColor = a.getColor(0, context.getColor(android.R.color.white)); |
| Doris Ling | 990460b | 2017-01-12 17:01:28 -0800 | [diff] [blame] | 362 | a.recycle(); |
| Fan Zhang | fabbfb4 | 2016-10-07 12:41:43 -0700 | [diff] [blame] | 363 | // Install dashboard tiles. |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 364 | for (Tile tile : tiles) { |
| 365 | final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile); |
| 366 | if (TextUtils.isEmpty(key)) { |
| 367 | Log.d(TAG, "tile does not contain a key, skipping " + tile); |
| 368 | continue; |
| 369 | } |
| Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 370 | if (!displayTile(tile)) { |
| 371 | continue; |
| 372 | } |
| Doris Ling | 7087fd9 | 2017-07-17 15:04:36 -0700 | [diff] [blame] | 373 | if (tintTileIcon(tile)) { |
| Fan Zhang | 1c61a58 | 2018-07-26 11:26:11 -0700 | [diff] [blame] | 374 | tile.getIcon(context).setTint(tintColor); |
| Doris Ling | 990460b | 2017-01-12 17:01:28 -0800 | [diff] [blame] | 375 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 376 | if (mDashboardTilePrefKeys.contains(key)) { |
| 377 | // Have the key already, will rebind. |
| Doris Ling | 8b14a1a | 2017-08-17 14:45:12 -0700 | [diff] [blame] | 378 | final Preference preference = screen.findPreference(key); |
| Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 379 | mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), getMetricsCategory(), |
| 380 | preference, tile, key, mPlaceholderPreferenceController.getOrder()); |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 381 | } else { |
| 382 | // Don't have this key, add it. |
| Fan Zhang | 8b30f53 | 2016-10-31 14:40:15 -0700 | [diff] [blame] | 383 | final Preference pref = new Preference(getPrefContext()); |
| Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 384 | mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), getMetricsCategory(), |
| 385 | pref, tile, key, mPlaceholderPreferenceController.getOrder()); |
| Doris Ling | 8b14a1a | 2017-08-17 14:45:12 -0700 | [diff] [blame] | 386 | screen.addPreference(pref); |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 387 | mDashboardTilePrefKeys.add(key); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 388 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 389 | remove.remove(key); |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 390 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 391 | // Finally remove tiles that are gone. |
| 392 | for (String key : remove) { |
| 393 | mDashboardTilePrefKeys.remove(key); |
| Doris Ling | 8b14a1a | 2017-08-17 14:45:12 -0700 | [diff] [blame] | 394 | final Preference preference = screen.findPreference(key); |
| 395 | if (preference != null) { |
| 396 | screen.removePreference(preference); |
| 397 | } |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 398 | } |
| Fan Zhang | 00d617d | 2016-12-27 13:56:42 -0800 | [diff] [blame] | 399 | mSummaryLoader.setListening(true); |
| Fan Zhang | a1a84e6 | 2016-10-19 14:15:34 -0700 | [diff] [blame] | 400 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 401 | } |