10589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/*
20589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Copyright (C) 2008 The Android Open Source Project
30589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
40589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
50589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * you may not use this file except in compliance with the License.
60589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * You may obtain a copy of the License at
70589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
80589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
90589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Unless required by applicable law or agreed to in writing, software
110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * See the License for the specific language governing permissions and
140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * limitations under the License.
150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratopackage com.android.launcher2;
180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
19aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.ArrayList;
20aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ComponentName;
220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ContentValues;
23374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasaniimport android.content.Context;
240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent;
250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.graphics.Bitmap;
260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.util.Log;
270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/**
290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Represents a launchable icon on the workspaces and in folders.
300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoclass ShortcutInfo extends ItemInfo {
320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The intent used to start the application.
350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent intent;
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Indicates whether the icon comes from an application's resource (if false)
400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * or from a custom Bitmap (if true.)
410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    boolean customIcon;
430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
4556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Indicates whether we're using the default fallback icon instead of something from the
4656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * app.
4756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
4856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    boolean usingFallbackIcon;
4956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
5056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * If isShortcut=true and customIcon=false, this contains a reference to the
520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * shortcut icon as an application's resource.
530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent.ShortcutIconResource iconResource;
550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The application icon.
580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mIcon;
600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
61c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    ShortcutInfo() {
620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
65c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    public ShortcutInfo(ShortcutInfo info) {
66c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (info.iconResource != null) {
700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource = new Intent.ShortcutIconResource();
710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.packageName = info.iconResource.packageName;
720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.resourceName = info.iconResource.resourceName;
730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = info.customIcon;
760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
79c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    public ShortcutInfo(ApplicationInfo info) {
80c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = false;
840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public void setIcon(Bitmap b) {
870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = b;
880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public Bitmap getIcon(IconCache iconCache) {
910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (mIcon == null) {
92e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka            updateIcon(iconCache);
930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mIcon;
950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
97e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    public void updateIcon(IconCache iconCache) {
98374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        mIcon = iconCache.getIcon(intent, user);
99e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
100e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    }
101e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka
1020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
1030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Creates the application intent based on a component name and various launch flags.
1040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
1050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     *
1060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param className the class name of the component representing the intent
1070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param launchFlags the launch flags
1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
109374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani    final void setActivity(Intent intent) {
110374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK |
111374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
112374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        this.intent = new Intent();
113374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        this.intent.setFlags(launchFlags);
114374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        this.intent.addCategory(Intent.CATEGORY_LAUNCHER);
115374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        this.intent.setComponent(intent.getComponent());
116374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        this.intent.putExtras(intent.getExtras());
1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
118374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        updateUser(this.intent);
1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
122374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani    void onAddToDatabase(Context context, ContentValues values) {
123374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        super.onAddToDatabase(context, values);
1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String titleStr = title != null ? title.toString() : null;
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String uri = intent != null ? intent.toUri(0) : null;
1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (customIcon) {
1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
13456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            writeBitmap(values, mIcon);
1350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
136ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato            if (!usingFallbackIcon) {
13756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                writeBitmap(values, mIcon);
13856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
1390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
1410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (iconResource != null) {
1420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
1430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.packageName);
1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
1450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.resourceName);
1460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
1470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
1510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public String toString() {
152db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung        return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
153db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
154db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
155487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                + " dropPos=" + dropPos + ")";
1560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public static void dumpShortcutInfoList(String tag, String label,
1590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            ArrayList<ShortcutInfo> list) {
1600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Log.d(tag, label + " size=" + list.size());
1610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        for (ShortcutInfo info: list) {
1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
1630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    + " customIcon=" + info.customIcon);
1640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato}
1670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
168