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.PackageManager; 22 import android.os.UserHandle; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 /** 28 * Utility methods about application packages. 29 */ 30 public final class PackageUtils { 31 PackageUtils()32 private PackageUtils() {} 33 34 /** 35 * Retrieve the {@link ApplicationInfo} of an application. 36 * 37 * @param packageName the package name of the application 38 * @param context the {@code Context} to retrieve system services 39 * 40 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 41 */ 42 @Nullable getApplicationInfo(@onNull String packageName, @NonNull Context context)43 public static ApplicationInfo getApplicationInfo(@NonNull String packageName, 44 @NonNull Context context) { 45 PackageManager packageManager = context.getPackageManager(); 46 try { 47 return packageManager.getApplicationInfo(packageName, 48 PackageManager.MATCH_DIRECT_BOOT_AWARE 49 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 50 } catch (PackageManager.NameNotFoundException e) { 51 return null; 52 } 53 } 54 55 /** 56 * Retrieve the {@link ApplicationInfo} of an application. 57 * 58 * @param packageName the package name of the application 59 * @param user the user of the application 60 * @param context the {@code Context} to retrieve system services 61 * 62 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 63 */ 64 @Nullable getApplicationInfoAsUser(@onNull String packageName, @NonNull UserHandle user, @NonNull Context context)65 public static ApplicationInfo getApplicationInfoAsUser(@NonNull String packageName, 66 @NonNull UserHandle user, @NonNull Context context) { 67 return getApplicationInfo(packageName, UserUtils.getUserContext(context, user)); 68 } 69 } 70