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 | |
Daniel Nishi | 422e7c3 | 2017-02-09 16:07:22 -0800 | [diff] [blame] | 19 | import static android.content.Intent.EXTRA_USER; |
| 20 | import static android.content.Intent.EXTRA_USER_ID; |
| 21 | import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH; |
| 22 | import static android.text.format.DateUtils.FORMAT_SHOW_DATE; |
| 23 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 26 | import android.app.AppGlobals; |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 27 | import android.app.Fragment; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 28 | import android.app.IActivityManager; |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 29 | import android.app.KeyguardManager; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManager; |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 31 | import android.content.ActivityNotFoundException; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame] | 32 | import android.content.ComponentName; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 33 | import android.content.ContentResolver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame] | 36 | import android.content.IntentFilter; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 38 | import android.content.pm.IPackageManager; |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 39 | import android.content.pm.IntentFilterVerificationInfo; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
Jason Monk | 7519954 | 2016-05-06 15:09:32 -0400 | [diff] [blame] | 41 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 42 | import android.content.pm.ResolveInfo; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 43 | import android.content.pm.UserInfo; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 44 | import android.content.res.Resources; |
Jason Monk | b5aa73f | 2015-03-31 12:59:33 -0400 | [diff] [blame] | 45 | import android.content.res.TypedArray; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 46 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 47 | import android.graphics.Bitmap; |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 48 | import android.graphics.Canvas; |
| 49 | import android.graphics.drawable.BitmapDrawable; |
| 50 | import android.graphics.drawable.Drawable; |
Phil Weaver | 5a3e02d | 2018-03-08 17:38:46 -0800 | [diff] [blame] | 51 | import android.graphics.drawable.VectorDrawable; |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 52 | import android.hardware.fingerprint.FingerprintManager; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 53 | import android.net.ConnectivityManager; |
| 54 | import android.net.LinkProperties; |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 55 | import android.net.Network; |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 56 | import android.net.wifi.WifiManager; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 57 | import android.os.BatteryManager; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 58 | import android.os.Bundle; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 59 | import android.os.IBinder; |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 60 | import android.os.INetworkManagementService; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 61 | import android.os.RemoteException; |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 63 | import android.os.UserHandle; |
| 64 | import android.os.UserManager; |
Fabrice Di Meglio | dff3faa | 2015-02-27 11:14:11 -0800 | [diff] [blame] | 65 | import android.os.storage.StorageManager; |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 66 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 67 | import android.preference.PreferenceFrameLayout; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 68 | import android.provider.ContactsContract.CommonDataKinds; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 69 | import android.provider.ContactsContract.Contacts; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 70 | import android.provider.ContactsContract.Data; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 71 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 72 | import android.provider.ContactsContract.RawContacts; |
Udam Saini | 679f7ad | 2016-03-25 10:47:00 -0700 | [diff] [blame] | 73 | import android.provider.Settings; |
jackqdyulei | ddba966 | 2017-03-08 19:35:02 -0800 | [diff] [blame] | 74 | import android.support.annotation.StringRes; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 75 | import android.support.v7.preference.Preference; |
| 76 | import android.support.v7.preference.PreferenceGroup; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 77 | import android.telephony.TelephonyManager; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 78 | import android.text.Spannable; |
| 79 | import android.text.SpannableString; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 80 | import android.text.TextUtils; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 81 | import android.text.format.DateUtils; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 82 | import android.text.style.TtsSpan; |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 83 | import android.util.ArraySet; |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 84 | import android.util.IconDrawableFactory; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 85 | import android.util.Log; |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 86 | import android.view.LayoutInflater; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 87 | import android.view.View; |
| 88 | import android.view.ViewGroup; |
PMS22 | 63a0560 | 2017-06-14 07:10:15 +0000 | [diff] [blame] | 89 | import android.widget.EditText; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 90 | import android.widget.ListView; |
| 91 | import android.widget.TabWidget; |
Fan Zhang | cc335d9 | 2016-09-29 14:37:14 -0700 | [diff] [blame] | 92 | |
Ricky Wai | 616342b | 2016-04-13 10:40:22 +0100 | [diff] [blame] | 93 | import com.android.internal.app.UnlaunchableAppActivity; |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 94 | import com.android.internal.util.ArrayUtils; |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 95 | import com.android.internal.widget.LockPatternUtils; |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 96 | import com.android.settings.password.ChooseLockSettingsHelper; |
Doris Ling | dee1a22 | 2017-08-31 16:17:30 -0700 | [diff] [blame] | 97 | import com.android.settings.wrapper.DevicePolicyManagerWrapper; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 98 | |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 99 | import java.net.InetAddress; |
| 100 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 101 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 102 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 103 | |
Jason Monk | 27985e1 | 2016-01-08 14:13:05 -0500 | [diff] [blame] | 104 | public final class Utils extends com.android.settingslib.Utils { |
| 105 | |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 106 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 107 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Set the preference's title to the matching activity's label. |
| 110 | */ |
| 111 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 112 | |
| 113 | /** |
Dianne Hackborn | 68f005f | 2014-06-18 18:29:12 -0700 | [diff] [blame] | 114 | * Color spectrum to use to indicate badness. 0 is completely transparent (no data), |
| 115 | * 1 is most bad (red), the last value is least bad (green). |
| 116 | */ |
| 117 | public static final int[] BADNESS_COLORS = new int[] { |
| 118 | 0x00000000, 0xffc43828, 0xffe54918, 0xfff47b00, |
| 119 | 0xfffabf2c, 0xff679e37, 0xff0a7f42 |
| 120 | }; |
| 121 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 122 | private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; |
| 123 | |
Jason Monk | beb171d | 2015-05-21 15:24:37 -0400 | [diff] [blame] | 124 | public static final String OS_PKG = "os"; |
| 125 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 126 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 127 | * Finds a matching activity for a preference's intent. If a matching |
| 128 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 129 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 130 | * @param context The context. |
| 131 | * @param parentPreferenceGroup The preference group that contains the |
| 132 | * preference whose intent is being resolved. |
| 133 | * @param preferenceKey The key of the preference whose intent is being |
| 134 | * resolved. |
| 135 | * @param flags 0 or one or more of |
| 136 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 137 | * . |
| 138 | * @return Whether an activity was found. If false, the preference was |
| 139 | * removed. |
| 140 | */ |
| 141 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 142 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 144 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 145 | if (preference == null) { |
| 146 | return false; |
| 147 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 148 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 149 | Intent intent = preference.getIntent(); |
| 150 | if (intent != null) { |
| 151 | // Find the activity that is in the system image |
| 152 | PackageManager pm = context.getPackageManager(); |
| 153 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 154 | int listSize = list.size(); |
| 155 | for (int i = 0; i < listSize; i++) { |
| 156 | ResolveInfo resolveInfo = list.get(i); |
| 157 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 158 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 159 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 160 | // Replace the intent with this specific activity |
| 161 | preference.setIntent(new Intent().setClassName( |
| 162 | resolveInfo.activityInfo.packageName, |
| 163 | resolveInfo.activityInfo.name)); |
| 164 | |
| 165 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 166 | // Set the preference title to the activity's label |
| 167 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 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 | return true; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Did not find a matching activity, so remove the preference |
| 176 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 177 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 178 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 179 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 180 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 181 | /** |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 182 | * Returns the UserManager for a given context |
| 183 | * |
| 184 | * @throws IllegalStateException if no UserManager could be retrieved. |
| 185 | */ |
| 186 | public static UserManager getUserManager(Context context) { |
| 187 | UserManager um = UserManager.get(context); |
| 188 | if (um == null) { |
| 189 | throw new IllegalStateException("Unable to load UserManager"); |
| 190 | } |
| 191 | return um; |
| 192 | } |
| 193 | |
| 194 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 195 | * Returns true if Monkey is running. |
| 196 | */ |
| 197 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 198 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 199 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 203 | */ |
| 204 | public static boolean isVoiceCapable(Context context) { |
| 205 | TelephonyManager telephony = |
| 206 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 207 | return telephony != null && telephony.isVoiceCapable(); |
| 208 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 209 | |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 212 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 213 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 214 | */ |
| 215 | public static String getWifiIpAddresses(Context context) { |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 216 | WifiManager wifiManager = context.getSystemService(WifiManager.class); |
| 217 | Network currentNetwork = wifiManager.getCurrentNetwork(); |
| 218 | if (currentNetwork != null) { |
| 219 | ConnectivityManager cm = (ConnectivityManager) |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 220 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 221 | LinkProperties prop = cm.getLinkProperties(currentNetwork); |
| 222 | return formatIpAddresses(prop); |
| 223 | } |
| 224 | return null; |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 227 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 228 | if (prop == null) return null; |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 229 | Iterator<InetAddress> iter = prop.getAllAddresses().iterator(); |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 230 | // If there are no entries, return null |
| 231 | if (!iter.hasNext()) return null; |
| 232 | // Concatenate all available addresses, comma separated |
| 233 | String addresses = ""; |
| 234 | while (iter.hasNext()) { |
| 235 | addresses += iter.next().getHostAddress(); |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 236 | if (iter.hasNext()) addresses += "\n"; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 237 | } |
| 238 | return addresses; |
| 239 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 240 | |
| 241 | public static Locale createLocaleFromString(String localeStr) { |
| 242 | // TODO: is there a better way to actually construct a locale that will match? |
| 243 | // The main problem is, on top of Java specs, locale.toString() and |
| 244 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 245 | // many cases, because the constructor takes the only string as the language |
| 246 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 247 | // And : new Locale("en_US").toString() => "en_us" |
| 248 | if (null == localeStr) |
| 249 | return Locale.getDefault(); |
| 250 | String[] brokenDownLocale = localeStr.split("_", 3); |
| 251 | // split may not return a 0-length array. |
| 252 | if (1 == brokenDownLocale.length) { |
| 253 | return new Locale(brokenDownLocale[0]); |
| 254 | } else if (2 == brokenDownLocale.length) { |
| 255 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 256 | } else { |
| 257 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 258 | } |
| 259 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 260 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 261 | public static boolean isBatteryPresent(Intent batteryChangedIntent) { |
| 262 | return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 263 | } |
| 264 | |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 265 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 266 | return formatPercentage(getBatteryLevel(batteryChangedIntent)); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 269 | /** |
| 270 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 271 | * when outside scrollbars are requested. Usually used to display |
| 272 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 273 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 274 | public static void prepareCustomPreferencesList( |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 275 | ViewGroup parent, View child, View list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 276 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 277 | if (movePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 278 | final Resources res = list.getResources(); |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 279 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 280 | final int paddingBottom = res.getDimensionPixelSize( |
| 281 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 282 | |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 283 | if (parent instanceof PreferenceFrameLayout) { |
| 284 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
| 285 | |
| 286 | final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; |
| 287 | list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); |
| 288 | } else { |
| 289 | list.setPaddingRelative(paddingSide, 0, paddingSide, paddingBottom); |
| 290 | } |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 293 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 294 | public static void forceCustomPadding(View view, boolean additive) { |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 295 | final Resources res = view.getResources(); |
| 296 | final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 297 | |
| 298 | final int paddingStart = paddingSide + (additive ? view.getPaddingStart() : 0); |
| 299 | final int paddingEnd = paddingSide + (additive ? view.getPaddingEnd() : 0); |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 300 | final int paddingBottom = res.getDimensionPixelSize( |
| 301 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
| 302 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 303 | view.setPaddingRelative(paddingStart, 0, paddingEnd, paddingBottom); |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 306 | public static String getMeProfileName(Context context, boolean full) { |
| 307 | if (full) { |
| 308 | return getProfileDisplayName(context); |
| 309 | } else { |
| 310 | return getShorterNameIfPossible(context); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 311 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | private static String getShorterNameIfPossible(Context context) { |
| 315 | final String given = getLocalProfileGivenName(context); |
| 316 | return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); |
| 317 | } |
| 318 | |
| 319 | private static String getLocalProfileGivenName(Context context) { |
| 320 | final ContentResolver cr = context.getContentResolver(); |
| 321 | |
| 322 | // Find the raw contact ID for the local ME profile raw contact. |
| 323 | final long localRowProfileId; |
| 324 | final Cursor localRawProfile = cr.query( |
| 325 | Profile.CONTENT_RAW_CONTACTS_URI, |
| 326 | new String[] {RawContacts._ID}, |
| 327 | RawContacts.ACCOUNT_TYPE + " IS NULL AND " + |
| 328 | RawContacts.ACCOUNT_NAME + " IS NULL", |
| 329 | null, null); |
| 330 | if (localRawProfile == null) return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 331 | |
| 332 | try { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 333 | if (!localRawProfile.moveToFirst()) { |
| 334 | return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 335 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 336 | localRowProfileId = localRawProfile.getLong(0); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 337 | } finally { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 338 | localRawProfile.close(); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 339 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 340 | |
| 341 | // Find the structured name for the raw contact. |
| 342 | final Cursor structuredName = cr.query( |
| 343 | Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), |
| 344 | new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, |
| 345 | CommonDataKinds.StructuredName.FAMILY_NAME}, |
| 346 | Data.RAW_CONTACT_ID + "=" + localRowProfileId, |
| 347 | null, null); |
| 348 | if (structuredName == null) return null; |
| 349 | |
| 350 | try { |
| 351 | if (!structuredName.moveToFirst()) { |
| 352 | return null; |
| 353 | } |
| 354 | String partialName = structuredName.getString(0); |
| 355 | if (TextUtils.isEmpty(partialName)) { |
| 356 | partialName = structuredName.getString(1); |
| 357 | } |
| 358 | return partialName; |
| 359 | } finally { |
| 360 | structuredName.close(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | private static final String getProfileDisplayName(Context context) { |
| 365 | final ContentResolver cr = context.getContentResolver(); |
| 366 | final Cursor profile = cr.query(Profile.CONTENT_URI, |
| 367 | new String[] {Profile.DISPLAY_NAME}, null, null, null); |
| 368 | if (profile == null) return null; |
| 369 | |
| 370 | try { |
| 371 | if (!profile.moveToFirst()) { |
| 372 | return null; |
| 373 | } |
| 374 | return profile.getString(0); |
| 375 | } finally { |
| 376 | profile.close(); |
| 377 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 378 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 379 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 380 | public static boolean hasMultipleUsers(Context context) { |
| 381 | return ((UserManager) context.getSystemService(Context.USER_SERVICE)) |
| 382 | .getUsers().size() > 1; |
| 383 | } |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 384 | |
| 385 | /** |
Esteban Talavera | 9b86e9c | 2017-05-30 16:48:08 +0100 | [diff] [blame] | 386 | * Returns the managed profile of the current user or {@code null} if none is found or a profile |
| 387 | * exists but it is disabled. |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 388 | */ |
| 389 | public static UserHandle getManagedProfile(UserManager userManager) { |
| 390 | List<UserHandle> userProfiles = userManager.getUserProfiles(); |
| 391 | final int count = userProfiles.size(); |
| 392 | for (int i = 0; i < count; i++) { |
| 393 | final UserHandle profile = userProfiles.get(i); |
| 394 | if (profile.getIdentifier() == userManager.getUserHandle()) { |
| 395 | continue; |
| 396 | } |
| 397 | final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier()); |
| 398 | if (userInfo.isManagedProfile()) { |
| 399 | return profile; |
| 400 | } |
| 401 | } |
| 402 | return null; |
| 403 | } |
| 404 | |
| 405 | /** |
Esteban Talavera | 9b86e9c | 2017-05-30 16:48:08 +0100 | [diff] [blame] | 406 | * Returns the managed profile of the current user or {@code null} if none is found. Unlike |
| 407 | * {@link #getManagedProfile} this method returns enabled and disabled managed profiles. |
| 408 | */ |
| 409 | public static UserHandle getManagedProfileWithDisabled(UserManager userManager) { |
| 410 | // TODO: Call getManagedProfileId from here once Robolectric supports |
| 411 | // API level 24 and UserManager.getProfileIdsWithDisabled can be Mocked (to avoid having |
| 412 | // yet another implementation that loops over user profiles in this method). In the meantime |
| 413 | // we need to use UserManager.getProfiles that is available on API 23 (the one currently |
| 414 | // used for Settings Robolectric tests). |
| 415 | final int myUserId = UserHandle.myUserId(); |
| 416 | List<UserInfo> profiles = userManager.getProfiles(myUserId); |
| 417 | final int count = profiles.size(); |
| 418 | for (int i = 0; i < count; i++) { |
| 419 | final UserInfo profile = profiles.get(i); |
| 420 | if (profile.isManagedProfile() |
| 421 | && profile.getUserHandle().getIdentifier() != myUserId) { |
| 422 | return profile.getUserHandle(); |
| 423 | } |
| 424 | } |
| 425 | return null; |
| 426 | } |
| 427 | |
| 428 | /** |
Clara Bayarri | 462cce1 | 2016-02-18 12:09:21 +0000 | [diff] [blame] | 429 | * Retrieves the id for the given user's managed profile. |
| 430 | * |
| 431 | * @return the managed profile id or UserHandle.USER_NULL if there is none. |
| 432 | */ |
| 433 | public static int getManagedProfileId(UserManager um, int parentUserId) { |
Fyodor Kupolov | 4a4af5a | 2016-04-07 16:46:18 -0700 | [diff] [blame] | 434 | int[] profileIds = um.getProfileIdsWithDisabled(parentUserId); |
| 435 | for (int profileId : profileIds) { |
| 436 | if (profileId != parentUserId) { |
| 437 | return profileId; |
Clara Bayarri | 462cce1 | 2016-02-18 12:09:21 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | return UserHandle.USER_NULL; |
| 441 | } |
| 442 | |
| 443 | /** |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 444 | * Returns the target user for a Settings activity. |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 445 | * <p> |
| 446 | * User would be retrieved in this order: |
| 447 | * <ul> |
| 448 | * <li> If this activity is launched from other user, return that user id. |
| 449 | * <li> If this is launched from the Settings app in same user, return the user contained as an |
| 450 | * extra in the arguments or intent extras. |
| 451 | * <li> Otherwise, return UserHandle.myUserId(). |
| 452 | * </ul> |
| 453 | * <p> |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 454 | * Note: This is secure in the sense that it only returns a target user different to the current |
| 455 | * one if the app launching this activity is the Settings app itself, running in the same user |
| 456 | * 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] | 457 | */ |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 458 | public static UserHandle getSecureTargetUser(IBinder activityToken, |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 459 | UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) { |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 460 | UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 461 | IActivityManager am = ActivityManager.getService(); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 462 | try { |
| 463 | String launchedFromPackage = am.getLaunchedFromPackage(activityToken); |
| 464 | boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage); |
| 465 | |
| 466 | UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
| 467 | am.getLaunchedFromUid(activityToken))); |
| 468 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 469 | // Check it's secure |
| 470 | if (isProfileOf(um, launchedFromUser)) { |
| 471 | return launchedFromUser; |
| 472 | } |
| 473 | } |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 474 | UserHandle extrasUser = getUserHandleFromBundle(intentExtras); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 475 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 476 | // Check it's secure |
| 477 | if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) { |
| 478 | return extrasUser; |
| 479 | } |
| 480 | } |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 481 | UserHandle argumentsUser = getUserHandleFromBundle(arguments); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 482 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 483 | // Check it's secure |
| 484 | if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) { |
| 485 | return argumentsUser; |
| 486 | } |
| 487 | } |
| 488 | } catch (RemoteException e) { |
| 489 | // Should not happen |
| 490 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 491 | } |
| 492 | return currentUser; |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Lookup both {@link Intent#EXTRA_USER} and {@link Intent#EXTRA_USER_ID} in the bundle |
| 497 | * and return the {@link UserHandle} object. Return {@code null} if nothing is found. |
| 498 | */ |
| 499 | private static @Nullable UserHandle getUserHandleFromBundle(Bundle bundle) { |
| 500 | if (bundle == null) { |
| 501 | return null; |
| 502 | } |
| 503 | final UserHandle user = bundle.getParcelable(EXTRA_USER); |
| 504 | if (user != null) { |
| 505 | return user; |
| 506 | } |
| 507 | final int userId = bundle.getInt(EXTRA_USER_ID, -1); |
| 508 | if (userId != -1) { |
| 509 | return UserHandle.of(userId); |
| 510 | } |
| 511 | return null; |
| 512 | } |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 513 | |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 514 | /** |
| 515 | * Returns true if the user provided is in the same profiles group as the current user. |
| 516 | */ |
| 517 | private static boolean isProfileOf(UserManager um, UserHandle otherUser) { |
| 518 | if (um == null || otherUser == null) return false; |
| 519 | return (UserHandle.myUserId() == otherUser.getIdentifier()) |
| 520 | || um.getUserProfiles().contains(otherUser); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 521 | } |
Amith Yamasani | 51c6dac | 2014-07-02 00:06:37 +0530 | [diff] [blame] | 522 | |
Alexandra Gherghina | 95b86a5 | 2014-07-24 19:13:25 +0100 | [diff] [blame] | 523 | /** |
Fabrice Di Meglio | 22a2a49 | 2014-08-08 12:27:57 -0700 | [diff] [blame] | 524 | * Return whether or not the user should have a SIM Cards option in Settings. |
| 525 | * TODO: Change back to returning true if count is greater than one after testing. |
| 526 | * TODO: See bug 16533525. |
| 527 | */ |
| 528 | public static boolean showSimCardTile(Context context) { |
| 529 | final TelephonyManager tm = |
| 530 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 531 | |
PauloftheWest | dbd0382 | 2014-12-04 11:56:35 -0800 | [diff] [blame] | 532 | return tm.getSimCount() > 1; |
Fabrice Di Meglio | 22a2a49 | 2014-08-08 12:27:57 -0700 | [diff] [blame] | 533 | } |
Dan Sandler | b58b512 | 2014-09-02 18:31:49 +0200 | [diff] [blame] | 534 | |
| 535 | /** |
Amith Yamasani | 45f8623 | 2014-11-19 17:12:46 -0800 | [diff] [blame] | 536 | * Queries for the UserInfo of a user. Returns null if the user doesn't exist (was removed). |
| 537 | * @param userManager Instance of UserManager |
| 538 | * @param checkUser The user to check the existence of. |
| 539 | * @return UserInfo of the user or null for non-existent user. |
| 540 | */ |
| 541 | public static UserInfo getExistingUser(UserManager userManager, UserHandle checkUser) { |
| 542 | final List<UserInfo> users = userManager.getUsers(true /* excludeDying */); |
| 543 | final int checkUserId = checkUser.getIdentifier(); |
| 544 | for (UserInfo user : users) { |
| 545 | if (user.id == checkUserId) { |
| 546 | return user; |
| 547 | } |
| 548 | } |
| 549 | return null; |
| 550 | } |
| 551 | |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 552 | public static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup parent) { |
| 553 | final TypedArray a = inflater.getContext().obtainStyledAttributes(null, |
| 554 | com.android.internal.R.styleable.Preference, |
| 555 | com.android.internal.R.attr.preferenceCategoryStyle, 0); |
| 556 | final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, |
| 557 | 0); |
| 558 | a.recycle(); |
| 559 | return inflater.inflate(resId, parent, false); |
| 560 | } |
| 561 | |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 562 | public static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) { |
| 563 | List<IntentFilterVerificationInfo> iviList = pm.getIntentFilterVerifications(packageName); |
| 564 | List<IntentFilter> filters = pm.getAllIntentFilters(packageName); |
| 565 | |
| 566 | ArraySet<String> result = new ArraySet<>(); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 567 | if (iviList != null && iviList.size() > 0) { |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 568 | for (IntentFilterVerificationInfo ivi : iviList) { |
| 569 | for (String host : ivi.getDomains()) { |
| 570 | result.add(host); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | if (filters != null && filters.size() > 0) { |
| 575 | for (IntentFilter filter : filters) { |
Christopher Tate | ddaa142 | 2015-07-16 16:00:49 -0700 | [diff] [blame] | 576 | if (filter.hasCategory(Intent.CATEGORY_BROWSABLE) |
| 577 | && (filter.hasDataScheme(IntentFilter.SCHEME_HTTP) || |
| 578 | filter.hasDataScheme(IntentFilter.SCHEME_HTTPS))) { |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 579 | result.addAll(filter.getHostsList()); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | return result; |
| 584 | } |
| 585 | |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 586 | /** |
| 587 | * Returns the application info of the currently installed MDM package. |
| 588 | */ |
| 589 | public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) { |
| 590 | DevicePolicyManager dpm = |
| 591 | (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 592 | ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId); |
| 593 | if (mdmPackage == null) { |
| 594 | return null; |
| 595 | } |
| 596 | String mdmPackageName = mdmPackage.getPackageName(); |
| 597 | try { |
| 598 | IPackageManager ipm = AppGlobals.getPackageManager(); |
| 599 | ApplicationInfo mdmApplicationInfo = |
| 600 | ipm.getApplicationInfo(mdmPackageName, 0, profileId); |
| 601 | return mdmApplicationInfo; |
| 602 | } catch (RemoteException e) { |
| 603 | Log.e(TAG, "Error while retrieving application info for package " + mdmPackageName |
| 604 | + ", userId " + profileId, e); |
| 605 | return null; |
| 606 | } |
| 607 | } |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 608 | |
| 609 | public static boolean isBandwidthControlEnabled() { |
| 610 | final INetworkManagementService netManager = INetworkManagementService.Stub |
| 611 | .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
| 612 | try { |
| 613 | return netManager.isBandwidthControlEnabled(); |
| 614 | } catch (RemoteException e) { |
| 615 | return false; |
| 616 | } |
| 617 | } |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 618 | |
| 619 | /** |
| 620 | * Returns an accessible SpannableString. |
| 621 | * @param displayText the text to display |
| 622 | * @param accessibileText the text text-to-speech engines should read |
| 623 | */ |
| 624 | public static SpannableString createAccessibleSequence(CharSequence displayText, |
| 625 | String accessibileText) { |
| 626 | SpannableString str = new SpannableString(displayText); |
| 627 | str.setSpan(new TtsSpan.TextBuilder(accessibileText).build(), 0, |
| 628 | displayText.length(), |
| 629 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 630 | return str; |
| 631 | } |
Andres Morales | 7bdffd8 | 2015-08-04 16:55:00 -0700 | [diff] [blame] | 632 | |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 633 | /** |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 634 | * Returns the user id present in the bundle with |
| 635 | * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 636 | * |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 637 | * @throws SecurityException if the given userId does not belong to the |
| 638 | * current user group. |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 639 | */ |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 640 | public static int getUserIdFromBundle(Context context, Bundle bundle) { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 641 | return getUserIdFromBundle(context, bundle, false); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Returns the user id present in the bundle with |
| 646 | * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. |
| 647 | * |
| 648 | * @param isInternal indicating if the caller is "internal" to the system, |
| 649 | * meaning we're willing to trust extras like |
| 650 | * {@link ChooseLockSettingsHelper#EXTRA_ALLOW_ANY_USER}. |
| 651 | * @throws SecurityException if the given userId does not belong to the |
| 652 | * current user group. |
| 653 | */ |
| 654 | public static int getUserIdFromBundle(Context context, Bundle bundle, boolean isInternal) { |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 655 | if (bundle == null) { |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 656 | return getCredentialOwnerUserId(context); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 657 | } |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 658 | final boolean allowAnyUser = isInternal |
| 659 | && bundle.getBoolean(ChooseLockSettingsHelper.EXTRA_ALLOW_ANY_USER, false); |
Clara Bayarri | 6934a04 | 2015-10-14 11:07:35 +0100 | [diff] [blame] | 660 | int userId = bundle.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId()); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 661 | if (userId == LockPatternUtils.USER_FRP) { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 662 | return allowAnyUser ? userId : enforceSystemUser(context, userId); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 663 | } else { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 664 | return allowAnyUser ? userId : enforceSameOwner(context, userId); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Returns the given user id if the current user is the system user. |
| 670 | * |
| 671 | * @throws SecurityException if the current user is not the system user. |
| 672 | */ |
| 673 | public static int enforceSystemUser(Context context, int userId) { |
| 674 | if (UserHandle.myUserId() == UserHandle.USER_SYSTEM) { |
| 675 | return userId; |
| 676 | } |
| 677 | throw new SecurityException("Given user id " + userId + " must only be used from " |
| 678 | + "USER_SYSTEM, but current user is " + UserHandle.myUserId()); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Returns the given user id if it belongs to the current user. |
| 683 | * |
| 684 | * @throws SecurityException if the given userId does not belong to the current user group. |
| 685 | */ |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 686 | public static int enforceSameOwner(Context context, int userId) { |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 687 | final UserManager um = getUserManager(context); |
| 688 | final int[] profileIds = um.getProfileIdsWithDisabled(UserHandle.myUserId()); |
| 689 | if (ArrayUtils.contains(profileIds, userId)) { |
| 690 | return userId; |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 691 | } |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 692 | throw new SecurityException("Given user id " + userId + " does not belong to user " |
| 693 | + UserHandle.myUserId()); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 696 | /** |
| 697 | * Returns the effective credential owner of the calling user. |
| 698 | */ |
| 699 | public static int getCredentialOwnerUserId(Context context) { |
| 700 | return getCredentialOwnerUserId(context, UserHandle.myUserId()); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Returns the user id of the credential owner of the given user id. |
| 705 | */ |
| 706 | public static int getCredentialOwnerUserId(Context context, int userId) { |
| 707 | UserManager um = getUserManager(context); |
| 708 | return um.getCredentialOwnerProfile(userId); |
Andres Morales | 7bdffd8 | 2015-08-04 16:55:00 -0700 | [diff] [blame] | 709 | } |
Jason Monk | db4ed19 | 2015-12-11 16:48:31 -0500 | [diff] [blame] | 710 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 711 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 712 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 713 | sBuilder, Locale.getDefault()); |
| 714 | |
| 715 | public static String formatDateRange(Context context, long start, long end) { |
| 716 | final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; |
| 717 | |
| 718 | synchronized (sBuilder) { |
| 719 | sBuilder.setLength(0); |
| 720 | return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null) |
| 721 | .toString(); |
| 722 | } |
| 723 | } |
Jason Monk | 91e2f89 | 2016-02-23 15:31:09 -0500 | [diff] [blame] | 724 | |
Udam Saini | 679f7ad | 2016-03-25 10:47:00 -0700 | [diff] [blame] | 725 | public static boolean isDeviceProvisioned(Context context) { |
| 726 | return Settings.Global.getInt(context.getContentResolver(), |
| 727 | Settings.Global.DEVICE_PROVISIONED, 0) != 0; |
| 728 | } |
Ricky Wai | 616342b | 2016-04-13 10:40:22 +0100 | [diff] [blame] | 729 | |
Bernard Chau | 88d523b | 2016-04-14 15:08:28 +0100 | [diff] [blame] | 730 | public static boolean startQuietModeDialogIfNecessary(Context context, UserManager um, |
Ricky Wai | 616342b | 2016-04-13 10:40:22 +0100 | [diff] [blame] | 731 | int userId) { |
| 732 | if (um.isQuietModeEnabled(UserHandle.of(userId))) { |
| 733 | final Intent intent = UnlaunchableAppActivity.createInQuietModeDialogIntent(userId); |
| 734 | context.startActivity(intent); |
| 735 | return true; |
| 736 | } |
| 737 | return false; |
| 738 | } |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 739 | |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 740 | public static boolean unlockWorkProfileIfNecessary(Context context, int userId) { |
| 741 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 742 | if (!ActivityManager.getService().isUserRunning(userId, |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 743 | ActivityManager.FLAG_AND_LOCKED)) { |
| 744 | return false; |
| 745 | } |
| 746 | } catch (RemoteException e) { |
| 747 | return false; |
| 748 | } |
| 749 | if (!(new LockPatternUtils(context)).isSecure(userId)) { |
| 750 | return false; |
| 751 | } |
Robin Lee | cccf324 | 2017-02-10 15:32:49 +0000 | [diff] [blame] | 752 | return confirmWorkProfileCredentials(context, userId); |
| 753 | } |
| 754 | |
Robin Lee | cccf324 | 2017-02-10 15:32:49 +0000 | [diff] [blame] | 755 | private static boolean confirmWorkProfileCredentials(Context context, int userId) { |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 756 | final KeyguardManager km = (KeyguardManager) context.getSystemService( |
| 757 | Context.KEYGUARD_SERVICE); |
| 758 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, userId); |
| 759 | if (unlockIntent != null) { |
| 760 | context.startActivity(unlockIntent); |
| 761 | return true; |
| 762 | } else { |
| 763 | return false; |
| 764 | } |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 767 | public static CharSequence getApplicationLabel(Context context, String packageName) { |
| 768 | try { |
| 769 | final ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo( |
| 770 | packageName, |
| 771 | PackageManager.MATCH_DISABLED_COMPONENTS |
Amith Yamasani | 21556cd | 2016-11-01 15:49:21 -0700 | [diff] [blame] | 772 | | PackageManager.MATCH_ANY_USER); |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 773 | return appInfo.loadLabel(context.getPackageManager()); |
| 774 | } catch (PackageManager.NameNotFoundException e) { |
| 775 | Log.w(TAG, "Unable to find info for package: " + packageName); |
| 776 | } |
| 777 | return null; |
| 778 | } |
Jason Monk | 7519954 | 2016-05-06 15:09:32 -0400 | [diff] [blame] | 779 | |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 780 | public static boolean isPackageDirectBootAware(Context context, String packageName) { |
| 781 | try { |
| 782 | final ApplicationInfo ai = context.getPackageManager().getApplicationInfo( |
| 783 | packageName, 0); |
| 784 | return ai.isDirectBootAware() || ai.isPartiallyDirectBootAware(); |
| 785 | } catch (NameNotFoundException ignored) { |
| 786 | } |
| 787 | return false; |
| 788 | } |
Andre Lago | 3e398c8 | 2016-07-25 14:12:28 +0100 | [diff] [blame] | 789 | |
| 790 | /** |
| 791 | * Returns a context created from the given context for the given user, or null if it fails |
| 792 | */ |
| 793 | public static Context createPackageContextAsUser(Context context, int userId) { |
| 794 | try { |
| 795 | return context.createPackageContextAsUser( |
| 796 | context.getPackageName(), 0 /* flags */, UserHandle.of(userId)); |
| 797 | } catch (PackageManager.NameNotFoundException e) { |
| 798 | Log.e(TAG, "Failed to create user context", e); |
| 799 | } |
| 800 | return null; |
| 801 | } |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 802 | |
| 803 | public static FingerprintManager getFingerprintManagerOrNull(Context context) { |
| 804 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
Fan Zhang | cf97f0b | 2018-01-05 11:18:44 -0800 | [diff] [blame] | 805 | return (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE); |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 806 | } else { |
| 807 | return null; |
| 808 | } |
| 809 | } |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 810 | |
Kevin Chyn | 82792cc | 2017-07-20 00:15:44 -0700 | [diff] [blame] | 811 | public static boolean hasFingerprintHardware(Context context) { |
| 812 | FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context); |
| 813 | return fingerprintManager != null && fingerprintManager.isHardwareDetected(); |
| 814 | } |
| 815 | |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 816 | /** |
| 817 | * Launches an intent which may optionally have a user id defined. |
| 818 | * @param fragment Fragment to use to launch the activity. |
| 819 | * @param intent Intent to launch. |
| 820 | */ |
| 821 | public static void launchIntent(Fragment fragment, Intent intent) { |
| 822 | try { |
| 823 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, -1); |
| 824 | |
| 825 | if (userId == -1) { |
| 826 | fragment.startActivity(intent); |
| 827 | } else { |
| 828 | fragment.getActivity().startActivityAsUser(intent, new UserHandle(userId)); |
| 829 | } |
| 830 | } catch (ActivityNotFoundException e) { |
| 831 | Log.w(TAG, "No activity found for " + intent); |
| 832 | } |
| 833 | } |
| 834 | |
Christine Franks | c6890ab | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 835 | public static boolean isDemoUser(Context context) { |
| 836 | return UserManager.isDeviceInDemoMode(context) && getUserManager(context).isDemoUser(); |
| 837 | } |
| 838 | |
Justin Klaassen | 3025727 | 2017-08-08 21:58:05 -0700 | [diff] [blame] | 839 | public static ComponentName getDeviceOwnerComponent(Context context) { |
| 840 | final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( |
| 841 | Context.DEVICE_POLICY_SERVICE); |
| 842 | return dpm.getDeviceOwnerComponentOnAnyUser(); |
Christine Franks | 1478222 | 2017-01-23 16:44:02 -0800 | [diff] [blame] | 843 | } |
Daniel Nishi | dfed8a2 | 2017-01-19 16:32:41 -0800 | [diff] [blame] | 844 | |
| 845 | /** |
| 846 | * Returns if a given user is a profile of another user. |
Christine Franks | a0dd987 | 2017-07-11 17:32:57 -0700 | [diff] [blame] | 847 | * @param user The user whose profiles wibe checked. |
Daniel Nishi | dfed8a2 | 2017-01-19 16:32:41 -0800 | [diff] [blame] | 848 | * @param profile The (potential) profile. |
| 849 | * @return if the profile is actually a profile |
| 850 | */ |
| 851 | public static boolean isProfileOf(UserInfo user, UserInfo profile) { |
| 852 | return user.id == profile.id || |
| 853 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 854 | && user.profileGroupId == profile.profileGroupId); |
| 855 | } |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * Tries to initalize a volume with the given bundle. If it is a valid, private, and readable |
| 859 | * {@link VolumeInfo}, it is returned. If it is not valid, null is returned. |
| 860 | */ |
| 861 | @Nullable |
| 862 | public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) { |
| 863 | final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID, |
| 864 | VolumeInfo.ID_PRIVATE_INTERNAL); |
| 865 | VolumeInfo volume = sm.findVolumeById(volumeId); |
| 866 | return isVolumeValid(volume) ? volume : null; |
| 867 | } |
| 868 | |
jackqdyulei | ddba966 | 2017-03-08 19:35:02 -0800 | [diff] [blame] | 869 | /** |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 870 | * Return {@code true} if the supplied package is device owner or profile owner of at |
| 871 | * least one user. |
| 872 | * @param userManager used to get profile owner app for each user |
| 873 | * @param devicePolicyManager used to check whether it is device owner app |
| 874 | * @param packageName package to check about |
| 875 | */ |
| 876 | public static boolean isProfileOrDeviceOwner(UserManager userManager, |
| 877 | DevicePolicyManagerWrapper devicePolicyManager, String packageName) { |
| 878 | List<UserInfo> userInfos = userManager.getUsers(); |
| 879 | if (devicePolicyManager.isDeviceOwnerAppOnAnyUser(packageName)) { |
| 880 | return true; |
| 881 | } |
| 882 | for (int i = 0, size = userInfos.size(); i < size; i++) { |
| 883 | ComponentName cn = devicePolicyManager.getProfileOwnerAsUser(userInfos.get(i).id); |
| 884 | if (cn != null && cn.getPackageName().equals(packageName)) { |
| 885 | return true; |
| 886 | } |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | /** |
jackqdyulei | ddba966 | 2017-03-08 19:35:02 -0800 | [diff] [blame] | 892 | * Return the resource id to represent the install status for an app |
| 893 | */ |
| 894 | @StringRes |
| 895 | public static int getInstallationStatus(ApplicationInfo info) { |
| 896 | if ((info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 897 | return R.string.not_installed; |
| 898 | } |
| 899 | return info.enabled ? R.string.installed : R.string.disabled; |
| 900 | } |
| 901 | |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 902 | private static boolean isVolumeValid(VolumeInfo volume) { |
| 903 | return (volume != null) && (volume.getType() == VolumeInfo.TYPE_PRIVATE) |
| 904 | && volume.isMountedReadable(); |
| 905 | } |
jackqdyulei | f4c1cef | 2017-03-31 12:57:10 -0700 | [diff] [blame] | 906 | |
PMS22 | 63a0560 | 2017-06-14 07:10:15 +0000 | [diff] [blame] | 907 | public static void setEditTextCursorPosition(EditText editText) { |
| 908 | editText.setSelection(editText.getText().length()); |
| 909 | } |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 910 | |
| 911 | /** |
| 912 | * Sets the preference icon with a drawable that is scaled down to to avoid crashing Settings if |
| 913 | * it's too big. |
| 914 | */ |
| 915 | public static void setSafeIcon(Preference pref, Drawable icon) { |
| 916 | Drawable safeIcon = icon; |
Phil Weaver | 5a3e02d | 2018-03-08 17:38:46 -0800 | [diff] [blame] | 917 | if ((icon != null) && !(icon instanceof VectorDrawable)) { |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 918 | safeIcon = getSafeDrawable(icon, 500, 500); |
| 919 | } |
| 920 | pref.setIcon(safeIcon); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Gets a drawable with a limited size to avoid crashing Settings if it's too big. |
| 925 | * |
| 926 | * @param original original drawable, typically an app icon. |
| 927 | * @param maxWidth maximum width, in pixels. |
| 928 | * @param maxHeight maximum height, in pixels. |
| 929 | */ |
| 930 | public static Drawable getSafeDrawable(Drawable original, int maxWidth, int maxHeight) { |
| 931 | final int actualWidth = original.getMinimumWidth(); |
| 932 | final int actualHeight = original.getMinimumHeight(); |
| 933 | |
| 934 | if (actualWidth <= maxWidth && actualHeight <= maxHeight) { |
| 935 | return original; |
| 936 | } |
| 937 | |
| 938 | float scaleWidth = ((float) maxWidth) / actualWidth; |
| 939 | float scaleHeight = ((float) maxHeight) / actualHeight; |
| 940 | float scale = Math.min(scaleWidth, scaleHeight); |
| 941 | final int width = (int) (actualWidth * scale); |
| 942 | final int height = (int) (actualHeight * scale); |
| 943 | |
| 944 | final Bitmap bitmap; |
| 945 | if (original instanceof BitmapDrawable) { |
| 946 | bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) original).getBitmap(), width, |
| 947 | height, false); |
| 948 | } else { |
| 949 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 950 | final Canvas canvas = new Canvas(bitmap); |
| 951 | original.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); |
| 952 | original.draw(canvas); |
| 953 | } |
| 954 | return new BitmapDrawable(null, bitmap); |
| 955 | } |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 956 | |
| 957 | /** |
| 958 | * Get the {@link Drawable} that represents the app icon |
| 959 | */ |
| 960 | public static Drawable getBadgedIcon(IconDrawableFactory iconDrawableFactory, |
| 961 | PackageManager packageManager, String packageName, int userId) { |
| 962 | try { |
Tony Mak | 4e29281 | 2018-03-19 15:37:21 +0000 | [diff] [blame] | 963 | final ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser( |
| 964 | packageName, PackageManager.GET_META_DATA, userId); |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 965 | return iconDrawableFactory.getBadgedIcon(appInfo, userId); |
| 966 | } catch (PackageManager.NameNotFoundException e) { |
| 967 | return packageManager.getDefaultActivityIcon(); |
| 968 | } |
| 969 | } |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 970 | } |