1 /* 2 * Copyright (C) 2016 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.launcher3.util; 18 19 import android.app.AppOpsManager; 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.PackageManager.NameNotFoundException; 25 import android.content.pm.ResolveInfo; 26 import android.os.Build; 27 import android.text.TextUtils; 28 29 import com.android.launcher3.Utilities; 30 31 import java.util.ArrayList; 32 33 /** 34 * Utility methods using package manager 35 */ 36 public class PackageManagerHelper { 37 38 private static final int FLAG_SUSPENDED = 1<<30; 39 private static final String LIVE_WALLPAPER_PICKER_PKG = "com.android.wallpaper.livepicker"; 40 41 /** 42 * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't 43 * guarantee that the app is on SD card. 44 */ 45 public static boolean isAppOnSdcard(PackageManager pm, String packageName) { 46 return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES); 47 } 48 49 public static boolean isAppEnabled(PackageManager pm, String packageName) { 50 return isAppEnabled(pm, packageName, 0); 51 } 52 53 public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) { 54 try { 55 ApplicationInfo info = pm.getApplicationInfo(packageName, flags); 56 return info != null && info.enabled; 57 } catch (PackageManager.NameNotFoundException e) { 58 return false; 59 } 60 } 61 62 public static boolean isAppSuspended(PackageManager pm, String packageName) { 63 try { 64 ApplicationInfo info = pm.getApplicationInfo(packageName, 0); 65 return info != null && isAppSuspended(info); 66 } catch (PackageManager.NameNotFoundException e) { 67 return false; 68 } 69 } 70 71 public static boolean isAppSuspended(ApplicationInfo info) { 72 // The value of FLAG_SUSPENDED was reused by a hidden constant 73 // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N 74 // or later. 75 if (Utilities.isNycOrAbove()) { 76 return (info.flags & FLAG_SUSPENDED) != 0; 77 } else { 78 return false; 79 } 80 } 81 82 /** 83 * Returns the package for a wallpaper picker system app giving preference to a app which 84 * is not as image picker. 85 */ 86 public static String getWallpaperPickerPackage(PackageManager pm) { 87 ArrayList<String> excludePackages = new ArrayList<>(); 88 // Exclude packages which contain an image picker 89 for (ResolveInfo info : pm.queryIntentActivities( 90 new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"), 0)) { 91 excludePackages.add(info.activityInfo.packageName); 92 } 93 excludePackages.add(LIVE_WALLPAPER_PICKER_PKG); 94 95 for (ResolveInfo info : pm.queryIntentActivities( 96 new Intent(Intent.ACTION_SET_WALLPAPER), 0)) { 97 if (!excludePackages.contains(info.activityInfo.packageName) && 98 (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 99 return info.activityInfo.packageName; 100 } 101 } 102 return excludePackages.get(0); 103 } 104 105 /** 106 * Returns true if {@param srcPackage} has the permission required to start the activity from 107 * {@param intent}. If {@param srcPackage} is null, then the activity should not need 108 * any permissions 109 */ 110 public static boolean hasPermissionForActivity(Context context, Intent intent, 111 String srcPackage) { 112 PackageManager pm = context.getPackageManager(); 113 ResolveInfo target = pm.resolveActivity(intent, 0); 114 if (target == null) { 115 // Not a valid target 116 return false; 117 } 118 if (TextUtils.isEmpty(target.activityInfo.permission)) { 119 // No permission is needed 120 return true; 121 } 122 if (TextUtils.isEmpty(srcPackage)) { 123 // The activity requires some permission but there is no source. 124 return false; 125 } 126 127 // Source does not have sufficient permissions. 128 if(pm.checkPermission(target.activityInfo.permission, srcPackage) != 129 PackageManager.PERMISSION_GRANTED) { 130 return false; 131 } 132 133 if (!Utilities.ATLEAST_MARSHMALLOW) { 134 // These checks are sufficient for below M devices. 135 return true; 136 } 137 138 // On M and above also check AppOpsManager for compatibility mode permissions. 139 if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) { 140 // There is no app-op for this permission, which could have been disabled. 141 return true; 142 } 143 144 // There is no direct way to check if the app-op is allowed for a particular app. Since 145 // app-op is only enabled for apps running in compatibility mode, simply block such apps. 146 147 try { 148 return pm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M; 149 } catch (NameNotFoundException e) { } 150 151 return false; 152 } 153 } 154