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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.ContentValues; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 23 | import android.content.pm.PackageInfo; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.PackageManager.NameNotFoundException; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 29 | import com.android.launcher3.compat.UserHandleCompat; |
| 30 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
Sameer Padala | be3e410 | 2014-04-21 19:36:14 -0700 | [diff] [blame] | 32 | import java.util.Arrays; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 33 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Represents a launchable icon on the workspaces and in folders. |
| 36 | */ |
Anjali Koppal | 7b168a1 | 2014-03-04 17:16:11 -0800 | [diff] [blame] | 37 | public class ShortcutInfo extends ItemInfo { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 38 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 39 | /** {@link #mState} meaning this package is not installed, and there is no other information. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 40 | public static final int PACKAGE_STATE_UNKNOWN = -2; |
| 41 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 42 | /** {@link #mState} meaning this package is not installed, because installation failed. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 43 | public static final int PACKAGE_STATE_ERROR = -1; |
| 44 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 45 | /** {@link #mState} meaning this package is installed. This is the typical case. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 46 | public static final int PACKAGE_STATE_DEFAULT = 0; |
| 47 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 48 | /** {@link #mState} meaning some external entity has promised to install this package. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 49 | public static final int PACKAGE_STATE_ENQUEUED = 1; |
| 50 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 51 | /** {@link #mState} meaning but some external entity is downloading this package. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 52 | public static final int PACKAGE_STATE_DOWNLOADING = 2; |
| 53 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 54 | /** {@link #mState} meaning some external entity is installing this package. */ |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 55 | public static final int PACKAGE_STATE_INSTALLING = 3; |
| 56 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 57 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 58 | * The intent used to start the application. |
| 59 | */ |
| 60 | Intent intent; |
| 61 | |
| 62 | /** |
| 63 | * Indicates whether the icon comes from an application's resource (if false) |
| 64 | * or from a custom Bitmap (if true.) |
| 65 | */ |
| 66 | boolean customIcon; |
| 67 | |
| 68 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 69 | * Indicates whether we're using the default fallback icon instead of something from the |
| 70 | * app. |
| 71 | */ |
| 72 | boolean usingFallbackIcon; |
| 73 | |
| 74 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 75 | * If isShortcut=true and customIcon=false, this contains a reference to the |
| 76 | * shortcut icon as an application's resource. |
| 77 | */ |
| 78 | Intent.ShortcutIconResource iconResource; |
| 79 | |
| 80 | /** |
| 81 | * The application icon. |
| 82 | */ |
| 83 | private Bitmap mIcon; |
| 84 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 85 | /** |
| 86 | * The installation state of the package that this shortcut represents. |
| 87 | */ |
| 88 | protected int mState; |
| 89 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 90 | long firstInstallTime; |
| 91 | int flags = 0; |
| 92 | |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 93 | /** |
| 94 | * If this shortcut is a placeholder, then intent will be a market intent for the package, and |
| 95 | * this will hold the original intent from the database. Otherwise, null. |
| 96 | */ |
| 97 | Intent restoredIntent; |
| 98 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 99 | ShortcutInfo() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 100 | itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; |
| 101 | } |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 102 | |
Anjali Koppal | e3e646e | 2014-03-17 09:34:39 -0700 | [diff] [blame] | 103 | public Intent getIntent() { |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 104 | return intent; |
| 105 | } |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 106 | |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 107 | protected Intent getRestoredIntent() { |
| 108 | return restoredIntent; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Overwrite placeholder data with restored data, or do nothing if this is not a placeholder. |
| 113 | */ |
| 114 | public void restore() { |
| 115 | if (restoredIntent != null) { |
| 116 | intent = restoredIntent; |
| 117 | restoredIntent = null; |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 118 | mState = PACKAGE_STATE_DEFAULT; |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame^] | 122 | ShortcutInfo(Intent intent, CharSequence title, String contentDescrition, |
| 123 | Bitmap icon, UserHandleCompat user) { |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 124 | this(); |
| 125 | this.intent = intent; |
| 126 | this.title = title; |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame^] | 127 | this.contentDescription = contentDescription; |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 128 | mIcon = icon; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 129 | this.user = user; |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 132 | public ShortcutInfo(Context context, ShortcutInfo info) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 133 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 134 | title = info.title.toString(); |
| 135 | intent = new Intent(info.intent); |
| 136 | if (info.iconResource != null) { |
| 137 | iconResource = new Intent.ShortcutIconResource(); |
| 138 | iconResource.packageName = info.iconResource.packageName; |
| 139 | iconResource.resourceName = info.iconResource.resourceName; |
| 140 | } |
| 141 | mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all |
| 142 | customIcon = info.customIcon; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 143 | flags = info.flags; |
| 144 | firstInstallTime = info.firstInstallTime; |
| 145 | user = info.user; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 149 | public ShortcutInfo(AppInfo info) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 150 | super(info); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 151 | title = info.title.toString(); |
| 152 | intent = new Intent(info.intent); |
| 153 | customIcon = false; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 154 | flags = info.flags; |
| 155 | firstInstallTime = info.firstInstallTime; |
| 156 | } |
| 157 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 158 | public void setIcon(Bitmap b) { |
| 159 | mIcon = b; |
| 160 | } |
| 161 | |
| 162 | public Bitmap getIcon(IconCache iconCache) { |
| 163 | if (mIcon == null) { |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 164 | updateIcon(iconCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 165 | } |
| 166 | return mIcon; |
| 167 | } |
| 168 | |
Michael Jurka | e384aff | 2012-03-09 15:59:25 -0800 | [diff] [blame] | 169 | public void updateIcon(IconCache iconCache) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 170 | mIcon = iconCache.getIcon(intent, user); |
| 171 | usingFallbackIcon = iconCache.isDefaultIcon(mIcon, user); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 175 | void onAddToDatabase(Context context, ContentValues values) { |
| 176 | super.onAddToDatabase(context, values); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 177 | |
| 178 | String titleStr = title != null ? title.toString() : null; |
| 179 | values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); |
| 180 | |
| 181 | String uri = intent != null ? intent.toUri(0) : null; |
| 182 | values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); |
| 183 | |
| 184 | if (customIcon) { |
| 185 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 186 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 187 | writeBitmap(values, mIcon); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 188 | } else { |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 189 | if (!usingFallbackIcon) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 190 | writeBitmap(values, mIcon); |
| 191 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 192 | values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, |
| 193 | LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); |
| 194 | if (iconResource != null) { |
| 195 | values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, |
| 196 | iconResource.packageName); |
| 197 | values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, |
| 198 | iconResource.resourceName); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public String toString() { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 205 | return "ShortcutInfo(title=" + title + "intent=" + intent + "id=" + this.id |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 206 | + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 207 | + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 208 | + " dropPos=" + Arrays.toString(dropPos) + " user=" + user + ")"; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 211 | public static void dumpShortcutInfoList(String tag, String label, |
| 212 | ArrayList<ShortcutInfo> list) { |
| 213 | Log.d(tag, label + " size=" + list.size()); |
| 214 | for (ShortcutInfo info: list) { |
| 215 | Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon |
| 216 | + " customIcon=" + info.customIcon); |
| 217 | } |
| 218 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 219 | |
| 220 | public boolean isPromise() { |
| 221 | return restoredIntent != null; |
| 222 | } |
| 223 | |
| 224 | public boolean isPromiseFor(String pkgName) { |
| 225 | return restoredIntent != null |
| 226 | && pkgName != null |
| 227 | && pkgName.equals(restoredIntent.getComponent().getPackageName()); |
| 228 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 229 | |
| 230 | public boolean isAbandoned() { |
| 231 | return isPromise() |
| 232 | && (mState == ShortcutInfo.PACKAGE_STATE_ERROR |
| 233 | || mState == ShortcutInfo.PACKAGE_STATE_UNKNOWN); |
| 234 | } |
| 235 | |
| 236 | public int getState() { |
| 237 | return mState; |
| 238 | } |
| 239 | |
| 240 | public void setState(int state) { |
| 241 | mState = state; |
| 242 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 243 | } |
| 244 | |