1 package com.android.settingslib;
2 
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.pm.PackageInfo;
6 import android.content.pm.PackageManager;
7 import android.content.pm.PackageManager.NameNotFoundException;
8 import android.content.pm.UserInfo;
9 import android.content.pm.Signature;
10 import android.content.res.Resources;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 import android.graphics.drawable.Drawable;
14 import android.net.ConnectivityManager;
15 import android.os.BatteryManager;
16 import android.os.UserManager;
17 import com.android.internal.util.UserIcons;
18 import com.android.settingslib.drawable.UserIconDrawable;
19 
20 import java.text.NumberFormat;
21 
22 public class Utils {
23     private static Signature[] sSystemSignature;
24     private static String sPermissionControllerPackageName;
25     private static String sServicesSystemSharedLibPackageName;
26     private static String sSharedSystemSharedLibPackageName;
27 
28     /**
29      * Return string resource that best describes combination of tethering
30      * options available on this device.
31      */
getTetheringLabel(ConnectivityManager cm)32     public static int getTetheringLabel(ConnectivityManager cm) {
33         String[] usbRegexs = cm.getTetherableUsbRegexs();
34         String[] wifiRegexs = cm.getTetherableWifiRegexs();
35         String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
36 
37         boolean usbAvailable = usbRegexs.length != 0;
38         boolean wifiAvailable = wifiRegexs.length != 0;
39         boolean bluetoothAvailable = bluetoothRegexs.length != 0;
40 
41         if (wifiAvailable && usbAvailable && bluetoothAvailable) {
42             return R.string.tether_settings_title_all;
43         } else if (wifiAvailable && usbAvailable) {
44             return R.string.tether_settings_title_all;
45         } else if (wifiAvailable && bluetoothAvailable) {
46             return R.string.tether_settings_title_all;
47         } else if (wifiAvailable) {
48             return R.string.tether_settings_title_wifi;
49         } else if (usbAvailable && bluetoothAvailable) {
50             return R.string.tether_settings_title_usb_bluetooth;
51         } else if (usbAvailable) {
52             return R.string.tether_settings_title_usb;
53         } else {
54             return R.string.tether_settings_title_bluetooth;
55         }
56     }
57 
58     /**
59      * Returns a label for the user, in the form of "User: user name" or "Work profile".
60      */
getUserLabel(Context context, UserInfo info)61     public static String getUserLabel(Context context, UserInfo info) {
62         String name = info != null ? info.name : null;
63         if (info.isManagedProfile()) {
64             // We use predefined values for managed profiles
65             return context.getString(R.string.managed_user_title);
66         } else if (info.isGuest()) {
67             name = context.getString(R.string.user_guest);
68         }
69         if (name == null && info != null) {
70             name = Integer.toString(info.id);
71         } else if (info == null) {
72             name = context.getString(R.string.unknown);
73         }
74         return context.getResources().getString(R.string.running_process_item_user_label, name);
75     }
76 
77     /**
78      * Returns a circular icon for a user.
79      */
getUserIcon(Context context, UserManager um, UserInfo user)80     public static UserIconDrawable getUserIcon(Context context, UserManager um, UserInfo user) {
81         final int iconSize = UserIconDrawable.getSizeForList(context);
82         if (user.isManagedProfile()) {
83             // We use predefined values for managed profiles
84             Bitmap b = BitmapFactory.decodeResource(context.getResources(),
85                     com.android.internal.R.drawable.ic_corp_icon);
86             return new UserIconDrawable(iconSize).setIcon(b).bake();
87         }
88         if (user.iconPath != null) {
89             Bitmap icon = um.getUserIcon(user.id);
90             if (icon != null) {
91                 return new UserIconDrawable(iconSize).setIcon(icon).bake();
92             }
93         }
94         return new UserIconDrawable(iconSize).setIconDrawable(
95                 UserIcons.getDefaultUserIcon(user.id, /* light= */ false)).bake();
96     }
97 
98     /** Formats the ratio of amount/total as a percentage. */
formatPercentage(long amount, long total)99     public static String formatPercentage(long amount, long total) {
100         return formatPercentage(((double) amount) / total);
101     }
102 
103     /** Formats an integer from 0..100 as a percentage. */
formatPercentage(int percentage)104     public static String formatPercentage(int percentage) {
105         return formatPercentage(((double) percentage) / 100.0);
106     }
107 
108     /** Formats a double from 0.0..1.0 as a percentage. */
formatPercentage(double percentage)109     private static String formatPercentage(double percentage) {
110       return NumberFormat.getPercentInstance().format(percentage);
111     }
112 
getBatteryLevel(Intent batteryChangedIntent)113     public static int getBatteryLevel(Intent batteryChangedIntent) {
114         int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
115         int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
116         return (level * 100) / scale;
117     }
118 
getBatteryStatus(Resources res, Intent batteryChangedIntent)119     public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
120         return Utils.getBatteryStatus(res, batteryChangedIntent, false);
121     }
122 
getBatteryStatus(Resources res, Intent batteryChangedIntent, boolean shortString)123     public static String getBatteryStatus(Resources res, Intent batteryChangedIntent,
124             boolean shortString) {
125         int plugType = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
126         int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
127                 BatteryManager.BATTERY_STATUS_UNKNOWN);
128         String statusString;
129         if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
130             int resId;
131             if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
132                 resId = shortString ? R.string.battery_info_status_charging_ac_short
133                         : R.string.battery_info_status_charging_ac;
134             } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
135                 resId = shortString ? R.string.battery_info_status_charging_usb_short
136                         : R.string.battery_info_status_charging_usb;
137             } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
138                 resId = shortString ? R.string.battery_info_status_charging_wireless_short
139                         : R.string.battery_info_status_charging_wireless;
140             } else {
141                 resId = R.string.battery_info_status_charging;
142             }
143             statusString = res.getString(resId);
144         } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
145             statusString = res.getString(R.string.battery_info_status_discharging);
146         } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
147             statusString = res.getString(R.string.battery_info_status_not_charging);
148         } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
149             statusString = res.getString(R.string.battery_info_status_full);
150         } else {
151             statusString = res.getString(R.string.battery_info_status_unknown);
152         }
153 
154         return statusString;
155     }
156 
157     /**
158      * Determine whether a package is a "system package", in which case certain things (like
159      * disabling notifications or disabling the package altogether) should be disallowed.
160      */
isSystemPackage(PackageManager pm, PackageInfo pkg)161     public static boolean isSystemPackage(PackageManager pm, PackageInfo pkg) {
162         if (sSystemSignature == null) {
163             sSystemSignature = new Signature[]{ getSystemSignature(pm) };
164         }
165         if (sPermissionControllerPackageName == null) {
166             sPermissionControllerPackageName = pm.getPermissionControllerPackageName();
167         }
168         if (sServicesSystemSharedLibPackageName == null) {
169             sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName();
170         }
171         if (sSharedSystemSharedLibPackageName == null) {
172             sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName();
173         }
174         return (sSystemSignature[0] != null
175                         && sSystemSignature[0].equals(getFirstSignature(pkg)))
176                 || pkg.packageName.equals(sPermissionControllerPackageName)
177                 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
178                 || pkg.packageName.equals(sSharedSystemSharedLibPackageName);
179     }
180 
getFirstSignature(PackageInfo pkg)181     private static Signature getFirstSignature(PackageInfo pkg) {
182         if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) {
183             return pkg.signatures[0];
184         }
185         return null;
186     }
187 
getSystemSignature(PackageManager pm)188     private static Signature getSystemSignature(PackageManager pm) {
189         try {
190             final PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
191             return getFirstSignature(sys);
192         } catch (NameNotFoundException e) {
193         }
194         return null;
195     }
196 }
197