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.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.os.UserHandle; 25 import android.provider.Settings; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 30 import java.util.Objects; 31 32 /** 33 * Utility methods about application packages. 34 */ 35 public final class PackageUtils { 36 PackageUtils()37 private PackageUtils() {} 38 39 /** 40 * Retrieve the {@link ApplicationInfo} of an application. 41 * 42 * @param packageName the package name of the application 43 * @param context the {@code Context} to retrieve system services 44 * 45 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 46 */ 47 @Nullable getApplicationInfo(@onNull String packageName, @NonNull Context context)48 public static ApplicationInfo getApplicationInfo(@NonNull String packageName, 49 @NonNull Context context) { 50 PackageManager packageManager = context.getPackageManager(); 51 try { 52 return packageManager.getApplicationInfo(packageName, 53 PackageManager.MATCH_DIRECT_BOOT_AWARE 54 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 55 } catch (PackageManager.NameNotFoundException e) { 56 return null; 57 } 58 } 59 60 /** 61 * Retrieve the {@link ApplicationInfo} of an application. 62 * 63 * @param packageName the package name of the application 64 * @param user the user of the application 65 * @param context the {@code Context} to retrieve system services 66 * 67 * @return the {@link ApplicationInfo} of the application, or {@code null} if not found 68 */ 69 @Nullable getApplicationInfoAsUser(@onNull String packageName, @NonNull UserHandle user, @NonNull Context context)70 public static ApplicationInfo getApplicationInfoAsUser(@NonNull String packageName, 71 @NonNull UserHandle user, @NonNull Context context) { 72 return getApplicationInfo(packageName, UserUtils.getUserContext(context, user)); 73 } 74 75 /** 76 * Check whether an intent is resolved to an activity inside Settings. 77 * 78 * @param intent the intent to check 79 * @param context the {@code Context} to retrieve system services 80 * 81 * @return whether the intent is resolved to an activity inside Settings, 82 */ isIntentResolvedToSettings(@onNull Intent intent, @NonNull Context context)83 public static boolean isIntentResolvedToSettings(@NonNull Intent intent, 84 @NonNull Context context) { 85 PackageManager packageManager = context.getPackageManager(); 86 ComponentName componentName = intent.resolveActivity(packageManager); 87 if (componentName == null) { 88 return false; 89 } 90 Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS); 91 String settingsPackageName = settingsIntent.resolveActivity(packageManager) 92 .getPackageName(); 93 return Objects.equals(componentName.getPackageName(), settingsPackageName); 94 } 95 } 96