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 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.ContentValues; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.graphics.Bitmap; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Represents a launchable icon on the workspaces and in folders. |
| 29 | */ |
| 30 | class 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 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 49 | * Indicates whether we're using the default fallback icon instead of something from the |
| 50 | * app. |
| 51 | */ |
| 52 | boolean usingFallbackIcon; |
| 53 | |
| 54 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 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 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 65 | ShortcutInfo() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 66 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; |
| 67 | } |
| 68 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 69 | public ShortcutInfo(ShortcutInfo info) { |
| 70 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 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. */ |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 83 | public ShortcutInfo(ApplicationInfo info) { |
| 84 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 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) { |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 96 | updateIcon(iconCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 97 | } |
| 98 | return mIcon; |
| 99 | } |
| 100 | |
Winson Chung | be36516 | 2012-05-07 10:34:12 -0700 | [diff] [blame] | 101 | /** Returns the package name that the shortcut's intent will resolve to, or an empty string if |
| 102 | * none exists. */ |
| 103 | String getPackageName() { |
| 104 | return super.getPackageName(intent); |
| 105 | } |
| 106 | |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 107 | public void updateIcon(IconCache iconCache) { |
| 108 | mIcon = iconCache.getIcon(intent); |
| 109 | usingFallbackIcon = iconCache.isDefaultIcon(mIcon); |
| 110 | } |
| 111 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Creates the application intent based on a component name and various launch flags. |
| 114 | * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. |
| 115 | * |
| 116 | * @param className the class name of the component representing the intent |
| 117 | * @param launchFlags the launch flags |
| 118 | */ |
| 119 | final void setActivity(ComponentName className, int launchFlags) { |
| 120 | intent = new Intent(Intent.ACTION_MAIN); |
| 121 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 122 | intent.setComponent(className); |
| 123 | intent.setFlags(launchFlags); |
| 124 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | void onAddToDatabase(ContentValues values) { |
| 129 | super.onAddToDatabase(values); |
| 130 | |
| 131 | String titleStr = title != null ? title.toString() : null; |
| 132 | values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); |
| 133 | |
| 134 | String uri = intent != null ? intent.toUri(0) : null; |
| 135 | values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); |
| 136 | |
| 137 | if (customIcon) { |
| 138 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 139 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 140 | writeBitmap(values, mIcon); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 141 | } else { |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 142 | if (!usingFallbackIcon) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 143 | writeBitmap(values, mIcon); |
| 144 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 145 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 146 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); |
| 147 | if (iconResource != null) { |
| 148 | values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, |
| 149 | iconResource.packageName); |
| 150 | values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, |
| 151 | iconResource.resourceName); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String toString() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 158 | return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id |
| 159 | + " type=" + this.itemType + " container=" + this.container + " screen=" + screen |
| 160 | + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY |
| 161 | + " isGesture=" + isGesture + " dropPos=" + dropPos + ")"; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 164 | public static void dumpShortcutInfoList(String tag, String label, |
| 165 | ArrayList<ShortcutInfo> list) { |
| 166 | Log.d(tag, label + " size=" + list.size()); |
| 167 | for (ShortcutInfo info: list) { |
| 168 | Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon |
| 169 | + " customIcon=" + info.customIcon); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |