blob: 1451431b7852f8165f224eec7935e9feaa607f37 [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
Mathew Inwood5c0d3542018-08-14 13:54:31 +010019import android.annotation.UnsupportedAppUsage;
Amith Yamasani4f582632014-02-19 14:31:52 -080020import android.content.ComponentName;
21import android.content.Context;
Amith Yamasani30acde72014-04-25 15:56:26 -070022import android.content.pm.PackageManager.NameNotFoundException;
23import android.content.res.Resources;
Amith Yamasani4f582632014-02-19 14:31:52 -080024import 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 -080028
29/**
30 * A representation of an activity that can belong to this user or a managed
31 * profile associated with this user. It can be used to query the label, icon
32 * and badged icon for the activity.
33 */
34public class LauncherActivityInfo {
Amith Yamasani30acde72014-04-25 15:56:26 -070035 private static final String TAG = "LauncherActivityInfo";
36
Amith Yamasani4f582632014-02-19 14:31:52 -080037 private final PackageManager mPm;
Amith Yamasani4f582632014-02-19 14:31:52 -080038
Mathew Inwood5c0d3542018-08-14 13:54:31 +010039 @UnsupportedAppUsage
Amith Yamasani4f582632014-02-19 14:31:52 -080040 private ActivityInfo mActivityInfo;
41 private ComponentName mComponentName;
42 private UserHandle mUser;
Amith Yamasani4f582632014-02-19 14:31:52 -080043
44 /**
45 * Create a launchable activity object for a given ResolveInfo and user.
Amith Yamasanie781c812014-05-28 15:28:18 -070046 *
Amith Yamasani4f582632014-02-19 14:31:52 -080047 * @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 */
Sunny Goyal45d3e972016-03-31 12:38:17 -070051 LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user) {
Amith Yamasani4f582632014-02-19 14:31:52 -080052 this(context);
Sunny Goyal45d3e972016-03-31 12:38:17 -070053 mActivityInfo = info;
54 mComponentName = new ComponentName(info.packageName, info.name);
Amith Yamasanie781c812014-05-28 15:28:18 -070055 mUser = user;
Amith Yamasani4f582632014-02-19 14:31:52 -080056 }
57
58 LauncherActivityInfo(Context context) {
59 mPm = context.getPackageManager();
Amith Yamasani4f582632014-02-19 14:31:52 -080060 }
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 /**
Amith Yamasanie781c812014-05-28 15:28:18 -070072 * 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)
Amith Yamasani4f582632014-02-19 14:31:52 -080079 *
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.
Amith Yamasanie781c812014-05-28 15:28:18 -070088 *
Amith Yamasani4f582632014-02-19 14:31:52 -080089 * @return The label for the activity.
90 */
91 public CharSequence getLabel() {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -070092 // TODO: Go through LauncherAppsService
Sunny Goyal45d3e972016-03-31 12:38:17 -070093 return mActivityInfo.loadLabel(mPm);
Amith Yamasani4f582632014-02-19 14:31:52 -080094 }
95
96 /**
97 * Returns the icon for this activity, without any badging for the profile.
Amith Yamasanie781c812014-05-28 15:28:18 -070098 * @param density The preferred density of the icon, zero for default density. Use
99 * density DPI values from {@link DisplayMetrics}.
Amith Yamasani4f582632014-02-19 14:31:52 -0800100 * @see #getBadgedIcon(int)
Amith Yamasanie781c812014-05-28 15:28:18 -0700101 * @see DisplayMetrics
Ricky Wai1281b182015-04-29 14:57:04 +0100102 * @return The drawable associated with the activity.
Amith Yamasani4f582632014-02-19 14:31:52 -0800103 */
104 public Drawable getIcon(int density) {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700105 // TODO: Go through LauncherAppsService
Sunny Goyal45d3e972016-03-31 12:38:17 -0700106 final int iconRes = mActivityInfo.getIconResource();
Sunny Goyalbc48d8c2016-03-30 15:55:42 -0700107 Drawable icon = null;
Ricky Wai1281b182015-04-29 14:57:04 +0100108 // Get the preferred density icon from the app's resources
109 if (density != 0 && iconRes != 0) {
110 try {
111 final Resources resources
112 = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
Sunny Goyalbc48d8c2016-03-30 15:55:42 -0700113 icon = resources.getDrawableForDensity(iconRes, density);
Ricky Wai1281b182015-04-29 14:57:04 +0100114 } catch (NameNotFoundException | Resources.NotFoundException exc) {
115 }
116 }
Sunny Goyalbc48d8c2016-03-30 15:55:42 -0700117 // Get the default density icon
118 if (icon == null) {
Sunny Goyal45d3e972016-03-31 12:38:17 -0700119 icon = mActivityInfo.loadIcon(mPm);
Sunny Goyalbc48d8c2016-03-30 15:55:42 -0700120 }
121 return icon;
Ricky Wai1281b182015-04-29 14:57:04 +0100122 }
123
124 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800125 * Returns the application flags from the ApplicationInfo of the activity.
Amith Yamasanie781c812014-05-28 15:28:18 -0700126 *
Amith Yamasani4f582632014-02-19 14:31:52 -0800127 * @return Application flags
Amith Yamasanie781c812014-05-28 15:28:18 -0700128 * @hide remove before shipping
Amith Yamasani4f582632014-02-19 14:31:52 -0800129 */
130 public int getApplicationFlags() {
131 return mActivityInfo.applicationInfo.flags;
132 }
133
134 /**
Amith Yamasanie781c812014-05-28 15:28:18 -0700135 * Returns the application info for the appliction this activity belongs to.
136 * @return
137 */
138 public ApplicationInfo getApplicationInfo() {
139 return mActivityInfo.applicationInfo;
140 }
141
142 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800143 * Returns the time at which the package was first installed.
Amith Yamasanie781c812014-05-28 15:28:18 -0700144 *
Amith Yamasani4f582632014-02-19 14:31:52 -0800145 * @return The time of installation of the package, in milliseconds.
146 */
147 public long getFirstInstallTime() {
Sunny Goyal0736e202015-11-24 10:42:11 -0800148 try {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700149 // TODO: Go through LauncherAppsService
Sunny Goyal0736e202015-11-24 10:42:11 -0800150 return mPm.getPackageInfo(mActivityInfo.packageName,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700151 PackageManager.MATCH_UNINSTALLED_PACKAGES).firstInstallTime;
Sunny Goyal0736e202015-11-24 10:42:11 -0800152 } catch (NameNotFoundException nnfe) {
153 // Sorry, can't find package
154 return 0;
155 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800156 }
157
158 /**
Kenny Guy86a64302014-04-25 20:48:32 +0100159 * Returns the name for the acitivty from android:name in the manifest.
160 * @return the name from android:name for the acitivity.
161 */
162 public String getName() {
163 return mActivityInfo.name;
164 }
165
166 /**
Amith Yamasani4f582632014-02-19 14:31:52 -0800167 * Returns the activity icon with badging appropriate for the profile.
Amith Yamasanie781c812014-05-28 15:28:18 -0700168 * @param density Optional density for the icon, or 0 to use the default density. Use
169 * {@link DisplayMetrics} for DPI values.
170 * @see DisplayMetrics
Amith Yamasani4f582632014-02-19 14:31:52 -0800171 * @return A badged icon for the activity.
172 */
173 public Drawable getBadgedIcon(int density) {
Sunny Goyalbc48d8c2016-03-30 15:55:42 -0700174 Drawable originalIcon = getIcon(density);
Amith Yamasani30acde72014-04-25 15:56:26 -0700175
Makoto Onuki8f2a4782017-06-23 13:10:41 -0700176 return mPm.getUserBadgedIcon(originalIcon, mUser);
Amith Yamasani4f582632014-02-19 14:31:52 -0800177 }
178}