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