10589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/*
20589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Copyright (C) 2008 The Android Open Source Project
30589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
40589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
50589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * you may not use this file except in compliance with the License.
60589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * You may obtain a copy of the License at
70589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
80589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
90589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato *
100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Unless required by applicable law or agreed to in writing, software
110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * See the License for the specific language governing permissions and
140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * limitations under the License.
150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
19bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport android.annotation.TargetApi;
20349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyalimport android.content.ComponentName;
210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ContentValues;
221e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurkaimport android.content.Context;
230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent;
240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.graphics.Bitmap;
25bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport android.graphics.drawable.Drawable;
26bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport android.os.Build;
27bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport android.text.TextUtils;
280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
294fbc3828c5ae1e8c5789ede974447fa365f3c5a1Sunny Goyalimport com.android.launcher3.LauncherSettings.Favorites;
3018bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyalimport com.android.launcher3.compat.LauncherActivityInfoCompat;
31ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport com.android.launcher3.compat.UserHandleCompat;
3218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyalimport com.android.launcher3.compat.UserManagerCompat;
33261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.folder.FolderIcon;
34bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport com.android.launcher3.shortcuts.ShortcutInfoCompat;
351e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/**
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Represents a launchable icon on the workspaces and in folders.
380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */
397b168a1bb94044d9ae11004bae18beba9eed46fdAnjali Koppalpublic class ShortcutInfo extends ItemInfo {
400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
41349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public static final int DEFAULT = 0;
42aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
43349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    /**
44349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * The shortcut was restored from a backup and it not ready to be used. This is automatically
45349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * set during backup/restore
46349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     */
47349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public static final int FLAG_RESTORED_ICON = 1;
48aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
49349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    /**
50349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * The icon was added as an auto-install app, and is not ready to be used. This flag can't
51349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout
52349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * parsing.
53349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     */
54bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal    public static final int FLAG_AUTOINTALL_ICON = 2; //0B10;
55aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
56349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    /**
570c2f0700a55080318a4c21457408abb8af26740dSunny Goyal     * The icon is being installed. If {@link #FLAG_RESTORED_ICON} or {@link #FLAG_AUTOINTALL_ICON}
58349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * is set, then the icon is either being installed or is in a broken state.
59349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     */
60bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal    public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
61aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
639448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal     * Indicates that the widget restore has started.
649448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal     */
65bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal    public static final int FLAG_RESTORE_STARTED = 8; //0B1000;
66bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal
67bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal    /**
68bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal     * Indicates if it represents a common type mentioned in {@link CommonAppTypeParser}.
69bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal     * Upto 15 different types supported.
70bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal     */
71bb3b02f562bef4de063099c085d3199a77d06cfdSunny Goyal    public static final int FLAG_RESTORED_APP_TYPE = 0B0011110000;
729448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal
739448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal    /**
740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The intent used to start the application.
750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
76fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshev    public Intent intent;
770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
7956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Indicates whether we're using the default fallback icon instead of something from the
8056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * app.
8156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
8256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    boolean usingFallbackIcon;
8356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
8456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
8534b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal     * Indicates whether we're using a low res icon
8634b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal     */
8734b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal    boolean usingLowResIcon;
8834b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal
8934b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal    /**
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * If isShortcut=true and customIcon=false, this contains a reference to the
910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * shortcut icon as an application's resource.
920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
934e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    public Intent.ShortcutIconResource iconResource;
940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /**
960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * The application icon.
970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     */
980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mIcon;
990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
10040c5ed303909c4df71037be3429aa1423e59585fChris Wren    /**
1011a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal     * Indicates that the icon is disabled due to safe mode restrictions.
1021a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal     */
10370a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    public static final int FLAG_DISABLED_SAFEMODE = 1 << 0;
1041a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal
1051a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal    /**
1061a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal     * Indicates that the icon is disabled as the app is not available.
1071a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal     */
10870a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    public static final int FLAG_DISABLED_NOT_AVAILABLE = 1 << 1;
1091a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal
1101a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal    /**
11144cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy     * Indicates that the icon is disabled as the app is suspended
11244cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy     */
11370a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    public static final int FLAG_DISABLED_SUSPENDED = 1 << 2;
11444cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy
11544cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy    /**
116ff05f4375dd47242d7e4864287e0d5af8ac8ba8fKenny Guy     * Indicates that the icon is disabled as the user is in quiet mode.
117ff05f4375dd47242d7e4864287e0d5af8ac8ba8fKenny Guy     */
11870a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    public static final int FLAG_DISABLED_QUIET_USER = 1 << 3;
11970a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal
12070a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    /**
12170a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal     * Indicates that the icon is disabled as the publisher has disabled the actual shortcut.
12270a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal     */
12370a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal    public static final int FLAG_DISABLED_BY_PUBLISHER = 1 << 4;
124ff05f4375dd47242d7e4864287e0d5af8ac8ba8fKenny Guy
125ff05f4375dd47242d7e4864287e0d5af8ac8ba8fKenny Guy    /**
126d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     * Indicates that the icon is disabled as the user partition is currently locked.
127d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     */
128d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public static final int FLAG_DISABLED_LOCKED_USER = 1 << 5;
129d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
130d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    /**
131c5c60ad3592f53549c9ffaa58e9a87b0480080e8Sunny Goyal     * Could be disabled, if the the app is installed but unavailable (eg. in safe mode or when
132c5c60ad3592f53549c9ffaa58e9a87b0480080e8Sunny Goyal     * sd-card is not available).
133c5c60ad3592f53549c9ffaa58e9a87b0480080e8Sunny Goyal     */
1341a745e8f18e54aff152ff51743ae7595adde6927Sunny Goyal    int isDisabled = DEFAULT;
135c5c60ad3592f53549c9ffaa58e9a87b0480080e8Sunny Goyal
1364efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham    /**
1374efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham     * A message to display when the user tries to start a disabled shortcut.
1384efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham     * This is currently only used for deep shortcuts.
1394efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham     */
1404efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham    CharSequence disabledMessage;
1414efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham
142349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    int status;
14340c5ed303909c4df71037be3429aa1423e59585fChris Wren
144e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    /**
145e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal     * The installation progress [0-100] of the package that this shortcut represents.
146e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal     */
147349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    private int mInstallProgress;
148e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
149349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    /**
1500c2f0700a55080318a4c21457408abb8af26740dSunny Goyal     * TODO move this to {@link #status}
151b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     */
152349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    int flags = 0;
153b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren
1543484638cad97e255a412b0489a63873fb3ca4218Sunny Goyal    /**
155349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * If this shortcut is a placeholder, then intent will be a market intent for the package, and
156349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal     * this will hold the original intent from the database.  Otherwise, null.
1570c2f0700a55080318a4c21457408abb8af26740dSunny Goyal     * Refer {@link #FLAG_RESTORED_ICON}, {@link #FLAG_AUTOINTALL_ICON}
1583484638cad97e255a412b0489a63873fb3ca4218Sunny Goyal     */
159349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    Intent promisedIntent;
1603484638cad97e255a412b0489a63873fb3ca4218Sunny Goyal
161d139b0aa7d03f676dc7869dc5b39fd9f24ff0a1dSunny Goyal    public ShortcutInfo() {
1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
1630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
164997a92348a6d6e061737399321393449c16cd4d8Winson Chung
165a7ce166b2e06c834ac7d90ba0f4db16a84d10171Sunny Goyal    @Override
166e3e646e1f05caa2b500cc3deecc3a31457c83302Anjali Koppal    public Intent getIntent() {
167997a92348a6d6e061737399321393449c16cd4d8Winson Chung        return intent;
168997a92348a6d6e061737399321393449c16cd4d8Winson Chung    }
16972fbec17e09a1120971621587d5005f683baafd1Mathew Inwood
170fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham    /** Returns {@link #promisedIntent}, or {@link #intent} if promisedIntent is null. */
171fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham    public Intent getPromisedIntent() {
172fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham        return promisedIntent != null ? promisedIntent : intent;
173fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham    }
174fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham
175d6fe52636dcaa96ec1e10ce2daebe98b820c9739Kenny Guy    ShortcutInfo(Intent intent, CharSequence title, CharSequence contentDescription,
176c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            Bitmap icon, UserHandleCompat user) {
17772fbec17e09a1120971621587d5005f683baafd1Mathew Inwood        this();
17872fbec17e09a1120971621587d5005f683baafd1Mathew Inwood        this.intent = intent;
17982b016cb56540fe26213e817dd0dd668099c8e20Winson Chung        this.title = Utilities.trim(title);
180c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        this.contentDescription = contentDescription;
18172fbec17e09a1120971621587d5005f683baafd1Mathew Inwood        mIcon = icon;
182ed13187a745866483139e2878037e1f8427ce567Kenny Guy        this.user = user;
18372fbec17e09a1120971621587d5005f683baafd1Mathew Inwood    }
18472fbec17e09a1120971621587d5005f683baafd1Mathew Inwood
18510923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    public ShortcutInfo(ShortcutInfo info) {
186c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
18710923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        title = info.title;
1880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
18910923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        iconResource = info.iconResource;
1900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
191ed13187a745866483139e2878037e1f8427ce567Kenny Guy        flags = info.flags;
192349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        status = info.status;
19310923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        mInstallProgress = info.mInstallProgress;
19410923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        isDisabled = info.isDisabled;
19510923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        usingFallbackIcon = info.usingFallbackIcon;
1960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
199eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public ShortcutInfo(AppInfo info) {
200c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        super(info);
20182b016cb56540fe26213e817dd0dd668099c8e20Winson Chung        title = Utilities.trim(info.title);
2020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        intent = new Intent(info.intent);
2031e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        flags = info.flags;
20444cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy        isDisabled = info.isDisabled;
2051e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
2061e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
20710923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    public ShortcutInfo(LauncherActivityInfoCompat info, Context context) {
20810923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        user = info.getUser();
20910923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        title = Utilities.trim(info.getLabel());
21010923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        contentDescription = UserManagerCompat.getInstance(context)
21110923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal                .getBadgedLabelForUser(info.getLabel(), info.getUser());
21210923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        intent = AppInfo.makeLaunchIntent(context, info, info.getUser());
21310923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
21410923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        flags = AppInfo.initFlags(info);
21510923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    }
21610923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal
21710923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    /**
21810923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal     * Creates a {@link ShortcutInfo} from a {@link ShortcutInfoCompat}.
21910923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal     */
22010923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    @TargetApi(Build.VERSION_CODES.N)
22110923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    public ShortcutInfo(ShortcutInfoCompat shortcutInfo, Context context) {
22210923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        user = shortcutInfo.getUserHandle();
22310923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        itemType = LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
22410923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        flags = 0;
22510923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal        updateFromDeepShortcutInfo(shortcutInfo, context);
22610923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    }
22710923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal
2280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public void setIcon(Bitmap b) {
2290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIcon = b;
2300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
2310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public Bitmap getIcon(IconCache iconCache) {
2330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (mIcon == null) {
234e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka            updateIcon(iconCache);
2350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
2360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mIcon;
2370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
2380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
239317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    public void updateIcon(IconCache iconCache, boolean useLowRes) {
2404fbc3828c5ae1e8c5789ede974447fa365f3c5a1Sunny Goyal        if (itemType == Favorites.ITEM_TYPE_APPLICATION) {
24134b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal            iconCache.getTitleAndIcon(this, promisedIntent != null ? promisedIntent : intent, user,
242317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal                    useLowRes);
2434fbc3828c5ae1e8c5789ede974447fa365f3c5a1Sunny Goyal        }
2440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
2450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
246317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    public void updateIcon(IconCache iconCache) {
247317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal        updateIcon(iconCache, shouldUseLowResIcon());
248317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    }
249317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal
2500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    @Override
251ed13187a745866483139e2878037e1f8427ce567Kenny Guy    void onAddToDatabase(Context context, ContentValues values) {
252ed13187a745866483139e2878037e1f8427ce567Kenny Guy        super.onAddToDatabase(context, values);
2530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String titleStr = title != null ? title.toString() : null;
2550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
2560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
257349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        String uri = promisedIntent != null ? promisedIntent.toUri(0)
2589623a9951acd3e2e7ceb4c8551e3bff4b35001ddSunny Goyal                : (intent != null ? intent.toUri(0) : null);
2590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
260756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal        values.put(LauncherSettings.Favorites.RESTORED, status);
2610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
262eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        if (!usingFallbackIcon && !usingLowResIcon) {
26356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            writeBitmap(values, mIcon);
264eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        }
265eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        if (iconResource != null) {
266eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
267eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal                    iconResource.packageName);
268eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
269eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal                    iconResource.resourceName);
2700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
2710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
2720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
273349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public ComponentName getTargetComponent() {
274349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        return promisedIntent != null ? promisedIntent.getComponent() : intent.getComponent();
275aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    }
276aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
277349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public boolean hasStatusFlag(int flag) {
278349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        return (status & flag) != 0;
279aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    }
28040c5ed303909c4df71037be3429aa1423e59585fChris Wren
281e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
282349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public final boolean isPromise() {
283349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINTALL_ICON);
28440c5ed303909c4df71037be3429aa1423e59585fChris Wren    }
28540c5ed303909c4df71037be3429aa1423e59585fChris Wren
286349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public int getInstallProgress() {
287349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        return mInstallProgress;
28840c5ed303909c4df71037be3429aa1423e59585fChris Wren    }
289e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
290349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public void setInstallProgress(int progress) {
291349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        mInstallProgress = progress;
292349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal        status |= FLAG_INSTALL_SESSION_ACTIVE;
293e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
29434b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal
29534b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal    public boolean shouldUseLowResIcon() {
29634b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal        return usingLowResIcon && container >= 0 && rank >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
29734b6527cefd36fbd5da78464ce9771e379158552Sunny Goyal    }
29818bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal
2999994b2b171ab78d1d93e8a25086d6853f766e80fSunny Goyal    public void updateFromDeepShortcutInfo(ShortcutInfoCompat shortcutInfo, Context context) {
300d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        // {@link ShortcutInfoCompat#getActivity} can change during an update. Recreate the intent
301d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        intent = shortcutInfo.makeIntent(context);
302bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        title = shortcutInfo.getShortLabel();
303bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham
304bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        CharSequence label = shortcutInfo.getLongLabel();
305bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        if (TextUtils.isEmpty(label)) {
306bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham            label = shortcutInfo.getShortLabel();
307bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        }
308b93f34a8be4450d2b6cb47a73f43c84c194bdb95Winson        contentDescription = UserManagerCompat.getInstance(context)
309bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham                .getBadgedLabelForUser(label, user);
31070a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal        if (shortcutInfo.isEnabled()) {
31170a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal            isDisabled &= ~FLAG_DISABLED_BY_PUBLISHER;
31270a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal        } else {
31370a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal            isDisabled |= FLAG_DISABLED_BY_PUBLISHER;
31470a7c9b70003ae87e2b2968bc6ceded0fe0f0e8bSunny Goyal        }
3154efffc5ba69fdf44be1bf2b540e24cd2bd85d18cTony Wickham        disabledMessage = shortcutInfo.getDisabledMessage();
316bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham
31779cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        // TODO: Use cache for this
318bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        LauncherAppState launcherAppState = LauncherAppState.getInstance();
31979cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        Drawable unbadgedDrawable = launcherAppState.getShortcutManager()
3209994b2b171ab78d1d93e8a25086d6853f766e80fSunny Goyal                .getShortcutIconDrawable(shortcutInfo,
3219994b2b171ab78d1d93e8a25086d6853f766e80fSunny Goyal                        launcherAppState.getInvariantDeviceProfile().fillResIconDpi);
32279cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal
32379cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        IconCache cache = launcherAppState.getIconCache();
32479cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        Bitmap unbadgedBitmap = unbadgedDrawable == null
32579cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal                ? cache.getDefaultIcon(UserHandleCompat.myUserHandle())
32679cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal                : Utilities.createScaledBitmapWithoutShadow(unbadgedDrawable, context);
32779cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        setIcon(getBadgedIcon(unbadgedBitmap, shortcutInfo, cache, context));
328bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham    }
329bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham
33079cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal    protected Bitmap getBadgedIcon(Bitmap unbadgedBitmap, ShortcutInfoCompat shortcutInfo,
33179cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal            IconCache cache, Context context) {
33279cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        unbadgedBitmap = Utilities.addShadowToIcon(unbadgedBitmap);
33379cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        // Get the app info for the source activity.
33479cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        AppInfo appInfo = new AppInfo();
33579cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        appInfo.user = user;
33679cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        appInfo.componentName = shortcutInfo.getActivity();
33779cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        try {
33879cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal            cache.getTitleAndIcon(appInfo, shortcutInfo.getActivityInfo(context), false);
33979cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        } catch (NullPointerException e) {
34079cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal            // This may happen when we fail to load the activity info. Worst case ignore badging.
34179cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal            return Utilities.badgeIconForUser(unbadgedBitmap, user, context);
34279cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        }
34379cf718fcdf39d0e60743b32c6611fbb971ff38cSunny Goyal        return Utilities.badgeWithBitmap(unbadgedBitmap, appInfo.iconBitmap, context);
34410923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal    }
34510923b30a22b4ec8d10cf8536bddf3f176e6c26fSunny Goyal
346bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham    /** Returns the ShortcutInfo id associated with the deep shortcut. */
347bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham    public String getDeepShortcutId() {
348bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        return itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT ?
349fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickham                getPromisedIntent().getStringExtra(ShortcutInfoCompat.EXTRA_SHORTCUT_ID) : null;
350bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham    }
351bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham
35244cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy    @Override
35344cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy    public boolean isDisabled() {
35444cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy        return isDisabled != 0;
35544cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy    }
3560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato}
357