blob: 0274aee0a1681ea94c63896cba7dfa05f3c1fa7b [file] [log] [blame]
Makoto Onuki31459242016-03-22 11:12:18 -07001/*
2 * Copyright (C) 2016 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 */
16package com.android.server.pm;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
Makoto Onukiabe84422016-04-07 09:41:19 -070020import android.annotation.UserIdInt;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080021import android.app.Person;
Makoto Onuki31459242016-03-22 11:12:18 -070022import android.content.ComponentName;
23import android.content.Intent;
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -080024import android.content.IntentFilter;
Felipe Leme90205ef2019-03-05 09:59:52 -080025import android.content.LocusId;
Makoto Onuki22fcc682016-05-17 14:52:19 -070026import android.content.pm.PackageInfo;
Makoto Onuki31459242016-03-22 11:12:18 -070027import android.content.pm.ShortcutInfo;
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -080028import android.content.pm.ShortcutManager;
Makoto Onuki157b1622016-06-02 16:13:10 -070029import android.content.res.Resources;
Makoto Onuki31459242016-03-22 11:12:18 -070030import android.os.PersistableBundle;
31import android.text.format.Formatter;
32import android.util.ArrayMap;
33import android.util.ArraySet;
Makoto Onuki7001a612016-05-27 13:24:28 -070034import android.util.Log;
Makoto Onuki31459242016-03-22 11:12:18 -070035import android.util.Slog;
36
Makoto Onuki2e210c42016-03-30 08:30:36 -070037import com.android.internal.annotations.VisibleForTesting;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080038import com.android.internal.util.ArrayUtils;
Makoto Onuki22fcc682016-05-17 14:52:19 -070039import com.android.internal.util.Preconditions;
Makoto Onukib6d35232016-04-04 15:57:17 -070040import com.android.internal.util.XmlUtils;
Makoto Onuki20b82212017-10-04 15:03:50 -070041import com.android.server.pm.ShortcutService.DumpFilter;
Makoto Onuki7001a612016-05-27 13:24:28 -070042import com.android.server.pm.ShortcutService.ShortcutOperation;
Makoto Onuki4e6cef42016-07-13 16:14:01 -070043import com.android.server.pm.ShortcutService.Stats;
Makoto Onuki2e210c42016-03-30 08:30:36 -070044
Makoto Onuki76269922016-07-15 14:58:54 -070045import org.json.JSONException;
46import org.json.JSONObject;
Makoto Onuki31459242016-03-22 11:12:18 -070047import org.xmlpull.v1.XmlPullParser;
48import org.xmlpull.v1.XmlPullParserException;
49import org.xmlpull.v1.XmlSerializer;
50
51import java.io.File;
52import java.io.IOException;
53import java.io.PrintWriter;
54import java.util.ArrayList;
Makoto Onuki7001a612016-05-27 13:24:28 -070055import java.util.Collections;
56import java.util.Comparator;
Makoto Onuki31459242016-03-22 11:12:18 -070057import java.util.List;
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +000058import java.util.Objects;
Makoto Onukibe73a802016-04-15 14:46:35 -070059import java.util.Set;
Makoto Onuki31459242016-03-22 11:12:18 -070060import java.util.function.Predicate;
61
62/**
63 * Package information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070064 * User information used by {@link ShortcutService}.
65 *
66 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070067 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070068class ShortcutPackage extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070069 private static final String TAG = ShortcutService.TAG;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070070 private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
Makoto Onuki31459242016-03-22 11:12:18 -070071
72 static final String TAG_ROOT = "package";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070073 private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras";
74 private static final String TAG_INTENT = "intent";
Makoto Onuki31459242016-03-22 11:12:18 -070075 private static final String TAG_EXTRAS = "extras";
76 private static final String TAG_SHORTCUT = "shortcut";
Mehdi Alizadehabec3192019-06-27 18:06:15 -070077 private static final String TAG_SHARE_TARGET = "share-target";
Makoto Onukib6d35232016-04-04 15:57:17 -070078 private static final String TAG_CATEGORIES = "categories";
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080079 private static final String TAG_PERSON = "person";
Makoto Onuki31459242016-03-22 11:12:18 -070080
81 private static final String ATTR_NAME = "name";
Makoto Onuki31459242016-03-22 11:12:18 -070082 private static final String ATTR_CALL_COUNT = "call-count";
83 private static final String ATTR_LAST_RESET = "last-reset";
84 private static final String ATTR_ID = "id";
85 private static final String ATTR_ACTIVITY = "activity";
86 private static final String ATTR_TITLE = "title";
Makoto Onuki20c95f82016-05-11 16:51:01 -070087 private static final String ATTR_TITLE_RES_ID = "titleid";
Makoto Onuki157b1622016-06-02 16:13:10 -070088 private static final String ATTR_TITLE_RES_NAME = "titlename";
Makoto Onukie3ae7ec2016-03-29 15:45:25 -070089 private static final String ATTR_TEXT = "text";
Makoto Onuki20c95f82016-05-11 16:51:01 -070090 private static final String ATTR_TEXT_RES_ID = "textid";
Makoto Onuki157b1622016-06-02 16:13:10 -070091 private static final String ATTR_TEXT_RES_NAME = "textname";
Makoto Onuki20c95f82016-05-11 16:51:01 -070092 private static final String ATTR_DISABLED_MESSAGE = "dmessage";
93 private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
Makoto Onuki157b1622016-06-02 16:13:10 -070094 private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
Makoto Onukia4f89b12017-10-05 10:37:55 -070095 private static final String ATTR_DISABLED_REASON = "disabled-reason";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070096 private static final String ATTR_INTENT_LEGACY = "intent";
97 private static final String ATTR_INTENT_NO_EXTRA = "intent-base";
Makoto Onuki20c95f82016-05-11 16:51:01 -070098 private static final String ATTR_RANK = "rank";
Makoto Onuki31459242016-03-22 11:12:18 -070099 private static final String ATTR_TIMESTAMP = "timestamp";
100 private static final String ATTR_FLAGS = "flags";
Makoto Onuki157b1622016-06-02 16:13:10 -0700101 private static final String ATTR_ICON_RES_ID = "icon-res";
102 private static final String ATTR_ICON_RES_NAME = "icon-resname";
Makoto Onuki31459242016-03-22 11:12:18 -0700103 private static final String ATTR_BITMAP_PATH = "bitmap-path";
Adam Hed4586b12019-03-19 12:01:00 -0700104 private static final String ATTR_LOCUS_ID = "locus-id";
Makoto Onuki31459242016-03-22 11:12:18 -0700105
Mehdi Alizadehebb4b602019-02-05 15:52:18 -0800106 private static final String ATTR_PERSON_NAME = "name";
107 private static final String ATTR_PERSON_URI = "uri";
108 private static final String ATTR_PERSON_KEY = "key";
109 private static final String ATTR_PERSON_IS_BOT = "is-bot";
110 private static final String ATTR_PERSON_IS_IMPORTANT = "is-important";
111
Makoto Onukib6d35232016-04-04 15:57:17 -0700112 private static final String NAME_CATEGORIES = "categories";
113
114 private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
115 private static final String ATTR_NAME_XMLUTILS = "name";
116
Makoto Onuki76269922016-07-15 14:58:54 -0700117 private static final String KEY_DYNAMIC = "dynamic";
118 private static final String KEY_MANIFEST = "manifest";
119 private static final String KEY_PINNED = "pinned";
120 private static final String KEY_BITMAPS = "bitmaps";
121 private static final String KEY_BITMAP_BYTES = "bitmapBytes";
122
Makoto Onuki31459242016-03-22 11:12:18 -0700123 /**
124 * All the shortcuts from the package, keyed on IDs.
125 */
126 final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
127
128 /**
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800129 * All the share targets from the package
130 */
131 private final ArrayList<ShareTargetInfo> mShareTargets = new ArrayList<>(0);
132
133 /**
Makoto Onuki31459242016-03-22 11:12:18 -0700134 * # of times the package has called rate-limited APIs.
135 */
136 private int mApiCallCount;
137
138 /**
139 * When {@link #mApiCallCount} was reset last time.
140 */
141 private long mLastResetTime;
142
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700143 private final int mPackageUid;
144
145 private long mLastKnownForegroundElapsedTime;
146
Makoto Onukic51b2872016-05-04 15:24:50 -0700147 private ShortcutPackage(ShortcutUser shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700148 int packageUserId, String packageName, ShortcutPackageInfo spi) {
149 super(shortcutUser, packageUserId, packageName,
150 spi != null ? spi : ShortcutPackageInfo.newEmpty());
151
Makoto Onukic51b2872016-05-04 15:24:50 -0700152 mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700153 }
154
Makoto Onukic51b2872016-05-04 15:24:50 -0700155 public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
156 this(shortcutUser, packageUserId, packageName, null);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700157 }
158
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700159 @Override
160 public int getOwnerUserId() {
161 // For packages, always owner user == package user.
162 return getPackageUserId();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700163 }
164
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700165 public int getPackageUid() {
166 return mPackageUid;
167 }
168
Makoto Onuki157b1622016-06-02 16:13:10 -0700169 @Nullable
170 public Resources getPackageResources() {
171 return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
172 getPackageName(), getPackageUserId());
173 }
174
Makoto Onuki50a320e2017-05-31 14:38:42 -0700175 public int getShortcutCount() {
176 return mShortcuts.size();
177 }
178
Makoto Onuki2e210c42016-03-30 08:30:36 -0700179 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700180 protected boolean canRestoreAnyVersion() {
181 return false;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700182 }
183
184 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700185 protected void onRestored(int restoreBlockReason) {
186 // Shortcuts have been restored.
187 // - Unshadow all shortcuts.
188 // - Set disabled reason.
189 // - Disable if needed.
190 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
191 ShortcutInfo si = mShortcuts.valueAt(i);
192 si.clearFlags(ShortcutInfo.FLAG_SHADOW);
193
194 si.setDisabledReason(restoreBlockReason);
195 if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
196 si.addFlags(ShortcutInfo.FLAG_DISABLED);
197 }
198 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700199 // Because some launchers may not have been restored (e.g. allowBackup=false),
200 // we need to re-calculate the pinned shortcuts.
Makoto Onukic51b2872016-05-04 15:24:50 -0700201 refreshPinnedFlags();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700202 }
203
Makoto Onukid99c6f02016-03-28 11:02:54 -0700204 /**
205 * Note this does *not* provide a correct view to the calling launcher.
206 */
Makoto Onuki31459242016-03-22 11:12:18 -0700207 @Nullable
208 public ShortcutInfo findShortcutById(String id) {
209 return mShortcuts.get(id);
210 }
211
Makoto Onukia4f89b12017-10-05 10:37:55 -0700212 public boolean isShortcutExistsAndInvisibleToPublisher(String id) {
213 ShortcutInfo si = findShortcutById(id);
214 return si != null && !si.isVisibleToPublisher();
215 }
216
217 public boolean isShortcutExistsAndVisibleToPublisher(String id) {
218 ShortcutInfo si = findShortcutById(id);
219 return si != null && si.isVisibleToPublisher();
220 }
221
222 private void ensureNotImmutable(@Nullable ShortcutInfo shortcut, boolean ignoreInvisible) {
223 if (shortcut != null && shortcut.isImmutable()
224 && (!ignoreInvisible || shortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700225 throw new IllegalArgumentException(
226 "Manifest shortcut ID=" + shortcut.getId()
227 + " may not be manipulated via APIs");
228 }
229 }
230
Makoto Onukia4f89b12017-10-05 10:37:55 -0700231 public void ensureNotImmutable(@NonNull String id, boolean ignoreInvisible) {
232 ensureNotImmutable(mShortcuts.get(id), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700233 }
234
Makoto Onukia4f89b12017-10-05 10:37:55 -0700235 public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds,
236 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700237 for (int i = shortcutIds.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700238 ensureNotImmutable(shortcutIds.get(i), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700239 }
240 }
241
Makoto Onukia4f89b12017-10-05 10:37:55 -0700242 public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts,
243 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700244 for (int i = shortcuts.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700245 ensureNotImmutable(shortcuts.get(i).getId(), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700246 }
247 }
248
Makoto Onukia4f89b12017-10-05 10:37:55 -0700249 /**
250 * Delete a shortcut by ID. This will *always* remove it even if it's immutable or invisible.
251 */
252 private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) {
Makoto Onuki31459242016-03-22 11:12:18 -0700253 final ShortcutInfo shortcut = mShortcuts.remove(id);
254 if (shortcut != null) {
Makoto Onuki475c3652017-05-08 14:29:03 -0700255 mShortcutUser.mService.removeIconLocked(shortcut);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700256 shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
Makoto Onuki99302b52017-03-29 12:42:26 -0700257 | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki31459242016-03-22 11:12:18 -0700258 }
259 return shortcut;
260 }
261
Makoto Onukia4f89b12017-10-05 10:37:55 -0700262 /**
263 * Force replace a shortcut. If there's already a shortcut with the same ID, it'll be removed,
264 * even if it's invisible.
265 */
266 private void forceReplaceShortcutInner(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700267 final ShortcutService s = mShortcutUser.mService;
268
Makoto Onukia4f89b12017-10-05 10:37:55 -0700269 forceDeleteShortcutInner(newShortcut.getId());
Makoto Onuki157b1622016-06-02 16:13:10 -0700270
271 // Extract Icon and update the icon res ID and the bitmap path.
Makoto Onuki475c3652017-05-08 14:29:03 -0700272 s.saveIconAndFixUpShortcutLocked(newShortcut);
Makoto Onuki157b1622016-06-02 16:13:10 -0700273 s.fixUpShortcutResourceNamesAndValues(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700274 mShortcuts.put(newShortcut.getId(), newShortcut);
275 }
276
277 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700278 * Add a shortcut. If there's already a one with the same ID, it'll be removed, even if it's
279 * invisible.
Makoto Onuki31459242016-03-22 11:12:18 -0700280 *
281 * It checks the max number of dynamic shortcuts.
282 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700283 public void addOrReplaceDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki39686e82016-04-13 18:03:00 -0700284
Makoto Onuki22fcc682016-05-17 14:52:19 -0700285 Preconditions.checkArgument(newShortcut.isEnabled(),
286 "add/setDynamicShortcuts() cannot publish disabled shortcuts");
287
Makoto Onuki99302b52017-03-29 12:42:26 -0700288 newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki31459242016-03-22 11:12:18 -0700289
290 final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
291
292 final boolean wasPinned;
Makoto Onuki31459242016-03-22 11:12:18 -0700293
294 if (oldShortcut == null) {
295 wasPinned = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700296 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700297 // It's an update case.
298 // Make sure the target is updatable. (i.e. should be mutable.)
Makoto Onukia4f89b12017-10-05 10:37:55 -0700299 oldShortcut.ensureUpdatableWith(newShortcut, /*isUpdating=*/ false);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700300
Makoto Onuki31459242016-03-22 11:12:18 -0700301 wasPinned = oldShortcut.isPinned();
Makoto Onuki31459242016-03-22 11:12:18 -0700302 }
303
Makoto Onuki157b1622016-06-02 16:13:10 -0700304 // If it was originally pinned, the new one should be pinned too.
Makoto Onuki31459242016-03-22 11:12:18 -0700305 if (wasPinned) {
306 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
307 }
308
Makoto Onukia4f89b12017-10-05 10:37:55 -0700309 forceReplaceShortcutInner(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700310 }
311
312 /**
313 * Remove all shortcuts that aren't pinned nor dynamic.
314 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700315 private void removeOrphans() {
Makoto Onuki31459242016-03-22 11:12:18 -0700316 ArrayList<String> removeList = null; // Lazily initialize.
317
318 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
319 final ShortcutInfo si = mShortcuts.valueAt(i);
320
Makoto Onuki22fcc682016-05-17 14:52:19 -0700321 if (si.isAlive()) continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700322
323 if (removeList == null) {
324 removeList = new ArrayList<>();
325 }
326 removeList.add(si.getId());
327 }
328 if (removeList != null) {
329 for (int i = removeList.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700330 forceDeleteShortcutInner(removeList.get(i));
Makoto Onuki31459242016-03-22 11:12:18 -0700331 }
332 }
333 }
334
335 /**
336 * Remove all dynamic shortcuts.
337 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700338 public void deleteAllDynamicShortcuts(boolean ignoreInvisible) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700339 final long now = mShortcutUser.mService.injectCurrentTimeMillis();
340
Makoto Onuki22fcc682016-05-17 14:52:19 -0700341 boolean changed = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700342 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700343 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700344 if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700345 changed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700346
347 si.setTimestamp(now);
Makoto Onuki99302b52017-03-29 12:42:26 -0700348 si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700349 si.setRank(0); // It may still be pinned, so clear the rank.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700350 }
Makoto Onuki31459242016-03-22 11:12:18 -0700351 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700352 if (changed) {
353 removeOrphans();
354 }
Makoto Onuki31459242016-03-22 11:12:18 -0700355 }
356
357 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700358 * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
359 * is pinned, it'll remain as a pinned shortcut, and is still enabled.
Makoto Onukib08790c2016-06-23 14:05:46 -0700360 *
361 * @return true if it's actually removed because it wasn't pinned, or false if it's still
362 * pinned.
Makoto Onuki31459242016-03-22 11:12:18 -0700363 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700364 public boolean deleteDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700365 final ShortcutInfo removed = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700366 shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false, ignoreInvisible,
367 ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukib08790c2016-06-23 14:05:46 -0700368 return removed == null;
369 }
370
371 /**
372 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
373 * is pinned, it'll remain as a pinned shortcut, but will be disabled.
374 *
375 * @return true if it's actually removed because it wasn't pinned, or false if it's still
376 * pinned.
377 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700378 private boolean disableDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible,
379 int disabledReason) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700380 final ShortcutInfo disabled = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700381 shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false, ignoreInvisible,
382 disabledReason);
Makoto Onukib08790c2016-06-23 14:05:46 -0700383 return disabled == null;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700384 }
385
Makoto Onuki7001a612016-05-27 13:24:28 -0700386 /**
387 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
388 * is pinned, it'll remain as a pinned shortcut but will be disabled.
389 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700390 public void disableWithId(@NonNull String shortcutId, String disabledMessage,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700391 int disabledMessageResId, boolean overrideImmutable, boolean ignoreInvisible,
392 int disabledReason) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700393 final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700394 overrideImmutable, ignoreInvisible, disabledReason);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700395
396 if (disabled != null) {
397 if (disabledMessage != null) {
398 disabled.setDisabledMessage(disabledMessage);
399 } else if (disabledMessageResId != 0) {
400 disabled.setDisabledMessageResId(disabledMessageResId);
Makoto Onuki157b1622016-06-02 16:13:10 -0700401
402 mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700403 }
404 }
405 }
406
407 @Nullable
408 private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700409 boolean overrideImmutable, boolean ignoreInvisible, int disabledReason) {
410 Preconditions.checkState(
411 (disable == (disabledReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED)),
412 "disable and disabledReason disagree: " + disable + " vs " + disabledReason);
Makoto Onuki31459242016-03-22 11:12:18 -0700413 final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
414
Makoto Onukia4f89b12017-10-05 10:37:55 -0700415 if (oldShortcut == null || !oldShortcut.isEnabled()
416 && (ignoreInvisible && !oldShortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700417 return null; // Doesn't exist or already disabled.
Makoto Onuki31459242016-03-22 11:12:18 -0700418 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700419 if (!overrideImmutable) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700420 ensureNotImmutable(oldShortcut, /*ignoreInvisible=*/ true);
Makoto Onuki31459242016-03-22 11:12:18 -0700421 }
422 if (oldShortcut.isPinned()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700423
424 oldShortcut.setRank(0);
Makoto Onuki99302b52017-03-29 12:42:26 -0700425 oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700426 if (disable) {
427 oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700428 // Do not overwrite the disabled reason if one is alreay set.
429 if (oldShortcut.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
430 oldShortcut.setDisabledReason(disabledReason);
431 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700432 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700433 oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
434
Makoto Onuki255461f2017-01-10 11:47:25 -0800435 // See ShortcutRequestPinProcessor.directPinShortcut().
436 if (mShortcutUser.mService.isDummyMainActivity(oldShortcut.getActivity())) {
437 oldShortcut.setActivity(null);
438 }
439
Makoto Onuki22fcc682016-05-17 14:52:19 -0700440 return oldShortcut;
Makoto Onuki31459242016-03-22 11:12:18 -0700441 } else {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700442 forceDeleteShortcutInner(shortcutId);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700443 return null;
444 }
445 }
446
447 public void enableWithId(@NonNull String shortcutId) {
448 final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
449 if (shortcut != null) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700450 ensureNotImmutable(shortcut, /*ignoreInvisible=*/ true);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700451 shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700452 shortcut.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onuki31459242016-03-22 11:12:18 -0700453 }
454 }
455
Makoto Onukia4f89b12017-10-05 10:37:55 -0700456 public void updateInvisibleShortcutForPinRequestWith(@NonNull ShortcutInfo shortcut) {
457 final ShortcutInfo source = mShortcuts.get(shortcut.getId());
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +0000458 Objects.requireNonNull(source);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700459
460 mShortcutUser.mService.validateShortcutForPinRequest(shortcut);
461
462 shortcut.addFlags(ShortcutInfo.FLAG_PINNED);
463
464 forceReplaceShortcutInner(shortcut);
465
466 adjustRanks();
467 }
468
Makoto Onuki31459242016-03-22 11:12:18 -0700469 /**
470 * Called after a launcher updates the pinned set. For each shortcut in this package,
471 * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it.
472 *
473 * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
474 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700475 public void refreshPinnedFlags() {
Makoto Onuki31459242016-03-22 11:12:18 -0700476 // First, un-pin all shortcuts
477 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
478 mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
479 }
480
481 // Then, for the pinned set for each launcher, set the pin flag one by one.
Makoto Onuki47ed1712017-12-20 14:40:20 +0900482 mShortcutUser.forAllLaunchers(launcherShortcuts -> {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700483 final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
Makoto Onuki2e210c42016-03-30 08:30:36 -0700484 getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700485
486 if (pinned == null || pinned.size() == 0) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700487 return;
Makoto Onuki31459242016-03-22 11:12:18 -0700488 }
489 for (int i = pinned.size() - 1; i >= 0; i--) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700490 final String id = pinned.valueAt(i);
491 final ShortcutInfo si = mShortcuts.get(id);
Makoto Onuki31459242016-03-22 11:12:18 -0700492 if (si == null) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700493 // This happens if a launcher pinned shortcuts from this package, then backup&
494 // restored, but this package doesn't allow backing up.
495 // In that case the launcher ends up having a dangling pinned shortcuts.
496 // That's fine, when the launcher is restored, we'll fix it.
497 continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700498 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700499 si.addFlags(ShortcutInfo.FLAG_PINNED);
Makoto Onuki31459242016-03-22 11:12:18 -0700500 }
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700501 });
Makoto Onuki31459242016-03-22 11:12:18 -0700502
503 // Lastly, remove the ones that are no longer pinned nor dynamic.
Makoto Onukic51b2872016-05-04 15:24:50 -0700504 removeOrphans();
Makoto Onuki31459242016-03-22 11:12:18 -0700505 }
506
507 /**
508 * Number of calls that the caller has made, since the last reset.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700509 *
510 * <p>This takes care of the resetting the counter for foreground apps as well as after
511 * locale changes.
Makoto Onuki31459242016-03-22 11:12:18 -0700512 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800513 public int getApiCallCount(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700514 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700515
516 // Reset the counter if:
517 // - the package is in foreground now.
518 // - the package is *not* in foreground now, but was in foreground at some point
519 // since the previous time it had been.
520 if (s.isUidForegroundLocked(mPackageUid)
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800521 || (mLastKnownForegroundElapsedTime
522 < s.getUidLastForegroundElapsedTimeLocked(mPackageUid))
523 || unlimited) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700524 mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
Makoto Onukic51b2872016-05-04 15:24:50 -0700525 resetRateLimiting();
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700526 }
527
528 // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
529 // but we just can't return 0 at this point, because we may have to update
530 // mLastResetTime.
531
Makoto Onuki31459242016-03-22 11:12:18 -0700532 final long last = s.getLastResetTimeLocked();
533
534 final long now = s.injectCurrentTimeMillis();
535 if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
536 Slog.w(TAG, "Clock rewound");
537 // Clock rewound.
538 mLastResetTime = now;
539 mApiCallCount = 0;
540 return mApiCallCount;
541 }
542
543 // If not reset yet, then reset.
544 if (mLastResetTime < last) {
545 if (ShortcutService.DEBUG) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700546 Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
547 getPackageName(), mLastResetTime, now, last));
Makoto Onuki31459242016-03-22 11:12:18 -0700548 }
549 mApiCallCount = 0;
550 mLastResetTime = last;
551 }
552 return mApiCallCount;
553 }
554
555 /**
556 * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
557 * and return true. Otherwise just return false.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700558 *
559 * <p>This takes care of the resetting the counter for foreground apps as well as after
560 * locale changes, which is done internally by {@link #getApiCallCount}.
Makoto Onuki31459242016-03-22 11:12:18 -0700561 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800562 public boolean tryApiCall(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700563 final ShortcutService s = mShortcutUser.mService;
564
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800565 if (getApiCallCount(unlimited) >= s.mMaxUpdatesPerInterval) {
Makoto Onuki31459242016-03-22 11:12:18 -0700566 return false;
567 }
568 mApiCallCount++;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700569 s.scheduleSaveUser(getOwnerUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700570 return true;
571 }
572
Makoto Onukic51b2872016-05-04 15:24:50 -0700573 public void resetRateLimiting() {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700574 if (ShortcutService.DEBUG) {
575 Slog.d(TAG, "resetRateLimiting: " + getPackageName());
576 }
577 if (mApiCallCount > 0) {
578 mApiCallCount = 0;
Makoto Onukic51b2872016-05-04 15:24:50 -0700579 mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700580 }
581 }
582
583 public void resetRateLimitingForCommandLineNoSaving() {
Makoto Onuki31459242016-03-22 11:12:18 -0700584 mApiCallCount = 0;
585 mLastResetTime = 0;
586 }
587
588 /**
589 * Find all shortcuts that match {@code query}.
590 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700591 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700592 @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700593 findAll(result, query, cloneFlag, null, 0, /*getPinnedByAnyLauncher=*/ false);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700594 }
595
596 /**
597 * Find all shortcuts that match {@code query}.
598 *
599 * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
600 * by the calling launcher will not be included in the result, and also "isPinned" will be
601 * adjusted for the caller too.
602 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700603 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onuki31459242016-03-22 11:12:18 -0700604 @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
Makoto Onuki634cecb2017-10-13 17:10:48 -0700605 @Nullable String callingLauncher, int launcherUserId, boolean getPinnedByAnyLauncher) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700606 if (getPackageInfo().isShadow()) {
607 // Restored and the app not installed yet, so don't return any.
608 return;
609 }
Makoto Onuki31459242016-03-22 11:12:18 -0700610
Makoto Onukic51b2872016-05-04 15:24:50 -0700611 final ShortcutService s = mShortcutUser.mService;
612
Makoto Onuki31459242016-03-22 11:12:18 -0700613 // Set of pinned shortcuts by the calling launcher.
614 final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
Makoto Onuki2e210c42016-03-30 08:30:36 -0700615 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
616 .getPinnedShortcutIds(getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700617
618 for (int i = 0; i < mShortcuts.size(); i++) {
619 final ShortcutInfo si = mShortcuts.valueAt(i);
620
Makoto Onuki22fcc682016-05-17 14:52:19 -0700621 // Need to adjust PINNED flag depending on the caller.
622 // Basically if the caller is a launcher (callingLauncher != null) and the launcher
623 // isn't pinning it, then we need to clear PINNED for this caller.
Makoto Onuki31459242016-03-22 11:12:18 -0700624 final boolean isPinnedByCaller = (callingLauncher == null)
625 || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700626
Makoto Onuki634cecb2017-10-13 17:10:48 -0700627 if (!getPinnedByAnyLauncher) {
628 if (si.isFloating()) {
629 if (!isPinnedByCaller) {
630 continue;
631 }
Makoto Onuki31459242016-03-22 11:12:18 -0700632 }
633 }
634 final ShortcutInfo clone = si.clone(cloneFlag);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700635
Makoto Onuki31459242016-03-22 11:12:18 -0700636 // Fix up isPinned for the caller. Note we need to do it before the "test" callback,
637 // since it may check isPinned.
Makoto Onuki35559d62017-11-06 16:26:32 -0800638 // However, if getPinnedByAnyLauncher is set, we do it after the test.
639 if (!getPinnedByAnyLauncher) {
640 if (!isPinnedByCaller) {
641 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
642 }
Makoto Onuki31459242016-03-22 11:12:18 -0700643 }
644 if (query == null || query.test(clone)) {
Makoto Onuki35559d62017-11-06 16:26:32 -0800645 if (!isPinnedByCaller) {
646 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
647 }
Makoto Onuki31459242016-03-22 11:12:18 -0700648 result.add(clone);
649 }
650 }
651 }
652
653 public void resetThrottling() {
654 mApiCallCount = 0;
655 }
656
Makoto Onuki39686e82016-04-13 18:03:00 -0700657 /**
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800658 * Returns a list of ShortcutInfos that match the given intent filter and the category of
659 * available ShareTarget definitions in this package.
660 */
661 public List<ShortcutManager.ShareShortcutInfo> getMatchingShareTargets(
662 @NonNull IntentFilter filter) {
663 final List<ShareTargetInfo> matchedTargets = new ArrayList<>();
664 for (int i = 0; i < mShareTargets.size(); i++) {
665 final ShareTargetInfo target = mShareTargets.get(i);
666 for (ShareTargetInfo.TargetData data : target.mTargetData) {
667 if (filter.hasDataType(data.mMimeType)) {
668 // Matched at least with one data type
669 matchedTargets.add(target);
670 break;
671 }
672 }
673 }
674
675 if (matchedTargets.isEmpty()) {
676 return new ArrayList<>();
677 }
678
Mehdi Alizadehc6096022019-05-27 12:17:36 -0700679 // Get the list of all dynamic shortcuts in this package.
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800680 final ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
Mehdi Alizadehc6096022019-05-27 12:17:36 -0700681 findAll(shortcuts, ShortcutInfo::isDynamicVisible,
682 ShortcutInfo.CLONE_REMOVE_FOR_APP_PREDICTION);
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800683
684 final List<ShortcutManager.ShareShortcutInfo> result = new ArrayList<>();
685 for (int i = 0; i < shortcuts.size(); i++) {
Mehdi Alizadeh97fb3ed2019-04-25 14:52:02 -0700686 final Set<String> categories = shortcuts.get(i).getCategories();
687 if (categories == null || categories.isEmpty()) {
688 continue;
689 }
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800690 for (int j = 0; j < matchedTargets.size(); j++) {
691 // Shortcut must have all of share target categories
692 boolean hasAllCategories = true;
693 final ShareTargetInfo target = matchedTargets.get(j);
694 for (int q = 0; q < target.mCategories.length; q++) {
Mehdi Alizadeh97fb3ed2019-04-25 14:52:02 -0700695 if (!categories.contains(target.mCategories[q])) {
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800696 hasAllCategories = false;
697 break;
698 }
699 }
700 if (hasAllCategories) {
Mehdi Alizadeh97fb3ed2019-04-25 14:52:02 -0700701 result.add(new ShortcutManager.ShareShortcutInfo(shortcuts.get(i),
702 new ComponentName(getPackageName(), target.mTargetClass)));
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800703 break;
704 }
705 }
706 }
707 return result;
708 }
709
Mehdi Alizadeh85fd3d52019-01-23 12:49:53 -0800710 public boolean hasShareTargets() {
711 return !mShareTargets.isEmpty();
712 }
713
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800714 /**
Mehdi Alizadeh97fb3ed2019-04-25 14:52:02 -0700715 * Returns the number of shortcuts that can be used as a share target in the ShareSheet. Such
716 * shortcuts must have a matching category with at least one of the defined ShareTargets from
717 * the app's Xml resource.
718 */
719 int getSharingShortcutCount() {
720 if (mShortcuts.isEmpty() || mShareTargets.isEmpty()) {
721 return 0;
722 }
723
724 // Get the list of all dynamic shortcuts in this package
725 final ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
726 findAll(shortcuts, ShortcutInfo::isDynamicVisible, ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
727
728 int sharingShortcutCount = 0;
729 for (int i = 0; i < shortcuts.size(); i++) {
730 final Set<String> categories = shortcuts.get(i).getCategories();
731 if (categories == null || categories.isEmpty()) {
732 continue;
733 }
734 for (int j = 0; j < mShareTargets.size(); j++) {
735 // A SharingShortcut must have all of share target categories
736 boolean hasAllCategories = true;
737 final ShareTargetInfo target = mShareTargets.get(j);
738 for (int q = 0; q < target.mCategories.length; q++) {
739 if (!categories.contains(target.mCategories[q])) {
740 hasAllCategories = false;
741 break;
742 }
743 }
744 if (hasAllCategories) {
745 sharingShortcutCount++;
746 break;
747 }
748 }
749 }
750 return sharingShortcutCount;
751 }
752
753 /**
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700754 * Return the filenames (excluding path names) of icon bitmap files from this package.
755 */
756 public ArraySet<String> getUsedBitmapFiles() {
757 final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
758
759 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
760 final ShortcutInfo si = mShortcuts.valueAt(i);
761 if (si.getBitmapPath() != null) {
762 usedFiles.add(getFileName(si.getBitmapPath()));
763 }
764 }
765 return usedFiles;
766 }
767
768 private static String getFileName(@NonNull String path) {
769 final int sep = path.lastIndexOf(File.separatorChar);
770 if (sep == -1) {
771 return path;
772 } else {
773 return path.substring(sep + 1);
774 }
775 }
776
777 /**
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700778 * @return false if any of the target activities are no longer enabled.
779 */
780 private boolean areAllActivitiesStillEnabled() {
781 if (mShortcuts.size() == 0) {
782 return true;
783 }
784 final ShortcutService s = mShortcutUser.mService;
785
786 // Normally the number of target activities is 1 or so, so no need to use a complex
787 // structure like a set.
788 final ArrayList<ComponentName> checked = new ArrayList<>(4);
789
790 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
791 final ShortcutInfo si = mShortcuts.valueAt(i);
792 final ComponentName activity = si.getActivity();
793
794 if (checked.contains(activity)) {
795 continue; // Already checked.
796 }
797 checked.add(activity);
798
Makoto Onuki40dc2112017-10-18 12:52:45 -0700799 if ((activity != null)
800 && !s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700801 return false;
802 }
803 }
804 return true;
805 }
806
807 /**
808 * Called when the package may be added or updated, or its activities may be disabled, and
809 * if so, rescan the package and do the necessary stuff.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700810 *
811 * Add case:
812 * - Publish manifest shortcuts.
813 *
814 * Update case:
815 * - Re-publish manifest shortcuts.
816 * - If there are shortcuts with resources (icons or strings), update their timestamps.
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700817 * - Disable shortcuts whose target activities are disabled.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700818 *
819 * @return TRUE if any shortcuts have been changed.
Makoto Onuki39686e82016-04-13 18:03:00 -0700820 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700821 public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) {
822 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki84d59342018-02-02 09:22:38 -0800823 final long start = s.getStatStartTime();
Makoto Onuki39686e82016-04-13 18:03:00 -0700824
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700825 final PackageInfo pi;
826 try {
827 pi = mShortcutUser.mService.getPackageInfo(
828 getPackageName(), getPackageUserId());
829 if (pi == null) {
830 return false; // Shouldn't happen.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700831 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700832
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700833 if (!isNewApp && !forceRescan) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700834 // Return if the package hasn't changed, ie:
835 // - version code hasn't change
836 // - lastUpdateTime hasn't change
837 // - all target activities are still enabled.
Makoto Onuki33663282016-08-22 16:19:04 -0700838
839 // Note, system apps timestamps do *not* change after OTAs. (But they do
840 // after an adb sync or a local flash.)
841 // This means if a system app's version code doesn't change on an OTA,
842 // we don't notice it's updated. But that's fine since their version code *should*
843 // really change on OTAs.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700844 if ((getPackageInfo().getVersionCode() == pi.getLongVersionCode())
Makoto Onuki64183d52016-08-08 14:11:34 -0700845 && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime)
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700846 && areAllActivitiesStillEnabled()) {
847 return false;
848 }
849 }
850 } finally {
851 s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700852 }
853
854 // Now prepare to publish manifest shortcuts.
855 List<ShortcutInfo> newManifestShortcutList = null;
856 try {
857 newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800858 getPackageName(), getPackageUserId(), mShareTargets);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700859 } catch (IOException|XmlPullParserException e) {
860 Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
861 }
862 final int manifestShortcutSize = newManifestShortcutList == null ? 0
863 : newManifestShortcutList.size();
864 if (ShortcutService.DEBUG) {
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800865 Slog.d(TAG,
866 String.format("Package %s has %d manifest shortcut(s), and %d share target(s)",
867 getPackageName(), manifestShortcutSize, mShareTargets.size()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700868 }
869 if (isNewApp && (manifestShortcutSize == 0)) {
870 // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
871
872 // If it's an update, then it may already have manifest shortcuts, which need to be
873 // disabled.
874 return false;
875 }
876 if (ShortcutService.DEBUG) {
877 Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
878 (isNewApp ? "added" : "updated"),
Dianne Hackborn3accca02013-09-20 09:32:11 -0700879 getPackageInfo().getVersionCode(), pi.getLongVersionCode()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700880 }
881
Makoto Onukia4f89b12017-10-05 10:37:55 -0700882 getPackageInfo().updateFromPackageInfo(pi);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700883 final long newVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700884
885 // See if there are any shortcuts that were prevented restoring because the app was of a
886 // lower version, and re-enable them.
887 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
888 final ShortcutInfo si = mShortcuts.valueAt(i);
889 if (si.getDisabledReason() != ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
890 continue;
891 }
892 if (getPackageInfo().getBackupSourceVersionCode() > newVersionCode) {
893 if (ShortcutService.DEBUG) {
894 Slog.d(TAG, String.format("Shortcut %s require version %s, still not restored.",
895 si.getId(), getPackageInfo().getBackupSourceVersionCode()));
896 }
897 continue;
898 }
899 Slog.i(TAG, String.format("Restoring shortcut: %s", si.getId()));
900 si.clearFlags(ShortcutInfo.FLAG_DISABLED);
901 si.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
902 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700903
Makoto Onuki22fcc682016-05-17 14:52:19 -0700904 // For existing shortcuts, update timestamps if they have any resources.
Makoto Onukib08790c2016-06-23 14:05:46 -0700905 // Also check if shortcuts' activities are still main activities. Otherwise, disable them.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700906 if (!isNewApp) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700907 Resources publisherRes = null;
908
Makoto Onuki22fcc682016-05-17 14:52:19 -0700909 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
910 final ShortcutInfo si = mShortcuts.valueAt(i);
911
Makoto Onuki2d895c32016-12-02 15:48:40 -0800912 // Disable dynamic shortcuts whose target activity is gone.
Makoto Onukib08790c2016-06-23 14:05:46 -0700913 if (si.isDynamic()) {
Makoto Onuki34145532017-03-14 17:58:36 -0700914 if (si.getActivity() == null) {
915 // Note if it's dynamic, it must have a target activity, but b/36228253.
916 s.wtf("null activity detected.");
917 // TODO Maybe remove it?
918 } else if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700919 Slog.w(TAG, String.format(
920 "%s is no longer main activity. Disabling shorcut %s.",
921 getPackageName(), si.getId()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700922 if (disableDynamicWithId(si.getId(), /*ignoreInvisible*/ false,
923 ShortcutInfo.DISABLED_REASON_APP_CHANGED)) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700924 continue; // Actually removed.
925 }
926 // Still pinned, so fall-through and possibly update the resources.
927 }
Makoto Onukib08790c2016-06-23 14:05:46 -0700928 }
929
Makoto Onuki22fcc682016-05-17 14:52:19 -0700930 if (si.hasAnyResources()) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700931 if (!si.isOriginallyFromManifest()) {
932 if (publisherRes == null) {
933 publisherRes = getPackageResources();
934 if (publisherRes == null) {
935 break; // Resources couldn't be loaded.
936 }
937 }
938
939 // If this shortcut is not from a manifest, then update all resource IDs
940 // from resource names. (We don't allow resource strings for
941 // non-manifest at the moment, but icons can still be resources.)
942 si.lookupAndFillInResourceIds(publisherRes);
943 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700944 si.setTimestamp(s.injectCurrentTimeMillis());
945 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700946 }
947 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700948
949 // (Re-)publish manifest shortcut.
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700950 publishManifestShortcuts(newManifestShortcutList);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700951
Makoto Onuki7001a612016-05-27 13:24:28 -0700952 if (newManifestShortcutList != null) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700953 pushOutExcessShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -0700954 }
955
Makoto Onukidf6da042016-06-16 09:51:40 -0700956 s.verifyStates();
957
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700958 // This will send a notification to the launcher, and also save .
959 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
960 return true; // true means changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700961 }
962
963 private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
964 if (ShortcutService.DEBUG) {
965 Slog.d(TAG, String.format(
966 "Package %s: publishing manifest shortcuts", getPackageName()));
967 }
968 boolean changed = false;
969
Makoto Onuki22fcc682016-05-17 14:52:19 -0700970 // Keep the previous IDs.
971 ArraySet<String> toDisableList = null;
972 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
973 final ShortcutInfo si = mShortcuts.valueAt(i);
974
975 if (si.isManifestShortcut()) {
976 if (toDisableList == null) {
977 toDisableList = new ArraySet<>();
978 }
979 toDisableList.add(si.getId());
980 }
981 }
982
983 // Publish new ones.
984 if (newManifestShortcutList != null) {
985 final int newListSize = newManifestShortcutList.size();
986
987 for (int i = 0; i < newListSize; i++) {
988 changed = true;
989
990 final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
991 final boolean newDisabled = !newShortcut.isEnabled();
992
Makoto Onuki7001a612016-05-27 13:24:28 -0700993 final String id = newShortcut.getId();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700994 final ShortcutInfo oldShortcut = mShortcuts.get(id);
995
996 boolean wasPinned = false;
997
998 if (oldShortcut != null) {
999 if (!oldShortcut.isOriginallyFromManifest()) {
1000 Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
1001 + " exists but is not from AndroidManifest.xml, not updating.");
1002 continue;
1003 }
1004 // Take over the pinned flag.
1005 if (oldShortcut.isPinned()) {
1006 wasPinned = true;
1007 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
1008 }
1009 }
1010 if (newDisabled && !wasPinned) {
1011 // If the shortcut is disabled, and it was *not* pinned, then this
1012 // just doesn't have to be published.
1013 // Just keep it in toDisableList, so the previous one would be removed.
1014 continue;
1015 }
Makoto Onuki22fcc682016-05-17 14:52:19 -07001016
1017 // Note even if enabled=false, we still need to update all fields, so do it
1018 // regardless.
Makoto Onukia4f89b12017-10-05 10:37:55 -07001019 forceReplaceShortcutInner(newShortcut); // This will clean up the old one too.
Makoto Onuki22fcc682016-05-17 14:52:19 -07001020
1021 if (!newDisabled && toDisableList != null) {
1022 // Still alive, don't remove.
1023 toDisableList.remove(id);
1024 }
1025 }
1026 }
1027
1028 // Disable the previous manifest shortcuts that are no longer in the manifest.
1029 if (toDisableList != null) {
1030 if (ShortcutService.DEBUG) {
1031 Slog.d(TAG, String.format(
1032 "Package %s: disabling %d stale shortcuts", getPackageName(),
1033 toDisableList.size()));
1034 }
1035 for (int i = toDisableList.size() - 1; i >= 0; i--) {
1036 changed = true;
1037
1038 final String id = toDisableList.valueAt(i);
1039
1040 disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001041 /* overrideImmutable=*/ true, /*ignoreInvisible=*/ false,
1042 ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -07001043 }
1044 removeOrphans();
1045 }
Makoto Onukidf6da042016-06-16 09:51:40 -07001046 adjustRanks();
Makoto Onuki22fcc682016-05-17 14:52:19 -07001047 return changed;
Makoto Onuki39686e82016-04-13 18:03:00 -07001048 }
1049
Makoto Onuki7001a612016-05-27 13:24:28 -07001050 /**
1051 * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
1052 * If too many, we'll remove the dynamic with the lowest ranks.
1053 */
1054 private boolean pushOutExcessShortcuts() {
1055 final ShortcutService service = mShortcutUser.mService;
1056 final int maxShortcuts = service.getMaxActivityShortcuts();
1057
1058 boolean changed = false;
1059
1060 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1061 sortShortcutsToActivities();
1062 for (int outer = all.size() - 1; outer >= 0; outer--) {
1063 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1064 if (list.size() <= maxShortcuts) {
1065 continue;
1066 }
1067 // Sort by isManifestShortcut() and getRank().
1068 Collections.sort(list, mShortcutTypeAndRankComparator);
1069
1070 // Keep [0 .. max), and remove (as dynamic) [max .. size)
1071 for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
1072 final ShortcutInfo shortcut = list.get(inner);
1073
1074 if (shortcut.isManifestShortcut()) {
1075 // This shouldn't happen -- excess shortcuts should all be non-manifest.
1076 // But just in case.
1077 service.wtf("Found manifest shortcuts in excess list.");
1078 continue;
1079 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001080 deleteDynamicWithId(shortcut.getId(), /*ignoreInvisible=*/ true);
Makoto Onuki7001a612016-05-27 13:24:28 -07001081 }
1082 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001083
1084 return changed;
1085 }
1086
1087 /**
1088 * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before
1089 * non-manifest shortcuts, then sort by rank.
1090 *
1091 * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
1092 * manifest shortcuts than before and as a result we need to remove some of the dynamic
1093 * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with
1094 * the last ones.
1095 *
1096 * (Note the number of manifest shortcuts is always <= the max number, because if there are
1097 * more, ShortcutParser would ignore the rest.)
1098 */
1099 final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
1100 ShortcutInfo b) -> {
1101 if (a.isManifestShortcut() && !b.isManifestShortcut()) {
1102 return -1;
1103 }
1104 if (!a.isManifestShortcut() && b.isManifestShortcut()) {
1105 return 1;
1106 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001107 return Integer.compare(a.getRank(), b.getRank());
Makoto Onuki7001a612016-05-27 13:24:28 -07001108 };
1109
1110 /**
1111 * Build a list of shortcuts for each target activity and return as a map. The result won't
1112 * contain "floating" shortcuts because they don't belong on any activities.
1113 */
1114 private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
Makoto Onuki7001a612016-05-27 13:24:28 -07001115 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
1116 = new ArrayMap<>();
1117 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1118 final ShortcutInfo si = mShortcuts.valueAt(i);
1119 if (si.isFloating()) {
1120 continue; // Ignore floating shortcuts, which are not tied to any activities.
1121 }
1122
1123 final ComponentName activity = si.getActivity();
Makoto Onuki34145532017-03-14 17:58:36 -07001124 if (activity == null) {
1125 mShortcutUser.mService.wtf("null activity detected.");
1126 continue;
1127 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001128
1129 ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
1130 if (list == null) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001131 list = new ArrayList<>();
Makoto Onuki7001a612016-05-27 13:24:28 -07001132 activitiesToShortcuts.put(activity, list);
1133 }
1134 list.add(si);
1135 }
1136 return activitiesToShortcuts;
1137 }
1138
1139 /** Used by {@link #enforceShortcutCountsBeforeOperation} */
1140 private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
1141 ComponentName cn, int increment) {
1142 Integer oldValue = counts.get(cn);
1143 if (oldValue == null) {
1144 oldValue = 0;
1145 }
1146
1147 counts.put(cn, oldValue + increment);
1148 }
1149
1150 /**
1151 * Called by
1152 * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
1153 * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
1154 * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
1155 * the operation to make sure the operation wouldn't result in the target activities having
1156 * more than the allowed number of dynamic/manifest shortcuts.
1157 *
1158 * @param newList shortcut list passed to set, add or updateShortcuts().
1159 * @param operation add, set or update.
1160 * @throws IllegalArgumentException if the operation would result in going over the max
1161 * shortcut count for any activity.
1162 */
1163 public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
1164 @ShortcutOperation int operation) {
1165 final ShortcutService service = mShortcutUser.mService;
1166
1167 // Current # of dynamic / manifest shortcuts for each activity.
1168 // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
1169 // anyway.)
1170 final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
1171 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1172 final ShortcutInfo shortcut = mShortcuts.valueAt(i);
1173
1174 if (shortcut.isManifestShortcut()) {
1175 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1176 } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
1177 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1178 }
1179 }
1180
1181 for (int i = newList.size() - 1; i >= 0; i--) {
1182 final ShortcutInfo newShortcut = newList.get(i);
1183 final ComponentName newActivity = newShortcut.getActivity();
1184 if (newActivity == null) {
1185 if (operation != ShortcutService.OPERATION_UPDATE) {
Makoto Onukib08790c2016-06-23 14:05:46 -07001186 service.wtf("Activity must not be null at this point");
1187 continue; // Just ignore this invalid case.
Makoto Onuki7001a612016-05-27 13:24:28 -07001188 }
1189 continue; // Activity can be null for update.
1190 }
1191
1192 final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
1193 if (original == null) {
1194 if (operation == ShortcutService.OPERATION_UPDATE) {
1195 continue; // When updating, ignore if there's no target.
1196 }
1197 // Add() or set(), and there's no existing shortcut with the same ID. We're
1198 // simply publishing (as opposed to updating) this shortcut, so just +1.
1199 incrementCountForActivity(counts, newActivity, 1);
1200 continue;
1201 }
1202 if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
1203 // Updating floating shortcuts doesn't affect the count, so ignore.
1204 continue;
1205 }
1206
1207 // If it's add() or update(), then need to decrement for the previous activity.
1208 // Skip it for set() since it's already been taken care of by not counting the original
1209 // dynamic shortcuts in the first loop.
1210 if (operation != ShortcutService.OPERATION_SET) {
1211 final ComponentName oldActivity = original.getActivity();
1212 if (!original.isFloating()) {
1213 incrementCountForActivity(counts, oldActivity, -1);
1214 }
1215 }
1216 incrementCountForActivity(counts, newActivity, 1);
1217 }
1218
1219 // Then make sure none of the activities have more than the max number of shortcuts.
1220 for (int i = counts.size() - 1; i >= 0; i--) {
1221 service.enforceMaxActivityShortcuts(counts.valueAt(i));
1222 }
1223 }
1224
Makoto Onuki157b1622016-06-02 16:13:10 -07001225 /**
1226 * For all the text fields, refresh the string values if they're from resources.
1227 */
1228 public void resolveResourceStrings() {
1229 final ShortcutService s = mShortcutUser.mService;
1230 boolean changed = false;
1231
1232 Resources publisherRes = null;
1233 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1234 final ShortcutInfo si = mShortcuts.valueAt(i);
1235
1236 if (si.hasStringResources()) {
1237 changed = true;
1238
1239 if (publisherRes == null) {
1240 publisherRes = getPackageResources();
1241 if (publisherRes == null) {
1242 break; // Resources couldn't be loaded.
1243 }
1244 }
1245
1246 si.resolveResourceStrings(publisherRes);
1247 si.setTimestamp(s.injectCurrentTimeMillis());
1248 }
1249 }
1250 if (changed) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001251 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001252 }
1253 }
1254
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001255 /** Clears the implicit ranks for all shortcuts. */
1256 public void clearAllImplicitRanks() {
1257 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1258 final ShortcutInfo si = mShortcuts.valueAt(i);
1259 si.clearImplicitRankAndRankChangedFlag();
1260 }
1261 }
1262
1263 /**
1264 * Used to sort shortcuts for rank auto-adjusting.
1265 */
1266 final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
1267 // First, sort by rank.
1268 int ret = Integer.compare(a.getRank(), b.getRank());
1269 if (ret != 0) {
1270 return ret;
1271 }
1272 // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
1273 // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
1274 // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
1275 // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
1276 // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
1277 if (a.isRankChanged() != b.isRankChanged()) {
1278 return a.isRankChanged() ? -1 : 1;
1279 }
1280 // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
1281 // they're passed to the API.
1282 ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
1283 if (ret != 0) {
1284 return ret;
1285 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001286 // If they're still tie, just sort by their IDs.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001287 // This may happen with updateShortcuts() -- see
1288 // the testUpdateShortcuts_noManifestShortcuts() test.
1289 return a.getId().compareTo(b.getId());
1290 };
1291
1292 /**
1293 * Re-calculate the ranks for all shortcuts.
1294 */
1295 public void adjustRanks() {
1296 final ShortcutService s = mShortcutUser.mService;
1297 final long now = s.injectCurrentTimeMillis();
1298
1299 // First, clear ranks for floating shortcuts.
1300 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1301 final ShortcutInfo si = mShortcuts.valueAt(i);
1302 if (si.isFloating()) {
1303 if (si.getRank() != 0) {
1304 si.setTimestamp(now);
1305 si.setRank(0);
1306 }
1307 }
1308 }
1309
1310 // Then adjust ranks. Ranks are unique for each activity, so we first need to sort
1311 // shortcuts to each activity.
1312 // Then sort the shortcuts within each activity with mShortcutRankComparator, and
1313 // assign ranks from 0.
1314 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1315 sortShortcutsToActivities();
1316 for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
1317 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1318
1319 // Sort by ranks and other signals.
1320 Collections.sort(list, mShortcutRankComparator);
1321
1322 int rank = 0;
1323
1324 final int size = list.size();
1325 for (int i = 0; i < size; i++) {
1326 final ShortcutInfo si = list.get(i);
1327 if (si.isManifestShortcut()) {
1328 // Don't adjust ranks for manifest shortcuts.
1329 continue;
1330 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001331 // At this point, it must be dynamic.
1332 if (!si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001333 s.wtf("Non-dynamic shortcut found.");
1334 continue;
1335 }
1336 final int thisRank = rank++;
1337 if (si.getRank() != thisRank) {
1338 si.setTimestamp(now);
1339 si.setRank(thisRank);
1340 }
1341 }
1342 }
1343 }
1344
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07001345 /** @return true if there's any shortcuts that are not manifest shortcuts. */
1346 public boolean hasNonManifestShortcuts() {
1347 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1348 final ShortcutInfo si = mShortcuts.valueAt(i);
1349 if (!si.isDeclaredInManifest()) {
1350 return true;
1351 }
1352 }
1353 return false;
1354 }
1355
Makoto Onuki20b82212017-10-04 15:03:50 -07001356 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -07001357 pw.println();
1358
1359 pw.print(prefix);
1360 pw.print("Package: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001361 pw.print(getPackageName());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001362 pw.print(" UID: ");
1363 pw.print(mPackageUid);
Makoto Onuki31459242016-03-22 11:12:18 -07001364 pw.println();
1365
1366 pw.print(prefix);
1367 pw.print(" ");
1368 pw.print("Calls: ");
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001369 pw.print(getApiCallCount(/*unlimited=*/ false));
Makoto Onuki31459242016-03-22 11:12:18 -07001370 pw.println();
1371
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001372 // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
1373 pw.print(prefix);
1374 pw.print(" ");
1375 pw.print("Last known FG: ");
1376 pw.print(mLastKnownForegroundElapsedTime);
1377 pw.println();
1378
Makoto Onuki31459242016-03-22 11:12:18 -07001379 // This should be after getApiCallCount(), which may update it.
1380 pw.print(prefix);
1381 pw.print(" ");
1382 pw.print("Last reset: [");
1383 pw.print(mLastResetTime);
1384 pw.print("] ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001385 pw.print(ShortcutService.formatTime(mLastResetTime));
Makoto Onuki31459242016-03-22 11:12:18 -07001386 pw.println();
1387
Makoto Onukic51b2872016-05-04 15:24:50 -07001388 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001389 pw.println();
1390
Makoto Onuki39686e82016-04-13 18:03:00 -07001391 pw.print(prefix);
1392 pw.println(" Shortcuts:");
Makoto Onuki31459242016-03-22 11:12:18 -07001393 long totalBitmapSize = 0;
1394 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1395 final int size = shortcuts.size();
1396 for (int i = 0; i < size; i++) {
1397 final ShortcutInfo si = shortcuts.valueAt(i);
Makoto Onuki6208c672017-10-02 16:20:35 -07001398 pw.println(si.toDumpString(prefix + " "));
Makoto Onuki31459242016-03-22 11:12:18 -07001399 if (si.getBitmapPath() != null) {
1400 final long len = new File(si.getBitmapPath()).length();
Makoto Onuki39686e82016-04-13 18:03:00 -07001401 pw.print(prefix);
1402 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001403 pw.print("bitmap size=");
1404 pw.println(len);
1405
1406 totalBitmapSize += len;
1407 }
1408 }
1409 pw.print(prefix);
1410 pw.print(" ");
1411 pw.print("Total bitmap size: ");
1412 pw.print(totalBitmapSize);
1413 pw.print(" (");
Makoto Onukic51b2872016-05-04 15:24:50 -07001414 pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
Makoto Onuki31459242016-03-22 11:12:18 -07001415 pw.println(")");
1416 }
1417
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001418 @Override
Makoto Onuki76269922016-07-15 14:58:54 -07001419 public JSONObject dumpCheckin(boolean clear) throws JSONException {
1420 final JSONObject result = super.dumpCheckin(clear);
1421
1422 int numDynamic = 0;
1423 int numPinned = 0;
1424 int numManifest = 0;
1425 int numBitmaps = 0;
1426 long totalBitmapSize = 0;
1427
1428 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1429 final int size = shortcuts.size();
1430 for (int i = 0; i < size; i++) {
1431 final ShortcutInfo si = shortcuts.valueAt(i);
1432
1433 if (si.isDynamic()) numDynamic++;
1434 if (si.isDeclaredInManifest()) numManifest++;
1435 if (si.isPinned()) numPinned++;
1436
1437 if (si.getBitmapPath() != null) {
1438 numBitmaps++;
1439 totalBitmapSize += new File(si.getBitmapPath()).length();
1440 }
1441 }
1442
1443 result.put(KEY_DYNAMIC, numDynamic);
1444 result.put(KEY_MANIFEST, numManifest);
1445 result.put(KEY_PINNED, numPinned);
1446 result.put(KEY_BITMAPS, numBitmaps);
1447 result.put(KEY_BITMAP_BYTES, totalBitmapSize);
1448
1449 // TODO Log update frequency too.
1450
1451 return result;
1452 }
1453
1454 @Override
Makoto Onuki0acbb142016-03-22 17:02:57 -07001455 public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
1456 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001457 final int size = mShortcuts.size();
Mehdi Alizadehabec3192019-06-27 18:06:15 -07001458 final int shareTargetSize = mShareTargets.size();
Makoto Onuki31459242016-03-22 11:12:18 -07001459
Mehdi Alizadehabec3192019-06-27 18:06:15 -07001460 if (size == 0 && shareTargetSize == 0 && mApiCallCount == 0) {
Makoto Onuki31459242016-03-22 11:12:18 -07001461 return; // nothing to write.
1462 }
1463
1464 out.startTag(null, TAG_ROOT);
1465
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001466 ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
Makoto Onuki31459242016-03-22 11:12:18 -07001467 ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
1468 ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
Makoto Onukie3fffa92018-02-28 16:25:40 -08001469 getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001470
1471 for (int j = 0; j < size; j++) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001472 saveShortcut(out, mShortcuts.valueAt(j), forBackup,
1473 getPackageInfo().isBackupAllowed());
Makoto Onuki31459242016-03-22 11:12:18 -07001474 }
1475
Mehdi Alizadehabec3192019-06-27 18:06:15 -07001476 if (!forBackup) {
1477 for (int j = 0; j < shareTargetSize; j++) {
1478 mShareTargets.get(j).saveToXml(out);
1479 }
1480 }
1481
Makoto Onuki31459242016-03-22 11:12:18 -07001482 out.endTag(null, TAG_ROOT);
1483 }
1484
Makoto Onukia4f89b12017-10-05 10:37:55 -07001485 private void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup,
1486 boolean appSupportsBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001487 throws IOException, XmlPullParserException {
Makoto Onuki475c3652017-05-08 14:29:03 -07001488
1489 final ShortcutService s = mShortcutUser.mService;
1490
Makoto Onuki0acbb142016-03-22 17:02:57 -07001491 if (forBackup) {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001492 if (!(si.isPinned() && si.isEnabled())) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001493 // We only backup pinned shortcuts that are enabled.
1494 // Note, this means, shortcuts that are restored but are blocked restore, e.g. due
1495 // to a lower version code, will not be ported to a new device.
1496 return;
Makoto Onuki0acbb142016-03-22 17:02:57 -07001497 }
1498 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001499 final boolean shouldBackupDetails =
1500 !forBackup // It's not backup
1501 || appSupportsBackup; // Or, it's a backup and app supports backup.
1502
Makoto Onuki475c3652017-05-08 14:29:03 -07001503 // Note: at this point no shortcuts should have bitmaps pending save, but if they do,
1504 // just remove the bitmap.
1505 if (si.isIconPendingSave()) {
1506 s.removeIconLocked(si);
1507 }
Makoto Onuki31459242016-03-22 11:12:18 -07001508 out.startTag(null, TAG_SHORTCUT);
1509 ShortcutService.writeAttr(out, ATTR_ID, si.getId());
1510 // writeAttr(out, "package", si.getPackageName()); // not needed
Makoto Onuki22fcc682016-05-17 14:52:19 -07001511 ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
Makoto Onuki31459242016-03-22 11:12:18 -07001512 // writeAttr(out, "icon", si.getIcon()); // We don't save it.
1513 ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001514 ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001515 ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001516 ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001517 ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001518 ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
Makoto Onukia4f89b12017-10-05 10:37:55 -07001519 if (shouldBackupDetails) {
1520 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
1521 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
1522 si.getDisabledMessageResourceId());
1523 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
1524 si.getDisabledMessageResName());
1525 }
1526 ShortcutService.writeAttr(out, ATTR_DISABLED_REASON, si.getDisabledReason());
Makoto Onuki31459242016-03-22 11:12:18 -07001527 ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
1528 si.getLastChangedTimestamp());
Adam Hed4586b12019-03-19 12:01:00 -07001529 final LocusId locusId = si.getLocusId();
1530 if (locusId != null) {
1531 ShortcutService.writeAttr(out, ATTR_LOCUS_ID, si.getLocusId().getId());
1532 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001533 if (forBackup) {
1534 // Don't write icon information. Also drop the dynamic flag.
Makoto Onukia4f89b12017-10-05 10:37:55 -07001535
1536 int flags = si.getFlags() &
1537 ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
Makoto Onuki475c3652017-05-08 14:29:03 -07001538 | ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE
Makoto Onukia4f89b12017-10-05 10:37:55 -07001539 | ShortcutInfo.FLAG_DYNAMIC);
1540 ShortcutService.writeAttr(out, ATTR_FLAGS, flags);
1541
1542 // Set the publisher version code at every backup.
Dianne Hackborn3accca02013-09-20 09:32:11 -07001543 final long packageVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -07001544 if (packageVersionCode == 0) {
1545 s.wtf("Package version code should be available at this point.");
1546 // However, 0 is a valid version code, so we just go ahead with it...
1547 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001548 } else {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001549 // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
1550 // as dynamic.
1551 ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
1552
Makoto Onuki0acbb142016-03-22 17:02:57 -07001553 ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
Makoto Onuki157b1622016-06-02 16:13:10 -07001554 ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
1555 ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001556 ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
1557 }
Makoto Onuki31459242016-03-22 11:12:18 -07001558
Makoto Onukia4f89b12017-10-05 10:37:55 -07001559 if (shouldBackupDetails) {
1560 {
1561 final Set<String> cat = si.getCategories();
1562 if (cat != null && cat.size() > 0) {
1563 out.startTag(null, TAG_CATEGORIES);
1564 XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
1565 NAME_CATEGORIES, out);
1566 out.endTag(null, TAG_CATEGORIES);
1567 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001568 }
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001569 if (!forBackup) { // Don't backup the persons field.
1570 final Person[] persons = si.getPersons();
1571 if (!ArrayUtils.isEmpty(persons)) {
1572 for (int i = 0; i < persons.length; i++) {
1573 final Person p = persons[i];
1574
1575 out.startTag(null, TAG_PERSON);
1576 ShortcutService.writeAttr(out, ATTR_PERSON_NAME, p.getName());
1577 ShortcutService.writeAttr(out, ATTR_PERSON_URI, p.getUri());
1578 ShortcutService.writeAttr(out, ATTR_PERSON_KEY, p.getKey());
1579 ShortcutService.writeAttr(out, ATTR_PERSON_IS_BOT, p.isBot());
1580 ShortcutService.writeAttr(out, ATTR_PERSON_IS_IMPORTANT, p.isImportant());
1581 out.endTag(null, TAG_PERSON);
1582 }
1583 }
1584 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001585 final Intent[] intentsNoExtras = si.getIntentsNoExtras();
1586 final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases();
1587 final int numIntents = intentsNoExtras.length;
1588 for (int i = 0; i < numIntents; i++) {
1589 out.startTag(null, TAG_INTENT);
1590 ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]);
1591 ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]);
1592 out.endTag(null, TAG_INTENT);
1593 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001594
Makoto Onukia4f89b12017-10-05 10:37:55 -07001595 ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
1596 }
Hakan Seyalioglu58fc95d2016-12-13 15:23:22 -08001597
Makoto Onuki31459242016-03-22 11:12:18 -07001598 out.endTag(null, TAG_SHORTCUT);
1599 }
1600
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001601 public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
1602 XmlPullParser parser, boolean fromBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001603 throws IOException, XmlPullParserException {
1604
1605 final String packageName = ShortcutService.parseStringAttribute(parser,
1606 ATTR_NAME);
1607
Makoto Onukic51b2872016-05-04 15:24:50 -07001608 final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001609 shortcutUser.getUserId(), packageName);
Makoto Onuki31459242016-03-22 11:12:18 -07001610
Makoto Onuki31459242016-03-22 11:12:18 -07001611 ret.mApiCallCount =
1612 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
1613 ret.mLastResetTime =
1614 ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
1615
Makoto Onukia4f89b12017-10-05 10:37:55 -07001616
Makoto Onuki31459242016-03-22 11:12:18 -07001617 final int outerDepth = parser.getDepth();
1618 int type;
1619 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1620 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1621 if (type != XmlPullParser.START_TAG) {
1622 continue;
1623 }
1624 final int depth = parser.getDepth();
1625 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001626 if (depth == outerDepth + 1) {
1627 switch (tag) {
1628 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -07001629 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001630
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001631 continue;
1632 case TAG_SHORTCUT:
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001633 final ShortcutInfo si = parseShortcut(parser, packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001634 shortcutUser.getUserId(), fromBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001635
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001636 // Don't use addShortcut(), we don't need to save the icon.
1637 ret.mShortcuts.put(si.getId(), si);
1638 continue;
Mehdi Alizadehabec3192019-06-27 18:06:15 -07001639 case TAG_SHARE_TARGET:
1640 ret.mShareTargets.add(ShareTargetInfo.loadFromXml(parser));
1641 continue;
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001642 }
Makoto Onuki31459242016-03-22 11:12:18 -07001643 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001644 ShortcutService.warnForInvalidTag(depth, tag);
1645 }
Makoto Onuki31459242016-03-22 11:12:18 -07001646 return ret;
1647 }
1648
Makoto Onukiabe84422016-04-07 09:41:19 -07001649 private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001650 @UserIdInt int userId, boolean fromBackup)
1651 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001652 String id;
1653 ComponentName activityComponent;
1654 // Icon icon;
1655 String title;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001656 int titleResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001657 String titleResName;
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001658 String text;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001659 int textResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001660 String textResName;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001661 String disabledMessage;
1662 int disabledMessageResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001663 String disabledMessageResName;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001664 int disabledReason;
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001665 Intent intentLegacy;
1666 PersistableBundle intentPersistableExtrasLegacy = null;
1667 ArrayList<Intent> intents = new ArrayList<>();
Makoto Onuki20c95f82016-05-11 16:51:01 -07001668 int rank;
Makoto Onuki31459242016-03-22 11:12:18 -07001669 PersistableBundle extras = null;
1670 long lastChangedTimestamp;
1671 int flags;
Makoto Onuki157b1622016-06-02 16:13:10 -07001672 int iconResId;
1673 String iconResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001674 String bitmapPath;
Adam Hed4586b12019-03-19 12:01:00 -07001675 final String locusIdString;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001676 int backupVersionCode;
Makoto Onukibe73a802016-04-15 14:46:35 -07001677 ArraySet<String> categories = null;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001678 ArrayList<Person> persons = new ArrayList<>();
Makoto Onuki31459242016-03-22 11:12:18 -07001679
1680 id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
1681 activityComponent = ShortcutService.parseComponentNameAttribute(parser,
1682 ATTR_ACTIVITY);
1683 title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001684 titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001685 titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001686 text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001687 textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001688 textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001689 disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
1690 disabledMessageResId = ShortcutService.parseIntAttribute(parser,
1691 ATTR_DISABLED_MESSAGE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001692 disabledMessageResName = ShortcutService.parseStringAttribute(parser,
1693 ATTR_DISABLED_MESSAGE_RES_NAME);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001694 disabledReason = ShortcutService.parseIntAttribute(parser, ATTR_DISABLED_REASON);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001695 intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001696 rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001697 lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
Makoto Onuki31459242016-03-22 11:12:18 -07001698 flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
Makoto Onuki157b1622016-06-02 16:13:10 -07001699 iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
1700 iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001701 bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
Adam Hed4586b12019-03-19 12:01:00 -07001702 locusIdString = ShortcutService.parseStringAttribute(parser, ATTR_LOCUS_ID);
Makoto Onuki31459242016-03-22 11:12:18 -07001703
1704 final int outerDepth = parser.getDepth();
1705 int type;
1706 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1707 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1708 if (type != XmlPullParser.START_TAG) {
1709 continue;
1710 }
1711 final int depth = parser.getDepth();
1712 final String tag = parser.getName();
1713 if (ShortcutService.DEBUG_LOAD) {
1714 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1715 depth, type, tag));
1716 }
1717 switch (tag) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001718 case TAG_INTENT_EXTRAS_LEGACY:
1719 intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser);
1720 continue;
1721 case TAG_INTENT:
1722 intents.add(parseIntent(parser));
Makoto Onuki31459242016-03-22 11:12:18 -07001723 continue;
1724 case TAG_EXTRAS:
1725 extras = PersistableBundle.restoreFromXml(parser);
1726 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001727 case TAG_CATEGORIES:
1728 // This just contains string-array.
1729 continue;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001730 case TAG_PERSON:
1731 persons.add(parsePerson(parser));
1732 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001733 case TAG_STRING_ARRAY_XMLUTILS:
1734 if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
1735 ATTR_NAME_XMLUTILS))) {
Makoto Onukibe73a802016-04-15 14:46:35 -07001736 final String[] ar = XmlUtils.readThisStringArrayXml(
1737 parser, TAG_STRING_ARRAY_XMLUTILS, null);
1738 categories = new ArraySet<>(ar.length);
1739 for (int i = 0; i < ar.length; i++) {
1740 categories.add(ar[i]);
1741 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001742 }
1743 continue;
Makoto Onuki31459242016-03-22 11:12:18 -07001744 }
1745 throw ShortcutService.throwForInvalidTag(depth, tag);
1746 }
Makoto Onukibe73a802016-04-15 14:46:35 -07001747
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001748 if (intentLegacy != null) {
1749 // For the legacy file format which supported only one intent per shortcut.
1750 ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy);
1751 intents.clear();
1752 intents.add(intentLegacy);
1753 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001754
Makoto Onukia4f89b12017-10-05 10:37:55 -07001755
1756 if ((disabledReason == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)
1757 && ((flags & ShortcutInfo.FLAG_DISABLED) != 0)) {
1758 // We didn't used to have the disabled reason, so if a shortcut is disabled
1759 // and has no reason, we assume it was disabled by publisher.
1760 disabledReason = ShortcutInfo.DISABLED_REASON_BY_APP;
1761 }
1762
1763 // All restored shortcuts are initially "shadow".
1764 if (fromBackup) {
1765 flags |= ShortcutInfo.FLAG_SHADOW;
1766 }
1767
Adam Hed4586b12019-03-19 12:01:00 -07001768 final LocusId locusId = locusIdString == null ? null : new LocusId(locusIdString);
Felipe Leme90205ef2019-03-05 09:59:52 -08001769
Makoto Onuki31459242016-03-22 11:12:18 -07001770 return new ShortcutInfo(
Felipe Leme90205ef2019-03-05 09:59:52 -08001771 userId, id, packageName, activityComponent, /* icon= */ null,
Makoto Onuki157b1622016-06-02 16:13:10 -07001772 title, titleResId, titleResName, text, textResId, textResName,
1773 disabledMessage, disabledMessageResId, disabledMessageResName,
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001774 categories,
1775 intents.toArray(new Intent[intents.size()]),
1776 rank, extras, lastChangedTimestamp, flags,
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001777 iconResId, iconResName, bitmapPath, disabledReason,
Felipe Leme90205ef2019-03-05 09:59:52 -08001778 persons.toArray(new Person[persons.size()]), locusId);
Makoto Onuki31459242016-03-22 11:12:18 -07001779 }
Makoto Onuki2e210c42016-03-30 08:30:36 -07001780
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001781 private static Intent parseIntent(XmlPullParser parser)
1782 throws IOException, XmlPullParserException {
1783
1784 Intent intent = ShortcutService.parseIntentAttribute(parser,
1785 ATTR_INTENT_NO_EXTRA);
1786
1787 final int outerDepth = parser.getDepth();
1788 int type;
1789 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1790 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1791 if (type != XmlPullParser.START_TAG) {
1792 continue;
1793 }
1794 final int depth = parser.getDepth();
1795 final String tag = parser.getName();
1796 if (ShortcutService.DEBUG_LOAD) {
1797 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1798 depth, type, tag));
1799 }
1800 switch (tag) {
1801 case TAG_EXTRAS:
1802 ShortcutInfo.setIntentExtras(intent,
1803 PersistableBundle.restoreFromXml(parser));
1804 continue;
1805 }
1806 throw ShortcutService.throwForInvalidTag(depth, tag);
1807 }
1808 return intent;
1809 }
1810
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001811 private static Person parsePerson(XmlPullParser parser)
1812 throws IOException, XmlPullParserException {
1813 CharSequence name = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_NAME);
1814 String uri = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_URI);
1815 String key = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_KEY);
1816 boolean isBot = ShortcutService.parseBooleanAttribute(parser, ATTR_PERSON_IS_BOT);
1817 boolean isImportant = ShortcutService.parseBooleanAttribute(parser,
1818 ATTR_PERSON_IS_IMPORTANT);
1819
1820 Person.Builder builder = new Person.Builder();
1821 builder.setName(name).setUri(uri).setKey(key).setBot(isBot).setImportant(isImportant);
1822 return builder.build();
1823 }
1824
Makoto Onuki2e210c42016-03-30 08:30:36 -07001825 @VisibleForTesting
1826 List<ShortcutInfo> getAllShortcutsForTest() {
1827 return new ArrayList<>(mShortcuts.values());
1828 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001829
Mehdi Alizadeh32774622018-11-05 17:32:01 -08001830 @VisibleForTesting
1831 List<ShareTargetInfo> getAllShareTargetsForTest() {
1832 return new ArrayList<>(mShareTargets);
1833 }
1834
Makoto Onuki7001a612016-05-27 13:24:28 -07001835 @Override
1836 public void verifyStates() {
1837 super.verifyStates();
1838
1839 boolean failed = false;
1840
Makoto Onuki255461f2017-01-10 11:47:25 -08001841 final ShortcutService s = mShortcutUser.mService;
1842
Makoto Onuki7001a612016-05-27 13:24:28 -07001843 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1844 sortShortcutsToActivities();
1845
1846 // Make sure each activity won't have more than max shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001847 for (int outer = all.size() - 1; outer >= 0; outer--) {
1848 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1849 if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001850 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001851 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
1852 + " has " + all.valueAt(outer).size() + " shortcuts.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001853 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001854
1855 // Sort by rank.
1856 Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
1857
1858 // Split into two arrays for each kind.
1859 final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
1860 dynamicList.removeIf((si) -> !si.isDynamic());
1861
1862 final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
1863 dynamicList.removeIf((si) -> !si.isManifestShortcut());
1864
1865 verifyRanksSequential(dynamicList);
1866 verifyRanksSequential(manifestList);
Makoto Onuki7001a612016-05-27 13:24:28 -07001867 }
1868
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001869 // Verify each shortcut's status.
Makoto Onuki7001a612016-05-27 13:24:28 -07001870 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1871 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onuki99302b52017-03-29 12:42:26 -07001872 if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001873 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001874 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki99302b52017-03-29 12:42:26 -07001875 + " is not manifest, dynamic or pinned.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001876 }
Makoto Onukiff14f732016-06-30 17:07:25 -07001877 if (si.isDeclaredInManifest() && si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001878 failed = true;
1879 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1880 + " is both dynamic and manifest at the same time.");
1881 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001882 if (si.getActivity() == null && !si.isFloating()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001883 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001884 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki255461f2017-01-10 11:47:25 -08001885 + " has null activity, but not floating.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001886 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001887 if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001888 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001889 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001890 + " is not floating, but is disabled.");
1891 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001892 if (si.isFloating() && si.getRank() != 0) {
1893 failed = true;
1894 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1895 + " is floating, but has rank=" + si.getRank());
1896 }
Makoto Onukidd097812016-06-29 13:10:09 -07001897 if (si.getIcon() != null) {
1898 failed = true;
1899 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1900 + " still has an icon");
1901 }
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001902 if (si.hasAdaptiveBitmap() && !si.hasIconFile()) {
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001903 failed = true;
1904 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001905 + " has adaptive bitmap but was not saved to a file.");
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001906 }
Makoto Onukidd097812016-06-29 13:10:09 -07001907 if (si.hasIconFile() && si.hasIconResource()) {
1908 failed = true;
1909 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1910 + " has both resource and bitmap icons");
1911 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001912 if (si.isEnabled()
1913 != (si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)) {
1914 failed = true;
1915 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1916 + " isEnabled() and getDisabledReason() disagree: "
1917 + si.isEnabled() + " vs " + si.getDisabledReason());
1918 }
1919 if ((si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
1920 && (getPackageInfo().getBackupSourceVersionCode()
1921 == ShortcutInfo.VERSION_CODE_UNKNOWN)) {
1922 failed = true;
1923 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1924 + " RESTORED_VERSION_LOWER with no backup source version code.");
1925 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001926 if (s.isDummyMainActivity(si.getActivity())) {
1927 failed = true;
1928 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1929 + " has a dummy target activity");
1930 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001931 }
1932
1933 if (failed) {
Makoto Onuki9fd90192017-01-06 18:31:03 +00001934 throw new IllegalStateException("See logcat for errors");
Makoto Onuki7001a612016-05-27 13:24:28 -07001935 }
1936 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001937
1938 private boolean verifyRanksSequential(List<ShortcutInfo> list) {
1939 boolean failed = false;
1940
1941 for (int i = 0; i < list.size(); i++) {
1942 final ShortcutInfo si = list.get(i);
1943 if (si.getRank() != i) {
1944 failed = true;
1945 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1946 + " rank=" + si.getRank() + " but expected to be "+ i);
1947 }
1948 }
1949 return failed;
1950 }
Makoto Onuki31459242016-03-22 11:12:18 -07001951}