ShortcutInfo.java revision 1e2f465f46ded990ea30516cdb7f0fcf3280411f
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
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ComponentName;
200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ContentValues;
211e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport android.content.Context;
220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent;
231e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport android.content.pm.PackageManager;
241e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport android.content.pm.PackageManager.NameNotFoundException;
251e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport android.content.pm.PackageInfo;
260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.graphics.Bitmap;
270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.util.Log;
280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
291e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport java.util.ArrayList;
301e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/**
320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Represents a launchable icon on the workspaces and in folders.
330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoclass ShortcutInfo extends ItemInfo {
350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The intent used to start the application.
380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent intent;
400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Indicates whether the icon comes from an application's resource (if false)
430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * or from a custom Bitmap (if true.)
440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    boolean customIcon;
460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
4856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Indicates whether we're using the default fallback icon instead of something from the
4956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * app.
5056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
5156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    boolean usingFallbackIcon;
5256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
5356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * If isShortcut=true and customIcon=false, this contains a reference to the
550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * shortcut icon as an application's resource.
560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    Intent.ShortcutIconResource iconResource;
580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The application icon.
610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mIcon;
630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
641e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    long firstInstallTime;
651e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    int flags = 0;
661e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
67c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    ShortcutInfo() {
680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
711e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public ShortcutInfo(Context context, ShortcutInfo info) {
72c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (info.iconResource != null) {
760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource = new Intent.ShortcutIconResource();
770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.packageName = info.iconResource.packageName;
780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            iconResource.resourceName = info.iconResource.resourceName;
790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = info.customIcon;
821e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        initFlagsAndFirstInstallTime(getPackageInfo(context, intent.getComponent().getPackageName()));
830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
86c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    public ShortcutInfo(ApplicationInfo info) {
87c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        title = info.title.toString();
890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        customIcon = false;
911e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        flags = info.flags;
921e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        firstInstallTime = info.firstInstallTime;
931e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
941e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
951e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public static PackageInfo getPackageInfo(Context context, String packageName) {
961e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        PackageInfo pi = null;
971e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        try {
981e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            PackageManager pm = context.getPackageManager();
991e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            pi = pm.getPackageInfo(packageName, 0);
1001e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        } catch (NameNotFoundException e) {
1011e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            Log.d("ShortcutInfo", "PackageManager.getPackageInfo failed for " + packageName);
1021e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        }
1031e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        return pi;
1041e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
1051e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
1061e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    void initFlagsAndFirstInstallTime(PackageInfo pi) {
1071e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        flags = ApplicationInfo.initFlags(pi);
1081e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        firstInstallTime = ApplicationInfo.initFirstInstallTime(pi);
1090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public void setIcon(Bitmap b) {
1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = b;
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public Bitmap getIcon(IconCache iconCache) {
1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (mIcon == null) {
117e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka            updateIcon(iconCache);
1180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mIcon;
1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
122e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    public void updateIcon(IconCache iconCache) {
123e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka        mIcon = iconCache.getIcon(intent);
124e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka        usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
125e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka    }
126e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Creates the application intent based on a component name and various launch flags.
1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     *
1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param className the class name of the component representing the intent
1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * @param launchFlags the launch flags
1330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
1341e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    final void setActivity(Context context, ComponentName className, int launchFlags) {
1350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(Intent.ACTION_MAIN);
1360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.addCategory(Intent.CATEGORY_LAUNCHER);
1370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.setComponent(className);
1380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent.setFlags(launchFlags);
1390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
1401e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        initFlagsAndFirstInstallTime(getPackageInfo(context, intent.getComponent().getPackageName()));
1410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    void onAddToDatabase(ContentValues values) {
1450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        super.onAddToDatabase(values);
1460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String titleStr = title != null ? title.toString() : null;
1480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
1490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String uri = intent != null ? intent.toUri(0) : null;
1510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
1520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (customIcon) {
1540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
15656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            writeBitmap(values, mIcon);
1570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
158ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato            if (!usingFallbackIcon) {
15956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                writeBitmap(values, mIcon);
16056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
1610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
1630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (iconResource != null) {
1640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
1650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.packageName);
1660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
1670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        iconResource.resourceName);
1680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
1690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
1730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public String toString() {
174db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung        return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
175dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
176db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
177487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                + " dropPos=" + dropPos + ")";
1780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public static void dumpShortcutInfoList(String tag, String label,
1810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            ArrayList<ShortcutInfo> list) {
1820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Log.d(tag, label + " size=" + list.size());
1830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        for (ShortcutInfo info: list) {
1840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
1850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    + " customIcon=" + info.customIcon);
1860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
1870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato}
1890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
190