1 /* 2 * Copyright (C) 2016 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 package com.example.android.pm.shortcutdemo; 17 18 import android.content.Context; 19 import android.content.pm.ApplicationInfo; 20 import android.content.pm.PackageManager; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.util.ArrayMap; 23 24 public class AppLabelCache { 25 private final Context mContext; 26 private ArrayMap<String, String> mAppNames = new ArrayMap<>(); 27 28 public AppLabelCache(Context context) { 29 mContext = context; 30 } 31 32 public String getAppLabel(String packageName) { 33 String name = mAppNames.get(packageName); 34 if (name != null) { 35 return name; 36 } 37 PackageManager pm = mContext.getPackageManager(); 38 try { 39 final ApplicationInfo ai = pm.getApplicationInfo(packageName, 0); 40 name = pm.getApplicationLabel(ai).toString(); 41 } catch (NameNotFoundException e) { 42 return packageName; 43 } 44 mAppNames.put(packageName, name); 45 return name; 46 } 47 } 48