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