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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame^] | 17 | package com.android.launcher3; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 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 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 33 | * The intent used to start the application. |
| 34 | */ |
| 35 | Intent intent; |
| 36 | |
| 37 | /** |
| 38 | * Indicates whether the icon comes from an application's resource (if false) |
| 39 | * or from a custom Bitmap (if true.) |
| 40 | */ |
| 41 | boolean customIcon; |
| 42 | |
| 43 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 44 | * Indicates whether we're using the default fallback icon instead of something from the |
| 45 | * app. |
| 46 | */ |
| 47 | boolean usingFallbackIcon; |
| 48 | |
| 49 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 50 | * If isShortcut=true and customIcon=false, this contains a reference to the |
| 51 | * shortcut icon as an application's resource. |
| 52 | */ |
| 53 | Intent.ShortcutIconResource iconResource; |
| 54 | |
| 55 | /** |
| 56 | * The application icon. |
| 57 | */ |
| 58 | private Bitmap mIcon; |
| 59 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 60 | ShortcutInfo() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 61 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; |
| 62 | } |
| 63 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 64 | public ShortcutInfo(ShortcutInfo info) { |
| 65 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 66 | title = info.title.toString(); |
| 67 | intent = new Intent(info.intent); |
| 68 | if (info.iconResource != null) { |
| 69 | iconResource = new Intent.ShortcutIconResource(); |
| 70 | iconResource.packageName = info.iconResource.packageName; |
| 71 | iconResource.resourceName = info.iconResource.resourceName; |
| 72 | } |
| 73 | mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all |
| 74 | customIcon = info.customIcon; |
| 75 | } |
| 76 | |
| 77 | /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */ |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 78 | public ShortcutInfo(ApplicationInfo info) { |
| 79 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 80 | title = info.title.toString(); |
| 81 | intent = new Intent(info.intent); |
| 82 | customIcon = false; |
| 83 | } |
| 84 | |
| 85 | public void setIcon(Bitmap b) { |
| 86 | mIcon = b; |
| 87 | } |
| 88 | |
| 89 | public Bitmap getIcon(IconCache iconCache) { |
| 90 | if (mIcon == null) { |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 91 | updateIcon(iconCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 92 | } |
| 93 | return mIcon; |
| 94 | } |
| 95 | |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 96 | public void updateIcon(IconCache iconCache) { |
| 97 | mIcon = iconCache.getIcon(intent); |
| 98 | usingFallbackIcon = iconCache.isDefaultIcon(mIcon); |
| 99 | } |
| 100 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Creates the application intent based on a component name and various launch flags. |
| 103 | * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. |
| 104 | * |
| 105 | * @param className the class name of the component representing the intent |
| 106 | * @param launchFlags the launch flags |
| 107 | */ |
| 108 | final void setActivity(ComponentName className, int launchFlags) { |
| 109 | intent = new Intent(Intent.ACTION_MAIN); |
| 110 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 111 | intent.setComponent(className); |
| 112 | intent.setFlags(launchFlags); |
| 113 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | void onAddToDatabase(ContentValues values) { |
| 118 | super.onAddToDatabase(values); |
| 119 | |
| 120 | String titleStr = title != null ? title.toString() : null; |
| 121 | values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); |
| 122 | |
| 123 | String uri = intent != null ? intent.toUri(0) : null; |
| 124 | values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); |
| 125 | |
| 126 | if (customIcon) { |
| 127 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 128 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 129 | writeBitmap(values, mIcon); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 130 | } else { |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 131 | if (!usingFallbackIcon) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 132 | writeBitmap(values, mIcon); |
| 133 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 134 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 135 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); |
| 136 | if (iconResource != null) { |
| 137 | values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, |
| 138 | iconResource.packageName); |
| 139 | values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, |
| 140 | iconResource.resourceName); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public String toString() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 147 | return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id |
| 148 | + " type=" + this.itemType + " container=" + this.container + " screen=" + screen |
| 149 | + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 150 | + " dropPos=" + dropPos + ")"; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 153 | public static void dumpShortcutInfoList(String tag, String label, |
| 154 | ArrayList<ShortcutInfo> list) { |
| 155 | Log.d(tag, label + " size=" + list.size()); |
| 156 | for (ShortcutInfo info: list) { |
| 157 | Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon |
| 158 | + " customIcon=" + info.customIcon); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |