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; |
tmfang | aed8f36 | 2019-03-18 16:56:09 +0800 | [diff] [blame] | 25 | import android.app.ActionBar; |
| 26 | import android.app.Activity; |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 27 | import android.app.ActivityManager; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 28 | import android.app.AppGlobals; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 29 | import android.app.IActivityManager; |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 30 | import android.app.KeyguardManager; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 31 | import android.app.admin.DevicePolicyManager; |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 32 | import android.content.ActivityNotFoundException; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame] | 33 | import android.content.ComponentName; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 34 | import android.content.ContentResolver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
Fabrice Di Meglio | 8b2ea39 | 2015-01-23 19:03:22 -0800 | [diff] [blame] | 37 | import android.content.IntentFilter; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 38 | import android.content.pm.ApplicationInfo; |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 39 | import android.content.pm.IPackageManager; |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 40 | import android.content.pm.IntentFilterVerificationInfo; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
Jason Monk | 7519954 | 2016-05-06 15:09:32 -0400 | [diff] [blame] | 42 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 43 | import android.content.pm.ResolveInfo; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 44 | import android.content.pm.UserInfo; |
Jason Chiu | 8ac1e4d | 2020-05-11 16:13:47 +0800 | [diff] [blame] | 45 | import android.content.res.Configuration; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 46 | import android.content.res.Resources; |
Jason Monk | b5aa73f | 2015-03-31 12:59:33 -0400 | [diff] [blame] | 47 | import android.content.res.TypedArray; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 48 | import android.database.Cursor; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 49 | import android.graphics.Bitmap; |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 50 | import android.graphics.Canvas; |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 51 | import android.graphics.drawable.AdaptiveIconDrawable; |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 52 | import android.graphics.drawable.BitmapDrawable; |
| 53 | import android.graphics.drawable.Drawable; |
Phil Weaver | 5a3e02d | 2018-03-08 17:38:46 -0800 | [diff] [blame] | 54 | import android.graphics.drawable.VectorDrawable; |
Kevin Chyn | 4882e87 | 2018-06-25 17:58:31 -0700 | [diff] [blame] | 55 | import android.hardware.face.FaceManager; |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 56 | import android.hardware.fingerprint.FingerprintManager; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 57 | import android.net.ConnectivityManager; |
Chiachang Wang | 003e27d | 2021-03-18 15:01:03 +0800 | [diff] [blame] | 58 | import android.net.LinkAddress; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 59 | import android.net.LinkProperties; |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 60 | import android.net.Network; |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 61 | import android.net.wifi.WifiManager; |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 62 | import android.os.BatteryManager; |
Yanting Yang | 96127fe | 2020-03-20 12:04:09 +0800 | [diff] [blame] | 63 | import android.os.Binder; |
Ng Zhi An | b97bdc3 | 2019-02-01 12:39:31 -0800 | [diff] [blame] | 64 | import android.os.Build; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 65 | import android.os.Bundle; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 66 | import android.os.IBinder; |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 67 | import android.os.INetworkManagementService; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 68 | import android.os.RemoteException; |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 69 | import android.os.ServiceManager; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 70 | import android.os.UserHandle; |
| 71 | import android.os.UserManager; |
Fabrice Di Meglio | dff3faa | 2015-02-27 11:14:11 -0800 | [diff] [blame] | 72 | import android.os.storage.StorageManager; |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 73 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 74 | import android.preference.PreferenceFrameLayout; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 75 | import android.provider.ContactsContract.CommonDataKinds; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 76 | import android.provider.ContactsContract.Contacts; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 77 | import android.provider.ContactsContract.Data; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 78 | import android.provider.ContactsContract.Profile; |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 79 | import android.provider.ContactsContract.RawContacts; |
markchien | 1dd1d79 | 2019-01-11 16:18:20 +0800 | [diff] [blame] | 80 | import android.telephony.SubscriptionManager; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 81 | import android.telephony.TelephonyManager; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 82 | import android.text.Spannable; |
| 83 | import android.text.SpannableString; |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 84 | import android.text.TextUtils; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 85 | import android.text.format.DateUtils; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 86 | import android.text.style.TtsSpan; |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 87 | import android.util.ArraySet; |
Zoey Chen | 3abe48e | 2020-10-28 22:46:48 +0800 | [diff] [blame] | 88 | import android.util.FeatureFlagUtils; |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 89 | import android.util.IconDrawableFactory; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 90 | import android.util.Log; |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 91 | import android.view.LayoutInflater; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 92 | import android.view.View; |
| 93 | import android.view.ViewGroup; |
PMS22 | 63a0560 | 2017-06-14 07:10:15 +0000 | [diff] [blame] | 94 | import android.widget.EditText; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 95 | import android.widget.ListView; |
| 96 | import android.widget.TabWidget; |
Fan Zhang | cc335d9 | 2016-09-29 14:37:14 -0700 | [diff] [blame] | 97 | |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 98 | import androidx.annotation.ColorInt; |
Tim Peng | df46788 | 2020-06-24 13:10:01 +0800 | [diff] [blame] | 99 | import androidx.annotation.NonNull; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 100 | import androidx.annotation.StringRes; |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 101 | import androidx.core.graphics.drawable.IconCompat; |
Tim Peng | df46788 | 2020-06-24 13:10:01 +0800 | [diff] [blame] | 102 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 103 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 104 | import androidx.fragment.app.Fragment; |
tmfang | aed8f36 | 2019-03-18 16:56:09 +0800 | [diff] [blame] | 105 | import androidx.lifecycle.Lifecycle; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 106 | import androidx.preference.Preference; |
| 107 | import androidx.preference.PreferenceGroup; |
| 108 | |
Ricky Wai | 616342b | 2016-04-13 10:40:22 +0100 | [diff] [blame] | 109 | import com.android.internal.app.UnlaunchableAppActivity; |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 110 | import com.android.internal.util.ArrayUtils; |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 111 | import com.android.internal.widget.LockPatternUtils; |
Raff Tsai | afd494d | 2019-10-16 15:25:21 +0800 | [diff] [blame] | 112 | import com.android.settings.dashboard.profileselector.ProfileFragmentBridge; |
Raff Tsai | 84327f6 | 2019-11-15 11:02:25 +0800 | [diff] [blame] | 113 | import com.android.settings.dashboard.profileselector.ProfileSelectFragment; |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 114 | import com.android.settings.password.ChooseLockSettingsHelper; |
tmfang | aed8f36 | 2019-03-18 16:56:09 +0800 | [diff] [blame] | 115 | import com.android.settingslib.widget.ActionBarShadowController; |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 116 | import com.android.settingslib.widget.AdaptiveIcon; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 117 | |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 118 | import java.util.Iterator; |
Daisuke Miyakawa | a2633d0 | 2010-09-15 20:09:12 -0700 | [diff] [blame] | 119 | import java.util.List; |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 120 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 121 | |
Jason Monk | 27985e1 | 2016-01-08 14:13:05 -0500 | [diff] [blame] | 122 | public final class Utils extends com.android.settingslib.Utils { |
| 123 | |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 124 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 125 | |
Andras Kloczl | 106431e | 2020-05-18 14:53:07 +0100 | [diff] [blame] | 126 | public static final String FILE_PROVIDER_AUTHORITY = "com.android.settings.files"; |
| 127 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Set the preference's title to the matching activity's label. |
| 130 | */ |
| 131 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 132 | |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 133 | public static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 134 | |
Jason Monk | beb171d | 2015-05-21 15:24:37 -0400 | [diff] [blame] | 135 | public static final String OS_PKG = "os"; |
| 136 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 137 | /** |
Svet Ganov | 864765f | 2019-04-05 19:00:05 -0700 | [diff] [blame] | 138 | * Whether to disable the new device identifier access restrictions. |
| 139 | */ |
| 140 | public static final String PROPERTY_DEVICE_IDENTIFIER_ACCESS_RESTRICTIONS_DISABLED = |
| 141 | "device_identifier_access_restrictions_disabled"; |
| 142 | |
| 143 | /** |
Joel Galenson | 2118f8a | 2019-05-31 07:54:20 -0700 | [diff] [blame] | 144 | * Whether to show the Permissions Hub. |
| 145 | */ |
| 146 | public static final String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled"; |
| 147 | |
| 148 | /** |
Wei Wang | edbe2cc | 2020-12-08 22:48:53 -0800 | [diff] [blame] | 149 | * Whether to show location indicators. |
| 150 | */ |
| 151 | public static final String PROPERTY_LOCATION_INDICATORS_ENABLED = "location_indicators_enabled"; |
| 152 | |
| 153 | /** |
Wei Wang | a6daf85 | 2021-01-12 10:30:25 -0800 | [diff] [blame] | 154 | * Whether to show location indicator settings in developer options. |
| 155 | */ |
| 156 | public static final String PROPERTY_LOCATION_INDICATOR_SETTINGS_ENABLED = |
| 157 | "location_indicator_settings_enabled"; |
| 158 | |
Zhen Zhang | 48bd52e | 2021-03-03 13:53:34 -0800 | [diff] [blame] | 159 | /** Whether or not app hibernation is enabled on the device **/ |
| 160 | public static final String PROPERTY_APP_HIBERNATION_ENABLED = "app_hibernation_enabled"; |
| 161 | |
Kevin Han | 4b34271 | 2021-05-25 17:45:23 -0700 | [diff] [blame] | 162 | /** Whether or not app hibernation targets apps that target a pre-S SDK **/ |
| 163 | public static final String PROPERTY_HIBERNATION_TARGETS_PRE_S_APPS = |
| 164 | "app_hibernation_targets_pre_s_apps"; |
| 165 | |
Wei Wang | a6daf85 | 2021-01-12 10:30:25 -0800 | [diff] [blame] | 166 | /** |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 167 | * Finds a matching activity for a preference's intent. If a matching |
| 168 | * activity is not found, it will remove the preference. |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 169 | * |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 170 | * @param context The context. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 171 | * @param parentPreferenceGroup The preference group that contains the |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 172 | * preference whose intent is being resolved. |
| 173 | * @param preferenceKey The key of the preference whose intent is being |
| 174 | * resolved. |
| 175 | * @param flags 0 or one or more of |
| 176 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 177 | * . |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 178 | * @return Whether an activity was found. If false, the preference was |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 179 | * removed. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 180 | */ |
| 181 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 182 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 183 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 184 | final Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 185 | if (preference == null) { |
| 186 | return false; |
| 187 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 188 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 189 | final Intent intent = preference.getIntent(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 190 | if (intent != null) { |
| 191 | // Find the activity that is in the system image |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 192 | final PackageManager pm = context.getPackageManager(); |
| 193 | final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 194 | final int listSize = list.size(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 195 | for (int i = 0; i < listSize; i++) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 196 | final ResolveInfo resolveInfo = list.get(i); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 197 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 198 | != 0) { |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 199 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 200 | // Replace the intent with this specific activity |
| 201 | preference.setIntent(new Intent().setClassName( |
| 202 | resolveInfo.activityInfo.packageName, |
| 203 | resolveInfo.activityInfo.name)); |
| 204 | |
| 205 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 206 | // Set the preference title to the activity's label |
| 207 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 208 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 209 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Did not find a matching activity, so remove the preference |
| 216 | parentPreferenceGroup.removePreference(preference); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 217 | |
Shuhrat Dehkanov | 7dc567a | 2012-04-23 01:59:56 +0900 | [diff] [blame] | 218 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 219 | } |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 220 | |
Anders Hammar1 | b2dd903 | 2010-04-08 10:03:50 +0200 | [diff] [blame] | 221 | /** |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 222 | * Returns true if Monkey is running. |
| 223 | */ |
| 224 | public static boolean isMonkeyRunning() { |
Amith Yamasani | ae69755 | 2011-09-27 11:33:17 -0700 | [diff] [blame] | 225 | return ActivityManager.isUserAMonkey(); |
Ying Wang | a718832 | 2010-01-04 18:45:10 -0800 | [diff] [blame] | 226 | } |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Returns whether the device is voice-capable (meaning, it is also a phone). |
| 230 | */ |
| 231 | public static boolean isVoiceCapable(Context context) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 232 | final TelephonyManager telephony = |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 233 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 234 | return telephony != null && telephony.isVoiceCapable(); |
| 235 | } |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 236 | |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. |
| 239 | * @param context the application context |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 240 | * @return the formatted and newline-separated IP addresses, or null if none. |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 241 | */ |
| 242 | public static String getWifiIpAddresses(Context context) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 243 | final WifiManager wifiManager = context.getSystemService(WifiManager.class); |
| 244 | final Network currentNetwork = wifiManager.getCurrentNetwork(); |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 245 | if (currentNetwork != null) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 246 | final ConnectivityManager cm = (ConnectivityManager) |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 247 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 248 | final LinkProperties prop = cm.getLinkProperties(currentNetwork); |
Jaewoong Jung | c260e6d | 2016-10-13 14:21:52 -0700 | [diff] [blame] | 249 | return formatIpAddresses(prop); |
| 250 | } |
| 251 | return null; |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Amith Yamasani | 6822b74 | 2011-10-17 16:41:00 -0700 | [diff] [blame] | 254 | private static String formatIpAddresses(LinkProperties prop) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 255 | if (prop == null) return null; |
Chiachang Wang | 003e27d | 2021-03-18 15:01:03 +0800 | [diff] [blame] | 256 | final Iterator<LinkAddress> iter = prop.getAllLinkAddresses().iterator(); |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 257 | // If there are no entries, return null |
| 258 | if (!iter.hasNext()) return null; |
| 259 | // Concatenate all available addresses, comma separated |
| 260 | String addresses = ""; |
| 261 | while (iter.hasNext()) { |
Chiachang Wang | 003e27d | 2021-03-18 15:01:03 +0800 | [diff] [blame] | 262 | addresses += iter.next().getAddress().getHostAddress(); |
Lorenzo Colitti | 769f069 | 2013-08-01 17:30:07 +0900 | [diff] [blame] | 263 | if (iter.hasNext()) addresses += "\n"; |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 264 | } |
| 265 | return addresses; |
| 266 | } |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 267 | |
| 268 | public static Locale createLocaleFromString(String localeStr) { |
| 269 | // TODO: is there a better way to actually construct a locale that will match? |
| 270 | // The main problem is, on top of Java specs, locale.toString() and |
| 271 | // new Locale(locale.toString()).toString() do not return equal() strings in |
| 272 | // many cases, because the constructor takes the only string as the language |
| 273 | // code. So : new Locale("en", "US").toString() => "en_US" |
| 274 | // And : new Locale("en_US").toString() => "en_us" |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 275 | if (null == localeStr) |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 276 | return Locale.getDefault(); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 277 | final String[] brokenDownLocale = localeStr.split("_", 3); |
Jean Chalard | 71ad1f4 | 2011-05-12 15:06:16 +0900 | [diff] [blame] | 278 | // split may not return a 0-length array. |
| 279 | if (1 == brokenDownLocale.length) { |
| 280 | return new Locale(brokenDownLocale[0]); |
| 281 | } else if (2 == brokenDownLocale.length) { |
| 282 | return new Locale(brokenDownLocale[0], brokenDownLocale[1]); |
| 283 | } else { |
| 284 | return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); |
| 285 | } |
| 286 | } |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 287 | |
Jaewan Kim | a3fe77b | 2013-06-04 21:17:40 +0900 | [diff] [blame] | 288 | public static boolean isBatteryPresent(Intent batteryChangedIntent) { |
| 289 | return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 290 | } |
| 291 | |
Mill Chen | ad99e2e | 2020-10-21 16:32:56 +0800 | [diff] [blame] | 292 | /** |
| 293 | * Return true if battery is present. |
| 294 | */ |
| 295 | public static boolean isBatteryPresent(Context context) { |
| 296 | Intent batteryBroadcast = context.registerReceiver(null /* receiver */, |
| 297 | new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); |
| 298 | return isBatteryPresent(batteryBroadcast); |
| 299 | } |
| 300 | |
Amith Yamasani | a4379d6 | 2011-07-22 10:34:58 -0700 | [diff] [blame] | 301 | public static String getBatteryPercentage(Intent batteryChangedIntent) { |
Elliott Hughes | 7253df3 | 2014-09-02 17:10:14 -0700 | [diff] [blame] | 302 | return formatPercentage(getBatteryLevel(batteryChangedIntent)); |
Dianne Hackborn | 525f2bd | 2014-04-29 11:24:06 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 305 | /** |
| 306 | * Prepare a custom preferences layout, moving padding to {@link ListView} |
| 307 | * when outside scrollbars are requested. Usually used to display |
| 308 | * {@link ListView} and {@link TabWidget} with correct padding. |
| 309 | */ |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 310 | public static void prepareCustomPreferencesList( |
Jeff Sharkey | 97d07fa | 2012-11-30 12:36:53 -0800 | [diff] [blame] | 311 | ViewGroup parent, View child, View list, boolean ignoreSidePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 312 | final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 313 | if (movePadding) { |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 314 | final Resources res = list.getResources(); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 315 | final int paddingBottom = res.getDimensionPixelSize( |
| 316 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 317 | |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 318 | if (parent instanceof PreferenceFrameLayout) { |
| 319 | ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; |
Fabrice Di Meglio | 97a18c8 | 2014-07-18 19:12:36 -0700 | [diff] [blame] | 320 | } |
Doris Ling | c5a1b4f | 2019-02-20 16:33:24 -0800 | [diff] [blame] | 321 | list.setPaddingRelative(0 /* start */, 0 /* top */, 0 /* end */, paddingBottom); |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 324 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 325 | public static void forceCustomPadding(View view, boolean additive) { |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 326 | final Resources res = view.getResources(); |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 327 | |
Doris Ling | c5a1b4f | 2019-02-20 16:33:24 -0800 | [diff] [blame] | 328 | final int paddingStart = additive ? view.getPaddingStart() : 0; |
| 329 | final int paddingEnd = additive ? view.getPaddingEnd() : 0; |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 330 | final int paddingBottom = res.getDimensionPixelSize( |
| 331 | com.android.internal.R.dimen.preference_fragment_padding_bottom); |
| 332 | |
Fabrice Di Meglio | 0f4a779 | 2014-07-28 18:25:14 -0700 | [diff] [blame] | 333 | view.setPaddingRelative(paddingStart, 0, paddingEnd, paddingBottom); |
Fabrice Di Meglio | 38ba9a2 | 2014-07-18 19:58:50 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 336 | public static String getMeProfileName(Context context, boolean full) { |
| 337 | if (full) { |
| 338 | return getProfileDisplayName(context); |
| 339 | } else { |
| 340 | return getShorterNameIfPossible(context); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 341 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | private static String getShorterNameIfPossible(Context context) { |
| 345 | final String given = getLocalProfileGivenName(context); |
| 346 | return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); |
| 347 | } |
| 348 | |
| 349 | private static String getLocalProfileGivenName(Context context) { |
| 350 | final ContentResolver cr = context.getContentResolver(); |
| 351 | |
| 352 | // Find the raw contact ID for the local ME profile raw contact. |
| 353 | final long localRowProfileId; |
| 354 | final Cursor localRawProfile = cr.query( |
| 355 | Profile.CONTENT_RAW_CONTACTS_URI, |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 356 | new String[] {RawContacts._ID}, |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 357 | RawContacts.ACCOUNT_TYPE + " IS NULL AND " + |
| 358 | RawContacts.ACCOUNT_NAME + " IS NULL", |
| 359 | null, null); |
| 360 | if (localRawProfile == null) return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 361 | |
| 362 | try { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 363 | if (!localRawProfile.moveToFirst()) { |
| 364 | return null; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 365 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 366 | localRowProfileId = localRawProfile.getLong(0); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 367 | } finally { |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 368 | localRawProfile.close(); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 369 | } |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 370 | |
| 371 | // Find the structured name for the raw contact. |
| 372 | final Cursor structuredName = cr.query( |
| 373 | Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 374 | new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, |
| 375 | CommonDataKinds.StructuredName.FAMILY_NAME}, |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 376 | Data.RAW_CONTACT_ID + "=" + localRowProfileId, |
| 377 | null, null); |
| 378 | if (structuredName == null) return null; |
| 379 | |
| 380 | try { |
| 381 | if (!structuredName.moveToFirst()) { |
| 382 | return null; |
| 383 | } |
| 384 | String partialName = structuredName.getString(0); |
| 385 | if (TextUtils.isEmpty(partialName)) { |
| 386 | partialName = structuredName.getString(1); |
| 387 | } |
| 388 | return partialName; |
| 389 | } finally { |
| 390 | structuredName.close(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | private static final String getProfileDisplayName(Context context) { |
| 395 | final ContentResolver cr = context.getContentResolver(); |
| 396 | final Cursor profile = cr.query(Profile.CONTENT_URI, |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 397 | new String[] {Profile.DISPLAY_NAME}, null, null, null); |
Amith Yamasani | 8d40fac | 2012-10-23 15:36:16 -0700 | [diff] [blame] | 398 | if (profile == null) return null; |
| 399 | |
| 400 | try { |
| 401 | if (!profile.moveToFirst()) { |
| 402 | return null; |
| 403 | } |
| 404 | return profile.getString(0); |
| 405 | } finally { |
| 406 | profile.close(); |
| 407 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 408 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 409 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 410 | public static boolean hasMultipleUsers(Context context) { |
Bonian Chen | f399bd1 | 2020-01-09 14:50:03 +0800 | [diff] [blame] | 411 | return context.getSystemService(UserManager.class) |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 412 | .getUsers().size() > 1; |
| 413 | } |
Fabrice Di Meglio | 769630c | 2014-04-24 14:48:48 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
Esteban Talavera | 9b86e9c | 2017-05-30 16:48:08 +0100 | [diff] [blame] | 416 | * Returns the managed profile of the current user or {@code null} if none is found or a profile |
| 417 | * exists but it is disabled. |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 418 | */ |
| 419 | public static UserHandle getManagedProfile(UserManager userManager) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 420 | final List<UserHandle> userProfiles = userManager.getUserProfiles(); |
Aarthi Balachander | d0f8359 | 2018-07-10 17:04:53 -0700 | [diff] [blame] | 421 | for (UserHandle profile : userProfiles) { |
Adam Bookatz | 30dafd4 | 2021-10-01 00:15:32 -0700 | [diff] [blame^] | 422 | if (profile.getIdentifier() == userManager.getProcessUserId()) { |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 423 | continue; |
| 424 | } |
| 425 | final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier()); |
| 426 | if (userInfo.isManagedProfile()) { |
| 427 | return profile; |
| 428 | } |
| 429 | } |
| 430 | return null; |
| 431 | } |
| 432 | |
| 433 | /** |
Esteban Talavera | 9b86e9c | 2017-05-30 16:48:08 +0100 | [diff] [blame] | 434 | * Returns the managed profile of the current user or {@code null} if none is found. Unlike |
| 435 | * {@link #getManagedProfile} this method returns enabled and disabled managed profiles. |
| 436 | */ |
| 437 | public static UserHandle getManagedProfileWithDisabled(UserManager userManager) { |
| 438 | // TODO: Call getManagedProfileId from here once Robolectric supports |
| 439 | // API level 24 and UserManager.getProfileIdsWithDisabled can be Mocked (to avoid having |
| 440 | // yet another implementation that loops over user profiles in this method). In the meantime |
| 441 | // we need to use UserManager.getProfiles that is available on API 23 (the one currently |
| 442 | // used for Settings Robolectric tests). |
| 443 | final int myUserId = UserHandle.myUserId(); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 444 | final List<UserInfo> profiles = userManager.getProfiles(myUserId); |
Esteban Talavera | 9b86e9c | 2017-05-30 16:48:08 +0100 | [diff] [blame] | 445 | final int count = profiles.size(); |
| 446 | for (int i = 0; i < count; i++) { |
| 447 | final UserInfo profile = profiles.get(i); |
| 448 | if (profile.isManagedProfile() |
| 449 | && profile.getUserHandle().getIdentifier() != myUserId) { |
| 450 | return profile.getUserHandle(); |
| 451 | } |
| 452 | } |
| 453 | return null; |
| 454 | } |
| 455 | |
| 456 | /** |
Clara Bayarri | 462cce1 | 2016-02-18 12:09:21 +0000 | [diff] [blame] | 457 | * Retrieves the id for the given user's managed profile. |
| 458 | * |
| 459 | * @return the managed profile id or UserHandle.USER_NULL if there is none. |
| 460 | */ |
| 461 | public static int getManagedProfileId(UserManager um, int parentUserId) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 462 | final int[] profileIds = um.getProfileIdsWithDisabled(parentUserId); |
Fyodor Kupolov | 4a4af5a | 2016-04-07 16:46:18 -0700 | [diff] [blame] | 463 | for (int profileId : profileIds) { |
| 464 | if (profileId != parentUserId) { |
| 465 | return profileId; |
Clara Bayarri | 462cce1 | 2016-02-18 12:09:21 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | return UserHandle.USER_NULL; |
| 469 | } |
| 470 | |
Arc Wang | 5430682 | 2021-04-29 18:54:10 +0800 | [diff] [blame] | 471 | /** Returns user ID of current user, throws IllegalStateException if it's not available. */ |
| 472 | public static int getCurrentUserId(UserManager userManager, boolean isWorkProfile) |
| 473 | throws IllegalStateException { |
| 474 | if (isWorkProfile) { |
| 475 | final UserHandle managedUserHandle = getManagedProfile(userManager); |
| 476 | if (managedUserHandle == null) { |
| 477 | throw new IllegalStateException("Work profile user ID is not available."); |
| 478 | } |
| 479 | return managedUserHandle.getIdentifier(); |
| 480 | } |
| 481 | return UserHandle.myUserId(); |
| 482 | } |
| 483 | |
Clara Bayarri | 462cce1 | 2016-02-18 12:09:21 +0000 | [diff] [blame] | 484 | /** |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 485 | * Returns the target user for a Settings activity. |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 486 | * <p> |
| 487 | * User would be retrieved in this order: |
| 488 | * <ul> |
| 489 | * <li> If this activity is launched from other user, return that user id. |
| 490 | * <li> If this is launched from the Settings app in same user, return the user contained as an |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 491 | * extra in the arguments or intent extras. |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 492 | * <li> Otherwise, return UserHandle.myUserId(). |
| 493 | * </ul> |
| 494 | * <p> |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 495 | * Note: This is secure in the sense that it only returns a target user different to the current |
| 496 | * one if the app launching this activity is the Settings app itself, running in the same user |
| 497 | * 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] | 498 | */ |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 499 | public static UserHandle getSecureTargetUser(IBinder activityToken, |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 500 | UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 501 | final UserHandle currentUser = new UserHandle(UserHandle.myUserId()); |
| 502 | final IActivityManager am = ActivityManager.getService(); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 503 | try { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 504 | final String launchedFromPackage = am.getLaunchedFromPackage(activityToken); |
| 505 | final boolean launchedFromSettingsApp = |
| 506 | SETTINGS_PACKAGE_NAME.equals(launchedFromPackage); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 507 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 508 | final UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 509 | am.getLaunchedFromUid(activityToken))); |
| 510 | if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { |
| 511 | // Check it's secure |
| 512 | if (isProfileOf(um, launchedFromUser)) { |
| 513 | return launchedFromUser; |
| 514 | } |
| 515 | } |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 516 | final UserHandle extrasUser = getUserHandleFromBundle(intentExtras); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 517 | if (extrasUser != null && !extrasUser.equals(currentUser)) { |
| 518 | // Check it's secure |
| 519 | if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) { |
| 520 | return extrasUser; |
| 521 | } |
| 522 | } |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 523 | final UserHandle argumentsUser = getUserHandleFromBundle(arguments); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 524 | if (argumentsUser != null && !argumentsUser.equals(currentUser)) { |
| 525 | // Check it's secure |
| 526 | if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) { |
| 527 | return argumentsUser; |
| 528 | } |
| 529 | } |
| 530 | } catch (RemoteException e) { |
| 531 | // Should not happen |
| 532 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 533 | } |
| 534 | return currentUser; |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Lookup both {@link Intent#EXTRA_USER} and {@link Intent#EXTRA_USER_ID} in the bundle |
| 539 | * and return the {@link UserHandle} object. Return {@code null} if nothing is found. |
| 540 | */ |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 541 | private static @Nullable UserHandle getUserHandleFromBundle(Bundle bundle) { |
Tony Mak | 8f41b9b | 2016-11-23 11:36:18 +0000 | [diff] [blame] | 542 | if (bundle == null) { |
| 543 | return null; |
| 544 | } |
| 545 | final UserHandle user = bundle.getParcelable(EXTRA_USER); |
| 546 | if (user != null) { |
| 547 | return user; |
| 548 | } |
| 549 | final int userId = bundle.getInt(EXTRA_USER_ID, -1); |
| 550 | if (userId != -1) { |
| 551 | return UserHandle.of(userId); |
| 552 | } |
| 553 | return null; |
| 554 | } |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 555 | |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 556 | /** |
| 557 | * Returns true if the user provided is in the same profiles group as the current user. |
| 558 | */ |
| 559 | private static boolean isProfileOf(UserManager um, UserHandle otherUser) { |
| 560 | if (um == null || otherUser == null) return false; |
| 561 | return (UserHandle.myUserId() == otherUser.getIdentifier()) |
| 562 | || um.getUserProfiles().contains(otherUser); |
| 563 | } |
Amith Yamasani | 51c6dac | 2014-07-02 00:06:37 +0530 | [diff] [blame] | 564 | |
Alexandra Gherghina | 95b86a5 | 2014-07-24 19:13:25 +0100 | [diff] [blame] | 565 | /** |
Amith Yamasani | 45f8623 | 2014-11-19 17:12:46 -0800 | [diff] [blame] | 566 | * Queries for the UserInfo of a user. Returns null if the user doesn't exist (was removed). |
| 567 | * @param userManager Instance of UserManager |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 568 | * @param checkUser The user to check the existence of. |
Amith Yamasani | 45f8623 | 2014-11-19 17:12:46 -0800 | [diff] [blame] | 569 | * @return UserInfo of the user or null for non-existent user. |
| 570 | */ |
| 571 | public static UserInfo getExistingUser(UserManager userManager, UserHandle checkUser) { |
Colin Cross | 38fa185 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 572 | final List<UserInfo> users = userManager.getAliveUsers(); |
Amith Yamasani | 45f8623 | 2014-11-19 17:12:46 -0800 | [diff] [blame] | 573 | final int checkUserId = checkUser.getIdentifier(); |
| 574 | for (UserInfo user : users) { |
| 575 | if (user.id == checkUserId) { |
| 576 | return user; |
| 577 | } |
| 578 | } |
| 579 | return null; |
| 580 | } |
| 581 | |
Zoltan Szatmary-Ban | 3af2e4c | 2014-12-19 17:17:23 +0000 | [diff] [blame] | 582 | public static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup parent) { |
| 583 | final TypedArray a = inflater.getContext().obtainStyledAttributes(null, |
| 584 | com.android.internal.R.styleable.Preference, |
| 585 | com.android.internal.R.attr.preferenceCategoryStyle, 0); |
| 586 | final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, |
| 587 | 0); |
| 588 | a.recycle(); |
| 589 | return inflater.inflate(resId, parent, false); |
| 590 | } |
| 591 | |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 592 | public static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 593 | final List<IntentFilterVerificationInfo> iviList = |
| 594 | pm.getIntentFilterVerifications(packageName); |
| 595 | final List<IntentFilter> filters = pm.getAllIntentFilters(packageName); |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 596 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 597 | final ArraySet<String> result = new ArraySet<>(); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 598 | if (iviList != null && iviList.size() > 0) { |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 599 | for (IntentFilterVerificationInfo ivi : iviList) { |
| 600 | for (String host : ivi.getDomains()) { |
| 601 | result.add(host); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | if (filters != null && filters.size() > 0) { |
| 606 | for (IntentFilter filter : filters) { |
Christopher Tate | ddaa142 | 2015-07-16 16:00:49 -0700 | [diff] [blame] | 607 | if (filter.hasCategory(Intent.CATEGORY_BROWSABLE) |
| 608 | && (filter.hasDataScheme(IntentFilter.SCHEME_HTTP) || |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 609 | filter.hasDataScheme(IntentFilter.SCHEME_HTTPS))) { |
Christopher Tate | a08a225 | 2015-07-01 16:52:43 -0700 | [diff] [blame] | 610 | result.addAll(filter.getHostsList()); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | return result; |
| 615 | } |
| 616 | |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 617 | /** |
| 618 | * Returns the application info of the currently installed MDM package. |
| 619 | */ |
| 620 | public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 621 | final DevicePolicyManager dpm = |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 622 | (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 623 | final ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId); |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 624 | if (mdmPackage == null) { |
| 625 | return null; |
| 626 | } |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 627 | final String mdmPackageName = mdmPackage.getPackageName(); |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 628 | try { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 629 | final IPackageManager ipm = AppGlobals.getPackageManager(); |
| 630 | final ApplicationInfo mdmApplicationInfo = |
Sudheer Shanka | bc95630 | 2015-04-09 12:19:53 +0100 | [diff] [blame] | 631 | ipm.getApplicationInfo(mdmPackageName, 0, profileId); |
| 632 | return mdmApplicationInfo; |
| 633 | } catch (RemoteException e) { |
| 634 | Log.e(TAG, "Error while retrieving application info for package " + mdmPackageName |
| 635 | + ", userId " + profileId, e); |
| 636 | return null; |
| 637 | } |
| 638 | } |
Jason Monk | b45e27b | 2015-05-20 13:35:43 -0400 | [diff] [blame] | 639 | |
| 640 | public static boolean isBandwidthControlEnabled() { |
| 641 | final INetworkManagementService netManager = INetworkManagementService.Stub |
| 642 | .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
| 643 | try { |
| 644 | return netManager.isBandwidthControlEnabled(); |
| 645 | } catch (RemoteException e) { |
| 646 | return false; |
| 647 | } |
| 648 | } |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 649 | |
| 650 | /** |
| 651 | * Returns an accessible SpannableString. |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 652 | * @param displayText the text to display |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 653 | * @param accessibileText the text text-to-speech engines should read |
| 654 | */ |
| 655 | public static SpannableString createAccessibleSequence(CharSequence displayText, |
| 656 | String accessibileText) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 657 | final SpannableString str = new SpannableString(displayText); |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 658 | str.setSpan(new TtsSpan.TextBuilder(accessibileText).build(), 0, |
| 659 | displayText.length(), |
| 660 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 661 | return str; |
| 662 | } |
Andres Morales | 7bdffd8 | 2015-08-04 16:55:00 -0700 | [diff] [blame] | 663 | |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 664 | /** |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 665 | * Returns the user id present in the bundle with |
| 666 | * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 667 | * |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 668 | * @throws SecurityException if the given userId does not belong to the |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 669 | * current user group. |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 670 | */ |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 671 | public static int getUserIdFromBundle(Context context, Bundle bundle) { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 672 | return getUserIdFromBundle(context, bundle, false); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Returns the user id present in the bundle with |
| 677 | * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. |
| 678 | * |
| 679 | * @param isInternal indicating if the caller is "internal" to the system, |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 680 | * meaning we're willing to trust extras like |
Kevin Chyn | b13bc50 | 2020-07-20 23:35:21 -0700 | [diff] [blame] | 681 | * {@link ChooseLockSettingsHelper#EXTRA_KEY_ALLOW_ANY_USER}. |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 682 | * @throws SecurityException if the given userId does not belong to the |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 683 | * current user group. |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 684 | */ |
| 685 | public static int getUserIdFromBundle(Context context, Bundle bundle, boolean isInternal) { |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 686 | if (bundle == null) { |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 687 | return getCredentialOwnerUserId(context); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 688 | } |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 689 | final boolean allowAnyUser = isInternal |
Kevin Chyn | b13bc50 | 2020-07-20 23:35:21 -0700 | [diff] [blame] | 690 | && bundle.getBoolean(ChooseLockSettingsHelper.EXTRA_KEY_ALLOW_ANY_USER, false); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 691 | final int userId = bundle.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId()); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 692 | if (userId == LockPatternUtils.USER_FRP) { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 693 | return allowAnyUser ? userId : enforceSystemUser(context, userId); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 694 | } else { |
Jeff Sharkey | 219ec91 | 2017-12-19 14:57:39 -0700 | [diff] [blame] | 695 | return allowAnyUser ? userId : enforceSameOwner(context, userId); |
Adrian Roos | 5a9a3cd | 2017-03-30 18:02:25 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Returns the given user id if the current user is the system user. |
| 701 | * |
| 702 | * @throws SecurityException if the current user is not the system user. |
| 703 | */ |
| 704 | public static int enforceSystemUser(Context context, int userId) { |
| 705 | if (UserHandle.myUserId() == UserHandle.USER_SYSTEM) { |
| 706 | return userId; |
| 707 | } |
| 708 | throw new SecurityException("Given user id " + userId + " must only be used from " |
| 709 | + "USER_SYSTEM, but current user is " + UserHandle.myUserId()); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Returns the given user id if it belongs to the current user. |
| 714 | * |
| 715 | * @throws SecurityException if the given userId does not belong to the current user group. |
| 716 | */ |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 717 | public static int enforceSameOwner(Context context, int userId) { |
Bonian Chen | f399bd1 | 2020-01-09 14:50:03 +0800 | [diff] [blame] | 718 | final UserManager um = context.getSystemService(UserManager.class); |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 719 | final int[] profileIds = um.getProfileIdsWithDisabled(UserHandle.myUserId()); |
| 720 | if (ArrayUtils.contains(profileIds, userId)) { |
| 721 | return userId; |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 722 | } |
Ricky Wai | 7250016 | 2016-06-07 16:54:25 +0100 | [diff] [blame] | 723 | throw new SecurityException("Given user id " + userId + " does not belong to user " |
| 724 | + UserHandle.myUserId()); |
Clara Bayarri | fe432e8 | 2015-10-12 12:07:02 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 727 | /** |
| 728 | * Returns the effective credential owner of the calling user. |
| 729 | */ |
| 730 | public static int getCredentialOwnerUserId(Context context) { |
| 731 | return getCredentialOwnerUserId(context, UserHandle.myUserId()); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Returns the user id of the credential owner of the given user id. |
| 736 | */ |
| 737 | public static int getCredentialOwnerUserId(Context context, int userId) { |
Bonian Chen | f399bd1 | 2020-01-09 14:50:03 +0800 | [diff] [blame] | 738 | final UserManager um = context.getSystemService(UserManager.class); |
Benjamin Franz | 194300d | 2016-01-13 12:16:25 +0000 | [diff] [blame] | 739 | return um.getCredentialOwnerProfile(userId); |
Andres Morales | 7bdffd8 | 2015-08-04 16:55:00 -0700 | [diff] [blame] | 740 | } |
Jason Monk | db4ed19 | 2015-12-11 16:48:31 -0500 | [diff] [blame] | 741 | |
Alex Johnston | 403c330 | 2020-01-07 15:40:05 +0000 | [diff] [blame] | 742 | /** |
| 743 | * Returns the credential type of the given user id. |
| 744 | */ |
| 745 | public static @LockPatternUtils.CredentialType int getCredentialType(Context context, |
| 746 | int userId) { |
| 747 | final LockPatternUtils lpu = new LockPatternUtils(context); |
| 748 | return lpu.getCredentialTypeForUser(userId); |
| 749 | } |
| 750 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 751 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 752 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 753 | sBuilder, Locale.getDefault()); |
| 754 | |
| 755 | public static String formatDateRange(Context context, long start, long end) { |
| 756 | final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; |
| 757 | |
| 758 | synchronized (sBuilder) { |
| 759 | sBuilder.setLength(0); |
| 760 | return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null) |
| 761 | .toString(); |
| 762 | } |
| 763 | } |
Jason Monk | 91e2f89 | 2016-02-23 15:31:09 -0500 | [diff] [blame] | 764 | |
Bernard Chau | 88d523b | 2016-04-14 15:08:28 +0100 | [diff] [blame] | 765 | public static boolean startQuietModeDialogIfNecessary(Context context, UserManager um, |
Ricky Wai | 616342b | 2016-04-13 10:40:22 +0100 | [diff] [blame] | 766 | int userId) { |
| 767 | if (um.isQuietModeEnabled(UserHandle.of(userId))) { |
| 768 | final Intent intent = UnlaunchableAppActivity.createInQuietModeDialogIntent(userId); |
| 769 | context.startActivity(intent); |
| 770 | return true; |
| 771 | } |
| 772 | return false; |
| 773 | } |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 774 | |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 775 | public static boolean unlockWorkProfileIfNecessary(Context context, int userId) { |
| 776 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 777 | if (!ActivityManager.getService().isUserRunning(userId, |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 778 | ActivityManager.FLAG_AND_LOCKED)) { |
| 779 | return false; |
| 780 | } |
| 781 | } catch (RemoteException e) { |
| 782 | return false; |
| 783 | } |
| 784 | if (!(new LockPatternUtils(context)).isSecure(userId)) { |
| 785 | return false; |
| 786 | } |
Robin Lee | cccf324 | 2017-02-10 15:32:49 +0000 | [diff] [blame] | 787 | return confirmWorkProfileCredentials(context, userId); |
| 788 | } |
| 789 | |
Robin Lee | cccf324 | 2017-02-10 15:32:49 +0000 | [diff] [blame] | 790 | private static boolean confirmWorkProfileCredentials(Context context, int userId) { |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 791 | final KeyguardManager km = (KeyguardManager) context.getSystemService( |
| 792 | Context.KEYGUARD_SERVICE); |
| 793 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, userId); |
| 794 | if (unlockIntent != null) { |
| 795 | context.startActivity(unlockIntent); |
| 796 | return true; |
| 797 | } else { |
| 798 | return false; |
| 799 | } |
Rubin Xu | 3231afe | 2016-08-24 10:15:07 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 802 | public static CharSequence getApplicationLabel(Context context, String packageName) { |
| 803 | try { |
| 804 | final ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo( |
| 805 | packageName, |
| 806 | PackageManager.MATCH_DISABLED_COMPONENTS |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 807 | | PackageManager.MATCH_ANY_USER); |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 808 | return appInfo.loadLabel(context.getPackageManager()); |
| 809 | } catch (PackageManager.NameNotFoundException e) { |
Raff Tsai | 376ce87 | 2020-01-06 00:42:35 +0800 | [diff] [blame] | 810 | Log.e(TAG, "Unable to find info for package: " + packageName); |
Sudheer Shanka | c3eb16e | 2016-04-21 12:51:43 -0700 | [diff] [blame] | 811 | } |
| 812 | return null; |
| 813 | } |
Jason Monk | 7519954 | 2016-05-06 15:09:32 -0400 | [diff] [blame] | 814 | |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 815 | public static boolean isPackageDirectBootAware(Context context, String packageName) { |
| 816 | try { |
| 817 | final ApplicationInfo ai = context.getPackageManager().getApplicationInfo( |
| 818 | packageName, 0); |
| 819 | return ai.isDirectBootAware() || ai.isPartiallyDirectBootAware(); |
| 820 | } catch (NameNotFoundException ignored) { |
| 821 | } |
| 822 | return false; |
| 823 | } |
Andre Lago | 3e398c8 | 2016-07-25 14:12:28 +0100 | [diff] [blame] | 824 | |
| 825 | /** |
| 826 | * Returns a context created from the given context for the given user, or null if it fails |
| 827 | */ |
| 828 | public static Context createPackageContextAsUser(Context context, int userId) { |
| 829 | try { |
| 830 | return context.createPackageContextAsUser( |
| 831 | context.getPackageName(), 0 /* flags */, UserHandle.of(userId)); |
| 832 | } catch (PackageManager.NameNotFoundException e) { |
| 833 | Log.e(TAG, "Failed to create user context", e); |
| 834 | } |
| 835 | return null; |
| 836 | } |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 837 | |
| 838 | public static FingerprintManager getFingerprintManagerOrNull(Context context) { |
| 839 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
Fan Zhang | cf97f0b | 2018-01-05 11:18:44 -0800 | [diff] [blame] | 840 | return (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE); |
Jeff Sharkey | ab50807 | 2016-10-11 14:25:22 -0600 | [diff] [blame] | 841 | } else { |
| 842 | return null; |
| 843 | } |
| 844 | } |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 845 | |
Kevin Chyn | 82792cc | 2017-07-20 00:15:44 -0700 | [diff] [blame] | 846 | public static boolean hasFingerprintHardware(Context context) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 847 | final FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context); |
Kevin Chyn | 82792cc | 2017-07-20 00:15:44 -0700 | [diff] [blame] | 848 | return fingerprintManager != null && fingerprintManager.isHardwareDetected(); |
| 849 | } |
| 850 | |
Kevin Chyn | 4882e87 | 2018-06-25 17:58:31 -0700 | [diff] [blame] | 851 | public static FaceManager getFaceManagerOrNull(Context context) { |
| 852 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 853 | return (FaceManager) context.getSystemService(Context.FACE_SERVICE); |
| 854 | } else { |
| 855 | return null; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | public static boolean hasFaceHardware(Context context) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 860 | final FaceManager faceManager = getFaceManagerOrNull(context); |
Kevin Chyn | 4882e87 | 2018-06-25 17:58:31 -0700 | [diff] [blame] | 861 | return faceManager != null && faceManager.isHardwareDetected(); |
| 862 | } |
| 863 | |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 864 | /** |
Mill Chen | 7edebee | 2021-04-09 23:14:27 +0800 | [diff] [blame] | 865 | * Return true if the device supports multiple biometrics authentications. |
| 866 | */ |
| 867 | public static boolean isMultipleBiometricsSupported(Context context) { |
| 868 | return hasFingerprintHardware(context) && hasFaceHardware(context); |
| 869 | } |
| 870 | |
| 871 | /** |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 872 | * Launches an intent which may optionally have a user id defined. |
| 873 | * @param fragment Fragment to use to launch the activity. |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 874 | * @param intent Intent to launch. |
Daniel Nishi | 31027da | 2017-01-19 14:03:57 -0800 | [diff] [blame] | 875 | */ |
| 876 | public static void launchIntent(Fragment fragment, Intent intent) { |
| 877 | try { |
| 878 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, -1); |
| 879 | |
| 880 | if (userId == -1) { |
| 881 | fragment.startActivity(intent); |
| 882 | } else { |
| 883 | fragment.getActivity().startActivityAsUser(intent, new UserHandle(userId)); |
| 884 | } |
| 885 | } catch (ActivityNotFoundException e) { |
| 886 | Log.w(TAG, "No activity found for " + intent); |
| 887 | } |
| 888 | } |
| 889 | |
Christine Franks | c6890ab | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 890 | public static boolean isDemoUser(Context context) { |
Bonian Chen | f399bd1 | 2020-01-09 14:50:03 +0800 | [diff] [blame] | 891 | return UserManager.isDeviceInDemoMode(context) |
| 892 | && context.getSystemService(UserManager.class).isDemoUser(); |
Christine Franks | c6890ab | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Justin Klaassen | 3025727 | 2017-08-08 21:58:05 -0700 | [diff] [blame] | 895 | public static ComponentName getDeviceOwnerComponent(Context context) { |
| 896 | final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( |
| 897 | Context.DEVICE_POLICY_SERVICE); |
| 898 | return dpm.getDeviceOwnerComponentOnAnyUser(); |
Christine Franks | 1478222 | 2017-01-23 16:44:02 -0800 | [diff] [blame] | 899 | } |
Daniel Nishi | dfed8a2 | 2017-01-19 16:32:41 -0800 | [diff] [blame] | 900 | |
| 901 | /** |
| 902 | * Returns if a given user is a profile of another user. |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 903 | * @param user The user whose profiles wibe checked. |
Daniel Nishi | dfed8a2 | 2017-01-19 16:32:41 -0800 | [diff] [blame] | 904 | * @param profile The (potential) profile. |
| 905 | * @return if the profile is actually a profile |
| 906 | */ |
| 907 | public static boolean isProfileOf(UserInfo user, UserInfo profile) { |
| 908 | return user.id == profile.id || |
| 909 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 910 | && user.profileGroupId == profile.profileGroupId); |
| 911 | } |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 912 | |
| 913 | /** |
| 914 | * Tries to initalize a volume with the given bundle. If it is a valid, private, and readable |
| 915 | * {@link VolumeInfo}, it is returned. If it is not valid, null is returned. |
| 916 | */ |
| 917 | @Nullable |
| 918 | public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) { |
| 919 | final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID, |
| 920 | VolumeInfo.ID_PRIVATE_INTERNAL); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 921 | final VolumeInfo volume = sm.findVolumeById(volumeId); |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 922 | return isVolumeValid(volume) ? volume : null; |
| 923 | } |
| 924 | |
jackqdyulei | ddba966 | 2017-03-08 19:35:02 -0800 | [diff] [blame] | 925 | /** |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 926 | * Return {@code true} if the supplied package is device owner or profile owner of at |
| 927 | * least one user. |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 928 | * @param userManager used to get profile owner app for each user |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 929 | * @param devicePolicyManager used to check whether it is device owner app |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 930 | * @param packageName package to check about |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 931 | */ |
| 932 | public static boolean isProfileOrDeviceOwner(UserManager userManager, |
Fan Zhang | aab36de | 2018-03-30 16:58:28 -0700 | [diff] [blame] | 933 | DevicePolicyManager devicePolicyManager, String packageName) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 934 | final List<UserInfo> userInfos = userManager.getUsers(); |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 935 | if (devicePolicyManager.isDeviceOwnerAppOnAnyUser(packageName)) { |
| 936 | return true; |
| 937 | } |
| 938 | for (int i = 0, size = userInfos.size(); i < size; i++) { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 939 | final ComponentName cn = devicePolicyManager |
| 940 | .getProfileOwnerAsUser(userInfos.get(i).id); |
jackqdyulei | 00015fb | 2017-08-22 11:25:30 -0700 | [diff] [blame] | 941 | if (cn != null && cn.getPackageName().equals(packageName)) { |
| 942 | return true; |
| 943 | } |
| 944 | } |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | /** |
Alex Johnston | 341f995 | 2020-02-26 15:54:11 +0000 | [diff] [blame] | 949 | * Return {@code true} if the supplied package is the device owner or profile owner of a |
| 950 | * given user. |
| 951 | * |
| 952 | * @param devicePolicyManager used to check whether it is device owner and profile owner app |
| 953 | * @param packageName package to check about |
| 954 | * @param userId the if of the relevant user |
| 955 | */ |
| 956 | public static boolean isProfileOrDeviceOwner(DevicePolicyManager devicePolicyManager, |
| 957 | String packageName, int userId) { |
| 958 | if ((devicePolicyManager.getDeviceOwnerUserId() == userId) |
| 959 | && devicePolicyManager.isDeviceOwnerApp(packageName)) { |
| 960 | return true; |
| 961 | } |
| 962 | final ComponentName cn = devicePolicyManager.getProfileOwnerAsUser(userId); |
| 963 | if (cn != null && cn.getPackageName().equals(packageName)) { |
| 964 | return true; |
| 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | /** |
jackqdyulei | ddba966 | 2017-03-08 19:35:02 -0800 | [diff] [blame] | 970 | * Return the resource id to represent the install status for an app |
| 971 | */ |
| 972 | @StringRes |
| 973 | public static int getInstallationStatus(ApplicationInfo info) { |
| 974 | if ((info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 975 | return R.string.not_installed; |
| 976 | } |
| 977 | return info.enabled ? R.string.installed : R.string.disabled; |
| 978 | } |
| 979 | |
Daniel Nishi | 9f60f42 | 2017-02-15 15:25:48 -0800 | [diff] [blame] | 980 | private static boolean isVolumeValid(VolumeInfo volume) { |
| 981 | return (volume != null) && (volume.getType() == VolumeInfo.TYPE_PRIVATE) |
| 982 | && volume.isMountedReadable(); |
| 983 | } |
jackqdyulei | f4c1cef | 2017-03-31 12:57:10 -0700 | [diff] [blame] | 984 | |
PMS22 | 63a0560 | 2017-06-14 07:10:15 +0000 | [diff] [blame] | 985 | public static void setEditTextCursorPosition(EditText editText) { |
| 986 | editText.setSelection(editText.getText().length()); |
| 987 | } |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 988 | |
| 989 | /** |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 990 | * Gets the adaptive icon with a drawable that wrapped with an adaptive background using {@code |
| 991 | * backgroundColor} if it is not a {@link AdaptiveIconDrawable} |
| 992 | * |
| 993 | * If the given {@code icon} is too big, it will be auto scaled down to to avoid crashing |
| 994 | * Settings. |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 995 | */ |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 996 | public static Drawable getAdaptiveIcon(Context context, Drawable icon, |
| 997 | @ColorInt int backgroundColor) { |
| 998 | Drawable adaptiveIcon = getSafeIcon(icon); |
| 999 | |
| 1000 | if (!(adaptiveIcon instanceof AdaptiveIconDrawable)) { |
| 1001 | adaptiveIcon = new AdaptiveIcon(context, adaptiveIcon); |
| 1002 | ((AdaptiveIcon) adaptiveIcon).setBackgroundColor(backgroundColor); |
| 1003 | } |
| 1004 | |
| 1005 | return adaptiveIcon; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Gets the icon with a drawable that is scaled down to to avoid crashing Settings if it's too |
| 1010 | * big and not a {@link VectorDrawable}. |
| 1011 | */ |
| 1012 | public static Drawable getSafeIcon(Drawable icon) { |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1013 | Drawable safeIcon = icon; |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 1014 | |
Phil Weaver | 5a3e02d | 2018-03-08 17:38:46 -0800 | [diff] [blame] | 1015 | if ((icon != null) && !(icon instanceof VectorDrawable)) { |
menghanli | a167478 | 2021-08-24 08:45:46 +0800 | [diff] [blame] | 1016 | safeIcon = getSafeDrawable(icon, |
| 1017 | /* MAX_DRAWABLE_SIZE */ 600, /* MAX_DRAWABLE_SIZE */ 600); |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1018 | } |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 1019 | |
| 1020 | return safeIcon; |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Gets a drawable with a limited size to avoid crashing Settings if it's too big. |
| 1025 | * |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 1026 | * @param original original drawable, typically an app icon. |
| 1027 | * @param maxWidth maximum width, in pixels. |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1028 | * @param maxHeight maximum height, in pixels. |
| 1029 | */ |
jasonwshsu | 9b284a9 | 2021-04-20 15:19:15 +0800 | [diff] [blame] | 1030 | private static Drawable getSafeDrawable(Drawable original, int maxWidth, int maxHeight) { |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1031 | final int actualWidth = original.getMinimumWidth(); |
| 1032 | final int actualHeight = original.getMinimumHeight(); |
| 1033 | |
| 1034 | if (actualWidth <= maxWidth && actualHeight <= maxHeight) { |
| 1035 | return original; |
| 1036 | } |
| 1037 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 1038 | final float scaleWidth = ((float) maxWidth) / actualWidth; |
| 1039 | final float scaleHeight = ((float) maxHeight) / actualHeight; |
| 1040 | final float scale = Math.min(scaleWidth, scaleHeight); |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1041 | final int width = (int) (actualWidth * scale); |
| 1042 | final int height = (int) (actualHeight * scale); |
| 1043 | |
| 1044 | final Bitmap bitmap; |
| 1045 | if (original instanceof BitmapDrawable) { |
| 1046 | bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) original).getBitmap(), width, |
| 1047 | height, false); |
| 1048 | } else { |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1049 | bitmap = createBitmap(original, width, height); |
Felipe Leme | bd88450 | 2017-09-15 18:16:21 -0700 | [diff] [blame] | 1050 | } |
| 1051 | return new BitmapDrawable(null, bitmap); |
| 1052 | } |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 1053 | |
| 1054 | /** |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1055 | * Create an Icon pointing to a drawable. |
Jason Chiu | 1217bbe | 2019-01-24 16:32:31 +0800 | [diff] [blame] | 1056 | */ |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1057 | public static IconCompat createIconWithDrawable(Drawable drawable) { |
| 1058 | Bitmap bitmap; |
Jason Chiu | 1217bbe | 2019-01-24 16:32:31 +0800 | [diff] [blame] | 1059 | if (drawable instanceof BitmapDrawable) { |
Sunny Shao | 7e866b6 | 2019-08-09 11:42:04 +0800 | [diff] [blame] | 1060 | bitmap = ((BitmapDrawable)drawable).getBitmap(); |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1061 | } else { |
| 1062 | final int width = drawable.getIntrinsicWidth(); |
| 1063 | final int height = drawable.getIntrinsicHeight(); |
| 1064 | bitmap = createBitmap(drawable, |
| 1065 | width > 0 ? width : 1, |
| 1066 | height > 0 ? height : 1); |
Jason Chiu | 1217bbe | 2019-01-24 16:32:31 +0800 | [diff] [blame] | 1067 | } |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1068 | return IconCompat.createWithBitmap(bitmap); |
| 1069 | } |
Jason Chiu | 1217bbe | 2019-01-24 16:32:31 +0800 | [diff] [blame] | 1070 | |
clownshen | 24827d3 | 2019-03-29 20:22:02 +0800 | [diff] [blame] | 1071 | /** |
| 1072 | * Creates a drawable with specified width and height. |
| 1073 | */ |
| 1074 | public static Bitmap createBitmap(Drawable drawable, int width, int height) { |
Jason Chiu | c78c54a | 2019-02-13 16:07:57 +0800 | [diff] [blame] | 1075 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
Jason Chiu | 1217bbe | 2019-01-24 16:32:31 +0800 | [diff] [blame] | 1076 | final Canvas canvas = new Canvas(bitmap); |
| 1077 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); |
| 1078 | drawable.draw(canvas); |
| 1079 | return bitmap; |
| 1080 | } |
| 1081 | |
| 1082 | /** |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 1083 | * Get the {@link Drawable} that represents the app icon |
| 1084 | */ |
| 1085 | public static Drawable getBadgedIcon(IconDrawableFactory iconDrawableFactory, |
| 1086 | PackageManager packageManager, String packageName, int userId) { |
| 1087 | try { |
Tony Mak | 4e29281 | 2018-03-19 15:37:21 +0000 | [diff] [blame] | 1088 | final ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser( |
| 1089 | packageName, PackageManager.GET_META_DATA, userId); |
jackqdyulei | fde637f | 2018-01-02 14:51:01 -0800 | [diff] [blame] | 1090 | return iconDrawableFactory.getBadgedIcon(appInfo, userId); |
| 1091 | } catch (PackageManager.NameNotFoundException e) { |
| 1092 | return packageManager.getDefaultActivityIcon(); |
| 1093 | } |
| 1094 | } |
Raff Tsai | 2928cd4 | 2018-11-05 16:10:19 +0800 | [diff] [blame] | 1095 | |
| 1096 | /** Returns true if the current package is installed & enabled. */ |
| 1097 | public static boolean isPackageEnabled(Context context, String packageName) { |
| 1098 | try { |
| 1099 | return context.getPackageManager().getApplicationInfo(packageName, 0).enabled; |
| 1100 | } catch (Exception e) { |
| 1101 | Log.e(TAG, "Error while retrieving application info for package " + packageName, e); |
| 1102 | } |
| 1103 | return false; |
| 1104 | } |
markchien | 1dd1d79 | 2019-01-11 16:18:20 +0800 | [diff] [blame] | 1105 | |
| 1106 | /** Get {@link Resources} by subscription id if subscription id is valid. */ |
| 1107 | public static Resources getResourcesForSubId(Context context, int subId) { |
| 1108 | if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
| 1109 | return SubscriptionManager.getResourcesForSubId(context, subId); |
| 1110 | } else { |
| 1111 | return context.getResources(); |
| 1112 | } |
| 1113 | } |
Ng Zhi An | b97bdc3 | 2019-02-01 12:39:31 -0800 | [diff] [blame] | 1114 | |
| 1115 | /** |
| 1116 | * Returns true if SYSTEM_ALERT_WINDOW permission is available. |
| 1117 | * Starting from Q, SYSTEM_ALERT_WINDOW is disabled on low ram phones. |
| 1118 | */ |
| 1119 | public static boolean isSystemAlertWindowEnabled(Context context) { |
| 1120 | // SYSTEM_ALERT_WINDOW is disabled on on low ram devices starting from Q |
| 1121 | ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 1122 | return !(am.isLowRamDevice() && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)); |
| 1123 | } |
tmfang | aed8f36 | 2019-03-18 16:56:09 +0800 | [diff] [blame] | 1124 | |
| 1125 | /** |
| 1126 | * Adds a shadow appear/disappear animation to action bar scroll. |
| 1127 | * |
| 1128 | * <p/> |
| 1129 | * This method must be called after {@link Fragment#onCreate(Bundle)}. |
| 1130 | */ |
| 1131 | public static void setActionBarShadowAnimation(Activity activity, Lifecycle lifecycle, |
| 1132 | View scrollView) { |
| 1133 | if (activity == null) { |
| 1134 | Log.w(TAG, "No activity, cannot style actionbar."); |
| 1135 | return; |
| 1136 | } |
| 1137 | final ActionBar actionBar = activity.getActionBar(); |
| 1138 | if (actionBar == null) { |
| 1139 | Log.w(TAG, "No actionbar, cannot style actionbar."); |
| 1140 | return; |
| 1141 | } |
| 1142 | actionBar.setElevation(0); |
| 1143 | |
| 1144 | if (lifecycle != null && scrollView != null) { |
| 1145 | ActionBarShadowController.attachToView(activity, lifecycle, scrollView); |
| 1146 | } |
| 1147 | } |
Raff Tsai | afd494d | 2019-10-16 15:25:21 +0800 | [diff] [blame] | 1148 | |
| 1149 | /** |
| 1150 | * Return correct target fragment based on argument |
| 1151 | * |
| 1152 | * @param activity the activity target fragment will be launched. |
| 1153 | * @param fragmentName initial target fragment name. |
| 1154 | * @param args fragment launch arguments. |
| 1155 | */ |
| 1156 | public static Fragment getTargetFragment(Activity activity, String fragmentName, Bundle args) { |
| 1157 | Fragment f = null; |
Raff Tsai | 84327f6 | 2019-11-15 11:02:25 +0800 | [diff] [blame] | 1158 | final boolean isPersonal = args != null ? args.getInt(ProfileSelectFragment.EXTRA_PROFILE) |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 1159 | == ProfileSelectFragment.ProfileType.PERSONAL : false; |
Raff Tsai | 84327f6 | 2019-11-15 11:02:25 +0800 | [diff] [blame] | 1160 | final boolean isWork = args != null ? args.getInt(ProfileSelectFragment.EXTRA_PROFILE) |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 1161 | == ProfileSelectFragment.ProfileType.WORK : false; |
Jason Chiu | c38978f | 2021-06-29 21:10:32 +0800 | [diff] [blame] | 1162 | try { |
| 1163 | if (activity.getSystemService(UserManager.class).getUserProfiles().size() > 1 |
| 1164 | && ProfileFragmentBridge.FRAGMENT_MAP.get(fragmentName) != null |
| 1165 | && !isWork && !isPersonal) { |
| 1166 | f = Fragment.instantiate(activity, |
| 1167 | ProfileFragmentBridge.FRAGMENT_MAP.get(fragmentName), args); |
| 1168 | } else { |
| 1169 | f = Fragment.instantiate(activity, fragmentName, args); |
| 1170 | } |
| 1171 | } catch (Exception e) { |
| 1172 | Log.e(TAG, "Unable to get target fragment", e); |
Raff Tsai | afd494d | 2019-10-16 15:25:21 +0800 | [diff] [blame] | 1173 | } |
| 1174 | return f; |
| 1175 | } |
Yanting Yang | 96127fe | 2020-03-20 12:04:09 +0800 | [diff] [blame] | 1176 | |
| 1177 | /** |
| 1178 | * Returns true if current binder uid is Settings Intelligence. |
| 1179 | */ |
| 1180 | public static boolean isSettingsIntelligence(Context context) { |
| 1181 | final int callingUid = Binder.getCallingUid(); |
| 1182 | final String callingPackage = context.getPackageManager().getPackagesForUid(callingUid)[0]; |
| 1183 | final boolean isSettingsIntelligence = TextUtils.equals(callingPackage, |
| 1184 | context.getString(R.string.config_settingsintelligence_package_name)); |
| 1185 | return isSettingsIntelligence; |
| 1186 | } |
Jason Chiu | 8ac1e4d | 2020-05-11 16:13:47 +0800 | [diff] [blame] | 1187 | |
| 1188 | /** |
| 1189 | * Returns true if the night mode is enabled. |
| 1190 | */ |
| 1191 | public static boolean isNightMode(Context context) { |
| 1192 | final int currentNightMode = |
| 1193 | context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 1194 | return currentNightMode == Configuration.UI_MODE_NIGHT_YES; |
| 1195 | } |
Tim Peng | df46788 | 2020-06-24 13:10:01 +0800 | [diff] [blame] | 1196 | |
| 1197 | /** |
| 1198 | * Returns a bitmap with rounded corner. |
| 1199 | * |
| 1200 | * @param context application context. |
| 1201 | * @param source bitmap to apply round corner. |
| 1202 | * @param cornerRadius corner radius value. |
| 1203 | */ |
| 1204 | public static Bitmap convertCornerRadiusBitmap(@NonNull Context context, |
| 1205 | @NonNull Bitmap source, @NonNull float cornerRadius) { |
| 1206 | final Bitmap roundedBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), |
| 1207 | Bitmap.Config.ARGB_8888); |
| 1208 | final RoundedBitmapDrawable drawable = |
| 1209 | RoundedBitmapDrawableFactory.create(context.getResources(), source); |
| 1210 | drawable.setAntiAlias(true); |
| 1211 | drawable.setCornerRadius(cornerRadius); |
| 1212 | final Canvas canvas = new Canvas(roundedBitmap); |
| 1213 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); |
| 1214 | drawable.draw(canvas); |
| 1215 | return roundedBitmap; |
| 1216 | } |
Zoey Chen | 3abe48e | 2020-10-28 22:46:48 +0800 | [diff] [blame] | 1217 | |
Jason Chiu | 7d9dcea | 2021-04-15 16:57:22 +0800 | [diff] [blame] | 1218 | /** |
| 1219 | * Returns the color of homepage preference icons. |
| 1220 | */ |
| 1221 | @ColorInt |
| 1222 | public static int getHomepageIconColor(Context context) { |
Jason Chiu | 263d1e2 | 2021-06-02 18:14:20 +0800 | [diff] [blame] | 1223 | return getColorAttrDefaultColor(context, android.R.attr.textColorSecondary); |
Jason Chiu | 7d9dcea | 2021-04-15 16:57:22 +0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Zoey Chen | 3abe48e | 2020-10-28 22:46:48 +0800 | [diff] [blame] | 1226 | public static boolean isProviderModelEnabled(Context context) { |
| 1227 | return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SETTINGS_PROVIDER_MODEL); |
| 1228 | } |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 1229 | } |