1 /*
2  * Copyright (C) 2017 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 package com.android.providers.contacts.util;
17 
18 import android.content.Context;
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 
23 public class PackageUtils {
PackageUtils()24     private PackageUtils() {
25     }
26 
27     /**
28      * @return TRUE if the given package is installed for this user.
29      */
isPackageInstalled(Context context, String packageName)30     public static boolean isPackageInstalled(Context context, String packageName) {
31         try {
32             // Need to pass MATCH_UNINSTALLED_PACKAGES to fetch it even if the package is
33             // being updated.  Then use FLAG_INSTALLED to see if it's actually installed for this
34             // user.
35             final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName,
36                     PackageManager.MATCH_DIRECT_BOOT_AWARE
37                             | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
38                             | PackageManager.MATCH_UNINSTALLED_PACKAGES);
39             return (ai != null) && ((ai.flags & ApplicationInfo.FLAG_INSTALLED) != 0);
40         } catch (NameNotFoundException e) {
41             return false;
42         }
43     }
44 }
45