• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher3;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ShortcutInfo;
23 import android.text.TextUtils;
24 
25 import com.android.launcher3.LauncherSettings.Favorites;
26 import com.android.launcher3.icons.IconCache;
27 import com.android.launcher3.shortcuts.ShortcutKey;
28 import com.android.launcher3.util.ContentWriter;
29 
30 /**
31  * Represents a launchable icon on the workspaces and in folders.
32  */
33 public class WorkspaceItemInfo extends ItemInfoWithIcon {
34 
35     public static final int DEFAULT = 0;
36 
37     /**
38      * The shortcut was restored from a backup and it not ready to be used. This is automatically
39      * set during backup/restore
40      */
41     public static final int FLAG_RESTORED_ICON = 1;
42 
43     /**
44      * The icon was added as an auto-install app, and is not ready to be used. This flag can't
45      * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout
46      * parsing.
47      */
48     public static final int FLAG_AUTOINSTALL_ICON = 2; //0B10;
49 
50     /**
51      * The icon is being installed. If {@link #FLAG_RESTORED_ICON} or {@link #FLAG_AUTOINSTALL_ICON}
52      * is set, then the icon is either being installed or is in a broken state.
53      */
54     public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
55 
56     /**
57      * Indicates that the widget restore has started.
58      */
59     public static final int FLAG_RESTORE_STARTED = 8; //0B1000;
60 
61     /**
62      * Web UI supported.
63      */
64     public static final int FLAG_SUPPORTS_WEB_UI = 16; //0B10000;
65 
66     /**
67      * The intent used to start the application.
68      */
69     public Intent intent;
70 
71     /**
72      * If isShortcut=true and customIcon=false, this contains a reference to the
73      * shortcut icon as an application's resource.
74      */
75     public Intent.ShortcutIconResource iconResource;
76 
77     /**
78      * A message to display when the user tries to start a disabled shortcut.
79      * This is currently only used for deep shortcuts.
80      */
81     public CharSequence disabledMessage;
82 
83     public int status;
84 
85     /**
86      * The installation progress [0-100] of the package that this shortcut represents.
87      */
88     private int mInstallProgress;
89 
WorkspaceItemInfo()90     public WorkspaceItemInfo() {
91         itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
92     }
93 
WorkspaceItemInfo(WorkspaceItemInfo info)94     public WorkspaceItemInfo(WorkspaceItemInfo info) {
95         super(info);
96         title = info.title;
97         intent = new Intent(info.intent);
98         iconResource = info.iconResource;
99         status = info.status;
100         mInstallProgress = info.mInstallProgress;
101     }
102 
103     /** TODO: Remove this.  It's only called by ApplicationInfo.makeWorkspaceItem. */
WorkspaceItemInfo(AppInfo info)104     public WorkspaceItemInfo(AppInfo info) {
105         super(info);
106         title = Utilities.trim(info.title);
107         intent = new Intent(info.intent);
108     }
109 
110     /**
111      * Creates a {@link WorkspaceItemInfo} from a {@link ShortcutInfo}.
112      */
WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context)113     public WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context) {
114         user = shortcutInfo.getUserHandle();
115         itemType = Favorites.ITEM_TYPE_DEEP_SHORTCUT;
116         updateFromDeepShortcutInfo(shortcutInfo, context);
117     }
118 
119     @Override
onAddToDatabase(ContentWriter writer)120     public void onAddToDatabase(ContentWriter writer) {
121         super.onAddToDatabase(writer);
122         writer.put(Favorites.TITLE, title)
123                 .put(Favorites.INTENT, getIntent())
124                 .put(Favorites.RESTORED, status);
125 
126         if (!usingLowResIcon()) {
127             writer.putIcon(iconBitmap, user);
128         }
129         if (iconResource != null) {
130             writer.put(Favorites.ICON_PACKAGE, iconResource.packageName)
131                     .put(Favorites.ICON_RESOURCE, iconResource.resourceName);
132         }
133     }
134 
135     @Override
getIntent()136     public Intent getIntent() {
137         return intent;
138     }
139 
hasStatusFlag(int flag)140     public boolean hasStatusFlag(int flag) {
141         return (status & flag) != 0;
142     }
143 
144 
isPromise()145     public final boolean isPromise() {
146         return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINSTALL_ICON);
147     }
148 
hasPromiseIconUi()149     public boolean hasPromiseIconUi() {
150         return isPromise() && !hasStatusFlag(FLAG_SUPPORTS_WEB_UI);
151     }
152 
getInstallProgress()153     public int getInstallProgress() {
154         return mInstallProgress;
155     }
156 
setInstallProgress(int progress)157     public void setInstallProgress(int progress) {
158         mInstallProgress = progress;
159         status |= FLAG_INSTALL_SESSION_ACTIVE;
160     }
161 
updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context)162     public void updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context) {
163         // {@link ShortcutInfo#getActivity} can change during an update. Recreate the intent
164         intent = ShortcutKey.makeIntent(shortcutInfo);
165         title = shortcutInfo.getShortLabel();
166 
167         CharSequence label = shortcutInfo.getLongLabel();
168         if (TextUtils.isEmpty(label)) {
169             label = shortcutInfo.getShortLabel();
170         }
171         contentDescription = context.getPackageManager().getUserBadgedLabel(label, user);
172         if (shortcutInfo.isEnabled()) {
173             runtimeStatusFlags &= ~FLAG_DISABLED_BY_PUBLISHER;
174         } else {
175             runtimeStatusFlags |= FLAG_DISABLED_BY_PUBLISHER;
176         }
177         disabledMessage = shortcutInfo.getDisabledMessage();
178     }
179 
180     /** Returns the WorkspaceItemInfo id associated with the deep shortcut. */
getDeepShortcutId()181     public String getDeepShortcutId() {
182         return itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT ?
183                 getIntent().getStringExtra(ShortcutKey.EXTRA_SHORTCUT_ID) : null;
184     }
185 
186     @Override
getTargetComponent()187     public ComponentName getTargetComponent() {
188         ComponentName cn = super.getTargetComponent();
189         if (cn == null && (itemType == Favorites.ITEM_TYPE_SHORTCUT
190                 || hasStatusFlag(FLAG_SUPPORTS_WEB_UI))) {
191             // Legacy shortcuts and promise icons with web UI may not have a componentName but just
192             // a packageName. In that case create a dummy componentName instead of adding additional
193             // check everywhere.
194             String pkg = intent.getPackage();
195             return pkg == null ? null : new ComponentName(pkg, IconCache.EMPTY_CLASS_NAME);
196         }
197         return cn;
198     }
199 }
200