ShortcutInfo.java revision 487f7dd3059621527eb439d7d51d34e00293f9b1
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 java.util.ArrayList;
20
21import android.content.ComponentName;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.graphics.Bitmap;
25import android.util.Log;
26
27/**
28 * Represents a launchable icon on the workspaces and in folders.
29 */
30class ShortcutInfo extends ItemInfo {
31
32    /**
33     * The intent used to start the application.
34     */
35    Intent intent;
36
37    /**
38     * Indicates whether the icon comes from an application's resource (if false)
39     * or from a custom Bitmap (if true.)
40     */
41    boolean customIcon;
42
43    /**
44     * Indicates whether we're using the default fallback icon instead of something from the
45     * app.
46     */
47    boolean usingFallbackIcon;
48
49    /**
50     * If isShortcut=true and customIcon=false, this contains a reference to the
51     * shortcut icon as an application's resource.
52     */
53    Intent.ShortcutIconResource iconResource;
54
55    /**
56     * The application icon.
57     */
58    private Bitmap mIcon;
59
60    ShortcutInfo() {
61        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
62    }
63
64    public ShortcutInfo(ShortcutInfo info) {
65        super(info);
66        title = info.title.toString();
67        intent = new Intent(info.intent);
68        if (info.iconResource != null) {
69            iconResource = new Intent.ShortcutIconResource();
70            iconResource.packageName = info.iconResource.packageName;
71            iconResource.resourceName = info.iconResource.resourceName;
72        }
73        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
74        customIcon = info.customIcon;
75    }
76
77    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
78    public ShortcutInfo(ApplicationInfo info) {
79        super(info);
80        title = info.title.toString();
81        intent = new Intent(info.intent);
82        customIcon = false;
83    }
84
85    public void setIcon(Bitmap b) {
86        mIcon = b;
87    }
88
89    public Bitmap getIcon(IconCache iconCache) {
90        if (mIcon == null) {
91            updateIcon(iconCache);
92        }
93        return mIcon;
94    }
95
96    /** Returns the package name that the shortcut's intent will resolve to, or an empty string if
97     *  none exists. */
98    String getPackageName() {
99        return super.getPackageName(intent);
100    }
101
102    public void updateIcon(IconCache iconCache) {
103        mIcon = iconCache.getIcon(intent);
104        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
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    void onAddToDatabase(ContentValues values) {
124        super.onAddToDatabase(values);
125
126        String titleStr = title != null ? title.toString() : null;
127        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
128
129        String uri = intent != null ? intent.toUri(0) : null;
130        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
131
132        if (customIcon) {
133            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
134                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
135            writeBitmap(values, mIcon);
136        } else {
137            if (!usingFallbackIcon) {
138                writeBitmap(values, mIcon);
139            }
140            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
141                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
142            if (iconResource != null) {
143                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
144                        iconResource.packageName);
145                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
146                        iconResource.resourceName);
147            }
148        }
149    }
150
151    @Override
152    public String toString() {
153        return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
154                + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
155                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
156                + " dropPos=" + dropPos + ")";
157    }
158
159    public static void dumpShortcutInfoList(String tag, String label,
160            ArrayList<ShortcutInfo> list) {
161        Log.d(tag, label + " size=" + list.size());
162        for (ShortcutInfo info: list) {
163            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
164                    + " customIcon=" + info.customIcon);
165        }
166    }
167}
168
169