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