blob: bf44835b1e07615ade1e101f62b6f5ac931f993b [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 Yamasaniae697552011-09-27 11:33:17 -070019import android.app.ActivityManager;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070020import android.app.AlertDialog;
21import android.app.Dialog;
Amith Yamasani8d40fac2012-10-23 15:36:16 -070022import android.content.ContentResolver;
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.UserHandle;
43import android.os.UserManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080044import android.preference.Preference;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070045import android.preference.PreferenceFrameLayout;
46import android.preference.PreferenceGroup;
Amith Yamasani8d40fac2012-10-23 15:36:16 -070047import android.provider.ContactsContract.CommonDataKinds;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070048import android.provider.ContactsContract.Contacts;
Amith Yamasani8d40fac2012-10-23 15:36:16 -070049import android.provider.ContactsContract.Data;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070050import android.provider.ContactsContract.Profile;
Amith Yamasani8d40fac2012-10-23 15:36:16 -070051import android.provider.ContactsContract.RawContacts;
Amith Yamasani60133dd2010-09-11 14:17:31 -070052import android.telephony.TelephonyManager;
Anders Hammar1b2dd9032010-04-08 10:03:50 +020053import android.text.TextUtils;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070054import android.view.View;
55import android.view.ViewGroup;
56import android.widget.ListView;
57import android.widget.TabWidget;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070058
Amith Yamasaniae47ef42012-09-16 17:53:35 -070059import java.io.IOException;
60import java.io.InputStream;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080061import java.net.InetAddress;
62import java.util.Iterator;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070063import java.util.List;
Jean Chalard71ad1f42011-05-12 15:06:16 +090064import java.util.Locale;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066public class Utils {
67
68 /**
69 * Set the preference's title to the matching activity's label.
70 */
71 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
72
73 /**
Shuhrat Dehkanov96577682012-10-03 12:24:07 +090074 * The opacity level of a disabled icon.
75 */
76 public static final float DISABLED_ALPHA = 0.4f;
77
78 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020079 * Name of the meta-data item that should be set in the AndroidManifest.xml
80 * to specify the icon that should be displayed for the preference.
81 */
82 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
83
84 /**
85 * Name of the meta-data item that should be set in the AndroidManifest.xml
86 * to specify the title that should be displayed for the preference.
87 */
88 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
89
90 /**
91 * Name of the meta-data item that should be set in the AndroidManifest.xml
92 * to specify the summary text that should be displayed for the preference.
93 */
94 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
95
96 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 * Finds a matching activity for a preference's intent. If a matching
98 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080099 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 * @param context The context.
101 * @param parentPreferenceGroup The preference group that contains the
102 * preference whose intent is being resolved.
103 * @param preferenceKey The key of the preference whose intent is being
104 * resolved.
105 * @param flags 0 or one or more of
106 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
107 * .
108 * @return Whether an activity was found. If false, the preference was
109 * removed.
110 */
111 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
112 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -0800113
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
115 if (preference == null) {
116 return false;
117 }
Ying Wanga7188322010-01-04 18:45:10 -0800118
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 Intent intent = preference.getIntent();
120 if (intent != null) {
121 // Find the activity that is in the system image
122 PackageManager pm = context.getPackageManager();
123 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
124 int listSize = list.size();
125 for (int i = 0; i < listSize; i++) {
126 ResolveInfo resolveInfo = list.get(i);
127 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
128 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -0800129
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800130 // Replace the intent with this specific activity
131 preference.setIntent(new Intent().setClassName(
132 resolveInfo.activityInfo.packageName,
133 resolveInfo.activityInfo.name));
134
135 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
136 // Set the preference title to the activity's label
137 preference.setTitle(resolveInfo.loadLabel(pm));
138 }
Ying Wanga7188322010-01-04 18:45:10 -0800139
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800140 return true;
141 }
142 }
143 }
144
145 // Did not find a matching activity, so remove the preference
146 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800147
Shuhrat Dehkanov7dc567a2012-04-23 01:59:56 +0900148 return false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149 }
Ying Wanga7188322010-01-04 18:45:10 -0800150
151 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200152 * Finds a matching activity for a preference's intent. If a matching
153 * activity is not found, it will remove the preference. The icon, title and
154 * summary of the preference will also be updated with the values retrieved
155 * from the activity's meta-data elements. If no meta-data elements are
156 * specified then the preference title will be set to match the label of the
157 * activity, an icon and summary text will not be displayed.
158 *
159 * @param context The context.
160 * @param parentPreferenceGroup The preference group that contains the
161 * preference whose intent is being resolved.
162 * @param preferenceKey The key of the preference whose intent is being
163 * resolved.
164 *
165 * @return Whether an activity was found. If false, the preference was
166 * removed.
167 *
168 * @see {@link #META_DATA_PREFERENCE_ICON}
169 * {@link #META_DATA_PREFERENCE_TITLE}
170 * {@link #META_DATA_PREFERENCE_SUMMARY}
171 */
172 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
173 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
174
175 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
176 .findPreference(preferenceKey);
177 if (preference == null) {
178 return false;
179 }
180
181 Intent intent = preference.getIntent();
182 if (intent != null) {
183 // Find the activity that is in the system image
184 PackageManager pm = context.getPackageManager();
185 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
186 int listSize = list.size();
187 for (int i = 0; i < listSize; i++) {
188 ResolveInfo resolveInfo = list.get(i);
189 if ((resolveInfo.activityInfo.applicationInfo.flags
190 & ApplicationInfo.FLAG_SYSTEM) != 0) {
191 Drawable icon = null;
192 String title = null;
193 String summary = null;
194
195 // Get the activity's meta-data
196 try {
197 Resources res = pm
198 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
199 Bundle metaData = resolveInfo.activityInfo.metaData;
200
201 if (res != null && metaData != null) {
202 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
203 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
204 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
205 }
206 } catch (NameNotFoundException e) {
207 // Ignore
208 } catch (NotFoundException e) {
209 // Ignore
210 }
211
212 // Set the preference title to the activity's label if no
213 // meta-data is found
214 if (TextUtils.isEmpty(title)) {
215 title = resolveInfo.loadLabel(pm).toString();
216 }
217
218 // Set icon, title and summary for the preference
219 preference.setIcon(icon);
220 preference.setTitle(title);
221 preference.setSummary(summary);
222
223 // Replace the intent with this specific activity
224 preference.setIntent(new Intent().setClassName(
225 resolveInfo.activityInfo.packageName,
226 resolveInfo.activityInfo.name));
227
228 return true;
229 }
230 }
231 }
232
233 // Did not find a matching activity, so remove the preference
234 parentPreferenceGroup.removePreference(preference);
235
236 return false;
237 }
238
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700239 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 List<SettingsActivity.Header> target, SettingsActivity.Header header) {
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700241
242 Intent intent = header.intent;
243 if (intent != null) {
244 // Find the activity that is in the system image
245 PackageManager pm = context.getPackageManager();
246 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
247 int listSize = list.size();
248 for (int i = 0; i < listSize; i++) {
249 ResolveInfo resolveInfo = list.get(i);
250 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
251 != 0) {
252 Drawable icon = null;
253 String title = null;
254 String summary = null;
255
256 // Get the activity's meta-data
257 try {
258 Resources res = pm.getResourcesForApplication(
259 resolveInfo.activityInfo.packageName);
260 Bundle metaData = resolveInfo.activityInfo.metaData;
261
262 if (res != null && metaData != null) {
263 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
264 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
265 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
266 }
267 } catch (NameNotFoundException e) {
268 // Ignore
269 } catch (NotFoundException e) {
270 // Ignore
271 }
272
273 // Set the preference title to the activity's label if no
274 // meta-data is found
275 if (TextUtils.isEmpty(title)) {
276 title = resolveInfo.loadLabel(pm).toString();
277 }
278
279 // Set icon, title and summary for the preference
280 // TODO:
281 //header.icon = icon;
282 header.title = title;
283 header.summary = summary;
284 // Replace the intent with this specific activity
285 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
286 resolveInfo.activityInfo.name);
287
288 return true;
289 }
290 }
291 }
292
293 // Did not find a matching activity, so remove the preference
Dianne Hackborn79d42bb2012-11-28 18:05:00 -0800294 target.remove(header);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700295
296 return false;
297 }
298
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200299 /**
Ying Wanga7188322010-01-04 18:45:10 -0800300 * Returns true if Monkey is running.
301 */
302 public static boolean isMonkeyRunning() {
Amith Yamasaniae697552011-09-27 11:33:17 -0700303 return ActivityManager.isUserAMonkey();
Ying Wanga7188322010-01-04 18:45:10 -0800304 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700305
306 /**
307 * Returns whether the device is voice-capable (meaning, it is also a phone).
308 */
309 public static boolean isVoiceCapable(Context context) {
310 TelephonyManager telephony =
311 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
312 return telephony != null && telephony.isVoiceCapable();
313 }
Amith Yamasani0f85c482011-02-23 17:19:11 -0800314
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700315 public static boolean isWifiOnly(Context context) {
316 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
317 Context.CONNECTIVITY_SERVICE);
318 return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
Amith Yamasani0f85c482011-02-23 17:19:11 -0800319 }
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800320
321 /**
322 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
323 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900324 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800325 */
326 public static String getWifiIpAddresses(Context context) {
327 ConnectivityManager cm = (ConnectivityManager)
328 context.getSystemService(Context.CONNECTIVITY_SERVICE);
329 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
Amith Yamasani6822b742011-10-17 16:41:00 -0700330 return formatIpAddresses(prop);
331 }
332
333 /**
334 * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
335 * addresses.
336 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900337 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasani6822b742011-10-17 16:41:00 -0700338 */
Lorenzo Colitti6eb6a902013-11-08 03:53:29 +0900339 public static String getDefaultIpAddresses(ConnectivityManager cm) {
Amith Yamasani6822b742011-10-17 16:41:00 -0700340 LinkProperties prop = cm.getActiveLinkProperties();
341 return formatIpAddresses(prop);
342 }
343
344 private static String formatIpAddresses(LinkProperties prop) {
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800345 if (prop == null) return null;
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900346 Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800347 // If there are no entries, return null
348 if (!iter.hasNext()) return null;
349 // Concatenate all available addresses, comma separated
350 String addresses = "";
351 while (iter.hasNext()) {
352 addresses += iter.next().getHostAddress();
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900353 if (iter.hasNext()) addresses += "\n";
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800354 }
355 return addresses;
356 }
Jean Chalard71ad1f42011-05-12 15:06:16 +0900357
358 public static Locale createLocaleFromString(String localeStr) {
359 // TODO: is there a better way to actually construct a locale that will match?
360 // The main problem is, on top of Java specs, locale.toString() and
361 // new Locale(locale.toString()).toString() do not return equal() strings in
362 // many cases, because the constructor takes the only string as the language
363 // code. So : new Locale("en", "US").toString() => "en_US"
364 // And : new Locale("en_US").toString() => "en_us"
365 if (null == localeStr)
366 return Locale.getDefault();
367 String[] brokenDownLocale = localeStr.split("_", 3);
368 // split may not return a 0-length array.
369 if (1 == brokenDownLocale.length) {
370 return new Locale(brokenDownLocale[0]);
371 } else if (2 == brokenDownLocale.length) {
372 return new Locale(brokenDownLocale[0], brokenDownLocale[1]);
373 } else {
374 return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]);
375 }
376 }
Amith Yamasania4379d62011-07-22 10:34:58 -0700377
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900378 public static boolean isBatteryPresent(Intent batteryChangedIntent) {
379 return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
380 }
381
Amith Yamasania4379d62011-07-22 10:34:58 -0700382 public static String getBatteryPercentage(Intent batteryChangedIntent) {
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900383 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
384 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
Amith Yamasania4379d62011-07-22 10:34:58 -0700385 return String.valueOf(level * 100 / scale) + "%";
386 }
387
388 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
389 final Intent intent = batteryChangedIntent;
390
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900391 int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
392 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
393 BatteryManager.BATTERY_STATUS_UNKNOWN);
Amith Yamasania4379d62011-07-22 10:34:58 -0700394 String statusString;
395 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
396 statusString = res.getString(R.string.battery_info_status_charging);
397 if (plugType > 0) {
Brian Muramatsu0bd54a62012-08-14 15:13:16 -0700398 int resId;
399 if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
400 resId = R.string.battery_info_status_charging_ac;
401 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
402 resId = R.string.battery_info_status_charging_usb;
403 } else {
404 resId = R.string.battery_info_status_charging_wireless;
405 }
406 statusString = statusString + " " + res.getString(resId);
Amith Yamasania4379d62011-07-22 10:34:58 -0700407 }
408 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
409 statusString = res.getString(R.string.battery_info_status_discharging);
410 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
411 statusString = res.getString(R.string.battery_info_status_not_charging);
412 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
413 statusString = res.getString(R.string.battery_info_status_full);
414 } else {
415 statusString = res.getString(R.string.battery_info_status_unknown);
416 }
417
418 return statusString;
419 }
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700420
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800421 public static void forcePrepareCustomPreferencesList(
422 ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) {
423 list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
424 list.setClipToPadding(false);
425 prepareCustomPreferencesList(parent, child, list, ignoreSidePadding);
426 }
427
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700428 /**
429 * Prepare a custom preferences layout, moving padding to {@link ListView}
430 * when outside scrollbars are requested. Usually used to display
431 * {@link ListView} and {@link TabWidget} with correct padding.
432 */
Jeff Sharkey5d706792011-09-08 18:57:17 -0700433 public static void prepareCustomPreferencesList(
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800434 ViewGroup parent, View child, View list, boolean ignoreSidePadding) {
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700435 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY;
436 if (movePadding && parent instanceof PreferenceFrameLayout) {
437 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true;
438
439 final Resources res = list.getResources();
Amith Yamasani56f51a82013-08-05 10:07:23 -0700440 final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700441 final int paddingBottom = res.getDimensionPixelSize(
442 com.android.internal.R.dimen.preference_fragment_padding_bottom);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700443
Amith Yamasanif0b05de2012-05-09 09:43:33 -0700444 final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide;
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800445 list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700446 }
447 }
Jeff Sharkeya83a24f2011-09-16 01:52:39 -0700448
449 /**
450 * Return string resource that best describes combination of tethering
451 * options available on this device.
452 */
453 public static int getTetheringLabel(ConnectivityManager cm) {
454 String[] usbRegexs = cm.getTetherableUsbRegexs();
455 String[] wifiRegexs = cm.getTetherableWifiRegexs();
456 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
457
458 boolean usbAvailable = usbRegexs.length != 0;
459 boolean wifiAvailable = wifiRegexs.length != 0;
460 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
461
462 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
463 return R.string.tether_settings_title_all;
464 } else if (wifiAvailable && usbAvailable) {
465 return R.string.tether_settings_title_all;
466 } else if (wifiAvailable && bluetoothAvailable) {
467 return R.string.tether_settings_title_all;
468 } else if (wifiAvailable) {
469 return R.string.tether_settings_title_wifi;
470 } else if (usbAvailable && bluetoothAvailable) {
471 return R.string.tether_settings_title_usb_bluetooth;
472 } else if (usbAvailable) {
473 return R.string.tether_settings_title_usb;
474 } else {
475 return R.string.tether_settings_title_bluetooth;
476 }
477 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700478
479 /* Used by UserSettings as well. Call this on a non-ui thread. */
480 public static boolean copyMeProfilePhoto(Context context, UserInfo user) {
481 Uri contactUri = Profile.CONTENT_URI;
482
483 InputStream avatarDataStream = Contacts.openContactPhotoInputStream(
484 context.getContentResolver(),
485 contactUri, true);
486 // If there's no profile photo, assign a default avatar
487 if (avatarDataStream == null) {
488 return false;
489 }
490 int userId = user != null ? user.id : UserHandle.myUserId();
491 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700492 Bitmap icon = BitmapFactory.decodeStream(avatarDataStream);
493 um.setUserIcon(userId, icon);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700494 try {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700495 avatarDataStream.close();
496 } catch (IOException ioe) { }
497 return true;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700498 }
499
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700500 public static String getMeProfileName(Context context, boolean full) {
501 if (full) {
502 return getProfileDisplayName(context);
503 } else {
504 return getShorterNameIfPossible(context);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700505 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700506 }
507
508 private static String getShorterNameIfPossible(Context context) {
509 final String given = getLocalProfileGivenName(context);
510 return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context);
511 }
512
513 private static String getLocalProfileGivenName(Context context) {
514 final ContentResolver cr = context.getContentResolver();
515
516 // Find the raw contact ID for the local ME profile raw contact.
517 final long localRowProfileId;
518 final Cursor localRawProfile = cr.query(
519 Profile.CONTENT_RAW_CONTACTS_URI,
520 new String[] {RawContacts._ID},
521 RawContacts.ACCOUNT_TYPE + " IS NULL AND " +
522 RawContacts.ACCOUNT_NAME + " IS NULL",
523 null, null);
524 if (localRawProfile == null) return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700525
526 try {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700527 if (!localRawProfile.moveToFirst()) {
528 return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700529 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700530 localRowProfileId = localRawProfile.getLong(0);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700531 } finally {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700532 localRawProfile.close();
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700533 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700534
535 // Find the structured name for the raw contact.
536 final Cursor structuredName = cr.query(
537 Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(),
538 new String[] {CommonDataKinds.StructuredName.GIVEN_NAME,
539 CommonDataKinds.StructuredName.FAMILY_NAME},
540 Data.RAW_CONTACT_ID + "=" + localRowProfileId,
541 null, null);
542 if (structuredName == null) return null;
543
544 try {
545 if (!structuredName.moveToFirst()) {
546 return null;
547 }
548 String partialName = structuredName.getString(0);
549 if (TextUtils.isEmpty(partialName)) {
550 partialName = structuredName.getString(1);
551 }
552 return partialName;
553 } finally {
554 structuredName.close();
555 }
556 }
557
558 private static final String getProfileDisplayName(Context context) {
559 final ContentResolver cr = context.getContentResolver();
560 final Cursor profile = cr.query(Profile.CONTENT_URI,
561 new String[] {Profile.DISPLAY_NAME}, null, null, null);
562 if (profile == null) return null;
563
564 try {
565 if (!profile.moveToFirst()) {
566 return null;
567 }
568 return profile.getString(0);
569 } finally {
570 profile.close();
571 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700572 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700573
574 /** Not global warming, it's global change warning. */
575 public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
576 final Runnable positiveAction) {
577 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
578 builder.setTitle(titleResId);
579 builder.setMessage(R.string.global_change_warning);
580 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
581 @Override
582 public void onClick(DialogInterface dialog, int which) {
583 positiveAction.run();
584 }
585 });
586 builder.setNegativeButton(android.R.string.cancel, null);
587
588 return builder.create();
589 }
590
591 public static boolean hasMultipleUsers(Context context) {
592 return ((UserManager) context.getSystemService(Context.USER_SERVICE))
593 .getUsers().size() > 1;
594 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800595}