1 /* 2 * Copyright (C) 2019 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.packageinstaller.permission.ui.auto; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.graphics.drawable.Drawable; 25 import android.os.UserHandle; 26 import android.util.Log; 27 28 import androidx.annotation.NonNull; 29 import androidx.preference.Preference; 30 31 import com.android.packageinstaller.permission.utils.Utils; 32 33 /** Common utilities shared between permissions settings. */ 34 public final class AutoPermissionsUtils { 35 private static final String LOG_TAG = "AutoPermissionsUtils"; 36 AutoPermissionsUtils()37 private AutoPermissionsUtils() { 38 } 39 40 /** Gets the {@link PackageInfo} for the given package name and user. */ getPackageInfo(Activity activity, @NonNull String packageName, @NonNull UserHandle userHandle)41 public static PackageInfo getPackageInfo(Activity activity, @NonNull String packageName, 42 @NonNull UserHandle userHandle) { 43 try { 44 return activity.createPackageContextAsUser(packageName, 0, userHandle) 45 .getPackageManager() 46 .getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); 47 } catch (PackageManager.NameNotFoundException e) { 48 Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); 49 return null; 50 } 51 } 52 53 /** Creates a {@link Preference} which shows the app icon and app name. */ createHeaderPreference(Context context, ApplicationInfo appInfo)54 public static Preference createHeaderPreference(Context context, ApplicationInfo appInfo) { 55 Drawable icon = Utils.getBadgedIcon(context, appInfo); 56 Preference preference = new Preference(context); 57 preference.setIcon(icon); 58 preference.setKey(appInfo.packageName); 59 preference.setTitle(Utils.getFullAppLabel(appInfo, context)); 60 return preference; 61 } 62 } 63