2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.util;
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.net.Uri;
27 import android.os.Build;
28 import android.text.TextUtils;
30 import com.android.launcher3.Utilities;
33 * Utility methods using package manager
35 public class PackageManagerHelper {
37 private static final int FLAG_SUSPENDED = 1<<30;
40 * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
41 * guarantee that the app is on SD card.
43 public static boolean isAppOnSdcard(PackageManager pm, String packageName) {
44 return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
47 public static boolean isAppEnabled(PackageManager pm, String packageName) {
48 return isAppEnabled(pm, packageName, 0);
51 public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
53 ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
54 return info != null && info.enabled;
55 } catch (PackageManager.NameNotFoundException e) {
60 public static boolean isAppSuspended(PackageManager pm, String packageName) {
62 ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
63 return info != null && isAppSuspended(info);
64 } catch (PackageManager.NameNotFoundException e) {
69 public static boolean isAppSuspended(ApplicationInfo info) {
70 // The value of FLAG_SUSPENDED was reused by a hidden constant
71 // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N
73 if (Utilities.ATLEAST_NOUGAT) {
74 return (info.flags & FLAG_SUSPENDED) != 0;
81 * Returns true if {@param srcPackage} has the permission required to start the activity from
82 * {@param intent}. If {@param srcPackage} is null, then the activity should not need
85 public static boolean hasPermissionForActivity(Context context, Intent intent,
87 PackageManager pm = context.getPackageManager();
88 ResolveInfo target = pm.resolveActivity(intent, 0);
93 if (TextUtils.isEmpty(target.activityInfo.permission)) {
94 // No permission is needed
97 if (TextUtils.isEmpty(srcPackage)) {
98 // The activity requires some permission but there is no source.
102 // Source does not have sufficient permissions.
103 if(pm.checkPermission(target.activityInfo.permission, srcPackage) !=
104 PackageManager.PERMISSION_GRANTED) {
108 if (!Utilities.ATLEAST_MARSHMALLOW) {
109 // These checks are sufficient for below M devices.
113 // On M and above also check AppOpsManager for compatibility mode permissions.
114 if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) {
115 // There is no app-op for this permission, which could have been disabled.
119 // There is no direct way to check if the app-op is allowed for a particular app. Since
120 // app-op is only enabled for apps running in compatibility mode, simply block such apps.
123 return pm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
124 } catch (NameNotFoundException e) { }
129 public static Intent getMarketIntent(String packageName) {
130 return new Intent(Intent.ACTION_VIEW)
131 .setData(new Uri.Builder()
133 .authority("details")
134 .appendQueryParameter("id", packageName)