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