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;
230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent;
240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.graphics.Bitmap;
250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.util.Log;
260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/**
280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Represents a launchable icon on the workspaces and in folders.
290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoclass ShortcutInfo extends ItemInfo {
310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The intent used to start the application.
340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent intent;
360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Indicates whether the icon comes from an application's resource (if false)
390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * or from a custom Bitmap (if true.)
400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    boolean customIcon;
420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
4456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Indicates whether we're using the default fallback icon instead of something from the
4556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * app.
4656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
4756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    boolean usingFallbackIcon;
4856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * If isShortcut=true and customIcon=false, this contains a reference to the
510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * shortcut icon as an application's resource.
520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent.ShortcutIconResource iconResource;
540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The application icon.
570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mIcon;
590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
60c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    ShortcutInfo() {
610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
64c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    public ShortcutInfo(ShortcutInfo info) {
65c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (info.iconResource != null) {
690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource = new Intent.ShortcutIconResource();
700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.packageName = info.iconResource.packageName;
710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.resourceName = info.iconResource.resourceName;
720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = info.customIcon;
750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
78c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    public ShortcutInfo(ApplicationInfo info) {
79c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = false;
830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public void setIcon(Bitmap b) {
860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = b;
870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public Bitmap getIcon(IconCache iconCache) {
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (mIcon == null) {
91e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka            updateIcon(iconCache);
920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mIcon;
940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
96e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    public void updateIcon(IconCache iconCache) {
97e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka        mIcon = iconCache.getIcon(intent);
98e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
99e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    }
100e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka
1010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
1020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Creates the application intent based on a component name and various launch flags.
1030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
1040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     *
1050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param className the class name of the component representing the intent
1060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param launchFlags the launch flags
1070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    final void setActivity(ComponentName className, int launchFlags) {
1090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(Intent.ACTION_MAIN);
1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.addCategory(Intent.CATEGORY_LAUNCHER);
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.setComponent(className);
1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.setFlags(launchFlags);
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    void onAddToDatabase(ContentValues values) {
1180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        super.onAddToDatabase(values);
1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String titleStr = title != null ? title.toString() : null;
1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
1220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String uri = intent != null ? intent.toUri(0) : null;
1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
1250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (customIcon) {
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
12956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            writeBitmap(values, mIcon);
1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
131ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato            if (!usingFallbackIcon) {
13256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                writeBitmap(values, mIcon);
13356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
1340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
1360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (iconResource != null) {
1370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
1380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.packageName);
1390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.resourceName);
1410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
1420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
1460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public String toString() {
147db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung        return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
148db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
149db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
150487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                + " dropPos=" + dropPos + ")";
1510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public static void dumpShortcutInfoList(String tag, String label,
1540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            ArrayList<ShortcutInfo> list) {
1550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Log.d(tag, label + " size=" + list.size());
1560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        for (ShortcutInfo info: list) {
1570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
1580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    + " customIcon=" + info.customIcon);
1590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato}
1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
163