Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 1 | package com.android.settingslib; |
| 2 | |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 3 | import android.annotation.ColorInt; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 4 | import android.content.Context; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 5 | import android.content.Intent; |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 6 | import android.content.pm.PackageInfo; |
| 7 | import android.content.pm.PackageManager; |
| 8 | import android.content.pm.PackageManager.NameNotFoundException; |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 9 | import android.content.pm.Signature; |
Fan Zhang | 73b161d | 2017-02-08 11:46:54 -0800 | [diff] [blame] | 10 | import android.content.pm.UserInfo; |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 11 | import android.content.res.ColorStateList; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 12 | import android.content.res.Resources; |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 13 | import android.content.res.TypedArray; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 14 | import android.graphics.Bitmap; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 15 | import android.graphics.Color; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 16 | import android.graphics.drawable.Drawable; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 17 | import android.location.LocationManager; |
timhypeng | 1b25c9a | 2018-04-23 18:08:47 +0800 | [diff] [blame] | 18 | import android.media.AudioManager; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 19 | import android.net.ConnectivityManager; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 20 | import android.os.BatteryManager; |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 21 | import android.os.SystemProperties; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 22 | import android.os.UserHandle; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 23 | import android.os.UserManager; |
Philip P. Moltmann | 545a58a | 2016-06-28 14:14:06 -0700 | [diff] [blame] | 24 | import android.print.PrintManager; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 25 | import android.provider.Settings; |
SongFerngWang | 8cfb4d5 | 2018-08-21 18:04:29 +0800 | [diff] [blame] | 26 | import android.telephony.ServiceState; |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 29 | import com.android.internal.util.UserIcons; |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 30 | import com.android.settingslib.drawable.UserIconDrawable; |
hjchangliao | 2da48f8 | 2018-04-27 11:30:55 +0800 | [diff] [blame] | 31 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 32 | import java.text.NumberFormat; |
| 33 | |
| 34 | public class Utils { |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 35 | |
| 36 | private static final String CURRENT_MODE_KEY = "CURRENT_MODE"; |
| 37 | private static final String NEW_MODE_KEY = "NEW_MODE"; |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 38 | @VisibleForTesting |
| 39 | static final String STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY = |
| 40 | "ro.storage_manager.show_opt_in"; |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 41 | |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 42 | private static Signature[] sSystemSignature; |
| 43 | private static String sPermissionControllerPackageName; |
| 44 | private static String sServicesSystemSharedLibPackageName; |
| 45 | private static String sSharedSystemSharedLibPackageName; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 46 | |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 47 | static final int[] WIFI_PIE = { |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 48 | com.android.internal.R.drawable.ic_wifi_signal_0, |
| 49 | com.android.internal.R.drawable.ic_wifi_signal_1, |
| 50 | com.android.internal.R.drawable.ic_wifi_signal_2, |
| 51 | com.android.internal.R.drawable.ic_wifi_signal_3, |
| 52 | com.android.internal.R.drawable.ic_wifi_signal_4 |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 55 | public static void updateLocationEnabled(Context context, boolean enabled, int userId, |
| 56 | int source) { |
| 57 | Settings.Secure.putIntForUser( |
| 58 | context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source, |
| 59 | userId); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 60 | Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION); |
| 61 | |
| 62 | final int oldMode = Settings.Secure.getIntForUser(context.getContentResolver(), |
| 63 | Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, userId); |
| 64 | final int newMode = enabled |
| 65 | ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY |
| 66 | : Settings.Secure.LOCATION_MODE_OFF; |
| 67 | intent.putExtra(CURRENT_MODE_KEY, oldMode); |
| 68 | intent.putExtra(NEW_MODE_KEY, newMode); |
| 69 | context.sendBroadcastAsUser( |
| 70 | intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 71 | LocationManager locationManager = |
| 72 | (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); |
hjchangliao | 2da48f8 | 2018-04-27 11:30:55 +0800 | [diff] [blame] | 73 | locationManager.setLocationEnabledForUser(enabled, UserHandle.of(userId)); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 76 | public static boolean updateLocationMode(Context context, int oldMode, int newMode, int userId, |
| 77 | int source) { |
| 78 | Settings.Secure.putIntForUser( |
| 79 | context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source, |
| 80 | userId); |
Maggie | fab2e2c | 2017-11-21 11:57:30 -0800 | [diff] [blame] | 81 | Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION); |
| 82 | intent.putExtra(CURRENT_MODE_KEY, oldMode); |
| 83 | intent.putExtra(NEW_MODE_KEY, newMode); |
| 84 | context.sendBroadcastAsUser( |
| 85 | intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 86 | return Settings.Secure.putIntForUser( |
| 87 | context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode, userId); |
| 88 | } |
| 89 | |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Return string resource that best describes combination of tethering |
| 92 | * options available on this device. |
| 93 | */ |
| 94 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 95 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 96 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 97 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 98 | |
| 99 | boolean usbAvailable = usbRegexs.length != 0; |
| 100 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 101 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 102 | |
| 103 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 104 | return R.string.tether_settings_title_all; |
| 105 | } else if (wifiAvailable && usbAvailable) { |
| 106 | return R.string.tether_settings_title_all; |
| 107 | } else if (wifiAvailable && bluetoothAvailable) { |
| 108 | return R.string.tether_settings_title_all; |
| 109 | } else if (wifiAvailable) { |
| 110 | return R.string.tether_settings_title_wifi; |
| 111 | } else if (usbAvailable && bluetoothAvailable) { |
| 112 | return R.string.tether_settings_title_usb_bluetooth; |
| 113 | } else if (usbAvailable) { |
| 114 | return R.string.tether_settings_title_usb; |
| 115 | } else { |
| 116 | return R.string.tether_settings_title_bluetooth; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a label for the user, in the form of "User: user name" or "Work profile". |
| 122 | */ |
| 123 | public static String getUserLabel(Context context, UserInfo info) { |
| 124 | String name = info != null ? info.name : null; |
| 125 | if (info.isManagedProfile()) { |
| 126 | // We use predefined values for managed profiles |
| 127 | return context.getString(R.string.managed_user_title); |
| 128 | } else if (info.isGuest()) { |
| 129 | name = context.getString(R.string.user_guest); |
| 130 | } |
| 131 | if (name == null && info != null) { |
| 132 | name = Integer.toString(info.id); |
| 133 | } else if (info == null) { |
| 134 | name = context.getString(R.string.unknown); |
| 135 | } |
| 136 | return context.getResources().getString(R.string.running_process_item_user_label, name); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns a circular icon for a user. |
| 141 | */ |
Daniel Nishi | f26dbd8 | 2017-08-15 15:25:39 -0700 | [diff] [blame] | 142 | public static Drawable getUserIcon(Context context, UserManager um, UserInfo user) { |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 143 | final int iconSize = UserIconDrawable.getSizeForList(context); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 144 | if (user.isManagedProfile()) { |
Tony Mak | 34b5bd8 | 2018-03-06 11:24:51 +0000 | [diff] [blame] | 145 | Drawable drawable = UserIconDrawable.getManagedUserDrawable(context); |
Daniel Nishi | 07d0f59 | 2017-08-21 15:50:11 -0700 | [diff] [blame] | 146 | drawable.setBounds(0, 0, iconSize, iconSize); |
| 147 | return drawable; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 148 | } |
| 149 | if (user.iconPath != null) { |
| 150 | Bitmap icon = um.getUserIcon(user.id); |
| 151 | if (icon != null) { |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 152 | return new UserIconDrawable(iconSize).setIcon(icon).bake(); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 155 | return new UserIconDrawable(iconSize).setIconDrawable( |
Tony Mak | 213955e | 2017-11-23 16:57:08 +0800 | [diff] [blame] | 156 | UserIcons.getDefaultUserIcon(context.getResources(), user.id, /* light= */ false)) |
| 157 | .bake(); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 158 | } |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 159 | |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 160 | /** Formats a double from 0.0..100.0 with an option to round **/ |
| 161 | public static String formatPercentage(double percentage, boolean round) { |
| 162 | final int localPercentage = round ? Math.round((float) percentage) : (int) percentage; |
| 163 | return formatPercentage(localPercentage); |
| 164 | } |
| 165 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 166 | /** Formats the ratio of amount/total as a percentage. */ |
| 167 | public static String formatPercentage(long amount, long total) { |
| 168 | return formatPercentage(((double) amount) / total); |
| 169 | } |
| 170 | |
| 171 | /** Formats an integer from 0..100 as a percentage. */ |
| 172 | public static String formatPercentage(int percentage) { |
| 173 | return formatPercentage(((double) percentage) / 100.0); |
| 174 | } |
| 175 | |
| 176 | /** Formats a double from 0.0..1.0 as a percentage. */ |
Daniel Nishi | 3f94f68 | 2017-06-27 14:38:06 -0700 | [diff] [blame] | 177 | public static String formatPercentage(double percentage) { |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 178 | return NumberFormat.getPercentInstance().format(percentage); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | public static int getBatteryLevel(Intent batteryChangedIntent) { |
| 182 | int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); |
| 183 | int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); |
| 184 | return (level * 100) / scale; |
| 185 | } |
| 186 | |
| 187 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
Muyuan Li | 18d2b32 | 2016-03-31 13:53:56 -0700 | [diff] [blame] | 188 | int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 189 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 190 | String statusString; |
| 191 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
jackqdyulei | 806f78d | 2017-03-24 12:13:49 -0700 | [diff] [blame] | 192 | statusString = res.getString(R.string.battery_info_status_charging); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 193 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 194 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 195 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 196 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 197 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 198 | statusString = res.getString(R.string.battery_info_status_full); |
| 199 | } else { |
| 200 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 201 | } |
| 202 | |
| 203 | return statusString; |
| 204 | } |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 205 | |
Jason Chang | 2386a37 | 2018-04-24 16:05:30 +0800 | [diff] [blame] | 206 | public static ColorStateList getColorAccent(Context context) { |
| 207 | return getColorAttr(context, android.R.attr.colorAccent); |
| 208 | } |
| 209 | |
| 210 | public static ColorStateList getColorError(Context context) { |
| 211 | return getColorAttr(context, android.R.attr.colorError); |
| 212 | } |
| 213 | |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 214 | @ColorInt |
Jason Chang | 2386a37 | 2018-04-24 16:05:30 +0800 | [diff] [blame] | 215 | public static int getColorAccentDefaultColor(Context context) { |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 216 | return getColorAttrDefaultColor(context, android.R.attr.colorAccent); |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 217 | } |
| 218 | |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 219 | @ColorInt |
Jason Chang | 2386a37 | 2018-04-24 16:05:30 +0800 | [diff] [blame] | 220 | public static int getColorErrorDefaultColor(Context context) { |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 221 | return getColorAttrDefaultColor(context, android.R.attr.colorError); |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @ColorInt |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 225 | public static int getColorStateListDefaultColor(Context context, int resId) { |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 226 | final ColorStateList list = |
| 227 | context.getResources().getColorStateList(resId, context.getTheme()); |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 228 | return list.getDefaultColor(); |
| 229 | } |
| 230 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 231 | @ColorInt |
| 232 | public static int getDisabled(Context context, int inputColor) { |
| 233 | return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor); |
| 234 | } |
| 235 | |
| 236 | @ColorInt |
| 237 | public static int applyAlphaAttr(Context context, int attr, int inputColor) { |
| 238 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 239 | float alpha = ta.getFloat(0, 0); |
| 240 | ta.recycle(); |
Jason Monk | 824ffff | 2017-04-11 15:49:06 -0400 | [diff] [blame] | 241 | return applyAlpha(alpha, inputColor); |
| 242 | } |
| 243 | |
| 244 | @ColorInt |
| 245 | public static int applyAlpha(float alpha, int inputColor) { |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 246 | alpha *= Color.alpha(inputColor); |
| 247 | return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor), |
| 248 | Color.blue(inputColor)); |
| 249 | } |
| 250 | |
| 251 | @ColorInt |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 252 | public static int getColorAttrDefaultColor(Context context, int attr) { |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 253 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 254 | @ColorInt int colorAccent = ta.getColor(0, 0); |
| 255 | ta.recycle(); |
| 256 | return colorAccent; |
| 257 | } |
| 258 | |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 259 | public static ColorStateList getColorAttr(Context context, int attr) { |
| 260 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 261 | ColorStateList stateList = null; |
| 262 | try { |
| 263 | stateList = ta.getColorStateList(0); |
| 264 | } finally { |
| 265 | ta.recycle(); |
| 266 | } |
| 267 | return stateList; |
| 268 | } |
| 269 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 270 | public static int getThemeAttr(Context context, int attr) { |
| 271 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 272 | int theme = ta.getResourceId(0, 0); |
| 273 | ta.recycle(); |
| 274 | return theme; |
| 275 | } |
| 276 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 277 | public static Drawable getDrawable(Context context, int attr) { |
| 278 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 279 | Drawable drawable = ta.getDrawable(0); |
| 280 | ta.recycle(); |
| 281 | return drawable; |
| 282 | } |
| 283 | |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 284 | /** |
| 285 | * Determine whether a package is a "system package", in which case certain things (like |
| 286 | * disabling notifications or disabling the package altogether) should be disallowed. |
| 287 | */ |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 288 | public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) { |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 289 | if (sSystemSignature == null) { |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 290 | sSystemSignature = new Signature[]{getSystemSignature(pm)}; |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 291 | } |
Svet Ganov | 81fb0d4 | 2016-03-04 09:27:23 -0800 | [diff] [blame] | 292 | if (sPermissionControllerPackageName == null) { |
| 293 | sPermissionControllerPackageName = pm.getPermissionControllerPackageName(); |
| 294 | } |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 295 | if (sServicesSystemSharedLibPackageName == null) { |
| 296 | sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName(); |
| 297 | } |
| 298 | if (sSharedSystemSharedLibPackageName == null) { |
| 299 | sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName(); |
| 300 | } |
Svet Ganov | 81fb0d4 | 2016-03-04 09:27:23 -0800 | [diff] [blame] | 301 | return (sSystemSignature[0] != null |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 302 | && sSystemSignature[0].equals(getFirstSignature(pkg))) |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 303 | || pkg.packageName.equals(sPermissionControllerPackageName) |
| 304 | || pkg.packageName.equals(sServicesSystemSharedLibPackageName) |
Philip P. Moltmann | 545a58a | 2016-06-28 14:14:06 -0700 | [diff] [blame] | 305 | || pkg.packageName.equals(sSharedSystemSharedLibPackageName) |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 306 | || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME) |
| 307 | || isDeviceProvisioningPackage(resources, pkg.packageName); |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 310 | private static Signature getFirstSignature(PackageInfo pkg) { |
| 311 | if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) { |
| 312 | return pkg.signatures[0]; |
| 313 | } |
| 314 | return null; |
| 315 | } |
| 316 | |
| 317 | private static Signature getSystemSignature(PackageManager pm) { |
| 318 | try { |
| 319 | final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES); |
| 320 | return getFirstSignature(sys); |
| 321 | } catch (NameNotFoundException e) { |
| 322 | } |
| 323 | return null; |
| 324 | } |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * Returns {@code true} if the supplied package is the device provisioning app. Otherwise, |
| 328 | * returns {@code false}. |
| 329 | */ |
| 330 | public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) { |
| 331 | String deviceProvisioningPackage = resources.getString( |
| 332 | com.android.internal.R.string.config_deviceProvisioningPackage); |
| 333 | return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName); |
| 334 | } |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 335 | |
| 336 | /** |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 337 | * Returns the Wifi icon resource for a given RSSI level. |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 338 | * |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 339 | * @param level The number of bars to show (0-4) |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 340 | * @throws IllegalArgumentException if an invalid RSSI level is given. |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 341 | */ |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 342 | public static int getWifiIconResource(int level) { |
| 343 | if (level < 0 || level >= WIFI_PIE.length) { |
| 344 | throw new IllegalArgumentException("No Wifi icon found for level: " + level); |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 345 | } |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 346 | return WIFI_PIE[level]; |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 347 | } |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 348 | |
| 349 | public static int getDefaultStorageManagerDaysToRetain(Resources resources) { |
| 350 | int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT; |
| 351 | try { |
| 352 | defaultDays = |
| 353 | resources.getInteger( |
| 354 | com.android |
| 355 | .internal |
| 356 | .R |
| 357 | .integer |
| 358 | .config_storageManagerDaystoRetainDefault); |
| 359 | } catch (Resources.NotFoundException e) { |
| 360 | // We are likely in a test environment. |
| 361 | } |
| 362 | return defaultDays; |
| 363 | } |
Tony Mantler | 8892014 | 2017-10-20 15:03:56 -0700 | [diff] [blame] | 364 | |
| 365 | public static boolean isWifiOnly(Context context) { |
| 366 | return !context.getSystemService(ConnectivityManager.class) |
| 367 | .isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
| 368 | } |
Daniel Nishi | 84522e0 | 2018-01-23 18:27:22 -0800 | [diff] [blame] | 369 | |
| 370 | /** Returns if the automatic storage management feature is turned on or not. **/ |
| 371 | public static boolean isStorageManagerEnabled(Context context) { |
| 372 | boolean isDefaultOn; |
| 373 | try { |
| 374 | // Turn off by default if the opt-in was shown. |
| 375 | isDefaultOn = !SystemProperties.getBoolean(STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY, true); |
| 376 | } catch (Resources.NotFoundException e) { |
| 377 | isDefaultOn = false; |
| 378 | } |
| 379 | return Settings.Secure.getInt(context.getContentResolver(), |
| 380 | Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, |
| 381 | isDefaultOn ? 1 : 0) |
| 382 | != 0; |
| 383 | } |
timhypeng | 1b25c9a | 2018-04-23 18:08:47 +0800 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * get that {@link AudioManager#getMode()} is in ringing/call/communication(VoIP) status. |
| 387 | */ |
| 388 | public static boolean isAudioModeOngoingCall(Context context) { |
| 389 | final AudioManager audioManager = context.getSystemService(AudioManager.class); |
| 390 | final int audioMode = audioManager.getMode(); |
| 391 | return audioMode == AudioManager.MODE_RINGTONE |
| 392 | || audioMode == AudioManager.MODE_IN_CALL |
| 393 | || audioMode == AudioManager.MODE_IN_COMMUNICATION; |
| 394 | } |
SongFerngWang | 8cfb4d5 | 2018-08-21 18:04:29 +0800 | [diff] [blame] | 395 | |
| 396 | /** |
| 397 | * Return the service state is in-service or not. |
| 398 | * To make behavior consistent with SystemUI and Settings/AboutPhone/SIM status UI |
| 399 | * |
| 400 | * @param serviceState Service state. {@link ServiceState} |
| 401 | */ |
| 402 | public static boolean isInService(ServiceState serviceState) { |
| 403 | if (serviceState == null) { |
| 404 | return false; |
| 405 | } |
| 406 | int state = getCombinedServiceState(serviceState); |
| 407 | if (state == ServiceState.STATE_POWER_OFF |
| 408 | || state == ServiceState.STATE_OUT_OF_SERVICE |
| 409 | || state == ServiceState.STATE_EMERGENCY_ONLY) { |
| 410 | return false; |
| 411 | } else { |
| 412 | return true; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Return the combined service state. |
| 418 | * To make behavior consistent with SystemUI and Settings/AboutPhone/SIM status UI |
| 419 | * |
| 420 | * @param serviceState Service state. {@link ServiceState} |
| 421 | */ |
| 422 | public static int getCombinedServiceState(ServiceState serviceState) { |
| 423 | if (serviceState == null) { |
| 424 | return ServiceState.STATE_OUT_OF_SERVICE; |
| 425 | } |
| 426 | |
| 427 | // Consider the device to be in service if either voice or data |
| 428 | // service is available. Some SIM cards are marketed as data-only |
| 429 | // and do not support voice service, and on these SIM cards, we |
| 430 | // want to show signal bars for data service as well as the "no |
| 431 | // service" or "emergency calls only" text that indicates that voice |
| 432 | // is not available. |
| 433 | int state = serviceState.getState(); |
| 434 | int dataState = serviceState.getDataRegState(); |
| 435 | if (state == ServiceState.STATE_OUT_OF_SERVICE |
| 436 | || state == ServiceState.STATE_EMERGENCY_ONLY) { |
| 437 | if (dataState == ServiceState.STATE_IN_SERVICE) { |
| 438 | return ServiceState.STATE_IN_SERVICE; |
| 439 | } |
| 440 | } |
| 441 | return state; |
| 442 | } |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 443 | } |