blob: e70cc29a7744dfb02e070003a34d89588a07a665 [file] [log] [blame]
Jason Monk744b6362015-11-03 18:24:29 -05001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
Jason Monkf509d7e2016-01-07 16:22:53 -05004 * 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
Jason Monk744b6362015-11-03 18:24:29 -05007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Jason Monkf509d7e2016-01-07 16:22:53 -050010 * 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
Jason Monk744b6362015-11-03 18:24:29 -050015 */
Jason Monk744b6362015-11-03 18:24:29 -050016package com.android.settingslib.drawer;
17
Jason Monk0d72d202015-11-04 13:16:00 -050018import android.app.ActivityManager;
Jason Monk744b6362015-11-03 18:24:29 -050019import android.content.Context;
20import android.content.Intent;
21import android.content.pm.ActivityInfo;
Jason Monke79790b2015-12-02 15:39:19 -050022import android.content.pm.ApplicationInfo;
Jason Monk744b6362015-11-03 18:24:29 -050023import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.content.res.Resources;
26import android.graphics.drawable.Icon;
27import android.os.Bundle;
28import android.os.UserHandle;
29import android.os.UserManager;
Jason Monk64600cf2016-06-30 13:15:48 -040030import android.provider.Settings.Global;
Jason Monk744b6362015-11-03 18:24:29 -050031import android.text.TextUtils;
32import android.util.Log;
33import android.util.Pair;
34
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.Comparator;
38import java.util.HashMap;
39import java.util.List;
40import java.util.Map;
41
42public class TileUtils {
43
Jason Monk0d72d202015-11-04 13:16:00 -050044 private static final boolean DEBUG = false;
Joe Onorato93dcff02016-02-01 17:44:14 -080045 private static final boolean DEBUG_TIMING = false;
Jason Monk744b6362015-11-03 18:24:29 -050046
47 private static final String LOG_TAG = "TileUtils";
48
49 /**
50 * Settings will search for system activities of this action and add them as a top level
51 * settings tile using the following parameters.
52 *
53 * <p>A category must be specified in the meta-data for the activity named
54 * {@link #EXTRA_CATEGORY_KEY}
55 *
56 * <p>The title may be defined by meta-data named {@link #META_DATA_PREFERENCE_TITLE}
57 * otherwise the label for the activity will be used.
58 *
59 * <p>The icon may be defined by meta-data named {@link #META_DATA_PREFERENCE_ICON}
60 * otherwise the icon for the activity will be used.
61 *
62 * <p>A summary my be defined by meta-data named {@link #META_DATA_PREFERENCE_SUMMARY}
63 */
64 private static final String EXTRA_SETTINGS_ACTION =
65 "com.android.settings.action.EXTRA_SETTINGS";
66
67 /**
68 * Same as #EXTRA_SETTINGS_ACTION but used for the platform Settings activities.
69 */
70 private static final String SETTINGS_ACTION =
71 "com.android.settings.action.SETTINGS";
72
73 private static final String OPERATOR_SETTINGS =
74 "com.android.settings.OPERATOR_APPLICATION_SETTING";
75
76 private static final String OPERATOR_DEFAULT_CATEGORY =
77 "com.android.settings.category.wireless";
78
79 private static final String MANUFACTURER_SETTINGS =
80 "com.android.settings.MANUFACTURER_APPLICATION_SETTING";
81
82 private static final String MANUFACTURER_DEFAULT_CATEGORY =
83 "com.android.settings.category.device";
84
85 /**
86 * The key used to get the category from metadata of activities of action
87 * {@link #EXTRA_SETTINGS_ACTION}
88 * The value must be one of:
89 * <li>com.android.settings.category.wireless</li>
90 * <li>com.android.settings.category.device</li>
91 * <li>com.android.settings.category.personal</li>
92 * <li>com.android.settings.category.system</li>
93 */
94 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
95
96 /**
97 * Name of the meta-data item that should be set in the AndroidManifest.xml
98 * to specify the icon that should be displayed for the preference.
99 */
100 public static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
101
102 /**
103 * Name of the meta-data item that should be set in the AndroidManifest.xml
104 * to specify the title that should be displayed for the preference.
105 */
106 public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
107
108 /**
109 * Name of the meta-data item that should be set in the AndroidManifest.xml
110 * to specify the summary text that should be displayed for the preference.
111 */
112 public static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
113
114 private static final String SETTING_PKG = "com.android.settings";
115
Jason Monk744b6362015-11-03 18:24:29 -0500116 public static List<DashboardCategory> getCategories(Context context,
Jason Monkf509d7e2016-01-07 16:22:53 -0500117 HashMap<Pair<String, String>, Tile> cache) {
Jason Monke79790b2015-12-02 15:39:19 -0500118 final long startTime = System.currentTimeMillis();
Jason Monk64600cf2016-06-30 13:15:48 -0400119 boolean setup = Global.getInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0)
120 != 0;
Jason Monkf509d7e2016-01-07 16:22:53 -0500121 ArrayList<Tile> tiles = new ArrayList<>();
Jason Monk744b6362015-11-03 18:24:29 -0500122 UserManager userManager = UserManager.get(context);
123 for (UserHandle user : userManager.getUserProfiles()) {
124 // TODO: Needs much optimization, too many PM queries going on here.
Jason Monk0d72d202015-11-04 13:16:00 -0500125 if (user.getIdentifier() == ActivityManager.getCurrentUser()) {
126 // Only add Settings for this user.
127 getTilesForAction(context, user, SETTINGS_ACTION, cache, null, tiles, true);
128 getTilesForAction(context, user, OPERATOR_SETTINGS, cache,
129 OPERATOR_DEFAULT_CATEGORY, tiles, false);
130 getTilesForAction(context, user, MANUFACTURER_SETTINGS, cache,
131 MANUFACTURER_DEFAULT_CATEGORY, tiles, false);
132 }
Jason Monk64600cf2016-06-30 13:15:48 -0400133 if (setup) {
134 getTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false);
135 }
Jason Monk744b6362015-11-03 18:24:29 -0500136 }
137 HashMap<String, DashboardCategory> categoryMap = new HashMap<>();
Jason Monkf509d7e2016-01-07 16:22:53 -0500138 for (Tile tile : tiles) {
Jason Monk744b6362015-11-03 18:24:29 -0500139 DashboardCategory category = categoryMap.get(tile.category);
140 if (category == null) {
141 category = createCategory(context, tile.category);
142 if (category == null) {
143 Log.w(LOG_TAG, "Couldn't find category " + tile.category);
144 continue;
145 }
146 categoryMap.put(category.key, category);
147 }
148 category.addTile(tile);
149 }
150 ArrayList<DashboardCategory> categories = new ArrayList<>(categoryMap.values());
151 for (DashboardCategory category : categories) {
152 Collections.sort(category.tiles, TILE_COMPARATOR);
153 }
154 Collections.sort(categories, CATEGORY_COMPARATOR);
Jason Monke79790b2015-12-02 15:39:19 -0500155 if (DEBUG_TIMING) Log.d(LOG_TAG, "getCategories took "
156 + (System.currentTimeMillis() - startTime) + " ms");
Jason Monk744b6362015-11-03 18:24:29 -0500157 return categories;
158 }
159
160 private static DashboardCategory createCategory(Context context, String categoryKey) {
161 DashboardCategory category = new DashboardCategory();
162 category.key = categoryKey;
163 PackageManager pm = context.getPackageManager();
164 List<ResolveInfo> results = pm.queryIntentActivities(new Intent(categoryKey), 0);
165 if (results.size() == 0) {
166 return null;
167 }
168 for (ResolveInfo resolved : results) {
169 if (!resolved.system) {
170 // Do not allow any app to add to settings, only system ones.
171 continue;
172 }
173 category.title = resolved.activityInfo.loadLabel(pm);
174 category.priority = SETTING_PKG.equals(
175 resolved.activityInfo.applicationInfo.packageName) ? resolved.priority : 0;
176 if (DEBUG) Log.d(LOG_TAG, "Adding category " + category.title);
177 }
178
179 return category;
180 }
181
182 private static void getTilesForAction(Context context,
Jason Monkf509d7e2016-01-07 16:22:53 -0500183 UserHandle user, String action, Map<Pair<String, String>, Tile> addedCache,
184 String defaultCategory, ArrayList<Tile> outTiles, boolean requireSettings) {
Jason Monk744b6362015-11-03 18:24:29 -0500185 Intent intent = new Intent(action);
Jason Monkf509d7e2016-01-07 16:22:53 -0500186 if (requireSettings) {
187 intent.setPackage(SETTING_PKG);
188 }
189 getTilesForIntent(context, user, intent, addedCache, defaultCategory, outTiles,
190 requireSettings, true);
191 }
192
193 public static void getTilesForIntent(Context context, UserHandle user, Intent intent,
194 Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles,
195 boolean usePriority, boolean checkCategory) {
196 PackageManager pm = context.getPackageManager();
Jason Monk744b6362015-11-03 18:24:29 -0500197 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
198 PackageManager.GET_META_DATA, user.getIdentifier());
199 for (ResolveInfo resolved : results) {
Jason Monkf509d7e2016-01-07 16:22:53 -0500200 if (!resolved.system) {
201 // Do not allow any app to add to settings, only system ones.
202 continue;
Jason Monk744b6362015-11-03 18:24:29 -0500203 }
204 ActivityInfo activityInfo = resolved.activityInfo;
205 Bundle metaData = activityInfo.metaData;
206 String categoryKey = defaultCategory;
Jason Monkf509d7e2016-01-07 16:22:53 -0500207 if (checkCategory && ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY))
Jason Monk744b6362015-11-03 18:24:29 -0500208 && categoryKey == null) {
Jason Monkf509d7e2016-01-07 16:22:53 -0500209 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for intent "
210 + intent + " missing metadata "
Jason Monk744b6362015-11-03 18:24:29 -0500211 + (metaData == null ? "" : EXTRA_CATEGORY_KEY));
212 continue;
213 } else {
214 categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
215 }
216 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
217 activityInfo.name);
Jason Monkf509d7e2016-01-07 16:22:53 -0500218 Tile tile = addedCache.get(key);
Jason Monk744b6362015-11-03 18:24:29 -0500219 if (tile == null) {
Jason Monkf509d7e2016-01-07 16:22:53 -0500220 tile = new Tile();
Jason Monk744b6362015-11-03 18:24:29 -0500221 tile.intent = new Intent().setClassName(
222 activityInfo.packageName, activityInfo.name);
223 tile.category = categoryKey;
Jason Monkf509d7e2016-01-07 16:22:53 -0500224 tile.priority = usePriority ? resolved.priority : 0;
Jason Monke79790b2015-12-02 15:39:19 -0500225 tile.metaData = activityInfo.metaData;
226 updateTileData(context, tile, activityInfo, activityInfo.applicationInfo,
227 pm);
Jason Monk744b6362015-11-03 18:24:29 -0500228 if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title);
229
230 addedCache.put(key, tile);
231 }
232 if (!tile.userHandle.contains(user)) {
233 tile.userHandle.add(user);
234 }
235 if (!outTiles.contains(tile)) {
236 outTiles.add(tile);
237 }
238 }
239 }
240
241 private static DashboardCategory getCategory(List<DashboardCategory> target,
242 String categoryKey) {
243 for (DashboardCategory category : target) {
244 if (categoryKey.equals(category.key)) {
245 return category;
246 }
247 }
248 return null;
249 }
250
Jason Monkf509d7e2016-01-07 16:22:53 -0500251 private static boolean updateTileData(Context context, Tile tile,
Jason Monke79790b2015-12-02 15:39:19 -0500252 ActivityInfo activityInfo, ApplicationInfo applicationInfo, PackageManager pm) {
253 if (applicationInfo.isSystemApp()) {
254 int icon = 0;
255 CharSequence title = null;
256 String summary = null;
Jason Monk744b6362015-11-03 18:24:29 -0500257
Jason Monke79790b2015-12-02 15:39:19 -0500258 // Get the activity's meta-data
259 try {
260 Resources res = pm.getResourcesForApplication(
261 applicationInfo.packageName);
262 Bundle metaData = activityInfo.metaData;
Jason Monk744b6362015-11-03 18:24:29 -0500263
Jason Monke79790b2015-12-02 15:39:19 -0500264 if (res != null && metaData != null) {
265 if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) {
266 icon = metaData.getInt(META_DATA_PREFERENCE_ICON);
Jason Monk744b6362015-11-03 18:24:29 -0500267 }
Jason Monke79790b2015-12-02 15:39:19 -0500268 if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) {
Jason Monkf509d7e2016-01-07 16:22:53 -0500269 if (metaData.get(META_DATA_PREFERENCE_TITLE) instanceof Integer) {
270 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
271 } else {
272 title = metaData.getString(META_DATA_PREFERENCE_TITLE);
273 }
Jason Monk744b6362015-11-03 18:24:29 -0500274 }
Jason Monke79790b2015-12-02 15:39:19 -0500275 if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
Jason Monkf509d7e2016-01-07 16:22:53 -0500276 if (metaData.get(META_DATA_PREFERENCE_SUMMARY) instanceof Integer) {
277 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
278 } else {
279 summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY);
280 }
Jason Monk744b6362015-11-03 18:24:29 -0500281 }
Jason Monk744b6362015-11-03 18:24:29 -0500282 }
Jason Monke79790b2015-12-02 15:39:19 -0500283 } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
284 if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e);
Jason Monk744b6362015-11-03 18:24:29 -0500285 }
Jason Monke79790b2015-12-02 15:39:19 -0500286
287 // Set the preference title to the activity's label if no
288 // meta-data is found
289 if (TextUtils.isEmpty(title)) {
290 title = activityInfo.loadLabel(pm).toString();
291 }
292 if (icon == 0) {
293 icon = activityInfo.icon;
294 }
295
296 // Set icon, title and summary for the preference
297 tile.icon = Icon.createWithResource(activityInfo.packageName, icon);
298 tile.title = title;
299 tile.summary = summary;
300 // Replace the intent with this specific activity
301 tile.intent = new Intent().setClassName(activityInfo.packageName,
302 activityInfo.name);
303
304 return true;
Jason Monk744b6362015-11-03 18:24:29 -0500305 }
306
307 return false;
308 }
309
Hyunyoung Songbe6c4482016-05-04 10:23:06 -0700310 public static final Comparator<Tile> TILE_COMPARATOR =
Jason Monkf509d7e2016-01-07 16:22:53 -0500311 new Comparator<Tile>() {
Jason Monk744b6362015-11-03 18:24:29 -0500312 @Override
Jason Monkf509d7e2016-01-07 16:22:53 -0500313 public int compare(Tile lhs, Tile rhs) {
Jason Monk744b6362015-11-03 18:24:29 -0500314 return rhs.priority - lhs.priority;
315 }
316 };
317
318 private static final Comparator<DashboardCategory> CATEGORY_COMPARATOR =
319 new Comparator<DashboardCategory>() {
320 @Override
321 public int compare(DashboardCategory lhs, DashboardCategory rhs) {
322 return rhs.priority - lhs.priority;
323 }
324 };
325}