blob: 56a242aea6a1b65894d5b8b98847fc3bfbc2014e [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;
Daniel Nishi84522e02018-01-23 18:27:22 -080020import android.os.SystemProperties;
Maggiefab2e2c2017-11-21 11:57:30 -080021import android.os.UserHandle;
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;
Daniel Nishi84522e02018-01-23 18:27:22 -080025
26import com.android.internal.annotations.VisibleForTesting;
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;
Maggieaa080f92018-01-04 15:35:11 -080029import com.android.settingslib.wrapper.LocationManagerWrapper;
Jason Monkc06fbb12016-01-08 14:12:18 -050030import java.text.NumberFormat;
31
32public class Utils {
Maggiefab2e2c2017-11-21 11:57:30 -080033
34 private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
35 private static final String NEW_MODE_KEY = "NEW_MODE";
Daniel Nishi84522e02018-01-23 18:27:22 -080036 @VisibleForTesting
37 static final String STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY =
38 "ro.storage_manager.show_opt_in";
Maggiefab2e2c2017-11-21 11:57:30 -080039
Svetoslav Ganova9c25002016-04-13 19:25:56 -070040 private static Signature[] sSystemSignature;
41 private static String sPermissionControllerPackageName;
42 private static String sServicesSystemSharedLibPackageName;
43 private static String sSharedSystemSharedLibPackageName;
Wei Liu30275c12015-08-24 17:35:49 -070044
Eric Schwarzenbach44268e32017-08-04 16:38:07 -070045 static final int[] WIFI_PIE = {
Daniel Nishi84522e02018-01-23 18:27:22 -080046 com.android.internal.R.drawable.ic_wifi_signal_0,
47 com.android.internal.R.drawable.ic_wifi_signal_1,
48 com.android.internal.R.drawable.ic_wifi_signal_2,
49 com.android.internal.R.drawable.ic_wifi_signal_3,
50 com.android.internal.R.drawable.ic_wifi_signal_4
Sundeep Ghumanf84e0572017-01-10 13:43:03 -080051 };
52
Lifu Tang0cba58f2018-01-23 21:14:15 -080053 public static void updateLocationEnabled(Context context, boolean enabled, int userId,
54 int source) {
55 Settings.Secure.putIntForUser(
56 context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source,
57 userId);
Maggieaa080f92018-01-04 15:35:11 -080058 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
59
60 final int oldMode = Settings.Secure.getIntForUser(context.getContentResolver(),
61 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, userId);
62 final int newMode = enabled
63 ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
64 : Settings.Secure.LOCATION_MODE_OFF;
65 intent.putExtra(CURRENT_MODE_KEY, oldMode);
66 intent.putExtra(NEW_MODE_KEY, newMode);
67 context.sendBroadcastAsUser(
68 intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS);
69 LocationManager locationManager =
70 (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
71 LocationManagerWrapper wrapper = new LocationManagerWrapper(locationManager);
72 wrapper.setLocationEnabledForUser(enabled, UserHandle.of(userId));
73 }
74
Lifu Tang0cba58f2018-01-23 21:14:15 -080075 public static boolean updateLocationMode(Context context, int oldMode, int newMode, int userId,
76 int source) {
77 Settings.Secure.putIntForUser(
78 context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source,
79 userId);
Maggiefab2e2c2017-11-21 11:57:30 -080080 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
81 intent.putExtra(CURRENT_MODE_KEY, oldMode);
82 intent.putExtra(NEW_MODE_KEY, newMode);
83 context.sendBroadcastAsUser(
84 intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS);
85 return Settings.Secure.putIntForUser(
86 context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode, userId);
87 }
88
Wei Liu30275c12015-08-24 17:35:49 -070089 /**
90 * Return string resource that best describes combination of tethering
91 * options available on this device.
92 */
93 public static int getTetheringLabel(ConnectivityManager cm) {
94 String[] usbRegexs = cm.getTetherableUsbRegexs();
95 String[] wifiRegexs = cm.getTetherableWifiRegexs();
96 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
97
98 boolean usbAvailable = usbRegexs.length != 0;
99 boolean wifiAvailable = wifiRegexs.length != 0;
100 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
101
102 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
103 return R.string.tether_settings_title_all;
104 } else if (wifiAvailable && usbAvailable) {
105 return R.string.tether_settings_title_all;
106 } else if (wifiAvailable && bluetoothAvailable) {
107 return R.string.tether_settings_title_all;
108 } else if (wifiAvailable) {
109 return R.string.tether_settings_title_wifi;
110 } else if (usbAvailable && bluetoothAvailable) {
111 return R.string.tether_settings_title_usb_bluetooth;
112 } else if (usbAvailable) {
113 return R.string.tether_settings_title_usb;
114 } else {
115 return R.string.tether_settings_title_bluetooth;
116 }
117 }
118
119 /**
120 * Returns a label for the user, in the form of "User: user name" or "Work profile".
121 */
122 public static String getUserLabel(Context context, UserInfo info) {
123 String name = info != null ? info.name : null;
124 if (info.isManagedProfile()) {
125 // We use predefined values for managed profiles
126 return context.getString(R.string.managed_user_title);
127 } else if (info.isGuest()) {
128 name = context.getString(R.string.user_guest);
129 }
130 if (name == null && info != null) {
131 name = Integer.toString(info.id);
132 } else if (info == null) {
133 name = context.getString(R.string.unknown);
134 }
135 return context.getResources().getString(R.string.running_process_item_user_label, name);
136 }
137
138 /**
139 * Returns a circular icon for a user.
140 */
Daniel Nishif26dbd82017-08-15 15:25:39 -0700141 public static Drawable getUserIcon(Context context, UserManager um, UserInfo user) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700142 final int iconSize = UserIconDrawable.getSizeForList(context);
Wei Liu30275c12015-08-24 17:35:49 -0700143 if (user.isManagedProfile()) {
Tony Mak34942ab2018-03-06 11:24:51 +0000144 Drawable drawable = UserIconDrawable.getManagedUserDrawable(context);
Daniel Nishi07d0f592017-08-21 15:50:11 -0700145 drawable.setBounds(0, 0, iconSize, iconSize);
146 return drawable;
Wei Liu30275c12015-08-24 17:35:49 -0700147 }
148 if (user.iconPath != null) {
149 Bitmap icon = um.getUserIcon(user.id);
150 if (icon != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700151 return new UserIconDrawable(iconSize).setIcon(icon).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700152 }
153 }
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700154 return new UserIconDrawable(iconSize).setIconDrawable(
Tony Mak213955e2017-11-23 16:57:08 +0800155 UserIcons.getDefaultUserIcon(context.getResources(), user.id, /* light= */ false))
156 .bake();
Wei Liu30275c12015-08-24 17:35:49 -0700157 }
Jason Monkc06fbb12016-01-08 14:12:18 -0500158
jackqdyulei5dc1f362017-02-17 14:41:05 -0800159 /** Formats a double from 0.0..100.0 with an option to round **/
160 public static String formatPercentage(double percentage, boolean round) {
161 final int localPercentage = round ? Math.round((float) percentage) : (int) percentage;
162 return formatPercentage(localPercentage);
163 }
164
Jason Monkc06fbb12016-01-08 14:12:18 -0500165 /** Formats the ratio of amount/total as a percentage. */
166 public static String formatPercentage(long amount, long total) {
167 return formatPercentage(((double) amount) / total);
168 }
169
170 /** Formats an integer from 0..100 as a percentage. */
171 public static String formatPercentage(int percentage) {
172 return formatPercentage(((double) percentage) / 100.0);
173 }
174
175 /** Formats a double from 0.0..1.0 as a percentage. */
Daniel Nishi3f94f682017-06-27 14:38:06 -0700176 public static String formatPercentage(double percentage) {
jackqdyulei5dc1f362017-02-17 14:41:05 -0800177 return NumberFormat.getPercentInstance().format(percentage);
Jason Monkc06fbb12016-01-08 14:12:18 -0500178 }
179
180 public static int getBatteryLevel(Intent batteryChangedIntent) {
181 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
182 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
183 return (level * 100) / scale;
184 }
185
186 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
Muyuan Li18d2b322016-03-31 13:53:56 -0700187 int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
Jason Monkc06fbb12016-01-08 14:12:18 -0500188 BatteryManager.BATTERY_STATUS_UNKNOWN);
189 String statusString;
190 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
jackqdyulei806f78d2017-03-24 12:13:49 -0700191 statusString = res.getString(R.string.battery_info_status_charging);
Jason Monkc06fbb12016-01-08 14:12:18 -0500192 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
193 statusString = res.getString(R.string.battery_info_status_discharging);
194 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
195 statusString = res.getString(R.string.battery_info_status_not_charging);
196 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
197 statusString = res.getString(R.string.battery_info_status_full);
198 } else {
199 statusString = res.getString(R.string.battery_info_status_unknown);
200 }
201
202 return statusString;
203 }
Julia Reynolds21c30542016-01-15 15:00:02 -0500204
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700205 @ColorInt
206 public static int getColorAccent(Context context) {
Jason Monk32508852017-01-18 09:17:13 -0500207 return getColorAttr(context, android.R.attr.colorAccent);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700208 }
209
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800210 @ColorInt
211 public static int getColorError(Context context) {
Jason Monk58be7a62017-02-01 20:17:51 -0500212 return getColorAttr(context, android.R.attr.colorError);
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800213 }
214
215 @ColorInt
216 public static int getDefaultColor(Context context, int resId) {
217 final ColorStateList list =
218 context.getResources().getColorStateList(resId, context.getTheme());
219
220 return list.getDefaultColor();
221 }
222
Jason Monk32508852017-01-18 09:17:13 -0500223 @ColorInt
224 public static int getDisabled(Context context, int inputColor) {
225 return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor);
226 }
227
228 @ColorInt
229 public static int applyAlphaAttr(Context context, int attr, int inputColor) {
230 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
231 float alpha = ta.getFloat(0, 0);
232 ta.recycle();
Jason Monk824ffff2017-04-11 15:49:06 -0400233 return applyAlpha(alpha, inputColor);
234 }
235
236 @ColorInt
237 public static int applyAlpha(float alpha, int inputColor) {
Jason Monk32508852017-01-18 09:17:13 -0500238 alpha *= Color.alpha(inputColor);
239 return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor),
240 Color.blue(inputColor));
241 }
242
243 @ColorInt
244 public static int getColorAttr(Context context, int attr) {
245 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
246 @ColorInt int colorAccent = ta.getColor(0, 0);
247 ta.recycle();
248 return colorAccent;
249 }
250
Jason Monk9a376bc2017-05-10 09:52:10 -0400251 public static int getThemeAttr(Context context, int attr) {
252 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
253 int theme = ta.getResourceId(0, 0);
254 ta.recycle();
255 return theme;
256 }
257
Jason Monk790442e2017-02-13 17:49:39 -0500258 public static Drawable getDrawable(Context context, int attr) {
259 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
260 Drawable drawable = ta.getDrawable(0);
261 ta.recycle();
262 return drawable;
263 }
264
Julia Reynolds21c30542016-01-15 15:00:02 -0500265 /**
266 * Determine whether a package is a "system package", in which case certain things (like
267 * disabling notifications or disabling the package altogether) should be disallowed.
268 */
Tony Makbfba9d42016-07-14 15:29:44 +0800269 public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500270 if (sSystemSignature == null) {
Daniel Nishi84522e02018-01-23 18:27:22 -0800271 sSystemSignature = new Signature[]{getSystemSignature(pm)};
Julia Reynolds21c30542016-01-15 15:00:02 -0500272 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800273 if (sPermissionControllerPackageName == null) {
274 sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
275 }
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700276 if (sServicesSystemSharedLibPackageName == null) {
277 sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
278 }
279 if (sSharedSystemSharedLibPackageName == null) {
280 sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
281 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800282 return (sSystemSignature[0] != null
Daniel Nishi84522e02018-01-23 18:27:22 -0800283 && sSystemSignature[0].equals(getFirstSignature(pkg)))
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700284 || pkg.packageName.equals(sPermissionControllerPackageName)
285 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
Philip P. Moltmann545a58a2016-06-28 14:14:06 -0700286 || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
Tony Makbfba9d42016-07-14 15:29:44 +0800287 || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
288 || isDeviceProvisioningPackage(resources, pkg.packageName);
Julia Reynolds21c30542016-01-15 15:00:02 -0500289 }
290
Julia Reynolds21c30542016-01-15 15:00:02 -0500291 private static Signature getFirstSignature(PackageInfo pkg) {
292 if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) {
293 return pkg.signatures[0];
294 }
295 return null;
296 }
297
298 private static Signature getSystemSignature(PackageManager pm) {
299 try {
300 final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
301 return getFirstSignature(sys);
302 } catch (NameNotFoundException e) {
303 }
304 return null;
305 }
Tony Makbfba9d42016-07-14 15:29:44 +0800306
307 /**
308 * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
309 * returns {@code false}.
310 */
311 public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) {
312 String deviceProvisioningPackage = resources.getString(
313 com.android.internal.R.string.config_deviceProvisioningPackage);
314 return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
315 }
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800316
317 /**
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700318 * Returns the Wifi icon resource for a given RSSI level.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800319 *
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800320 * @param level The number of bars to show (0-4)
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700321 * @throws IllegalArgumentException if an invalid RSSI level is given.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800322 */
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700323 public static int getWifiIconResource(int level) {
324 if (level < 0 || level >= WIFI_PIE.length) {
325 throw new IllegalArgumentException("No Wifi icon found for level: " + level);
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800326 }
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700327 return WIFI_PIE[level];
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800328 }
Daniel Nishi53982802017-05-23 15:54:34 -0700329
330 public static int getDefaultStorageManagerDaysToRetain(Resources resources) {
331 int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT;
332 try {
333 defaultDays =
334 resources.getInteger(
335 com.android
336 .internal
337 .R
338 .integer
339 .config_storageManagerDaystoRetainDefault);
340 } catch (Resources.NotFoundException e) {
341 // We are likely in a test environment.
342 }
343 return defaultDays;
344 }
Tony Mantler88920142017-10-20 15:03:56 -0700345
346 public static boolean isWifiOnly(Context context) {
347 return !context.getSystemService(ConnectivityManager.class)
348 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
349 }
Daniel Nishi84522e02018-01-23 18:27:22 -0800350
351 /** Returns if the automatic storage management feature is turned on or not. **/
352 public static boolean isStorageManagerEnabled(Context context) {
353 boolean isDefaultOn;
354 try {
355 // Turn off by default if the opt-in was shown.
356 isDefaultOn = !SystemProperties.getBoolean(STORAGE_MANAGER_SHOW_OPT_IN_PROPERTY, true);
357 } catch (Resources.NotFoundException e) {
358 isDefaultOn = false;
359 }
360 return Settings.Secure.getInt(context.getContentResolver(),
361 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
362 isDefaultOn ? 1 : 0)
363 != 0;
364 }
Wei Liu30275c12015-08-24 17:35:49 -0700365}