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