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