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.Drawable;
24import android.os.UserHandle;
25import android.os.UserManager;
26import android.util.DisplayMetrics;
27
28/**
29 * A representation of an activity that can belong to this user or a managed
30 * profile associated with this user. It can be used to query the label, icon
31 * and badged icon for the activity.
32 */
33public class LauncherActivityInfo {
34    private static final String TAG = "LauncherActivityInfo";
35
36    private final PackageManager mPm;
37
38    private ActivityInfo mActivityInfo;
39    private ComponentName mComponentName;
40    private UserHandle mUser;
41
42    /**
43     * Create a launchable activity object for a given ResolveInfo and user.
44     *
45     * @param context The context for fetching resources.
46     * @param info ResolveInfo from which to create the LauncherActivityInfo.
47     * @param user The UserHandle of the profile to which this activity belongs.
48     */
49    LauncherActivityInfo(Context context, ActivityInfo info, UserHandle user) {
50        this(context);
51        mActivityInfo = info;
52        mComponentName =  new ComponentName(info.packageName, info.name);
53        mUser = user;
54    }
55
56    LauncherActivityInfo(Context context) {
57        mPm = context.getPackageManager();
58    }
59
60    /**
61     * Returns the component name of this activity.
62     *
63     * @return ComponentName of the activity
64     */
65    public ComponentName getComponentName() {
66        return mComponentName;
67    }
68
69    /**
70     * Returns the user handle of the user profile that this activity belongs to. In order to
71     * persist the identity of the profile, do not store the UserHandle. Instead retrieve its
72     * serial number from UserManager. You can convert the serial number back to a UserHandle
73     * for later use.
74     *
75     * @see UserManager#getSerialNumberForUser(UserHandle)
76     * @see UserManager#getUserForSerialNumber(long)
77     *
78     * @return The UserHandle of the profile.
79     */
80    public UserHandle getUser() {
81        return mUser;
82    }
83
84    /**
85     * Retrieves the label for the activity.
86     *
87     * @return The label for the activity.
88     */
89    public CharSequence getLabel() {
90        // TODO: Go through LauncherAppsService
91        return mActivityInfo.loadLabel(mPm);
92    }
93
94    /**
95     * Returns the icon for this activity, without any badging for the profile.
96     * @param density The preferred density of the icon, zero for default density. Use
97     * density DPI values from {@link DisplayMetrics}.
98     * @see #getBadgedIcon(int)
99     * @see DisplayMetrics
100     * @return The drawable associated with the activity.
101     */
102    public Drawable getIcon(int density) {
103        // TODO: Go through LauncherAppsService
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     */
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     */
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     */
145    public long getFirstInstallTime() {
146        try {
147            // TODO: Go through LauncherAppsService
148            return mPm.getPackageInfo(mActivityInfo.packageName,
149                    PackageManager.MATCH_UNINSTALLED_PACKAGES).firstInstallTime;
150        } catch (NameNotFoundException nnfe) {
151            // Sorry, can't find package
152            return 0;
153        }
154    }
155
156    /**
157     * Returns the name for the acitivty from  android:name in the manifest.
158     * @return the name from android:name for the acitivity.
159     */
160    public String getName() {
161        return mActivityInfo.name;
162    }
163
164    /**
165     * Returns the activity icon with badging appropriate for the profile.
166     * @param density Optional density for the icon, or 0 to use the default density. Use
167     * {@link DisplayMetrics} for DPI values.
168     * @see DisplayMetrics
169     * @return A badged icon for the activity.
170     */
171    public Drawable getBadgedIcon(int density) {
172        Drawable originalIcon = getIcon(density);
173
174        return mPm.getUserBadgedIcon(originalIcon, mUser);
175    }
176}
177