blob: 1f67dfb568a6d6b93c3225af4fced040d7bc642e [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
Lifu Tang0cba58f2018-01-23 21:14:15 -080047 public static void updateLocationEnabled(Context context, boolean enabled, int userId,
48 int source) {
49 Settings.Secure.putIntForUser(
50 context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source,
51 userId);
Maggieaa080f92018-01-04 15:35:11 -080052 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
53
54 final int oldMode = Settings.Secure.getIntForUser(context.getContentResolver(),
55 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, userId);
56 final int newMode = enabled
57 ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
58 : Settings.Secure.LOCATION_MODE_OFF;
59 intent.putExtra(CURRENT_MODE_KEY, oldMode);
60 intent.putExtra(NEW_MODE_KEY, newMode);
61 context.sendBroadcastAsUser(
62 intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS);
63 LocationManager locationManager =
64 (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
65 LocationManagerWrapper wrapper = new LocationManagerWrapper(locationManager);
66 wrapper.setLocationEnabledForUser(enabled, UserHandle.of(userId));
67 }
68
Lifu Tang0cba58f2018-01-23 21:14:15 -080069 public static boolean updateLocationMode(Context context, int oldMode, int newMode, int userId,
70 int source) {
71 Settings.Secure.putIntForUser(
72 context.getContentResolver(), Settings.Secure.LOCATION_CHANGER, source,
73 userId);
Maggiefab2e2c2017-11-21 11:57:30 -080074 Intent intent = new Intent(LocationManager.MODE_CHANGING_ACTION);
75 intent.putExtra(CURRENT_MODE_KEY, oldMode);
76 intent.putExtra(NEW_MODE_KEY, newMode);
77 context.sendBroadcastAsUser(
78 intent, UserHandle.of(userId), android.Manifest.permission.WRITE_SECURE_SETTINGS);
79 return Settings.Secure.putIntForUser(
80 context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode, userId);
81 }
82
Wei Liu30275c12015-08-24 17:35:49 -070083 /**
84 * Return string resource that best describes combination of tethering
85 * options available on this device.
86 */
87 public static int getTetheringLabel(ConnectivityManager cm) {
88 String[] usbRegexs = cm.getTetherableUsbRegexs();
89 String[] wifiRegexs = cm.getTetherableWifiRegexs();
90 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
91
92 boolean usbAvailable = usbRegexs.length != 0;
93 boolean wifiAvailable = wifiRegexs.length != 0;
94 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
95
96 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
97 return R.string.tether_settings_title_all;
98 } else if (wifiAvailable && usbAvailable) {
99 return R.string.tether_settings_title_all;
100 } else if (wifiAvailable && bluetoothAvailable) {
101 return R.string.tether_settings_title_all;
102 } else if (wifiAvailable) {
103 return R.string.tether_settings_title_wifi;
104 } else if (usbAvailable && bluetoothAvailable) {
105 return R.string.tether_settings_title_usb_bluetooth;
106 } else if (usbAvailable) {
107 return R.string.tether_settings_title_usb;
108 } else {
109 return R.string.tether_settings_title_bluetooth;
110 }
111 }
112
113 /**
114 * Returns a label for the user, in the form of "User: user name" or "Work profile".
115 */
116 public static String getUserLabel(Context context, UserInfo info) {
117 String name = info != null ? info.name : null;
118 if (info.isManagedProfile()) {
119 // We use predefined values for managed profiles
120 return context.getString(R.string.managed_user_title);
121 } else if (info.isGuest()) {
122 name = context.getString(R.string.user_guest);
123 }
124 if (name == null && info != null) {
125 name = Integer.toString(info.id);
126 } else if (info == null) {
127 name = context.getString(R.string.unknown);
128 }
129 return context.getResources().getString(R.string.running_process_item_user_label, name);
130 }
131
132 /**
133 * Returns a circular icon for a user.
134 */
Daniel Nishif26dbd82017-08-15 15:25:39 -0700135 public static Drawable getUserIcon(Context context, UserManager um, UserInfo user) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700136 final int iconSize = UserIconDrawable.getSizeForList(context);
Wei Liu30275c12015-08-24 17:35:49 -0700137 if (user.isManagedProfile()) {
Tony Makbca5c932017-11-08 19:54:15 +0000138 Drawable drawable = context.getDrawable(com.android.internal.R.drawable.ic_corp_badge);
Daniel Nishi07d0f592017-08-21 15:50:11 -0700139 drawable.setBounds(0, 0, iconSize, iconSize);
140 return drawable;
Wei Liu30275c12015-08-24 17:35:49 -0700141 }
142 if (user.iconPath != null) {
143 Bitmap icon = um.getUserIcon(user.id);
144 if (icon != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700145 return new UserIconDrawable(iconSize).setIcon(icon).bake();
Wei Liu30275c12015-08-24 17:35:49 -0700146 }
147 }
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700148 return new UserIconDrawable(iconSize).setIconDrawable(
Tony Mak213955e2017-11-23 16:57:08 +0800149 UserIcons.getDefaultUserIcon(context.getResources(), user.id, /* light= */ false))
150 .bake();
Wei Liu30275c12015-08-24 17:35:49 -0700151 }
Jason Monkc06fbb12016-01-08 14:12:18 -0500152
jackqdyulei5dc1f362017-02-17 14:41:05 -0800153 /** Formats a double from 0.0..100.0 with an option to round **/
154 public static String formatPercentage(double percentage, boolean round) {
155 final int localPercentage = round ? Math.round((float) percentage) : (int) percentage;
156 return formatPercentage(localPercentage);
157 }
158
Jason Monkc06fbb12016-01-08 14:12:18 -0500159 /** Formats the ratio of amount/total as a percentage. */
160 public static String formatPercentage(long amount, long total) {
161 return formatPercentage(((double) amount) / total);
162 }
163
164 /** Formats an integer from 0..100 as a percentage. */
165 public static String formatPercentage(int percentage) {
166 return formatPercentage(((double) percentage) / 100.0);
167 }
168
169 /** Formats a double from 0.0..1.0 as a percentage. */
Daniel Nishi3f94f682017-06-27 14:38:06 -0700170 public static String formatPercentage(double percentage) {
jackqdyulei5dc1f362017-02-17 14:41:05 -0800171 return NumberFormat.getPercentInstance().format(percentage);
Jason Monkc06fbb12016-01-08 14:12:18 -0500172 }
173
174 public static int getBatteryLevel(Intent batteryChangedIntent) {
175 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
176 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
177 return (level * 100) / scale;
178 }
179
180 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
Muyuan Li18d2b322016-03-31 13:53:56 -0700181 int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
Jason Monkc06fbb12016-01-08 14:12:18 -0500182 BatteryManager.BATTERY_STATUS_UNKNOWN);
183 String statusString;
184 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
jackqdyulei806f78d2017-03-24 12:13:49 -0700185 statusString = res.getString(R.string.battery_info_status_charging);
Jason Monkc06fbb12016-01-08 14:12:18 -0500186 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
187 statusString = res.getString(R.string.battery_info_status_discharging);
188 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
189 statusString = res.getString(R.string.battery_info_status_not_charging);
190 } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
191 statusString = res.getString(R.string.battery_info_status_full);
192 } else {
193 statusString = res.getString(R.string.battery_info_status_unknown);
194 }
195
196 return statusString;
197 }
Julia Reynolds21c30542016-01-15 15:00:02 -0500198
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700199 @ColorInt
200 public static int getColorAccent(Context context) {
Jason Monk32508852017-01-18 09:17:13 -0500201 return getColorAttr(context, android.R.attr.colorAccent);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700202 }
203
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800204 @ColorInt
205 public static int getColorError(Context context) {
Jason Monk58be7a62017-02-01 20:17:51 -0500206 return getColorAttr(context, android.R.attr.colorError);
jackqdyuleib68fd7a2017-01-04 15:43:26 -0800207 }
208
209 @ColorInt
210 public static int getDefaultColor(Context context, int resId) {
211 final ColorStateList list =
212 context.getResources().getColorStateList(resId, context.getTheme());
213
214 return list.getDefaultColor();
215 }
216
Jason Monk32508852017-01-18 09:17:13 -0500217 @ColorInt
218 public static int getDisabled(Context context, int inputColor) {
219 return applyAlphaAttr(context, android.R.attr.disabledAlpha, inputColor);
220 }
221
222 @ColorInt
223 public static int applyAlphaAttr(Context context, int attr, int inputColor) {
224 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
225 float alpha = ta.getFloat(0, 0);
226 ta.recycle();
Jason Monk824ffff2017-04-11 15:49:06 -0400227 return applyAlpha(alpha, inputColor);
228 }
229
230 @ColorInt
231 public static int applyAlpha(float alpha, int inputColor) {
Jason Monk32508852017-01-18 09:17:13 -0500232 alpha *= Color.alpha(inputColor);
233 return Color.argb((int) (alpha), Color.red(inputColor), Color.green(inputColor),
234 Color.blue(inputColor));
235 }
236
237 @ColorInt
238 public static int getColorAttr(Context context, int attr) {
239 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
240 @ColorInt int colorAccent = ta.getColor(0, 0);
241 ta.recycle();
242 return colorAccent;
243 }
244
Jason Monk9a376bc2017-05-10 09:52:10 -0400245 public static int getThemeAttr(Context context, int attr) {
246 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
247 int theme = ta.getResourceId(0, 0);
248 ta.recycle();
249 return theme;
250 }
251
Jason Monk790442e2017-02-13 17:49:39 -0500252 public static Drawable getDrawable(Context context, int attr) {
253 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
254 Drawable drawable = ta.getDrawable(0);
255 ta.recycle();
256 return drawable;
257 }
258
Julia Reynolds21c30542016-01-15 15:00:02 -0500259 /**
260 * Determine whether a package is a "system package", in which case certain things (like
261 * disabling notifications or disabling the package altogether) should be disallowed.
262 */
Tony Makbfba9d42016-07-14 15:29:44 +0800263 public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500264 if (sSystemSignature == null) {
265 sSystemSignature = new Signature[]{ getSystemSignature(pm) };
266 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800267 if (sPermissionControllerPackageName == null) {
268 sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
269 }
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700270 if (sServicesSystemSharedLibPackageName == null) {
271 sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
272 }
273 if (sSharedSystemSharedLibPackageName == null) {
274 sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
275 }
Svet Ganov81fb0d42016-03-04 09:27:23 -0800276 return (sSystemSignature[0] != null
277 && sSystemSignature[0].equals(getFirstSignature(pkg)))
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700278 || pkg.packageName.equals(sPermissionControllerPackageName)
279 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
Philip P. Moltmann545a58a2016-06-28 14:14:06 -0700280 || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
Tony Makbfba9d42016-07-14 15:29:44 +0800281 || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
282 || isDeviceProvisioningPackage(resources, pkg.packageName);
Julia Reynolds21c30542016-01-15 15:00:02 -0500283 }
284
Julia Reynolds21c30542016-01-15 15:00:02 -0500285 private static Signature getFirstSignature(PackageInfo pkg) {
286 if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) {
287 return pkg.signatures[0];
288 }
289 return null;
290 }
291
292 private static Signature getSystemSignature(PackageManager pm) {
293 try {
294 final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
295 return getFirstSignature(sys);
296 } catch (NameNotFoundException e) {
297 }
298 return null;
299 }
Tony Makbfba9d42016-07-14 15:29:44 +0800300
301 /**
302 * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
303 * returns {@code false}.
304 */
305 public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) {
306 String deviceProvisioningPackage = resources.getString(
307 com.android.internal.R.string.config_deviceProvisioningPackage);
308 return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
309 }
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800310
311 /**
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700312 * Returns the Wifi icon resource for a given RSSI level.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800313 *
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800314 * @param level The number of bars to show (0-4)
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800315 *
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700316 * @throws IllegalArgumentException if an invalid RSSI level is given.
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800317 */
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700318 public static int getWifiIconResource(int level) {
319 if (level < 0 || level >= WIFI_PIE.length) {
320 throw new IllegalArgumentException("No Wifi icon found for level: " + level);
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800321 }
Eric Schwarzenbach44268e32017-08-04 16:38:07 -0700322 return WIFI_PIE[level];
Sundeep Ghumanf84e0572017-01-10 13:43:03 -0800323 }
Daniel Nishi53982802017-05-23 15:54:34 -0700324
325 public static int getDefaultStorageManagerDaysToRetain(Resources resources) {
326 int defaultDays = Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT;
327 try {
328 defaultDays =
329 resources.getInteger(
330 com.android
331 .internal
332 .R
333 .integer
334 .config_storageManagerDaystoRetainDefault);
335 } catch (Resources.NotFoundException e) {
336 // We are likely in a test environment.
337 }
338 return defaultDays;
339 }
Tony Mantler88920142017-10-20 15:03:56 -0700340
341 public static boolean isWifiOnly(Context context) {
342 return !context.getSystemService(ConnectivityManager.class)
343 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
344 }
Wei Liu30275c12015-08-24 17:35:49 -0700345}