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