| 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; |
| 20 | import android.support.v7.preference.Preference; |
| 21 | import android.support.v7.preference.PreferenceScreen; |
| 22 | import android.text.TextUtils; |
| 23 | import android.util.ArrayMap; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.settings.SettingsPreferenceFragment; |
| 27 | import com.android.settings.core.PreferenceController; |
| 28 | import com.android.settings.overlay.FeatureFactory; |
| 29 | import com.android.settings.search.Indexable; |
| 30 | import com.android.settingslib.drawer.DashboardCategory; |
| 31 | import com.android.settingslib.drawer.SettingsDrawerActivity; |
| 32 | import com.android.settingslib.drawer.Tile; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | |
| 38 | /** |
| 39 | * Base fragment for dashboard style UI containing a list of static and dynamic setting items. |
| 40 | */ |
| 41 | public abstract class DashboardFragment extends SettingsPreferenceFragment |
| 42 | implements SettingsDrawerActivity.CategoryListener, Indexable { |
| 43 | |
| 44 | private final Map<Class, PreferenceController> mPreferenceControllers = |
| 45 | new ArrayMap<>(); |
| 46 | |
| 47 | protected DashboardFeatureProvider mDashboardFeatureProvider; |
| 48 | |
| 49 | @Override |
| 50 | public void onAttach(Context context) { |
| 51 | super.onAttach(context); |
| 52 | mDashboardFeatureProvider = |
| 53 | FeatureFactory.getFactory(context).getDashboardFeatureProvider(context); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void onStart() { |
| 58 | super.onStart(); |
| 59 | final Activity activity = getActivity(); |
| 60 | if (activity instanceof SettingsDrawerActivity) { |
| 61 | ((SettingsDrawerActivity) activity).addCategoryListener(this); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean onPreferenceTreeClick(Preference preference) { |
| 67 | Collection<PreferenceController> controllers = mPreferenceControllers.values(); |
| 68 | // Give all controllers a chance to handle click. |
| 69 | for (PreferenceController controller : controllers) { |
| 70 | if (controller.handlePreferenceTreeClick(preference)) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | return super.onPreferenceTreeClick(preference); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onStop() { |
| 79 | super.onStop(); |
| 80 | final Activity activity = getActivity(); |
| 81 | if (activity instanceof SettingsDrawerActivity) { |
| 82 | ((SettingsDrawerActivity) activity).remCategoryListener(this); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | protected <T extends PreferenceController> T getPreferenceController(Class<T> clazz) { |
| 87 | PreferenceController controller = mPreferenceControllers.get(clazz); |
| 88 | return (T) controller; |
| 89 | } |
| 90 | |
| 91 | protected void addPreferenceController(PreferenceController controller) { |
| 92 | mPreferenceControllers.put(controller.getClass(), controller); |
| 93 | } |
| 94 | |
| 95 | protected final void displayTilesAsPreference(String TAG, PreferenceScreen screen, |
| 96 | DashboardCategory category) { |
| 97 | final Context context = getContext(); |
| 98 | List<Tile> tiles = category.tiles; |
| Fan Zhang | e6c60c2 | 2016-10-04 17:48:32 -0700 | [diff] [blame^] | 99 | if (tiles == null) { |
| 100 | Log.d(TAG, "tile list is empty, skipping category " + category.title); |
| 101 | return; |
| 102 | } |
| Fan Zhang | bb6d260 | 2016-10-04 13:21:06 -0700 | [diff] [blame] | 103 | for (Tile tile : tiles) { |
| 104 | final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile); |
| 105 | if (TextUtils.isEmpty(key)) { |
| 106 | Log.d(TAG, "tile does not contain a key, skipping " + tile); |
| 107 | continue; |
| 108 | } |
| 109 | final Preference pref = new DashboardTilePreference(context); |
| 110 | pref.setTitle(tile.title); |
| 111 | pref.setKey(key); |
| 112 | pref.setSummary(tile.summary); |
| 113 | if (tile.icon != null) { |
| 114 | pref.setIcon(tile.icon.loadDrawable(context)); |
| 115 | } |
| 116 | if (tile.intent != null) { |
| 117 | pref.setIntent(tile.intent); |
| 118 | } |
| 119 | // Use negated priority for order, because tile priority is based on intent-filter |
| 120 | // (larger value has higher priority). However pref order defines smaller value has |
| 121 | // higher priority. |
| 122 | pref.setOrder(-tile.priority); |
| 123 | screen.addPreference(pref); |
| 124 | } |
| 125 | } |
| 126 | } |