• 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.model.data;
18 
19 import android.app.Person;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ShortcutInfo;
24 import android.text.TextUtils;
25 
26 import androidx.annotation.NonNull;
27 
28 import com.android.launcher3.LauncherSettings;
29 import com.android.launcher3.LauncherSettings.Favorites;
30 import com.android.launcher3.Utilities;
31 import com.android.launcher3.icons.IconCache;
32 import com.android.launcher3.shortcuts.ShortcutKey;
33 import com.android.launcher3.uioverrides.ApiWrapper;
34 import com.android.launcher3.util.ContentWriter;
35 
36 import java.util.Arrays;
37 
38 /**
39  * Represents a launchable icon on the workspaces and in folders.
40  */
41 public class WorkspaceItemInfo extends ItemInfoWithIcon {
42 
43     public static final int DEFAULT = 0;
44 
45     /**
46      * The shortcut was restored from a backup and it not ready to be used. This is automatically
47      * set during backup/restore
48      */
49     public static final int FLAG_RESTORED_ICON = 1;
50 
51     /**
52      * The icon was added as an auto-install app, and is not ready to be used. This flag can't
53      * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout
54      * parsing.
55      *
56      * OR this icon was added due to it being an active install session created by the user.
57      */
58     public static final int FLAG_AUTOINSTALL_ICON = 1 << 1;
59 
60     /**
61      * Indicates that the widget restore has started.
62      */
63     public static final int FLAG_RESTORE_STARTED = 1 << 2;
64 
65     /**
66      * Web UI supported.
67      */
68     public static final int FLAG_SUPPORTS_WEB_UI = 1 << 3;
69 
70     /**
71      *
72      */
73     public static final int FLAG_START_FOR_RESULT = 1 << 4;
74 
75     /**
76      * The intent used to start the application.
77      */
78     @NonNull
79     public Intent intent;
80 
81     /**
82      * If isShortcut=true and customIcon=false, this contains a reference to the
83      * shortcut icon as an application's resource.
84      */
85     public Intent.ShortcutIconResource iconResource;
86 
87     /**
88      * A message to display when the user tries to start a disabled shortcut.
89      * This is currently only used for deep shortcuts.
90      */
91     public CharSequence disabledMessage;
92 
93     public int status;
94 
95     /**
96      * A set of person's Id associated with the WorkspaceItemInfo, this is only used if the item
97      * represents a deep shortcut.
98      */
99     @NonNull private String[] personKeys = Utilities.EMPTY_STRING_ARRAY;
100 
101     public int options;
102 
103 
WorkspaceItemInfo()104     public WorkspaceItemInfo() {
105         itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
106     }
107 
WorkspaceItemInfo(WorkspaceItemInfo info)108     public WorkspaceItemInfo(WorkspaceItemInfo info) {
109         super(info);
110         title = info.title;
111         intent = new Intent(info.intent);
112         iconResource = info.iconResource;
113         status = info.status;
114         personKeys = info.personKeys.clone();
115     }
116 
117     /** TODO: Remove this.  It's only called by ApplicationInfo.makeWorkspaceItem. */
WorkspaceItemInfo(AppInfo info)118     public WorkspaceItemInfo(AppInfo info) {
119         super(info);
120         title = Utilities.trim(info.title);
121         intent = new Intent(info.getIntent());
122     }
123 
124     /**
125      * Creates a {@link WorkspaceItemInfo} from a {@link ShortcutInfo}.
126      */
WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context)127     public WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context) {
128         user = shortcutInfo.getUserHandle();
129         itemType = Favorites.ITEM_TYPE_DEEP_SHORTCUT;
130         updateFromDeepShortcutInfo(shortcutInfo, context);
131     }
132 
133     @Override
onAddToDatabase(@onNull ContentWriter writer)134     public void onAddToDatabase(@NonNull ContentWriter writer) {
135         super.onAddToDatabase(writer);
136         writer.put(Favorites.TITLE, title)
137                 .put(Favorites.INTENT, getIntent())
138                 .put(Favorites.OPTIONS, options)
139                 .put(Favorites.RESTORED, status);
140 
141         if (!usingLowResIcon()) {
142             writer.putIcon(bitmap, user);
143         }
144         if (iconResource != null) {
145             writer.put(Favorites.ICON_PACKAGE, iconResource.packageName)
146                     .put(Favorites.ICON_RESOURCE, iconResource.resourceName);
147         }
148     }
149 
150     @Override
151     @NonNull
getIntent()152     public Intent getIntent() {
153         return intent;
154     }
155 
hasStatusFlag(int flag)156     public boolean hasStatusFlag(int flag) {
157         return (status & flag) != 0;
158     }
159 
160 
isPromise()161     public final boolean isPromise() {
162         return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINSTALL_ICON);
163     }
164 
hasPromiseIconUi()165     public boolean hasPromiseIconUi() {
166         return isPromise() && !hasStatusFlag(FLAG_SUPPORTS_WEB_UI);
167     }
168 
updateFromDeepShortcutInfo(@onNull final ShortcutInfo shortcutInfo, @NonNull final Context context)169     public void updateFromDeepShortcutInfo(@NonNull final ShortcutInfo shortcutInfo,
170             @NonNull final Context context) {
171         // {@link ShortcutInfo#getActivity} can change during an update. Recreate the intent
172         intent = ShortcutKey.makeIntent(shortcutInfo);
173         title = shortcutInfo.getShortLabel();
174 
175         CharSequence label = shortcutInfo.getLongLabel();
176         if (TextUtils.isEmpty(label)) {
177             label = shortcutInfo.getShortLabel();
178         }
179         contentDescription = context.getPackageManager().getUserBadgedLabel(label, user);
180         if (shortcutInfo.isEnabled()) {
181             runtimeStatusFlags &= ~FLAG_DISABLED_BY_PUBLISHER;
182         } else {
183             runtimeStatusFlags |= FLAG_DISABLED_BY_PUBLISHER;
184         }
185         disabledMessage = shortcutInfo.getDisabledMessage();
186         if (Utilities.ATLEAST_P
187                 && shortcutInfo.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
188             runtimeStatusFlags |= FLAG_DISABLED_VERSION_LOWER;
189         } else {
190             runtimeStatusFlags &= ~FLAG_DISABLED_VERSION_LOWER;
191         }
192 
193         Person[] persons = ApiWrapper.getPersons(shortcutInfo);
194         personKeys = persons.length == 0 ? Utilities.EMPTY_STRING_ARRAY
195             : Arrays.stream(persons).map(Person::getKey).sorted().toArray(String[]::new);
196     }
197 
198     /**
199      * {@code true} if the shortcut is disabled due to its app being a lower version.
200      */
isDisabledVersionLower()201     public boolean isDisabledVersionLower() {
202         return (runtimeStatusFlags & FLAG_DISABLED_VERSION_LOWER) != 0;
203     }
204 
205     /** Returns the WorkspaceItemInfo id associated with the deep shortcut. */
getDeepShortcutId()206     public String getDeepShortcutId() {
207         return itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
208                 ? getIntent().getStringExtra(ShortcutKey.EXTRA_SHORTCUT_ID) : null;
209     }
210 
211     @NonNull
getPersonKeys()212     public String[] getPersonKeys() {
213         return personKeys;
214     }
215 
216     @Override
getTargetComponent()217     public ComponentName getTargetComponent() {
218         ComponentName cn = super.getTargetComponent();
219         if (cn == null && (itemType == Favorites.ITEM_TYPE_SHORTCUT || hasStatusFlag(
220                 FLAG_SUPPORTS_WEB_UI | FLAG_AUTOINSTALL_ICON | FLAG_RESTORED_ICON))) {
221             // Legacy shortcuts and promise icons with web UI may not have a componentName but just
222             // a packageName. In that case create a empty componentName instead of adding additional
223             // check everywhere.
224             String pkg = intent.getPackage();
225             return pkg == null ? null : new ComponentName(pkg, IconCache.EMPTY_CLASS_NAME);
226         }
227         return cn;
228     }
229 
230     @Override
clone()231     public WorkspaceItemInfo clone() {
232         return new WorkspaceItemInfo(this);
233     }
234 }
235