The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 19 | import static android.content.Intent.EXTRA_USER; |
| 20 | |
| 21 | import android.annotation.Nullable; |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 23 | import android.app.ActivityManagerNative; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 27 | import android.app.IActivityManager; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 28 | import android.content.ComponentName; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 30 | import android.content.Context; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 31 | import android.content.DialogInterface; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 32 | import android.content.Intent; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 33 | import android.content.IntentFilter; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | import android.content.pm.ApplicationInfo; |
Dan Sandler | b58b512 | 2014-09-02 18:31:49 +0200 | [diff] [blame] | 35 | import android.content.pm.PackageInfo; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 36 | import android.content.pm.PackageManager; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 38 | import android.content.pm.ResolveInfo; |
Dan Sandler | b58b512 | 2014-09-02 18:31:49 +0200 | [diff] [blame] | 39 | import android.content.pm.Signature; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 40 | import android.content.pm.UserInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 41 | import android.content.res.Resources; |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 42 | import android.content.res.TypedArray; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 43 | import android.content.res.Resources.NotFoundException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 44 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 45 | import android.graphics.Bitmap; |
| 46 | import android.graphics.BitmapFactory; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 47 | import android.graphics.drawable.Drawable; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 48 | import android.hardware.usb.IUsbManager; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 49 | import android.net.ConnectivityManager; |
| 50 | import android.net.LinkProperties; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 52 | import android.os.BatteryManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 53 | import android.os.Bundle; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 54 | import android.os.IBinder; |
| 55 | import android.os.RemoteException; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 56 | import android.os.UserHandle; |
| 57 | import android.os.UserManager; |
Fabrice Di Meglio | dff3faa | 2015-02-27 11:14:11 -0800 | [diff] [blame] | 58 | import android.os.storage.StorageManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 59 | import android.preference.Preference; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 60 | import android.preference.PreferenceFrameLayout; |
| 61 | import android.preference.PreferenceGroup; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 62 | import android.provider.ContactsContract.CommonDataKinds; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 63 | import android.provider.ContactsContract.Contacts; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 64 | import android.provider.ContactsContract.Data; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 65 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 66 | import android.provider.ContactsContract.RawContacts; |
Andres Morales | ce249fe | 2014-07-07 16:58:16 -0700 | [diff] [blame] | 67 | import android.service.persistentdata.PersistentDataBlockManager; |
Wink Saville | ca75661 | 2014-11-08 10:47:12 -0800 | [diff] [blame] | 68 | import android.telephony.SubscriptionInfo; |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 69 | import android.telephony.SubscriptionManager; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 70 | import android.telephony.TelephonyManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 71 | import android.text.TextUtils; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 72 | import android.util.Log; |
Zoltan Szatmary-Ban | e5814ff | 2014-12-19 16:27:45 +0000 | [diff] [blame] | 73 | import android.util.SparseArray; |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 74 | import android.view.LayoutInflater; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 75 | import android.view.View; |
| 76 | import android.view.ViewGroup; |
| 77 | import android.widget.ListView; |
| 78 | import android.widget.TabWidget; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 79 | |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 80 | import com.android.internal.util.UserIcons; |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 81 | import com.android.settings.UserAdapter.UserDetails; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 82 | import com.android.settings.applications.ApplicationsState; |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 83 | import com.android.settings.dashboard.DashboardTile; |
Alexandra Gherghina | fe47a8d | 2014-07-18 17:23:37 +0100 | [diff] [blame] | 84 | import com.android.settings.drawable.CircleFramedDrawable; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 85 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 86 | import java.io.IOException; |
| 87 | import java.io.InputStream; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 88 | import java.net.InetAddress; |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 89 | import java.text.NumberFormat; |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 90 | import java.util.ArrayList; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 91 | import java.util.Collections; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 92 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 93 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 94 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 95 | |
Fabrice Di Meglio | c9711be | 2014-06-17 12:30:18 -0700 | [diff] [blame] | 96 | public final class Utils { |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 97 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 98 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Set the preference's title to the matching activity's label. |
| 101 | */ |
| 102 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 103 | |
| 104 | /** |
Shuhrat Dehkanov | 9657768 | 2012-10-03 12:24:07 +0900 | [diff] [blame] | 105 | * The opacity level of a disabled icon. |
| 106 | */ |
| 107 | public static final float DISABLED_ALPHA = 0.4f; |
| 108 | |
| 109 | /** |
Dianne Hackborn | 68f005f | 2014-06-18 18:29:12 -0700 | [diff] [blame] | 110 | * Color spectrum to use to indicate badness. 0 is completely transparent (no data), |
| 111 | * 1 is most bad (red), the last value is least bad (green). |
| 112 | */ |
| 113 | public static final int[] BADNESS_COLORS = new int[] { |
| 114 | 0x00000000, 0xffc43828, 0xffe54918, 0xfff47b00, |
| 115 | 0xfffabf2c, 0xff679e37, 0xff0a7f42 |
| 116 | }; |
| 117 | |
| 118 | /** |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 119 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 120 | * to specify the icon that should be displayed for the preference. |
| 121 | */ |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 122 | public static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 126 | * to specify the title that should be displayed for the preference. |
| 127 | */ |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 128 | public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Name of the meta-data item that should be set in the AndroidManifest.xml |
| 132 | * to specify the summary text that should be displayed for the preference. |
| 133 | */ |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 134 | public static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 135 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 136 | private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; |
| 137 | |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 138 | private static final int SECONDS_PER_MINUTE = 60; |
| 139 | private static final int SECONDS_PER_HOUR = 60 * 60; |
| 140 | private static final int SECONDS_PER_DAY = 24 * 60 * 60; |
| 141 | |
Zoltan Szatmary-Ban | e5814ff | 2014-12-19 16:27:45 +0000 | [diff] [blame] | 142 | private static SparseArray<Bitmap> sDarkDefaultUserBitmapCache = new SparseArray<Bitmap>(); |
| 143 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 144 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 145 | * Finds a matching activity for a preference's intent. If a matching |
| 146 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 147 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 148 | * @param context The context. |
| 149 | * @param parentPreferenceGroup The preference group that contains the |
| 150 | * preference whose intent is being resolved. |
| 151 | * @param preferenceKey The key of the preference whose intent is being |
| 152 | * resolved. |
| 153 | * @param flags 0 or one or more of |
| 154 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 155 | * . |
| 156 | * @return Whether an activity was found. If false, the preference was |
| 157 | * removed. |
| 158 | */ |
| 159 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 160 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 161 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 162 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 163 | if (preference == null) { |
| 164 | return false; |
| 165 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 166 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 167 | Intent intent = preference.getIntent(); |
| 168 | if (intent != null) { |
| 169 | // Find the activity that is in the system image |
| 170 | PackageManager pm = context.getPackageManager(); |
| 171 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 172 | int listSize = list.size(); |
| 173 | for (int i = 0; i < listSize; i++) { |
| 174 | ResolveInfo resolveInfo = list.get(i); |
| 175 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 176 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 177 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 178 | // Replace the intent with this specific activity |
| 179 | preference.setIntent(new Intent().setClassName( |
| 180 | resolveInfo.activityInfo.packageName, |
| 181 | resolveInfo.activityInfo.name)); |
| 182 | |
| 183 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 184 | // Set the preference title to the activity's label |
| 185 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 186 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 187 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Did not find a matching activity, so remove the preference |
| 194 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 195 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 196 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 197 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 198 | |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 199 | public static boolean updateTileToSpecificActivityFromMetaDataOrRemove(Context context, |
Amith Yamasani | 57fd5fd | 2014-08-06 15:48:10 -0700 | [diff] [blame] | 200 | DashboardTile tile) { |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 201 | |
| 202 | Intent intent = tile.intent; |
| 203 | if (intent != null) { |
| 204 | // Find the activity that is in the system image |
| 205 | PackageManager pm = context.getPackageManager(); |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 206 | List<ResolveInfo> list = tile.userHandle.size() != 0 |
| 207 | ? pm.queryIntentActivitiesAsUser(intent, PackageManager.GET_META_DATA, |
| 208 | tile.userHandle.get(0).getIdentifier()) |
| 209 | : pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 210 | int listSize = list.size(); |
| 211 | for (int i = 0; i < listSize; i++) { |
| 212 | ResolveInfo resolveInfo = list.get(i); |
| 213 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 214 | != 0) { |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 215 | int icon = 0; |
| 216 | CharSequence title = null; |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 217 | String summary = null; |
| 218 | |
| 219 | // Get the activity's meta-data |
| 220 | try { |
| 221 | Resources res = pm.getResourcesForApplication( |
| 222 | resolveInfo.activityInfo.packageName); |
| 223 | Bundle metaData = resolveInfo.activityInfo.metaData; |
| 224 | |
| 225 | if (res != null && metaData != null) { |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 226 | if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) { |
| 227 | icon = metaData.getInt(META_DATA_PREFERENCE_ICON); |
| 228 | } |
| 229 | if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) { |
| 230 | title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); |
| 231 | } |
| 232 | if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) { |
| 233 | summary = res.getString( |
| 234 | metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); |
| 235 | } |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 236 | } |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 237 | } catch (NameNotFoundException | NotFoundException e) { |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 238 | // Ignore |
| 239 | } |
| 240 | |
| 241 | // Set the preference title to the activity's label if no |
| 242 | // meta-data is found |
| 243 | if (TextUtils.isEmpty(title)) { |
| 244 | title = resolveInfo.loadLabel(pm).toString(); |
| 245 | } |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 246 | if (icon == 0) { |
| 247 | icon = resolveInfo.activityInfo.icon; |
| 248 | } |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 249 | |
| 250 | // Set icon, title and summary for the preference |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 251 | tile.iconRes = icon; |
| 252 | tile.iconPkg = resolveInfo.activityInfo.packageName; |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 253 | tile.title = title; |
| 254 | tile.summary = summary; |
| 255 | // Replace the intent with this specific activity |
| 256 | tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, |
| 257 | resolveInfo.activityInfo.name); |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Fabrice Di Meglio | 63bbb8e | 2014-04-23 16:44:30 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 267 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 268 | * Returns true if Monkey is running. |
| 269 | */ |
| 270 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 271 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 272 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 276 | */ |
| 277 | public static boolean isVoiceCapable(Context context) { |
| 278 | TelephonyManager telephony = |
| 279 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 280 | return telephony != null && telephony.isVoiceCapable(); |
| 281 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 282 | |
Robert Greenwalt | 8af88fb | 2011-08-31 11:17:47 -0700 | [diff] [blame] | 283 | public static boolean isWifiOnly(Context context) { |
| 284 | ConnectivityManager cm = (ConnectivityManager)context.getSystemService( |
| 285 | Context.CONNECTIVITY_SERVICE); |
| 286 | return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 287 | } |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 291 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 292 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 293 | */ |
| 294 | public static String getWifiIpAddresses(Context context) { |
| 295 | ConnectivityManager cm = (ConnectivityManager) |
| 296 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 297 | LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 298 | return formatIpAddresses(prop); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style |
| 303 | * addresses. |
| 304 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 305 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 306 | */ |
Lorenzo Colitti | 6eb6a90 | 2013-11-08 03:53:29 +0900 | [diff] [blame] | 307 | public static String getDefaultIpAddresses(ConnectivityManager cm) { |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 308 | LinkProperties prop = cm.getActiveLinkProperties(); |
| 309 | return formatIpAddresses(prop); |
| 310 | } |
| 311 | |
| 312 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 313 | if (prop == null) return null; |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 314 | Iterator<InetAddress> iter = prop.getAllAddresses().iterator(); |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 315 | // If there are no entries, return null |
| 316 | if (!iter.hasNext()) return null; |
| 317 | // Concatenate all available addresses, comma separated |
| 318 | String addresses = ""; |
| 319 | while (iter.hasNext()) { |
| 320 | addresses += iter.next().getHostAddress(); |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 321 | if (iter.hasNext()) addresses += "\n"; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 322 | } |
| 323 | return addresses; |
| 324 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 325 | |
| 326 | public static Locale createLocaleFromString(String localeStr) { |
| 327 | // TODO: is there a better way to actually construct a locale that will match? |
| 328 | // The main problem is, on top of Java specs, locale.toString() and |
| 329 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 330 | // many cases, because the constructor takes the only string as the language |
| 331 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 332 | // And : new Locale("en_US").toString() => "en_us" |
| 333 | if (null == localeStr) |
| 334 | return Locale.getDefault(); |
| 335 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 336 | // split may not return a 0-length array. |
| 337 | if (1 == brokenDownLocale.length) { |
| 338 | return new Locale(brokenDownLocale[0]); |
| 339 | } else if (2 == brokenDownLocale.length) { |
| 340 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 341 | } else { |
| 342 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 343 | } |
| 344 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 346 | /** Formats the ratio of amount/total as a percentage. */ |
| 347 | public static String formatPercentage(long amount, long total) { |
| 348 | return formatPercentage(((double) amount) / total); |
| 349 | } |
| 350 | |
| 351 | /** Formats an integer from 0..100 as a percentage. */ |
| 352 | public static String formatPercentage(int percentage) { |
| 353 | return formatPercentage(((double) percentage) / 100.0); |
| 354 | } |
| 355 | |
| 356 | /** Formats a double from 0.0..1.0 as a percentage. */ |
| 357 | private static String formatPercentage(double percentage) { |
Elliott Hughes | 83f013c | 2014-10-02 16:36:32 -0700 | [diff] [blame] | 358 | return NumberFormat.getPercentInstance().format(percentage); |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 361 | public static boolean isBatteryPresent(Intent batteryChangedIntent) { |
| 362 | return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 363 | } |
| 364 | |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 365 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 366 | return formatPercentage(getBatteryLevel(batteryChangedIntent)); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | public static int getBatteryLevel(Intent batteryChangedIntent) { |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 370 | int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); |
| 371 | int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 372 | return (level * 100) / scale; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
| 376 | final Intent intent = batteryChangedIntent; |
| 377 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 378 | int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); |
| 379 | int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, |
| 380 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 381 | String statusString; |
| 382 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 383 | int resId; |
| 384 | if (plugType == BatteryManager.BATTERY_PLUGGED_AC) { |
| 385 | resId = R.string.battery_info_status_charging_ac; |
| 386 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) { |
| 387 | resId = R.string.battery_info_status_charging_usb; |
| 388 | } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { |
| 389 | resId = R.string.battery_info_status_charging_wireless; |
| 390 | } else { |
| 391 | resId = R.string.battery_info_status_charging; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 392 | } |
Dianne Hackborn | 5a9ace3 | 2014-05-15 17:04:42 -0700 | [diff] [blame] | 393 | statusString = res.getString(resId); |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 394 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 395 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 396 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 397 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 398 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 399 | statusString = res.getString(R.string.battery_info_status_full); |
| 400 | } else { |
| 401 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 402 | } |
| 403 | |
| 404 | return statusString; |
| 405 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 406 | |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 407 | public static void forcePrepareCustomPreferencesList( |
| 408 | ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) { |
| 409 | list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
| 410 | list.setClipToPadding(false); |
| 411 | prepareCustomPreferencesList(parent, child, list, ignoreSidePadding); |
| 412 | } |
| 413 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 416 | * when outside scrollbars are requested. Usually used to display |
| 417 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 418 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 419 | public static void prepareCustomPreferencesList( |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 420 | ViewGroup parent, View child, View list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 421 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 422 | if (movePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 423 | final Resources res = list.getResources(); |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 424 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 425 | final int paddingBottom = res.getDimensionPixelSize( |
| 426 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 427 | |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 428 | if (parent instanceof PreferenceFrameLayout) { |
| 429 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
| 430 | |
| 431 | final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; |
| 432 | list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); |
| 433 | } else { |
| 434 | list.setPaddingRelative(paddingSide, 0, paddingSide, paddingBottom); |
| 435 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 438 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 439 | public static void forceCustomPadding(View view, boolean additive) { |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 440 | final Resources res = view.getResources(); |
| 441 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 442 | |
| 443 | final int paddingStart = paddingSide + (additive ? view.getPaddingStart() : 0); |
| 444 | final int paddingEnd = paddingSide + (additive ? view.getPaddingEnd() : 0); |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 445 | final int paddingBottom = res.getDimensionPixelSize( |
| 446 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
| 447 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 448 | view.setPaddingRelative(paddingStart, 0, paddingEnd, paddingBottom); |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Return string resource that best describes combination of tethering |
| 453 | * options available on this device. |
| 454 | */ |
| 455 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 456 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 457 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 458 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 459 | |
| 460 | boolean usbAvailable = usbRegexs.length != 0; |
| 461 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 462 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 463 | |
| 464 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 465 | return R.string.tether_settings_title_all; |
| 466 | } else if (wifiAvailable && usbAvailable) { |
| 467 | return R.string.tether_settings_title_all; |
| 468 | } else if (wifiAvailable && bluetoothAvailable) { |
| 469 | return R.string.tether_settings_title_all; |
| 470 | } else if (wifiAvailable) { |
| 471 | return R.string.tether_settings_title_wifi; |
| 472 | } else if (usbAvailable && bluetoothAvailable) { |
| 473 | return R.string.tether_settings_title_usb_bluetooth; |
| 474 | } else if (usbAvailable) { |
| 475 | return R.string.tether_settings_title_usb; |
| 476 | } else { |
| 477 | return R.string.tether_settings_title_bluetooth; |
| 478 | } |
| 479 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 480 | |
| 481 | /* Used by UserSettings as well. Call this on a non-ui thread. */ |
| 482 | public static boolean copyMeProfilePhoto(Context context, UserInfo user) { |
| 483 | Uri contactUri = Profile.CONTENT_URI; |
| 484 | |
| 485 | InputStream avatarDataStream = Contacts.openContactPhotoInputStream( |
| 486 | context.getContentResolver(), |
| 487 | contactUri, true); |
| 488 | // If there's no profile photo, assign a default avatar |
| 489 | if (avatarDataStream == null) { |
| 490 | return false; |
| 491 | } |
| 492 | int userId = user != null ? user.id : UserHandle.myUserId(); |
| 493 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 494 | Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); |
| 495 | um.setUserIcon(userId, icon); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 496 | try { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 497 | avatarDataStream.close(); |
| 498 | } catch (IOException ioe) { } |
| 499 | return true; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 502 | public static String getMeProfileName(Context context, boolean full) { |
| 503 | if (full) { |
| 504 | return getProfileDisplayName(context); |
| 505 | } else { |
| 506 | return getShorterNameIfPossible(context); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 507 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | private static String getShorterNameIfPossible(Context context) { |
| 511 | final String given = getLocalProfileGivenName(context); |
| 512 | return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); |
| 513 | } |
| 514 | |
| 515 | private static String getLocalProfileGivenName(Context context) { |
| 516 | final ContentResolver cr = context.getContentResolver(); |
| 517 | |
| 518 | // Find the raw contact ID for the local ME profile raw contact. |
| 519 | final long localRowProfileId; |
| 520 | final Cursor localRawProfile = cr.query( |
| 521 | Profile.CONTENT_RAW_CONTACTS_URI, |
| 522 | new String[] {RawContacts._ID}, |
| 523 | RawContacts.ACCOUNT_TYPE + " IS NULL AND " + |
| 524 | RawContacts.ACCOUNT_NAME + " IS NULL", |
| 525 | null, null); |
| 526 | if (localRawProfile == null) return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 527 | |
| 528 | try { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 529 | if (!localRawProfile.moveToFirst()) { |
| 530 | return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 531 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 532 | localRowProfileId = localRawProfile.getLong(0); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 533 | } finally { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 534 | localRawProfile.close(); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 535 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 536 | |
| 537 | // Find the structured name for the raw contact. |
| 538 | final Cursor structuredName = cr.query( |
| 539 | Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), |
| 540 | new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, |
| 541 | CommonDataKinds.StructuredName.FAMILY_NAME}, |
| 542 | Data.RAW_CONTACT_ID + "=" + localRowProfileId, |
| 543 | null, null); |
| 544 | if (structuredName == null) return null; |
| 545 | |
| 546 | try { |
| 547 | if (!structuredName.moveToFirst()) { |
| 548 | return null; |
| 549 | } |
| 550 | String partialName = structuredName.getString(0); |
| 551 | if (TextUtils.isEmpty(partialName)) { |
| 552 | partialName = structuredName.getString(1); |
| 553 | } |
| 554 | return partialName; |
| 555 | } finally { |
| 556 | structuredName.close(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | private static final String getProfileDisplayName(Context context) { |
| 561 | final ContentResolver cr = context.getContentResolver(); |
| 562 | final Cursor profile = cr.query(Profile.CONTENT_URI, |
| 563 | new String[] {Profile.DISPLAY_NAME}, null, null, null); |
| 564 | if (profile == null) return null; |
| 565 | |
| 566 | try { |
| 567 | if (!profile.moveToFirst()) { |
| 568 | return null; |
| 569 | } |
| 570 | return profile.getString(0); |
| 571 | } finally { |
| 572 | profile.close(); |
| 573 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 574 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 575 | |
| 576 | /** Not global warming, it's global change warning. */ |
| 577 | public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId, |
| 578 | final Runnable positiveAction) { |
| 579 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 580 | builder.setTitle(titleResId); |
| 581 | builder.setMessage(R.string.global_change_warning); |
| 582 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 583 | @Override |
| 584 | public void onClick(DialogInterface dialog, int which) { |
| 585 | positiveAction.run(); |
| 586 | } |
| 587 | }); |
| 588 | builder.setNegativeButton(android.R.string.cancel, null); |
| 589 | |
| 590 | return builder.create(); |
| 591 | } |
| 592 | |
| 593 | public static boolean hasMultipleUsers(Context context) { |
| 594 | return ((UserManager) context.getSystemService(Context.USER_SERVICE)) |
| 595 | .getUsers().size() > 1; |
| 596 | } |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * Start a new instance of the activity, showing only the given fragment. |
| 600 | * When launched in this mode, the given preference fragment will be instantiated and fill the |
| 601 | * entire activity. |
| 602 | * |
| 603 | * @param context The context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 604 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 605 | * @param args Optional arguments to supply to the fragment. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 606 | * @param resultTo Option fragment that should receive the result of the activity launch. |
| 607 | * @param resultRequestCode If resultTo is non-null, this is the request code in which |
| 608 | * to report the result. |
| 609 | * @param titleResId resource id for the String to display for the title of this set |
| 610 | * of preferences. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 611 | * @param title String to display for the title of this set of preferences. |
| 612 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 613 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 614 | Fragment resultTo, int resultRequestCode, int titleResId, |
| 615 | CharSequence title) { |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 616 | startWithFragment(context, fragmentName, args, resultTo, resultRequestCode, |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 617 | null /* titleResPackageName */, titleResId, title, false /* not a shortcut */); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Start a new instance of the activity, showing only the given fragment. |
| 622 | * When launched in this mode, the given preference fragment will be instantiated and fill the |
| 623 | * entire activity. |
| 624 | * |
| 625 | * @param context The context. |
| 626 | * @param fragmentName The name of the fragment to display. |
| 627 | * @param args Optional arguments to supply to the fragment. |
| 628 | * @param resultTo Option fragment that should receive the result of the activity launch. |
| 629 | * @param resultRequestCode If resultTo is non-null, this is the request code in which |
| 630 | * to report the result. |
| 631 | * @param titleResPackageName Optional package name for the resource id of the title. |
| 632 | * @param titleResId resource id for the String to display for the title of this set |
| 633 | * of preferences. |
| 634 | * @param title String to display for the title of this set of preferences. |
| 635 | */ |
| 636 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
| 637 | Fragment resultTo, int resultRequestCode, String titleResPackageName, int titleResId, |
| 638 | CharSequence title) { |
| 639 | startWithFragment(context, fragmentName, args, resultTo, resultRequestCode, |
| 640 | titleResPackageName, titleResId, title, false /* not a shortcut */); |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 644 | Fragment resultTo, int resultRequestCode, int titleResId, |
| 645 | CharSequence title, boolean isShortcut) { |
| 646 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, |
| 647 | null /* titleResPackageName */, titleResId, title, isShortcut); |
| 648 | if (resultTo == null) { |
| 649 | context.startActivity(intent); |
| 650 | } else { |
| 651 | resultTo.startActivityForResult(intent, resultRequestCode); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | public static void startWithFragment(Context context, String fragmentName, Bundle args, |
| 656 | Fragment resultTo, int resultRequestCode, String titleResPackageName, int titleResId, |
| 657 | CharSequence title, boolean isShortcut) { |
| 658 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResPackageName, |
| 659 | titleResId, title, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 660 | if (resultTo == null) { |
| 661 | context.startActivity(intent); |
| 662 | } else { |
| 663 | resultTo.startActivityForResult(intent, resultRequestCode); |
| 664 | } |
| 665 | } |
| 666 | |
Zoltan Szatmary-Ban | 7a2ccf2 | 2014-09-18 10:26:11 +0100 | [diff] [blame] | 667 | public static void startWithFragmentAsUser(Context context, String fragmentName, Bundle args, |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 668 | int titleResId, CharSequence title, boolean isShortcut, |
| 669 | UserHandle userHandle) { |
| 670 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, |
| 671 | null /* titleResPackageName */, titleResId, title, isShortcut); |
| 672 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 673 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 674 | context.startActivityAsUser(intent, userHandle); |
| 675 | } |
| 676 | |
| 677 | public static void startWithFragmentAsUser(Context context, String fragmentName, Bundle args, |
| 678 | String titleResPackageName, int titleResId, CharSequence title, boolean isShortcut, |
| 679 | UserHandle userHandle) { |
| 680 | Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResPackageName, |
| 681 | titleResId, title, isShortcut); |
Zoltan Szatmary-Ban | d50c7a8 | 2014-09-22 17:14:08 +0100 | [diff] [blame] | 682 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 683 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Zoltan Szatmary-Ban | 7a2ccf2 | 2014-09-18 10:26:11 +0100 | [diff] [blame] | 684 | context.startActivityAsUser(intent, userHandle); |
| 685 | } |
| 686 | |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 687 | /** |
| 688 | * Build an Intent to launch a new activity showing the selected fragment. |
| 689 | * The implementation constructs an Intent that re-launches the current activity with the |
| 690 | * appropriate arguments to display the fragment. |
| 691 | * |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 692 | * |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 693 | * @param context The Context. |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 694 | * @param fragmentName The name of the fragment to display. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 695 | * @param args Optional arguments to supply to the fragment. |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 696 | * @param titleResPackageName Optional package name for the resource id of the title. |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 697 | * @param titleResId Optional title resource id to show for this item. |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 698 | * @param title Optional title to show for this item. |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 699 | * @param isShortcut tell if this is a Launcher Shortcut or not |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 700 | * @return Returns an Intent that can be launched to display the given |
| 701 | * fragment. |
| 702 | */ |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 703 | public static Intent onBuildStartFragmentIntent(Context context, String fragmentName, |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 704 | Bundle args, String titleResPackageName, int titleResId, CharSequence title, |
| 705 | boolean isShortcut) { |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 706 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 707 | intent.setClass(context, SubSettings.class); |
Fabrice Di Meglio | 93b77b7 | 2014-05-17 00:01:07 +0000 | [diff] [blame] | 708 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, fragmentName); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 709 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); |
Alexandra Gherghina | 62464b8 | 2014-08-11 12:40:13 +0100 | [diff] [blame] | 710 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME, |
| 711 | titleResPackageName); |
Fabrice Di Meglio | a9e7799 | 2014-06-09 12:52:24 -0700 | [diff] [blame] | 712 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, titleResId); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 713 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE, title); |
Fabrice Di Meglio | 0d643fd | 2014-06-16 20:11:27 -0700 | [diff] [blame] | 714 | intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, isShortcut); |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 715 | return intent; |
| 716 | } |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * Returns the managed profile of the current user or null if none found. |
| 720 | */ |
| 721 | public static UserHandle getManagedProfile(UserManager userManager) { |
| 722 | List<UserHandle> userProfiles = userManager.getUserProfiles(); |
| 723 | final int count = userProfiles.size(); |
| 724 | for (int i = 0; i < count; i++) { |
| 725 | final UserHandle profile = userProfiles.get(i); |
| 726 | if (profile.getIdentifier() == userManager.getUserHandle()) { |
| 727 | continue; |
| 728 | } |
| 729 | final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier()); |
| 730 | if (userInfo.isManagedProfile()) { |
| 731 | return profile; |
| 732 | } |
| 733 | } |
| 734 | return null; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Returns true if the current profile is a managed one. |
| 739 | */ |
| 740 | public static boolean isManagedProfile(UserManager userManager) { |
| 741 | UserInfo currentUser = userManager.getUserInfo(userManager.getUserHandle()); |
| 742 | return currentUser.isManagedProfile(); |
| 743 | } |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 744 | |
| 745 | /** |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 746 | * Creates a {@link UserAdapter} if there is more than one profile on the device. |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 747 | * |
| 748 | * <p> The adapter can be used to populate a spinner that switches between the Settings |
| 749 | * app on the different profiles. |
| 750 | * |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 751 | * @return a {@link UserAdapter} or null if there is only one profile. |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 752 | */ |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 753 | public static UserAdapter createUserSpinnerAdapter(UserManager userManager, |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 754 | Context context) { |
| 755 | List<UserHandle> userProfiles = userManager.getUserProfiles(); |
| 756 | if (userProfiles.size() < 2) { |
| 757 | return null; |
| 758 | } |
| 759 | |
| 760 | UserHandle myUserHandle = new UserHandle(UserHandle.myUserId()); |
| 761 | // The first option should be the current profile |
| 762 | userProfiles.remove(myUserHandle); |
| 763 | userProfiles.add(0, myUserHandle); |
| 764 | |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 765 | return createUserAdapter(userManager, context, userProfiles); |
| 766 | } |
| 767 | |
| 768 | public static UserAdapter createUserAdapter(UserManager userManager, |
| 769 | Context context, List<UserHandle> userProfiles) { |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 770 | ArrayList<UserDetails> userDetails = new ArrayList<UserDetails>(userProfiles.size()); |
| 771 | final int count = userProfiles.size(); |
| 772 | for (int i = 0; i < count; i++) { |
| 773 | userDetails.add(new UserDetails(userProfiles.get(i), userManager, context)); |
| 774 | } |
Jason Monk | 2ebc8a0 | 2015-02-13 15:23:19 -0500 | [diff] [blame] | 775 | return new UserAdapter(context, userDetails); |
Alexandra Gherghina | 80e1f1b | 2014-07-31 14:56:33 +0100 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 779 | * Returns the target user for a Settings activity. |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 780 | * |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 781 | * The target user can be either the current user, the user that launched this activity or |
| 782 | * the user contained as an extra in the arguments or intent extras. |
| 783 | * |
| 784 | * Note: This is secure in the sense that it only returns a target user different to the current |
| 785 | * one if the app launching this activity is the Settings app itself, running in the same user |
| 786 | * or in one that is in the same profile group, or if the user id is provided by the system. |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 787 | */ |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 788 | public static UserHandle getSecureTargetUser(IBinder activityToken, |
| 789 | UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) { |
| 790 | UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
| 791 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 792 | try { |
| 793 | String launchedFromPackage = am.getLaunchedFromPackage(activityToken); |
| 794 | boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage); |
| 795 | |
| 796 | UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
| 797 | am.getLaunchedFromUid(activityToken))); |
| 798 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 799 | // Check it's secure |
| 800 | if (isProfileOf(um, launchedFromUser)) { |
| 801 | return launchedFromUser; |
| 802 | } |
| 803 | } |
| 804 | UserHandle extrasUser = intentExtras != null |
| 805 | ? (UserHandle) intentExtras.getParcelable(EXTRA_USER) : null; |
| 806 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 807 | // Check it's secure |
| 808 | if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) { |
| 809 | return extrasUser; |
| 810 | } |
| 811 | } |
| 812 | UserHandle argumentsUser = arguments != null |
| 813 | ? (UserHandle) arguments.getParcelable(EXTRA_USER) : null; |
| 814 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 815 | // Check it's secure |
| 816 | if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) { |
| 817 | return argumentsUser; |
| 818 | } |
| 819 | } |
| 820 | } catch (RemoteException e) { |
| 821 | // Should not happen |
| 822 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 823 | } |
| 824 | return currentUser; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Returns the target user for a Settings activity. |
| 829 | * |
| 830 | * The target user can be either the current user, the user that launched this activity or |
| 831 | * the user contained as an extra in the arguments or intent extras. |
| 832 | * |
| 833 | * You should use {@link #getSecureTargetUser(IBinder, UserManager, Bundle, Bundle)} if |
| 834 | * possible. |
| 835 | * |
| 836 | * @see #getInsecureTargetUser(IBinder, Bundle, Bundle) |
| 837 | */ |
| 838 | public static UserHandle getInsecureTargetUser(IBinder activityToken, @Nullable Bundle arguments, |
| 839 | @Nullable Bundle intentExtras) { |
| 840 | UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
| 841 | IActivityManager am = ActivityManagerNative.getDefault(); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 842 | try { |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 843 | UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
| 844 | am.getLaunchedFromUid(activityToken))); |
| 845 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 846 | return launchedFromUser; |
| 847 | } |
| 848 | UserHandle extrasUser = intentExtras != null |
| 849 | ? (UserHandle) intentExtras.getParcelable(EXTRA_USER) : null; |
| 850 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 851 | return extrasUser; |
| 852 | } |
| 853 | UserHandle argumentsUser = arguments != null |
| 854 | ? (UserHandle) arguments.getParcelable(EXTRA_USER) : null; |
| 855 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 856 | return argumentsUser; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 857 | } |
| 858 | } catch (RemoteException e) { |
| 859 | // Should not happen |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 860 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 861 | return null; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 862 | } |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 863 | return currentUser; |
| 864 | } |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 865 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 866 | /** |
| 867 | * Returns true if the user provided is in the same profiles group as the current user. |
| 868 | */ |
| 869 | private static boolean isProfileOf(UserManager um, UserHandle otherUser) { |
| 870 | if (um == null || otherUser == null) return false; |
| 871 | return (UserHandle.myUserId() == otherUser.getIdentifier()) |
| 872 | || um.getUserProfiles().contains(otherUser); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 873 | } |
Amith Yamasani | 51c6dac | 2014-07-02 00:06:37 +0530 | [diff] [blame] | 874 | |
Andres Morales | ce249fe | 2014-07-07 16:58:16 -0700 | [diff] [blame] | 875 | |
| 876 | /** |
| 877 | * Returns whether or not this device is able to be OEM unlocked. |
| 878 | */ |
| 879 | static boolean isOemUnlockEnabled(Context context) { |
| 880 | PersistentDataBlockManager manager =(PersistentDataBlockManager) |
| 881 | context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
| 882 | return manager.getOemUnlockEnabled(); |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Allows enabling or disabling OEM unlock on this device. OEM unlocked |
| 887 | * devices allow users to flash other OSes to them. |
| 888 | */ |
| 889 | static void setOemUnlockEnabled(Context context, boolean enabled) { |
| 890 | PersistentDataBlockManager manager =(PersistentDataBlockManager) |
| 891 | context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
| 892 | manager.setOemUnlockEnabled(enabled); |
| 893 | } |
Alexandra Gherghina | 95b86a5 | 2014-07-24 19:13:25 +0100 | [diff] [blame] | 894 | |
| 895 | /** |
| 896 | * Returns a circular icon for a user. |
| 897 | */ |
| 898 | public static Drawable getUserIcon(Context context, UserManager um, UserInfo user) { |
Alexandra Gherghina | 0f5440f | 2014-10-30 11:56:18 +0000 | [diff] [blame] | 899 | if (user.isManagedProfile()) { |
| 900 | // We use predefined values for managed profiles |
| 901 | Bitmap b = BitmapFactory.decodeResource(context.getResources(), |
| 902 | com.android.internal.R.drawable.ic_corp_icon); |
| 903 | return CircleFramedDrawable.getInstance(context, b); |
| 904 | } |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 905 | if (user.iconPath != null) { |
| 906 | Bitmap icon = um.getUserIcon(user.id); |
| 907 | if (icon != null) { |
| 908 | return CircleFramedDrawable.getInstance(context, icon); |
| 909 | } |
| 910 | } |
Zoltan Szatmary-Ban | ae6ea36 | 2014-12-18 12:10:16 +0000 | [diff] [blame] | 911 | return CircleFramedDrawable.getInstance(context, UserIcons.convertToBitmap( |
| 912 | UserIcons.getDefaultUserIcon(user.id, /* light= */ false))); |
Alexandra Gherghina | 95b86a5 | 2014-07-24 19:13:25 +0100 | [diff] [blame] | 913 | } |
Fabrice Di Meglio | 22a2a49 | 2014-08-08 12:27:57 -0700 | [diff] [blame] | 914 | |
| 915 | /** |
Alexandra Gherghina | 0f5440f | 2014-10-30 11:56:18 +0000 | [diff] [blame] | 916 | * Returns a label for the user, in the form of "User: user name" or "Work profile". |
| 917 | */ |
| 918 | public static String getUserLabel(Context context, UserInfo info) { |
| 919 | if (info.isManagedProfile()) { |
| 920 | // We use predefined values for managed profiles |
| 921 | return context.getString(R.string.managed_user_title); |
| 922 | } |
| 923 | String name = info != null ? info.name : null; |
| 924 | if (name == null && info != null) { |
| 925 | name = Integer.toString(info.id); |
| 926 | } else if (info == null) { |
| 927 | name = context.getString(R.string.unknown); |
| 928 | } |
| 929 | return context.getResources().getString(R.string.running_process_item_user_label, name); |
| 930 | } |
| 931 | |
| 932 | /** |
Fabrice Di Meglio | 22a2a49 | 2014-08-08 12:27:57 -0700 | [diff] [blame] | 933 | * Return whether or not the user should have a SIM Cards option in Settings. |
| 934 | * TODO: Change back to returning true if count is greater than one after testing. |
| 935 | * TODO: See bug 16533525. |
| 936 | */ |
| 937 | public static boolean showSimCardTile(Context context) { |
| 938 | final TelephonyManager tm = |
| 939 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 940 | |
PauloftheWest | dbd0382 | 2014-12-04 11:56:35 -0800 | [diff] [blame] | 941 | return tm.getSimCount() > 1; |
Fabrice Di Meglio | 22a2a49 | 2014-08-08 12:27:57 -0700 | [diff] [blame] | 942 | } |
Dan Sandler | b58b512 | 2014-09-02 18:31:49 +0200 | [diff] [blame] | 943 | |
| 944 | /** |
| 945 | * Determine whether a package is a "system package", in which case certain things (like |
| 946 | * disabling notifications or disabling the package altogether) should be disallowed. |
| 947 | */ |
| 948 | public static boolean isSystemPackage(PackageManager pm, PackageInfo pkg) { |
| 949 | if (sSystemSignature == null) { |
| 950 | sSystemSignature = new Signature[]{ getSystemSignature(pm) }; |
| 951 | } |
| 952 | return sSystemSignature[0] != null && sSystemSignature[0].equals(getFirstSignature(pkg)); |
| 953 | } |
| 954 | |
| 955 | private static Signature[] sSystemSignature; |
| 956 | |
| 957 | private static Signature getFirstSignature(PackageInfo pkg) { |
| 958 | if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) { |
| 959 | return pkg.signatures[0]; |
| 960 | } |
| 961 | return null; |
| 962 | } |
| 963 | |
| 964 | private static Signature getSystemSignature(PackageManager pm) { |
| 965 | try { |
| 966 | final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES); |
| 967 | return getFirstSignature(sys); |
| 968 | } catch (NameNotFoundException e) { |
| 969 | } |
| 970 | return null; |
| 971 | } |
| 972 | |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 973 | /** |
| 974 | * Returns elapsed time for the given millis, in the following format: |
| 975 | * 2d 5h 40m 29s |
| 976 | * @param context the application context |
| 977 | * @param millis the elapsed time in milli seconds |
| 978 | * @param withSeconds include seconds? |
| 979 | * @return the formatted elapsed time |
| 980 | */ |
| 981 | public static String formatElapsedTime(Context context, double millis, boolean withSeconds) { |
| 982 | StringBuilder sb = new StringBuilder(); |
| 983 | int seconds = (int) Math.floor(millis / 1000); |
| 984 | if (!withSeconds) { |
| 985 | // Round up. |
| 986 | seconds += 30; |
| 987 | } |
| 988 | |
| 989 | int days = 0, hours = 0, minutes = 0; |
| 990 | if (seconds >= SECONDS_PER_DAY) { |
| 991 | days = seconds / SECONDS_PER_DAY; |
| 992 | seconds -= days * SECONDS_PER_DAY; |
| 993 | } |
| 994 | if (seconds >= SECONDS_PER_HOUR) { |
| 995 | hours = seconds / SECONDS_PER_HOUR; |
| 996 | seconds -= hours * SECONDS_PER_HOUR; |
| 997 | } |
| 998 | if (seconds >= SECONDS_PER_MINUTE) { |
| 999 | minutes = seconds / SECONDS_PER_MINUTE; |
| 1000 | seconds -= minutes * SECONDS_PER_MINUTE; |
| 1001 | } |
| 1002 | if (withSeconds) { |
| 1003 | if (days > 0) { |
| 1004 | sb.append(context.getString(R.string.battery_history_days, |
| 1005 | days, hours, minutes, seconds)); |
| 1006 | } else if (hours > 0) { |
| 1007 | sb.append(context.getString(R.string.battery_history_hours, |
| 1008 | hours, minutes, seconds)); |
| 1009 | } else if (minutes > 0) { |
| 1010 | sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds)); |
| 1011 | } else { |
| 1012 | sb.append(context.getString(R.string.battery_history_seconds, seconds)); |
| 1013 | } |
| 1014 | } else { |
| 1015 | if (days > 0) { |
| 1016 | sb.append(context.getString(R.string.battery_history_days_no_seconds, |
| 1017 | days, hours, minutes)); |
| 1018 | } else if (hours > 0) { |
| 1019 | sb.append(context.getString(R.string.battery_history_hours_no_seconds, |
| 1020 | hours, minutes)); |
| 1021 | } else { |
| 1022 | sb.append(context.getString(R.string.battery_history_minutes_no_seconds, minutes)); |
| 1023 | } |
| 1024 | } |
| 1025 | return sb.toString(); |
| 1026 | } |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 1027 | |
| 1028 | /** |
| 1029 | * finds a record with subId. |
| 1030 | * Since the number of SIMs are few, an array is fine. |
| 1031 | */ |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1032 | public static SubscriptionInfo findRecordBySubId(Context context, final int subId) { |
Wink Saville | ca75661 | 2014-11-08 10:47:12 -0800 | [diff] [blame] | 1033 | final List<SubscriptionInfo> subInfoList = |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1034 | SubscriptionManager.from(context).getActiveSubscriptionInfoList(); |
| 1035 | if (subInfoList != null) { |
| 1036 | final int subInfoLength = subInfoList.size(); |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 1037 | |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1038 | for (int i = 0; i < subInfoLength; ++i) { |
| 1039 | final SubscriptionInfo sir = subInfoList.get(i); |
| 1040 | if (sir != null && sir.getSubscriptionId() == subId) { |
| 1041 | return sir; |
| 1042 | } |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return null; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * finds a record with slotId. |
| 1051 | * Since the number of SIMs are few, an array is fine. |
| 1052 | */ |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1053 | public static SubscriptionInfo findRecordBySlotId(Context context, final int slotId) { |
Wink Saville | ca75661 | 2014-11-08 10:47:12 -0800 | [diff] [blame] | 1054 | final List<SubscriptionInfo> subInfoList = |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1055 | SubscriptionManager.from(context).getActiveSubscriptionInfoList(); |
| 1056 | if (subInfoList != null) { |
| 1057 | final int subInfoLength = subInfoList.size(); |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 1058 | |
Wink Saville | 0183fb5 | 2014-11-22 10:11:39 -0800 | [diff] [blame] | 1059 | for (int i = 0; i < subInfoLength; ++i) { |
| 1060 | final SubscriptionInfo sir = subInfoList.get(i); |
| 1061 | if (sir.getSimSlotIndex() == slotId) { |
| 1062 | //Right now we take the first subscription on a SIM. |
| 1063 | return sir; |
| 1064 | } |
PauloftheWest | 0a0daca | 2014-11-06 15:02:58 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | return null; |
| 1069 | } |
| 1070 | |
Amith Yamasani | 45f8623 | 2014-11-19 17:12:46 -0800 | [diff] [blame] | 1071 | /** |
| 1072 | * Queries for the UserInfo of a user. Returns null if the user doesn't exist (was removed). |
| 1073 | * @param userManager Instance of UserManager |
| 1074 | * @param checkUser The user to check the existence of. |
| 1075 | * @return UserInfo of the user or null for non-existent user. |
| 1076 | */ |
| 1077 | public static UserInfo getExistingUser(UserManager userManager, UserHandle checkUser) { |
| 1078 | final List<UserInfo> users = userManager.getUsers(true /* excludeDying */); |
| 1079 | final int checkUserId = checkUser.getIdentifier(); |
| 1080 | for (UserInfo user : users) { |
| 1081 | if (user.id == checkUserId) { |
| 1082 | return user; |
| 1083 | } |
| 1084 | } |
| 1085 | return null; |
| 1086 | } |
| 1087 | |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 1088 | public static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup parent) { |
| 1089 | final TypedArray a = inflater.getContext().obtainStyledAttributes(null, |
| 1090 | com.android.internal.R.styleable.Preference, |
| 1091 | com.android.internal.R.attr.preferenceCategoryStyle, 0); |
| 1092 | final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, |
| 1093 | 0); |
| 1094 | a.recycle(); |
| 1095 | return inflater.inflate(resId, parent, false); |
| 1096 | } |
| 1097 | |
Fabrice Di Meglio | dff3faa | 2015-02-27 11:14:11 -0800 | [diff] [blame] | 1098 | /** |
| 1099 | * Return if we are running low on storage space or not. |
| 1100 | * |
| 1101 | * @param context The context |
| 1102 | * @return true if we are running low on storage space |
| 1103 | */ |
| 1104 | public static boolean isLowStorage(Context context) { |
| 1105 | final StorageManager sm = StorageManager.from(context); |
| 1106 | return (sm.getStorageBytesUntilLow(context.getFilesDir()) < 0); |
| 1107 | } |
| 1108 | |
Zoltan Szatmary-Ban | e5814ff | 2014-12-19 16:27:45 +0000 | [diff] [blame] | 1109 | /** |
| 1110 | * Returns a default user icon (as a {@link Bitmap}) for the given user. |
| 1111 | * |
| 1112 | * Note that for guest users, you should pass in {@code UserHandle.USER_NULL}. |
| 1113 | * @param userId the user id or {@code UserHandle.USER_NULL} for a non-user specific icon |
| 1114 | */ |
| 1115 | public static Bitmap getDefaultUserIconAsBitmap(int userId) { |
| 1116 | Bitmap bitmap = null; |
| 1117 | // Try finding the corresponding bitmap in the dark bitmap cache |
| 1118 | bitmap = sDarkDefaultUserBitmapCache.get(userId); |
| 1119 | if (bitmap == null) { |
| 1120 | bitmap = UserIcons.convertToBitmap(UserIcons.getDefaultUserIcon(userId, false)); |
| 1121 | // Save it to cache |
| 1122 | sDarkDefaultUserBitmapCache.put(userId, bitmap); |
| 1123 | } |
| 1124 | return bitmap; |
| 1125 | } |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame^] | 1126 | |
| 1127 | public static boolean hasUsbDefaults(IUsbManager usbManager, String packageName) { |
| 1128 | try { |
| 1129 | if (usbManager != null) { |
| 1130 | return usbManager.hasDefaults(packageName, UserHandle.myUserId()); |
| 1131 | } |
| 1132 | } catch (RemoteException e) { |
| 1133 | Log.e(TAG, "mUsbManager.hasDefaults", e); |
| 1134 | } |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | public static boolean hasPreferredActivities(PackageManager pm, String packageName) { |
| 1139 | // Get list of preferred activities |
| 1140 | List<ComponentName> prefActList = new ArrayList<>(); |
| 1141 | // Intent list cannot be null. so pass empty list |
| 1142 | List<IntentFilter> intentList = new ArrayList<>(); |
| 1143 | pm.getPreferredActivities(intentList, prefActList, packageName); |
| 1144 | Log.d(TAG, "Have " + prefActList.size() + " number of activities in preferred list"); |
| 1145 | return prefActList.size() > 0; |
| 1146 | } |
| 1147 | |
| 1148 | public static CharSequence getLaunchByDeafaultSummary(ApplicationsState.AppEntry appEntry, |
| 1149 | IUsbManager usbManager, PackageManager pm, Context context) { |
| 1150 | String packageName = appEntry.info.packageName; |
| 1151 | boolean hasPreferred = hasPreferredActivities(pm, packageName) |
| 1152 | || hasUsbDefaults(usbManager, packageName); |
| 1153 | int status = pm.getIntentVerificationStatus(packageName, UserHandle.myUserId()); |
| 1154 | boolean hasDomainURLsPreference = |
| 1155 | (status == PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) || |
| 1156 | (status == PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER); |
| 1157 | return context.getString(hasPreferred || hasDomainURLsPreference |
| 1158 | ? R.string.launch_defaults_some |
| 1159 | : R.string.launch_defaults_none); |
| 1160 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1161 | } |