Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame^] | 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.ContentValues; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | |
| 29 | /** |
| 30 | * Represents a launchable icon on the workspaces and in folders. |
| 31 | */ |
| 32 | class ShortcutInfo extends ItemInfo { |
| 33 | |
| 34 | /** |
| 35 | * The application name. |
| 36 | */ |
| 37 | CharSequence title; |
| 38 | |
| 39 | /** |
| 40 | * The intent used to start the application. |
| 41 | */ |
| 42 | Intent intent; |
| 43 | |
| 44 | /** |
| 45 | * Indicates whether the icon comes from an application's resource (if false) |
| 46 | * or from a custom Bitmap (if true.) |
| 47 | */ |
| 48 | boolean customIcon; |
| 49 | |
| 50 | /** |
| 51 | * If isShortcut=true and customIcon=false, this contains a reference to the |
| 52 | * shortcut icon as an application's resource. |
| 53 | */ |
| 54 | Intent.ShortcutIconResource iconResource; |
| 55 | |
| 56 | /** |
| 57 | * The application icon. |
| 58 | */ |
| 59 | private Bitmap mIcon; |
| 60 | |
| 61 | ShortcutInfo() { |
| 62 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; |
| 63 | } |
| 64 | |
| 65 | public ShortcutInfo(ShortcutInfo info) { |
| 66 | super(info); |
| 67 | title = info.title.toString(); |
| 68 | intent = new Intent(info.intent); |
| 69 | if (info.iconResource != null) { |
| 70 | iconResource = new Intent.ShortcutIconResource(); |
| 71 | iconResource.packageName = info.iconResource.packageName; |
| 72 | iconResource.resourceName = info.iconResource.resourceName; |
| 73 | } |
| 74 | mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all |
| 75 | customIcon = info.customIcon; |
| 76 | } |
| 77 | |
| 78 | /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */ |
| 79 | public ShortcutInfo(ApplicationInfo info) { |
| 80 | super(info); |
| 81 | title = info.title.toString(); |
| 82 | intent = new Intent(info.intent); |
| 83 | customIcon = false; |
| 84 | } |
| 85 | |
| 86 | public void setIcon(Bitmap b) { |
| 87 | mIcon = b; |
| 88 | } |
| 89 | |
| 90 | public Bitmap getIcon(IconCache iconCache) { |
| 91 | if (mIcon == null) { |
| 92 | mIcon = iconCache.getIcon(this.intent); |
| 93 | } |
| 94 | return mIcon; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Creates the application intent based on a component name and various launch flags. |
| 99 | * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. |
| 100 | * |
| 101 | * @param className the class name of the component representing the intent |
| 102 | * @param launchFlags the launch flags |
| 103 | */ |
| 104 | final void setActivity(ComponentName className, int launchFlags) { |
| 105 | intent = new Intent(Intent.ACTION_MAIN); |
| 106 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 107 | intent.setComponent(className); |
| 108 | intent.setFlags(launchFlags); |
| 109 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | void onAddToDatabase(ContentValues values) { |
| 114 | super.onAddToDatabase(values); |
| 115 | |
| 116 | String titleStr = title != null ? title.toString() : null; |
| 117 | values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); |
| 118 | |
| 119 | String uri = intent != null ? intent.toUri(0) : null; |
| 120 | values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); |
| 121 | |
| 122 | if (customIcon) { |
| 123 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 124 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); |
| 125 | Bitmap bitmap = this.mIcon; |
| 126 | writeBitmap(values, bitmap); |
| 127 | } else { |
| 128 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 129 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); |
| 130 | if (iconResource != null) { |
| 131 | values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, |
| 132 | iconResource.packageName); |
| 133 | values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, |
| 134 | iconResource.resourceName); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | return title.toString(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | void unbind() { |
| 146 | super.unbind(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | public static void dumpShortcutInfoList(String tag, String label, |
| 151 | ArrayList<ShortcutInfo> list) { |
| 152 | Log.d(tag, label + " size=" + list.size()); |
| 153 | for (ShortcutInfo info: list) { |
| 154 | Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon |
| 155 | + " customIcon=" + info.customIcon); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |