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; |
| 15 | import android.graphics.BitmapFactory; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 16 | import android.graphics.Color; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 17 | import android.graphics.drawable.Drawable; |
| 18 | import android.net.ConnectivityManager; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 19 | import android.os.BatteryManager; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 20 | import android.os.UserManager; |
Philip P. Moltmann | 545a58a | 2016-06-28 14:14:06 -0700 | [diff] [blame] | 21 | import android.print.PrintManager; |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 22 | import android.provider.Settings; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 23 | |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 24 | import com.android.internal.util.UserIcons; |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 25 | import com.android.settingslib.drawable.UserIconDrawable; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 26 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 27 | import java.text.NumberFormat; |
| 28 | |
| 29 | public class Utils { |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 30 | private static Signature[] sSystemSignature; |
| 31 | private static String sPermissionControllerPackageName; |
| 32 | private static String sServicesSystemSharedLibPackageName; |
| 33 | private static String sSharedSystemSharedLibPackageName; |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 34 | |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 35 | static final int[] WIFI_PIE = { |
| 36 | com.android.internal.R.drawable.ic_wifi_signal_0, |
| 37 | com.android.internal.R.drawable.ic_wifi_signal_1, |
| 38 | com.android.internal.R.drawable.ic_wifi_signal_2, |
| 39 | com.android.internal.R.drawable.ic_wifi_signal_3, |
| 40 | com.android.internal.R.drawable.ic_wifi_signal_4 |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Return string resource that best describes combination of tethering |
| 45 | * options available on this device. |
| 46 | */ |
| 47 | public static int getTetheringLabel(ConnectivityManager cm) { |
| 48 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 49 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
| 50 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 51 | |
| 52 | boolean usbAvailable = usbRegexs.length != 0; |
| 53 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 54 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 55 | |
| 56 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 57 | return R.string.tether_settings_title_all; |
| 58 | } else if (wifiAvailable && usbAvailable) { |
| 59 | return R.string.tether_settings_title_all; |
| 60 | } else if (wifiAvailable && bluetoothAvailable) { |
| 61 | return R.string.tether_settings_title_all; |
| 62 | } else if (wifiAvailable) { |
| 63 | return R.string.tether_settings_title_wifi; |
| 64 | } else if (usbAvailable && bluetoothAvailable) { |
| 65 | return R.string.tether_settings_title_usb_bluetooth; |
| 66 | } else if (usbAvailable) { |
| 67 | return R.string.tether_settings_title_usb; |
| 68 | } else { |
| 69 | return R.string.tether_settings_title_bluetooth; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns a label for the user, in the form of "User: user name" or "Work profile". |
| 75 | */ |
| 76 | public static String getUserLabel(Context context, UserInfo info) { |
| 77 | String name = info != null ? info.name : null; |
| 78 | if (info.isManagedProfile()) { |
| 79 | // We use predefined values for managed profiles |
| 80 | return context.getString(R.string.managed_user_title); |
| 81 | } else if (info.isGuest()) { |
| 82 | name = context.getString(R.string.user_guest); |
| 83 | } |
| 84 | if (name == null && info != null) { |
| 85 | name = Integer.toString(info.id); |
| 86 | } else if (info == null) { |
| 87 | name = context.getString(R.string.unknown); |
| 88 | } |
| 89 | return context.getResources().getString(R.string.running_process_item_user_label, name); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns a circular icon for a user. |
| 94 | */ |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 95 | public static UserIconDrawable getUserIcon(Context context, UserManager um, UserInfo user) { |
| 96 | final int iconSize = UserIconDrawable.getSizeForList(context); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 97 | if (user.isManagedProfile()) { |
| 98 | // We use predefined values for managed profiles |
| 99 | Bitmap b = BitmapFactory.decodeResource(context.getResources(), |
| 100 | com.android.internal.R.drawable.ic_corp_icon); |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 101 | return new UserIconDrawable(iconSize).setIcon(b).bake(); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 102 | } |
| 103 | if (user.iconPath != null) { |
| 104 | Bitmap icon = um.getUserIcon(user.id); |
| 105 | if (icon != null) { |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 106 | return new UserIconDrawable(iconSize).setIcon(icon).bake(); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 109 | return new UserIconDrawable(iconSize).setIconDrawable( |
| 110 | UserIcons.getDefaultUserIcon(user.id, /* light= */ false)).bake(); |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 111 | } |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 112 | |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 113 | /** Formats a double from 0.0..100.0 with an option to round **/ |
| 114 | public static String formatPercentage(double percentage, boolean round) { |
| 115 | final int localPercentage = round ? Math.round((float) percentage) : (int) percentage; |
| 116 | return formatPercentage(localPercentage); |
| 117 | } |
| 118 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 119 | /** Formats the ratio of amount/total as a percentage. */ |
| 120 | public static String formatPercentage(long amount, long total) { |
| 121 | return formatPercentage(((double) amount) / total); |
| 122 | } |
| 123 | |
| 124 | /** Formats an integer from 0..100 as a percentage. */ |
| 125 | public static String formatPercentage(int percentage) { |
| 126 | return formatPercentage(((double) percentage) / 100.0); |
| 127 | } |
| 128 | |
| 129 | /** Formats a double from 0.0..1.0 as a percentage. */ |
Daniel Nishi | 3f94f68 | 2017-06-27 14:38:06 -0700 | [diff] [blame] | 130 | public static String formatPercentage(double percentage) { |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 131 | return NumberFormat.getPercentInstance().format(percentage); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public static int getBatteryLevel(Intent batteryChangedIntent) { |
| 135 | int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); |
| 136 | int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); |
| 137 | return (level * 100) / scale; |
| 138 | } |
| 139 | |
| 140 | public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { |
Muyuan Li | 18d2b32 | 2016-03-31 13:53:56 -0700 | [diff] [blame] | 141 | int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 142 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 143 | String statusString; |
| 144 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
jackqdyulei | 806f78d | 2017-03-24 12:13:49 -0700 | [diff] [blame] | 145 | statusString = res.getString(R.string.battery_info_status_charging); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 146 | } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
| 147 | statusString = res.getString(R.string.battery_info_status_discharging); |
| 148 | } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { |
| 149 | statusString = res.getString(R.string.battery_info_status_not_charging); |
| 150 | } else if (status == BatteryManager.BATTERY_STATUS_FULL) { |
| 151 | statusString = res.getString(R.string.battery_info_status_full); |
| 152 | } else { |
| 153 | statusString = res.getString(R.string.battery_info_status_unknown); |
| 154 | } |
| 155 | |
| 156 | return statusString; |
| 157 | } |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 158 | |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 159 | @ColorInt |
| 160 | public static int getColorAccent(Context context) { |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 161 | return getColorAttr(context, android.R.attr.colorAccent); |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 162 | } |
| 163 | |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 164 | @ColorInt |
| 165 | public static int getColorError(Context context) { |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 166 | return getColorAttr(context, android.R.attr.colorError); |
jackqdyulei | b68fd7a | 2017-01-04 15:43:26 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @ColorInt |
| 170 | public static int getDefaultColor(Context context, int resId) { |
| 171 | final ColorStateList list = |
| 172 | context.getResources().getColorStateList(resId, context.getTheme()); |
| 173 | |
| 174 | return list.getDefaultColor(); |
| 175 | } |
| 176 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 177 | @ColorInt |
| 178 | public static int getDisabled(Context context, int inputColor) { |
| 179 | return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor); |
| 180 | } |
| 181 | |
| 182 | @ColorInt |
| 183 | public static int applyAlphaAttr(Context context, int attr, int inputColor) { |
| 184 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 185 | float alpha = ta.getFloat(0, 0); |
| 186 | ta.recycle(); |
Jason Monk | 824ffff | 2017-04-11 15:49:06 -0400 | [diff] [blame] | 187 | return applyAlpha(alpha, inputColor); |
| 188 | } |
| 189 | |
| 190 | @ColorInt |
| 191 | public static int applyAlpha(float alpha, int inputColor) { |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 192 | alpha *= Color.alpha(inputColor); |
| 193 | return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor), |
| 194 | Color.blue(inputColor)); |
| 195 | } |
| 196 | |
| 197 | @ColorInt |
| 198 | public static int getColorAttr(Context context, int attr) { |
| 199 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 200 | @ColorInt int colorAccent = ta.getColor(0, 0); |
| 201 | ta.recycle(); |
| 202 | return colorAccent; |
| 203 | } |
| 204 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 205 | public static int getThemeAttr(Context context, int attr) { |
| 206 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 207 | int theme = ta.getResourceId(0, 0); |
| 208 | ta.recycle(); |
| 209 | return theme; |
| 210 | } |
| 211 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 212 | public static Drawable getDrawable(Context context, int attr) { |
| 213 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 214 | Drawable drawable = ta.getDrawable(0); |
| 215 | ta.recycle(); |
| 216 | return drawable; |
| 217 | } |
| 218 | |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 219 | /** |
| 220 | * Determine whether a package is a "system package", in which case certain things (like |
| 221 | * disabling notifications or disabling the package altogether) should be disallowed. |
| 222 | */ |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 223 | public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) { |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 224 | if (sSystemSignature == null) { |
| 225 | sSystemSignature = new Signature[]{ getSystemSignature(pm) }; |
| 226 | } |
Svet Ganov | 81fb0d4 | 2016-03-04 09:27:23 -0800 | [diff] [blame] | 227 | if (sPermissionControllerPackageName == null) { |
| 228 | sPermissionControllerPackageName = pm.getPermissionControllerPackageName(); |
| 229 | } |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 230 | if (sServicesSystemSharedLibPackageName == null) { |
| 231 | sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName(); |
| 232 | } |
| 233 | if (sSharedSystemSharedLibPackageName == null) { |
| 234 | sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName(); |
| 235 | } |
Svet Ganov | 81fb0d4 | 2016-03-04 09:27:23 -0800 | [diff] [blame] | 236 | return (sSystemSignature[0] != null |
| 237 | && sSystemSignature[0].equals(getFirstSignature(pkg))) |
Svetoslav Ganov | a9c2500 | 2016-04-13 19:25:56 -0700 | [diff] [blame] | 238 | || pkg.packageName.equals(sPermissionControllerPackageName) |
| 239 | || pkg.packageName.equals(sServicesSystemSharedLibPackageName) |
Philip P. Moltmann | 545a58a | 2016-06-28 14:14:06 -0700 | [diff] [blame] | 240 | || pkg.packageName.equals(sSharedSystemSharedLibPackageName) |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 241 | || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME) |
| 242 | || isDeviceProvisioningPackage(resources, pkg.packageName); |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Julia Reynolds | 21c3054 | 2016-01-15 15:00:02 -0500 | [diff] [blame] | 245 | private static Signature getFirstSignature(PackageInfo pkg) { |
| 246 | if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) { |
| 247 | return pkg.signatures[0]; |
| 248 | } |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | private static Signature getSystemSignature(PackageManager pm) { |
| 253 | try { |
| 254 | final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES); |
| 255 | return getFirstSignature(sys); |
| 256 | } catch (NameNotFoundException e) { |
| 257 | } |
| 258 | return null; |
| 259 | } |
Tony Mak | bfba9d4 | 2016-07-14 15:29:44 +0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Returns {@code true} if the supplied package is the device provisioning app. Otherwise, |
| 263 | * returns {@code false}. |
| 264 | */ |
| 265 | public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) { |
| 266 | String deviceProvisioningPackage = resources.getString( |
| 267 | com.android.internal.R.string.config_deviceProvisioningPackage); |
| 268 | return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName); |
| 269 | } |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 270 | |
| 271 | /** |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 272 | * Returns the Wifi icon resource for a given RSSI level. |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 273 | * |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 274 | * @param level The number of bars to show (0-4) |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 275 | * |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 276 | * @throws IllegalArgumentException if an invalid RSSI level is given. |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 277 | */ |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 278 | public static int getWifiIconResource(int level) { |
| 279 | if (level < 0 || level >= WIFI_PIE.length) { |
| 280 | throw new IllegalArgumentException("No Wifi icon found for level: " + level); |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 281 | } |
Eric Schwarzenbach | 44268e3 | 2017-08-04 16:38:07 -0700 | [diff] [blame] | 282 | return WIFI_PIE[level]; |
Sundeep Ghuman | f84e057 | 2017-01-10 13:43:03 -0800 | [diff] [blame] | 283 | } |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 284 | |
| 285 | public static int getDefaultStorageManagerDaysToRetain(Resources resources) { |
| 286 | int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT; |
| 287 | try { |
| 288 | defaultDays = |
| 289 | resources.getInteger( |
| 290 | com.android |
| 291 | .internal |
| 292 | .R |
| 293 | .integer |
| 294 | .config_storageManagerDaystoRetainDefault); |
| 295 | } catch (Resources.NotFoundException e) { |
| 296 | // We are likely in a test environment. |
| 297 | } |
| 298 | return defaultDays; |
| 299 | } |
Wei Liu | 30275c1 | 2015-08-24 17:35:49 -0700 | [diff] [blame] | 300 | } |