blob: 73a9a30b0ceeb32c6fde5fda436af59d0a133b96 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/**
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
17package com.android.settings;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070023import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070024import android.content.pm.ResolveInfo;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020025import android.content.res.Resources;
26import android.content.res.Resources.NotFoundException;
27import android.graphics.drawable.Drawable;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080028import android.net.ConnectivityManager;
29import android.net.LinkProperties;
Amith Yamasania4379d62011-07-22 10:34:58 -070030import android.os.BatteryManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020031import android.os.Bundle;
Amith Yamasani60133dd2010-09-11 14:17:31 -070032import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070034import android.preference.PreferenceActivity.Header;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070035import android.preference.PreferenceFrameLayout;
36import android.preference.PreferenceGroup;
Amith Yamasani60133dd2010-09-11 14:17:31 -070037import android.telephony.TelephonyManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020038import android.text.TextUtils;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070039import android.view.View;
40import android.view.ViewGroup;
41import android.widget.ListView;
42import android.widget.TabWidget;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070043
Amith Yamasanic06d4c42011-02-25 14:35:20 -080044import java.net.InetAddress;
45import java.util.Iterator;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070046import java.util.List;
Jean Chalard71ad1f42011-05-12 15:06:16 +090047import java.util.Locale;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049public class Utils {
50
51 /**
52 * Set the preference's title to the matching activity's label.
53 */
54 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
55
56 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020057 * Name of the meta-data item that should be set in the AndroidManifest.xml
58 * to specify the icon that should be displayed for the preference.
59 */
60 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
61
62 /**
63 * Name of the meta-data item that should be set in the AndroidManifest.xml
64 * to specify the title that should be displayed for the preference.
65 */
66 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
67
68 /**
69 * Name of the meta-data item that should be set in the AndroidManifest.xml
70 * to specify the summary text that should be displayed for the preference.
71 */
72 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
73
74 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075 * Finds a matching activity for a preference's intent. If a matching
76 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080077 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078 * @param context The context.
79 * @param parentPreferenceGroup The preference group that contains the
80 * preference whose intent is being resolved.
81 * @param preferenceKey The key of the preference whose intent is being
82 * resolved.
83 * @param flags 0 or one or more of
84 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
85 * .
86 * @return Whether an activity was found. If false, the preference was
87 * removed.
88 */
89 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
90 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -080091
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
93 if (preference == null) {
94 return false;
95 }
Ying Wanga7188322010-01-04 18:45:10 -080096
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 Intent intent = preference.getIntent();
98 if (intent != null) {
99 // Find the activity that is in the system image
100 PackageManager pm = context.getPackageManager();
101 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
102 int listSize = list.size();
103 for (int i = 0; i < listSize; i++) {
104 ResolveInfo resolveInfo = list.get(i);
105 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
106 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -0800107
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800108 // Replace the intent with this specific activity
109 preference.setIntent(new Intent().setClassName(
110 resolveInfo.activityInfo.packageName,
111 resolveInfo.activityInfo.name));
112
113 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
114 // Set the preference title to the activity's label
115 preference.setTitle(resolveInfo.loadLabel(pm));
116 }
Ying Wanga7188322010-01-04 18:45:10 -0800117
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 return true;
119 }
120 }
121 }
122
123 // Did not find a matching activity, so remove the preference
124 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800125
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 return true;
127 }
Ying Wanga7188322010-01-04 18:45:10 -0800128
129 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200130 * Finds a matching activity for a preference's intent. If a matching
131 * activity is not found, it will remove the preference. The icon, title and
132 * summary of the preference will also be updated with the values retrieved
133 * from the activity's meta-data elements. If no meta-data elements are
134 * specified then the preference title will be set to match the label of the
135 * activity, an icon and summary text will not be displayed.
136 *
137 * @param context The context.
138 * @param parentPreferenceGroup The preference group that contains the
139 * preference whose intent is being resolved.
140 * @param preferenceKey The key of the preference whose intent is being
141 * resolved.
142 *
143 * @return Whether an activity was found. If false, the preference was
144 * removed.
145 *
146 * @see {@link #META_DATA_PREFERENCE_ICON}
147 * {@link #META_DATA_PREFERENCE_TITLE}
148 * {@link #META_DATA_PREFERENCE_SUMMARY}
149 */
150 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
151 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
152
153 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
154 .findPreference(preferenceKey);
155 if (preference == null) {
156 return false;
157 }
158
159 Intent intent = preference.getIntent();
160 if (intent != null) {
161 // Find the activity that is in the system image
162 PackageManager pm = context.getPackageManager();
163 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
164 int listSize = list.size();
165 for (int i = 0; i < listSize; i++) {
166 ResolveInfo resolveInfo = list.get(i);
167 if ((resolveInfo.activityInfo.applicationInfo.flags
168 & ApplicationInfo.FLAG_SYSTEM) != 0) {
169 Drawable icon = null;
170 String title = null;
171 String summary = null;
172
173 // Get the activity's meta-data
174 try {
175 Resources res = pm
176 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
177 Bundle metaData = resolveInfo.activityInfo.metaData;
178
179 if (res != null && metaData != null) {
180 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
181 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
182 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
183 }
184 } catch (NameNotFoundException e) {
185 // Ignore
186 } catch (NotFoundException e) {
187 // Ignore
188 }
189
190 // Set the preference title to the activity's label if no
191 // meta-data is found
192 if (TextUtils.isEmpty(title)) {
193 title = resolveInfo.loadLabel(pm).toString();
194 }
195
196 // Set icon, title and summary for the preference
197 preference.setIcon(icon);
198 preference.setTitle(title);
199 preference.setSummary(summary);
200
201 // Replace the intent with this specific activity
202 preference.setIntent(new Intent().setClassName(
203 resolveInfo.activityInfo.packageName,
204 resolveInfo.activityInfo.name));
205
206 return true;
207 }
208 }
209 }
210
211 // Did not find a matching activity, so remove the preference
212 parentPreferenceGroup.removePreference(preference);
213
214 return false;
215 }
216
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700217 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
218 List<Header> target, Header header) {
219
220 Intent intent = header.intent;
221 if (intent != null) {
222 // Find the activity that is in the system image
223 PackageManager pm = context.getPackageManager();
224 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
225 int listSize = list.size();
226 for (int i = 0; i < listSize; i++) {
227 ResolveInfo resolveInfo = list.get(i);
228 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
229 != 0) {
230 Drawable icon = null;
231 String title = null;
232 String summary = null;
233
234 // Get the activity's meta-data
235 try {
236 Resources res = pm.getResourcesForApplication(
237 resolveInfo.activityInfo.packageName);
238 Bundle metaData = resolveInfo.activityInfo.metaData;
239
240 if (res != null && metaData != null) {
241 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
242 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
243 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
244 }
245 } catch (NameNotFoundException e) {
246 // Ignore
247 } catch (NotFoundException e) {
248 // Ignore
249 }
250
251 // Set the preference title to the activity's label if no
252 // meta-data is found
253 if (TextUtils.isEmpty(title)) {
254 title = resolveInfo.loadLabel(pm).toString();
255 }
256
257 // Set icon, title and summary for the preference
258 // TODO:
259 //header.icon = icon;
260 header.title = title;
261 header.summary = summary;
262 // Replace the intent with this specific activity
263 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
264 resolveInfo.activityInfo.name);
265
266 return true;
267 }
268 }
269 }
270
271 // Did not find a matching activity, so remove the preference
272 if (target.remove(header)) System.err.println("Removed " + header.id);
273
274 return false;
275 }
276
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200277 /**
Ying Wanga7188322010-01-04 18:45:10 -0800278 * Returns true if Monkey is running.
279 */
280 public static boolean isMonkeyRunning() {
281 return SystemProperties.getBoolean("ro.monkey", false);
282 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700283
284 /**
285 * Returns whether the device is voice-capable (meaning, it is also a phone).
286 */
287 public static boolean isVoiceCapable(Context context) {
288 TelephonyManager telephony =
289 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
290 return telephony != null && telephony.isVoiceCapable();
291 }
Amith Yamasani0f85c482011-02-23 17:19:11 -0800292
293 public static boolean isWifiOnly() {
294 return "wifi-only".equals(SystemProperties.get("ro.carrier"));
295 }
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800296
297 /**
298 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
299 * @param context the application context
300 * @return the formatted and comma-separated IP addresses, or null if none.
301 */
302 public static String getWifiIpAddresses(Context context) {
303 ConnectivityManager cm = (ConnectivityManager)
304 context.getSystemService(Context.CONNECTIVITY_SERVICE);
305 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
306 if (prop == null) return null;
307 Iterator<InetAddress> iter = prop.getAddresses().iterator();
308 // If there are no entries, return null
309 if (!iter.hasNext()) return null;
310 // Concatenate all available addresses, comma separated
311 String addresses = "";
312 while (iter.hasNext()) {
313 addresses += iter.next().getHostAddress();
314 if (iter.hasNext()) addresses += ", ";
315 }
316 return addresses;
317 }
Jean Chalard71ad1f42011-05-12 15:06:16 +0900318
319 public static Locale createLocaleFromString(String localeStr) {
320 // TODO: is there a better way to actually construct a locale that will match?
321 // The main problem is, on top of Java specs, locale.toString() and
322 // new Locale(locale.toString()).toString() do not return equal() strings in
323 // many cases, because the constructor takes the only string as the language
324 // code. So : new Locale("en", "US").toString() => "en_US"
325 // And : new Locale("en_US").toString() => "en_us"
326 if (null == localeStr)
327 return Locale.getDefault();
328 String[] brokenDownLocale = localeStr.split("_", 3);
329 // split may not return a 0-length array.
330 if (1 == brokenDownLocale.length) {
331 return new Locale(brokenDownLocale[0]);
332 } else if (2 == brokenDownLocale.length) {
333 return new Locale(brokenDownLocale[0], brokenDownLocale[1]);
334 } else {
335 return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]);
336 }
337 }
Amith Yamasania4379d62011-07-22 10:34:58 -0700338
339 public static String getBatteryPercentage(Intent batteryChangedIntent) {
340 int level = batteryChangedIntent.getIntExtra("level", 0);
341 int scale = batteryChangedIntent.getIntExtra("scale", 100);
342 return String.valueOf(level * 100 / scale) + "%";
343 }
344
345 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
346 final Intent intent = batteryChangedIntent;
347
348 int plugType = intent.getIntExtra("plugged", 0);
349 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
350 String statusString;
351 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
352 statusString = res.getString(R.string.battery_info_status_charging);
353 if (plugType > 0) {
354 statusString = statusString
355 + " "
356 + res.getString((plugType == BatteryManager.BATTERY_PLUGGED_AC)
357 ? R.string.battery_info_status_charging_ac
358 : R.string.battery_info_status_charging_usb);
359 }
360 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
361 statusString = res.getString(R.string.battery_info_status_discharging);
362 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
363 statusString = res.getString(R.string.battery_info_status_not_charging);
364 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
365 statusString = res.getString(R.string.battery_info_status_full);
366 } else {
367 statusString = res.getString(R.string.battery_info_status_unknown);
368 }
369
370 return statusString;
371 }
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700372
373 /**
374 * Prepare a custom preferences layout, moving padding to {@link ListView}
375 * when outside scrollbars are requested. Usually used to display
376 * {@link ListView} and {@link TabWidget} with correct padding.
377 */
378 public static void prepareCustomPreferencesList(ViewGroup parent, View child, ListView list) {
379 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY;
380 if (movePadding && parent instanceof PreferenceFrameLayout) {
381 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true;
382
383 final Resources res = list.getResources();
384 final int paddingSide = res.getDimensionPixelSize(
385 com.android.internal.R.dimen.preference_fragment_padding_side);
386 final int paddingBottom = res.getDimensionPixelSize(
387 com.android.internal.R.dimen.preference_fragment_padding_bottom);
388 list.setPadding(paddingSide, 0, paddingSide, paddingBottom);
389 }
390 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800391}