• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.UnsupportedAppUsage;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.content.res.Resources;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.DisplayMetrics;
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 {
35     private static final String TAG = "LauncherActivityInfo";
36 
37     private final PackageManager mPm;
38 
39     @UnsupportedAppUsage
40     private ActivityInfo mActivityInfo;
41     private ComponentName mComponentName;
42     private UserHandle mUser;
43 
44     /**
45      * Create a launchable activity object for a given ResolveInfo and user.
46      *
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      */
LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user)51     LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user) {
52         this(context);
53         mActivityInfo = info;
54         mComponentName =  new ComponentName(info.packageName, info.name);
55         mUser = user;
56     }
57 
LauncherActivityInfo(Context context)58     LauncherActivityInfo(Context context) {
59         mPm = context.getPackageManager();
60     }
61 
62     /**
63      * Returns the component name of this activity.
64      *
65      * @return ComponentName of the activity
66      */
getComponentName()67     public ComponentName getComponentName() {
68         return mComponentName;
69     }
70 
71     /**
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)
79      *
80      * @return The UserHandle of the profile.
81      */
getUser()82     public UserHandle getUser() {
83         return mUser;
84     }
85 
86     /**
87      * Retrieves the label for the activity.
88      *
89      * @return The label for the activity.
90      */
getLabel()91     public CharSequence getLabel() {
92         // TODO: Go through LauncherAppsService
93         return mActivityInfo.loadLabel(mPm);
94     }
95 
96     /**
97      * Returns the icon for this activity, without any badging for the profile.
98      * @param density The preferred density of the icon, zero for default density. Use
99      * density DPI values from {@link DisplayMetrics}.
100      * @see #getBadgedIcon(int)
101      * @see DisplayMetrics
102      * @return The drawable associated with the activity.
103      */
getIcon(int density)104     public Drawable getIcon(int density) {
105         // TODO: Go through LauncherAppsService
106         final int iconRes = mActivityInfo.getIconResource();
107         Drawable icon = null;
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);
113                 icon = resources.getDrawableForDensity(iconRes, density);
114             } catch (NameNotFoundException | Resources.NotFoundException exc) {
115             }
116         }
117         // Get the default density icon
118         if (icon == null) {
119             icon = mActivityInfo.loadIcon(mPm);
120         }
121         return icon;
122     }
123 
124     /**
125      * Returns the application flags from the ApplicationInfo of the activity.
126      *
127      * @return Application flags
128      * @hide remove before shipping
129      */
getApplicationFlags()130     public int getApplicationFlags() {
131         return mActivityInfo.applicationInfo.flags;
132     }
133 
134     /**
135      * Returns the application info for the appliction this activity belongs to.
136      * @return
137      */
getApplicationInfo()138     public ApplicationInfo getApplicationInfo() {
139         return mActivityInfo.applicationInfo;
140     }
141 
142     /**
143      * Returns the time at which the package was first installed.
144      *
145      * @return The time of installation of the package, in milliseconds.
146      */
getFirstInstallTime()147     public long getFirstInstallTime() {
148         try {
149             // TODO: Go through LauncherAppsService
150             return mPm.getPackageInfo(mActivityInfo.packageName,
151                     PackageManager.MATCH_UNINSTALLED_PACKAGES).firstInstallTime;
152         } catch (NameNotFoundException nnfe) {
153             // Sorry, can't find package
154             return 0;
155         }
156     }
157 
158     /**
159      * Returns the name for the acitivty from  android:name in the manifest.
160      * @return the name from android:name for the acitivity.
161      */
getName()162     public String getName() {
163         return mActivityInfo.name;
164     }
165 
166     /**
167      * Returns the activity icon with badging appropriate for the profile.
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
171      * @return A badged icon for the activity.
172      */
getBadgedIcon(int density)173     public Drawable getBadgedIcon(int density) {
174         Drawable originalIcon = getIcon(density);
175 
176         return mPm.getUserBadgedIcon(originalIcon, mUser);
177     }
178 }
179