LauncherActivityInfo.java revision 86a6430e526efc9656c539f9d7e5dea34219ef44
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.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Bitmap.Config;
25import android.graphics.drawable.BitmapDrawable;
26import android.graphics.drawable.Drawable;
27import android.os.Bundle;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.os.UserManager;
31import android.util.Log;
32
33/**
34 * A representation of an activity that can belong to this user or a managed
35 * profile associated with this user. It can be used to query the label, icon
36 * and badged icon for the activity.
37 */
38public class LauncherActivityInfo {
39    private static final boolean DEBUG = false;
40    private final PackageManager mPm;
41    private final UserManager mUm;
42
43    private ActivityInfo mActivityInfo;
44    private ComponentName mComponentName;
45    private UserHandle mUser;
46    // TODO: Fetch this value from PM
47    private long mFirstInstallTime;
48
49    /**
50     * Create a launchable activity object for a given ResolveInfo and user.
51     *
52     * @param context The context for fetching resources.
53     * @param info ResolveInfo from which to create the LauncherActivityInfo.
54     * @param user The UserHandle of the profile to which this activity belongs.
55     */
56    LauncherActivityInfo(Context context, ResolveInfo info, UserHandle user) {
57        this(context);
58        this.mActivityInfo = info.activityInfo;
59        this.mComponentName = LauncherApps.getComponentName(info);
60        this.mUser = user;
61    }
62
63    LauncherActivityInfo(Context context) {
64        mPm = context.getPackageManager();
65        mUm = UserManager.get(context);
66    }
67
68    /**
69     * Returns the component name of this activity.
70     *
71     * @return ComponentName of the activity
72     */
73    public ComponentName getComponentName() {
74        return mComponentName;
75    }
76
77    /**
78     * Returns the user handle of the user profile that this activity belongs to.
79     *
80     * @return The UserHandle of the profile.
81     */
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     */
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.
98     * @see #getBadgedIcon(int)
99     * @return The drawable associated with the activity
100     */
101    public Drawable getIcon(int density) {
102        // TODO: Use density
103        return mActivityInfo.loadIcon(mPm);
104    }
105
106    /**
107     * Returns the application flags from the ApplicationInfo of the activity.
108     *
109     * @return Application flags
110     */
111    public int getApplicationFlags() {
112        return mActivityInfo.applicationInfo.flags;
113    }
114
115    /**
116     * Returns the time at which the package was first installed.
117     * @return The time of installation of the package, in milliseconds.
118     */
119    public long getFirstInstallTime() {
120        return mFirstInstallTime;
121    }
122
123    /**
124     * Returns the name for the acitivty from  android:name in the manifest.
125     * @return the name from android:name for the acitivity.
126     */
127    public String getName() {
128        return mActivityInfo.name;
129    }
130
131    /**
132     * Returns the activity icon with badging appropriate for the profile.
133     * @param density Optional density for the icon, or 0 to use the default density.
134     * @return A badged icon for the activity.
135     */
136    public Drawable getBadgedIcon(int density) {
137        // TODO: Handle density
138        if (mUser.equals(android.os.Process.myUserHandle())) {
139            return mActivityInfo.loadIcon(mPm);
140        }
141        Drawable originalIcon = mActivityInfo.loadIcon(mPm);
142        if (originalIcon == null) {
143            if (DEBUG) {
144                Log.w("LauncherActivityInfo", "Couldn't find icon for activity");
145            }
146            originalIcon = mPm.getDefaultActivityIcon();
147        }
148        if (originalIcon instanceof BitmapDrawable) {
149            return mUm.getBadgedDrawableForUser(
150                    originalIcon, mUser);
151        }
152        return originalIcon;
153    }
154}
155