blob: dee5a93d8eea28696ff51ac40f6c1d86820158ae [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;
Sundeep Ghumanf84e0572017-01-10 13:43:03 -080018import android.graphics.drawable.LayerDrawable;
Wei Liu30275c12015-08-24 17:35:49 -070019import android.net.ConnectivityManager;
Sundeep Ghuman699deaf2017-02-13 15:32:13 -080020import android.net.NetworkBadging;
Jason Monkc06fbb12016-01-08 14:12:18 -050021import android.os.BatteryManager;
Wei Liu30275c12015-08-24 17:35:49 -070022import android.os.UserManager;
Philip P. Moltmann545a58a2016-06-28 14:14:06 -070023import android.print.PrintManager;
Daniel Nishi53982802017-05-23 15:54:34 -070024import android.provider.Settings;
Sundeep Ghumand57f3242017-01-13 15:31:48 -080025import android.view.View;
26
Wei Liu30275c12015-08-24 17:35:49 -070027import com.android.internal.util.UserIcons;
Evan Roskyaa7f51f2016-03-16 13:15:53 -070028import com.android.settingslib.drawable.UserIconDrawable;
Wei Liu30275c12015-08-24 17:35:49 -070029
Jason Monkc06fbb12016-01-08 14:12:18 -050030import java.text.NumberFormat;
31
32public class Utils {
Svetoslav Ganova9c25002016-04-13 19:25:56 -070033 private static Signature[] sSystemSignature;
34 private static String sPermissionControllerPackageName;
35 private static String sServicesSystemSharedLibPackageName;
36 private static String sSharedSystemSharedLibPackageName;
Wei Liu30275c12015-08-24 17:35:49 -070037
Sundeep Ghuman9d10a3c2017-06-16 00:05:16 +000038 static final int[] WIFI_PIE_FOR_BADGING = {
Sundeep Ghumanf84e0572017-01-10 13:43:03 -080039 com.android.internal.R.drawable.ic_signal_wifi_badged_0_bars,
40 com.android.internal.R.drawable.ic_signal_wifi_badged_1_bar,
41 com.android.internal.R.drawable.ic_signal_wifi_badged_2_bars,
42 com.android.internal.R.drawable.ic_signal_wifi_badged_3_bars,
43 com.android.internal.R.drawable.ic_signal_wifi_badged_4_bars
44 };
45
Wei Liu30275c12015-08-24 17:35:49 -070046 /**
47 * Return string resource that best describes combination of tethering
48 * options available on this device.
49 */
50 public static int getTetheringLabel(ConnectivityManager cm) {
51 String[] usbRegexs = cm.getTetherableUsbRegexs();
52 String[] wifiRegexs = cm.getTetherableWifiRegexs();
53 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
54
55 boolean usbAvailable = usbRegexs.length != 0;
56 boolean wifiAvailable = wifiRegexs.length != 0;
57 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
58
59 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
60 return R.string.tether_settings_title_all;
61 } else if (wifiAvailable && usbAvailable) {
62 return R.string.tether_settings_title_all;
63 } else if (wifiAvailable && bluetoothAvailable) {
64 return R.string.tether_settings_title_all;
65 } else if (wifiAvailable) {
66 return R.string.tether_settings_title_wifi;
67 } else if (usbAvailable && bluetoothAvailable) {
68 return R.string.tether_settings_title_usb_bluetooth;
69 } else if (usbAvailable) {
70 return R.string.tether_settings_title_usb;
71 } else {
72 return R.string.tether_settings_title_bluetooth;
73 }
74 }
75
76 /**
77 * Returns a label for the user, in the form of "User: user name" or "Work profile".
78 */
79 public static String getUserLabel(Context context, UserInfo info) {
80 String name = info != null ? info.name : null;
81 if (info.isManagedProfile()) {
82 // We use predefined values for managed profiles
83 return context.getString(R.string.managed_user_title);
84 } else if (info.isGuest()) {
85 name = context.getString(R.string.user_guest);
86 }
87 if (name == null && info != null) {
88 name = Integer.toString(info.id);
89 } else if (info == null) {
90 name = context.getString(R.string.unknown);
91 }
92 return context.getResources().getString(R.string.running_process_item_user_label, name);
93 }
94
95 /**
96 * Returns a circular icon for a user.
97 */
Evan Roskyaa7f51f2016-03-16 13:15:53 -070098 public static UserIconDrawable getUserIcon(Context context, UserManager um, UserInfo user) {
99 final int iconSize = UserIconDrawable.getSizeForList(context);
Wei Liu30275c12015-08-24 17:35:49 -0700100 if (user.isManagedProfile()) {
101 // We use predefined values for managed profiles
102 Bitmap b = BitmapFactory.decodeResource(context.getResources(),
103 com.android.internal.R.drawable.ic_corp_icon);
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700104 return new UserIconDrawable(iconSize).setIcon(b).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700105 }
106 if (user.iconPath != null) {
107 Bitmap icon = um.getUserIcon(user.id);
108 if (icon != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700109 return new UserIconDrawable(iconSize).setIcon(icon).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700110 }
111 }
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700112 return new UserIconDrawable(iconSize).setIconDrawable(
113 UserIcons.getDefaultUserIcon(user.id, /* light= */ false)).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700114 }
Jason Monkc06fbb12016-01-08 14:12:18 -0500115
jackqdyulei5dc1f362017-02-17 14:41:05 -0800116 /** Formats a double from 0.0..100.0 with an option to round **/
117 public static String formatPercentage(double percentage, boolean round) {
118 final int localPercentage = round ? Math.round((float) percentage) : (int) percentage;
119 return formatPercentage(localPercentage);
120 }
121
Jason Monkc06fbb12016-01-08 14:12:18 -0500122 /** Formats the ratio of amount/total as a percentage. */
123 public static String formatPercentage(long amount, long total) {
124 return formatPercentage(((double) amount) / total);
125 }
126
127 /** Formats an integer from 0..100 as a percentage. */
128 public static String formatPercentage(int percentage) {
129 return formatPercentage(((double) percentage) / 100.0);
130 }
131
132 /** Formats a double from 0.0..1.0 as a percentage. */
Daniel Nishi3f94f682017-06-27 14:38:06 -0700133 public static String formatPercentage(double percentage) {
jackqdyulei5dc1f362017-02-17 14:41:05 -0800134 return NumberFormat.getPercentInstance().format(percentage);
Jason Monkc06fbb12016-01-08 14:12:18 -0500135 }
136
137 public static int getBatteryLevel(Intent batteryChangedIntent) {
138 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
139 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
140 return (level * 100) / scale;
141 }
142
143 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
Muyuan Li18d2b322016-03-31 13:53:56 -0700144 int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
Jason Monkc06fbb12016-01-08 14:12:18 -0500145 BatteryManager.BATTERY_STATUS_UNKNOWN);
146 String statusString;
147 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
jackqdyulei806f78d2017-03-24 12:13:49 -0700148 statusString = res.getString(R.string.battery_info_status_charging);
Jason Monkc06fbb12016-01-08 14:12:18 -0500149 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
150 statusString = res.getString(R.string.battery_info_status_discharging);
151 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
152 statusString = res.getString(R.string.battery_info_status_not_charging);
153 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
154 statusString = res.getString(R.string.battery_info_status_full);
155 } else {
156 statusString = res.getString(R.string.battery_info_status_unknown);
157 }
158
159 return statusString;
160 }
Julia Reynolds21c30542016-01-15 15:00:02 -0500161
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700162 @ColorInt
163 public static int getColorAccent(Context context) {
Jason Monk32508852017-01-18 09:17:13 -0500164 return getColorAttr(context, android.R.attr.colorAccent);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700165 }
166
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800167 @ColorInt
168 public static int getColorError(Context context) {
Jason Monk58be7a62017-02-01 20:17:51 -0500169 return getColorAttr(context, android.R.attr.colorError);
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800170 }
171
172 @ColorInt
173 public static int getDefaultColor(Context context, int resId) {
174 final ColorStateList list =
175 context.getResources().getColorStateList(resId, context.getTheme());
176
177 return list.getDefaultColor();
178 }
179
Jason Monk32508852017-01-18 09:17:13 -0500180 @ColorInt
181 public static int getDisabled(Context context, int inputColor) {
182 return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor);
183 }
184
185 @ColorInt
186 public static int applyAlphaAttr(Context context, int attr, int inputColor) {
187 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
188 float alpha = ta.getFloat(0, 0);
189 ta.recycle();
Jason Monk824ffff2017-04-11 15:49:06 -0400190 return applyAlpha(alpha, inputColor);
191 }
192
193 @ColorInt
194 public static int applyAlpha(float alpha, int inputColor) {
Jason Monk32508852017-01-18 09:17:13 -0500195 alpha *= Color.alpha(inputColor);
196 return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor),
197 Color.blue(inputColor));
198 }
199
200 @ColorInt
201 public static int getColorAttr(Context context, int attr) {
202 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
203 @ColorInt int colorAccent = ta.getColor(0, 0);
204 ta.recycle();
205 return colorAccent;
206 }
207
Jason Monk9a376bc2017-05-10 09:52:10 -0400208 public static int getThemeAttr(Context context, int attr) {
209 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
210 int theme = ta.getResourceId(0, 0);
211 ta.recycle();
212 return theme;
213 }
214
Jason Monk790442e2017-02-13 17:49:39 -0500215 public static Drawable getDrawable(Context context, int attr) {
216 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
217 Drawable drawable = ta.getDrawable(0);
218 ta.recycle();
219 return drawable;
220 }
221
Julia Reynolds21c30542016-01-15 15:00:02 -0500222 /**
223 * Determine whether a package is a "system package", in which case certain things (like
224 * disabling notifications or disabling the package altogether) should be disallowed.
225 */
Tony Makbfba9d42016-07-14 15:29:44 +0800226 public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500227 if (sSystemSignature == null) {
228 sSystemSignature = new Signature[]{ getSystemSignature(pm) };
229 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800230 if (sPermissionControllerPackageName == null) {
231 sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
232 }
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700233 if (sServicesSystemSharedLibPackageName == null) {
234 sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
235 }
236 if (sSharedSystemSharedLibPackageName == null) {
237 sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
238 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800239 return (sSystemSignature[0] != null
240 && sSystemSignature[0].equals(getFirstSignature(pkg)))
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700241 || pkg.packageName.equals(sPermissionControllerPackageName)
242 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
Philip P. Moltmann545a58a2016-06-28 14:14:06 -0700243 || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
Tony Makbfba9d42016-07-14 15:29:44 +0800244 || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
245 || isDeviceProvisioningPackage(resources, pkg.packageName);
Julia Reynolds21c30542016-01-15 15:00:02 -0500246 }
247
Julia Reynolds21c30542016-01-15 15:00:02 -0500248 private static Signature getFirstSignature(PackageInfo pkg) {
249 if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) {
250 return pkg.signatures[0];
251 }
252 return null;
253 }
254
255 private static Signature getSystemSignature(PackageManager pm) {
256 try {
257 final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
258 return getFirstSignature(sys);
259 } catch (NameNotFoundException e) {
260 }
261 return null;
262 }
Tony Makbfba9d42016-07-14 15:29:44 +0800263
264 /**
265 * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
266 * returns {@code false}.
267 */
268 public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) {
269 String deviceProvisioningPackage = resources.getString(
270 com.android.internal.R.string.config_deviceProvisioningPackage);
271 return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
272 }
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800273
274 /**
275 * Returns a badged Wifi icon drawable.
276 *
277 * <p>The first layer contains the Wifi pie and the second layer contains the badge. Callers
278 * should set the drawable to the appropriate size and tint color.
279 *
280 * @param context The caller's context (must have access to internal resources)
281 * @param level The number of bars to show (0-4)
282 * @param badge The badge enum {@see android.net.ScoredNetwork}
283 *
284 * @throws IllegalArgumentException if an invalid badge enum is given
285 *
286 * @deprecated TODO(sghuman): Finalize the form of this method and then move it to a new
287 * location.
288 */
289 public static LayerDrawable getBadgedWifiIcon(Context context, int level, int badge) {
290 return new LayerDrawable(
291 new Drawable[] {
292 context.getDrawable(WIFI_PIE_FOR_BADGING[level]),
293 context.getDrawable(getWifiBadgeResource(badge))
294 });
295 }
296
Sundeep Ghuman9d10a3c2017-06-16 00:05:16 +0000297 private static int getWifiBadgeResource(int badge) {
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800298 switch (badge) {
Sundeep Ghuman699deaf2017-02-13 15:32:13 -0800299 case NetworkBadging.BADGING_NONE:
Sundeep Ghumand57f3242017-01-13 15:31:48 -0800300 return View.NO_ID;
Sundeep Ghuman699deaf2017-02-13 15:32:13 -0800301 case NetworkBadging.BADGING_SD:
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800302 return com.android.internal.R.drawable.ic_signal_wifi_badged_sd;
Sundeep Ghuman699deaf2017-02-13 15:32:13 -0800303 case NetworkBadging.BADGING_HD:
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800304 return com.android.internal.R.drawable.ic_signal_wifi_badged_hd;
Sundeep Ghuman699deaf2017-02-13 15:32:13 -0800305 case NetworkBadging.BADGING_4K:
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800306 return com.android.internal.R.drawable.ic_signal_wifi_badged_4k;
307 default:
308 throw new IllegalArgumentException(
309 "No badge resource found for badge value: " + badge);
310 }
311 }
Daniel Nishi53982802017-05-23 15:54:34 -0700312
313 public static int getDefaultStorageManagerDaysToRetain(Resources resources) {
314 int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT;
315 try {
316 defaultDays =
317 resources.getInteger(
318 com.android
319 .internal
320 .R
321 .integer
322 .config_storageManagerDaystoRetainDefault);
323 } catch (Resources.NotFoundException e) {
324 // We are likely in a test environment.
325 }
326 return defaultDays;
327 }
Wei Liu30275c12015-08-24 17:35:49 -0700328}