blob: d2573a4a19372a667769a24e5eef52deb02ede81 [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
19import android.content.ComponentName;
20import android.content.ContentValues;
Michael Jurka1e2f4652013-07-08 18:03:46 -070021import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080022import android.content.Intent;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010023import android.content.pm.PackageInfo;
Michael Jurka1e2f4652013-07-08 18:03:46 -070024import android.content.pm.PackageManager;
25import android.content.pm.PackageManager.NameNotFoundException;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.graphics.Bitmap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.util.Log;
28
Kenny Guyed131872014-04-30 03:02:21 +010029import com.android.launcher3.compat.UserHandleCompat;
30
Michael Jurka1e2f4652013-07-08 18:03:46 -070031import java.util.ArrayList;
Sameer Padalabe3e4102014-04-21 19:36:14 -070032import java.util.Arrays;
Michael Jurka1e2f4652013-07-08 18:03:46 -070033
Joe Onorato0589f0f2010-02-08 13:44:00 -080034/**
35 * Represents a launchable icon on the workspaces and in folders.
36 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080037public class ShortcutInfo extends ItemInfo {
Joe Onorato0589f0f2010-02-08 13:44:00 -080038
Chris Wren40c5ed32014-06-24 18:24:23 -040039 /** {@link #mState} meaning this package is not installed, and there is no other information. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050040 public static final int PACKAGE_STATE_UNKNOWN = -2;
41
Chris Wren40c5ed32014-06-24 18:24:23 -040042 /** {@link #mState} meaning this package is not installed, because installation failed. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050043 public static final int PACKAGE_STATE_ERROR = -1;
44
Chris Wren40c5ed32014-06-24 18:24:23 -040045 /** {@link #mState} meaning this package is installed. This is the typical case. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050046 public static final int PACKAGE_STATE_DEFAULT = 0;
47
Chris Wren40c5ed32014-06-24 18:24:23 -040048 /** {@link #mState} meaning some external entity has promised to install this package. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050049 public static final int PACKAGE_STATE_ENQUEUED = 1;
50
Chris Wren40c5ed32014-06-24 18:24:23 -040051 /** {@link #mState} meaning but some external entity is downloading this package. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050052 public static final int PACKAGE_STATE_DOWNLOADING = 2;
53
Chris Wren40c5ed32014-06-24 18:24:23 -040054 /** {@link #mState} meaning some external entity is installing this package. */
Chris Wrenaeff7ea2014-02-14 16:59:24 -050055 public static final int PACKAGE_STATE_INSTALLING = 3;
56
Joe Onorato0589f0f2010-02-08 13:44:00 -080057 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080058 * 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 Onorato56d82912010-03-07 14:32:10 -050069 * Indicates whether we're using the default fallback icon instead of something from the
70 * app.
71 */
72 boolean usingFallbackIcon;
73
74 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080075 * 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 Wren40c5ed32014-06-24 18:24:23 -040085 /**
86 * The installation state of the package that this shortcut represents.
87 */
88 protected int mState;
89
Michael Jurka1e2f4652013-07-08 18:03:46 -070090 long firstInstallTime;
91 int flags = 0;
92
Chris Wrenb6d4c282014-01-27 14:17:08 -050093 /**
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 Jurkac9d95c52011-08-29 14:03:34 -070099 ShortcutInfo() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
101 }
Winson Chung997a9232013-07-24 15:33:46 -0700102
Anjali Koppale3e646e2014-03-17 09:34:39 -0700103 public Intent getIntent() {
Winson Chung997a9232013-07-24 15:33:46 -0700104 return intent;
105 }
Mathew Inwood72fbec12013-11-19 15:45:07 +0000106
Chris Wrenb6d4c282014-01-27 14:17:08 -0500107 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 Wren40c5ed32014-06-24 18:24:23 -0400118 mState = PACKAGE_STATE_DEFAULT;
Chris Wrenb6d4c282014-01-27 14:17:08 -0500119 }
120 }
121
Kenny Guyc2bd8102014-06-30 12:30:31 +0100122 ShortcutInfo(Intent intent, CharSequence title, String contentDescrition,
123 Bitmap icon, UserHandleCompat user) {
Mathew Inwood72fbec12013-11-19 15:45:07 +0000124 this();
125 this.intent = intent;
126 this.title = title;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100127 this.contentDescription = contentDescription;
Mathew Inwood72fbec12013-11-19 15:45:07 +0000128 mIcon = icon;
Kenny Guyed131872014-04-30 03:02:21 +0100129 this.user = user;
Mathew Inwood72fbec12013-11-19 15:45:07 +0000130 }
131
Michael Jurka1e2f4652013-07-08 18:03:46 -0700132 public ShortcutInfo(Context context, ShortcutInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700133 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 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 Guyed131872014-04-30 03:02:21 +0100143 flags = info.flags;
144 firstInstallTime = info.firstInstallTime;
145 user = info.user;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 }
147
148 /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200149 public ShortcutInfo(AppInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700150 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 title = info.title.toString();
152 intent = new Intent(info.intent);
153 customIcon = false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700154 flags = info.flags;
155 firstInstallTime = info.firstInstallTime;
156 }
157
Joe Onorato0589f0f2010-02-08 13:44:00 -0800158 public void setIcon(Bitmap b) {
159 mIcon = b;
160 }
161
162 public Bitmap getIcon(IconCache iconCache) {
163 if (mIcon == null) {
Michael Jurkae384aff2012-03-09 15:59:25 -0800164 updateIcon(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800165 }
166 return mIcon;
167 }
168
Michael Jurkae384aff2012-03-09 15:59:25 -0800169 public void updateIcon(IconCache iconCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100170 mIcon = iconCache.getIcon(intent, user);
171 usingFallbackIcon = iconCache.isDefaultIcon(mIcon, user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800172 }
173
174 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100175 void onAddToDatabase(Context context, ContentValues values) {
176 super.onAddToDatabase(context, values);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177
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 Onorato56d82912010-03-07 14:32:10 -0500187 writeBitmap(values, mIcon);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 } else {
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700189 if (!usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -0500190 writeBitmap(values, mIcon);
191 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 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 Guyed131872014-04-30 03:02:21 +0100205 return "ShortcutInfo(title=" + title + "intent=" + intent + "id=" + this.id
Adam Cohendcd297f2013-06-18 13:13:40 -0700206 + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
Winson Chungdb8a8942012-04-03 14:08:41 -0700207 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
Kenny Guyed131872014-04-30 03:02:21 +0100208 + " dropPos=" + Arrays.toString(dropPos) + " user=" + user + ")";
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 }
210
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 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 Wrenaeff7ea2014-02-14 16:59:24 -0500219
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 Wren40c5ed32014-06-24 18:24:23 -0400229
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 Onorato0589f0f2010-02-08 13:44:00 -0800243}
244