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 android.content.pm; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.pm.PackageManager.NameNotFoundException; 22import android.content.res.Resources; 23import android.graphics.drawable.BitmapDrawable; 24import android.graphics.drawable.Drawable; 25import android.os.UserHandle; 26import android.os.UserManager; 27import android.util.DisplayMetrics; 28import android.util.Log; 29 30/** 31 * A representation of an activity that can belong to this user or a managed 32 * profile associated with this user. It can be used to query the label, icon 33 * and badged icon for the activity. 34 */ 35public class LauncherActivityInfo { 36 private static final String TAG = "LauncherActivityInfo"; 37 38 private final PackageManager mPm; 39 40 private ActivityInfo mActivityInfo; 41 private ComponentName mComponentName; 42 private UserHandle mUser; 43 44 /** 45 * Create a launchable activity object for a given ResolveInfo and user. 46 * 47 * @param context The context for fetching resources. 48 * @param info ResolveInfo from which to create the LauncherActivityInfo. 49 * @param user The UserHandle of the profile to which this activity belongs. 50 */ 51 LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user) { 52 this(context); 53 mActivityInfo = info; 54 mComponentName = new ComponentName(info.packageName, info.name); 55 mUser = user; 56 } 57 58 LauncherActivityInfo(Context context) { 59 mPm = context.getPackageManager(); 60 } 61 62 /** 63 * Returns the component name of this activity. 64 * 65 * @return ComponentName of the activity 66 */ 67 public ComponentName getComponentName() { 68 return mComponentName; 69 } 70 71 /** 72 * Returns the user handle of the user profile that this activity belongs to. In order to 73 * persist the identity of the profile, do not store the UserHandle. Instead retrieve its 74 * serial number from UserManager. You can convert the serial number back to a UserHandle 75 * for later use. 76 * 77 * @see UserManager#getSerialNumberForUser(UserHandle) 78 * @see UserManager#getUserForSerialNumber(long) 79 * 80 * @return The UserHandle of the profile. 81 */ 82 public UserHandle getUser() { 83 return mUser; 84 } 85 86 /** 87 * Retrieves the label for the activity. 88 * 89 * @return The label for the activity. 90 */ 91 public CharSequence getLabel() { 92 return mActivityInfo.loadLabel(mPm); 93 } 94 95 /** 96 * Returns the icon for this activity, without any badging for the profile. 97 * @param density The preferred density of the icon, zero for default density. Use 98 * density DPI values from {@link DisplayMetrics}. 99 * @see #getBadgedIcon(int) 100 * @see DisplayMetrics 101 * @return The drawable associated with the activity. 102 */ 103 public Drawable getIcon(int density) { 104 final int iconRes = mActivityInfo.getIconResource(); 105 Drawable icon = null; 106 // Get the preferred density icon from the app's resources 107 if (density != 0 && iconRes != 0) { 108 try { 109 final Resources resources 110 = mPm.getResourcesForApplication(mActivityInfo.applicationInfo); 111 icon = resources.getDrawableForDensity(iconRes, density); 112 } catch (NameNotFoundException | Resources.NotFoundException exc) { 113 } 114 } 115 // Get the default density icon 116 if (icon == null) { 117 icon = mActivityInfo.loadIcon(mPm); 118 } 119 return icon; 120 } 121 122 /** 123 * Returns the application flags from the ApplicationInfo of the activity. 124 * 125 * @return Application flags 126 * @hide remove before shipping 127 */ 128 public int getApplicationFlags() { 129 return mActivityInfo.applicationInfo.flags; 130 } 131 132 /** 133 * Returns the application info for the appliction this activity belongs to. 134 * @return 135 */ 136 public ApplicationInfo getApplicationInfo() { 137 return mActivityInfo.applicationInfo; 138 } 139 140 /** 141 * Returns the time at which the package was first installed. 142 * 143 * @return The time of installation of the package, in milliseconds. 144 */ 145 public long getFirstInstallTime() { 146 try { 147 return mPm.getPackageInfo(mActivityInfo.packageName, 148 PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime; 149 } catch (NameNotFoundException nnfe) { 150 // Sorry, can't find package 151 return 0; 152 } 153 } 154 155 /** 156 * Returns the name for the acitivty from android:name in the manifest. 157 * @return the name from android:name for the acitivity. 158 */ 159 public String getName() { 160 return mActivityInfo.name; 161 } 162 163 /** 164 * Returns the activity icon with badging appropriate for the profile. 165 * @param density Optional density for the icon, or 0 to use the default density. Use 166 * {@link DisplayMetrics} for DPI values. 167 * @see DisplayMetrics 168 * @return A badged icon for the activity. 169 */ 170 public Drawable getBadgedIcon(int density) { 171 Drawable originalIcon = getIcon(density); 172 173 if (originalIcon instanceof BitmapDrawable) { 174 return mPm.getUserBadgedIcon(originalIcon, mUser); 175 } else { 176 Log.e(TAG, "Unable to create badged icon for " + mActivityInfo); 177 } 178 return originalIcon; 179 } 180} 181