• 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.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 import android.content.res.Resources;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.DisplayMetrics;
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 {
36     private static final String TAG = "LauncherActivityInfo";
37 
38     private final PackageManager mPm;
39 
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         return mActivityInfo.loadLabel(mPm);
93     }
94 
95     /**
96      * Returns the icon for this activity, without any badging for the profile.
97      * @param density The preferred density of the icon, zero for default density. Use
98      * density DPI values from {@link DisplayMetrics}.
99      * @see #getBadgedIcon(int)
100      * @see DisplayMetrics
101      * @return The drawable associated with the activity.
102      */
getIcon(int density)103     public Drawable getIcon(int density) {
104         final int iconRes = mActivityInfo.getIconResource();
105         Drawable icon = null;
106         // Get the preferred density icon from the app's resources
107         if (density != 0 && iconRes != 0) {
108             try {
109                 final Resources resources
110                         = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
111                 icon = resources.getDrawableForDensity(iconRes, density);
112             } catch (NameNotFoundException | Resources.NotFoundException exc) {
113             }
114         }
115         // Get the default density icon
116         if (icon == null) {
117             icon = mActivityInfo.loadIcon(mPm);
118         }
119         return icon;
120     }
121 
122     /**
123      * Returns the application flags from the ApplicationInfo of the activity.
124      *
125      * @return Application flags
126      * @hide remove before shipping
127      */
getApplicationFlags()128     public int getApplicationFlags() {
129         return mActivityInfo.applicationInfo.flags;
130     }
131 
132     /**
133      * Returns the application info for the appliction this activity belongs to.
134      * @return
135      */
getApplicationInfo()136     public ApplicationInfo getApplicationInfo() {
137         return mActivityInfo.applicationInfo;
138     }
139 
140     /**
141      * Returns the time at which the package was first installed.
142      *
143      * @return The time of installation of the package, in milliseconds.
144      */
getFirstInstallTime()145     public long getFirstInstallTime() {
146         try {
147             return mPm.getPackageInfo(mActivityInfo.packageName,
148                     PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
149         } catch (NameNotFoundException nnfe) {
150             // Sorry, can't find package
151             return 0;
152         }
153     }
154 
155     /**
156      * Returns the name for the acitivty from  android:name in the manifest.
157      * @return the name from android:name for the acitivity.
158      */
getName()159     public String getName() {
160         return mActivityInfo.name;
161     }
162 
163     /**
164      * Returns the activity icon with badging appropriate for the profile.
165      * @param density Optional density for the icon, or 0 to use the default density. Use
166      * {@link DisplayMetrics} for DPI values.
167      * @see DisplayMetrics
168      * @return A badged icon for the activity.
169      */
getBadgedIcon(int density)170     public Drawable getBadgedIcon(int density) {
171         Drawable originalIcon = getIcon(density);
172 
173         if (originalIcon instanceof BitmapDrawable) {
174             return mPm.getUserBadgedIcon(originalIcon, mUser);
175         } else {
176             Log.e(TAG, "Unable to create badged icon for " + mActivityInfo);
177         }
178         return originalIcon;
179     }
180 }
181