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.Configuration; 26 import android.content.res.Resources; 27 import android.content.res.Resources.Theme; 28 import android.graphics.drawable.Drawable; 29 import android.util.ArraySet; 30 import android.util.Log; 31 import android.util.TypedValue; 32 33 import com.android.packageinstaller.permission.model.AppPermissionGroup; 34 import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; 35 36 public class Utils { 37 38 private static final String LOG_TAG = "Utils"; 39 40 public static final String OS_PKG = "android"; 41 42 public static final String[] MODERN_PERMISSION_GROUPS = { 43 Manifest.permission_group.CALENDAR, 44 Manifest.permission_group.CAMERA, 45 Manifest.permission_group.CONTACTS, 46 Manifest.permission_group.LOCATION, 47 Manifest.permission_group.SENSORS, 48 Manifest.permission_group.SMS, 49 Manifest.permission_group.PHONE, 50 Manifest.permission_group.MICROPHONE, 51 Manifest.permission_group.STORAGE 52 }; 53 54 private static final Intent LAUNCHER_INTENT = new Intent(Intent.ACTION_MAIN, null) 55 .addCategory(Intent.CATEGORY_LAUNCHER); 56 57 private Utils() { 58 /* do nothing - hide constructor */ 59 } 60 61 public static Drawable loadDrawable(PackageManager pm, String pkg, int resId) { 62 try { 63 return pm.getResourcesForApplication(pkg).getDrawable(resId, null); 64 } catch (Resources.NotFoundException | PackageManager.NameNotFoundException e) { 65 Log.d(LOG_TAG, "Couldn't get resource", e); 66 return null; 67 } 68 } 69 70 public static boolean isModernPermissionGroup(String name) { 71 for (String modernGroup : MODERN_PERMISSION_GROUPS) { 72 if (modernGroup.equals(name)) { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 public static boolean shouldShowPermission(AppPermissionGroup group, String packageName) { 80 // We currently will not show permissions fixed by the system. 81 // which is what the system does for system components. 82 if (group.isSystemFixed() && !LocationUtils.isLocationGroupAndProvider( 83 group.getName(), packageName)) { 84 return false; 85 } 86 87 final boolean isPlatformPermission = group.getDeclaringPackage().equals(OS_PKG); 88 // Show legacy permissions only if the user chose that. 89 if (isPlatformPermission 90 && !Utils.isModernPermissionGroup(group.getName())) { 91 return false; 92 } 93 return true; 94 } 95 96 public static boolean shouldShowPermission(PermissionApp app) { 97 // We currently will not show permissions fixed by the system 98 // which is what the system does for system components. 99 if (app.isSystemFixed() && !LocationUtils.isLocationGroupAndProvider( 100 app.getPermissionGroup().getName(), app.getPackageName())) { 101 return false; 102 } 103 104 return true; 105 } 106 107 public static Drawable applyTint(Context context, Drawable icon, int attr) { 108 Theme theme = context.getTheme(); 109 TypedValue typedValue = new TypedValue(); 110 theme.resolveAttribute(attr, typedValue, true); 111 icon = icon.mutate(); 112 icon.setTint(context.getColor(typedValue.resourceId)); 113 return icon; 114 } 115 116 public static Drawable applyTint(Context context, int iconResId, int attr) { 117 return applyTint(context, context.getDrawable(iconResId), attr); 118 } 119 120 public static ArraySet<String> getLauncherPackages(Context context) { 121 ArraySet<String> launcherPkgs = new ArraySet<>(); 122 for (ResolveInfo info : 123 context.getPackageManager().queryIntentActivities(LAUNCHER_INTENT, 0)) { 124 launcherPkgs.add(info.activityInfo.packageName); 125 } 126 127 return launcherPkgs; 128 } 129 130 public static boolean isSystem(PermissionApp app, ArraySet<String> launcherPkgs) { 131 ApplicationInfo info = app.getAppInfo(); 132 return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 133 && !launcherPkgs.contains(info.packageName); 134 } 135 136 public static boolean isTelevision(Context context) { 137 int uiMode = context.getResources().getConfiguration().uiMode; 138 return (uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION; 139 } 140 } 141