1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.packageinstaller.permission.utils; 18 19 import android.Manifest; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.res.Resources; 26 import android.content.res.Resources.Theme; 27 import android.graphics.drawable.Drawable; 28 import android.util.ArraySet; 29 import android.util.Log; 30 import android.util.TypedValue; 31 32 import com.android.packageinstaller.permission.model.AppPermissionGroup; 33 import com.android.packageinstaller.permission.model.AppPermissions; 34 import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; 35 36 import java.util.List; 37 38 public final class Utils { 39 40 private static final String LOG_TAG = "Utils"; 41 42 public static final String OS_PKG = "android"; 43 44 public static final String[] MODERN_PERMISSION_GROUPS = { 45 Manifest.permission_group.CALENDAR, 46 Manifest.permission_group.CAMERA, 47 Manifest.permission_group.CONTACTS, 48 Manifest.permission_group.LOCATION, 49 Manifest.permission_group.SENSORS, 50 Manifest.permission_group.SMS, 51 Manifest.permission_group.PHONE, 52 Manifest.permission_group.MICROPHONE, 53 Manifest.permission_group.STORAGE 54 }; 55 56 private static final Intent LAUNCHER_INTENT = new Intent(Intent.ACTION_MAIN, null) 57 .addCategory(Intent.CATEGORY_LAUNCHER); 58 Utils()59 private Utils() { 60 /* do nothing - hide constructor */ 61 } 62 loadDrawable(PackageManager pm, String pkg, int resId)63 public static Drawable loadDrawable(PackageManager pm, String pkg, int resId) { 64 try { 65 return pm.getResourcesForApplication(pkg).getDrawable(resId, null); 66 } catch (Resources.NotFoundException | PackageManager.NameNotFoundException e) { 67 Log.d(LOG_TAG, "Couldn't get resource", e); 68 return null; 69 } 70 } 71 isModernPermissionGroup(String name)72 public static boolean isModernPermissionGroup(String name) { 73 for (String modernGroup : MODERN_PERMISSION_GROUPS) { 74 if (modernGroup.equals(name)) { 75 return true; 76 } 77 } 78 return false; 79 } 80 shouldShowPermission(AppPermissionGroup group, String packageName)81 public static boolean shouldShowPermission(AppPermissionGroup group, String packageName) { 82 // We currently will not show permissions fixed by the system. 83 // which is what the system does for system components. 84 if (group.isSystemFixed() && !LocationUtils.isLocationGroupAndProvider( 85 group.getName(), packageName)) { 86 return false; 87 } 88 89 if (!group.isGrantingAllowed()) { 90 return false; 91 } 92 93 final boolean isPlatformPermission = group.getDeclaringPackage().equals(OS_PKG); 94 // Show legacy permissions only if the user chose that. 95 if (isPlatformPermission 96 && !Utils.isModernPermissionGroup(group.getName())) { 97 return false; 98 } 99 return true; 100 } 101 shouldShowPermission(PermissionApp app)102 public static boolean shouldShowPermission(PermissionApp app) { 103 // We currently will not show permissions fixed by the system 104 // which is what the system does for system components. 105 if (app.isSystemFixed() && !LocationUtils.isLocationGroupAndProvider( 106 app.getPermissionGroup().getName(), app.getPackageName())) { 107 return false; 108 } 109 110 return true; 111 } 112 applyTint(Context context, Drawable icon, int attr)113 public static Drawable applyTint(Context context, Drawable icon, int attr) { 114 Theme theme = context.getTheme(); 115 TypedValue typedValue = new TypedValue(); 116 theme.resolveAttribute(attr, typedValue, true); 117 icon = icon.mutate(); 118 icon.setTint(context.getColor(typedValue.resourceId)); 119 return icon; 120 } 121 applyTint(Context context, int iconResId, int attr)122 public static Drawable applyTint(Context context, int iconResId, int attr) { 123 return applyTint(context, context.getDrawable(iconResId), attr); 124 } 125 getLauncherPackages(Context context)126 public static ArraySet<String> getLauncherPackages(Context context) { 127 ArraySet<String> launcherPkgs = new ArraySet<>(); 128 for (ResolveInfo info : 129 context.getPackageManager().queryIntentActivities(LAUNCHER_INTENT, 0)) { 130 launcherPkgs.add(info.activityInfo.packageName); 131 } 132 133 return launcherPkgs; 134 } 135 getAllInstalledApplications(Context context)136 public static List<ApplicationInfo> getAllInstalledApplications(Context context) { 137 return context.getPackageManager().getInstalledApplications(0); 138 } 139 isSystem(PermissionApp app, ArraySet<String> launcherPkgs)140 public static boolean isSystem(PermissionApp app, ArraySet<String> launcherPkgs) { 141 return isSystem(app.getAppInfo(), launcherPkgs); 142 } 143 isSystem(AppPermissions app, ArraySet<String> launcherPkgs)144 public static boolean isSystem(AppPermissions app, ArraySet<String> launcherPkgs) { 145 return isSystem(app.getPackageInfo().applicationInfo, launcherPkgs); 146 } 147 isSystem(ApplicationInfo info, ArraySet<String> launcherPkgs)148 public static boolean isSystem(ApplicationInfo info, ArraySet<String> launcherPkgs) { 149 return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 150 && !launcherPkgs.contains(info.packageName); 151 } 152 areGroupPermissionsIndividuallyControlled(Context context, String group)153 public static boolean areGroupPermissionsIndividuallyControlled(Context context, String group) { 154 if (!context.getPackageManager().isPermissionReviewModeEnabled()) { 155 return false; 156 } 157 return Manifest.permission_group.SMS.equals(group) 158 || Manifest.permission_group.PHONE.equals(group) 159 || Manifest.permission_group.CONTACTS.equals(group); 160 } 161 isPermissionIndividuallyControlled(Context context, String permission)162 public static boolean isPermissionIndividuallyControlled(Context context, String permission) { 163 if (!context.getPackageManager().isPermissionReviewModeEnabled()) { 164 return false; 165 } 166 return Manifest.permission.READ_CONTACTS.equals(permission) 167 || Manifest.permission.WRITE_CONTACTS.equals(permission) 168 || Manifest.permission.SEND_SMS.equals(permission) 169 || Manifest.permission.RECEIVE_SMS.equals(permission) 170 || Manifest.permission.READ_SMS.equals(permission) 171 || Manifest.permission.RECEIVE_MMS.equals(permission) 172 || Manifest.permission.CALL_PHONE.equals(permission) 173 || Manifest.permission.READ_CALL_LOG.equals(permission) 174 || Manifest.permission.WRITE_CALL_LOG.equals(permission); 175 } 176 } 177