blob: 8be48721cc7f5bbbf2137b7f7449c5f375f3c3c2 [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
Sunny Goyal34942622014-08-29 17:20:55 -070019import android.content.ComponentName;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import 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;
23import android.graphics.Bitmap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080024import android.util.Log;
25
Sunny Goyal4fbc3822015-02-18 16:46:50 -080026import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070027import com.android.launcher3.compat.LauncherActivityInfoCompat;
Kenny Guyed131872014-04-30 03:02:21 +010028import com.android.launcher3.compat.UserHandleCompat;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070029import com.android.launcher3.compat.UserManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010030
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
Sunny Goyal34942622014-08-29 17:20:55 -070039 public static final int DEFAULT = 0;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050040
Sunny Goyal34942622014-08-29 17:20:55 -070041 /**
42 * The shortcut was restored from a backup and it not ready to be used. This is automatically
43 * set during backup/restore
44 */
45 public static final int FLAG_RESTORED_ICON = 1;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050046
Sunny Goyal34942622014-08-29 17:20:55 -070047 /**
48 * The icon was added as an auto-install app, and is not ready to be used. This flag can't
49 * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout
50 * parsing.
51 */
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080052 public static final int FLAG_AUTOINTALL_ICON = 2; //0B10;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050053
Sunny Goyal34942622014-08-29 17:20:55 -070054 /**
55 * The icon is being installed. If {@link FLAG_RESTORED_ICON} or {@link FLAG_AUTOINTALL_ICON}
56 * is set, then the icon is either being installed or is in a broken state.
57 */
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080058 public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050059
Joe Onorato0589f0f2010-02-08 13:44:00 -080060 /**
Sunny Goyal94485362014-09-18 16:13:58 -070061 * Indicates that the widget restore has started.
62 */
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080063 public static final int FLAG_RESTORE_STARTED = 8; //0B1000;
64
65 /**
66 * Indicates if it represents a common type mentioned in {@link CommonAppTypeParser}.
67 * Upto 15 different types supported.
68 */
69 public static final int FLAG_RESTORED_APP_TYPE = 0B0011110000;
Sunny Goyal94485362014-09-18 16:13:58 -070070
71 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080072 * The intent used to start the application.
73 */
74 Intent intent;
75
76 /**
77 * Indicates whether the icon comes from an application's resource (if false)
78 * or from a custom Bitmap (if true.)
79 */
80 boolean customIcon;
81
82 /**
Joe Onorato56d82912010-03-07 14:32:10 -050083 * Indicates whether we're using the default fallback icon instead of something from the
84 * app.
85 */
86 boolean usingFallbackIcon;
87
88 /**
Sunny Goyal34b65272015-03-11 16:56:52 -070089 * Indicates whether we're using a low res icon
90 */
91 boolean usingLowResIcon;
92
93 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080094 * If isShortcut=true and customIcon=false, this contains a reference to the
95 * shortcut icon as an application's resource.
96 */
97 Intent.ShortcutIconResource iconResource;
98
99 /**
100 * The application icon.
101 */
102 private Bitmap mIcon;
103
Chris Wren40c5ed32014-06-24 18:24:23 -0400104 /**
Sunny Goyal1a745e82014-10-02 15:58:31 -0700105 * Indicates that the icon is disabled due to safe mode restrictions.
106 */
107 public static final int FLAG_DISABLED_SAFEMODE = 1;
108
109 /**
110 * Indicates that the icon is disabled as the app is not available.
111 */
112 public static final int FLAG_DISABLED_NOT_AVAILABLE = 2;
113
114 /**
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700115 * Could be disabled, if the the app is installed but unavailable (eg. in safe mode or when
116 * sd-card is not available).
117 */
Sunny Goyal1a745e82014-10-02 15:58:31 -0700118 int isDisabled = DEFAULT;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700119
Sunny Goyal34942622014-08-29 17:20:55 -0700120 int status;
Chris Wren40c5ed32014-06-24 18:24:23 -0400121
Sunny Goyale755d462014-07-22 13:48:29 -0700122 /**
123 * The installation progress [0-100] of the package that this shortcut represents.
124 */
Sunny Goyal34942622014-08-29 17:20:55 -0700125 private int mInstallProgress;
Sunny Goyale755d462014-07-22 13:48:29 -0700126
Sunny Goyal34942622014-08-29 17:20:55 -0700127 /**
128 * Refer {@link AppInfo#firstInstallTime}.
129 */
Michael Jurka1e2f4652013-07-08 18:03:46 -0700130 long firstInstallTime;
Sunny Goyal34942622014-08-29 17:20:55 -0700131
132 /**
133 * TODO move this to {@link status}
134 */
Michael Jurka1e2f4652013-07-08 18:03:46 -0700135 int flags = 0;
136
Chris Wrenb6d4c282014-01-27 14:17:08 -0500137 /**
138 * If this shortcut is a placeholder, then intent will be a market intent for the package, and
139 * this will hold the original intent from the database. Otherwise, null.
Sunny Goyal34942622014-08-29 17:20:55 -0700140 * Refer {@link #FLAG_RESTORE_PENDING}, {@link #FLAG_INSTALL_PENDING}
Chris Wrenb6d4c282014-01-27 14:17:08 -0500141 */
Sunny Goyal34942622014-08-29 17:20:55 -0700142 Intent promisedIntent;
Sunny Goyal34846382014-07-09 00:09:28 -0700143
Michael Jurkac9d95c52011-08-29 14:03:34 -0700144 ShortcutInfo() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
146 }
Winson Chung997a9232013-07-24 15:33:46 -0700147
Anjali Koppale3e646e2014-03-17 09:34:39 -0700148 public Intent getIntent() {
Winson Chung997a9232013-07-24 15:33:46 -0700149 return intent;
150 }
Mathew Inwood72fbec12013-11-19 15:45:07 +0000151
Kenny Guyd6fe5262014-07-21 17:11:41 +0100152 ShortcutInfo(Intent intent, CharSequence title, CharSequence contentDescription,
Kenny Guyc2bd8102014-06-30 12:30:31 +0100153 Bitmap icon, UserHandleCompat user) {
Mathew Inwood72fbec12013-11-19 15:45:07 +0000154 this();
155 this.intent = intent;
Winson Chung82b016c2015-05-08 17:00:10 -0700156 this.title = Utilities.trim(title);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100157 this.contentDescription = contentDescription;
Mathew Inwood72fbec12013-11-19 15:45:07 +0000158 mIcon = icon;
Kenny Guyed131872014-04-30 03:02:21 +0100159 this.user = user;
Mathew Inwood72fbec12013-11-19 15:45:07 +0000160 }
161
Michael Jurka1e2f4652013-07-08 18:03:46 -0700162 public ShortcutInfo(Context context, ShortcutInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700163 super(info);
Winson Chung82b016c2015-05-08 17:00:10 -0700164 title = Utilities.trim(info.title);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800165 intent = new Intent(info.intent);
166 if (info.iconResource != null) {
167 iconResource = new Intent.ShortcutIconResource();
168 iconResource.packageName = info.iconResource.packageName;
169 iconResource.resourceName = info.iconResource.resourceName;
170 }
171 mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all
172 customIcon = info.customIcon;
Kenny Guyed131872014-04-30 03:02:21 +0100173 flags = info.flags;
174 firstInstallTime = info.firstInstallTime;
175 user = info.user;
Sunny Goyal34942622014-08-29 17:20:55 -0700176 status = info.status;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177 }
178
179 /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200180 public ShortcutInfo(AppInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700181 super(info);
Winson Chung82b016c2015-05-08 17:00:10 -0700182 title = Utilities.trim(info.title);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800183 intent = new Intent(info.intent);
184 customIcon = false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700185 flags = info.flags;
186 firstInstallTime = info.firstInstallTime;
187 }
188
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 public void setIcon(Bitmap b) {
190 mIcon = b;
191 }
192
193 public Bitmap getIcon(IconCache iconCache) {
194 if (mIcon == null) {
Michael Jurkae384aff2012-03-09 15:59:25 -0800195 updateIcon(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800196 }
197 return mIcon;
198 }
199
Michael Jurkae384aff2012-03-09 15:59:25 -0800200 public void updateIcon(IconCache iconCache) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800201 if (itemType == Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700202 iconCache.getTitleAndIcon(this, promisedIntent != null ? promisedIntent : intent, user,
203 shouldUseLowResIcon());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800204 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800205 }
206
207 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100208 void onAddToDatabase(Context context, ContentValues values) {
209 super.onAddToDatabase(context, values);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210
211 String titleStr = title != null ? title.toString() : null;
212 values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
213
Sunny Goyal34942622014-08-29 17:20:55 -0700214 String uri = promisedIntent != null ? promisedIntent.toUri(0)
Sunny Goyal9623a992014-07-23 15:06:44 -0700215 : (intent != null ? intent.toUri(0) : null);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700217 values.put(LauncherSettings.Favorites.RESTORED, status);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218
219 if (customIcon) {
220 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
221 LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
Joe Onorato56d82912010-03-07 14:32:10 -0500222 writeBitmap(values, mIcon);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 } else {
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700224 if (!usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -0500225 writeBitmap(values, mIcon);
226 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 if (iconResource != null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800228 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
229 LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
231 iconResource.packageName);
232 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
233 iconResource.resourceName);
234 }
235 }
236 }
237
238 @Override
239 public String toString() {
Kenny Guyed131872014-04-30 03:02:21 +0100240 return "ShortcutInfo(title=" + title + "intent=" + intent + "id=" + this.id
Adam Cohendcd297f2013-06-18 13:13:40 -0700241 + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
Winson Chungdb8a8942012-04-03 14:08:41 -0700242 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
Kenny Guyed131872014-04-30 03:02:21 +0100243 + " dropPos=" + Arrays.toString(dropPos) + " user=" + user + ")";
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 }
245
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 public static void dumpShortcutInfoList(String tag, String label,
247 ArrayList<ShortcutInfo> list) {
248 Log.d(tag, label + " size=" + list.size());
249 for (ShortcutInfo info: list) {
250 Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon
251 + " customIcon=" + info.customIcon);
252 }
253 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500254
Sunny Goyal34942622014-08-29 17:20:55 -0700255 public ComponentName getTargetComponent() {
256 return promisedIntent != null ? promisedIntent.getComponent() : intent.getComponent();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500257 }
258
Sunny Goyal34942622014-08-29 17:20:55 -0700259 public boolean hasStatusFlag(int flag) {
260 return (status & flag) != 0;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500261 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400262
Sunny Goyal34942622014-08-29 17:20:55 -0700263
264 public final boolean isPromise() {
265 return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINTALL_ICON);
Chris Wren40c5ed32014-06-24 18:24:23 -0400266 }
267
Sunny Goyal34942622014-08-29 17:20:55 -0700268 public int getInstallProgress() {
269 return mInstallProgress;
Sunny Goyale755d462014-07-22 13:48:29 -0700270 }
271
Sunny Goyal34942622014-08-29 17:20:55 -0700272 public void setInstallProgress(int progress) {
273 mInstallProgress = progress;
274 status |= FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyale755d462014-07-22 13:48:29 -0700275 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700276
277 public boolean shouldUseLowResIcon() {
278 return usingLowResIcon && container >= 0 && rank >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
279 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700280
281 public static ShortcutInfo fromActivityInfo(LauncherActivityInfoCompat info, Context context) {
282 final ShortcutInfo shortcut = new ShortcutInfo();
283 shortcut.user = info.getUser();
Winson Chung82b016c2015-05-08 17:00:10 -0700284 shortcut.title = Utilities.trim(info.getLabel());
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700285 shortcut.contentDescription = UserManagerCompat.getInstance(context)
286 .getBadgedLabelForUser(info.getLabel(), info.getUser());
287 shortcut.customIcon = false;
288 shortcut.intent = AppInfo.makeLaunchIntent(context, info, info.getUser());
289 shortcut.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
290 shortcut.flags = AppInfo.initFlags(info);
291 shortcut.firstInstallTime = info.getFirstInstallTime();
292 return shortcut;
293 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294}
295