blob: 1ae08299099ce6fc7ce0bdaea67add2e94c65b68 [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;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070058import com.android.settings.dashboard.Header;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070059
Amith Yamasaniae47ef42012-09-16 17:53:35 -070060import java.io.IOException;
61import java.io.InputStream;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080062import java.net.InetAddress;
63import java.util.Iterator;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070064import java.util.List;
Jean Chalard71ad1f42011-05-12 15:06:16 +090065import java.util.Locale;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067public class Utils {
68
69 /**
70 * Set the preference's title to the matching activity's label.
71 */
72 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
73
74 /**
Shuhrat Dehkanov96577682012-10-03 12:24:07 +090075 * The opacity level of a disabled icon.
76 */
77 public static final float DISABLED_ALPHA = 0.4f;
78
79 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020080 * Name of the meta-data item that should be set in the AndroidManifest.xml
81 * to specify the icon that should be displayed for the preference.
82 */
83 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
84
85 /**
86 * Name of the meta-data item that should be set in the AndroidManifest.xml
87 * to specify the title that should be displayed for the preference.
88 */
89 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
90
91 /**
92 * Name of the meta-data item that should be set in the AndroidManifest.xml
93 * to specify the summary text that should be displayed for the preference.
94 */
95 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
96
97 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 * Finds a matching activity for a preference's intent. If a matching
99 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -0800100 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 * @param context The context.
102 * @param parentPreferenceGroup The preference group that contains the
103 * preference whose intent is being resolved.
104 * @param preferenceKey The key of the preference whose intent is being
105 * resolved.
106 * @param flags 0 or one or more of
107 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
108 * .
109 * @return Whether an activity was found. If false, the preference was
110 * removed.
111 */
112 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
113 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -0800114
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
116 if (preference == null) {
117 return false;
118 }
Ying Wanga7188322010-01-04 18:45:10 -0800119
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 Intent intent = preference.getIntent();
121 if (intent != null) {
122 // Find the activity that is in the system image
123 PackageManager pm = context.getPackageManager();
124 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
125 int listSize = list.size();
126 for (int i = 0; i < listSize; i++) {
127 ResolveInfo resolveInfo = list.get(i);
128 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
129 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -0800130
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800131 // Replace the intent with this specific activity
132 preference.setIntent(new Intent().setClassName(
133 resolveInfo.activityInfo.packageName,
134 resolveInfo.activityInfo.name));
135
136 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
137 // Set the preference title to the activity's label
138 preference.setTitle(resolveInfo.loadLabel(pm));
139 }
Ying Wanga7188322010-01-04 18:45:10 -0800140
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800141 return true;
142 }
143 }
144 }
145
146 // Did not find a matching activity, so remove the preference
147 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800148
Shuhrat Dehkanov7dc567a2012-04-23 01:59:56 +0900149 return false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 }
Ying Wanga7188322010-01-04 18:45:10 -0800151
152 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200153 * Finds a matching activity for a preference's intent. If a matching
154 * activity is not found, it will remove the preference. The icon, title and
155 * summary of the preference will also be updated with the values retrieved
156 * from the activity's meta-data elements. If no meta-data elements are
157 * specified then the preference title will be set to match the label of the
158 * activity, an icon and summary text will not be displayed.
159 *
160 * @param context The context.
161 * @param parentPreferenceGroup The preference group that contains the
162 * preference whose intent is being resolved.
163 * @param preferenceKey The key of the preference whose intent is being
164 * resolved.
165 *
166 * @return Whether an activity was found. If false, the preference was
167 * removed.
168 *
169 * @see {@link #META_DATA_PREFERENCE_ICON}
170 * {@link #META_DATA_PREFERENCE_TITLE}
171 * {@link #META_DATA_PREFERENCE_SUMMARY}
172 */
173 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
174 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
175
176 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
177 .findPreference(preferenceKey);
178 if (preference == null) {
179 return false;
180 }
181
182 Intent intent = preference.getIntent();
183 if (intent != null) {
184 // Find the activity that is in the system image
185 PackageManager pm = context.getPackageManager();
186 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
187 int listSize = list.size();
188 for (int i = 0; i < listSize; i++) {
189 ResolveInfo resolveInfo = list.get(i);
190 if ((resolveInfo.activityInfo.applicationInfo.flags
191 & ApplicationInfo.FLAG_SYSTEM) != 0) {
192 Drawable icon = null;
193 String title = null;
194 String summary = null;
195
196 // Get the activity's meta-data
197 try {
198 Resources res = pm
199 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
200 Bundle metaData = resolveInfo.activityInfo.metaData;
201
202 if (res != null && metaData != null) {
203 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
204 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
205 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
206 }
207 } catch (NameNotFoundException e) {
208 // Ignore
209 } catch (NotFoundException e) {
210 // Ignore
211 }
212
213 // Set the preference title to the activity's label if no
214 // meta-data is found
215 if (TextUtils.isEmpty(title)) {
216 title = resolveInfo.loadLabel(pm).toString();
217 }
218
219 // Set icon, title and summary for the preference
220 preference.setIcon(icon);
221 preference.setTitle(title);
222 preference.setSummary(summary);
223
224 // Replace the intent with this specific activity
225 preference.setIntent(new Intent().setClassName(
226 resolveInfo.activityInfo.packageName,
227 resolveInfo.activityInfo.name));
228
229 return true;
230 }
231 }
232 }
233
234 // Did not find a matching activity, so remove the preference
235 parentPreferenceGroup.removePreference(preference);
236
237 return false;
238 }
239
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700240 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700241 List<Header> target, Header header) {
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700242
243 Intent intent = header.intent;
244 if (intent != null) {
245 // Find the activity that is in the system image
246 PackageManager pm = context.getPackageManager();
247 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
248 int listSize = list.size();
249 for (int i = 0; i < listSize; i++) {
250 ResolveInfo resolveInfo = list.get(i);
251 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
252 != 0) {
253 Drawable icon = null;
254 String title = null;
255 String summary = null;
256
257 // Get the activity's meta-data
258 try {
259 Resources res = pm.getResourcesForApplication(
260 resolveInfo.activityInfo.packageName);
261 Bundle metaData = resolveInfo.activityInfo.metaData;
262
263 if (res != null && metaData != null) {
264 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
265 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
266 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
267 }
268 } catch (NameNotFoundException e) {
269 // Ignore
270 } catch (NotFoundException e) {
271 // Ignore
272 }
273
274 // Set the preference title to the activity's label if no
275 // meta-data is found
276 if (TextUtils.isEmpty(title)) {
277 title = resolveInfo.loadLabel(pm).toString();
278 }
279
280 // Set icon, title and summary for the preference
281 // TODO:
282 //header.icon = icon;
283 header.title = title;
284 header.summary = summary;
285 // Replace the intent with this specific activity
286 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
287 resolveInfo.activityInfo.name);
288
289 return true;
290 }
291 }
292 }
293
294 // Did not find a matching activity, so remove the preference
Dianne Hackborn79d42bb2012-11-28 18:05:00 -0800295 target.remove(header);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700296
297 return false;
298 }
299
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200300 /**
Ying Wanga7188322010-01-04 18:45:10 -0800301 * Returns true if Monkey is running.
302 */
303 public static boolean isMonkeyRunning() {
Amith Yamasaniae697552011-09-27 11:33:17 -0700304 return ActivityManager.isUserAMonkey();
Ying Wanga7188322010-01-04 18:45:10 -0800305 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700306
307 /**
308 * Returns whether the device is voice-capable (meaning, it is also a phone).
309 */
310 public static boolean isVoiceCapable(Context context) {
311 TelephonyManager telephony =
312 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
313 return telephony != null && telephony.isVoiceCapable();
314 }
Amith Yamasani0f85c482011-02-23 17:19:11 -0800315
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700316 public static boolean isWifiOnly(Context context) {
317 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
318 Context.CONNECTIVITY_SERVICE);
319 return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
Amith Yamasani0f85c482011-02-23 17:19:11 -0800320 }
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800321
322 /**
323 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
324 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900325 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800326 */
327 public static String getWifiIpAddresses(Context context) {
328 ConnectivityManager cm = (ConnectivityManager)
329 context.getSystemService(Context.CONNECTIVITY_SERVICE);
330 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
Amith Yamasani6822b742011-10-17 16:41:00 -0700331 return formatIpAddresses(prop);
332 }
333
334 /**
335 * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
336 * addresses.
337 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900338 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasani6822b742011-10-17 16:41:00 -0700339 */
Lorenzo Colitti6eb6a902013-11-08 03:53:29 +0900340 public static String getDefaultIpAddresses(ConnectivityManager cm) {
Amith Yamasani6822b742011-10-17 16:41:00 -0700341 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;
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900347 Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800348 // 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();
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900354 if (iter.hasNext()) addresses += "\n";
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800355 }
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
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900379 public static boolean isBatteryPresent(Intent batteryChangedIntent) {
380 return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
381 }
382
Amith Yamasania4379d62011-07-22 10:34:58 -0700383 public static String getBatteryPercentage(Intent batteryChangedIntent) {
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900384 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
385 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
Amith Yamasania4379d62011-07-22 10:34:58 -0700386 return String.valueOf(level * 100 / scale) + "%";
387 }
388
389 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
390 final Intent intent = batteryChangedIntent;
391
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900392 int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
393 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
394 BatteryManager.BATTERY_STATUS_UNKNOWN);
Amith Yamasania4379d62011-07-22 10:34:58 -0700395 String statusString;
396 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
397 statusString = res.getString(R.string.battery_info_status_charging);
398 if (plugType > 0) {
Brian Muramatsu0bd54a62012-08-14 15:13:16 -0700399 int resId;
400 if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
401 resId = R.string.battery_info_status_charging_ac;
402 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
403 resId = R.string.battery_info_status_charging_usb;
404 } else {
405 resId = R.string.battery_info_status_charging_wireless;
406 }
407 statusString = statusString + " " + res.getString(resId);
Amith Yamasania4379d62011-07-22 10:34:58 -0700408 }
409 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
410 statusString = res.getString(R.string.battery_info_status_discharging);
411 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
412 statusString = res.getString(R.string.battery_info_status_not_charging);
413 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
414 statusString = res.getString(R.string.battery_info_status_full);
415 } else {
416 statusString = res.getString(R.string.battery_info_status_unknown);
417 }
418
419 return statusString;
420 }
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700421
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800422 public static void forcePrepareCustomPreferencesList(
423 ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) {
424 list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
425 list.setClipToPadding(false);
426 prepareCustomPreferencesList(parent, child, list, ignoreSidePadding);
427 }
428
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700429 /**
430 * Prepare a custom preferences layout, moving padding to {@link ListView}
431 * when outside scrollbars are requested. Usually used to display
432 * {@link ListView} and {@link TabWidget} with correct padding.
433 */
Jeff Sharkey5d706792011-09-08 18:57:17 -0700434 public static void prepareCustomPreferencesList(
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800435 ViewGroup parent, View child, View list, boolean ignoreSidePadding) {
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700436 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY;
437 if (movePadding && parent instanceof PreferenceFrameLayout) {
438 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true;
439
440 final Resources res = list.getResources();
Amith Yamasani56f51a82013-08-05 10:07:23 -0700441 final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700442 final int paddingBottom = res.getDimensionPixelSize(
443 com.android.internal.R.dimen.preference_fragment_padding_bottom);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700444
Amith Yamasanif0b05de2012-05-09 09:43:33 -0700445 final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide;
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800446 list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700447 }
448 }
Jeff Sharkeya83a24f2011-09-16 01:52:39 -0700449
450 /**
451 * Return string resource that best describes combination of tethering
452 * options available on this device.
453 */
454 public static int getTetheringLabel(ConnectivityManager cm) {
455 String[] usbRegexs = cm.getTetherableUsbRegexs();
456 String[] wifiRegexs = cm.getTetherableWifiRegexs();
457 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
458
459 boolean usbAvailable = usbRegexs.length != 0;
460 boolean wifiAvailable = wifiRegexs.length != 0;
461 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
462
463 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
464 return R.string.tether_settings_title_all;
465 } else if (wifiAvailable && usbAvailable) {
466 return R.string.tether_settings_title_all;
467 } else if (wifiAvailable && bluetoothAvailable) {
468 return R.string.tether_settings_title_all;
469 } else if (wifiAvailable) {
470 return R.string.tether_settings_title_wifi;
471 } else if (usbAvailable && bluetoothAvailable) {
472 return R.string.tether_settings_title_usb_bluetooth;
473 } else if (usbAvailable) {
474 return R.string.tether_settings_title_usb;
475 } else {
476 return R.string.tether_settings_title_bluetooth;
477 }
478 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700479
480 /* Used by UserSettings as well. Call this on a non-ui thread. */
481 public static boolean copyMeProfilePhoto(Context context, UserInfo user) {
482 Uri contactUri = Profile.CONTENT_URI;
483
484 InputStream avatarDataStream = Contacts.openContactPhotoInputStream(
485 context.getContentResolver(),
486 contactUri, true);
487 // If there's no profile photo, assign a default avatar
488 if (avatarDataStream == null) {
489 return false;
490 }
491 int userId = user != null ? user.id : UserHandle.myUserId();
492 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700493 Bitmap icon = BitmapFactory.decodeStream(avatarDataStream);
494 um.setUserIcon(userId, icon);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700495 try {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700496 avatarDataStream.close();
497 } catch (IOException ioe) { }
498 return true;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700499 }
500
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700501 public static String getMeProfileName(Context context, boolean full) {
502 if (full) {
503 return getProfileDisplayName(context);
504 } else {
505 return getShorterNameIfPossible(context);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700506 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700507 }
508
509 private static String getShorterNameIfPossible(Context context) {
510 final String given = getLocalProfileGivenName(context);
511 return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context);
512 }
513
514 private static String getLocalProfileGivenName(Context context) {
515 final ContentResolver cr = context.getContentResolver();
516
517 // Find the raw contact ID for the local ME profile raw contact.
518 final long localRowProfileId;
519 final Cursor localRawProfile = cr.query(
520 Profile.CONTENT_RAW_CONTACTS_URI,
521 new String[] {RawContacts._ID},
522 RawContacts.ACCOUNT_TYPE + " IS NULL AND " +
523 RawContacts.ACCOUNT_NAME + " IS NULL",
524 null, null);
525 if (localRawProfile == null) return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700526
527 try {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700528 if (!localRawProfile.moveToFirst()) {
529 return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700530 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700531 localRowProfileId = localRawProfile.getLong(0);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700532 } finally {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700533 localRawProfile.close();
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700534 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700535
536 // Find the structured name for the raw contact.
537 final Cursor structuredName = cr.query(
538 Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(),
539 new String[] {CommonDataKinds.StructuredName.GIVEN_NAME,
540 CommonDataKinds.StructuredName.FAMILY_NAME},
541 Data.RAW_CONTACT_ID + "=" + localRowProfileId,
542 null, null);
543 if (structuredName == null) return null;
544
545 try {
546 if (!structuredName.moveToFirst()) {
547 return null;
548 }
549 String partialName = structuredName.getString(0);
550 if (TextUtils.isEmpty(partialName)) {
551 partialName = structuredName.getString(1);
552 }
553 return partialName;
554 } finally {
555 structuredName.close();
556 }
557 }
558
559 private static final String getProfileDisplayName(Context context) {
560 final ContentResolver cr = context.getContentResolver();
561 final Cursor profile = cr.query(Profile.CONTENT_URI,
562 new String[] {Profile.DISPLAY_NAME}, null, null, null);
563 if (profile == null) return null;
564
565 try {
566 if (!profile.moveToFirst()) {
567 return null;
568 }
569 return profile.getString(0);
570 } finally {
571 profile.close();
572 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700573 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700574
575 /** Not global warming, it's global change warning. */
576 public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
577 final Runnable positiveAction) {
578 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
579 builder.setTitle(titleResId);
580 builder.setMessage(R.string.global_change_warning);
581 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
582 @Override
583 public void onClick(DialogInterface dialog, int which) {
584 positiveAction.run();
585 }
586 });
587 builder.setNegativeButton(android.R.string.cancel, null);
588
589 return builder.create();
590 }
591
592 public static boolean hasMultipleUsers(Context context) {
593 return ((UserManager) context.getSystemService(Context.USER_SERVICE))
594 .getUsers().size() > 1;
595 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800596}