Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 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 |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 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 |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 15 | */ |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 16 | package com.android.settingslib.drawer; |
| 17 | |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 18 | import android.app.ActivityManager; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 19 | import android.content.Context; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 20 | import android.content.IContentProvider; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.pm.ActivityInfo; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.content.res.Resources; |
| 27 | import android.graphics.drawable.Icon; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 28 | import android.net.Uri; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 29 | import android.os.Bundle; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 30 | import android.os.RemoteException; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 31 | import android.os.UserHandle; |
| 32 | import android.os.UserManager; |
Jason Monk | 64600cf | 2016-06-30 13:15:48 -0400 | [diff] [blame] | 33 | import android.provider.Settings.Global; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 34 | import android.text.TextUtils; |
| 35 | import android.util.Log; |
| 36 | import android.util.Pair; |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 37 | import android.widget.RemoteViews; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Comparator; |
| 42 | import java.util.HashMap; |
| 43 | import java.util.List; |
| 44 | import java.util.Map; |
| 45 | |
| 46 | public class TileUtils { |
| 47 | |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 48 | private static final boolean DEBUG = false; |
Joe Onorato | 93dcff0 | 2016-02-01 17:44:14 -0800 | [diff] [blame] | 49 | private static final boolean DEBUG_TIMING = false; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 50 | |
| 51 | private static final String LOG_TAG = "TileUtils"; |
| 52 | |
| 53 | /** |
| 54 | * Settings will search for system activities of this action and add them as a top level |
| 55 | * settings tile using the following parameters. |
| 56 | * |
| 57 | * <p>A category must be specified in the meta-data for the activity named |
| 58 | * {@link #EXTRA_CATEGORY_KEY} |
| 59 | * |
| 60 | * <p>The title may be defined by meta-data named {@link #META_DATA_PREFERENCE_TITLE} |
| 61 | * otherwise the label for the activity will be used. |
| 62 | * |
| 63 | * <p>The icon may be defined by meta-data named {@link #META_DATA_PREFERENCE_ICON} |
| 64 | * otherwise the icon for the activity will be used. |
| 65 | * |
| 66 | * <p>A summary my be defined by meta-data named {@link #META_DATA_PREFERENCE_SUMMARY} |
| 67 | */ |
| 68 | private static final String EXTRA_SETTINGS_ACTION = |
| 69 | "com.android.settings.action.EXTRA_SETTINGS"; |
| 70 | |
| 71 | /** |
Fan Zhang | ca60fac | 2016-11-02 15:54:53 -0700 | [diff] [blame] | 72 | * @See {@link #EXTRA_SETTINGS_ACTION}. |
| 73 | */ |
| 74 | private static final String IA_SETTINGS_ACTION = |
| 75 | "com.android.settings.action.IA_SETTINGS"; |
| 76 | |
| 77 | |
| 78 | /** |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 79 | * Same as #EXTRA_SETTINGS_ACTION but used for the platform Settings activities. |
| 80 | */ |
| 81 | private static final String SETTINGS_ACTION = |
| 82 | "com.android.settings.action.SETTINGS"; |
| 83 | |
| 84 | private static final String OPERATOR_SETTINGS = |
| 85 | "com.android.settings.OPERATOR_APPLICATION_SETTING"; |
| 86 | |
| 87 | private static final String OPERATOR_DEFAULT_CATEGORY = |
| 88 | "com.android.settings.category.wireless"; |
| 89 | |
| 90 | private static final String MANUFACTURER_SETTINGS = |
| 91 | "com.android.settings.MANUFACTURER_APPLICATION_SETTING"; |
| 92 | |
| 93 | private static final String MANUFACTURER_DEFAULT_CATEGORY = |
| 94 | "com.android.settings.category.device"; |
| 95 | |
| 96 | /** |
| 97 | * The key used to get the category from metadata of activities of action |
| 98 | * {@link #EXTRA_SETTINGS_ACTION} |
| 99 | * The value must be one of: |
| 100 | * <li>com.android.settings.category.wireless</li> |
| 101 | * <li>com.android.settings.category.device</li> |
| 102 | * <li>com.android.settings.category.personal</li> |
| 103 | * <li>com.android.settings.category.system</li> |
| 104 | */ |
| 105 | private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category"; |
| 106 | |
| 107 | /** |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 108 | * The key used to get the package name of the icon resource for the preference. |
| 109 | */ |
| 110 | private static final String EXTRA_PREFERENCE_ICON_PACKAGE = |
| 111 | "com.android.settings.icon_package"; |
| 112 | |
| 113 | /** |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 114 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 115 | * to specify the key that should be used for the preference. |
| 116 | */ |
| 117 | public static final String META_DATA_PREFERENCE_KEYHINT = "com.android.settings.keyhint"; |
| 118 | |
| 119 | /** |
| 120 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 121 | * to specify the icon that should be displayed for the preference. |
| 122 | */ |
| 123 | public static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
| 124 | |
| 125 | /** |
| 126 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 127 | * to specify the content provider providing the icon that should be displayed for |
| 128 | * the preference. |
| 129 | * |
| 130 | * Icon provided by the content provider overrides any static icon. |
| 131 | */ |
| 132 | public static final String META_DATA_PREFERENCE_ICON_URI = "com.android.settings.icon_uri"; |
| 133 | |
| 134 | /** |
| 135 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 136 | * to specify whether the icon is tintable. This should be a boolean value {@code true} or |
| 137 | * {@code false}, set using {@code android:value} |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 138 | */ |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 139 | public static final String META_DATA_PREFERENCE_ICON_TINTABLE = |
| 140 | "com.android.settings.icon_tintable"; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 144 | * to specify the title that should be displayed for the preference. |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 145 | * |
| 146 | * <p>Note: It is preferred to provide this value using {@code android:resource} with a string |
| 147 | * resource for localization. |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 148 | */ |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 149 | public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
| 150 | |
| 151 | /** |
| 152 | * @deprecated Use {@link #META_DATA_PREFERENCE_TITLE} with {@code android:resource} |
| 153 | */ |
| 154 | @Deprecated |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 155 | public static final String META_DATA_PREFERENCE_TITLE_RES_ID = |
| 156 | "com.android.settings.title.resid"; |
| 157 | |
| 158 | /** |
| 159 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 160 | * to specify the summary text that should be displayed for the preference. |
| 161 | */ |
| 162 | public static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
| 163 | |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 166 | * to specify the content provider providing the summary text that should be displayed for the |
| 167 | * preference. |
| 168 | * |
| 169 | * Summary provided by the content provider overrides any static summary. |
| 170 | */ |
| 171 | public static final String META_DATA_PREFERENCE_SUMMARY_URI = |
| 172 | "com.android.settings.summary_uri"; |
| 173 | |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Name of the meta-data item that should be set in the AndroidManifest.xml to specify the |
| 176 | * custom view which should be displayed for the preference. The custom view will be inflated |
| 177 | * as a remote view. |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 178 | * |
| 179 | * This also can be used with {@link META_DATA_PREFERENCE_SUMMARY_URI} above, by setting the id |
| 180 | * of the summary TextView to '@android:id/summary'. |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 181 | */ |
| 182 | public static final String META_DATA_PREFERENCE_CUSTOM_VIEW = |
| 183 | "com.android.settings.custom_view"; |
| 184 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 185 | public static final String SETTING_PKG = "com.android.settings"; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 186 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Build a list of DashboardCategory. Each category must be defined in manifest. |
| 189 | * eg: .Settings$DeviceSettings |
| 190 | * @deprecated |
| 191 | */ |
| 192 | @Deprecated |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 193 | public static List<DashboardCategory> getCategories(Context context, |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 194 | Map<Pair<String, String>, Tile> cache) { |
| 195 | return getCategories(context, cache, true /*categoryDefinedInManifest*/); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Build a list of DashboardCategory. |
| 200 | * @param categoryDefinedInManifest If true, an dummy activity must exists in manifest to |
| 201 | * represent this category (eg: .Settings$DeviceSettings) |
| 202 | */ |
| 203 | public static List<DashboardCategory> getCategories(Context context, |
| 204 | Map<Pair<String, String>, Tile> cache, boolean categoryDefinedInManifest) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 205 | return getCategories(context, cache, categoryDefinedInManifest, null, SETTING_PKG); |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Build a list of DashboardCategory. |
| 210 | * @param categoryDefinedInManifest If true, an dummy activity must exists in manifest to |
| 211 | * represent this category (eg: .Settings$DeviceSettings) |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 212 | * @param extraAction additional intent filter action to be usetileutild to build the dashboard |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 213 | * categories |
| 214 | */ |
| 215 | public static List<DashboardCategory> getCategories(Context context, |
| 216 | Map<Pair<String, String>, Tile> cache, boolean categoryDefinedInManifest, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 217 | String extraAction, String settingPkg) { |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 218 | final long startTime = System.currentTimeMillis(); |
Jason Monk | 64600cf | 2016-06-30 13:15:48 -0400 | [diff] [blame] | 219 | boolean setup = Global.getInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0) |
| 220 | != 0; |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 221 | ArrayList<Tile> tiles = new ArrayList<>(); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 222 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 223 | for (UserHandle user : userManager.getUserProfiles()) { |
| 224 | // TODO: Needs much optimization, too many PM queries going on here. |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 225 | if (user.getIdentifier() == ActivityManager.getCurrentUser()) { |
| 226 | // Only add Settings for this user. |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 227 | getTilesForAction(context, user, SETTINGS_ACTION, cache, null, tiles, true, |
| 228 | settingPkg); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 229 | getTilesForAction(context, user, OPERATOR_SETTINGS, cache, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 230 | OPERATOR_DEFAULT_CATEGORY, tiles, false, true, settingPkg); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 231 | getTilesForAction(context, user, MANUFACTURER_SETTINGS, cache, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 232 | MANUFACTURER_DEFAULT_CATEGORY, tiles, false, true, settingPkg); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 233 | } |
Jason Monk | 64600cf | 2016-06-30 13:15:48 -0400 | [diff] [blame] | 234 | if (setup) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 235 | getTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false, |
| 236 | settingPkg); |
Fan Zhang | 8bfb3ea | 2017-01-05 14:19:30 -0800 | [diff] [blame] | 237 | if (!categoryDefinedInManifest) { |
| 238 | getTilesForAction(context, user, IA_SETTINGS_ACTION, cache, null, tiles, false, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 239 | settingPkg); |
Fan Zhang | 8bfb3ea | 2017-01-05 14:19:30 -0800 | [diff] [blame] | 240 | if (extraAction != null) { |
| 241 | getTilesForAction(context, user, extraAction, cache, null, tiles, false, |
| 242 | settingPkg); |
| 243 | } |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 244 | } |
Jason Monk | 64600cf | 2016-06-30 13:15:48 -0400 | [diff] [blame] | 245 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 246 | } |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 247 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 248 | HashMap<String, DashboardCategory> categoryMap = new HashMap<>(); |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 249 | for (Tile tile : tiles) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 250 | DashboardCategory category = categoryMap.get(tile.category); |
| 251 | if (category == null) { |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 252 | category = createCategory(context, tile.category, categoryDefinedInManifest); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 253 | if (category == null) { |
| 254 | Log.w(LOG_TAG, "Couldn't find category " + tile.category); |
| 255 | continue; |
| 256 | } |
| 257 | categoryMap.put(category.key, category); |
| 258 | } |
| 259 | category.addTile(tile); |
| 260 | } |
| 261 | ArrayList<DashboardCategory> categories = new ArrayList<>(categoryMap.values()); |
| 262 | for (DashboardCategory category : categories) { |
| 263 | Collections.sort(category.tiles, TILE_COMPARATOR); |
| 264 | } |
| 265 | Collections.sort(categories, CATEGORY_COMPARATOR); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 266 | if (DEBUG_TIMING) Log.d(LOG_TAG, "getCategories took " |
| 267 | + (System.currentTimeMillis() - startTime) + " ms"); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 268 | return categories; |
| 269 | } |
| 270 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Create a new DashboardCategory from key. |
| 273 | * |
| 274 | * @param context Context to query intent |
| 275 | * @param categoryKey The category key |
| 276 | * @param categoryDefinedInManifest If true, an dummy activity must exists in manifest to |
| 277 | * represent this category (eg: .Settings$DeviceSettings) |
| 278 | */ |
| 279 | private static DashboardCategory createCategory(Context context, String categoryKey, |
| 280 | boolean categoryDefinedInManifest) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 281 | DashboardCategory category = new DashboardCategory(); |
| 282 | category.key = categoryKey; |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 283 | if (!categoryDefinedInManifest) { |
| 284 | return category; |
| 285 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 286 | PackageManager pm = context.getPackageManager(); |
| 287 | List<ResolveInfo> results = pm.queryIntentActivities(new Intent(categoryKey), 0); |
| 288 | if (results.size() == 0) { |
| 289 | return null; |
| 290 | } |
| 291 | for (ResolveInfo resolved : results) { |
| 292 | if (!resolved.system) { |
| 293 | // Do not allow any app to add to settings, only system ones. |
| 294 | continue; |
| 295 | } |
| 296 | category.title = resolved.activityInfo.loadLabel(pm); |
| 297 | category.priority = SETTING_PKG.equals( |
| 298 | resolved.activityInfo.applicationInfo.packageName) ? resolved.priority : 0; |
| 299 | if (DEBUG) Log.d(LOG_TAG, "Adding category " + category.title); |
| 300 | } |
| 301 | |
| 302 | return category; |
| 303 | } |
| 304 | |
| 305 | private static void getTilesForAction(Context context, |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 306 | UserHandle user, String action, Map<Pair<String, String>, Tile> addedCache, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 307 | String defaultCategory, ArrayList<Tile> outTiles, boolean requireSettings, |
| 308 | String settingPkg) { |
Yoshinori Hirano | 4adbbfc | 2016-06-06 15:47:47 +0900 | [diff] [blame] | 309 | getTilesForAction(context, user, action, addedCache, defaultCategory, outTiles, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 310 | requireSettings, requireSettings, settingPkg); |
Yoshinori Hirano | 4adbbfc | 2016-06-06 15:47:47 +0900 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | private static void getTilesForAction(Context context, |
| 314 | UserHandle user, String action, Map<Pair<String, String>, Tile> addedCache, |
| 315 | String defaultCategory, ArrayList<Tile> outTiles, boolean requireSettings, |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 316 | boolean usePriority, String settingPkg) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 317 | Intent intent = new Intent(action); |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 318 | if (requireSettings) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 319 | intent.setPackage(settingPkg); |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 320 | } |
| 321 | getTilesForIntent(context, user, intent, addedCache, defaultCategory, outTiles, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 322 | usePriority, true, true); |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 325 | public static void getTilesForIntent( |
| 326 | Context context, UserHandle user, Intent intent, |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 327 | Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 328 | boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 329 | PackageManager pm = context.getPackageManager(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 330 | List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent, |
| 331 | PackageManager.GET_META_DATA, user.getIdentifier()); |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 332 | Map<String, IContentProvider> providerMap = new HashMap<>(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 333 | for (ResolveInfo resolved : results) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 334 | if (!resolved.system) { |
| 335 | // Do not allow any app to add to settings, only system ones. |
| 336 | continue; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 337 | } |
| 338 | ActivityInfo activityInfo = resolved.activityInfo; |
| 339 | Bundle metaData = activityInfo.metaData; |
| 340 | String categoryKey = defaultCategory; |
Fan Zhang | b12e197 | 2016-09-29 14:43:43 -0700 | [diff] [blame] | 341 | |
| 342 | // Load category |
| 343 | if (checkCategory && ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) |
| 344 | && categoryKey == null) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 345 | Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for intent " |
| 346 | + intent + " missing metadata " |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 347 | + (metaData == null ? "" : EXTRA_CATEGORY_KEY)); |
| 348 | continue; |
Fan Zhang | b12e197 | 2016-09-29 14:43:43 -0700 | [diff] [blame] | 349 | } else { |
| 350 | categoryKey = metaData.getString(EXTRA_CATEGORY_KEY); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 351 | } |
Fan Zhang | b12e197 | 2016-09-29 14:43:43 -0700 | [diff] [blame] | 352 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 353 | Pair<String, String> key = new Pair<String, String>(activityInfo.packageName, |
| 354 | activityInfo.name); |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 355 | Tile tile = addedCache.get(key); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 356 | if (tile == null) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 357 | tile = new Tile(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 358 | tile.intent = new Intent().setClassName( |
| 359 | activityInfo.packageName, activityInfo.name); |
| 360 | tile.category = categoryKey; |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 361 | tile.priority = usePriority ? resolved.priority : 0; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 362 | tile.metaData = activityInfo.metaData; |
| 363 | updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 364 | pm, providerMap, forceTintExternalIcon); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 365 | if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title); |
| 366 | |
| 367 | addedCache.put(key, tile); |
| 368 | } |
| 369 | if (!tile.userHandle.contains(user)) { |
| 370 | tile.userHandle.add(user); |
| 371 | } |
| 372 | if (!outTiles.contains(tile)) { |
| 373 | outTiles.add(tile); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 378 | private static boolean updateTileData(Context context, Tile tile, |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 379 | ActivityInfo activityInfo, ApplicationInfo applicationInfo, PackageManager pm, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 380 | Map<String, IContentProvider> providerMap, boolean forceTintExternalIcon) { |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 381 | if (applicationInfo.isSystemApp()) { |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 382 | boolean forceTintIcon = false; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 383 | int icon = 0; |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 384 | Pair<String, Integer> iconFromUri = null; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 385 | CharSequence title = null; |
| 386 | String summary = null; |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 387 | String keyHint = null; |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 388 | boolean isIconTintable = false; |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 389 | RemoteViews remoteViews = null; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 390 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 391 | // Get the activity's meta-data |
| 392 | try { |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 393 | Resources res = pm.getResourcesForApplication(applicationInfo.packageName); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 394 | Bundle metaData = activityInfo.metaData; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 395 | |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 396 | if (forceTintExternalIcon |
| 397 | && !context.getPackageName().equals(applicationInfo.packageName)) { |
| 398 | isIconTintable = true; |
| 399 | forceTintIcon = true; |
| 400 | } |
| 401 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 402 | if (res != null && metaData != null) { |
William Luh | 204af1c | 2017-02-23 11:10:05 -0800 | [diff] [blame] | 403 | if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) { |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 404 | icon = metaData.getInt(META_DATA_PREFERENCE_ICON); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 405 | } |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 406 | if (metaData.containsKey(META_DATA_PREFERENCE_ICON_TINTABLE)) { |
| 407 | if (forceTintIcon) { |
| 408 | Log.w(LOG_TAG, "Ignoring icon tintable for " + activityInfo); |
| 409 | } else { |
| 410 | isIconTintable = |
| 411 | metaData.getBoolean(META_DATA_PREFERENCE_ICON_TINTABLE); |
| 412 | } |
| 413 | } |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 414 | int resId = 0; |
| 415 | if (metaData.containsKey(META_DATA_PREFERENCE_TITLE_RES_ID)) { |
| 416 | resId = metaData.getInt(META_DATA_PREFERENCE_TITLE_RES_ID); |
| 417 | if (resId != 0) { |
| 418 | title = res.getString(resId); |
| 419 | } |
| 420 | } |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 421 | if ((resId == 0) && metaData.containsKey(META_DATA_PREFERENCE_TITLE)) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 422 | if (metaData.get(META_DATA_PREFERENCE_TITLE) instanceof Integer) { |
| 423 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 424 | } else { |
| 425 | title = metaData.getString(META_DATA_PREFERENCE_TITLE); |
| 426 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 427 | } |
William Luh | 204af1c | 2017-02-23 11:10:05 -0800 | [diff] [blame] | 428 | if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) { |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 429 | if (metaData.get(META_DATA_PREFERENCE_SUMMARY) instanceof Integer) { |
| 430 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 431 | } else { |
| 432 | summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY); |
| 433 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 434 | } |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 435 | if (metaData.containsKey(META_DATA_PREFERENCE_KEYHINT)) { |
| 436 | if (metaData.get(META_DATA_PREFERENCE_KEYHINT) instanceof Integer) { |
| 437 | keyHint = res.getString(metaData.getInt(META_DATA_PREFERENCE_KEYHINT)); |
| 438 | } else { |
| 439 | keyHint = metaData.getString(META_DATA_PREFERENCE_KEYHINT); |
| 440 | } |
| 441 | } |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 442 | if (metaData.containsKey(META_DATA_PREFERENCE_CUSTOM_VIEW)) { |
| 443 | int layoutId = metaData.getInt(META_DATA_PREFERENCE_CUSTOM_VIEW); |
| 444 | remoteViews = new RemoteViews(applicationInfo.packageName, layoutId); |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 445 | if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) { |
| 446 | String uriString = metaData.getString( |
| 447 | META_DATA_PREFERENCE_SUMMARY_URI); |
| 448 | String overrideSummary = getTextFromUri(context, uriString, providerMap, |
| 449 | META_DATA_PREFERENCE_SUMMARY); |
| 450 | if (overrideSummary != null) { |
| 451 | remoteViews.setTextViewText(android.R.id.summary, overrideSummary); |
| 452 | } |
| 453 | } |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 454 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 455 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 456 | } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { |
| 457 | if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 458 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 459 | |
| 460 | // Set the preference title to the activity's label if no |
| 461 | // meta-data is found |
| 462 | if (TextUtils.isEmpty(title)) { |
| 463 | title = activityInfo.loadLabel(pm).toString(); |
| 464 | } |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 465 | |
| 466 | // Set the icon |
| 467 | if (iconFromUri != null) { |
| 468 | tile.icon = Icon.createWithResource(iconFromUri.first, iconFromUri.second); |
| 469 | } else { |
| 470 | if (icon == 0) { |
| 471 | icon = activityInfo.icon; |
| 472 | } |
| 473 | tile.icon = Icon.createWithResource(activityInfo.packageName, icon); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 476 | // Set title and summary for the preference |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 477 | tile.title = title; |
| 478 | tile.summary = summary; |
| 479 | // Replace the intent with this specific activity |
| 480 | tile.intent = new Intent().setClassName(activityInfo.packageName, |
| 481 | activityInfo.name); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 482 | // Suggest a key for this tile |
| 483 | tile.key = keyHint; |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 484 | tile.isIconTintable = isIconTintable; |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 485 | tile.remoteViews = remoteViews; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 486 | |
| 487 | return true; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | return false; |
| 491 | } |
| 492 | |
Shahriyar Amini | 1fc3aee | 2016-12-28 08:51:04 -0800 | [diff] [blame] | 493 | /** |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 494 | * Gets the icon package name and resource id from content provider. |
Shahriyar Amini | 1fc3aee | 2016-12-28 08:51:04 -0800 | [diff] [blame] | 495 | * @param Context context |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 496 | * @param packageName package name of the target activity |
Shahriyar Amini | 1fc3aee | 2016-12-28 08:51:04 -0800 | [diff] [blame] | 497 | * @param uriString URI for the content provider |
| 498 | * @param providerMap Maps URI authorities to providers |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 499 | * @return package name and resource id of the icon specified |
Shahriyar Amini | 1fc3aee | 2016-12-28 08:51:04 -0800 | [diff] [blame] | 500 | */ |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 501 | public static Pair<String, Integer> getIconFromUri(Context context, String packageName, |
| 502 | String uriString, Map<String, IContentProvider> providerMap) { |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 503 | Bundle bundle = getBundleFromUri(context, uriString, providerMap); |
Shahriyar Amini | 778cf1d | 2017-01-18 18:52:27 -0800 | [diff] [blame] | 504 | if (bundle == null) { |
| 505 | return null; |
| 506 | } |
| 507 | String iconPackageName = bundle.getString(EXTRA_PREFERENCE_ICON_PACKAGE); |
| 508 | if (TextUtils.isEmpty(iconPackageName)) { |
| 509 | return null; |
| 510 | } |
| 511 | int resId = bundle.getInt(META_DATA_PREFERENCE_ICON, 0); |
| 512 | if (resId == 0) { |
| 513 | return null; |
| 514 | } |
| 515 | // Icon can either come from the target package or from the Settings app. |
| 516 | if (iconPackageName.equals(packageName) |
| 517 | || iconPackageName.equals(context.getPackageName())) { |
| 518 | return Pair.create(iconPackageName, bundle.getInt(META_DATA_PREFERENCE_ICON, 0)); |
| 519 | } |
| 520 | return null; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Shahriyar Amini | 1fc3aee | 2016-12-28 08:51:04 -0800 | [diff] [blame] | 523 | /** |
| 524 | * Gets text associated with the input key from the content provider. |
| 525 | * @param Context context |
| 526 | * @param uriString URI for the content provider |
| 527 | * @param providerMap Maps URI authorities to providers |
| 528 | * @param key Key mapping to the text in bundle returned by the content provider |
| 529 | * @return Text associated with the key, if returned by the content provider |
| 530 | */ |
| 531 | public static String getTextFromUri(Context context, String uriString, |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 532 | Map<String, IContentProvider> providerMap, String key) { |
| 533 | Bundle bundle = getBundleFromUri(context, uriString, providerMap); |
| 534 | return (bundle != null) ? bundle.getString(key) : null; |
| 535 | } |
| 536 | |
| 537 | private static Bundle getBundleFromUri(Context context, String uriString, |
| 538 | Map<String, IContentProvider> providerMap) { |
| 539 | if (TextUtils.isEmpty(uriString)) { |
| 540 | return null; |
| 541 | } |
| 542 | Uri uri = Uri.parse(uriString); |
| 543 | String method = getMethodFromUri(uri); |
| 544 | if (TextUtils.isEmpty(method)) { |
| 545 | return null; |
| 546 | } |
| 547 | IContentProvider provider = getProviderFromUri(context, uri, providerMap); |
| 548 | if (provider == null) { |
| 549 | return null; |
| 550 | } |
| 551 | try { |
| 552 | return provider.call(context.getPackageName(), method, uriString, null); |
| 553 | } catch (RemoteException e) { |
| 554 | return null; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private static IContentProvider getProviderFromUri(Context context, Uri uri, |
| 559 | Map<String, IContentProvider> providerMap) { |
| 560 | if (uri == null) { |
| 561 | return null; |
| 562 | } |
| 563 | String authority = uri.getAuthority(); |
| 564 | if (TextUtils.isEmpty(authority)) { |
| 565 | return null; |
| 566 | } |
| 567 | if (!providerMap.containsKey(authority)) { |
William Luh | 5a0a0d8 | 2017-04-18 11:34:38 -0700 | [diff] [blame] | 568 | providerMap.put(authority, context.getContentResolver().acquireUnstableProvider(uri)); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 569 | } |
| 570 | return providerMap.get(authority); |
| 571 | } |
| 572 | |
| 573 | /** Returns the first path segment of the uri if it exists as the method, otherwise null. */ |
| 574 | static String getMethodFromUri(Uri uri) { |
| 575 | if (uri == null) { |
| 576 | return null; |
| 577 | } |
| 578 | List<String> pathSegments = uri.getPathSegments(); |
| 579 | if ((pathSegments == null) || pathSegments.isEmpty()) { |
| 580 | return null; |
| 581 | } |
| 582 | return pathSegments.get(0); |
| 583 | } |
| 584 | |
Hyunyoung Song | be6c448 | 2016-05-04 10:23:06 -0700 | [diff] [blame] | 585 | public static final Comparator<Tile> TILE_COMPARATOR = |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 586 | new Comparator<Tile>() { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 587 | @Override |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 588 | public int compare(Tile lhs, Tile rhs) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 589 | return rhs.priority - lhs.priority; |
| 590 | } |
| 591 | }; |
| 592 | |
| 593 | private static final Comparator<DashboardCategory> CATEGORY_COMPARATOR = |
| 594 | new Comparator<DashboardCategory>() { |
| 595 | @Override |
| 596 | public int compare(DashboardCategory lhs, DashboardCategory rhs) { |
| 597 | return rhs.priority - lhs.priority; |
| 598 | } |
| 599 | }; |
| 600 | } |