ShortcutInfo.java revision e384affda684a48c61d99ebfe8be40fb7d46d761
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    public void updateIcon(IconCache iconCache) {
102        mIcon = iconCache.getIcon(intent);
103        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
104    }
105
106    /**
107     * Creates the application intent based on a component name and various launch flags.
108     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
109     *
110     * @param className the class name of the component representing the intent
111     * @param launchFlags the launch flags
112     */
113    final void setActivity(ComponentName className, int launchFlags) {
114        intent = new Intent(Intent.ACTION_MAIN);
115        intent.addCategory(Intent.CATEGORY_LAUNCHER);
116        intent.setComponent(className);
117        intent.setFlags(launchFlags);
118        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
119    }
120
121    @Override
122    void onAddToDatabase(ContentValues values) {
123        super.onAddToDatabase(values);
124
125        String titleStr = title != null ? title.toString() : null;
126        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
127
128        String uri = intent != null ? intent.toUri(0) : null;
129        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
130
131        if (customIcon) {
132            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
133                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
134            writeBitmap(values, mIcon);
135        } else {
136            if (!usingFallbackIcon) {
137                writeBitmap(values, mIcon);
138            }
139            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
140                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
141            if (iconResource != null) {
142                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
143                        iconResource.packageName);
144                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
145                        iconResource.resourceName);
146            }
147        }
148    }
149
150    @Override
151    public String toString() {
152        return "ShortcutInfo(title=" + title.toString() + ")";
153    }
154
155    public static void dumpShortcutInfoList(String tag, String label,
156            ArrayList<ShortcutInfo> list) {
157        Log.d(tag, label + " size=" + list.size());
158        for (ShortcutInfo info: list) {
159            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
160                    + " customIcon=" + info.customIcon);
161        }
162    }
163}
164
165