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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager.NameNotFoundException; |
| 22 | import android.content.res.Resources; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.BitmapDrawable; |
| 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 | import 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 | */ |
| 35 | public class LauncherActivityInfo { |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 36 | private static final String TAG = "LauncherActivityInfo"; |
| 37 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 38 | private final PackageManager mPm; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 39 | |
| 40 | private ActivityInfo mActivityInfo; |
| 41 | private ComponentName mComponentName; |
Amith Yamasani | 7e19f50 | 2015-01-30 14:18:52 -0800 | [diff] [blame] | 42 | private ResolveInfo mResolveInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 43 | private UserHandle mUser; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Create a launchable activity object for a given ResolveInfo and user. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 47 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 48 | * @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 Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 52 | LauncherActivityInfo(Context context, ResolveInfo info, UserHandle user) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 53 | this(context); |
Amith Yamasani | 7e19f50 | 2015-01-30 14:18:52 -0800 | [diff] [blame] | 54 | mResolveInfo = info; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 55 | mActivityInfo = info.activityInfo; |
| 56 | mComponentName = LauncherApps.getComponentName(info); |
| 57 | mUser = user; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | LauncherActivityInfo(Context context) { |
| 61 | mPm = context.getPackageManager(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 62 | } |
| 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 Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 74 | * 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 81 | * |
| 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 Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 90 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 91 | * @return The label for the activity. |
| 92 | */ |
| 93 | public CharSequence getLabel() { |
Amith Yamasani | 7e19f50 | 2015-01-30 14:18:52 -0800 | [diff] [blame] | 94 | return mResolveInfo.loadLabel(mPm); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the icon for this activity, without any badging for the profile. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 99 | * @param density The preferred density of the icon, zero for default density. Use |
| 100 | * density DPI values from {@link DisplayMetrics}. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 101 | * @see #getBadgedIcon(int) |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 102 | * @see DisplayMetrics |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 103 | * @return The drawable associated with the activity. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 104 | */ |
| 105 | public Drawable getIcon(int density) { |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 106 | final int iconRes = mResolveInfo.getIconResource(); |
| 107 | Drawable icon = getDrawableForDensity(iconRes, density); |
Amith Yamasani | 7e19f50 | 2015-01-30 14:18:52 -0800 | [diff] [blame] | 108 | // Get the default density icon |
| 109 | if (icon == null) { |
| 110 | icon = mResolveInfo.loadIcon(mPm); |
| 111 | } |
| 112 | return icon; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 116 | * 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 Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 136 | * @param iconRes id of the drawable. |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 137 | * @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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 156 | * Returns the application flags from the ApplicationInfo of the activity. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 157 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 158 | * @return Application flags |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 159 | * @hide remove before shipping |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 160 | */ |
| 161 | public int getApplicationFlags() { |
| 162 | return mActivityInfo.applicationInfo.flags; |
| 163 | } |
| 164 | |
| 165 | /** |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 166 | * 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 174 | * Returns the time at which the package was first installed. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 175 | * |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 176 | * @return The time of installation of the package, in milliseconds. |
| 177 | */ |
| 178 | public long getFirstInstallTime() { |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 179 | 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Kenny Guy | 86a6430 | 2014-04-25 20:48:32 +0100 | [diff] [blame] | 189 | * 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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 197 | * Returns the activity icon with badging appropriate for the profile. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 198 | * @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 Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 201 | * @return A badged icon for the activity. |
| 202 | */ |
| 203 | public Drawable getBadgedIcon(int density) { |
Ricky Wai | 1281b18 | 2015-04-29 14:57:04 +0100 | [diff] [blame] | 204 | Drawable originalIcon = getOriginalIcon(density); |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 205 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 206 | if (originalIcon instanceof BitmapDrawable) { |
Svetoslav | c7d62f0 | 2014-09-04 15:39:54 -0700 | [diff] [blame] | 207 | return mPm.getUserBadgedIcon(originalIcon, mUser); |
Amith Yamasani | 30acde7 | 2014-04-25 15:56:26 -0700 | [diff] [blame] | 208 | } else { |
| 209 | Log.e(TAG, "Unable to create badged icon for " + mActivityInfo); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 210 | } |
| 211 | return originalIcon; |
| 212 | } |
| 213 | } |