1 /* 2 * Copyright (C) 2018 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.permissioncontroller.role.utils; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager; 23 import android.os.UserHandle; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 /** 29 * Utility methods about application packages. 30 */ 31 public final class PackageUtils { 32 PackageUtils()33 private PackageUtils() {} 34 35 /** 36 * Retrieve the {@link PackageInfo} of an application. 37 * 38 * @param packageName the package name of the application 39 * @param extraFlags the extra flags to pass to {@link PackageManager#getPackageInfo(String, 40 * int)} 41 * @param context the {@code Context} to retrieve system services 42 * 43 * @return the {@link PackageInfo} of the application, or {@code null} if not found 44 */ 45 @Nullable getPackageInfo(@onNull String packageName, int extraFlags, @NonNull Context context)46 public static PackageInfo getPackageInfo(@NonNull String packageName, int extraFlags, 47 @NonNull Context context) { 48 PackageManager packageManager = context.getPackageManager(); 49 try { 50 return packageManager.getPackageInfo(packageName, PackageManager.MATCH_DIRECT_BOOT_AWARE 51 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE | extraFlags); 52 } catch (PackageManager.NameNotFoundException e) { 53 return null; 54 } 55 } 56 57 /** 58 * Retrieve if a package is a system package. 59 * 60 * @param packageName the name of the package 61 * @param context the {@code Context} to retrieve system services 62 * 63 * @return whether the package is a system package 64 */ isSystemPackage(@onNull String packageName, @NonNull Context context)65 public static boolean isSystemPackage(@NonNull String packageName, @NonNull Context context) { 66 return getPackageInfo(packageName, PackageManager.MATCH_SYSTEM_ONLY, context) != null; 67 } 68 69 /** 70 * Retrieve the {@link ApplicationInfo} of an application. 71 * 72 * @param packageName the package name of the application 73 * @param context the {@code Context} to retrieve system services 74 * 75 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 76 */ 77 @Nullable getApplicationInfo(@onNull String packageName, @NonNull Context context)78 public static ApplicationInfo getApplicationInfo(@NonNull String packageName, 79 @NonNull Context context) { 80 PackageManager packageManager = context.getPackageManager(); 81 try { 82 return packageManager.getApplicationInfo(packageName, 83 PackageManager.MATCH_DIRECT_BOOT_AWARE 84 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 85 } catch (PackageManager.NameNotFoundException e) { 86 return null; 87 } 88 } 89 90 /** 91 * Retrieve the {@link ApplicationInfo} of an application. 92 * 93 * @param packageName the package name of the application 94 * @param user the user of the application 95 * @param context the {@code Context} to retrieve system services 96 * 97 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 98 */ 99 @Nullable getApplicationInfoAsUser(@onNull String packageName, @NonNull UserHandle user, @NonNull Context context)100 public static ApplicationInfo getApplicationInfoAsUser(@NonNull String packageName, 101 @NonNull UserHandle user, @NonNull Context context) { 102 return getApplicationInfo(packageName, UserUtils.getUserContext(context, user)); 103 } 104 } 105