Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 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 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager.NameNotFoundException; |
| 23 | import android.content.res.Resources; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 27 | import android.util.DisplayMetrics; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 28 | |
| 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 | */ |
| 34 | public class LauncherActivityInfo { |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 35 | private static final String TAG = "LauncherActivityInfo"; |
| 36 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 37 | private final PackageManager mPm; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 38 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 39 | @UnsupportedAppUsage |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 40 | private ActivityInfo mActivityInfo; |
| 41 | private ComponentName mComponentName; |
| 42 | private UserHandle mUser; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Create a launchable activity object for a given ResolveInfo and user. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 46 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 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 | */ |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 51 | LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 52 | this(context); |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 53 | mActivityInfo = info; |
| 54 | mComponentName = new ComponentName(info.packageName, info.name); |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 55 | mUser = user; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | LauncherActivityInfo(Context context) { |
| 59 | mPm = context.getPackageManager(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 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 | /** |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 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) |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 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. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 88 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 89 | * @return The label for the activity. |
| 90 | */ |
| 91 | public CharSequence getLabel() { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 92 | // TODO: Go through LauncherAppsService |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 93 | return mActivityInfo.loadLabel(mPm); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the icon for this activity, without any badging for the profile. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 98 | * @param density The preferred density of the icon, zero for default density. Use |
| 99 | * density DPI values from {@link DisplayMetrics}. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 100 | * @see #getBadgedIcon(int) |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 101 | * @see DisplayMetrics |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 102 | * @return The drawable associated with the activity. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 103 | */ |
| 104 | public Drawable getIcon(int density) { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 105 | // TODO: Go through LauncherAppsService |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 106 | final int iconRes = mActivityInfo.getIconResource(); |
Sunny Goyal | bc48d8c | 2016-03-30 15:55:42 -0700 | [diff] [blame] | 107 | Drawable icon = null; |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 108 | // 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 Goyal | bc48d8c | 2016-03-30 15:55:42 -0700 | [diff] [blame] | 113 | icon = resources.getDrawableForDensity(iconRes, density); |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 114 | } catch (NameNotFoundException | Resources.NotFoundException exc) { |
| 115 | } |
| 116 | } |
Sunny Goyal | bc48d8c | 2016-03-30 15:55:42 -0700 | [diff] [blame] | 117 | // Get the default density icon |
| 118 | if (icon == null) { |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 119 | icon = mActivityInfo.loadIcon(mPm); |
Sunny Goyal | bc48d8c | 2016-03-30 15:55:42 -0700 | [diff] [blame] | 120 | } |
| 121 | return icon; |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 125 | * Returns the application flags from the ApplicationInfo of the activity. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 126 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 127 | * @return Application flags |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 128 | * @hide remove before shipping |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 129 | */ |
| 130 | public int getApplicationFlags() { |
| 131 | return mActivityInfo.applicationInfo.flags; |
| 132 | } |
| 133 | |
| 134 | /** |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 135 | * 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 143 | * Returns the time at which the package was first installed. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 144 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 145 | * @return The time of installation of the package, in milliseconds. |
| 146 | */ |
| 147 | public long getFirstInstallTime() { |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 148 | try { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 149 | // TODO: Go through LauncherAppsService |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 150 | return mPm.getPackageInfo(mActivityInfo.packageName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 151 | PackageManager.MATCH_UNINSTALLED_PACKAGES).firstInstallTime; |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 152 | } catch (NameNotFoundException nnfe) { |
| 153 | // Sorry, can't find package |
| 154 | return 0; |
| 155 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Kenny Guy | 86a6430 | 2014-04-25 20:48:32 +0100 | [diff] [blame] | 159 | * 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 167 | * Returns the activity icon with badging appropriate for the profile. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 168 | * @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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 171 | * @return A badged icon for the activity. |
| 172 | */ |
| 173 | public Drawable getBadgedIcon(int density) { |
Sunny Goyal | bc48d8c | 2016-03-30 15:55:42 -0700 | [diff] [blame] | 174 | Drawable originalIcon = getIcon(density); |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 175 | |
Makoto Onuki | 8f2a478 | 2017-06-23 13:10:41 -0700 | [diff] [blame] | 176 | return mPm.getUserBadgedIcon(originalIcon, mUser); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 177 | } |
| 178 | } |