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