blob: d001e66acf1d829e319cf8c9ff04c9c0622165d0 [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;
Jason Monk32508852017-01-18 09:17:13 -050015import android.graphics.Color;
Wei Liu30275c12015-08-24 17:35:49 -070016import android.graphics.drawable.Drawable;
Maggiefab2e2c2017-11-21 11:57:30 -080017import android.location.LocationManager;
Wei Liu30275c12015-08-24 17:35:49 -070018import android.net.ConnectivityManager;
Jason Monkc06fbb12016-01-08 14:12:18 -050019import android.os.BatteryManager;
Maggiefab2e2c2017-11-21 11:57:30 -080020import android.os.UserHandle;
Wei Liu30275c12015-08-24 17:35:49 -070021import android.os.UserManager;
Philip P. Moltmann545a58a2016-06-28 14:14:06 -070022import android.print.PrintManager;
Daniel Nishi53982802017-05-23 15:54:34 -070023import android.provider.Settings;
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;
Maggieaa080f92018-01-04 15:35:11 -080026import com.android.settingslib.wrapper.LocationManagerWrapper;
Jason Monkc06fbb12016-01-08 14:12:18 -050027import java.text.NumberFormat;
28
29public class Utils {
Maggiefab2e2c2017-11-21 11:57:30 -080030
31 private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
32 private static final String NEW_MODE_KEY = "NEW_MODE";
33
Svetoslav Ganova9c25002016-04-13 19:25:56 -070034 private static Signature[] sSystemSignature;
35 private static String sPermissionControllerPackageName;
36 private static String sServicesSystemSharedLibPackageName;
37 private static String sSharedSystemSharedLibPackageName;
Wei Liu30275c12015-08-24 17:35:49 -070038
Eric Schwarzenbach44268e32017-08-04 16:38:07 -070039 static final int[] WIFI_PIE = {
40 com.android.internal.R.drawable.ic_wifi_signal_0,
41 com.android.internal.R.drawable.ic_wifi_signal_1,
42 com.android.internal.R.drawable.ic_wifi_signal_2,
43 com.android.internal.R.drawable.ic_wifi_signal_3,
44 com.android.internal.R.drawable.ic_wifi_signal_4
Sundeep Ghumanf84e0572017-01-10 13:43:03 -080045 };
46
Maggieaa080f92018-01-04 15:35:11 -080047 public static void updateLocationEnabled(Context context, boolean enabled, int userId) {
48 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
49
50 final int oldMode = Settings.Secure.getIntForUser(context.getContentResolver(),
51 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, userId);
52 final int newMode = enabled
53 ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
54 : Settings.Secure.LOCATION_MODE_OFF;
55 intent.putExtra(CURRENT_MODE_KEY, oldMode);
56 intent.putExtra(NEW_MODE_KEY, newMode);
57 context.sendBroadcastAsUser(
58 intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS);
59 LocationManager locationManager =
60 (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
61 LocationManagerWrapper wrapper = new LocationManagerWrapper(locationManager);
62 wrapper.setLocationEnabledForUser(enabled, UserHandle.of(userId));
63 }
64
Maggiefab2e2c2017-11-21 11:57:30 -080065 public static boolean updateLocationMode(Context context, int oldMode, int newMode, int userId) {
66 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
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 return Settings.Secure.putIntForUser(
72 context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode, userId);
73 }
74
Wei Liu30275c12015-08-24 17:35:49 -070075 /**
76 * Return string resource that best describes combination of tethering
77 * options available on this device.
78 */
79 public static int getTetheringLabel(ConnectivityManager cm) {
80 String[] usbRegexs = cm.getTetherableUsbRegexs();
81 String[] wifiRegexs = cm.getTetherableWifiRegexs();
82 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
83
84 boolean usbAvailable = usbRegexs.length != 0;
85 boolean wifiAvailable = wifiRegexs.length != 0;
86 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
87
88 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
89 return R.string.tether_settings_title_all;
90 } else if (wifiAvailable && usbAvailable) {
91 return R.string.tether_settings_title_all;
92 } else if (wifiAvailable && bluetoothAvailable) {
93 return R.string.tether_settings_title_all;
94 } else if (wifiAvailable) {
95 return R.string.tether_settings_title_wifi;
96 } else if (usbAvailable && bluetoothAvailable) {
97 return R.string.tether_settings_title_usb_bluetooth;
98 } else if (usbAvailable) {
99 return R.string.tether_settings_title_usb;
100 } else {
101 return R.string.tether_settings_title_bluetooth;
102 }
103 }
104
105 /**
106 * Returns a label for the user, in the form of "User: user name" or "Work profile".
107 */
108 public static String getUserLabel(Context context, UserInfo info) {
109 String name = info != null ? info.name : null;
110 if (info.isManagedProfile()) {
111 // We use predefined values for managed profiles
112 return context.getString(R.string.managed_user_title);
113 } else if (info.isGuest()) {
114 name = context.getString(R.string.user_guest);
115 }
116 if (name == null && info != null) {
117 name = Integer.toString(info.id);
118 } else if (info == null) {
119 name = context.getString(R.string.unknown);
120 }
121 return context.getResources().getString(R.string.running_process_item_user_label, name);
122 }
123
124 /**
125 * Returns a circular icon for a user.
126 */
Daniel Nishif26dbd82017-08-15 15:25:39 -0700127 public static Drawable getUserIcon(Context context, UserManager um, UserInfo user) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700128 final int iconSize = UserIconDrawable.getSizeForList(context);
Wei Liu30275c12015-08-24 17:35:49 -0700129 if (user.isManagedProfile()) {
Tony Makbca5c932017-11-08 19:54:15 +0000130 Drawable drawable = context.getDrawable(com.android.internal.R.drawable.ic_corp_badge);
Daniel Nishi07d0f592017-08-21 15:50:11 -0700131 drawable.setBounds(0, 0, iconSize, iconSize);
132 return drawable;
Wei Liu30275c12015-08-24 17:35:49 -0700133 }
134 if (user.iconPath != null) {
135 Bitmap icon = um.getUserIcon(user.id);
136 if (icon != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700137 return new UserIconDrawable(iconSize).setIcon(icon).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700138 }
139 }
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700140 return new UserIconDrawable(iconSize).setIconDrawable(
Tony Mak213955e2017-11-23 16:57:08 +0800141 UserIcons.getDefaultUserIcon(context.getResources(), user.id, /* light= */ false))
142 .bake();
Wei Liu30275c12015-08-24 17:35:49 -0700143 }
Jason Monkc06fbb12016-01-08 14:12:18 -0500144
jackqdyulei5dc1f362017-02-17 14:41:05 -0800145 /** Formats a double from 0.0..100.0 with an option to round **/
146 public static String formatPercentage(double percentage, boolean round) {
147 final int localPercentage = round ? Math.round((float) percentage) : (int) percentage;
148 return formatPercentage(localPercentage);
149 }
150
Jason Monkc06fbb12016-01-08 14:12:18 -0500151 /** Formats the ratio of amount/total as a percentage. */
152 public static String formatPercentage(long amount, long total) {
153 return formatPercentage(((double) amount) / total);
154 }
155
156 /** Formats an integer from 0..100 as a percentage. */
157 public static String formatPercentage(int percentage) {
158 return formatPercentage(((double) percentage) / 100.0);
159 }
160
161 /** Formats a double from 0.0..1.0 as a percentage. */
Daniel Nishi3f94f682017-06-27 14:38:06 -0700162 public static String formatPercentage(double percentage) {
jackqdyulei5dc1f362017-02-17 14:41:05 -0800163 return NumberFormat.getPercentInstance().format(percentage);
Jason Monkc06fbb12016-01-08 14:12:18 -0500164 }
165
166 public static int getBatteryLevel(Intent batteryChangedIntent) {
167 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
168 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
169 return (level * 100) / scale;
170 }
171
172 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
Muyuan Li18d2b322016-03-31 13:53:56 -0700173 int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
Jason Monkc06fbb12016-01-08 14:12:18 -0500174 BatteryManager.BATTERY_STATUS_UNKNOWN);
175 String statusString;
176 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
jackqdyulei806f78d2017-03-24 12:13:49 -0700177 statusString = res.getString(R.string.battery_info_status_charging);
Jason Monkc06fbb12016-01-08 14:12:18 -0500178 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
179 statusString = res.getString(R.string.battery_info_status_discharging);
180 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
181 statusString = res.getString(R.string.battery_info_status_not_charging);
182 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
183 statusString = res.getString(R.string.battery_info_status_full);
184 } else {
185 statusString = res.getString(R.string.battery_info_status_unknown);
186 }
187
188 return statusString;
189 }
Julia Reynolds21c30542016-01-15 15:00:02 -0500190
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700191 @ColorInt
192 public static int getColorAccent(Context context) {
Jason Monk32508852017-01-18 09:17:13 -0500193 return getColorAttr(context, android.R.attr.colorAccent);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700194 }
195
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800196 @ColorInt
197 public static int getColorError(Context context) {
Jason Monk58be7a62017-02-01 20:17:51 -0500198 return getColorAttr(context, android.R.attr.colorError);
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800199 }
200
201 @ColorInt
202 public static int getDefaultColor(Context context, int resId) {
203 final ColorStateList list =
204 context.getResources().getColorStateList(resId, context.getTheme());
205
206 return list.getDefaultColor();
207 }
208
Jason Monk32508852017-01-18 09:17:13 -0500209 @ColorInt
210 public static int getDisabled(Context context, int inputColor) {
211 return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor);
212 }
213
214 @ColorInt
215 public static int applyAlphaAttr(Context context, int attr, int inputColor) {
216 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
217 float alpha = ta.getFloat(0, 0);
218 ta.recycle();
Jason Monk824ffff2017-04-11 15:49:06 -0400219 return applyAlpha(alpha, inputColor);
220 }
221
222 @ColorInt
223 public static int applyAlpha(float alpha, int inputColor) {
Jason Monk32508852017-01-18 09:17:13 -0500224 alpha *= Color.alpha(inputColor);
225 return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor),
226 Color.blue(inputColor));
227 }
228
229 @ColorInt
230 public static int getColorAttr(Context context, int attr) {
231 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
232 @ColorInt int colorAccent = ta.getColor(0, 0);
233 ta.recycle();
234 return colorAccent;
235 }
236
Jason Monk9a376bc2017-05-10 09:52:10 -0400237 public static int getThemeAttr(Context context, int attr) {
238 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
239 int theme = ta.getResourceId(0, 0);
240 ta.recycle();
241 return theme;
242 }
243
Jason Monk790442e2017-02-13 17:49:39 -0500244 public static Drawable getDrawable(Context context, int attr) {
245 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
246 Drawable drawable = ta.getDrawable(0);
247 ta.recycle();
248 return drawable;
249 }
250
Julia Reynolds21c30542016-01-15 15:00:02 -0500251 /**
252 * Determine whether a package is a "system package", in which case certain things (like
253 * disabling notifications or disabling the package altogether) should be disallowed.
254 */
Tony Makbfba9d42016-07-14 15:29:44 +0800255 public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500256 if (sSystemSignature == null) {
257 sSystemSignature = new Signature[]{ getSystemSignature(pm) };
258 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800259 if (sPermissionControllerPackageName == null) {
260 sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
261 }
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700262 if (sServicesSystemSharedLibPackageName == null) {
263 sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
264 }
265 if (sSharedSystemSharedLibPackageName == null) {
266 sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
267 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800268 return (sSystemSignature[0] != null
269 && sSystemSignature[0].equals(getFirstSignature(pkg)))
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700270 || pkg.packageName.equals(sPermissionControllerPackageName)
271 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
Philip P. Moltmann545a58a2016-06-28 14:14:06 -0700272 || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
Tony Makbfba9d42016-07-14 15:29:44 +0800273 || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
274 || isDeviceProvisioningPackage(resources, pkg.packageName);
Julia Reynolds21c30542016-01-15 15:00:02 -0500275 }
276
Julia Reynolds21c30542016-01-15 15:00:02 -0500277 private static Signature getFirstSignature(PackageInfo pkg) {
278 if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) {
279 return pkg.signatures[0];
280 }
281 return null;
282 }
283
284 private static Signature getSystemSignature(PackageManager pm) {
285 try {
286 final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
287 return getFirstSignature(sys);
288 } catch (NameNotFoundException e) {
289 }
290 return null;
291 }
Tony Makbfba9d42016-07-14 15:29:44 +0800292
293 /**
294 * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
295 * returns {@code false}.
296 */
297 public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) {
298 String deviceProvisioningPackage = resources.getString(
299 com.android.internal.R.string.config_deviceProvisioningPackage);
300 return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
301 }
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800302
303 /**
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700304 * Returns the Wifi icon resource for a given RSSI level.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800305 *
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800306 * @param level The number of bars to show (0-4)
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800307 *
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700308 * @throws IllegalArgumentException if an invalid RSSI level is given.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800309 */
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700310 public static int getWifiIconResource(int level) {
311 if (level < 0 || level >= WIFI_PIE.length) {
312 throw new IllegalArgumentException("No Wifi icon found for level: " + level);
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800313 }
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700314 return WIFI_PIE[level];
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800315 }
Daniel Nishi53982802017-05-23 15:54:34 -0700316
317 public static int getDefaultStorageManagerDaysToRetain(Resources resources) {
318 int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT;
319 try {
320 defaultDays =
321 resources.getInteger(
322 com.android
323 .internal
324 .R
325 .integer
326 .config_storageManagerDaystoRetainDefault);
327 } catch (Resources.NotFoundException e) {
328 // We are likely in a test environment.
329 }
330 return defaultDays;
331 }
Tony Mantler88920142017-10-20 15:03:56 -0700332
333 public static boolean isWifiOnly(Context context) {
334 return !context.getSystemService(ConnectivityManager.class)
335 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
336 }
Wei Liu30275c12015-08-24 17:35:49 -0700337}