LauncherActivityInfo.java revision e781c81d3394642583d555e7a5d6f6f8f63bc538
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
17package android.content.pm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.Bitmap.Config;
27import android.graphics.drawable.BitmapDrawable;
28import android.graphics.drawable.Drawable;
29import android.os.Bundle;
30import android.os.RemoteException;
31import android.os.UserHandle;
32import android.os.UserManager;
33import android.util.DisplayMetrics;
34import android.util.Log;
35
36/**
37 * A representation of an activity that can belong to this user or a managed
38 * profile associated with this user. It can be used to query the label, icon
39 * and badged icon for the activity.
40 */
41public class LauncherActivityInfo {
42    private static final boolean DEBUG = false;
43    private static final String TAG = "LauncherActivityInfo";
44
45    private final PackageManager mPm;
46    private final UserManager mUm;
47
48    private ActivityInfo mActivityInfo;
49    private ComponentName mComponentName;
50    private UserHandle mUser;
51    private long mFirstInstallTime;
52
53    /**
54     * Create a launchable activity object for a given ResolveInfo and user.
55     *
56     * @param context The context for fetching resources.
57     * @param info ResolveInfo from which to create the LauncherActivityInfo.
58     * @param user The UserHandle of the profile to which this activity belongs.
59     */
60    LauncherActivityInfo(Context context, ResolveInfo info, UserHandle user,
61            long firstInstallTime) {
62        this(context);
63        mActivityInfo = info.activityInfo;
64        mComponentName = LauncherApps.getComponentName(info);
65        mUser = user;
66        mFirstInstallTime = firstInstallTime;
67    }
68
69    LauncherActivityInfo(Context context) {
70        mPm = context.getPackageManager();
71        mUm = UserManager.get(context);
72    }
73
74    /**
75     * Returns the component name of this activity.
76     *
77     * @return ComponentName of the activity
78     */
79    public ComponentName getComponentName() {
80        return mComponentName;
81    }
82
83    /**
84     * Returns the user handle of the user profile that this activity belongs to. In order to
85     * persist the identity of the profile, do not store the UserHandle. Instead retrieve its
86     * serial number from UserManager. You can convert the serial number back to a UserHandle
87     * for later use.
88     *
89     * @see UserManager#getSerialNumberForUser(UserHandle)
90     * @see UserManager#getUserForSerialNumber(long)
91     *
92     * @return The UserHandle of the profile.
93     */
94    public UserHandle getUser() {
95        return mUser;
96    }
97
98    /**
99     * Retrieves the label for the activity.
100     *
101     * @return The label for the activity.
102     */
103    public CharSequence getLabel() {
104        return mActivityInfo.loadLabel(mPm);
105    }
106
107    /**
108     * Returns the icon for this activity, without any badging for the profile.
109     * @param density The preferred density of the icon, zero for default density. Use
110     * density DPI values from {@link DisplayMetrics}.
111     * @see #getBadgedIcon(int)
112     * @see DisplayMetrics
113     * @return The drawable associated with the activity
114     */
115    public Drawable getIcon(int density) {
116        // TODO: Use density
117        return mActivityInfo.loadIcon(mPm);
118    }
119
120    /**
121     * Returns the application flags from the ApplicationInfo of the activity.
122     *
123     * @return Application flags
124     * @hide remove before shipping
125     */
126    public int getApplicationFlags() {
127        return mActivityInfo.applicationInfo.flags;
128    }
129
130    /**
131     * Returns the application info for the appliction this activity belongs to.
132     * @return
133     */
134    public ApplicationInfo getApplicationInfo() {
135        return mActivityInfo.applicationInfo;
136    }
137
138    /**
139     * Returns the time at which the package was first installed.
140     *
141     * @return The time of installation of the package, in milliseconds.
142     */
143    public long getFirstInstallTime() {
144        return mFirstInstallTime;
145    }
146
147    /**
148     * Returns the name for the acitivty from  android:name in the manifest.
149     * @return the name from android:name for the acitivity.
150     */
151    public String getName() {
152        return mActivityInfo.name;
153    }
154
155    /**
156     * Returns the activity icon with badging appropriate for the profile.
157     * @param density Optional density for the icon, or 0 to use the default density. Use
158     * {@link DisplayMetrics} for DPI values.
159     * @see DisplayMetrics
160     * @return A badged icon for the activity.
161     */
162    public Drawable getBadgedIcon(int density) {
163        int iconRes = mActivityInfo.getIconResource();
164        Resources resources = null;
165        Drawable originalIcon = null;
166        try {
167            resources = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
168            try {
169                if (density != 0) {
170                    originalIcon = resources.getDrawableForDensity(iconRes, density);
171                }
172            } catch (Resources.NotFoundException e) {
173            }
174        } catch (NameNotFoundException nnfe) {
175        }
176
177        if (originalIcon == null) {
178            originalIcon = mActivityInfo.loadIcon(mPm);
179        }
180
181        if (originalIcon instanceof BitmapDrawable) {
182            return mUm.getBadgedDrawableForUser(
183                    originalIcon, mUser);
184        } else {
185            Log.e(TAG, "Unable to create badged icon for " + mActivityInfo);
186        }
187        return originalIcon;
188    }
189}
190