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