ApplicationInfo.java revision be365165ed00205265c1876c4829fa9ac630da2a
1/*
2 * Copyright (C) 2008 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 com.android.launcher2;
18
19import android.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.content.pm.ResolveInfo;
24import android.graphics.Bitmap;
25import android.util.Log;
26
27import java.util.ArrayList;
28import java.util.HashMap;
29
30/**
31 * Represents an app in AllAppsView.
32 */
33class ApplicationInfo extends ItemInfo {
34    private static final String TAG = "Launcher2.ApplicationInfo";
35
36    /**
37     * The application name.
38     */
39    CharSequence title;
40
41    /**
42     * The intent used to start the application.
43     */
44    Intent intent;
45
46    /**
47     * A bitmap version of the application icon.
48     */
49    Bitmap iconBitmap;
50
51    /**
52     * The time at which the app was first installed.
53     */
54    long firstInstallTime;
55
56    ComponentName componentName;
57
58    static final int DOWNLOADED_FLAG = 1;
59    static final int UPDATED_SYSTEM_APP_FLAG = 2;
60
61    int flags = 0;
62
63    ApplicationInfo() {
64        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
65    }
66
67    /**
68     * Must not hold the Context.
69     */
70    public ApplicationInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
71            HashMap<Object, CharSequence> labelCache) {
72        final String packageName = info.activityInfo.applicationInfo.packageName;
73
74        this.componentName = new ComponentName(packageName, info.activityInfo.name);
75        this.container = ItemInfo.NO_ID;
76        this.setActivity(componentName,
77                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
78
79        try {
80            int appFlags = pm.getApplicationInfo(packageName, 0).flags;
81            if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
82                flags |= DOWNLOADED_FLAG;
83
84                if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
85                    flags |= UPDATED_SYSTEM_APP_FLAG;
86                }
87            }
88            firstInstallTime = pm.getPackageInfo(packageName, 0).firstInstallTime;
89        } catch (NameNotFoundException e) {
90            Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
91        }
92
93        iconCache.getTitleAndIcon(this, info, labelCache);
94    }
95
96    public ApplicationInfo(ApplicationInfo info) {
97        super(info);
98        componentName = info.componentName;
99        title = info.title.toString();
100        intent = new Intent(info.intent);
101        flags = info.flags;
102        firstInstallTime = info.firstInstallTime;
103    }
104
105    /** Returns the package name that the shortcut's intent will resolve to, or an empty string if
106     *  none exists. */
107    String getPackageName() {
108        return super.getPackageName(intent);
109    }
110
111    /**
112     * Creates the application intent based on a component name and various launch flags.
113     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
114     *
115     * @param className the class name of the component representing the intent
116     * @param launchFlags the launch flags
117     */
118    final void setActivity(ComponentName className, int launchFlags) {
119        intent = new Intent(Intent.ACTION_MAIN);
120        intent.addCategory(Intent.CATEGORY_LAUNCHER);
121        intent.setComponent(className);
122        intent.setFlags(launchFlags);
123        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
124    }
125
126    @Override
127    public String toString() {
128        return "ApplicationInfo(title=" + title.toString() + ")";
129    }
130
131    public static void dumpApplicationInfoList(String tag, String label,
132            ArrayList<ApplicationInfo> list) {
133        Log.d(tag, label + " size=" + list.size());
134        for (ApplicationInfo info: list) {
135            Log.d(tag, "   title=\"" + info.title + "\" iconBitmap="
136                    + info.iconBitmap + " firstInstallTime="
137                    + info.firstInstallTime);
138        }
139    }
140
141    public ShortcutInfo makeShortcut() {
142        return new ShortcutInfo(this);
143    }
144}
145