The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2007 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 19 | import static android.content.Intent.EXTRA_USER; |
| 20 | |
| 21 | import android.annotation.Nullable; |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 23 | import android.app.ActivityManagerNative; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 27 | import android.app.IActivityManager; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 28 | import android.content.ContentResolver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 30 | import android.content.DialogInterface; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.pm.ApplicationInfo; |
| 33 | import android.content.pm.PackageManager; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 35 | import android.content.pm.ResolveInfo; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 37 | import android.content.res.Resources; |
| 38 | import android.content.res.Resources.NotFoundException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 39 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 40 | import android.graphics.Bitmap; |
| 41 | import android.graphics.BitmapFactory; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 43 | import android.net.ConnectivityManager; |
| 44 | import android.net.LinkProperties; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 46 | import android.os.BatteryManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 47 | import android.os.Bundle; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.RemoteException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
| 51 | import android.os.UserManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 52 | import android.preference.Preference; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 53 | import android.preference.PreferenceFrameLayout; |
| 54 | import android.preference.PreferenceGroup; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 55 | import android.provider.ContactsContract.CommonDataKinds; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 56 | import android.provider.ContactsContract.Contacts; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 57 | import android.provider.ContactsContract.Data; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 58 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 59 | import android.provider.ContactsContract.RawContacts; |
Andres Morales | ce249fe | 2014-07-07 16:58:16 -0700 | [diff] [blame] | 60 | import android.service.persistentdata.PersistentDataBlockManager; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 61 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 62 | import android.text.TextUtils; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 63 | import android.util.Log; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 64 | import android.view.View; |
| 65 | import android.view.ViewGroup; |
| 66 | import android.widget.ListView; |
| 67 | import android.widget.TabWidget; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 68 | |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 69 | import com.android.settings.dashboard.DashboardCategory; |
| 70 | import com.android.settings.dashboard.DashboardTile; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 71 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 72 | import java.io.IOException; |
| 73 | import java.io.InputStream; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 74 | import java.net.InetAddress; |
| 75 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 76 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 77 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 78 | |
Fabrice Di Meglio | c9711be | 2014-06-17 12:30:18 -0700 | [diff] [blame] | 79 | public final class Utils { |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 80 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 81 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Set the preference's title to the matching activity's label. |
| 84 | */ |
| 85 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 86 | |
| 87 | /** |
Shuhrat Dehkanov | 9657768 | 2012-10-03 12:24:07 +0900 | [diff] [blame] | 88 | * The opacity level of a disabled icon. |
| 89 | */ |
| 90 | public static final float DISABLED_ALPHA = 0.4f; |
| 91 | |
| 92 | /** |
Dianne Hackborn | 68f005f | 2014-06-18 18:29:12 -0700 | [diff] [blame] | 93 | * Color spectrum to use to indicate badness. 0 is completely transparent (no data), |
| 94 | * 1 is most bad (red), the last value is least bad (green). |
| 95 | */ |
| 96 | public static final int[] BADNESS_COLORS = new int[] { |
| 97 | 0x00000000, 0xffc43828, 0xffe54918, 0xfff47b00, |
| 98 | 0xfffabf2c, 0xff679e37, 0xff0a7f42 |
| 99 | }; |
| 100 | |
| 101 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 102 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 103 | * to specify the icon that should be displayed for the preference. |
| 104 | */ |
| 105 | private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
| 106 | |
| 107 | /** |
| 108 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 109 | * to specify the title that should be displayed for the preference. |
| 110 | */ |
| 111 | private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
| 112 | |
| 113 | /** |
| 114 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 115 | * to specify the summary text that should be displayed for the preference. |
| 116 | */ |
| 117 | private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
| 118 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 119 | private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; |
| 120 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 121 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 122 | * Finds a matching activity for a preference's intent. If a matching |
| 123 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 124 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 125 | * @param context The context. |
| 126 | * @param parentPreferenceGroup The preference group that contains the |
| 127 | * preference whose intent is being resolved. |
| 128 | * @param preferenceKey The key of the preference whose intent is being |
| 129 | * resolved. |
| 130 | * @param flags 0 or one or more of |
| 131 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 132 | * . |
| 133 | * @return Whether an activity was found. If false, the preference was |
| 134 | * removed. |
| 135 | */ |
| 136 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 137 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 138 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 139 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 140 | if (preference == null) { |
| 141 | return false; |
| 142 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 144 | Intent intent = preference.getIntent(); |
| 145 | if (intent != null) { |
| 146 | // Find the activity that is in the system image |
| 147 | PackageManager pm = context.getPackageManager(); |
| 148 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 149 | int listSize = list.size(); |
| 150 | for (int i = 0; i < listSize; i++) { |
| 151 | ResolveInfo resolveInfo = list.get(i); |
| 152 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 153 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 154 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 155 | // Replace the intent with this specific activity |
| 156 | preference.setIntent(new Intent().setClassName( |
| 157 | resolveInfo.activityInfo.packageName, |
| 158 | resolveInfo.activityInfo.name)); |
| 159 | |
| 160 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 161 | // Set the preference title to the activity's label |
| 162 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 163 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 164 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Did not find a matching activity, so remove the preference |
| 171 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 172 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 173 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 174 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 175 | |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 176 | public static boolean updateTileToSpecificActivityFromMetaDataOrRemove(Context context, |
| 177 | DashboardCategory target, DashboardTile tile) { |
| 178 | |
| 179 | Intent intent = tile.intent; |
| 180 | if (intent != null) { |
| 181 | // Find the activity that is in the system image |
| 182 | PackageManager pm = context.getPackageManager(); |
| 183 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 184 | int listSize = list.size(); |
| 185 | for (int i = 0; i < listSize; i++) { |
| 186 | ResolveInfo resolveInfo = list.get(i); |
| 187 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 188 | != 0) { |
| 189 | Drawable icon = null; |
| 190 | String title = null; |
| 191 | String summary = null; |
| 192 | |
| 193 | // Get the activity's meta-data |
| 194 | try { |
| 195 | Resources res = pm.getResourcesForApplication( |
| 196 | resolveInfo.activityInfo.packageName); |
| 197 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 198 | |
| 199 | if (res != null && metaData != null) { |
| 200 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 201 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 202 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 203 | } |
| 204 | } catch (NameNotFoundException e) { |
| 205 | // Ignore |
| 206 | } catch (NotFoundException e) { |
| 207 | // Ignore |
| 208 | } |
| 209 | |
| 210 | // Set the preference title to the activity's label if no |
| 211 | // meta-data is found |
| 212 | if (TextUtils.isEmpty(title)) { |
| 213 | title = resolveInfo.loadLabel(pm).toString(); |
| 214 | } |
| 215 | |
| 216 | // Set icon, title and summary for the preference |
| 217 | // TODO: |
| 218 | //tile.icon = icon; |
| 219 | tile.title = title; |
| 220 | tile.summary = summary; |
| 221 | // Replace the intent with this specific activity |
| 222 | tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, |
| 223 | resolveInfo.activityInfo.name); |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Did not find a matching activity, so remove the preference |
| 231 | target.removeTile(tile); |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 236 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 237 | * Returns true if Monkey is running. |
| 238 | */ |
| 239 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 240 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 241 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 245 | */ |
| 246 | public static boolean isVoiceCapable(Context context) { |
| 247 | TelephonyManager telephony = |
| 248 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 249 | return telephony != null && telephony.isVoiceCapable(); |
| 250 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 251 | |
Robert Greenwalt | 8af88fb | 2011-08-31 11:17:47 -0700 | [diff] [blame] | 252 | public static boolean isWifiOnly(Context context) { |
| 253 | ConnectivityManager cm = (ConnectivityManager)context.getSystemService( |
| 254 | Context.CONNECTIVITY_SERVICE); |
| 255 | return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 256 | } |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 260 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 261 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 262 | */ |
| 263 | public static String getWifiIpAddresses(Context context) { |
| 264 | ConnectivityManager cm = (ConnectivityManager) |
| 265 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 266 | LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 267 | return formatIpAddresses(prop); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style |
| 272 | * addresses. |
| 273 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 274 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 275 | */ |
Lorenzo Colitti | 6eb6a90 | 2013-11-08 03:53:29 +0900 | [diff] [blame] | 276 | public static String getDefaultIpAddresses(ConnectivityManager cm) { |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 277 | LinkProperties prop = cm.getActiveLinkProperties(); |
| 278 | return formatIpAddresses(prop); |
| 279 | } |
| 280 | |
| 281 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 282 | if (prop == null) return null; |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 283 | Iterator<InetAddress> iter = prop.getAllAddresses().iterator(); |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 284 | // If there are no entries, return null |
| 285 | if (!iter.hasNext()) return null; |
| 286 | // Concatenate all available addresses, comma separated |
| 287 | String addresses = ""; |
| 288 | while (iter.hasNext()) { |
| 289 | addresses += iter.next().getHostAddress(); |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 290 | if (iter.hasNext()) addresses += "\n"; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 291 | } |
| 292 | return addresses; |
| 293 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 294 | |
| 295 | public static Locale createLocaleFromString(String localeStr) { |
| 296 | // TODO: is there a better way to actually construct a locale that will match? |
| 297 | // The main problem is, on top of Java specs, locale.toString() and |
| 298 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 299 | // many cases, because the constructor takes the only string as the language |
| 300 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 301 | // And : new Locale("en_US").toString() => "en_us" |
| 302 | if (null == localeStr) |
| 303 | return Locale.getDefault(); |
| 304 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 305 | // split may not return a 0-length array. |
| 306 | if (1 == brokenDownLocale.length) { |
| 307 | return new Locale(brokenDownLocale[0]); |
| 308 | } else if (2 == brokenDownLocale.length) { |
| 309 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 310 | } else { |
| 311 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 312 | } |
| 313 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 314 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 315 | public static boolean isBatteryPresent(Intent batteryChangedIntent) { |
| 316 | return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 317 | } |
| 318 | |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 319 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 320 | return String.valueOf(getBatteryLevel(batteryChangedIntent)) + "%"; |
| 321 | } |
| 322 | |
| 323 | public static int getBatteryLevel(Intent batteryChangedIntent) { |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 324 | int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); |
| 325 | int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 326 | return (level * 100) / scale; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
| 330 | final Intent intent = batteryChangedIntent; |
| 331 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 332 | int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); |
| 333 | int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, |
| 334 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 335 | String statusString; |
| 336 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 337 | int resId; |
| 338 | if (plugType == BatteryManager.BATTERY_PLUGGED_AC) { |
| 339 | resId = R.string.battery_info_status_charging_ac; |
| 340 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) { |
| 341 | resId = R.string.battery_info_status_charging_usb; |
| 342 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { |
| 343 | resId = R.string.battery_info_status_charging_wireless; |
| 344 | } else { |
| 345 | resId = R.string.battery_info_status_charging; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 346 | } |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 347 | statusString = res.getString(resId); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 348 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 349 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 350 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 351 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 352 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 353 | statusString = res.getString(R.string.battery_info_status_full); |
| 354 | } else { |
| 355 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 356 | } |
| 357 | |
| 358 | return statusString; |
| 359 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 360 | |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 361 | public static void forcePrepareCustomPreferencesList( |
| 362 | ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) { |
| 363 | list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
| 364 | list.setClipToPadding(false); |
| 365 | prepareCustomPreferencesList(parent, child, list, ignoreSidePadding); |
| 366 | } |
| 367 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 368 | /** |
| 369 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 370 | * when outside scrollbars are requested. Usually used to display |
| 371 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 372 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 373 | public static void prepareCustomPreferencesList( |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 374 | ViewGroup parent, View child, View list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 375 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
| 376 | if (movePadding && parent instanceof PreferenceFrameLayout) { |
| 377 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
| 378 | |
| 379 | final Resources res = list.getResources(); |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 380 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 381 | final int paddingBottom = res.getDimensionPixelSize( |
| 382 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 383 | |
Amith Yamasani | f0b05de | 2012-05-09 09:43:33 -0700 | [diff] [blame] | 384 | final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; |
Fabrice Di Meglio | b27223f | 2013-01-15 18:54:11 -0800 | [diff] [blame] | 385 | list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * Return string resource that best describes combination of tethering |
| 391 | * options available on this device. |
| 392 | */ |
| 393 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 394 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 395 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 396 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 397 | |
| 398 | boolean usbAvailable = usbRegexs.length != 0; |
| 399 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 400 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 401 | |
| 402 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 403 | return R.string.tether_settings_title_all; |
| 404 | } else if (wifiAvailable && usbAvailable) { |
| 405 | return R.string.tether_settings_title_all; |
| 406 | } else if (wifiAvailable && bluetoothAvailable) { |
| 407 | return R.string.tether_settings_title_all; |
| 408 | } else if (wifiAvailable) { |
| 409 | return R.string.tether_settings_title_wifi; |
| 410 | } else if (usbAvailable && bluetoothAvailable) { |
| 411 | return R.string.tether_settings_title_usb_bluetooth; |
| 412 | } else if (usbAvailable) { |
| 413 | return R.string.tether_settings_title_usb; |
| 414 | } else { |
| 415 | return R.string.tether_settings_title_bluetooth; |
| 416 | } |
| 417 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 418 | |
| 419 | /* Used by UserSettings as well. Call this on a non-ui thread. */ |
| 420 | public static boolean copyMeProfilePhoto(Context context, UserInfo user) { |
| 421 | Uri contactUri = Profile.CONTENT_URI; |
| 422 | |
| 423 | InputStream avatarDataStream = Contacts.openContactPhotoInputStream( |
| 424 | context.getContentResolver(), |
| 425 | contactUri, true); |
| 426 | // If there's no profile photo, assign a default avatar |
| 427 | if (avatarDataStream == null) { |
| 428 | return false; |
| 429 | } |
| 430 | int userId = user != null ? user.id : UserHandle.myUserId(); |
| 431 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 432 | Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); |
| 433 | um.setUserIcon(userId, icon); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 434 | try { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 435 | avatarDataStream.close(); |
| 436 | } catch (IOException ioe) { } |
| 437 | return true; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 440 | public static String getMeProfileName(Context context, boolean full) { |
| 441 | if (full) { |
| 442 | return getProfileDisplayName(context); |
| 443 | } else { |
| 444 | return getShorterNameIfPossible(context); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 445 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | private static String getShorterNameIfPossible(Context context) { |
| 449 | final String given = getLocalProfileGivenName(context); |
| 450 | return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); |
| 451 | } |
| 452 | |
| 453 | private static String getLocalProfileGivenName(Context context) { |
| 454 | final ContentResolver cr = context.getContentResolver(); |
| 455 | |
| 456 | // Find the raw contact ID for the local ME profile raw contact. |
| 457 | final long localRowProfileId; |
| 458 | final Cursor localRawProfile = cr.query( |
| 459 | Profile.CONTENT_RAW_CONTACTS_URI, |
| 460 | new String[] {RawContacts._ID}, |
| 461 | RawContacts.ACCOUNT_TYPE + " IS NULL AND " + |
| 462 | RawContacts.ACCOUNT_NAME + " IS NULL", |
| 463 | null, null); |
| 464 | if (localRawProfile == null) return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 465 | |
| 466 | try { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 467 | if (!localRawProfile.moveToFirst()) { |
| 468 | return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 469 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 470 | localRowProfileId = localRawProfile.getLong(0); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 471 | } finally { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 472 | localRawProfile.close(); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 473 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 474 | |
| 475 | // Find the structured name for the raw contact. |
| 476 | final Cursor structuredName = cr.query( |
| 477 | Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), |
| 478 | new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, |
| 479 | CommonDataKinds.StructuredName.FAMILY_NAME}, |
| 480 | Data.RAW_CONTACT_ID + "=" + localRowProfileId, |
| 481 | null, null); |
| 482 | if (structuredName == null) return null; |
| 483 | |
| 484 | try { |
| 485 | if (!structuredName.moveToFirst()) { |
| 486 | return null; |
| 487 | } |
| 488 | String partialName = structuredName.getString(0); |
| 489 | if (TextUtils.isEmpty(partialName)) { |
| 490 | partialName = structuredName.getString(1); |
| 491 | } |
| 492 | return partialName; |
| 493 | } finally { |
| 494 | structuredName.close(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | private static final String getProfileDisplayName(Context context) { |
| 499 | final ContentResolver cr = context.getContentResolver(); |
| 500 | final Cursor profile = cr.query(Profile.CONTENT_URI, |
| 501 | new String[] {Profile.DISPLAY_NAME}, null, null, null); |
| 502 | if (profile == null) return null; |
| 503 | |
| 504 | try { |
| 505 | if (!profile.moveToFirst()) { |
| 506 | return null; |
| 507 | } |
| 508 | return profile.getString(0); |
| 509 | } finally { |
| 510 | profile.close(); |
| 511 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 512 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 513 | |
| 514 | /** Not global warming, it's global change warning. */ |
| 515 | public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId, |
| 516 | final Runnable positiveAction) { |
| 517 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 518 | builder.setTitle(titleResId); |
| 519 | builder.setMessage(R.string.global_change_warning); |
| 520 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 521 | @Override |
| 522 | public void onClick(DialogInterface dialog, int which) { |
| 523 | positiveAction.run(); |
| 524 | } |
| 525 | }); |
| 526 | builder.setNegativeButton(android.R.string.cancel, null); |
| 527 | |
| 528 | return builder.create(); |
| 529 | } |
| 530 | |
| 531 | public static boolean hasMultipleUsers(Context context) { |
| 532 | return ((UserManager) context.getSystemService(Context.USER_SERVICE)) |
| 533 | .getUsers().size() > 1; |
| 534 | } |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * Start a new instance of the activity, showing only the given fragment. |
| 538 | * When launched in this mode, the given preference fragment will be instantiated and fill the |
| 539 | * entire activity. |
| 540 | * |
| 541 | * @param context The context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 542 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 543 | * @param args Optional arguments to supply to the fragment. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 544 | * @param resultTo Option fragment that should receive the result of the activity launch. |
| 545 | * @param resultRequestCode If resultTo is non-null, this is the request code in which |
| 546 | * to report the result. |
| 547 | * @param titleResId resource id for the String to display for the title of this set |
| 548 | * of preferences. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 549 | * @param title String to display for the title of this set of preferences. |
| 550 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 551 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 552 | Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title) { |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 553 | startWithFragment(context, fragmentName, args, resultTo, resultRequestCode, |
| 554 | titleResId, title, false /* not a shortcut */); |
| 555 | } |
| 556 | |
| 557 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
| 558 | Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title, |
| 559 | boolean isShortcut) { |
| 560 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResId, |
| 561 | title, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 562 | if (resultTo == null) { |
| 563 | context.startActivity(intent); |
| 564 | } else { |
| 565 | resultTo.startActivityForResult(intent, resultRequestCode); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Build an Intent to launch a new activity showing the selected fragment. |
| 571 | * The implementation constructs an Intent that re-launches the current activity with the |
| 572 | * appropriate arguments to display the fragment. |
| 573 | * |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 574 | * |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 575 | * @param context The Context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 576 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 577 | * @param args Optional arguments to supply to the fragment. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 578 | * @param titleResId Optional title resource id to show for this item. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 579 | * @param title Optional title to show for this item. |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 580 | * @param isShortcut tell if this is a Launcher Shortcut or not |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 581 | * @return Returns an Intent that can be launched to display the given |
| 582 | * fragment. |
| 583 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 584 | public static Intent onBuildStartFragmentIntent(Context context, String fragmentName, |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 585 | Bundle args, int titleResId, CharSequence title, boolean isShortcut) { |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 586 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 587 | intent.setClass(context, SubSettings.class); |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 588 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, fragmentName); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 589 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 590 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, titleResId); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 591 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE, title); |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 592 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 593 | return intent; |
| 594 | } |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 595 | |
| 596 | /** |
| 597 | * Returns the managed profile of the current user or null if none found. |
| 598 | */ |
| 599 | public static UserHandle getManagedProfile(UserManager userManager) { |
| 600 | List<UserHandle> userProfiles = userManager.getUserProfiles(); |
| 601 | final int count = userProfiles.size(); |
| 602 | for (int i = 0; i < count; i++) { |
| 603 | final UserHandle profile = userProfiles.get(i); |
| 604 | if (profile.getIdentifier() == userManager.getUserHandle()) { |
| 605 | continue; |
| 606 | } |
| 607 | final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier()); |
| 608 | if (userInfo.isManagedProfile()) { |
| 609 | return profile; |
| 610 | } |
| 611 | } |
| 612 | return null; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Returns true if the current profile is a managed one. |
| 617 | */ |
| 618 | public static boolean isManagedProfile(UserManager userManager) { |
| 619 | UserInfo currentUser = userManager.getUserInfo(userManager.getUserHandle()); |
| 620 | return currentUser.isManagedProfile(); |
| 621 | } |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 622 | |
| 623 | /** |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 624 | * Returns the target user for a Settings activity. |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 625 | * |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 626 | * The target user can be either the current user, the user that launched this activity or |
| 627 | * the user contained as an extra in the arguments or intent extras. |
| 628 | * |
| 629 | * Note: This is secure in the sense that it only returns a target user different to the current |
| 630 | * one if the app launching this activity is the Settings app itself, running in the same user |
| 631 | * or in one that is in the same profile group, or if the user id is provided by the system. |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 632 | */ |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 633 | public static UserHandle getSecureTargetUser(IBinder activityToken, |
| 634 | UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) { |
| 635 | UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
| 636 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 637 | try { |
| 638 | String launchedFromPackage = am.getLaunchedFromPackage(activityToken); |
| 639 | boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage); |
| 640 | |
| 641 | UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
| 642 | am.getLaunchedFromUid(activityToken))); |
| 643 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 644 | // Check it's secure |
| 645 | if (isProfileOf(um, launchedFromUser)) { |
| 646 | return launchedFromUser; |
| 647 | } |
| 648 | } |
| 649 | UserHandle extrasUser = intentExtras != null |
| 650 | ? (UserHandle) intentExtras.getParcelable(EXTRA_USER) : null; |
| 651 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 652 | // Check it's secure |
| 653 | if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) { |
| 654 | return extrasUser; |
| 655 | } |
| 656 | } |
| 657 | UserHandle argumentsUser = arguments != null |
| 658 | ? (UserHandle) arguments.getParcelable(EXTRA_USER) : null; |
| 659 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 660 | // Check it's secure |
| 661 | if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) { |
| 662 | return argumentsUser; |
| 663 | } |
| 664 | } |
| 665 | } catch (RemoteException e) { |
| 666 | // Should not happen |
| 667 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 668 | } |
| 669 | return currentUser; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Returns the target user for a Settings activity. |
| 674 | * |
| 675 | * The target user can be either the current user, the user that launched this activity or |
| 676 | * the user contained as an extra in the arguments or intent extras. |
| 677 | * |
| 678 | * You should use {@link #getSecureTargetUser(IBinder, UserManager, Bundle, Bundle)} if |
| 679 | * possible. |
| 680 | * |
| 681 | * @see #getInsecureTargetUser(IBinder, Bundle, Bundle) |
| 682 | */ |
| 683 | public static UserHandle getInsecureTargetUser(IBinder activityToken, @Nullable Bundle arguments, |
| 684 | @Nullable Bundle intentExtras) { |
| 685 | UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
| 686 | IActivityManager am = ActivityManagerNative.getDefault(); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 687 | try { |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 688 | UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
| 689 | am.getLaunchedFromUid(activityToken))); |
| 690 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 691 | return launchedFromUser; |
| 692 | } |
| 693 | UserHandle extrasUser = intentExtras != null |
| 694 | ? (UserHandle) intentExtras.getParcelable(EXTRA_USER) : null; |
| 695 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 696 | return extrasUser; |
| 697 | } |
| 698 | UserHandle argumentsUser = arguments != null |
| 699 | ? (UserHandle) arguments.getParcelable(EXTRA_USER) : null; |
| 700 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 701 | return argumentsUser; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 702 | } |
| 703 | } catch (RemoteException e) { |
| 704 | // Should not happen |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 705 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 706 | return null; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 707 | } |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 708 | return currentUser; |
| 709 | } |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 710 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame^] | 711 | /** |
| 712 | * Returns true if the user provided is in the same profiles group as the current user. |
| 713 | */ |
| 714 | private static boolean isProfileOf(UserManager um, UserHandle otherUser) { |
| 715 | if (um == null || otherUser == null) return false; |
| 716 | return (UserHandle.myUserId() == otherUser.getIdentifier()) |
| 717 | || um.getUserProfiles().contains(otherUser); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 718 | } |
Amith Yamasani | 51c6dac | 2014-07-02 00:06:37 +0530 | [diff] [blame] | 719 | |
| 720 | /** |
| 721 | * Creates a dialog to confirm with the user if it's ok to remove the user |
| 722 | * and delete all the data. |
| 723 | * |
| 724 | * @param context a Context object |
| 725 | * @param removingUserId The userId of the user to remove |
| 726 | * @param onConfirmListener Callback object for positive action |
| 727 | * @return the created Dialog |
| 728 | */ |
| 729 | public static Dialog createRemoveConfirmationDialog(Context context, int removingUserId, |
| 730 | DialogInterface.OnClickListener onConfirmListener) { |
| 731 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 732 | UserInfo userInfo = um.getUserInfo(removingUserId); |
| 733 | Dialog dlg = new AlertDialog.Builder(context) |
| 734 | .setTitle(UserHandle.myUserId() == removingUserId |
| 735 | ? R.string.user_confirm_remove_self_title |
| 736 | : (userInfo.isRestricted() |
| 737 | ? R.string.user_profile_confirm_remove_title |
| 738 | : R.string.user_confirm_remove_title)) |
| 739 | .setMessage(UserHandle.myUserId() == removingUserId |
| 740 | ? R.string.user_confirm_remove_self_message |
| 741 | : (userInfo.isRestricted() |
| 742 | ? R.string.user_profile_confirm_remove_message |
| 743 | : R.string.user_confirm_remove_message)) |
| 744 | .setPositiveButton(R.string.user_delete_button, |
| 745 | onConfirmListener) |
| 746 | .setNegativeButton(android.R.string.cancel, null) |
| 747 | .create(); |
| 748 | return dlg; |
| 749 | } |
Andres Morales | ce249fe | 2014-07-07 16:58:16 -0700 | [diff] [blame] | 750 | |
| 751 | /** |
| 752 | * Returns whether or not this device is able to be OEM unlocked. |
| 753 | */ |
| 754 | static boolean isOemUnlockEnabled(Context context) { |
| 755 | PersistentDataBlockManager manager =(PersistentDataBlockManager) |
| 756 | context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
| 757 | return manager.getOemUnlockEnabled(); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Allows enabling or disabling OEM unlock on this device. OEM unlocked |
| 762 | * devices allow users to flash other OSes to them. |
| 763 | */ |
| 764 | static void setOemUnlockEnabled(Context context, boolean enabled) { |
| 765 | PersistentDataBlockManager manager =(PersistentDataBlockManager) |
| 766 | context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
| 767 | manager.setOemUnlockEnabled(enabled); |
| 768 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 769 | } |