blob: b3d2b6677f0a24f0f06d85a94562b521594bc3e5 [file] [log] [blame]
Wei Liu30275c12015-08-24 17:35:49 -07001package com.android.settingslib;
2
Andrew Sapperstein5c373442016-06-12 13:17:16 -07003import android.annotation.ColorInt;
Wei Liu30275c12015-08-24 17:35:49 -07004import android.content.Context;
Jason Monkc06fbb12016-01-08 14:12:18 -05005import android.content.Intent;
Julia Reynolds21c30542016-01-15 15:00:02 -05006import android.content.pm.PackageInfo;
7import android.content.pm.PackageManager;
8import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds21c30542016-01-15 15:00:02 -05009import android.content.pm.Signature;
Fan Zhang73b161d2017-02-08 11:46:54 -080010import android.content.pm.UserInfo;
jackqdyuleib68fd7a2017-01-04 15:43:26 -080011import android.content.res.ColorStateList;
Jason Monkc06fbb12016-01-08 14:12:18 -050012import android.content.res.Resources;
Andrew Sapperstein5c373442016-06-12 13:17:16 -070013import android.content.res.TypedArray;
Wei Liu30275c12015-08-24 17:35:49 -070014import android.graphics.Bitmap;
15import android.graphics.BitmapFactory;
Jason Monk32508852017-01-18 09:17:13 -050016import android.graphics.Color;
Wei Liu30275c12015-08-24 17:35:49 -070017import android.graphics.drawable.Drawable;
18import android.net.ConnectivityManager;
Jason Monkc06fbb12016-01-08 14:12:18 -050019import android.os.BatteryManager;
Wei Liu30275c12015-08-24 17:35:49 -070020import android.os.UserManager;
Philip P. Moltmann545a58a2016-06-28 14:14:06 -070021import android.print.PrintManager;
Daniel Nishi53982802017-05-23 15:54:34 -070022import android.provider.Settings;
Sundeep Ghumand57f3242017-01-13 15:31:48 -080023
Wei Liu30275c12015-08-24 17:35:49 -070024import com.android.internal.util.UserIcons;
Evan Roskyaa7f51f2016-03-16 13:15:53 -070025import com.android.settingslib.drawable.UserIconDrawable;
Wei Liu30275c12015-08-24 17:35:49 -070026
Jason Monkc06fbb12016-01-08 14:12:18 -050027import java.text.NumberFormat;
28
29public class Utils {
Svetoslav Ganova9c25002016-04-13 19:25:56 -070030 private static Signature[] sSystemSignature;
31 private static String sPermissionControllerPackageName;
32 private static String sServicesSystemSharedLibPackageName;
33 private static String sSharedSystemSharedLibPackageName;
Wei Liu30275c12015-08-24 17:35:49 -070034
Eric Schwarzenbach44268e32017-08-04 16:38:07 -070035 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 Ghumanf84e0572017-01-10 13:43:03 -080041 };
42
Wei Liu30275c12015-08-24 17:35:49 -070043 /**
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 Roskyaa7f51f2016-03-16 13:15:53 -070095 public static UserIconDrawable getUserIcon(Context context, UserManager um, UserInfo user) {
96 final int iconSize = UserIconDrawable.getSizeForList(context);
Wei Liu30275c12015-08-24 17:35:49 -070097 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 Roskyaa7f51f2016-03-16 13:15:53 -0700101 return new UserIconDrawable(iconSize).setIcon(b).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700102 }
103 if (user.iconPath != null) {
104 Bitmap icon = um.getUserIcon(user.id);
105 if (icon != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700106 return new UserIconDrawable(iconSize).setIcon(icon).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700107 }
108 }
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700109 return new UserIconDrawable(iconSize).setIconDrawable(
110 UserIcons.getDefaultUserIcon(user.id, /* light= */ false)).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700111 }
Jason Monkc06fbb12016-01-08 14:12:18 -0500112
jackqdyulei5dc1f362017-02-17 14:41:05 -0800113 /** 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 Monkc06fbb12016-01-08 14:12:18 -0500119 /** 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 Nishi3f94f682017-06-27 14:38:06 -0700130 public static String formatPercentage(double percentage) {
jackqdyulei5dc1f362017-02-17 14:41:05 -0800131 return NumberFormat.getPercentInstance().format(percentage);
Jason Monkc06fbb12016-01-08 14:12:18 -0500132 }
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 Li18d2b322016-03-31 13:53:56 -0700141 int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
Jason Monkc06fbb12016-01-08 14:12:18 -0500142 BatteryManager.BATTERY_STATUS_UNKNOWN);
143 String statusString;
144 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
jackqdyulei806f78d2017-03-24 12:13:49 -0700145 statusString = res.getString(R.string.battery_info_status_charging);
Jason Monkc06fbb12016-01-08 14:12:18 -0500146 } 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 Reynolds21c30542016-01-15 15:00:02 -0500158
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700159 @ColorInt
160 public static int getColorAccent(Context context) {
Jason Monk32508852017-01-18 09:17:13 -0500161 return getColorAttr(context, android.R.attr.colorAccent);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700162 }
163
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800164 @ColorInt
165 public static int getColorError(Context context) {
Jason Monk58be7a62017-02-01 20:17:51 -0500166 return getColorAttr(context, android.R.attr.colorError);
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800167 }
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 Monk32508852017-01-18 09:17:13 -0500177 @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 Monk824ffff2017-04-11 15:49:06 -0400187 return applyAlpha(alpha, inputColor);
188 }
189
190 @ColorInt
191 public static int applyAlpha(float alpha, int inputColor) {
Jason Monk32508852017-01-18 09:17:13 -0500192 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 Monk9a376bc2017-05-10 09:52:10 -0400205 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 Monk790442e2017-02-13 17:49:39 -0500212 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 Reynolds21c30542016-01-15 15:00:02 -0500219 /**
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 Makbfba9d42016-07-14 15:29:44 +0800223 public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500224 if (sSystemSignature == null) {
225 sSystemSignature = new Signature[]{ getSystemSignature(pm) };
226 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800227 if (sPermissionControllerPackageName == null) {
228 sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
229 }
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700230 if (sServicesSystemSharedLibPackageName == null) {
231 sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
232 }
233 if (sSharedSystemSharedLibPackageName == null) {
234 sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
235 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800236 return (sSystemSignature[0] != null
237 && sSystemSignature[0].equals(getFirstSignature(pkg)))
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700238 || pkg.packageName.equals(sPermissionControllerPackageName)
239 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
Philip P. Moltmann545a58a2016-06-28 14:14:06 -0700240 || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
Tony Makbfba9d42016-07-14 15:29:44 +0800241 || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
242 || isDeviceProvisioningPackage(resources, pkg.packageName);
Julia Reynolds21c30542016-01-15 15:00:02 -0500243 }
244
Julia Reynolds21c30542016-01-15 15:00:02 -0500245 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 Makbfba9d42016-07-14 15:29:44 +0800260
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 Ghumanf84e0572017-01-10 13:43:03 -0800270
271 /**
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700272 * Returns the Wifi icon resource for a given RSSI level.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800273 *
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800274 * @param level The number of bars to show (0-4)
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800275 *
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700276 * @throws IllegalArgumentException if an invalid RSSI level is given.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800277 */
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700278 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 Ghumanf84e0572017-01-10 13:43:03 -0800281 }
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700282 return WIFI_PIE[level];
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800283 }
Daniel Nishi53982802017-05-23 15:54:34 -0700284
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 Liu30275c12015-08-24 17:35:49 -0700300}