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.launcher3;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.graphics.Bitmap;
23import android.util.Log;
24
25import com.android.launcher3.compat.LauncherActivityInfoCompat;
26import com.android.launcher3.compat.UserHandleCompat;
27import com.android.launcher3.compat.UserManagerCompat;
28import com.android.launcher3.util.ComponentKey;
29
30import java.util.ArrayList;
31import java.util.Arrays;
32
33/**
34 * Represents an app in AllAppsView.
35 */
36public class AppInfo extends ItemInfo {
37    private static final String TAG = "Launcher3.AppInfo";
38
39    /**
40     * The intent used to start the application.
41     */
42    public Intent intent;
43
44    /**
45     * A bitmap version of the application icon.
46     */
47    public Bitmap iconBitmap;
48
49    /**
50     * Indicates whether we're using a low res icon
51     */
52    boolean usingLowResIcon;
53
54    /**
55     * The time at which the app was first installed.
56     */
57    long firstInstallTime;
58
59    public ComponentName componentName;
60
61    static final int DOWNLOADED_FLAG = 1;
62    static final int UPDATED_SYSTEM_APP_FLAG = 2;
63
64    int flags = 0;
65
66    AppInfo() {
67        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
68    }
69
70    public Intent getIntent() {
71        return intent;
72    }
73
74    protected Intent getRestoredIntent() {
75        return null;
76    }
77
78    /**
79     * Must not hold the Context.
80     */
81    public AppInfo(Context context, LauncherActivityInfoCompat info, UserHandleCompat user,
82            IconCache iconCache) {
83        this.componentName = info.getComponentName();
84        this.container = ItemInfo.NO_ID;
85
86        flags = initFlags(info);
87        firstInstallTime = info.getFirstInstallTime();
88        iconCache.getTitleAndIcon(this, info, true /* useLowResIcon */);
89        intent = makeLaunchIntent(context, info, user);
90        this.user = user;
91    }
92
93    public static int initFlags(LauncherActivityInfoCompat info) {
94        int appFlags = info.getApplicationInfo().flags;
95        int flags = 0;
96        if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
97            flags |= DOWNLOADED_FLAG;
98
99            if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
100                flags |= UPDATED_SYSTEM_APP_FLAG;
101            }
102        }
103        return flags;
104    }
105
106    public AppInfo(AppInfo info) {
107        super(info);
108        componentName = info.componentName;
109        title = Utilities.trim(info.title);
110        intent = new Intent(info.intent);
111        flags = info.flags;
112        firstInstallTime = info.firstInstallTime;
113        iconBitmap = info.iconBitmap;
114    }
115
116    @Override
117    public String toString() {
118        return "ApplicationInfo(title=" + title + " id=" + this.id
119                + " type=" + this.itemType + " container=" + this.container
120                + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY
121                + " spanX=" + spanX + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos)
122                + " user=" + user + ")";
123    }
124
125    /**
126     * Helper method used for debugging.
127     */
128    public static void dumpApplicationInfoList(String tag, String label, ArrayList<AppInfo> list) {
129        Log.d(tag, label + " size=" + list.size());
130        for (AppInfo info: list) {
131            Log.d(tag, "   title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
132                    + " firstInstallTime=" + info.firstInstallTime
133                    + " componentName=" + info.componentName.getPackageName());
134        }
135    }
136
137    public ShortcutInfo makeShortcut() {
138        return new ShortcutInfo(this);
139    }
140
141    public ComponentKey toComponentKey() {
142        return new ComponentKey(componentName, user);
143    }
144
145    public static Intent makeLaunchIntent(Context context, LauncherActivityInfoCompat info,
146            UserHandleCompat user) {
147        long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
148        return new Intent(Intent.ACTION_MAIN)
149            .addCategory(Intent.CATEGORY_LAUNCHER)
150            .setComponent(info.getComponentName())
151            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
152            .putExtra(EXTRA_PROFILE, serialNumber);
153    }
154}
155