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