blob: d743b6f0a6282c29a2f0a8fd634c53b85e601981 [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 Meglio63bbb8e2014-04-23 16:44:30 -070058import com.android.settings.dashboard.DashboardCategory;
59import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070060import com.android.settings.dashboard.Header;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070061
Amith Yamasaniae47ef42012-09-16 17:53:35 -070062import java.io.IOException;
63import java.io.InputStream;
Amith Yamasanic06d4c42011-02-25 14:35:20 -080064import java.net.InetAddress;
65import java.util.Iterator;
Daisuke Miyakawaa2633d02010-09-15 20:09:12 -070066import java.util.List;
Jean Chalard71ad1f42011-05-12 15:06:16 +090067import java.util.Locale;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069public class Utils {
70
71 /**
72 * Set the preference's title to the matching activity's label.
73 */
74 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
75
76 /**
Shuhrat Dehkanov96577682012-10-03 12:24:07 +090077 * The opacity level of a disabled icon.
78 */
79 public static final float DISABLED_ALPHA = 0.4f;
80
81 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +020082 * Name of the meta-data item that should be set in the AndroidManifest.xml
83 * to specify the icon that should be displayed for the preference.
84 */
85 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
86
87 /**
88 * Name of the meta-data item that should be set in the AndroidManifest.xml
89 * to specify the title that should be displayed for the preference.
90 */
91 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
92
93 /**
94 * Name of the meta-data item that should be set in the AndroidManifest.xml
95 * to specify the summary text that should be displayed for the preference.
96 */
97 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
98
99 /**
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 * Finds a matching activity for a preference's intent. If a matching
101 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -0800102 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103 * @param context The context.
104 * @param parentPreferenceGroup The preference group that contains the
105 * preference whose intent is being resolved.
106 * @param preferenceKey The key of the preference whose intent is being
107 * resolved.
108 * @param flags 0 or one or more of
109 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
110 * .
111 * @return Whether an activity was found. If false, the preference was
112 * removed.
113 */
114 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
115 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -0800116
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
118 if (preference == null) {
119 return false;
120 }
Ying Wanga7188322010-01-04 18:45:10 -0800121
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122 Intent intent = preference.getIntent();
123 if (intent != null) {
124 // Find the activity that is in the system image
125 PackageManager pm = context.getPackageManager();
126 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
127 int listSize = list.size();
128 for (int i = 0; i < listSize; i++) {
129 ResolveInfo resolveInfo = list.get(i);
130 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
131 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -0800132
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 // Replace the intent with this specific activity
134 preference.setIntent(new Intent().setClassName(
135 resolveInfo.activityInfo.packageName,
136 resolveInfo.activityInfo.name));
137
138 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
139 // Set the preference title to the activity's label
140 preference.setTitle(resolveInfo.loadLabel(pm));
141 }
Ying Wanga7188322010-01-04 18:45:10 -0800142
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800143 return true;
144 }
145 }
146 }
147
148 // Did not find a matching activity, so remove the preference
149 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -0800150
Shuhrat Dehkanov7dc567a2012-04-23 01:59:56 +0900151 return false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800152 }
Ying Wanga7188322010-01-04 18:45:10 -0800153
154 /**
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200155 * Finds a matching activity for a preference's intent. If a matching
156 * activity is not found, it will remove the preference. The icon, title and
157 * summary of the preference will also be updated with the values retrieved
158 * from the activity's meta-data elements. If no meta-data elements are
159 * specified then the preference title will be set to match the label of the
160 * activity, an icon and summary text will not be displayed.
161 *
162 * @param context The context.
163 * @param parentPreferenceGroup The preference group that contains the
164 * preference whose intent is being resolved.
165 * @param preferenceKey The key of the preference whose intent is being
166 * resolved.
167 *
168 * @return Whether an activity was found. If false, the preference was
169 * removed.
170 *
171 * @see {@link #META_DATA_PREFERENCE_ICON}
172 * {@link #META_DATA_PREFERENCE_TITLE}
173 * {@link #META_DATA_PREFERENCE_SUMMARY}
174 */
175 public static boolean updatePreferenceToSpecificActivityFromMetaDataOrRemove(Context context,
176 PreferenceGroup parentPreferenceGroup, String preferenceKey) {
177
178 IconPreferenceScreen preference = (IconPreferenceScreen)parentPreferenceGroup
179 .findPreference(preferenceKey);
180 if (preference == null) {
181 return false;
182 }
183
184 Intent intent = preference.getIntent();
185 if (intent != null) {
186 // Find the activity that is in the system image
187 PackageManager pm = context.getPackageManager();
188 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
189 int listSize = list.size();
190 for (int i = 0; i < listSize; i++) {
191 ResolveInfo resolveInfo = list.get(i);
192 if ((resolveInfo.activityInfo.applicationInfo.flags
193 & ApplicationInfo.FLAG_SYSTEM) != 0) {
194 Drawable icon = null;
195 String title = null;
196 String summary = null;
197
198 // Get the activity's meta-data
199 try {
200 Resources res = pm
201 .getResourcesForApplication(resolveInfo.activityInfo.packageName);
202 Bundle metaData = resolveInfo.activityInfo.metaData;
203
204 if (res != null && metaData != null) {
205 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
206 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
207 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
208 }
209 } catch (NameNotFoundException e) {
210 // Ignore
211 } catch (NotFoundException e) {
212 // Ignore
213 }
214
215 // Set the preference title to the activity's label if no
216 // meta-data is found
217 if (TextUtils.isEmpty(title)) {
218 title = resolveInfo.loadLabel(pm).toString();
219 }
220
221 // Set icon, title and summary for the preference
222 preference.setIcon(icon);
223 preference.setTitle(title);
224 preference.setSummary(summary);
225
226 // Replace the intent with this specific activity
227 preference.setIntent(new Intent().setClassName(
228 resolveInfo.activityInfo.packageName,
229 resolveInfo.activityInfo.name));
230
231 return true;
232 }
233 }
234 }
235
236 // Did not find a matching activity, so remove the preference
237 parentPreferenceGroup.removePreference(preference);
238
239 return false;
240 }
241
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700242 public static boolean updateHeaderToSpecificActivityFromMetaDataOrRemove(Context context,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700243 List<Header> target, Header header) {
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700244
245 Intent intent = header.intent;
246 if (intent != null) {
247 // Find the activity that is in the system image
248 PackageManager pm = context.getPackageManager();
249 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
250 int listSize = list.size();
251 for (int i = 0; i < listSize; i++) {
252 ResolveInfo resolveInfo = list.get(i);
253 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
254 != 0) {
255 Drawable icon = null;
256 String title = null;
257 String summary = null;
258
259 // Get the activity's meta-data
260 try {
261 Resources res = pm.getResourcesForApplication(
262 resolveInfo.activityInfo.packageName);
263 Bundle metaData = resolveInfo.activityInfo.metaData;
264
265 if (res != null && metaData != null) {
266 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
267 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
268 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
269 }
270 } catch (NameNotFoundException e) {
271 // Ignore
272 } catch (NotFoundException e) {
273 // Ignore
274 }
275
276 // Set the preference title to the activity's label if no
277 // meta-data is found
278 if (TextUtils.isEmpty(title)) {
279 title = resolveInfo.loadLabel(pm).toString();
280 }
281
282 // Set icon, title and summary for the preference
283 // TODO:
284 //header.icon = icon;
285 header.title = title;
286 header.summary = summary;
287 // Replace the intent with this specific activity
288 header.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
289 resolveInfo.activityInfo.name);
290
291 return true;
292 }
293 }
294 }
295
296 // Did not find a matching activity, so remove the preference
Dianne Hackborn79d42bb2012-11-28 18:05:00 -0800297 target.remove(header);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700298
299 return false;
300 }
301
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700302 public static boolean updateTileToSpecificActivityFromMetaDataOrRemove(Context context,
303 DashboardCategory target, DashboardTile tile) {
304
305 Intent intent = tile.intent;
306 if (intent != null) {
307 // Find the activity that is in the system image
308 PackageManager pm = context.getPackageManager();
309 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
310 int listSize = list.size();
311 for (int i = 0; i < listSize; i++) {
312 ResolveInfo resolveInfo = list.get(i);
313 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
314 != 0) {
315 Drawable icon = null;
316 String title = null;
317 String summary = null;
318
319 // Get the activity's meta-data
320 try {
321 Resources res = pm.getResourcesForApplication(
322 resolveInfo.activityInfo.packageName);
323 Bundle metaData = resolveInfo.activityInfo.metaData;
324
325 if (res != null && metaData != null) {
326 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON));
327 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
328 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
329 }
330 } catch (NameNotFoundException e) {
331 // Ignore
332 } catch (NotFoundException e) {
333 // Ignore
334 }
335
336 // Set the preference title to the activity's label if no
337 // meta-data is found
338 if (TextUtils.isEmpty(title)) {
339 title = resolveInfo.loadLabel(pm).toString();
340 }
341
342 // Set icon, title and summary for the preference
343 // TODO:
344 //tile.icon = icon;
345 tile.title = title;
346 tile.summary = summary;
347 // Replace the intent with this specific activity
348 tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
349 resolveInfo.activityInfo.name);
350
351 return true;
352 }
353 }
354 }
355
356 // Did not find a matching activity, so remove the preference
357 target.removeTile(tile);
358
359 return false;
360 }
361
Anders Hammar1b2dd9032010-04-08 10:03:50 +0200362 /**
Ying Wanga7188322010-01-04 18:45:10 -0800363 * Returns true if Monkey is running.
364 */
365 public static boolean isMonkeyRunning() {
Amith Yamasaniae697552011-09-27 11:33:17 -0700366 return ActivityManager.isUserAMonkey();
Ying Wanga7188322010-01-04 18:45:10 -0800367 }
Amith Yamasani60133dd2010-09-11 14:17:31 -0700368
369 /**
370 * Returns whether the device is voice-capable (meaning, it is also a phone).
371 */
372 public static boolean isVoiceCapable(Context context) {
373 TelephonyManager telephony =
374 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
375 return telephony != null && telephony.isVoiceCapable();
376 }
Amith Yamasani0f85c482011-02-23 17:19:11 -0800377
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700378 public static boolean isWifiOnly(Context context) {
379 ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
380 Context.CONNECTIVITY_SERVICE);
381 return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
Amith Yamasani0f85c482011-02-23 17:19:11 -0800382 }
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800383
384 /**
385 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
386 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900387 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800388 */
389 public static String getWifiIpAddresses(Context context) {
390 ConnectivityManager cm = (ConnectivityManager)
391 context.getSystemService(Context.CONNECTIVITY_SERVICE);
392 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
Amith Yamasani6822b742011-10-17 16:41:00 -0700393 return formatIpAddresses(prop);
394 }
395
396 /**
397 * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
398 * addresses.
399 * @param context the application context
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900400 * @return the formatted and newline-separated IP addresses, or null if none.
Amith Yamasani6822b742011-10-17 16:41:00 -0700401 */
Lorenzo Colitti6eb6a902013-11-08 03:53:29 +0900402 public static String getDefaultIpAddresses(ConnectivityManager cm) {
Amith Yamasani6822b742011-10-17 16:41:00 -0700403 LinkProperties prop = cm.getActiveLinkProperties();
404 return formatIpAddresses(prop);
405 }
406
407 private static String formatIpAddresses(LinkProperties prop) {
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800408 if (prop == null) return null;
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900409 Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800410 // If there are no entries, return null
411 if (!iter.hasNext()) return null;
412 // Concatenate all available addresses, comma separated
413 String addresses = "";
414 while (iter.hasNext()) {
415 addresses += iter.next().getHostAddress();
Lorenzo Colitti769f0692013-08-01 17:30:07 +0900416 if (iter.hasNext()) addresses += "\n";
Amith Yamasanic06d4c42011-02-25 14:35:20 -0800417 }
418 return addresses;
419 }
Jean Chalard71ad1f42011-05-12 15:06:16 +0900420
421 public static Locale createLocaleFromString(String localeStr) {
422 // TODO: is there a better way to actually construct a locale that will match?
423 // The main problem is, on top of Java specs, locale.toString() and
424 // new Locale(locale.toString()).toString() do not return equal() strings in
425 // many cases, because the constructor takes the only string as the language
426 // code. So : new Locale("en", "US").toString() => "en_US"
427 // And : new Locale("en_US").toString() => "en_us"
428 if (null == localeStr)
429 return Locale.getDefault();
430 String[] brokenDownLocale = localeStr.split("_", 3);
431 // split may not return a 0-length array.
432 if (1 == brokenDownLocale.length) {
433 return new Locale(brokenDownLocale[0]);
434 } else if (2 == brokenDownLocale.length) {
435 return new Locale(brokenDownLocale[0], brokenDownLocale[1]);
436 } else {
437 return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]);
438 }
439 }
Amith Yamasania4379d62011-07-22 10:34:58 -0700440
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900441 public static boolean isBatteryPresent(Intent batteryChangedIntent) {
442 return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
443 }
444
Amith Yamasania4379d62011-07-22 10:34:58 -0700445 public static String getBatteryPercentage(Intent batteryChangedIntent) {
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900446 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
447 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
Amith Yamasania4379d62011-07-22 10:34:58 -0700448 return String.valueOf(level * 100 / scale) + "%";
449 }
450
451 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
452 final Intent intent = batteryChangedIntent;
453
Jaewan Kima3fe77b2013-06-04 21:17:40 +0900454 int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
455 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
456 BatteryManager.BATTERY_STATUS_UNKNOWN);
Amith Yamasania4379d62011-07-22 10:34:58 -0700457 String statusString;
458 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
459 statusString = res.getString(R.string.battery_info_status_charging);
460 if (plugType > 0) {
Brian Muramatsu0bd54a62012-08-14 15:13:16 -0700461 int resId;
462 if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
463 resId = R.string.battery_info_status_charging_ac;
464 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
465 resId = R.string.battery_info_status_charging_usb;
466 } else {
467 resId = R.string.battery_info_status_charging_wireless;
468 }
469 statusString = statusString + " " + res.getString(resId);
Amith Yamasania4379d62011-07-22 10:34:58 -0700470 }
471 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
472 statusString = res.getString(R.string.battery_info_status_discharging);
473 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
474 statusString = res.getString(R.string.battery_info_status_not_charging);
475 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
476 statusString = res.getString(R.string.battery_info_status_full);
477 } else {
478 statusString = res.getString(R.string.battery_info_status_unknown);
479 }
480
481 return statusString;
482 }
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700483
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800484 public static void forcePrepareCustomPreferencesList(
485 ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) {
486 list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
487 list.setClipToPadding(false);
488 prepareCustomPreferencesList(parent, child, list, ignoreSidePadding);
489 }
490
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700491 /**
492 * Prepare a custom preferences layout, moving padding to {@link ListView}
493 * when outside scrollbars are requested. Usually used to display
494 * {@link ListView} and {@link TabWidget} with correct padding.
495 */
Jeff Sharkey5d706792011-09-08 18:57:17 -0700496 public static void prepareCustomPreferencesList(
Jeff Sharkey97d07fa2012-11-30 12:36:53 -0800497 ViewGroup parent, View child, View list, boolean ignoreSidePadding) {
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700498 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY;
499 if (movePadding && parent instanceof PreferenceFrameLayout) {
500 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true;
501
502 final Resources res = list.getResources();
Amith Yamasani56f51a82013-08-05 10:07:23 -0700503 final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700504 final int paddingBottom = res.getDimensionPixelSize(
505 com.android.internal.R.dimen.preference_fragment_padding_bottom);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700506
Amith Yamasanif0b05de2012-05-09 09:43:33 -0700507 final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide;
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800508 list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700509 }
510 }
Jeff Sharkeya83a24f2011-09-16 01:52:39 -0700511
512 /**
513 * Return string resource that best describes combination of tethering
514 * options available on this device.
515 */
516 public static int getTetheringLabel(ConnectivityManager cm) {
517 String[] usbRegexs = cm.getTetherableUsbRegexs();
518 String[] wifiRegexs = cm.getTetherableWifiRegexs();
519 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
520
521 boolean usbAvailable = usbRegexs.length != 0;
522 boolean wifiAvailable = wifiRegexs.length != 0;
523 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
524
525 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
526 return R.string.tether_settings_title_all;
527 } else if (wifiAvailable && usbAvailable) {
528 return R.string.tether_settings_title_all;
529 } else if (wifiAvailable && bluetoothAvailable) {
530 return R.string.tether_settings_title_all;
531 } else if (wifiAvailable) {
532 return R.string.tether_settings_title_wifi;
533 } else if (usbAvailable && bluetoothAvailable) {
534 return R.string.tether_settings_title_usb_bluetooth;
535 } else if (usbAvailable) {
536 return R.string.tether_settings_title_usb;
537 } else {
538 return R.string.tether_settings_title_bluetooth;
539 }
540 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700541
542 /* Used by UserSettings as well. Call this on a non-ui thread. */
543 public static boolean copyMeProfilePhoto(Context context, UserInfo user) {
544 Uri contactUri = Profile.CONTENT_URI;
545
546 InputStream avatarDataStream = Contacts.openContactPhotoInputStream(
547 context.getContentResolver(),
548 contactUri, true);
549 // If there's no profile photo, assign a default avatar
550 if (avatarDataStream == null) {
551 return false;
552 }
553 int userId = user != null ? user.id : UserHandle.myUserId();
554 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700555 Bitmap icon = BitmapFactory.decodeStream(avatarDataStream);
556 um.setUserIcon(userId, icon);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700557 try {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700558 avatarDataStream.close();
559 } catch (IOException ioe) { }
560 return true;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700561 }
562
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700563 public static String getMeProfileName(Context context, boolean full) {
564 if (full) {
565 return getProfileDisplayName(context);
566 } else {
567 return getShorterNameIfPossible(context);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700568 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700569 }
570
571 private static String getShorterNameIfPossible(Context context) {
572 final String given = getLocalProfileGivenName(context);
573 return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context);
574 }
575
576 private static String getLocalProfileGivenName(Context context) {
577 final ContentResolver cr = context.getContentResolver();
578
579 // Find the raw contact ID for the local ME profile raw contact.
580 final long localRowProfileId;
581 final Cursor localRawProfile = cr.query(
582 Profile.CONTENT_RAW_CONTACTS_URI,
583 new String[] {RawContacts._ID},
584 RawContacts.ACCOUNT_TYPE + " IS NULL AND " +
585 RawContacts.ACCOUNT_NAME + " IS NULL",
586 null, null);
587 if (localRawProfile == null) return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700588
589 try {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700590 if (!localRawProfile.moveToFirst()) {
591 return null;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700592 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700593 localRowProfileId = localRawProfile.getLong(0);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700594 } finally {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700595 localRawProfile.close();
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700596 }
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700597
598 // Find the structured name for the raw contact.
599 final Cursor structuredName = cr.query(
600 Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(),
601 new String[] {CommonDataKinds.StructuredName.GIVEN_NAME,
602 CommonDataKinds.StructuredName.FAMILY_NAME},
603 Data.RAW_CONTACT_ID + "=" + localRowProfileId,
604 null, null);
605 if (structuredName == null) return null;
606
607 try {
608 if (!structuredName.moveToFirst()) {
609 return null;
610 }
611 String partialName = structuredName.getString(0);
612 if (TextUtils.isEmpty(partialName)) {
613 partialName = structuredName.getString(1);
614 }
615 return partialName;
616 } finally {
617 structuredName.close();
618 }
619 }
620
621 private static final String getProfileDisplayName(Context context) {
622 final ContentResolver cr = context.getContentResolver();
623 final Cursor profile = cr.query(Profile.CONTENT_URI,
624 new String[] {Profile.DISPLAY_NAME}, null, null, null);
625 if (profile == null) return null;
626
627 try {
628 if (!profile.moveToFirst()) {
629 return null;
630 }
631 return profile.getString(0);
632 } finally {
633 profile.close();
634 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700635 }
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700636
637 /** Not global warming, it's global change warning. */
638 public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
639 final Runnable positiveAction) {
640 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
641 builder.setTitle(titleResId);
642 builder.setMessage(R.string.global_change_warning);
643 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
644 @Override
645 public void onClick(DialogInterface dialog, int which) {
646 positiveAction.run();
647 }
648 });
649 builder.setNegativeButton(android.R.string.cancel, null);
650
651 return builder.create();
652 }
653
654 public static boolean hasMultipleUsers(Context context) {
655 return ((UserManager) context.getSystemService(Context.USER_SERVICE))
656 .getUsers().size() > 1;
657 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800658}