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.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 
22 import com.android.launcher3.Utilities;
23 
24 /**
25  * Utility methods using package manager
26  */
27 public class PackageManagerHelper {
28 
29     private static final int FLAG_SUSPENDED = 1<<30;
30 
31     /**
32      * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
33      * guarantee that the app is on SD card.
34      */
isAppOnSdcard(PackageManager pm, String packageName)35     public static boolean isAppOnSdcard(PackageManager pm, String packageName) {
36         return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
37     }
38 
isAppEnabled(PackageManager pm, String packageName)39     public static boolean isAppEnabled(PackageManager pm, String packageName) {
40         return isAppEnabled(pm, packageName, 0);
41     }
42 
isAppEnabled(PackageManager pm, String packageName, int flags)43     public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
44         try {
45             ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
46             return info != null && info.enabled;
47         } catch (PackageManager.NameNotFoundException e) {
48             return false;
49         }
50     }
51 
isAppSuspended(PackageManager pm, String packageName)52     public static boolean isAppSuspended(PackageManager pm, String packageName) {
53         try {
54             ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
55             return info != null && isAppSuspended(info);
56         } catch (PackageManager.NameNotFoundException e) {
57             return false;
58         }
59     }
60 
isAppSuspended(ApplicationInfo info)61     public static boolean isAppSuspended(ApplicationInfo info) {
62         // The value of FLAG_SUSPENDED was reused by a hidden constant
63         // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N
64         // or later.
65         if (Utilities.ATLEAST_N) {
66             return (info.flags & FLAG_SUSPENDED) != 0;
67         } else {
68             return false;
69         }
70     }
71 }
72