• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
32  * Utility methods using package manager
33  */
34 public class PackageManagerHelper {
35 
36     private static final int FLAG_SUSPENDED = 1<<30;
37 
38     /**
39      * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
40      * guarantee that the app is on SD card.
41      */
isAppOnSdcard(PackageManager pm, String packageName)42     public static boolean isAppOnSdcard(PackageManager pm, String packageName) {
43         return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
44     }
45 
isAppEnabled(PackageManager pm, String packageName)46     public static boolean isAppEnabled(PackageManager pm, String packageName) {
47         return isAppEnabled(pm, packageName, 0);
48     }
49 
isAppEnabled(PackageManager pm, String packageName, int flags)50     public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
51         try {
52             ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
53             return info != null && info.enabled;
54         } catch (PackageManager.NameNotFoundException e) {
55             return false;
56         }
57     }
58 
isAppSuspended(PackageManager pm, String packageName)59     public static boolean isAppSuspended(PackageManager pm, String packageName) {
60         try {
61             ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
62             return info != null && isAppSuspended(info);
63         } catch (PackageManager.NameNotFoundException e) {
64             return false;
65         }
66     }
67 
isAppSuspended(ApplicationInfo info)68     public static boolean isAppSuspended(ApplicationInfo info) {
69         // The value of FLAG_SUSPENDED was reused by a hidden constant
70         // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N
71         // or later.
72         if (Utilities.ATLEAST_N) {
73             return (info.flags & FLAG_SUSPENDED) != 0;
74         } else {
75             return false;
76         }
77     }
78 
79     /**
80      * Returns true if {@param srcPackage} has the permission required to start the activity from
81      * {@param intent}. If {@param srcPackage} is null, then the activity should not need
82      * any permissions
83      */
hasPermissionForActivity(Context context, Intent intent, String srcPackage)84     public static boolean hasPermissionForActivity(Context context, Intent intent,
85             String srcPackage) {
86         PackageManager pm = context.getPackageManager();
87         ResolveInfo target = pm.resolveActivity(intent, 0);
88         if (target == null) {
89             // Not a valid target
90             return false;
91         }
92         if (TextUtils.isEmpty(target.activityInfo.permission)) {
93             // No permission is needed
94             return true;
95         }
96         if (TextUtils.isEmpty(srcPackage)) {
97             // The activity requires some permission but there is no source.
98             return false;
99         }
100 
101         // Source does not have sufficient permissions.
102         if(pm.checkPermission(target.activityInfo.permission, srcPackage) !=
103                 PackageManager.PERMISSION_GRANTED) {
104             return false;
105         }
106 
107         if (!Utilities.ATLEAST_MARSHMALLOW) {
108             // These checks are sufficient for below M devices.
109             return true;
110         }
111 
112         // On M and above also check AppOpsManager for compatibility mode permissions.
113         if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) {
114             // There is no app-op for this permission, which could have been disabled.
115             return true;
116         }
117 
118         // There is no direct way to check if the app-op is allowed for a particular app. Since
119         // app-op is only enabled for apps running in compatibility mode, simply block such apps.
120 
121         try {
122             return pm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
123         } catch (NameNotFoundException e) { }
124 
125         return false;
126     }
127 }
128