blob: 1388be7c628e5221c44184eb87364cfeddfe3a95 [file] [log] [blame]
Fan Zhangbb6d2602016-10-04 13:21:06 -07001/*
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 */
16package com.android.settings.dashboard;
17
18import android.app.Activity;
19import android.content.Context;
Fan Zhang36924652016-10-07 08:38:48 -070020import android.os.Bundle;
Fan Zhangbb6d2602016-10-04 13:21:06 -070021import android.support.v7.preference.Preference;
22import android.support.v7.preference.PreferenceScreen;
23import android.text.TextUtils;
24import android.util.ArrayMap;
25import android.util.Log;
26
27import com.android.settings.SettingsPreferenceFragment;
28import com.android.settings.core.PreferenceController;
29import com.android.settings.overlay.FeatureFactory;
30import com.android.settings.search.Indexable;
31import com.android.settingslib.drawer.DashboardCategory;
32import com.android.settingslib.drawer.SettingsDrawerActivity;
33import com.android.settingslib.drawer.Tile;
34
35import java.util.Collection;
36import java.util.List;
37import java.util.Map;
38
39/**
40 * Base fragment for dashboard style UI containing a list of static and dynamic setting items.
41 */
42public 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 Zhang36924652016-10-07 08:38:48 -070049 private boolean mListeningToCategoryChange;
Fan Zhangbb6d2602016-10-04 13:21:06 -070050
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 Zhang36924652016-10-07 08:38:48 -070059 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 Zhangbb6d2602016-10-04 13:21:06 -070074 public void onStart() {
75 super.onStart();
Fan Zhang36924652016-10-07 08:38:48 -070076 final DashboardCategory category = getDashboardTiles();
77 if (category == null) {
78 return;
79 }
80
Fan Zhangbb6d2602016-10-04 13:21:06 -070081 final Activity activity = getActivity();
82 if (activity instanceof SettingsDrawerActivity) {
Fan Zhang36924652016-10-07 08:38:48 -070083 mListeningToCategoryChange = true;
Fan Zhangbb6d2602016-10-04 13:21:06 -070084 ((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 Zhang36924652016-10-07 08:38:48 -0700103 if (mListeningToCategoryChange) {
104 final Activity activity = getActivity();
105 if (activity instanceof SettingsDrawerActivity) {
106 ((SettingsDrawerActivity) activity).remCategoryListener(this);
107 }
108 mListeningToCategoryChange = false;
Fan Zhangbb6d2602016-10-04 13:21:06 -0700109 }
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 Zhang36924652016-10-07 08:38:48 -0700121 /**
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 Zhangbb6d2602016-10-04 13:21:06 -0700137 final Context context = getContext();
Fan Zhang36924652016-10-07 08:38:48 -0700138 final DashboardCategory category = getDashboardTiles();
139 if (category == null) {
140 Log.d(TAG, "NO dynamic tiles for " + TAG);
141 return;
142 }
Fan Zhangbb6d2602016-10-04 13:21:06 -0700143 List<Tile> tiles = category.tiles;
Fan Zhange6c60c22016-10-04 17:48:32 -0700144 if (tiles == null) {
145 Log.d(TAG, "tile list is empty, skipping category " + category.title);
146 return;
147 }
Fan Zhangbb6d2602016-10-04 13:21:06 -0700148 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 Zhang36924652016-10-07 08:38:48 -0700171
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 Zhangbb6d2602016-10-04 13:21:06 -0700186}