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
17package com.android.launcher3;
18
19import android.content.ComponentName;
20import android.content.ContentValues;
21import android.content.Context;
22import android.content.Intent;
23import android.graphics.Bitmap;
24import android.util.Log;
25
26import com.android.launcher3.LauncherSettings.Favorites;
27import com.android.launcher3.compat.LauncherActivityInfoCompat;
28import com.android.launcher3.compat.UserHandleCompat;
29import com.android.launcher3.compat.UserManagerCompat;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33
34/**
35 * Represents a launchable icon on the workspaces and in folders.
36 */
37public class ShortcutInfo extends ItemInfo {
38
39    public static final int DEFAULT = 0;
40
41    /**
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;
46
47    /**
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     */
52    public static final int FLAG_AUTOINTALL_ICON = 2; //0B10;
53
54    /**
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     */
58    public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
59
60    /**
61     * Indicates that the widget restore has started.
62     */
63    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;
70
71    /**
72     * 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     * TODO: remove this flag
80     */
81    public boolean customIcon;
82
83    /**
84     * Indicates whether we're using the default fallback icon instead of something from the
85     * app.
86     */
87    boolean usingFallbackIcon;
88
89    /**
90     * Indicates whether we're using a low res icon
91     */
92    boolean usingLowResIcon;
93
94    /**
95     * If isShortcut=true and customIcon=false, this contains a reference to the
96     * shortcut icon as an application's resource.
97     */
98    public Intent.ShortcutIconResource iconResource;
99
100    /**
101     * The application icon.
102     */
103    private Bitmap mIcon;
104
105    /**
106     * Indicates that the icon is disabled due to safe mode restrictions.
107     */
108    public static final int FLAG_DISABLED_SAFEMODE = 1;
109
110    /**
111     * Indicates that the icon is disabled as the app is not available.
112     */
113    public static final int FLAG_DISABLED_NOT_AVAILABLE = 2;
114
115    /**
116     * Could be disabled, if the the app is installed but unavailable (eg. in safe mode or when
117     * sd-card is not available).
118     */
119    int isDisabled = DEFAULT;
120
121    int status;
122
123    /**
124     * The installation progress [0-100] of the package that this shortcut represents.
125     */
126    private int mInstallProgress;
127
128    /**
129     * Refer {@link AppInfo#firstInstallTime}.
130     */
131    public long firstInstallTime;
132
133    /**
134     * TODO move this to {@link status}
135     */
136    int flags = 0;
137
138    /**
139     * If this shortcut is a placeholder, then intent will be a market intent for the package, and
140     * this will hold the original intent from the database.  Otherwise, null.
141     * Refer {@link #FLAG_RESTORE_PENDING}, {@link #FLAG_INSTALL_PENDING}
142     */
143    Intent promisedIntent;
144
145    ShortcutInfo() {
146        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
147    }
148
149    public Intent getIntent() {
150        return intent;
151    }
152
153    ShortcutInfo(Intent intent, CharSequence title, CharSequence contentDescription,
154            Bitmap icon, UserHandleCompat user) {
155        this();
156        this.intent = intent;
157        this.title = Utilities.trim(title);
158        this.contentDescription = contentDescription;
159        mIcon = icon;
160        this.user = user;
161    }
162
163    public ShortcutInfo(Context context, ShortcutInfo info) {
164        super(info);
165        title = Utilities.trim(info.title);
166        intent = new Intent(info.intent);
167        if (info.iconResource != null) {
168            iconResource = new Intent.ShortcutIconResource();
169            iconResource.packageName = info.iconResource.packageName;
170            iconResource.resourceName = info.iconResource.resourceName;
171        }
172        mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
173        customIcon = info.customIcon;
174        flags = info.flags;
175        firstInstallTime = info.firstInstallTime;
176        user = info.user;
177        status = info.status;
178    }
179
180    /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
181    public ShortcutInfo(AppInfo info) {
182        super(info);
183        title = Utilities.trim(info.title);
184        intent = new Intent(info.intent);
185        customIcon = false;
186        flags = info.flags;
187        firstInstallTime = info.firstInstallTime;
188    }
189
190    public void setIcon(Bitmap b) {
191        mIcon = b;
192    }
193
194    public Bitmap getIcon(IconCache iconCache) {
195        if (mIcon == null) {
196            updateIcon(iconCache);
197        }
198        return mIcon;
199    }
200
201    public void updateIcon(IconCache iconCache, boolean useLowRes) {
202        if (itemType == Favorites.ITEM_TYPE_APPLICATION) {
203            iconCache.getTitleAndIcon(this, promisedIntent != null ? promisedIntent : intent, user,
204                    useLowRes);
205        }
206    }
207
208    public void updateIcon(IconCache iconCache) {
209        updateIcon(iconCache, shouldUseLowResIcon());
210    }
211
212    @Override
213    void onAddToDatabase(Context context, ContentValues values) {
214        super.onAddToDatabase(context, values);
215
216        String titleStr = title != null ? title.toString() : null;
217        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
218
219        String uri = promisedIntent != null ? promisedIntent.toUri(0)
220                : (intent != null ? intent.toUri(0) : null);
221        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
222        values.put(LauncherSettings.Favorites.RESTORED, status);
223
224        if (customIcon) {
225            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
226                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
227            writeBitmap(values, mIcon);
228        } else {
229            if (!usingFallbackIcon) {
230                writeBitmap(values, mIcon);
231            }
232            if (iconResource != null) {
233                values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
234                        LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
235                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
236                        iconResource.packageName);
237                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
238                        iconResource.resourceName);
239            }
240        }
241    }
242
243    @Override
244    public String toString() {
245        return "ShortcutInfo(title=" + title + "intent=" + intent + "id=" + this.id
246                + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
247                + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
248                + " dropPos=" + Arrays.toString(dropPos) + " user=" + user + ")";
249    }
250
251    public static void dumpShortcutInfoList(String tag, String label,
252            ArrayList<ShortcutInfo> list) {
253        Log.d(tag, label + " size=" + list.size());
254        for (ShortcutInfo info: list) {
255            Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
256                    + " customIcon=" + info.customIcon);
257        }
258    }
259
260    public ComponentName getTargetComponent() {
261        return promisedIntent != null ? promisedIntent.getComponent() : intent.getComponent();
262    }
263
264    public boolean hasStatusFlag(int flag) {
265        return (status & flag) != 0;
266    }
267
268
269    public final boolean isPromise() {
270        return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINTALL_ICON);
271    }
272
273    public int getInstallProgress() {
274        return mInstallProgress;
275    }
276
277    public void setInstallProgress(int progress) {
278        mInstallProgress = progress;
279        status |= FLAG_INSTALL_SESSION_ACTIVE;
280    }
281
282    public boolean shouldUseLowResIcon() {
283        return usingLowResIcon && container >= 0 && rank >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
284    }
285
286    public static ShortcutInfo fromActivityInfo(LauncherActivityInfoCompat info, Context context) {
287        final ShortcutInfo shortcut = new ShortcutInfo();
288        shortcut.user = info.getUser();
289        shortcut.title = Utilities.trim(info.getLabel());
290        shortcut.contentDescription = UserManagerCompat.getInstance(context)
291                .getBadgedLabelForUser(info.getLabel(), info.getUser());
292        shortcut.customIcon = false;
293        shortcut.intent = AppInfo.makeLaunchIntent(context, info, info.getUser());
294        shortcut.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
295        shortcut.flags = AppInfo.initFlags(info);
296        shortcut.firstInstallTime = info.getFirstInstallTime();
297        return shortcut;
298    }
299}
300
301