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 | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame^] | 19 | import android.app.Activity; |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame^] | 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame^] | 24 | import android.content.DialogInterface; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.pm.ApplicationInfo; |
| 27 | import android.content.pm.PackageManager; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 29 | import android.content.pm.ResolveInfo; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 31 | import android.content.res.Resources; |
| 32 | import android.content.res.Resources.NotFoundException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 33 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
| 35 | import android.graphics.BitmapFactory; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 36 | import android.graphics.drawable.Drawable; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 37 | import android.net.ConnectivityManager; |
| 38 | import android.net.LinkProperties; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 39 | import android.net.Uri; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 40 | import android.os.BatteryManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 41 | import android.os.Bundle; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 42 | import android.os.ParcelFileDescriptor; |
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; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceActivity.Header; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 47 | import android.preference.PreferenceFrameLayout; |
| 48 | import android.preference.PreferenceGroup; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 49 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 50 | import android.provider.ContactsContract.Contacts; |
| 51 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 52 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 53 | import android.text.TextUtils; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 54 | import android.util.Log; |
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; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 59 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 60 | import com.android.settings.users.ProfileUpdateReceiver; |
| 61 | |
| 62 | import java.io.FileOutputStream; |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 65 | import java.net.InetAddress; |
| 66 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 67 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 68 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 69 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 70 | public class Utils { |
| 71 | |
| 72 | /** |
| 73 | * Set the preference's title to the matching activity's label. |
| 74 | */ |
| 75 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 76 | |
| 77 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 78 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 79 | * to specify the icon that should be displayed for the preference. |
| 80 | */ |
| 81 | private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
| 82 | |
| 83 | /** |
| 84 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 85 | * to specify the title that should be displayed for the preference. |
| 86 | */ |
| 87 | private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
| 88 | |
| 89 | /** |
| 90 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 91 | * to specify the summary text that should be displayed for the preference. |
| 92 | */ |
| 93 | private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
| 94 | |
| 95 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 96 | * Finds a matching activity for a preference's intent. If a matching |
| 97 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 98 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 99 | * @param context The context. |
| 100 | * @param parentPreferenceGroup The preference group that contains the |
| 101 | * preference whose intent is being resolved. |
| 102 | * @param preferenceKey The key of the preference whose intent is being |
| 103 | * resolved. |
| 104 | * @param flags 0 or one or more of |
| 105 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 106 | * . |
| 107 | * @return Whether an activity was found. If false, the preference was |
| 108 | * removed. |
| 109 | */ |
| 110 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 111 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 112 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 113 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 114 | if (preference == null) { |
| 115 | return false; |
| 116 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 117 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 118 | Intent intent = preference.getIntent(); |
| 119 | if (intent != null) { |
| 120 | // Find the activity that is in the system image |
| 121 | PackageManager pm = context.getPackageManager(); |
| 122 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 123 | int listSize = list.size(); |
| 124 | for (int i = 0; i < listSize; i++) { |
| 125 | ResolveInfo resolveInfo = list.get(i); |
| 126 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 127 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 128 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 129 | // Replace the intent with this specific activity |
| 130 | preference.setIntent(new Intent().setClassName( |
| 131 | resolveInfo.activityInfo.packageName, |
| 132 | resolveInfo.activityInfo.name)); |
| 133 | |
| 134 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 135 | // Set the preference title to the activity's label |
| 136 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 137 | } |
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 | return true; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Did not find a matching activity, so remove the preference |
| 145 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 146 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 147 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 148 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 149 | |
| 150 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 151 | * Finds a matching activity for a preference's intent. If a matching |
| 152 | * activity is not found, it will remove the preference. The icon, title and |
| 153 | * summary of the preference will also be updated with the values retrieved |
| 154 | * from the activity's meta-data elements. If no meta-data elements are |
| 155 | * specified then the preference title will be set to match the label of the |
| 156 | * activity, an icon and summary text will not be displayed. |
| 157 | * |
| 158 | * @param context The context. |
| 159 | * @param parentPreferenceGroup The preference group that contains the |
| 160 | * preference whose intent is being resolved. |
| 161 | * @param preferenceKey The key of the preference whose intent is being |
| 162 | * resolved. |
| 163 | * |
| 164 | * @return Whether an activity was found. If false, the preference was |
| 165 | * removed. |
| 166 | * |
| 167 | * @see {@link #META_DATA_PREFERENCE_ICON} |
| 168 | * {@link #META_DATA_PREFERENCE_TITLE} |
| 169 | * {@link #META_DATA_PREFERENCE_SUMMARY} |
| 170 | */ |
| 171 | public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context, |
| 172 | PreferenceGroup parentPreferenceGroup, String preferenceKey) { |
| 173 | |
| 174 | IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup |
| 175 | .findPreference(preferenceKey); |
| 176 | if (preference == null) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | Intent intent = preference.getIntent(); |
| 181 | if (intent != null) { |
| 182 | // Find the activity that is in the system image |
| 183 | PackageManager pm = context.getPackageManager(); |
| 184 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 185 | int listSize = list.size(); |
| 186 | for (int i = 0; i < listSize; i++) { |
| 187 | ResolveInfo resolveInfo = list.get(i); |
| 188 | if ((resolveInfo.activityInfo.applicationInfo.flags |
| 189 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 190 | Drawable icon = null; |
| 191 | String title = null; |
| 192 | String summary = null; |
| 193 | |
| 194 | // Get the activity's meta-data |
| 195 | try { |
| 196 | Resources res = pm |
| 197 | .getResourcesForApplication(resolveInfo.activityInfo.packageName); |
| 198 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 199 | |
| 200 | if (res != null && metaData != null) { |
| 201 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 202 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 203 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 204 | } |
| 205 | } catch (NameNotFoundException e) { |
| 206 | // Ignore |
| 207 | } catch (NotFoundException e) { |
| 208 | // Ignore |
| 209 | } |
| 210 | |
| 211 | // Set the preference title to the activity's label if no |
| 212 | // meta-data is found |
| 213 | if (TextUtils.isEmpty(title)) { |
| 214 | title = resolveInfo.loadLabel(pm).toString(); |
| 215 | } |
| 216 | |
| 217 | // Set icon, title and summary for the preference |
| 218 | preference.setIcon(icon); |
| 219 | preference.setTitle(title); |
| 220 | preference.setSummary(summary); |
| 221 | |
| 222 | // Replace the intent with this specific activity |
| 223 | preference.setIntent(new Intent().setClassName( |
| 224 | resolveInfo.activityInfo.packageName, |
| 225 | resolveInfo.activityInfo.name)); |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Did not find a matching activity, so remove the preference |
| 233 | parentPreferenceGroup.removePreference(preference); |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 238 | public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context, |
| 239 | List<Header> target, Header header) { |
| 240 | |
| 241 | Intent intent = header.intent; |
| 242 | if (intent != null) { |
| 243 | // Find the activity that is in the system image |
| 244 | PackageManager pm = context.getPackageManager(); |
| 245 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 246 | int listSize = list.size(); |
| 247 | for (int i = 0; i < listSize; i++) { |
| 248 | ResolveInfo resolveInfo = list.get(i); |
| 249 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 250 | != 0) { |
| 251 | Drawable icon = null; |
| 252 | String title = null; |
| 253 | String summary = null; |
| 254 | |
| 255 | // Get the activity's meta-data |
| 256 | try { |
| 257 | Resources res = pm.getResourcesForApplication( |
| 258 | resolveInfo.activityInfo.packageName); |
| 259 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 260 | |
| 261 | if (res != null && metaData != null) { |
| 262 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 263 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 264 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 265 | } |
| 266 | } catch (NameNotFoundException e) { |
| 267 | // Ignore |
| 268 | } catch (NotFoundException e) { |
| 269 | // Ignore |
| 270 | } |
| 271 | |
| 272 | // Set the preference title to the activity's label if no |
| 273 | // meta-data is found |
| 274 | if (TextUtils.isEmpty(title)) { |
| 275 | title = resolveInfo.loadLabel(pm).toString(); |
| 276 | } |
| 277 | |
| 278 | // Set icon, title and summary for the preference |
| 279 | // TODO: |
| 280 | //header.icon = icon; |
| 281 | header.title = title; |
| 282 | header.summary = summary; |
| 283 | // Replace the intent with this specific activity |
| 284 | header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, |
| 285 | resolveInfo.activityInfo.name); |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Did not find a matching activity, so remove the preference |
| 293 | if (target.remove(header)) System.err.println("Removed " + header.id); |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 298 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 299 | * Returns true if Monkey is running. |
| 300 | */ |
| 301 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 302 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 303 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 307 | */ |
| 308 | public static boolean isVoiceCapable(Context context) { |
| 309 | TelephonyManager telephony = |
| 310 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 311 | return telephony != null && telephony.isVoiceCapable(); |
| 312 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 313 | |
Robert Greenwalt | 8af88fb | 2011-08-31 11:17:47 -0700 | [diff] [blame] | 314 | public static boolean isWifiOnly(Context context) { |
| 315 | ConnectivityManager cm = (ConnectivityManager)context.getSystemService( |
| 316 | Context.CONNECTIVITY_SERVICE); |
| 317 | return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 318 | } |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 322 | * @param context the application context |
| 323 | * @return the formatted and comma-separated IP addresses, or null if none. |
| 324 | */ |
| 325 | public static String getWifiIpAddresses(Context context) { |
| 326 | ConnectivityManager cm = (ConnectivityManager) |
| 327 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 328 | LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 329 | return formatIpAddresses(prop); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style |
| 334 | * addresses. |
| 335 | * @param context the application context |
| 336 | * @return the formatted and comma-separated IP addresses, or null if none. |
| 337 | */ |
| 338 | public static String getDefaultIpAddresses(Context context) { |
| 339 | ConnectivityManager cm = (ConnectivityManager) |
| 340 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 341 | LinkProperties prop = cm.getActiveLinkProperties(); |
| 342 | return formatIpAddresses(prop); |
| 343 | } |
| 344 | |
| 345 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 346 | if (prop == null) return null; |
| 347 | Iterator<InetAddress> iter = prop.getAddresses().iterator(); |
| 348 | // If there are no entries, return null |
| 349 | if (!iter.hasNext()) return null; |
| 350 | // Concatenate all available addresses, comma separated |
| 351 | String addresses = ""; |
| 352 | while (iter.hasNext()) { |
| 353 | addresses += iter.next().getHostAddress(); |
| 354 | if (iter.hasNext()) addresses += ", "; |
| 355 | } |
| 356 | return addresses; |
| 357 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 358 | |
| 359 | public static Locale createLocaleFromString(String localeStr) { |
| 360 | // TODO: is there a better way to actually construct a locale that will match? |
| 361 | // The main problem is, on top of Java specs, locale.toString() and |
| 362 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 363 | // many cases, because the constructor takes the only string as the language |
| 364 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 365 | // And : new Locale("en_US").toString() => "en_us" |
| 366 | if (null == localeStr) |
| 367 | return Locale.getDefault(); |
| 368 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 369 | // split may not return a 0-length array. |
| 370 | if (1 == brokenDownLocale.length) { |
| 371 | return new Locale(brokenDownLocale[0]); |
| 372 | } else if (2 == brokenDownLocale.length) { |
| 373 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 374 | } else { |
| 375 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 376 | } |
| 377 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 378 | |
| 379 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
| 380 | int level = batteryChangedIntent.getIntExtra("level", 0); |
| 381 | int scale = batteryChangedIntent.getIntExtra("scale", 100); |
| 382 | return String.valueOf(level * 100 / scale) + "%"; |
| 383 | } |
| 384 | |
| 385 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
| 386 | final Intent intent = batteryChangedIntent; |
| 387 | |
| 388 | int plugType = intent.getIntExtra("plugged", 0); |
| 389 | int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 390 | String statusString; |
| 391 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 392 | statusString = res.getString(R.string.battery_info_status_charging); |
| 393 | if (plugType > 0) { |
Brian Muramatsu | 0bd54a6 | 2012-08-14 15:13:16 -0700 | [diff] [blame] | 394 | int resId; |
| 395 | if (plugType == BatteryManager.BATTERY_PLUGGED_AC) { |
| 396 | resId = R.string.battery_info_status_charging_ac; |
| 397 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) { |
| 398 | resId = R.string.battery_info_status_charging_usb; |
| 399 | } else { |
| 400 | resId = R.string.battery_info_status_charging_wireless; |
| 401 | } |
| 402 | statusString = statusString + " " + res.getString(resId); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 403 | } |
| 404 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 405 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 406 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 407 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 408 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 409 | statusString = res.getString(R.string.battery_info_status_full); |
| 410 | } else { |
| 411 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 412 | } |
| 413 | |
| 414 | return statusString; |
| 415 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 419 | * when outside scrollbars are requested. Usually used to display |
| 420 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 421 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 422 | public static void prepareCustomPreferencesList( |
| 423 | ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 424 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
| 425 | if (movePadding && parent instanceof PreferenceFrameLayout) { |
| 426 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
| 427 | |
| 428 | final Resources res = list.getResources(); |
| 429 | final int paddingSide = res.getDimensionPixelSize( |
| 430 | com.android.internal.R.dimen.preference_fragment_padding_side); |
| 431 | final int paddingBottom = res.getDimensionPixelSize( |
| 432 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 433 | |
Amith Yamasani | f0b05de | 2012-05-09 09:43:33 -0700 | [diff] [blame] | 434 | final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 435 | list.setPadding(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 438 | |
| 439 | /** |
| 440 | * Return string resource that best describes combination of tethering |
| 441 | * options available on this device. |
| 442 | */ |
| 443 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 444 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 445 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 446 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 447 | |
| 448 | boolean usbAvailable = usbRegexs.length != 0; |
| 449 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 450 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 451 | |
| 452 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 453 | return R.string.tether_settings_title_all; |
| 454 | } else if (wifiAvailable && usbAvailable) { |
| 455 | return R.string.tether_settings_title_all; |
| 456 | } else if (wifiAvailable && bluetoothAvailable) { |
| 457 | return R.string.tether_settings_title_all; |
| 458 | } else if (wifiAvailable) { |
| 459 | return R.string.tether_settings_title_wifi; |
| 460 | } else if (usbAvailable && bluetoothAvailable) { |
| 461 | return R.string.tether_settings_title_usb_bluetooth; |
| 462 | } else if (usbAvailable) { |
| 463 | return R.string.tether_settings_title_usb; |
| 464 | } else { |
| 465 | return R.string.tether_settings_title_bluetooth; |
| 466 | } |
| 467 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 468 | |
| 469 | /* Used by UserSettings as well. Call this on a non-ui thread. */ |
| 470 | public static boolean copyMeProfilePhoto(Context context, UserInfo user) { |
| 471 | Uri contactUri = Profile.CONTENT_URI; |
| 472 | |
| 473 | InputStream avatarDataStream = Contacts.openContactPhotoInputStream( |
| 474 | context.getContentResolver(), |
| 475 | contactUri, true); |
| 476 | // If there's no profile photo, assign a default avatar |
| 477 | if (avatarDataStream == null) { |
| 478 | return false; |
| 479 | } |
| 480 | int userId = user != null ? user.id : UserHandle.myUserId(); |
| 481 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 482 | Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); |
| 483 | um.setUserIcon(userId, icon); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 484 | try { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 485 | avatarDataStream.close(); |
| 486 | } catch (IOException ioe) { } |
| 487 | return true; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | public static String getMeProfileName(Context context) { |
| 491 | Cursor cursor = context.getContentResolver().query( |
| 492 | Profile.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME}, |
| 493 | null, null, null); |
| 494 | if (cursor == null) { |
| 495 | return null; |
| 496 | } |
| 497 | |
| 498 | try { |
| 499 | if (cursor.moveToFirst()) { |
| 500 | return cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)); |
| 501 | } |
| 502 | } finally { |
| 503 | cursor.close(); |
| 504 | } |
| 505 | return null; |
| 506 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame^] | 507 | |
| 508 | /** Not global warming, it's global change warning. */ |
| 509 | public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId, |
| 510 | final Runnable positiveAction) { |
| 511 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 512 | builder.setTitle(titleResId); |
| 513 | builder.setMessage(R.string.global_change_warning); |
| 514 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 515 | @Override |
| 516 | public void onClick(DialogInterface dialog, int which) { |
| 517 | positiveAction.run(); |
| 518 | } |
| 519 | }); |
| 520 | builder.setNegativeButton(android.R.string.cancel, null); |
| 521 | |
| 522 | return builder.create(); |
| 523 | } |
| 524 | |
| 525 | public static boolean hasMultipleUsers(Context context) { |
| 526 | return ((UserManager) context.getSystemService(Context.USER_SERVICE)) |
| 527 | .getUsers().size() > 1; |
| 528 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 529 | } |