ShortcutInfo.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 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 application name.
34     */
35    CharSequence title;
36
37    /**
38     * The intent used to start the application.
39     */
40    Intent intent;
41
42    /**
43     * Indicates whether the icon comes from an application's resource (if false)
44     * or from a custom Bitmap (if true.)
45     */
46    boolean customIcon;
47
48    /**
49     * Indicates whether we're using the default fallback icon instead of something from the
50     * app.
51     */
52    boolean usingFallbackIcon;
53
54    /**
55     * If isShortcut=true and customIcon=false, this contains a reference to the
56     * shortcut icon as an application's resource.
57     */
58    Intent.ShortcutIconResource iconResource;
59
60    /**
61     * The application icon.
62     */
63    private Bitmap mIcon;
64
65    ShortcutInfo() {
66        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
67    }
68
69    public ShortcutInfo(ShortcutInfo info) {
70        super(info);
71        title = info.title.toString();
72        intent = new Intent(info.intent);
73        if (info.iconResource != null) {
74            iconResource = new Intent.ShortcutIconResource();
75            iconResource.packageName = info.iconResource.packageName;
76            iconResource.resourceName = info.iconResource.resourceName;
77        }
78        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
79        customIcon = info.customIcon;
80    }
81
82    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
83    public ShortcutInfo(ApplicationInfo info) {
84        super(info);
85        title = info.title.toString();
86        intent = new Intent(info.intent);
87        customIcon = false;
88    }
89
90    public void setIcon(Bitmap b) {
91        mIcon = b;
92    }
93
94    public Bitmap getIcon(IconCache iconCache) {
95        if (mIcon == null) {
96            updateIcon(iconCache);
97        }
98        return mIcon;
99    }
100
101    /** Returns the package name that the shortcut's intent will resolve to, or an empty string if
102     *  none exists. */
103    String getPackageName() {
104        return super.getPackageName(intent);
105    }
106
107    public void updateIcon(IconCache iconCache) {
108        mIcon = iconCache.getIcon(intent);
109        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
110    }
111
112    /**
113     * Creates the application intent based on a component name and various launch flags.
114     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
115     *
116     * @param className the class name of the component representing the intent
117     * @param launchFlags the launch flags
118     */
119    final void setActivity(ComponentName className, int launchFlags) {
120        intent = new Intent(Intent.ACTION_MAIN);
121        intent.addCategory(Intent.CATEGORY_LAUNCHER);
122        intent.setComponent(className);
123        intent.setFlags(launchFlags);
124        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
125    }
126
127    @Override
128    void onAddToDatabase(ContentValues values) {
129        super.onAddToDatabase(values);
130
131        String titleStr = title != null ? title.toString() : null;
132        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
133
134        String uri = intent != null ? intent.toUri(0) : null;
135        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
136
137        if (customIcon) {
138            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
139                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
140            writeBitmap(values, mIcon);
141        } else {
142            if (!usingFallbackIcon) {
143                writeBitmap(values, mIcon);
144            }
145            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
146                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
147            if (iconResource != null) {
148                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
149                        iconResource.packageName);
150                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
151                        iconResource.resourceName);
152            }
153        }
154    }
155
156    @Override
157    public String toString() {
158        return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
159                + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
160                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
161                + " isGesture=" + isGesture + " dropPos=" + dropPos + ")";
162    }
163
164    public static void dumpShortcutInfoList(String tag, String label,
165            ArrayList<ShortcutInfo> list) {
166        Log.d(tag, label + " size=" + list.size());
167        for (ShortcutInfo info: list) {
168            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
169                    + " customIcon=" + info.customIcon);
170        }
171    }
172}
173
174