OSDN Git Service

Making methods in PaackageManagerHelper non-static
[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.LauncherActivityInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.content.pm.ResolveInfo;
27 import android.net.Uri;
28 import android.os.Build;
29 import android.os.UserHandle;
30 import android.text.TextUtils;
31
32 import com.android.launcher3.AppInfo;
33 import com.android.launcher3.Utilities;
34 import com.android.launcher3.compat.LauncherAppsCompat;
35
36 import java.util.List;
37
38 /**
39  * Utility methods using package manager
40  */
41 public class PackageManagerHelper {
42
43     private static final int FLAG_SUSPENDED = 1<<30;
44
45     private final Context mContext;
46     private final PackageManager mPm;
47
48     public PackageManagerHelper(Context context) {
49         mContext = context;
50         mPm = context.getPackageManager();
51     }
52
53     /**
54      * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
55      * guarantee that the app is on SD card.
56      */
57     public boolean isAppOnSdcard(String packageName) {
58         return isAppEnabled(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
59     }
60
61     public boolean isAppEnabled(String packageName) {
62         return isAppEnabled(packageName, 0);
63     }
64
65     public boolean isAppEnabled(String packageName, int flags) {
66         try {
67             ApplicationInfo info = mPm.getApplicationInfo(packageName, flags);
68             return info != null && info.enabled;
69         } catch (PackageManager.NameNotFoundException e) {
70             return false;
71         }
72     }
73
74     public boolean isAppSuspended(String packageName) {
75         try {
76             ApplicationInfo info = mPm.getApplicationInfo(packageName, 0);
77             return info != null && isAppSuspended(info);
78         } catch (PackageManager.NameNotFoundException e) {
79             return false;
80         }
81     }
82
83     public boolean isSafeMode() {
84         return mPm.isSafeMode();
85     }
86
87     public Intent getAppLaunchIntent(String pkg, UserHandle user) {
88         List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(mContext)
89                 .getActivityList(pkg, user);
90         return activities.isEmpty() ? null :
91                 AppInfo.makeLaunchIntent(mContext, activities.get(0), user);
92     }
93
94     public static boolean isAppSuspended(ApplicationInfo info) {
95         // The value of FLAG_SUSPENDED was reused by a hidden constant
96         // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N
97         // or later.
98         if (Utilities.ATLEAST_NOUGAT) {
99             return (info.flags & FLAG_SUSPENDED) != 0;
100         } else {
101             return false;
102         }
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 boolean hasPermissionForActivity(Intent intent, String srcPackage) {
111         ResolveInfo target = mPm.resolveActivity(intent, 0);
112         if (target == null) {
113             // Not a valid target
114             return false;
115         }
116         if (TextUtils.isEmpty(target.activityInfo.permission)) {
117             // No permission is needed
118             return true;
119         }
120         if (TextUtils.isEmpty(srcPackage)) {
121             // The activity requires some permission but there is no source.
122             return false;
123         }
124
125         // Source does not have sufficient permissions.
126         if(mPm.checkPermission(target.activityInfo.permission, srcPackage) !=
127                 PackageManager.PERMISSION_GRANTED) {
128             return false;
129         }
130
131         if (!Utilities.ATLEAST_MARSHMALLOW) {
132             // These checks are sufficient for below M devices.
133             return true;
134         }
135
136         // On M and above also check AppOpsManager for compatibility mode permissions.
137         if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) {
138             // There is no app-op for this permission, which could have been disabled.
139             return true;
140         }
141
142         // There is no direct way to check if the app-op is allowed for a particular app. Since
143         // app-op is only enabled for apps running in compatibility mode, simply block such apps.
144
145         try {
146             return mPm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
147         } catch (NameNotFoundException e) { }
148
149         return false;
150     }
151
152     public static Intent getMarketIntent(String packageName) {
153         return new Intent(Intent.ACTION_VIEW)
154                 .setData(new Uri.Builder()
155                         .scheme("market")
156                         .authority("details")
157                         .appendQueryParameter("id", packageName)
158                         .build());
159     }
160 }