OSDN Git Service

33a9fc68d7aa101aee5a5dca6359e1e440c44edf
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / util / PackageManagerHelper.java
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.net.Uri;
27 import android.os.Build;
28 import android.text.TextUtils;
29
30 import com.android.launcher3.Utilities;
31
32 /**
33  * Utility methods using package manager
34  */
35 public class PackageManagerHelper {
36
37     private static final int FLAG_SUSPENDED = 1<<30;
38
39     /**
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.
42      */
43     public static boolean isAppOnSdcard(PackageManager pm, String packageName) {
44         return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
45     }
46
47     public static boolean isAppEnabled(PackageManager pm, String packageName) {
48         return isAppEnabled(pm, packageName, 0);
49     }
50
51     public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
52         try {
53             ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
54             return info != null && info.enabled;
55         } catch (PackageManager.NameNotFoundException e) {
56             return false;
57         }
58     }
59
60     public static boolean isAppSuspended(PackageManager pm, String packageName) {
61         try {
62             ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
63             return info != null && isAppSuspended(info);
64         } catch (PackageManager.NameNotFoundException e) {
65             return false;
66         }
67     }
68
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
72         // or later.
73         if (Utilities.ATLEAST_NOUGAT) {
74             return (info.flags & FLAG_SUSPENDED) != 0;
75         } else {
76             return false;
77         }
78     }
79
80     /**
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
83      * any permissions
84      */
85     public static boolean hasPermissionForActivity(Context context, Intent intent,
86             String srcPackage) {
87         PackageManager pm = context.getPackageManager();
88         ResolveInfo target = pm.resolveActivity(intent, 0);
89         if (target == null) {
90             // Not a valid target
91             return false;
92         }
93         if (TextUtils.isEmpty(target.activityInfo.permission)) {
94             // No permission is needed
95             return true;
96         }
97         if (TextUtils.isEmpty(srcPackage)) {
98             // The activity requires some permission but there is no source.
99             return false;
100         }
101
102         // Source does not have sufficient permissions.
103         if(pm.checkPermission(target.activityInfo.permission, srcPackage) !=
104                 PackageManager.PERMISSION_GRANTED) {
105             return false;
106         }
107
108         if (!Utilities.ATLEAST_MARSHMALLOW) {
109             // These checks are sufficient for below M devices.
110             return true;
111         }
112
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.
116             return true;
117         }
118
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.
121
122         try {
123             return pm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
124         } catch (NameNotFoundException e) { }
125
126         return false;
127     }
128
129     public static Intent getMarketIntent(String packageName) {
130         return new Intent(Intent.ACTION_VIEW)
131                 .setData(new Uri.Builder()
132                         .scheme("market")
133                         .authority("details")
134                         .appendQueryParameter("id", packageName)
135                         .build());
136     }
137 }