• 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      * The intent used to start the application.
72      */
73     public Intent intent;
74 
75     /**
76      * If isShortcut=true and customIcon=false, this contains a reference to the
77      * shortcut icon as an application's resource.
78      */
79     public Intent.ShortcutIconResource iconResource;
80 
81     /**
82      * A message to display when the user tries to start a disabled shortcut.
83      * This is currently only used for deep shortcuts.
84      */
85     public CharSequence disabledMessage;
86 
87     public int status;
88 
89     /**
90      * A set of person's Id associated with the WorkspaceItemInfo, this is only used if the item
91      * represents a deep shortcut.
92      */
93     @NonNull private String[] personKeys = Utilities.EMPTY_STRING_ARRAY;
94 
95 
WorkspaceItemInfo()96     public WorkspaceItemInfo() {
97         itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
98     }
99 
WorkspaceItemInfo(WorkspaceItemInfo info)100     public WorkspaceItemInfo(WorkspaceItemInfo info) {
101         super(info);
102         title = info.title;
103         intent = new Intent(info.intent);
104         iconResource = info.iconResource;
105         status = info.status;
106         personKeys = info.personKeys.clone();
107     }
108 
109     /** TODO: Remove this.  It's only called by ApplicationInfo.makeWorkspaceItem. */
WorkspaceItemInfo(AppInfo info)110     public WorkspaceItemInfo(AppInfo info) {
111         super(info);
112         title = Utilities.trim(info.title);
113         intent = new Intent(info.getIntent());
114     }
115 
116     /**
117      * Creates a {@link WorkspaceItemInfo} from a {@link ShortcutInfo}.
118      */
WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context)119     public WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context) {
120         user = shortcutInfo.getUserHandle();
121         itemType = Favorites.ITEM_TYPE_DEEP_SHORTCUT;
122         updateFromDeepShortcutInfo(shortcutInfo, context);
123     }
124 
125     @Override
onAddToDatabase(ContentWriter writer)126     public void onAddToDatabase(ContentWriter writer) {
127         super.onAddToDatabase(writer);
128         writer.put(Favorites.TITLE, title)
129                 .put(Favorites.INTENT, getIntent())
130                 .put(Favorites.RESTORED, status);
131 
132         if (!usingLowResIcon()) {
133             writer.putIcon(bitmap, user);
134         }
135         if (iconResource != null) {
136             writer.put(Favorites.ICON_PACKAGE, iconResource.packageName)
137                     .put(Favorites.ICON_RESOURCE, iconResource.resourceName);
138         }
139     }
140 
141     @Override
getIntent()142     public Intent getIntent() {
143         return intent;
144     }
145 
hasStatusFlag(int flag)146     public boolean hasStatusFlag(int flag) {
147         return (status & flag) != 0;
148     }
149 
150 
isPromise()151     public final boolean isPromise() {
152         return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINSTALL_ICON);
153     }
154 
hasPromiseIconUi()155     public boolean hasPromiseIconUi() {
156         return isPromise() && !hasStatusFlag(FLAG_SUPPORTS_WEB_UI);
157     }
158 
updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context)159     public void updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context) {
160         // {@link ShortcutInfo#getActivity} can change during an update. Recreate the intent
161         intent = ShortcutKey.makeIntent(shortcutInfo);
162         title = shortcutInfo.getShortLabel();
163 
164         CharSequence label = shortcutInfo.getLongLabel();
165         if (TextUtils.isEmpty(label)) {
166             label = shortcutInfo.getShortLabel();
167         }
168         contentDescription = context.getPackageManager().getUserBadgedLabel(label, user);
169         if (shortcutInfo.isEnabled()) {
170             runtimeStatusFlags &= ~FLAG_DISABLED_BY_PUBLISHER;
171         } else {
172             runtimeStatusFlags |= FLAG_DISABLED_BY_PUBLISHER;
173         }
174         disabledMessage = shortcutInfo.getDisabledMessage();
175 
176         Person[] persons = ApiWrapper.getPersons(shortcutInfo);
177         personKeys = persons.length == 0 ? Utilities.EMPTY_STRING_ARRAY
178             : Arrays.stream(persons).map(Person::getKey).sorted().toArray(String[]::new);
179     }
180 
181     /** Returns the WorkspaceItemInfo id associated with the deep shortcut. */
getDeepShortcutId()182     public String getDeepShortcutId() {
183         return itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
184                 ? getIntent().getStringExtra(ShortcutKey.EXTRA_SHORTCUT_ID) : null;
185     }
186 
187     @NonNull
getPersonKeys()188     public String[] getPersonKeys() {
189         return personKeys;
190     }
191 
192     @Override
getTargetComponent()193     public ComponentName getTargetComponent() {
194         ComponentName cn = super.getTargetComponent();
195         if (cn == null && (itemType == Favorites.ITEM_TYPE_SHORTCUT || hasStatusFlag(
196                 FLAG_SUPPORTS_WEB_UI | FLAG_AUTOINSTALL_ICON | FLAG_RESTORED_ICON))) {
197             // Legacy shortcuts and promise icons with web UI may not have a componentName but just
198             // a packageName. In that case create a empty componentName instead of adding additional
199             // check everywhere.
200             String pkg = intent.getPackage();
201             return pkg == null ? null : new ComponentName(pkg, IconCache.EMPTY_CLASS_NAME);
202         }
203         return cn;
204     }
205 
206     @Override
clone()207     public ItemInfoWithIcon clone() {
208         return new WorkspaceItemInfo(this);
209     }
210 }
211