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 | |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 22 | import android.app.Fragment; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 25 | import android.content.DialogInterface; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.PackageManager; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 31 | import android.content.pm.UserInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 32 | import android.content.res.Resources; |
| 33 | import android.content.res.Resources.NotFoundException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 34 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
| 36 | import android.graphics.BitmapFactory; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 37 | import android.graphics.drawable.Drawable; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 38 | import android.net.ConnectivityManager; |
| 39 | import android.net.LinkProperties; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 40 | import android.net.Uri; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 41 | import android.os.BatteryManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 42 | import android.os.Bundle; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
| 44 | import android.os.UserManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 45 | import android.preference.Preference; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceFrameLayout; |
| 47 | import android.preference.PreferenceGroup; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.CommonDataKinds; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 49 | import android.provider.ContactsContract.Contacts; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 50 | import android.provider.ContactsContract.Data; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 51 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 52 | import android.provider.ContactsContract.RawContacts; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 53 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 54 | import android.text.TextUtils; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 55 | import android.view.View; |
| 56 | import android.view.ViewGroup; |
| 57 | import android.widget.ListView; |
| 58 | import android.widget.TabWidget; |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 59 | import com.android.settings.dashboard.DashboardCategory; |
| 60 | import com.android.settings.dashboard.DashboardTile; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 61 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 62 | import java.io.IOException; |
| 63 | import java.io.InputStream; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 64 | import java.net.InetAddress; |
| 65 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 66 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 67 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 68 | |
Fabrice Di Meglio | c9711be | 2014-06-17 12:30:18 -0700 | [diff] [blame] | 69 | public final class Utils { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Set the preference's title to the matching activity's label. |
| 73 | */ |
| 74 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 75 | |
| 76 | /** |
Shuhrat Dehkanov | 9657768 | 2012-10-03 12:24:07 +0900 | [diff] [blame] | 77 | * The opacity level of a disabled icon. |
| 78 | */ |
| 79 | public static final float DISABLED_ALPHA = 0.4f; |
| 80 | |
| 81 | /** |
Dianne Hackborn | 68f005f | 2014-06-18 18:29:12 -0700 | [diff] [blame^] | 82 | * Color spectrum to use to indicate badness. 0 is completely transparent (no data), |
| 83 | * 1 is most bad (red), the last value is least bad (green). |
| 84 | */ |
| 85 | public static final int[] BADNESS_COLORS = new int[] { |
| 86 | 0x00000000, 0xffc43828, 0xffe54918, 0xfff47b00, |
| 87 | 0xfffabf2c, 0xff679e37, 0xff0a7f42 |
| 88 | }; |
| 89 | |
| 90 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 91 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 92 | * to specify the icon that should be displayed for the preference. |
| 93 | */ |
| 94 | private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
| 95 | |
| 96 | /** |
| 97 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 98 | * to specify the title that should be displayed for the preference. |
| 99 | */ |
| 100 | private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
| 101 | |
| 102 | /** |
| 103 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 104 | * to specify the summary text that should be displayed for the preference. |
| 105 | */ |
| 106 | private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
| 107 | |
| 108 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 109 | * Finds a matching activity for a preference's intent. If a matching |
| 110 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 111 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 112 | * @param context The context. |
| 113 | * @param parentPreferenceGroup The preference group that contains the |
| 114 | * preference whose intent is being resolved. |
| 115 | * @param preferenceKey The key of the preference whose intent is being |
| 116 | * resolved. |
| 117 | * @param flags 0 or one or more of |
| 118 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 119 | * . |
| 120 | * @return Whether an activity was found. If false, the preference was |
| 121 | * removed. |
| 122 | */ |
| 123 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 124 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 125 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 126 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 127 | if (preference == null) { |
| 128 | return false; |
| 129 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 130 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 131 | Intent intent = preference.getIntent(); |
| 132 | if (intent != null) { |
| 133 | // Find the activity that is in the system image |
| 134 | PackageManager pm = context.getPackageManager(); |
| 135 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 136 | int listSize = list.size(); |
| 137 | for (int i = 0; i < listSize; i++) { |
| 138 | ResolveInfo resolveInfo = list.get(i); |
| 139 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 140 | != 0) { |
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 | // Replace the intent with this specific activity |
| 143 | preference.setIntent(new Intent().setClassName( |
| 144 | resolveInfo.activityInfo.packageName, |
| 145 | resolveInfo.activityInfo.name)); |
| 146 | |
| 147 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 148 | // Set the preference title to the activity's label |
| 149 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 150 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 151 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Did not find a matching activity, so remove the preference |
| 158 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 159 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 160 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 161 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 162 | |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 163 | public static boolean updateTileToSpecificActivityFromMetaDataOrRemove(Context context, |
| 164 | DashboardCategory target, DashboardTile tile) { |
| 165 | |
| 166 | Intent intent = tile.intent; |
| 167 | if (intent != null) { |
| 168 | // Find the activity that is in the system image |
| 169 | PackageManager pm = context.getPackageManager(); |
| 170 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 171 | int listSize = list.size(); |
| 172 | for (int i = 0; i < listSize; i++) { |
| 173 | ResolveInfo resolveInfo = list.get(i); |
| 174 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 175 | != 0) { |
| 176 | Drawable icon = null; |
| 177 | String title = null; |
| 178 | String summary = null; |
| 179 | |
| 180 | // Get the activity's meta-data |
| 181 | try { |
| 182 | Resources res = pm.getResourcesForApplication( |
| 183 | resolveInfo.activityInfo.packageName); |
| 184 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 185 | |
| 186 | if (res != null && metaData != null) { |
| 187 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 188 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 189 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 190 | } |
| 191 | } catch (NameNotFoundException e) { |
| 192 | // Ignore |
| 193 | } catch (NotFoundException e) { |
| 194 | // Ignore |
| 195 | } |
| 196 | |
| 197 | // Set the preference title to the activity's label if no |
| 198 | // meta-data is found |
| 199 | if (TextUtils.isEmpty(title)) { |
| 200 | title = resolveInfo.loadLabel(pm).toString(); |
| 201 | } |
| 202 | |
| 203 | // Set icon, title and summary for the preference |
| 204 | // TODO: |
| 205 | //tile.icon = icon; |
| 206 | tile.title = title; |
| 207 | tile.summary = summary; |
| 208 | // Replace the intent with this specific activity |
| 209 | tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, |
| 210 | resolveInfo.activityInfo.name); |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Did not find a matching activity, so remove the preference |
| 218 | target.removeTile(tile); |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 223 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 224 | * Returns true if Monkey is running. |
| 225 | */ |
| 226 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 227 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 228 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 232 | */ |
| 233 | public static boolean isVoiceCapable(Context context) { |
| 234 | TelephonyManager telephony = |
| 235 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 236 | return telephony != null && telephony.isVoiceCapable(); |
| 237 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 238 | |
Robert Greenwalt | 8af88fb | 2011-08-31 11:17:47 -0700 | [diff] [blame] | 239 | public static boolean isWifiOnly(Context context) { |
| 240 | ConnectivityManager cm = (ConnectivityManager)context.getSystemService( |
| 241 | Context.CONNECTIVITY_SERVICE); |
| 242 | return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 243 | } |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 247 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 248 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 249 | */ |
| 250 | public static String getWifiIpAddresses(Context context) { |
| 251 | ConnectivityManager cm = (ConnectivityManager) |
| 252 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 253 | LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 254 | return formatIpAddresses(prop); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style |
| 259 | * 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 | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 262 | */ |
Lorenzo Colitti | 6eb6a90 | 2013-11-08 03:53:29 +0900 | [diff] [blame] | 263 | public static String getDefaultIpAddresses(ConnectivityManager cm) { |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 264 | LinkProperties prop = cm.getActiveLinkProperties(); |
| 265 | return formatIpAddresses(prop); |
| 266 | } |
| 267 | |
| 268 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 269 | if (prop == null) return null; |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 270 | Iterator<InetAddress> iter = prop.getAllAddresses().iterator(); |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 271 | // If there are no entries, return null |
| 272 | if (!iter.hasNext()) return null; |
| 273 | // Concatenate all available addresses, comma separated |
| 274 | String addresses = ""; |
| 275 | while (iter.hasNext()) { |
| 276 | addresses += iter.next().getHostAddress(); |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 277 | if (iter.hasNext()) addresses += "\n"; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 278 | } |
| 279 | return addresses; |
| 280 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 281 | |
| 282 | public static Locale createLocaleFromString(String localeStr) { |
| 283 | // TODO: is there a better way to actually construct a locale that will match? |
| 284 | // The main problem is, on top of Java specs, locale.toString() and |
| 285 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 286 | // many cases, because the constructor takes the only string as the language |
| 287 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 288 | // And : new Locale("en_US").toString() => "en_us" |
| 289 | if (null == localeStr) |
| 290 | return Locale.getDefault(); |
| 291 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 292 | // split may not return a 0-length array. |
| 293 | if (1 == brokenDownLocale.length) { |
| 294 | return new Locale(brokenDownLocale[0]); |
| 295 | } else if (2 == brokenDownLocale.length) { |
| 296 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 297 | } else { |
| 298 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 299 | } |
| 300 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 301 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 302 | public static boolean isBatteryPresent(Intent batteryChangedIntent) { |
| 303 | return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 304 | } |
| 305 | |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 306 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 307 | return String.valueOf(getBatteryLevel(batteryChangedIntent)) + "%"; |
| 308 | } |
| 309 | |
| 310 | public static int getBatteryLevel(Intent batteryChangedIntent) { |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 311 | int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); |
| 312 | int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 313 | return (level * 100) / scale; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
| 317 | final Intent intent = batteryChangedIntent; |
| 318 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 319 | int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); |
| 320 | int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, |
| 321 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 322 | String statusString; |
| 323 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 324 | int resId; |
| 325 | if (plugType == BatteryManager.BATTERY_PLUGGED_AC) { |
| 326 | resId = R.string.battery_info_status_charging_ac; |
| 327 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) { |
| 328 | resId = R.string.battery_info_status_charging_usb; |
| 329 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { |
| 330 | resId = R.string.battery_info_status_charging_wireless; |
| 331 | } else { |
| 332 | resId = R.string.battery_info_status_charging; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 333 | } |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 334 | statusString = res.getString(resId); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 335 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 336 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 337 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 338 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 339 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 340 | statusString = res.getString(R.string.battery_info_status_full); |
| 341 | } else { |
| 342 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 343 | } |
| 344 | |
| 345 | return statusString; |
| 346 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 347 | |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 348 | public static void forcePrepareCustomPreferencesList( |
| 349 | ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) { |
| 350 | list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
| 351 | list.setClipToPadding(false); |
| 352 | prepareCustomPreferencesList(parent, child, list, ignoreSidePadding); |
| 353 | } |
| 354 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 355 | /** |
| 356 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 357 | * when outside scrollbars are requested. Usually used to display |
| 358 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 359 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 360 | public static void prepareCustomPreferencesList( |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 361 | ViewGroup parent, View child, View list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 362 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
| 363 | if (movePadding && parent instanceof PreferenceFrameLayout) { |
| 364 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
| 365 | |
| 366 | final Resources res = list.getResources(); |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 367 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 368 | final int paddingBottom = res.getDimensionPixelSize( |
| 369 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 370 | |
Amith Yamasani | f0b05de | 2012-05-09 09:43:33 -0700 | [diff] [blame] | 371 | final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; |
Fabrice Di Meglio | b27223f | 2013-01-15 18:54:11 -0800 | [diff] [blame] | 372 | list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Return string resource that best describes combination of tethering |
| 378 | * options available on this device. |
| 379 | */ |
| 380 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 381 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 382 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 383 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 384 | |
| 385 | boolean usbAvailable = usbRegexs.length != 0; |
| 386 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 387 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 388 | |
| 389 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 390 | return R.string.tether_settings_title_all; |
| 391 | } else if (wifiAvailable && usbAvailable) { |
| 392 | return R.string.tether_settings_title_all; |
| 393 | } else if (wifiAvailable && bluetoothAvailable) { |
| 394 | return R.string.tether_settings_title_all; |
| 395 | } else if (wifiAvailable) { |
| 396 | return R.string.tether_settings_title_wifi; |
| 397 | } else if (usbAvailable && bluetoothAvailable) { |
| 398 | return R.string.tether_settings_title_usb_bluetooth; |
| 399 | } else if (usbAvailable) { |
| 400 | return R.string.tether_settings_title_usb; |
| 401 | } else { |
| 402 | return R.string.tether_settings_title_bluetooth; |
| 403 | } |
| 404 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 405 | |
| 406 | /* Used by UserSettings as well. Call this on a non-ui thread. */ |
| 407 | public static boolean copyMeProfilePhoto(Context context, UserInfo user) { |
| 408 | Uri contactUri = Profile.CONTENT_URI; |
| 409 | |
| 410 | InputStream avatarDataStream = Contacts.openContactPhotoInputStream( |
| 411 | context.getContentResolver(), |
| 412 | contactUri, true); |
| 413 | // If there's no profile photo, assign a default avatar |
| 414 | if (avatarDataStream == null) { |
| 415 | return false; |
| 416 | } |
| 417 | int userId = user != null ? user.id : UserHandle.myUserId(); |
| 418 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 419 | Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); |
| 420 | um.setUserIcon(userId, icon); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 421 | try { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 422 | avatarDataStream.close(); |
| 423 | } catch (IOException ioe) { } |
| 424 | return true; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 427 | public static String getMeProfileName(Context context, boolean full) { |
| 428 | if (full) { |
| 429 | return getProfileDisplayName(context); |
| 430 | } else { |
| 431 | return getShorterNameIfPossible(context); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 432 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | private static String getShorterNameIfPossible(Context context) { |
| 436 | final String given = getLocalProfileGivenName(context); |
| 437 | return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); |
| 438 | } |
| 439 | |
| 440 | private static String getLocalProfileGivenName(Context context) { |
| 441 | final ContentResolver cr = context.getContentResolver(); |
| 442 | |
| 443 | // Find the raw contact ID for the local ME profile raw contact. |
| 444 | final long localRowProfileId; |
| 445 | final Cursor localRawProfile = cr.query( |
| 446 | Profile.CONTENT_RAW_CONTACTS_URI, |
| 447 | new String[] {RawContacts._ID}, |
| 448 | RawContacts.ACCOUNT_TYPE + " IS NULL AND " + |
| 449 | RawContacts.ACCOUNT_NAME + " IS NULL", |
| 450 | null, null); |
| 451 | if (localRawProfile == null) return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 452 | |
| 453 | try { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 454 | if (!localRawProfile.moveToFirst()) { |
| 455 | return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 456 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 457 | localRowProfileId = localRawProfile.getLong(0); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 458 | } finally { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 459 | localRawProfile.close(); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 460 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 461 | |
| 462 | // Find the structured name for the raw contact. |
| 463 | final Cursor structuredName = cr.query( |
| 464 | Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), |
| 465 | new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, |
| 466 | CommonDataKinds.StructuredName.FAMILY_NAME}, |
| 467 | Data.RAW_CONTACT_ID + "=" + localRowProfileId, |
| 468 | null, null); |
| 469 | if (structuredName == null) return null; |
| 470 | |
| 471 | try { |
| 472 | if (!structuredName.moveToFirst()) { |
| 473 | return null; |
| 474 | } |
| 475 | String partialName = structuredName.getString(0); |
| 476 | if (TextUtils.isEmpty(partialName)) { |
| 477 | partialName = structuredName.getString(1); |
| 478 | } |
| 479 | return partialName; |
| 480 | } finally { |
| 481 | structuredName.close(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private static final String getProfileDisplayName(Context context) { |
| 486 | final ContentResolver cr = context.getContentResolver(); |
| 487 | final Cursor profile = cr.query(Profile.CONTENT_URI, |
| 488 | new String[] {Profile.DISPLAY_NAME}, null, null, null); |
| 489 | if (profile == null) return null; |
| 490 | |
| 491 | try { |
| 492 | if (!profile.moveToFirst()) { |
| 493 | return null; |
| 494 | } |
| 495 | return profile.getString(0); |
| 496 | } finally { |
| 497 | profile.close(); |
| 498 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 499 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 500 | |
| 501 | /** Not global warming, it's global change warning. */ |
| 502 | public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId, |
| 503 | final Runnable positiveAction) { |
| 504 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 505 | builder.setTitle(titleResId); |
| 506 | builder.setMessage(R.string.global_change_warning); |
| 507 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 508 | @Override |
| 509 | public void onClick(DialogInterface dialog, int which) { |
| 510 | positiveAction.run(); |
| 511 | } |
| 512 | }); |
| 513 | builder.setNegativeButton(android.R.string.cancel, null); |
| 514 | |
| 515 | return builder.create(); |
| 516 | } |
| 517 | |
| 518 | public static boolean hasMultipleUsers(Context context) { |
| 519 | return ((UserManager) context.getSystemService(Context.USER_SERVICE)) |
| 520 | .getUsers().size() > 1; |
| 521 | } |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 522 | |
| 523 | /** |
| 524 | * Start a new instance of the activity, showing only the given fragment. |
| 525 | * When launched in this mode, the given preference fragment will be instantiated and fill the |
| 526 | * entire activity. |
| 527 | * |
| 528 | * @param context The context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 529 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 530 | * @param args Optional arguments to supply to the fragment. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 531 | * @param resultTo Option fragment that should receive the result of the activity launch. |
| 532 | * @param resultRequestCode If resultTo is non-null, this is the request code in which |
| 533 | * to report the result. |
| 534 | * @param titleResId resource id for the String to display for the title of this set |
| 535 | * of preferences. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 536 | * @param title String to display for the title of this set of preferences. |
| 537 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 538 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 539 | Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title) { |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 540 | startWithFragment(context, fragmentName, args, resultTo, resultRequestCode, |
| 541 | titleResId, title, false /* not a shortcut */); |
| 542 | } |
| 543 | |
| 544 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
| 545 | Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title, |
| 546 | boolean isShortcut) { |
| 547 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResId, |
| 548 | title, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 549 | if (resultTo == null) { |
| 550 | context.startActivity(intent); |
| 551 | } else { |
| 552 | resultTo.startActivityForResult(intent, resultRequestCode); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Build an Intent to launch a new activity showing the selected fragment. |
| 558 | * The implementation constructs an Intent that re-launches the current activity with the |
| 559 | * appropriate arguments to display the fragment. |
| 560 | * |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 561 | * |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 562 | * @param context The Context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 563 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 564 | * @param args Optional arguments to supply to the fragment. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 565 | * @param titleResId Optional title resource id to show for this item. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 566 | * @param title Optional title to show for this item. |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 567 | * @param isShortcut tell if this is a Launcher Shortcut or not |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 568 | * @return Returns an Intent that can be launched to display the given |
| 569 | * fragment. |
| 570 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 571 | public static Intent onBuildStartFragmentIntent(Context context, String fragmentName, |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 572 | Bundle args, int titleResId, CharSequence title, boolean isShortcut) { |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 573 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 574 | intent.setClass(context, SubSettings.class); |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 575 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, fragmentName); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 576 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 577 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, titleResId); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 578 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE, title); |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 579 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 580 | return intent; |
| 581 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 582 | } |