ApplicationInfo.java revision b60fd0eafbb4e6e61b429c403f024dc903bc483a
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(String whereCreated) {
64        super(whereCreated);
65        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
66    }
67
68    /**
69     * Must not hold the Context.
70     */
71    public ApplicationInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
72            HashMap<Object, CharSequence> labelCache, String whereCreated) {
73        super(whereCreated);
74        final String packageName = info.activityInfo.applicationInfo.packageName;
75
76        this.componentName = new ComponentName(packageName, info.activityInfo.name);
77        this.container = ItemInfo.NO_ID;
78        this.setActivity(componentName,
79                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
80
81        try {
82            int appFlags = pm.getApplicationInfo(packageName, 0).flags;
83            if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
84                flags |= DOWNLOADED_FLAG;
85
86                if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
87                    flags |= UPDATED_SYSTEM_APP_FLAG;
88                }
89            }
90            firstInstallTime = pm.getPackageInfo(packageName, 0).firstInstallTime;
91        } catch (NameNotFoundException e) {
92            Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
93        }
94
95        iconCache.getTitleAndIcon(this, info, labelCache);
96    }
97
98    public ApplicationInfo(ApplicationInfo info, String whereCreated) {
99        super(info, whereCreated);
100        componentName = info.componentName;
101        title = info.title.toString();
102        intent = new Intent(info.intent);
103        flags = info.flags;
104        firstInstallTime = info.firstInstallTime;
105    }
106
107    /**
108     * Creates the application intent based on a component name and various launch flags.
109     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
110     *
111     * @param className the class name of the component representing the intent
112     * @param launchFlags the launch flags
113     */
114    final void setActivity(ComponentName className, int launchFlags) {
115        intent = new Intent(Intent.ACTION_MAIN);
116        intent.addCategory(Intent.CATEGORY_LAUNCHER);
117        intent.setComponent(className);
118        intent.setFlags(launchFlags);
119        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
120    }
121
122    @Override
123    public String toString() {
124        return "ApplicationInfo(title=" + title.toString() + ")";
125    }
126
127    public static void dumpApplicationInfoList(String tag, String label,
128            ArrayList<ApplicationInfo> list) {
129        Log.d(tag, label + " size=" + list.size());
130        for (ApplicationInfo info: list) {
131            Log.d(tag, "   title=\"" + info.title + "\" iconBitmap="
132                    + info.iconBitmap + " firstInstallTime="
133                    + info.firstInstallTime);
134        }
135    }
136
137    public ShortcutInfo makeShortcut() {
138        return new ShortcutInfo(this, "18");
139    }
140}
141