blob: 07f671614e11f10f393c86ae2b02b31ec55b4e92 [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
Amith Yamasani9627a8e2012-09-23 12:54:14 -070019import android.app.Activity;
Amith Yamasaniae697552011-09-27 11:33:17 -070020import android.app.ActivityManager;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070021import android.app.AlertDialog;
22import android.app.Dialog;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.content.Context;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070024import android.content.DialogInterface;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.content.Intent;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageManager;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070028import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070029import android.content.pm.ResolveInfo;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070030import android.content.pm.UserInfo;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020031import android.content.res.Resources;
32import android.content.res.Resources.NotFoundException;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070033import android.database.Cursor;
Amith Yamasanif34a85d2012-09-17 18:31:45 -070034import android.graphics.Bitmap;
35import android.graphics.BitmapFactory;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020036import android.graphics.drawable.Drawable;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080037import android.net.ConnectivityManager;
38import android.net.LinkProperties;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070039import android.net.Uri;
Amith Yamasania4379d62011-07-22 10:34:58 -070040import android.os.BatteryManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020041import android.os.Bundle;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070042import android.os.ParcelFileDescriptor;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070043import android.os.UserHandle;
44import android.os.UserManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045import android.preference.Preference;
Amith Yamasani02cf71a2010-09-21 15:48:52 -070046import android.preference.PreferenceActivity.Header;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070047import android.preference.PreferenceFrameLayout;
48import android.preference.PreferenceGroup;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070049import android.provider.ContactsContract.CommonDataKinds.Phone;
50import android.provider.ContactsContract.Contacts;
51import android.provider.ContactsContract.Profile;
Amith Yamasani60133dd2010-09-11 14:17:31 -070052import android.telephony.TelephonyManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020053import android.text.TextUtils;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070054import android.util.Log;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070055import android.view.View;
56import android.view.ViewGroup;
57import android.widget.ListView;
58import android.widget.TabWidget;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070059
Amith Yamasaniae47ef42012-09-16 17:53:35 -070060import com.android.settings.users.ProfileUpdateReceiver;
61
62import java.io.FileOutputStream;
63import java.io.IOException;
64import java.io.InputStream;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080065import java.net.InetAddress;
66import java.util.Iterator;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070067import java.util.List;
Jean Chalard71ad1f42011-05-12 15:06:16 +090068import java.util.Locale;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070public class Utils {
71
72 /**
73 * Set the preference's title to the matching activity's label.
74 */
75 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
76
77 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020078 * Name of the meta-data item that should be set in the AndroidManifest.xml
79 * to specify the icon that should be displayed for the preference.
80 */
81 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
82
83 /**
84 * Name of the meta-data item that should be set in the AndroidManifest.xml
85 * to specify the title that should be displayed for the preference.
86 */
87 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
88
89 /**
90 * Name of the meta-data item that should be set in the AndroidManifest.xml
91 * to specify the summary text that should be displayed for the preference.
92 */
93 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
94
95 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 * Finds a matching activity for a preference's intent. If a matching
97 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080098 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099 * @param context The context.
100 * @param parentPreferenceGroup The preference group that contains the
101 * preference whose intent is being resolved.
102 * @param preferenceKey The key of the preference whose intent is being
103 * resolved.
104 * @param flags 0 or one or more of
105 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
106 * .
107 * @return Whether an activity was found. If false, the preference was
108 * removed.
109 */
110 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
111 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -0800112
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
114 if (preference == null) {
115 return false;
116 }
Ying Wanga7188322010-01-04 18:45:10 -0800117
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 Intent intent = preference.getIntent();
119 if (intent != null) {
120 // Find the activity that is in the system image
121 PackageManager pm = context.getPackageManager();
122 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
123 int listSize = list.size();
124 for (int i = 0; i < listSize; i++) {
125 ResolveInfo resolveInfo = list.get(i);
126 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
127 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -0800128
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 // Replace the intent with this specific activity
130 preference.setIntent(new Intent().setClassName(
131 resolveInfo.activityInfo.packageName,
132 resolveInfo.activityInfo.name));
133
134 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
135 // Set the preference title to the activity's label
136 preference.setTitle(resolveInfo.loadLabel(pm));
137 }
Ying Wanga7188322010-01-04 18:45:10 -0800138
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 return true;
140 }
141 }
142 }
143
144 // Did not find a matching activity, so remove the preference
145 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800146
Shuhrat Dehkanov7dc567a2012-04-23 01:59:56 +0900147 return false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800148 }
Ying Wanga7188322010-01-04 18:45:10 -0800149
150 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200151 * Finds a matching activity for a preference's intent. If a matching
152 * activity is not found, it will remove the preference. The icon, title and
153 * summary of the preference will also be updated with the values retrieved
154 * from the activity's meta-data elements. If no meta-data elements are
155 * specified then the preference title will be set to match the label of the
156 * activity, an icon and summary text will not be displayed.
157 *
158 * @param context The context.
159 * @param parentPreferenceGroup The preference group that contains the
160 * preference whose intent is being resolved.
161 * @param preferenceKey The key of the preference whose intent is being
162 * resolved.
163 *
164 * @return Whether an activity was found. If false, the preference was
165 * removed.
166 *
167 * @see {@link #META_DATA_PREFERENCE_ICON}
168 * {@link #META_DATA_PREFERENCE_TITLE}
169 * {@link #META_DATA_PREFERENCE_SUMMARY}
170 */
171 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
172 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
173
174 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
175 .findPreference(preferenceKey);
176 if (preference == null) {
177 return false;
178 }
179
180 Intent intent = preference.getIntent();
181 if (intent != null) {
182 // Find the activity that is in the system image
183 PackageManager pm = context.getPackageManager();
184 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
185 int listSize = list.size();
186 for (int i = 0; i < listSize; i++) {
187 ResolveInfo resolveInfo = list.get(i);
188 if ((resolveInfo.activityInfo.applicationInfo.flags
189 & ApplicationInfo.FLAG_SYSTEM) != 0) {
190 Drawable icon = null;
191 String title = null;
192 String summary = null;
193
194 // Get the activity's meta-data
195 try {
196 Resources res = pm
197 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
198 Bundle metaData = resolveInfo.activityInfo.metaData;
199
200 if (res != null && metaData != null) {
201 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
202 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
203 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
204 }
205 } catch (NameNotFoundException e) {
206 // Ignore
207 } catch (NotFoundException e) {
208 // Ignore
209 }
210
211 // Set the preference title to the activity's label if no
212 // meta-data is found
213 if (TextUtils.isEmpty(title)) {
214 title = resolveInfo.loadLabel(pm).toString();
215 }
216
217 // Set icon, title and summary for the preference
218 preference.setIcon(icon);
219 preference.setTitle(title);
220 preference.setSummary(summary);
221
222 // Replace the intent with this specific activity
223 preference.setIntent(new Intent().setClassName(
224 resolveInfo.activityInfo.packageName,
225 resolveInfo.activityInfo.name));
226
227 return true;
228 }
229 }
230 }
231
232 // Did not find a matching activity, so remove the preference
233 parentPreferenceGroup.removePreference(preference);
234
235 return false;
236 }
237
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700238 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
239 List<Header> target, Header header) {
240
241 Intent intent = header.intent;
242 if (intent != null) {
243 // Find the activity that is in the system image
244 PackageManager pm = context.getPackageManager();
245 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
246 int listSize = list.size();
247 for (int i = 0; i < listSize; i++) {
248 ResolveInfo resolveInfo = list.get(i);
249 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
250 != 0) {
251 Drawable icon = null;
252 String title = null;
253 String summary = null;
254
255 // Get the activity's meta-data
256 try {
257 Resources res = pm.getResourcesForApplication(
258 resolveInfo.activityInfo.packageName);
259 Bundle metaData = resolveInfo.activityInfo.metaData;
260
261 if (res != null && metaData != null) {
262 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
263 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
264 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
265 }
266 } catch (NameNotFoundException e) {
267 // Ignore
268 } catch (NotFoundException e) {
269 // Ignore
270 }
271
272 // Set the preference title to the activity's label if no
273 // meta-data is found
274 if (TextUtils.isEmpty(title)) {
275 title = resolveInfo.loadLabel(pm).toString();
276 }
277
278 // Set icon, title and summary for the preference
279 // TODO:
280 //header.icon = icon;
281 header.title = title;
282 header.summary = summary;
283 // Replace the intent with this specific activity
284 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
285 resolveInfo.activityInfo.name);
286
287 return true;
288 }
289 }
290 }
291
292 // Did not find a matching activity, so remove the preference
293 if (target.remove(header)) System.err.println("Removed " + header.id);
294
295 return false;
296 }
297
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200298 /**
Ying Wanga7188322010-01-04 18:45:10 -0800299 * Returns true if Monkey is running.
300 */
301 public static boolean isMonkeyRunning() {
Amith Yamasaniae697552011-09-27 11:33:17 -0700302 return ActivityManager.isUserAMonkey();
Ying Wanga7188322010-01-04 18:45:10 -0800303 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700304
305 /**
306 * Returns whether the device is voice-capable (meaning, it is also a phone).
307 */
308 public static boolean isVoiceCapable(Context context) {
309 TelephonyManager telephony =
310 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
311 return telephony != null && telephony.isVoiceCapable();
312 }
Amith Yamasani0f85c482011-02-23 17:19:11 -0800313
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700314 public static boolean isWifiOnly(Context context) {
315 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
316 Context.CONNECTIVITY_SERVICE);
317 return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
Amith Yamasani0f85c482011-02-23 17:19:11 -0800318 }
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800319
320 /**
321 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
322 * @param context the application context
323 * @return the formatted and comma-separated IP addresses, or null if none.
324 */
325 public static String getWifiIpAddresses(Context context) {
326 ConnectivityManager cm = (ConnectivityManager)
327 context.getSystemService(Context.CONNECTIVITY_SERVICE);
328 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
Amith Yamasani6822b742011-10-17 16:41:00 -0700329 return formatIpAddresses(prop);
330 }
331
332 /**
333 * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
334 * addresses.
335 * @param context the application context
336 * @return the formatted and comma-separated IP addresses, or null if none.
337 */
338 public static String getDefaultIpAddresses(Context context) {
339 ConnectivityManager cm = (ConnectivityManager)
340 context.getSystemService(Context.CONNECTIVITY_SERVICE);
341 LinkProperties prop = cm.getActiveLinkProperties();
342 return formatIpAddresses(prop);
343 }
344
345 private static String formatIpAddresses(LinkProperties prop) {
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800346 if (prop == null) return null;
347 Iterator<InetAddress> iter = prop.getAddresses().iterator();
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();
354 if (iter.hasNext()) addresses += ", ";
355 }
356 return addresses;
357 }
Jean Chalard71ad1f42011-05-12 15:06:16 +0900358
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 Yamasania4379d62011-07-22 10:34:58 -0700378
379 public static String getBatteryPercentage(Intent batteryChangedIntent) {
380 int level = batteryChangedIntent.getIntExtra("level", 0);
381 int scale = batteryChangedIntent.getIntExtra("scale", 100);
382 return String.valueOf(level * 100 / scale) + "%";
383 }
384
385 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
386 final Intent intent = batteryChangedIntent;
387
388 int plugType = intent.getIntExtra("plugged", 0);
389 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
390 String statusString;
391 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
392 statusString = res.getString(R.string.battery_info_status_charging);
393 if (plugType > 0) {
Brian Muramatsu0bd54a62012-08-14 15:13:16 -0700394 int resId;
395 if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
396 resId = R.string.battery_info_status_charging_ac;
397 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
398 resId = R.string.battery_info_status_charging_usb;
399 } else {
400 resId = R.string.battery_info_status_charging_wireless;
401 }
402 statusString = statusString + " " + res.getString(resId);
Amith Yamasania4379d62011-07-22 10:34:58 -0700403 }
404 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
405 statusString = res.getString(R.string.battery_info_status_discharging);
406 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
407 statusString = res.getString(R.string.battery_info_status_not_charging);
408 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
409 statusString = res.getString(R.string.battery_info_status_full);
410 } else {
411 statusString = res.getString(R.string.battery_info_status_unknown);
412 }
413
414 return statusString;
415 }
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700416
417 /**
418 * Prepare a custom preferences layout, moving padding to {@link ListView}
419 * when outside scrollbars are requested. Usually used to display
420 * {@link ListView} and {@link TabWidget} with correct padding.
421 */
Jeff Sharkey5d706792011-09-08 18:57:17 -0700422 public static void prepareCustomPreferencesList(
423 ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) {
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700424 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY;
425 if (movePadding && parent instanceof PreferenceFrameLayout) {
426 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true;
427
428 final Resources res = list.getResources();
429 final int paddingSide = res.getDimensionPixelSize(
430 com.android.internal.R.dimen.preference_fragment_padding_side);
431 final int paddingBottom = res.getDimensionPixelSize(
432 com.android.internal.R.dimen.preference_fragment_padding_bottom);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700433
Amith Yamasanif0b05de2012-05-09 09:43:33 -0700434 final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700435 list.setPadding(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700436 }
437 }
Jeff Sharkeya83a24f2011-09-16 01:52:39 -0700438
439 /**
440 * Return string resource that best describes combination of tethering
441 * options available on this device.
442 */
443 public static int getTetheringLabel(ConnectivityManager cm) {
444 String[] usbRegexs = cm.getTetherableUsbRegexs();
445 String[] wifiRegexs = cm.getTetherableWifiRegexs();
446 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
447
448 boolean usbAvailable = usbRegexs.length != 0;
449 boolean wifiAvailable = wifiRegexs.length != 0;
450 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
451
452 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
453 return R.string.tether_settings_title_all;
454 } else if (wifiAvailable && usbAvailable) {
455 return R.string.tether_settings_title_all;
456 } else if (wifiAvailable && bluetoothAvailable) {
457 return R.string.tether_settings_title_all;
458 } else if (wifiAvailable) {
459 return R.string.tether_settings_title_wifi;
460 } else if (usbAvailable && bluetoothAvailable) {
461 return R.string.tether_settings_title_usb_bluetooth;
462 } else if (usbAvailable) {
463 return R.string.tether_settings_title_usb;
464 } else {
465 return R.string.tether_settings_title_bluetooth;
466 }
467 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700468
469 /* Used by UserSettings as well. Call this on a non-ui thread. */
470 public static boolean copyMeProfilePhoto(Context context, UserInfo user) {
471 Uri contactUri = Profile.CONTENT_URI;
472
473 InputStream avatarDataStream = Contacts.openContactPhotoInputStream(
474 context.getContentResolver(),
475 contactUri, true);
476 // If there's no profile photo, assign a default avatar
477 if (avatarDataStream == null) {
478 return false;
479 }
480 int userId = user != null ? user.id : UserHandle.myUserId();
481 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700482 Bitmap icon = BitmapFactory.decodeStream(avatarDataStream);
483 um.setUserIcon(userId, icon);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700484 try {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700485 avatarDataStream.close();
486 } catch (IOException ioe) { }
487 return true;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700488 }
489
490 public static String getMeProfileName(Context context) {
491 Cursor cursor = context.getContentResolver().query(
492 Profile.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME},
493 null, null, null);
494 if (cursor == null) {
495 return null;
496 }
497
498 try {
499 if (cursor.moveToFirst()) {
500 return cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
501 }
502 } finally {
503 cursor.close();
504 }
505 return null;
506 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700507
508 /** Not global warming, it's global change warning. */
509 public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
510 final Runnable positiveAction) {
511 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
512 builder.setTitle(titleResId);
513 builder.setMessage(R.string.global_change_warning);
514 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
515 @Override
516 public void onClick(DialogInterface dialog, int which) {
517 positiveAction.run();
518 }
519 });
520 builder.setNegativeButton(android.R.string.cancel, null);
521
522 return builder.create();
523 }
524
525 public static boolean hasMultipleUsers(Context context) {
526 return ((UserManager) context.getSystemService(Context.USER_SERVICE))
527 .getUsers().size() > 1;
528 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800529}