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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageManager; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 23 | import android.content.pm.ResolveInfo; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager.NameNotFoundException; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 25 | import android.content.res.Resources; |
| 26 | import android.content.res.Resources.NotFoundException; |
| 27 | import android.graphics.drawable.Drawable; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 28 | import android.net.ConnectivityManager; |
| 29 | import android.net.LinkProperties; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 30 | import android.os.Bundle; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 32 | import android.preference.Preference; |
| 33 | import android.preference.PreferenceGroup; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 34 | import android.preference.PreferenceActivity.Header; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 35 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 36 | import android.text.TextUtils; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 37 | |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 38 | import java.net.InetAddress; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame^] | 39 | import java.util.Arrays; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 40 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 41 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame^] | 42 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 44 | public class Utils { |
| 45 | |
| 46 | /** |
| 47 | * Set the preference's title to the matching activity's label. |
| 48 | */ |
| 49 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 50 | |
| 51 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 52 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 53 | * to specify the icon that should be displayed for the preference. |
| 54 | */ |
| 55 | private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
| 56 | |
| 57 | /** |
| 58 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 59 | * to specify the title that should be displayed for the preference. |
| 60 | */ |
| 61 | private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
| 62 | |
| 63 | /** |
| 64 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 65 | * to specify the summary text that should be displayed for the preference. |
| 66 | */ |
| 67 | private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
| 68 | |
| 69 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 70 | * Finds a matching activity for a preference's intent. If a matching |
| 71 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 72 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 73 | * @param context The context. |
| 74 | * @param parentPreferenceGroup The preference group that contains the |
| 75 | * preference whose intent is being resolved. |
| 76 | * @param preferenceKey The key of the preference whose intent is being |
| 77 | * resolved. |
| 78 | * @param flags 0 or one or more of |
| 79 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 80 | * . |
| 81 | * @return Whether an activity was found. If false, the preference was |
| 82 | * removed. |
| 83 | */ |
| 84 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 85 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 86 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 87 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 88 | if (preference == null) { |
| 89 | return false; |
| 90 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 91 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 92 | Intent intent = preference.getIntent(); |
| 93 | if (intent != null) { |
| 94 | // Find the activity that is in the system image |
| 95 | PackageManager pm = context.getPackageManager(); |
| 96 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 97 | int listSize = list.size(); |
| 98 | for (int i = 0; i < listSize; i++) { |
| 99 | ResolveInfo resolveInfo = list.get(i); |
| 100 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 101 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 102 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 103 | // Replace the intent with this specific activity |
| 104 | preference.setIntent(new Intent().setClassName( |
| 105 | resolveInfo.activityInfo.packageName, |
| 106 | resolveInfo.activityInfo.name)); |
| 107 | |
| 108 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 109 | // Set the preference title to the activity's label |
| 110 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 111 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 112 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Did not find a matching activity, so remove the preference |
| 119 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 120 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 121 | return true; |
| 122 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 125 | * Finds a matching activity for a preference's intent. If a matching |
| 126 | * activity is not found, it will remove the preference. The icon, title and |
| 127 | * summary of the preference will also be updated with the values retrieved |
| 128 | * from the activity's meta-data elements. If no meta-data elements are |
| 129 | * specified then the preference title will be set to match the label of the |
| 130 | * activity, an icon and summary text will not be displayed. |
| 131 | * |
| 132 | * @param context The context. |
| 133 | * @param parentPreferenceGroup The preference group that contains the |
| 134 | * preference whose intent is being resolved. |
| 135 | * @param preferenceKey The key of the preference whose intent is being |
| 136 | * resolved. |
| 137 | * |
| 138 | * @return Whether an activity was found. If false, the preference was |
| 139 | * removed. |
| 140 | * |
| 141 | * @see {@link #META_DATA_PREFERENCE_ICON} |
| 142 | * {@link #META_DATA_PREFERENCE_TITLE} |
| 143 | * {@link #META_DATA_PREFERENCE_SUMMARY} |
| 144 | */ |
| 145 | public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context, |
| 146 | PreferenceGroup parentPreferenceGroup, String preferenceKey) { |
| 147 | |
| 148 | IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup |
| 149 | .findPreference(preferenceKey); |
| 150 | if (preference == null) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | Intent intent = preference.getIntent(); |
| 155 | if (intent != null) { |
| 156 | // Find the activity that is in the system image |
| 157 | PackageManager pm = context.getPackageManager(); |
| 158 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 159 | int listSize = list.size(); |
| 160 | for (int i = 0; i < listSize; i++) { |
| 161 | ResolveInfo resolveInfo = list.get(i); |
| 162 | if ((resolveInfo.activityInfo.applicationInfo.flags |
| 163 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 164 | Drawable icon = null; |
| 165 | String title = null; |
| 166 | String summary = null; |
| 167 | |
| 168 | // Get the activity's meta-data |
| 169 | try { |
| 170 | Resources res = pm |
| 171 | .getResourcesForApplication(resolveInfo.activityInfo.packageName); |
| 172 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 173 | |
| 174 | if (res != null && metaData != null) { |
| 175 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 176 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 177 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 178 | } |
| 179 | } catch (NameNotFoundException e) { |
| 180 | // Ignore |
| 181 | } catch (NotFoundException e) { |
| 182 | // Ignore |
| 183 | } |
| 184 | |
| 185 | // Set the preference title to the activity's label if no |
| 186 | // meta-data is found |
| 187 | if (TextUtils.isEmpty(title)) { |
| 188 | title = resolveInfo.loadLabel(pm).toString(); |
| 189 | } |
| 190 | |
| 191 | // Set icon, title and summary for the preference |
| 192 | preference.setIcon(icon); |
| 193 | preference.setTitle(title); |
| 194 | preference.setSummary(summary); |
| 195 | |
| 196 | // Replace the intent with this specific activity |
| 197 | preference.setIntent(new Intent().setClassName( |
| 198 | resolveInfo.activityInfo.packageName, |
| 199 | resolveInfo.activityInfo.name)); |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Did not find a matching activity, so remove the preference |
| 207 | parentPreferenceGroup.removePreference(preference); |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 212 | public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context, |
| 213 | List<Header> target, Header header) { |
| 214 | |
| 215 | Intent intent = header.intent; |
| 216 | if (intent != null) { |
| 217 | // Find the activity that is in the system image |
| 218 | PackageManager pm = context.getPackageManager(); |
| 219 | List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 220 | int listSize = list.size(); |
| 221 | for (int i = 0; i < listSize; i++) { |
| 222 | ResolveInfo resolveInfo = list.get(i); |
| 223 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 224 | != 0) { |
| 225 | Drawable icon = null; |
| 226 | String title = null; |
| 227 | String summary = null; |
| 228 | |
| 229 | // Get the activity's meta-data |
| 230 | try { |
| 231 | Resources res = pm.getResourcesForApplication( |
| 232 | resolveInfo.activityInfo.packageName); |
| 233 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 234 | |
| 235 | if (res != null && metaData != null) { |
| 236 | icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); |
| 237 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 238 | summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 239 | } |
| 240 | } catch (NameNotFoundException e) { |
| 241 | // Ignore |
| 242 | } catch (NotFoundException e) { |
| 243 | // Ignore |
| 244 | } |
| 245 | |
| 246 | // Set the preference title to the activity's label if no |
| 247 | // meta-data is found |
| 248 | if (TextUtils.isEmpty(title)) { |
| 249 | title = resolveInfo.loadLabel(pm).toString(); |
| 250 | } |
| 251 | |
| 252 | // Set icon, title and summary for the preference |
| 253 | // TODO: |
| 254 | //header.icon = icon; |
| 255 | header.title = title; |
| 256 | header.summary = summary; |
| 257 | // Replace the intent with this specific activity |
| 258 | header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, |
| 259 | resolveInfo.activityInfo.name); |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Did not find a matching activity, so remove the preference |
| 267 | if (target.remove(header)) System.err.println("Removed " + header.id); |
| 268 | |
| 269 | return false; |
| 270 | } |
| 271 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 272 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 273 | * Returns true if Monkey is running. |
| 274 | */ |
| 275 | public static boolean isMonkeyRunning() { |
| 276 | return SystemProperties.getBoolean("ro.monkey", false); |
| 277 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 281 | */ |
| 282 | public static boolean isVoiceCapable(Context context) { |
| 283 | TelephonyManager telephony = |
| 284 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 285 | return telephony != null && telephony.isVoiceCapable(); |
| 286 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 287 | |
| 288 | public static boolean isWifiOnly() { |
| 289 | return "wifi-only".equals(SystemProperties.get("ro.carrier")); |
| 290 | } |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 294 | * @param context the application context |
| 295 | * @return the formatted and comma-separated IP addresses, or null if none. |
| 296 | */ |
| 297 | public static String getWifiIpAddresses(Context context) { |
| 298 | ConnectivityManager cm = (ConnectivityManager) |
| 299 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 300 | LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); |
| 301 | if (prop == null) return null; |
| 302 | Iterator<InetAddress> iter = prop.getAddresses().iterator(); |
| 303 | // If there are no entries, return null |
| 304 | if (!iter.hasNext()) return null; |
| 305 | // Concatenate all available addresses, comma separated |
| 306 | String addresses = ""; |
| 307 | while (iter.hasNext()) { |
| 308 | addresses += iter.next().getHostAddress(); |
| 309 | if (iter.hasNext()) addresses += ", "; |
| 310 | } |
| 311 | return addresses; |
| 312 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame^] | 313 | |
| 314 | public static Locale createLocaleFromString(String localeStr) { |
| 315 | // TODO: is there a better way to actually construct a locale that will match? |
| 316 | // The main problem is, on top of Java specs, locale.toString() and |
| 317 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 318 | // many cases, because the constructor takes the only string as the language |
| 319 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 320 | // And : new Locale("en_US").toString() => "en_us" |
| 321 | if (null == localeStr) |
| 322 | return Locale.getDefault(); |
| 323 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 324 | // split may not return a 0-length array. |
| 325 | if (1 == brokenDownLocale.length) { |
| 326 | return new Locale(brokenDownLocale[0]); |
| 327 | } else if (2 == brokenDownLocale.length) { |
| 328 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 329 | } else { |
| 330 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 331 | } |
| 332 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 333 | } |