| 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; |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 21 | import android.support.v7.preference.Preference; |
| 22 | import android.support.v7.preference.PreferenceScreen; |
| 23 | import android.text.TextUtils; |
| 24 | import android.util.ArrayMap; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import com.android.settings.SettingsPreferenceFragment; |
| 28 | import com.android.settings.core.PreferenceController; |
| 29 | import com.android.settings.overlay.FeatureFactory; |
| 30 | import com.android.settings.search.Indexable; |
| 31 | import com.android.settingslib.drawer.DashboardCategory; |
| 32 | import com.android.settingslib.drawer.SettingsDrawerActivity; |
| 33 | import com.android.settingslib.drawer.Tile; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | /** |
| 40 | * Base fragment for dashboard style UI containing a list of static and dynamic setting items. |
| 41 | */ |
| 42 | public abstract class DashboardFragment extends SettingsPreferenceFragment |
| 43 | implements SettingsDrawerActivity.CategoryListener, Indexable { |
| 44 | |
| 45 | private final Map<Class, PreferenceController> mPreferenceControllers = |
| 46 | new ArrayMap<>(); |
| 47 | |
| 48 | protected DashboardFeatureProvider mDashboardFeatureProvider; |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 49 | private boolean mListeningToCategoryChange; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 50 | |
| 51 | @Override |
| 52 | public void onAttach(Context context) { |
| 53 | super.onAttach(context); |
| 54 | mDashboardFeatureProvider = |
| 55 | FeatureFactory.getFactory(context).getDashboardFeatureProvider(context); |
| 56 | } |
| 57 | |
| 58 | @Override |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 59 | public void onCategoriesChanged() { |
| 60 | final DashboardCategory category = getDashboardTiles(); |
| 61 | if (category == null) { |
| 62 | return; |
| 63 | } |
| 64 | refreshAllPreferences(getLogTag()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 69 | super.onCreatePreferences(savedInstanceState, rootKey); |
| 70 | refreshAllPreferences(getLogTag()); |
| 71 | } |
| 72 | |
| 73 | @Override |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 74 | public void onStart() { |
| 75 | super.onStart(); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 76 | final DashboardCategory category = getDashboardTiles(); |
| 77 | if (category == null) { |
| 78 | return; |
| 79 | } |
| 80 | |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 81 | final Activity activity = getActivity(); |
| 82 | if (activity instanceof SettingsDrawerActivity) { |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 83 | mListeningToCategoryChange = true; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 84 | ((SettingsDrawerActivity) activity).addCategoryListener(this); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean onPreferenceTreeClick(Preference preference) { |
| 90 | Collection<PreferenceController> controllers = mPreferenceControllers.values(); |
| 91 | // Give all controllers a chance to handle click. |
| 92 | for (PreferenceController controller : controllers) { |
| 93 | if (controller.handlePreferenceTreeClick(preference)) { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | return super.onPreferenceTreeClick(preference); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onStop() { |
| 102 | super.onStop(); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 103 | if (mListeningToCategoryChange) { |
| 104 | final Activity activity = getActivity(); |
| 105 | if (activity instanceof SettingsDrawerActivity) { |
| 106 | ((SettingsDrawerActivity) activity).remCategoryListener(this); |
| 107 | } |
| 108 | mListeningToCategoryChange = false; |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | protected <T extends PreferenceController> T getPreferenceController(Class<T> clazz) { |
| 113 | PreferenceController controller = mPreferenceControllers.get(clazz); |
| 114 | return (T) controller; |
| 115 | } |
| 116 | |
| 117 | protected void addPreferenceController(PreferenceController controller) { |
| 118 | mPreferenceControllers.put(controller.getClass(), controller); |
| 119 | } |
| 120 | |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Returns {@link DashboardCategory} for this fragment. |
| 123 | */ |
| 124 | protected abstract DashboardCategory getDashboardTiles(); |
| 125 | |
| 126 | /** |
| 127 | * Displays resource based tiles. |
| 128 | */ |
| 129 | protected abstract void displayResourceTiles(); |
| 130 | |
| 131 | protected abstract String getLogTag(); |
| 132 | |
| 133 | /** |
| 134 | * Displays dashboard tiles as preference. |
| 135 | */ |
| 136 | private final void displayDashboardTiles(final String TAG, PreferenceScreen screen) { |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 137 | final Context context = getContext(); |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 138 | final DashboardCategory category = getDashboardTiles(); |
| 139 | if (category == null) { |
| 140 | Log.d(TAG, "NO dynamic tiles for " + TAG); |
| 141 | return; |
| 142 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 143 | List<Tile> tiles = category.tiles; |
| Fan Zhang | e6c60c2 | 2016-10-04 17:48:32 -0700 | [diff] [blame] | 144 | if (tiles == null) { |
| 145 | Log.d(TAG, "tile list is empty, skipping category " + category.title); |
| 146 | return; |
| 147 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 148 | for (Tile tile : tiles) { |
| 149 | final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile); |
| 150 | if (TextUtils.isEmpty(key)) { |
| 151 | Log.d(TAG, "tile does not contain a key, skipping " + tile); |
| 152 | continue; |
| 153 | } |
| 154 | final Preference pref = new DashboardTilePreference(context); |
| 155 | pref.setTitle(tile.title); |
| 156 | pref.setKey(key); |
| 157 | pref.setSummary(tile.summary); |
| 158 | if (tile.icon != null) { |
| 159 | pref.setIcon(tile.icon.loadDrawable(context)); |
| 160 | } |
| 161 | if (tile.intent != null) { |
| 162 | pref.setIntent(tile.intent); |
| 163 | } |
| 164 | // Use negated priority for order, because tile priority is based on intent-filter |
| 165 | // (larger value has higher priority). However pref order defines smaller value has |
| 166 | // higher priority. |
| 167 | pref.setOrder(-tile.priority); |
| 168 | screen.addPreference(pref); |
| 169 | } |
| 170 | } |
| Fan Zhang | 3692465 | 2016-10-07 08:38:48 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Refresh preference items using system category dashboard items. |
| 174 | */ |
| 175 | private void refreshAllPreferences(final String TAG) { |
| 176 | // First remove old preferences. |
| 177 | PreferenceScreen screen = getPreferenceScreen(); |
| 178 | if (screen != null) { |
| 179 | screen.removeAll(); |
| 180 | } |
| 181 | // Add resource based tiles. |
| 182 | displayResourceTiles(); |
| 183 | // Add dashboard tiles. |
| 184 | displayDashboardTiles(TAG, getPreferenceScreen()); |
| 185 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 186 | } |