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