1 /* 2 * Copyright (C) 2020 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.server.telecom; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import com.android.server.telecom.flags.FeatureFlags; 23 import android.os.UserHandle; 24 import android.telecom.Log; 25 26 /** 27 * Abstracts away dependency on the {@link PackageManager} required to fetch the label for an 28 * app. 29 */ 30 public interface AppLabelProxy { 31 String LOG_TAG = AppLabelProxy.class.getSimpleName(); 32 33 class Util { 34 /** 35 * Default impl of getAppLabel. 36 * @param context Context instance that is not necessarily associated with the correct user. 37 * @param userHandle UserHandle instance of the user that is associated with the app. 38 * @param packageName package name to look up. 39 */ getAppLabel(Context context, UserHandle userHandle, String packageName, FeatureFlags featureFlags)40 public static CharSequence getAppLabel(Context context, UserHandle userHandle, 41 String packageName, FeatureFlags featureFlags) { 42 try { 43 if (featureFlags.telecomAppLabelProxyHsumAware()){ 44 Context userContext = context.createContextAsUser(userHandle, 0 /* flags */); 45 PackageManager userPackageManager = userContext.getPackageManager(); 46 if (userPackageManager == null) { 47 Log.w(LOG_TAG, "Could not determine app label since PackageManager is " 48 + "null. Package name is %s", packageName); 49 return null; 50 } 51 ApplicationInfo info = userPackageManager.getApplicationInfo(packageName, 0); 52 CharSequence result = userPackageManager.getApplicationLabel(info); 53 Log.i(LOG_TAG, "package %s: name is %s for user = %s", packageName, result, 54 userHandle.toString()); 55 return result; 56 } else { 57 // Legacy code path: 58 PackageManager pm = context.getPackageManager(); 59 ApplicationInfo info = pm.getApplicationInfo(packageName, 0); 60 CharSequence result = pm.getApplicationLabel(info); 61 Log.i(LOG_TAG, "package %s: name is %s", packageName, result); 62 return result; 63 } 64 } catch (PackageManager.NameNotFoundException nnfe) { 65 Log.w(LOG_TAG, "Could not determine app label. Package name is %s", packageName); 66 } 67 68 return null; 69 } 70 } 71 getAppLabel(String packageName, UserHandle userHandle)72 CharSequence getAppLabel(String packageName, UserHandle userHandle); 73 } 74