blob: a5617b46b403096a09a7fdaad5a0d46030ec451c [file] [log] [blame]
Amith Yamasani4f582632014-02-19 14:31:52 -08001/*
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;
Amith Yamasani30acde72014-04-25 15:56:26 -070021import android.content.pm.PackageManager.NameNotFoundException;
22import android.content.res.Resources;
Amith Yamasani4f582632014-02-19 14:31:52 -080023import android.graphics.drawable.BitmapDrawable;
24import android.graphics.drawable.Drawable;
Amith Yamasani4f582632014-02-19 14:31:52 -080025import android.os.UserHandle;
26import android.os.UserManager;
Amith Yamasanie781c812014-05-28 15:28:18 -070027import android.util.DisplayMetrics;
Amith Yamasani4f582632014-02-19 14:31:52 -080028import 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 {
Amith Yamasani30acde72014-04-25 15:56:26 -070036 private static final String TAG = "LauncherActivityInfo";
37
Amith Yamasani4f582632014-02-19 14:31:52 -080038 private final PackageManager mPm;
Amith Yamasani4f582632014-02-19 14:31:52 -080039
40 private ActivityInfo mActivityInfo;
41 private ComponentName mComponentName;
Amith Yamasani7e19f502015-01-30 14:18:52 -080042 private ResolveInfo mResolveInfo;
Amith Yamasani4f582632014-02-19 14:31:52 -080043 private UserHandle mUser;
Amith Yamasani4f582632014-02-19 14:31:52 -080044
45 /**
46 * Create a launchable activity object for a given ResolveInfo and user.
Amith Yamasanie781c812014-05-28 15:28:18 -070047 *
Amith Yamasani4f582632014-02-19 14:31:52 -080048 * @param context The context for fetching resources.
49 * @param info ResolveInfo from which to create the LauncherActivityInfo.
50 * @param user The UserHandle of the profile to which this activity belongs.
51 */
Sunny Goyal0736e202015-11-24 10:42:11 -080052 LauncherActivityInfo(Context context, ResolveInfo info, UserHandle user) {
Amith Yamasani4f582632014-02-19 14:31:52 -080053 this(context);
Amith Yamasani7e19f502015-01-30 14:18:52 -080054 mResolveInfo = info;
Amith Yamasanie781c812014-05-28 15:28:18 -070055 mActivityInfo = info.activityInfo;
56 mComponentName = LauncherApps.getComponentName(info);
57 mUser = user;
Amith Yamasani4f582632014-02-19 14:31:52 -080058 }
59
60 LauncherActivityInfo(Context context) {
61 mPm = context.getPackageManager();
Amith Yamasani4f582632014-02-19 14:31:52 -080062 }
63
64 /**
65 * Returns the component name of this activity.
66 *
67 * @return ComponentName of the activity
68 */
69 public ComponentName getComponentName() {
70 return mComponentName;
71 }
72
73 /**
Amith Yamasanie781c812014-05-28 15:28:18 -070074 * Returns the user handle of the user profile that this activity belongs to. In order to
75 * persist the identity of the profile, do not store the UserHandle. Instead retrieve its
76 * serial number from UserManager. You can convert the serial number back to a UserHandle
77 * for later use.
78 *
79 * @see UserManager#getSerialNumberForUser(UserHandle)
80 * @see UserManager#getUserForSerialNumber(long)
Amith Yamasani4f582632014-02-19 14:31:52 -080081 *
82 * @return The UserHandle of the profile.
83 */
84 public UserHandle getUser() {
85 return mUser;
86 }
87
88 /**
89 * Retrieves the label for the activity.
Amith Yamasanie781c812014-05-28 15:28:18 -070090 *
Amith Yamasani4f582632014-02-19 14:31:52 -080091 * @return The label for the activity.
92 */
93 public CharSequence getLabel() {
Amith Yamasani7e19f502015-01-30 14:18:52 -080094 return mResolveInfo.loadLabel(mPm);
Amith Yamasani4f582632014-02-19 14:31:52 -080095 }
96
97 /**
98 * Returns the icon for this activity, without any badging for the profile.
Amith Yamasanie781c812014-05-28 15:28:18 -070099 * @param density The preferred density of the icon, zero for default density. Use
100 * density DPI values from {@link DisplayMetrics}.
Amith Yamasani4f582632014-02-19 14:31:52 -0800101 * @see #getBadgedIcon(int)
Amith Yamasanie781c812014-05-28 15:28:18 -0700102 * @see DisplayMetrics
Ricky Wai1281b182015-04-29 14:57:04 +0100103 * @return The drawable associated with the activity.
Amith Yamasani4f582632014-02-19 14:31:52 -0800104 */
105 public Drawable getIcon(int density) {
Ricky Wai1281b182015-04-29 14:57:04 +0100106 final int iconRes = mResolveInfo.getIconResource();
107 Drawable icon = getDrawableForDensity(iconRes, density);
Amith Yamasani7e19f502015-01-30 14:18:52 -0800108 // Get the default density icon
109 if (icon == null) {
110 icon = mResolveInfo.loadIcon(mPm);
111 }
112 return icon;
Amith Yamasani4f582632014-02-19 14:31:52 -0800113 }
114
115 /**
Ricky Wai1281b182015-04-29 14:57:04 +0100116 * Returns the icon for this activity, without any badging for the profile.
117 * This function can get the icon no matter the icon needs to be badged or not.
118 * @param density The preferred density of the icon, zero for default density. Use
119 * density DPI values from {@link DisplayMetrics}.
120 * @see #getBadgedIcon(int)
121 * @see DisplayMetrics
122 * @return The drawable associated with the activity.
123 */
124 private Drawable getOriginalIcon(int density) {
125 final int iconRes = mResolveInfo.getIconResourceInternal();
126 Drawable icon = getDrawableForDensity(iconRes, density);
127 // Get the default density icon
128 if (icon == null) {
129 icon = mResolveInfo.loadIcon(mPm);
130 }
131 return icon;
132 }
133
134 /**
135 * Returns the drawable for this activity, without any badging for the profile.
Sunny Goyal0736e202015-11-24 10:42:11 -0800136 * @param iconRes id of the drawable.
Ricky Wai1281b182015-04-29 14:57:04 +0100137 * @param density The preferred density of the icon, zero for default density. Use
138 * density DPI values from {@link DisplayMetrics}.
139 * @see DisplayMetrics
140 * @return The drawable associated with the resource id.
141 */
142 private Drawable getDrawableForDensity(int iconRes, int density) {
143 // Get the preferred density icon from the app's resources
144 if (density != 0 && iconRes != 0) {
145 try {
146 final Resources resources
147 = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
148 return resources.getDrawableForDensity(iconRes, density);
149 } catch (NameNotFoundException | Resources.NotFoundException exc) {
150 }
151 }
152 return null;
153 }
154
155 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800156 * Returns the application flags from the ApplicationInfo of the activity.
Amith Yamasanie781c812014-05-28 15:28:18 -0700157 *
Amith Yamasani4f582632014-02-19 14:31:52 -0800158 * @return Application flags
Amith Yamasanie781c812014-05-28 15:28:18 -0700159 * @hide remove before shipping
Amith Yamasani4f582632014-02-19 14:31:52 -0800160 */
161 public int getApplicationFlags() {
162 return mActivityInfo.applicationInfo.flags;
163 }
164
165 /**
Amith Yamasanie781c812014-05-28 15:28:18 -0700166 * Returns the application info for the appliction this activity belongs to.
167 * @return
168 */
169 public ApplicationInfo getApplicationInfo() {
170 return mActivityInfo.applicationInfo;
171 }
172
173 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800174 * Returns the time at which the package was first installed.
Amith Yamasanie781c812014-05-28 15:28:18 -0700175 *
Amith Yamasani4f582632014-02-19 14:31:52 -0800176 * @return The time of installation of the package, in milliseconds.
177 */
178 public long getFirstInstallTime() {
Sunny Goyal0736e202015-11-24 10:42:11 -0800179 try {
180 return mPm.getPackageInfo(mActivityInfo.packageName,
181 PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
182 } catch (NameNotFoundException nnfe) {
183 // Sorry, can't find package
184 return 0;
185 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800186 }
187
188 /**
Kenny Guy86a64302014-04-25 20:48:32 +0100189 * Returns the name for the acitivty from android:name in the manifest.
190 * @return the name from android:name for the acitivity.
191 */
192 public String getName() {
193 return mActivityInfo.name;
194 }
195
196 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800197 * Returns the activity icon with badging appropriate for the profile.
Amith Yamasanie781c812014-05-28 15:28:18 -0700198 * @param density Optional density for the icon, or 0 to use the default density. Use
199 * {@link DisplayMetrics} for DPI values.
200 * @see DisplayMetrics
Amith Yamasani4f582632014-02-19 14:31:52 -0800201 * @return A badged icon for the activity.
202 */
203 public Drawable getBadgedIcon(int density) {
Ricky Wai1281b182015-04-29 14:57:04 +0100204 Drawable originalIcon = getOriginalIcon(density);
Amith Yamasani30acde72014-04-25 15:56:26 -0700205
Amith Yamasani4f582632014-02-19 14:31:52 -0800206 if (originalIcon instanceof BitmapDrawable) {
Svetoslavc7d62f02014-09-04 15:39:54 -0700207 return mPm.getUserBadgedIcon(originalIcon, mUser);
Amith Yamasani30acde72014-04-25 15:56:26 -0700208 } else {
209 Log.e(TAG, "Unable to create badged icon for " + mActivityInfo);
Amith Yamasani4f582632014-02-19 14:31:52 -0800210 }
211 return originalIcon;
212 }
213}