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.LauncherActivityInfo;
22import android.content.pm.LauncherApps;
23import android.content.pm.PackageManager.NameNotFoundException;
24import android.content.pm.ResolveInfo;
25import android.graphics.Bitmap;
26import android.os.UserHandle;
27import android.util.Log;
28
29import java.util.ArrayList;
30import java.util.HashMap;
31
32/**
33 * Represents an app in AllAppsView.
34 */
35class ApplicationInfo extends ItemInfo {
36    private static final String TAG = "Launcher2.ApplicationInfo";
37
38    /**
39     * The intent used to start the application.
40     */
41    Intent intent;
42
43    /**
44     * A bitmap version of the application icon.
45     */
46    Bitmap iconBitmap;
47
48    /**
49     * The time at which the app was first installed.
50     */
51    long firstInstallTime;
52
53    ComponentName componentName;
54
55    static final int DOWNLOADED_FLAG = 1;
56    static final int UPDATED_SYSTEM_APP_FLAG = 2;
57
58    int flags = 0;
59
60    ApplicationInfo() {
61        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
62    }
63
64    /**
65     * Must not hold the Context.
66     */
67    public ApplicationInfo(LauncherActivityInfo info, UserHandle user, IconCache iconCache,
68            HashMap<Object, CharSequence> labelCache) {
69
70        this.componentName = info.getComponentName();
71        this.container = ItemInfo.NO_ID;
72
73        int appFlags = info.getApplicationInfo().flags;
74        if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
75            flags |= DOWNLOADED_FLAG;
76        }
77        if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
78            flags |= UPDATED_SYSTEM_APP_FLAG;
79        }
80        firstInstallTime = info.getFirstInstallTime();
81        iconCache.getTitleAndIcon(this, info, labelCache);
82        intent = new Intent(Intent.ACTION_MAIN);
83        intent.addCategory(Intent.CATEGORY_LAUNCHER);
84        intent.setComponent(info.getComponentName());
85        intent.putExtra(EXTRA_PROFILE, user);
86        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
87        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
88        updateUser(intent);
89    }
90
91    public ApplicationInfo(ApplicationInfo info) {
92        super(info);
93        componentName = info.componentName;
94        title = info.title.toString();
95        intent = new Intent(info.intent);
96        flags = info.flags;
97        firstInstallTime = info.firstInstallTime;
98    }
99
100    @Override
101    public String toString() {
102        return "ApplicationInfo(title=" + title.toString() + " P=" + user + ")";
103    }
104
105    public static void dumpApplicationInfoList(String tag, String label,
106            ArrayList<ApplicationInfo> list) {
107        Log.d(tag, label + " size=" + list.size());
108        for (ApplicationInfo info: list) {
109            Log.d(tag, "   title=\"" + info.title + "\" iconBitmap="
110                    + info.iconBitmap + " firstInstallTime="
111                    + info.firstInstallTime);
112        }
113    }
114
115    public ShortcutInfo makeShortcut() {
116        return new ShortcutInfo(this);
117    }
118}
119