1 /* 2 * Copyright (C) 2014 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.launcher3.compat; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.content.pm.ActivityInfo; 24 import android.content.pm.PackageManager; 25 import android.content.pm.PackageInfo; 26 import android.content.pm.ResolveInfo; 27 import android.content.res.Resources; 28 import android.graphics.drawable.Drawable; 29 import android.util.Log; 30 31 32 public class LauncherActivityInfoCompatV16 extends LauncherActivityInfoCompat { 33 private final ResolveInfo mResolveInfo; 34 private final ActivityInfo mActivityInfo; 35 private final ComponentName mComponentName; 36 private final PackageManager mPm; 37 38 LauncherActivityInfoCompatV16(Context context, ResolveInfo info) { 39 super(); 40 mResolveInfo = info; 41 mActivityInfo = info.activityInfo; 42 mComponentName = new ComponentName(mActivityInfo.packageName, mActivityInfo.name); 43 mPm = context.getPackageManager(); 44 } 45 46 public ComponentName getComponentName() { 47 return mComponentName; 48 } 49 50 public UserHandleCompat getUser() { 51 return UserHandleCompat.myUserHandle(); 52 } 53 54 public CharSequence getLabel() { 55 try { 56 return mResolveInfo.loadLabel(mPm); 57 } catch (SecurityException e) { 58 Log.e("LAInfoCompat", "Failed to extract app display name from resolve info", e); 59 return ""; 60 } 61 } 62 63 public Drawable getIcon(int density) { 64 int iconRes = mResolveInfo.getIconResource(); 65 Resources resources = null; 66 Drawable icon = null; 67 // Get the preferred density icon from the app's resources 68 if (density != 0 && iconRes != 0) { 69 try { 70 resources = mPm.getResourcesForApplication(mActivityInfo.applicationInfo); 71 icon = resources.getDrawableForDensity(iconRes, density); 72 } catch (NameNotFoundException | Resources.NotFoundException exc) { 73 } 74 } 75 // Get the default density icon 76 if (icon == null) { 77 icon = mResolveInfo.loadIcon(mPm); 78 } 79 if (icon == null) { 80 resources = Resources.getSystem(); 81 icon = resources.getDrawableForDensity(android.R.mipmap.sym_def_app_icon, density); 82 } 83 return icon; 84 } 85 86 public ApplicationInfo getApplicationInfo() { 87 return mActivityInfo.applicationInfo; 88 } 89 90 public long getFirstInstallTime() { 91 try { 92 PackageInfo info = mPm.getPackageInfo(mActivityInfo.packageName, 0); 93 return info != null ? info.firstInstallTime : 0; 94 } catch (NameNotFoundException e) { 95 return 0; 96 } 97 } 98 99 public String getName() { 100 return mActivityInfo.name; 101 } 102 } 103