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