blob: ebf6672cf57e304c902849af56d5e9d30e47dd80 [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;
Makoto Onuki31459242016-03-22 11:12:18 -070021import android.content.ComponentName;
22import android.content.Intent;
Makoto Onuki22fcc682016-05-17 14:52:19 -070023import android.content.pm.PackageInfo;
Makoto Onuki31459242016-03-22 11:12:18 -070024import android.content.pm.ShortcutInfo;
Makoto Onuki157b1622016-06-02 16:13:10 -070025import android.content.res.Resources;
Makoto Onuki31459242016-03-22 11:12:18 -070026import android.os.PersistableBundle;
27import android.text.format.Formatter;
28import android.util.ArrayMap;
29import android.util.ArraySet;
Makoto Onuki7001a612016-05-27 13:24:28 -070030import android.util.Log;
Makoto Onuki31459242016-03-22 11:12:18 -070031import android.util.Slog;
32
Makoto Onuki2e210c42016-03-30 08:30:36 -070033import com.android.internal.annotations.VisibleForTesting;
Makoto Onuki22fcc682016-05-17 14:52:19 -070034import com.android.internal.util.Preconditions;
Makoto Onukib6d35232016-04-04 15:57:17 -070035import com.android.internal.util.XmlUtils;
Makoto Onuki20b82212017-10-04 15:03:50 -070036import com.android.server.pm.ShortcutService.DumpFilter;
Makoto Onuki7001a612016-05-27 13:24:28 -070037import com.android.server.pm.ShortcutService.ShortcutOperation;
Makoto Onuki4e6cef42016-07-13 16:14:01 -070038import com.android.server.pm.ShortcutService.Stats;
Makoto Onuki2e210c42016-03-30 08:30:36 -070039
Makoto Onuki76269922016-07-15 14:58:54 -070040import org.json.JSONException;
41import org.json.JSONObject;
Makoto Onuki31459242016-03-22 11:12:18 -070042import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlPullParserException;
44import org.xmlpull.v1.XmlSerializer;
45
46import java.io.File;
47import java.io.IOException;
48import java.io.PrintWriter;
49import java.util.ArrayList;
Makoto Onuki7001a612016-05-27 13:24:28 -070050import java.util.Collections;
51import java.util.Comparator;
Makoto Onuki31459242016-03-22 11:12:18 -070052import java.util.List;
Makoto Onukibe73a802016-04-15 14:46:35 -070053import java.util.Set;
Makoto Onuki31459242016-03-22 11:12:18 -070054import java.util.function.Predicate;
55
56/**
57 * Package information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070058 * User information used by {@link ShortcutService}.
59 *
60 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070061 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070062class ShortcutPackage extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070063 private static final String TAG = ShortcutService.TAG;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070064 private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
Makoto Onuki31459242016-03-22 11:12:18 -070065
66 static final String TAG_ROOT = "package";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070067 private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras";
68 private static final String TAG_INTENT = "intent";
Makoto Onuki31459242016-03-22 11:12:18 -070069 private static final String TAG_EXTRAS = "extras";
70 private static final String TAG_SHORTCUT = "shortcut";
Makoto Onukib6d35232016-04-04 15:57:17 -070071 private static final String TAG_CATEGORIES = "categories";
Makoto Onuki31459242016-03-22 11:12:18 -070072
73 private static final String ATTR_NAME = "name";
Makoto Onuki31459242016-03-22 11:12:18 -070074 private static final String ATTR_CALL_COUNT = "call-count";
75 private static final String ATTR_LAST_RESET = "last-reset";
76 private static final String ATTR_ID = "id";
77 private static final String ATTR_ACTIVITY = "activity";
78 private static final String ATTR_TITLE = "title";
Makoto Onuki20c95f82016-05-11 16:51:01 -070079 private static final String ATTR_TITLE_RES_ID = "titleid";
Makoto Onuki157b1622016-06-02 16:13:10 -070080 private static final String ATTR_TITLE_RES_NAME = "titlename";
Makoto Onukie3ae7ec2016-03-29 15:45:25 -070081 private static final String ATTR_TEXT = "text";
Makoto Onuki20c95f82016-05-11 16:51:01 -070082 private static final String ATTR_TEXT_RES_ID = "textid";
Makoto Onuki157b1622016-06-02 16:13:10 -070083 private static final String ATTR_TEXT_RES_NAME = "textname";
Makoto Onuki20c95f82016-05-11 16:51:01 -070084 private static final String ATTR_DISABLED_MESSAGE = "dmessage";
85 private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
Makoto Onuki157b1622016-06-02 16:13:10 -070086 private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
Makoto Onukia4f89b12017-10-05 10:37:55 -070087 private static final String ATTR_DISABLED_REASON = "disabled-reason";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070088 private static final String ATTR_INTENT_LEGACY = "intent";
89 private static final String ATTR_INTENT_NO_EXTRA = "intent-base";
Makoto Onuki20c95f82016-05-11 16:51:01 -070090 private static final String ATTR_RANK = "rank";
Makoto Onuki31459242016-03-22 11:12:18 -070091 private static final String ATTR_TIMESTAMP = "timestamp";
92 private static final String ATTR_FLAGS = "flags";
Makoto Onuki157b1622016-06-02 16:13:10 -070093 private static final String ATTR_ICON_RES_ID = "icon-res";
94 private static final String ATTR_ICON_RES_NAME = "icon-resname";
Makoto Onuki31459242016-03-22 11:12:18 -070095 private static final String ATTR_BITMAP_PATH = "bitmap-path";
96
Makoto Onukib6d35232016-04-04 15:57:17 -070097 private static final String NAME_CATEGORIES = "categories";
98
99 private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
100 private static final String ATTR_NAME_XMLUTILS = "name";
101
Makoto Onuki76269922016-07-15 14:58:54 -0700102 private static final String KEY_DYNAMIC = "dynamic";
103 private static final String KEY_MANIFEST = "manifest";
104 private static final String KEY_PINNED = "pinned";
105 private static final String KEY_BITMAPS = "bitmaps";
106 private static final String KEY_BITMAP_BYTES = "bitmapBytes";
107
Makoto Onuki31459242016-03-22 11:12:18 -0700108 /**
109 * All the shortcuts from the package, keyed on IDs.
110 */
111 final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
112
113 /**
Makoto Onuki31459242016-03-22 11:12:18 -0700114 * # of times the package has called rate-limited APIs.
115 */
116 private int mApiCallCount;
117
118 /**
119 * When {@link #mApiCallCount} was reset last time.
120 */
121 private long mLastResetTime;
122
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700123 private final int mPackageUid;
124
125 private long mLastKnownForegroundElapsedTime;
126
Makoto Onukic51b2872016-05-04 15:24:50 -0700127 private ShortcutPackage(ShortcutUser shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700128 int packageUserId, String packageName, ShortcutPackageInfo spi) {
129 super(shortcutUser, packageUserId, packageName,
130 spi != null ? spi : ShortcutPackageInfo.newEmpty());
131
Makoto Onukic51b2872016-05-04 15:24:50 -0700132 mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700133 }
134
Makoto Onukic51b2872016-05-04 15:24:50 -0700135 public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
136 this(shortcutUser, packageUserId, packageName, null);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700137 }
138
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700139 @Override
140 public int getOwnerUserId() {
141 // For packages, always owner user == package user.
142 return getPackageUserId();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700143 }
144
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700145 public int getPackageUid() {
146 return mPackageUid;
147 }
148
Makoto Onuki157b1622016-06-02 16:13:10 -0700149 @Nullable
150 public Resources getPackageResources() {
151 return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
152 getPackageName(), getPackageUserId());
153 }
154
Makoto Onuki50a320e2017-05-31 14:38:42 -0700155 public int getShortcutCount() {
156 return mShortcuts.size();
157 }
158
Makoto Onuki2e210c42016-03-30 08:30:36 -0700159 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700160 protected boolean canRestoreAnyVersion() {
161 return false;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700162 }
163
164 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700165 protected void onRestored(int restoreBlockReason) {
166 // Shortcuts have been restored.
167 // - Unshadow all shortcuts.
168 // - Set disabled reason.
169 // - Disable if needed.
170 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
171 ShortcutInfo si = mShortcuts.valueAt(i);
172 si.clearFlags(ShortcutInfo.FLAG_SHADOW);
173
174 si.setDisabledReason(restoreBlockReason);
175 if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
176 si.addFlags(ShortcutInfo.FLAG_DISABLED);
177 }
178 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700179 // Because some launchers may not have been restored (e.g. allowBackup=false),
180 // we need to re-calculate the pinned shortcuts.
Makoto Onukic51b2872016-05-04 15:24:50 -0700181 refreshPinnedFlags();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700182 }
183
Makoto Onukid99c6f02016-03-28 11:02:54 -0700184 /**
185 * Note this does *not* provide a correct view to the calling launcher.
186 */
Makoto Onuki31459242016-03-22 11:12:18 -0700187 @Nullable
188 public ShortcutInfo findShortcutById(String id) {
189 return mShortcuts.get(id);
190 }
191
Makoto Onukia4f89b12017-10-05 10:37:55 -0700192 public boolean isShortcutExistsAndInvisibleToPublisher(String id) {
193 ShortcutInfo si = findShortcutById(id);
194 return si != null && !si.isVisibleToPublisher();
195 }
196
197 public boolean isShortcutExistsAndVisibleToPublisher(String id) {
198 ShortcutInfo si = findShortcutById(id);
199 return si != null && si.isVisibleToPublisher();
200 }
201
202 private void ensureNotImmutable(@Nullable ShortcutInfo shortcut, boolean ignoreInvisible) {
203 if (shortcut != null && shortcut.isImmutable()
204 && (!ignoreInvisible || shortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700205 throw new IllegalArgumentException(
206 "Manifest shortcut ID=" + shortcut.getId()
207 + " may not be manipulated via APIs");
208 }
209 }
210
Makoto Onukia4f89b12017-10-05 10:37:55 -0700211 public void ensureNotImmutable(@NonNull String id, boolean ignoreInvisible) {
212 ensureNotImmutable(mShortcuts.get(id), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700213 }
214
Makoto Onukia4f89b12017-10-05 10:37:55 -0700215 public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds,
216 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700217 for (int i = shortcutIds.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700218 ensureNotImmutable(shortcutIds.get(i), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700219 }
220 }
221
Makoto Onukia4f89b12017-10-05 10:37:55 -0700222 public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts,
223 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700224 for (int i = shortcuts.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700225 ensureNotImmutable(shortcuts.get(i).getId(), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700226 }
227 }
228
Makoto Onukia4f89b12017-10-05 10:37:55 -0700229 /**
230 * Delete a shortcut by ID. This will *always* remove it even if it's immutable or invisible.
231 */
232 private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) {
Makoto Onuki31459242016-03-22 11:12:18 -0700233 final ShortcutInfo shortcut = mShortcuts.remove(id);
234 if (shortcut != null) {
Makoto Onuki475c3652017-05-08 14:29:03 -0700235 mShortcutUser.mService.removeIconLocked(shortcut);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700236 shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
Makoto Onuki99302b52017-03-29 12:42:26 -0700237 | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki31459242016-03-22 11:12:18 -0700238 }
239 return shortcut;
240 }
241
Makoto Onukia4f89b12017-10-05 10:37:55 -0700242 /**
243 * Force replace a shortcut. If there's already a shortcut with the same ID, it'll be removed,
244 * even if it's invisible.
245 */
246 private void forceReplaceShortcutInner(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700247 final ShortcutService s = mShortcutUser.mService;
248
Makoto Onukia4f89b12017-10-05 10:37:55 -0700249 forceDeleteShortcutInner(newShortcut.getId());
Makoto Onuki157b1622016-06-02 16:13:10 -0700250
251 // Extract Icon and update the icon res ID and the bitmap path.
Makoto Onuki475c3652017-05-08 14:29:03 -0700252 s.saveIconAndFixUpShortcutLocked(newShortcut);
Makoto Onuki157b1622016-06-02 16:13:10 -0700253 s.fixUpShortcutResourceNamesAndValues(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700254 mShortcuts.put(newShortcut.getId(), newShortcut);
255 }
256
257 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700258 * Add a shortcut. If there's already a one with the same ID, it'll be removed, even if it's
259 * invisible.
Makoto Onuki31459242016-03-22 11:12:18 -0700260 *
261 * It checks the max number of dynamic shortcuts.
262 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700263 public void addOrReplaceDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki39686e82016-04-13 18:03:00 -0700264
Makoto Onuki22fcc682016-05-17 14:52:19 -0700265 Preconditions.checkArgument(newShortcut.isEnabled(),
266 "add/setDynamicShortcuts() cannot publish disabled shortcuts");
267
Makoto Onuki99302b52017-03-29 12:42:26 -0700268 newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki31459242016-03-22 11:12:18 -0700269
270 final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
271
272 final boolean wasPinned;
Makoto Onuki31459242016-03-22 11:12:18 -0700273
274 if (oldShortcut == null) {
275 wasPinned = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700276 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700277 // It's an update case.
278 // Make sure the target is updatable. (i.e. should be mutable.)
Makoto Onukia4f89b12017-10-05 10:37:55 -0700279 oldShortcut.ensureUpdatableWith(newShortcut, /*isUpdating=*/ false);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700280
Makoto Onuki31459242016-03-22 11:12:18 -0700281 wasPinned = oldShortcut.isPinned();
Makoto Onuki31459242016-03-22 11:12:18 -0700282 }
283
Makoto Onuki157b1622016-06-02 16:13:10 -0700284 // If it was originally pinned, the new one should be pinned too.
Makoto Onuki31459242016-03-22 11:12:18 -0700285 if (wasPinned) {
286 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
287 }
288
Makoto Onukia4f89b12017-10-05 10:37:55 -0700289 forceReplaceShortcutInner(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700290 }
291
292 /**
293 * Remove all shortcuts that aren't pinned nor dynamic.
294 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700295 private void removeOrphans() {
Makoto Onuki31459242016-03-22 11:12:18 -0700296 ArrayList<String> removeList = null; // Lazily initialize.
297
298 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
299 final ShortcutInfo si = mShortcuts.valueAt(i);
300
Makoto Onuki22fcc682016-05-17 14:52:19 -0700301 if (si.isAlive()) continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700302
303 if (removeList == null) {
304 removeList = new ArrayList<>();
305 }
306 removeList.add(si.getId());
307 }
308 if (removeList != null) {
309 for (int i = removeList.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700310 forceDeleteShortcutInner(removeList.get(i));
Makoto Onuki31459242016-03-22 11:12:18 -0700311 }
312 }
313 }
314
315 /**
316 * Remove all dynamic shortcuts.
317 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700318 public void deleteAllDynamicShortcuts(boolean ignoreInvisible) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700319 final long now = mShortcutUser.mService.injectCurrentTimeMillis();
320
Makoto Onuki22fcc682016-05-17 14:52:19 -0700321 boolean changed = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700322 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700323 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700324 if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700325 changed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700326
327 si.setTimestamp(now);
Makoto Onuki99302b52017-03-29 12:42:26 -0700328 si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700329 si.setRank(0); // It may still be pinned, so clear the rank.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700330 }
Makoto Onuki31459242016-03-22 11:12:18 -0700331 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700332 if (changed) {
333 removeOrphans();
334 }
Makoto Onuki31459242016-03-22 11:12:18 -0700335 }
336
337 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700338 * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
339 * is pinned, it'll remain as a pinned shortcut, and is still enabled.
Makoto Onukib08790c2016-06-23 14:05:46 -0700340 *
341 * @return true if it's actually removed because it wasn't pinned, or false if it's still
342 * pinned.
Makoto Onuki31459242016-03-22 11:12:18 -0700343 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700344 public boolean deleteDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700345 final ShortcutInfo removed = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700346 shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false, ignoreInvisible,
347 ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukib08790c2016-06-23 14:05:46 -0700348 return removed == null;
349 }
350
351 /**
352 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
353 * is pinned, it'll remain as a pinned shortcut, but will be disabled.
354 *
355 * @return true if it's actually removed because it wasn't pinned, or false if it's still
356 * pinned.
357 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700358 private boolean disableDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible,
359 int disabledReason) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700360 final ShortcutInfo disabled = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700361 shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false, ignoreInvisible,
362 disabledReason);
Makoto Onukib08790c2016-06-23 14:05:46 -0700363 return disabled == null;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700364 }
365
Makoto Onuki7001a612016-05-27 13:24:28 -0700366 /**
367 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
368 * is pinned, it'll remain as a pinned shortcut but will be disabled.
369 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700370 public void disableWithId(@NonNull String shortcutId, String disabledMessage,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700371 int disabledMessageResId, boolean overrideImmutable, boolean ignoreInvisible,
372 int disabledReason) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700373 final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700374 overrideImmutable, ignoreInvisible, disabledReason);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700375
376 if (disabled != null) {
377 if (disabledMessage != null) {
378 disabled.setDisabledMessage(disabledMessage);
379 } else if (disabledMessageResId != 0) {
380 disabled.setDisabledMessageResId(disabledMessageResId);
Makoto Onuki157b1622016-06-02 16:13:10 -0700381
382 mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700383 }
384 }
385 }
386
387 @Nullable
388 private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700389 boolean overrideImmutable, boolean ignoreInvisible, int disabledReason) {
390 Preconditions.checkState(
391 (disable == (disabledReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED)),
392 "disable and disabledReason disagree: " + disable + " vs " + disabledReason);
Makoto Onuki31459242016-03-22 11:12:18 -0700393 final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
394
Makoto Onukia4f89b12017-10-05 10:37:55 -0700395 if (oldShortcut == null || !oldShortcut.isEnabled()
396 && (ignoreInvisible && !oldShortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700397 return null; // Doesn't exist or already disabled.
Makoto Onuki31459242016-03-22 11:12:18 -0700398 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700399 if (!overrideImmutable) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700400 ensureNotImmutable(oldShortcut, /*ignoreInvisible=*/ true);
Makoto Onuki31459242016-03-22 11:12:18 -0700401 }
402 if (oldShortcut.isPinned()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700403
404 oldShortcut.setRank(0);
Makoto Onuki99302b52017-03-29 12:42:26 -0700405 oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700406 if (disable) {
407 oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700408 // Do not overwrite the disabled reason if one is alreay set.
409 if (oldShortcut.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
410 oldShortcut.setDisabledReason(disabledReason);
411 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700412 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700413 oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
414
Makoto Onuki255461f2017-01-10 11:47:25 -0800415 // See ShortcutRequestPinProcessor.directPinShortcut().
416 if (mShortcutUser.mService.isDummyMainActivity(oldShortcut.getActivity())) {
417 oldShortcut.setActivity(null);
418 }
419
Makoto Onuki22fcc682016-05-17 14:52:19 -0700420 return oldShortcut;
Makoto Onuki31459242016-03-22 11:12:18 -0700421 } else {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700422 forceDeleteShortcutInner(shortcutId);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700423 return null;
424 }
425 }
426
427 public void enableWithId(@NonNull String shortcutId) {
428 final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
429 if (shortcut != null) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700430 ensureNotImmutable(shortcut, /*ignoreInvisible=*/ true);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700431 shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700432 shortcut.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onuki31459242016-03-22 11:12:18 -0700433 }
434 }
435
Makoto Onukia4f89b12017-10-05 10:37:55 -0700436 public void updateInvisibleShortcutForPinRequestWith(@NonNull ShortcutInfo shortcut) {
437 final ShortcutInfo source = mShortcuts.get(shortcut.getId());
438 Preconditions.checkNotNull(source);
439
440 mShortcutUser.mService.validateShortcutForPinRequest(shortcut);
441
442 shortcut.addFlags(ShortcutInfo.FLAG_PINNED);
443
444 forceReplaceShortcutInner(shortcut);
445
446 adjustRanks();
447 }
448
Makoto Onuki31459242016-03-22 11:12:18 -0700449 /**
450 * Called after a launcher updates the pinned set. For each shortcut in this package,
451 * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it.
452 *
453 * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
454 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700455 public void refreshPinnedFlags() {
Makoto Onuki31459242016-03-22 11:12:18 -0700456 // First, un-pin all shortcuts
457 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
458 mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
459 }
460
461 // Then, for the pinned set for each launcher, set the pin flag one by one.
Makoto Onuki47ed1712017-12-20 14:40:20 +0900462 mShortcutUser.forAllLaunchers(launcherShortcuts -> {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700463 final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
Makoto Onuki2e210c42016-03-30 08:30:36 -0700464 getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700465
466 if (pinned == null || pinned.size() == 0) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700467 return;
Makoto Onuki31459242016-03-22 11:12:18 -0700468 }
469 for (int i = pinned.size() - 1; i >= 0; i--) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700470 final String id = pinned.valueAt(i);
471 final ShortcutInfo si = mShortcuts.get(id);
Makoto Onuki31459242016-03-22 11:12:18 -0700472 if (si == null) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700473 // This happens if a launcher pinned shortcuts from this package, then backup&
474 // restored, but this package doesn't allow backing up.
475 // In that case the launcher ends up having a dangling pinned shortcuts.
476 // That's fine, when the launcher is restored, we'll fix it.
477 continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700478 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700479 si.addFlags(ShortcutInfo.FLAG_PINNED);
Makoto Onuki31459242016-03-22 11:12:18 -0700480 }
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700481 });
Makoto Onuki31459242016-03-22 11:12:18 -0700482
483 // Lastly, remove the ones that are no longer pinned nor dynamic.
Makoto Onukic51b2872016-05-04 15:24:50 -0700484 removeOrphans();
Makoto Onuki31459242016-03-22 11:12:18 -0700485 }
486
487 /**
488 * Number of calls that the caller has made, since the last reset.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700489 *
490 * <p>This takes care of the resetting the counter for foreground apps as well as after
491 * locale changes.
Makoto Onuki31459242016-03-22 11:12:18 -0700492 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700493 public int getApiCallCount() {
Makoto Onukic51b2872016-05-04 15:24:50 -0700494 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700495
496 // Reset the counter if:
497 // - the package is in foreground now.
498 // - the package is *not* in foreground now, but was in foreground at some point
499 // since the previous time it had been.
500 if (s.isUidForegroundLocked(mPackageUid)
501 || mLastKnownForegroundElapsedTime
502 < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) {
503 mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
Makoto Onukic51b2872016-05-04 15:24:50 -0700504 resetRateLimiting();
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700505 }
506
507 // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
508 // but we just can't return 0 at this point, because we may have to update
509 // mLastResetTime.
510
Makoto Onuki31459242016-03-22 11:12:18 -0700511 final long last = s.getLastResetTimeLocked();
512
513 final long now = s.injectCurrentTimeMillis();
514 if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
515 Slog.w(TAG, "Clock rewound");
516 // Clock rewound.
517 mLastResetTime = now;
518 mApiCallCount = 0;
519 return mApiCallCount;
520 }
521
522 // If not reset yet, then reset.
523 if (mLastResetTime < last) {
524 if (ShortcutService.DEBUG) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700525 Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
526 getPackageName(), mLastResetTime, now, last));
Makoto Onuki31459242016-03-22 11:12:18 -0700527 }
528 mApiCallCount = 0;
529 mLastResetTime = last;
530 }
531 return mApiCallCount;
532 }
533
534 /**
535 * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
536 * and return true. Otherwise just return false.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700537 *
538 * <p>This takes care of the resetting the counter for foreground apps as well as after
539 * locale changes, which is done internally by {@link #getApiCallCount}.
Makoto Onuki31459242016-03-22 11:12:18 -0700540 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700541 public boolean tryApiCall() {
542 final ShortcutService s = mShortcutUser.mService;
543
544 if (getApiCallCount() >= s.mMaxUpdatesPerInterval) {
Makoto Onuki31459242016-03-22 11:12:18 -0700545 return false;
546 }
547 mApiCallCount++;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700548 s.scheduleSaveUser(getOwnerUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700549 return true;
550 }
551
Makoto Onukic51b2872016-05-04 15:24:50 -0700552 public void resetRateLimiting() {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700553 if (ShortcutService.DEBUG) {
554 Slog.d(TAG, "resetRateLimiting: " + getPackageName());
555 }
556 if (mApiCallCount > 0) {
557 mApiCallCount = 0;
Makoto Onukic51b2872016-05-04 15:24:50 -0700558 mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700559 }
560 }
561
562 public void resetRateLimitingForCommandLineNoSaving() {
Makoto Onuki31459242016-03-22 11:12:18 -0700563 mApiCallCount = 0;
564 mLastResetTime = 0;
565 }
566
567 /**
568 * Find all shortcuts that match {@code query}.
569 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700570 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700571 @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700572 findAll(result, query, cloneFlag, null, 0, /*getPinnedByAnyLauncher=*/ false);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700573 }
574
575 /**
576 * Find all shortcuts that match {@code query}.
577 *
578 * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
579 * by the calling launcher will not be included in the result, and also "isPinned" will be
580 * adjusted for the caller too.
581 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700582 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onuki31459242016-03-22 11:12:18 -0700583 @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
Makoto Onuki634cecb2017-10-13 17:10:48 -0700584 @Nullable String callingLauncher, int launcherUserId, boolean getPinnedByAnyLauncher) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700585 if (getPackageInfo().isShadow()) {
586 // Restored and the app not installed yet, so don't return any.
587 return;
588 }
Makoto Onuki31459242016-03-22 11:12:18 -0700589
Makoto Onukic51b2872016-05-04 15:24:50 -0700590 final ShortcutService s = mShortcutUser.mService;
591
Makoto Onuki31459242016-03-22 11:12:18 -0700592 // Set of pinned shortcuts by the calling launcher.
593 final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
Makoto Onuki2e210c42016-03-30 08:30:36 -0700594 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
595 .getPinnedShortcutIds(getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700596
597 for (int i = 0; i < mShortcuts.size(); i++) {
598 final ShortcutInfo si = mShortcuts.valueAt(i);
599
Makoto Onuki22fcc682016-05-17 14:52:19 -0700600 // Need to adjust PINNED flag depending on the caller.
601 // Basically if the caller is a launcher (callingLauncher != null) and the launcher
602 // isn't pinning it, then we need to clear PINNED for this caller.
Makoto Onuki31459242016-03-22 11:12:18 -0700603 final boolean isPinnedByCaller = (callingLauncher == null)
604 || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700605
Makoto Onuki634cecb2017-10-13 17:10:48 -0700606 if (!getPinnedByAnyLauncher) {
607 if (si.isFloating()) {
608 if (!isPinnedByCaller) {
609 continue;
610 }
Makoto Onuki31459242016-03-22 11:12:18 -0700611 }
612 }
613 final ShortcutInfo clone = si.clone(cloneFlag);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700614
Makoto Onuki31459242016-03-22 11:12:18 -0700615 // Fix up isPinned for the caller. Note we need to do it before the "test" callback,
616 // since it may check isPinned.
Makoto Onuki35559d62017-11-06 16:26:32 -0800617 // However, if getPinnedByAnyLauncher is set, we do it after the test.
618 if (!getPinnedByAnyLauncher) {
619 if (!isPinnedByCaller) {
620 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
621 }
Makoto Onuki31459242016-03-22 11:12:18 -0700622 }
623 if (query == null || query.test(clone)) {
Makoto Onuki35559d62017-11-06 16:26:32 -0800624 if (!isPinnedByCaller) {
625 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
626 }
Makoto Onuki31459242016-03-22 11:12:18 -0700627 result.add(clone);
628 }
629 }
630 }
631
632 public void resetThrottling() {
633 mApiCallCount = 0;
634 }
635
Makoto Onuki39686e82016-04-13 18:03:00 -0700636 /**
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700637 * Return the filenames (excluding path names) of icon bitmap files from this package.
638 */
639 public ArraySet<String> getUsedBitmapFiles() {
640 final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
641
642 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
643 final ShortcutInfo si = mShortcuts.valueAt(i);
644 if (si.getBitmapPath() != null) {
645 usedFiles.add(getFileName(si.getBitmapPath()));
646 }
647 }
648 return usedFiles;
649 }
650
651 private static String getFileName(@NonNull String path) {
652 final int sep = path.lastIndexOf(File.separatorChar);
653 if (sep == -1) {
654 return path;
655 } else {
656 return path.substring(sep + 1);
657 }
658 }
659
660 /**
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700661 * @return false if any of the target activities are no longer enabled.
662 */
663 private boolean areAllActivitiesStillEnabled() {
664 if (mShortcuts.size() == 0) {
665 return true;
666 }
667 final ShortcutService s = mShortcutUser.mService;
668
669 // Normally the number of target activities is 1 or so, so no need to use a complex
670 // structure like a set.
671 final ArrayList<ComponentName> checked = new ArrayList<>(4);
672
673 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
674 final ShortcutInfo si = mShortcuts.valueAt(i);
675 final ComponentName activity = si.getActivity();
676
677 if (checked.contains(activity)) {
678 continue; // Already checked.
679 }
680 checked.add(activity);
681
Makoto Onuki40dc2112017-10-18 12:52:45 -0700682 if ((activity != null)
683 && !s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700684 return false;
685 }
686 }
687 return true;
688 }
689
690 /**
691 * Called when the package may be added or updated, or its activities may be disabled, and
692 * if so, rescan the package and do the necessary stuff.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700693 *
694 * Add case:
695 * - Publish manifest shortcuts.
696 *
697 * Update case:
698 * - Re-publish manifest shortcuts.
699 * - If there are shortcuts with resources (icons or strings), update their timestamps.
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700700 * - Disable shortcuts whose target activities are disabled.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700701 *
702 * @return TRUE if any shortcuts have been changed.
Makoto Onuki39686e82016-04-13 18:03:00 -0700703 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700704 public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) {
705 final ShortcutService s = mShortcutUser.mService;
706 final long start = s.injectElapsedRealtime();
Makoto Onuki39686e82016-04-13 18:03:00 -0700707
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700708 final PackageInfo pi;
709 try {
710 pi = mShortcutUser.mService.getPackageInfo(
711 getPackageName(), getPackageUserId());
712 if (pi == null) {
713 return false; // Shouldn't happen.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700714 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700715
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700716 if (!isNewApp && !forceRescan) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700717 // Return if the package hasn't changed, ie:
718 // - version code hasn't change
719 // - lastUpdateTime hasn't change
720 // - all target activities are still enabled.
Makoto Onuki33663282016-08-22 16:19:04 -0700721
722 // Note, system apps timestamps do *not* change after OTAs. (But they do
723 // after an adb sync or a local flash.)
724 // This means if a system app's version code doesn't change on an OTA,
725 // we don't notice it's updated. But that's fine since their version code *should*
726 // really change on OTAs.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700727 if ((getPackageInfo().getVersionCode() == pi.getLongVersionCode())
Makoto Onuki64183d52016-08-08 14:11:34 -0700728 && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime)
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700729 && areAllActivitiesStillEnabled()) {
730 return false;
731 }
732 }
733 } finally {
734 s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700735 }
736
737 // Now prepare to publish manifest shortcuts.
738 List<ShortcutInfo> newManifestShortcutList = null;
739 try {
740 newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
741 getPackageName(), getPackageUserId());
742 } catch (IOException|XmlPullParserException e) {
743 Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
744 }
745 final int manifestShortcutSize = newManifestShortcutList == null ? 0
746 : newManifestShortcutList.size();
747 if (ShortcutService.DEBUG) {
748 Slog.d(TAG, String.format("Package %s has %d manifest shortcut(s)",
749 getPackageName(), manifestShortcutSize));
750 }
751 if (isNewApp && (manifestShortcutSize == 0)) {
752 // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
753
754 // If it's an update, then it may already have manifest shortcuts, which need to be
755 // disabled.
756 return false;
757 }
758 if (ShortcutService.DEBUG) {
759 Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
760 (isNewApp ? "added" : "updated"),
Dianne Hackborn3accca02013-09-20 09:32:11 -0700761 getPackageInfo().getVersionCode(), pi.getLongVersionCode()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700762 }
763
Makoto Onukia4f89b12017-10-05 10:37:55 -0700764 getPackageInfo().updateFromPackageInfo(pi);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700765 final long newVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700766
767 // See if there are any shortcuts that were prevented restoring because the app was of a
768 // lower version, and re-enable them.
769 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
770 final ShortcutInfo si = mShortcuts.valueAt(i);
771 if (si.getDisabledReason() != ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
772 continue;
773 }
774 if (getPackageInfo().getBackupSourceVersionCode() > newVersionCode) {
775 if (ShortcutService.DEBUG) {
776 Slog.d(TAG, String.format("Shortcut %s require version %s, still not restored.",
777 si.getId(), getPackageInfo().getBackupSourceVersionCode()));
778 }
779 continue;
780 }
781 Slog.i(TAG, String.format("Restoring shortcut: %s", si.getId()));
782 si.clearFlags(ShortcutInfo.FLAG_DISABLED);
783 si.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
784 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700785
Makoto Onuki22fcc682016-05-17 14:52:19 -0700786 // For existing shortcuts, update timestamps if they have any resources.
Makoto Onukib08790c2016-06-23 14:05:46 -0700787 // Also check if shortcuts' activities are still main activities. Otherwise, disable them.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700788 if (!isNewApp) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700789 Resources publisherRes = null;
790
Makoto Onuki22fcc682016-05-17 14:52:19 -0700791 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
792 final ShortcutInfo si = mShortcuts.valueAt(i);
793
Makoto Onuki2d895c32016-12-02 15:48:40 -0800794 // Disable dynamic shortcuts whose target activity is gone.
Makoto Onukib08790c2016-06-23 14:05:46 -0700795 if (si.isDynamic()) {
Makoto Onuki34145532017-03-14 17:58:36 -0700796 if (si.getActivity() == null) {
797 // Note if it's dynamic, it must have a target activity, but b/36228253.
798 s.wtf("null activity detected.");
799 // TODO Maybe remove it?
800 } else if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700801 Slog.w(TAG, String.format(
802 "%s is no longer main activity. Disabling shorcut %s.",
803 getPackageName(), si.getId()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700804 if (disableDynamicWithId(si.getId(), /*ignoreInvisible*/ false,
805 ShortcutInfo.DISABLED_REASON_APP_CHANGED)) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700806 continue; // Actually removed.
807 }
808 // Still pinned, so fall-through and possibly update the resources.
809 }
Makoto Onukib08790c2016-06-23 14:05:46 -0700810 }
811
Makoto Onuki22fcc682016-05-17 14:52:19 -0700812 if (si.hasAnyResources()) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700813 if (!si.isOriginallyFromManifest()) {
814 if (publisherRes == null) {
815 publisherRes = getPackageResources();
816 if (publisherRes == null) {
817 break; // Resources couldn't be loaded.
818 }
819 }
820
821 // If this shortcut is not from a manifest, then update all resource IDs
822 // from resource names. (We don't allow resource strings for
823 // non-manifest at the moment, but icons can still be resources.)
824 si.lookupAndFillInResourceIds(publisherRes);
825 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700826 si.setTimestamp(s.injectCurrentTimeMillis());
827 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700828 }
829 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700830
831 // (Re-)publish manifest shortcut.
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700832 publishManifestShortcuts(newManifestShortcutList);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700833
Makoto Onuki7001a612016-05-27 13:24:28 -0700834 if (newManifestShortcutList != null) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700835 pushOutExcessShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -0700836 }
837
Makoto Onukidf6da042016-06-16 09:51:40 -0700838 s.verifyStates();
839
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700840 // This will send a notification to the launcher, and also save .
841 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
842 return true; // true means changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700843 }
844
845 private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
846 if (ShortcutService.DEBUG) {
847 Slog.d(TAG, String.format(
848 "Package %s: publishing manifest shortcuts", getPackageName()));
849 }
850 boolean changed = false;
851
Makoto Onuki22fcc682016-05-17 14:52:19 -0700852 // Keep the previous IDs.
853 ArraySet<String> toDisableList = null;
854 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
855 final ShortcutInfo si = mShortcuts.valueAt(i);
856
857 if (si.isManifestShortcut()) {
858 if (toDisableList == null) {
859 toDisableList = new ArraySet<>();
860 }
861 toDisableList.add(si.getId());
862 }
863 }
864
865 // Publish new ones.
866 if (newManifestShortcutList != null) {
867 final int newListSize = newManifestShortcutList.size();
868
869 for (int i = 0; i < newListSize; i++) {
870 changed = true;
871
872 final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
873 final boolean newDisabled = !newShortcut.isEnabled();
874
Makoto Onuki7001a612016-05-27 13:24:28 -0700875 final String id = newShortcut.getId();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700876 final ShortcutInfo oldShortcut = mShortcuts.get(id);
877
878 boolean wasPinned = false;
879
880 if (oldShortcut != null) {
881 if (!oldShortcut.isOriginallyFromManifest()) {
882 Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
883 + " exists but is not from AndroidManifest.xml, not updating.");
884 continue;
885 }
886 // Take over the pinned flag.
887 if (oldShortcut.isPinned()) {
888 wasPinned = true;
889 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
890 }
891 }
892 if (newDisabled && !wasPinned) {
893 // If the shortcut is disabled, and it was *not* pinned, then this
894 // just doesn't have to be published.
895 // Just keep it in toDisableList, so the previous one would be removed.
896 continue;
897 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700898
899 // Note even if enabled=false, we still need to update all fields, so do it
900 // regardless.
Makoto Onukia4f89b12017-10-05 10:37:55 -0700901 forceReplaceShortcutInner(newShortcut); // This will clean up the old one too.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700902
903 if (!newDisabled && toDisableList != null) {
904 // Still alive, don't remove.
905 toDisableList.remove(id);
906 }
907 }
908 }
909
910 // Disable the previous manifest shortcuts that are no longer in the manifest.
911 if (toDisableList != null) {
912 if (ShortcutService.DEBUG) {
913 Slog.d(TAG, String.format(
914 "Package %s: disabling %d stale shortcuts", getPackageName(),
915 toDisableList.size()));
916 }
917 for (int i = toDisableList.size() - 1; i >= 0; i--) {
918 changed = true;
919
920 final String id = toDisableList.valueAt(i);
921
922 disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700923 /* overrideImmutable=*/ true, /*ignoreInvisible=*/ false,
924 ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700925 }
926 removeOrphans();
927 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700928 adjustRanks();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700929 return changed;
Makoto Onuki39686e82016-04-13 18:03:00 -0700930 }
931
Makoto Onuki7001a612016-05-27 13:24:28 -0700932 /**
933 * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
934 * If too many, we'll remove the dynamic with the lowest ranks.
935 */
936 private boolean pushOutExcessShortcuts() {
937 final ShortcutService service = mShortcutUser.mService;
938 final int maxShortcuts = service.getMaxActivityShortcuts();
939
940 boolean changed = false;
941
942 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
943 sortShortcutsToActivities();
944 for (int outer = all.size() - 1; outer >= 0; outer--) {
945 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
946 if (list.size() <= maxShortcuts) {
947 continue;
948 }
949 // Sort by isManifestShortcut() and getRank().
950 Collections.sort(list, mShortcutTypeAndRankComparator);
951
952 // Keep [0 .. max), and remove (as dynamic) [max .. size)
953 for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
954 final ShortcutInfo shortcut = list.get(inner);
955
956 if (shortcut.isManifestShortcut()) {
957 // This shouldn't happen -- excess shortcuts should all be non-manifest.
958 // But just in case.
959 service.wtf("Found manifest shortcuts in excess list.");
960 continue;
961 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700962 deleteDynamicWithId(shortcut.getId(), /*ignoreInvisible=*/ true);
Makoto Onuki7001a612016-05-27 13:24:28 -0700963 }
964 }
Makoto Onuki7001a612016-05-27 13:24:28 -0700965
966 return changed;
967 }
968
969 /**
970 * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before
971 * non-manifest shortcuts, then sort by rank.
972 *
973 * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
974 * manifest shortcuts than before and as a result we need to remove some of the dynamic
975 * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with
976 * the last ones.
977 *
978 * (Note the number of manifest shortcuts is always <= the max number, because if there are
979 * more, ShortcutParser would ignore the rest.)
980 */
981 final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
982 ShortcutInfo b) -> {
983 if (a.isManifestShortcut() && !b.isManifestShortcut()) {
984 return -1;
985 }
986 if (!a.isManifestShortcut() && b.isManifestShortcut()) {
987 return 1;
988 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700989 return Integer.compare(a.getRank(), b.getRank());
Makoto Onuki7001a612016-05-27 13:24:28 -0700990 };
991
992 /**
993 * Build a list of shortcuts for each target activity and return as a map. The result won't
994 * contain "floating" shortcuts because they don't belong on any activities.
995 */
996 private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
Makoto Onuki7001a612016-05-27 13:24:28 -0700997 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
998 = new ArrayMap<>();
999 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1000 final ShortcutInfo si = mShortcuts.valueAt(i);
1001 if (si.isFloating()) {
1002 continue; // Ignore floating shortcuts, which are not tied to any activities.
1003 }
1004
1005 final ComponentName activity = si.getActivity();
Makoto Onuki34145532017-03-14 17:58:36 -07001006 if (activity == null) {
1007 mShortcutUser.mService.wtf("null activity detected.");
1008 continue;
1009 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001010
1011 ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
1012 if (list == null) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001013 list = new ArrayList<>();
Makoto Onuki7001a612016-05-27 13:24:28 -07001014 activitiesToShortcuts.put(activity, list);
1015 }
1016 list.add(si);
1017 }
1018 return activitiesToShortcuts;
1019 }
1020
1021 /** Used by {@link #enforceShortcutCountsBeforeOperation} */
1022 private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
1023 ComponentName cn, int increment) {
1024 Integer oldValue = counts.get(cn);
1025 if (oldValue == null) {
1026 oldValue = 0;
1027 }
1028
1029 counts.put(cn, oldValue + increment);
1030 }
1031
1032 /**
1033 * Called by
1034 * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
1035 * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
1036 * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
1037 * the operation to make sure the operation wouldn't result in the target activities having
1038 * more than the allowed number of dynamic/manifest shortcuts.
1039 *
1040 * @param newList shortcut list passed to set, add or updateShortcuts().
1041 * @param operation add, set or update.
1042 * @throws IllegalArgumentException if the operation would result in going over the max
1043 * shortcut count for any activity.
1044 */
1045 public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
1046 @ShortcutOperation int operation) {
1047 final ShortcutService service = mShortcutUser.mService;
1048
1049 // Current # of dynamic / manifest shortcuts for each activity.
1050 // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
1051 // anyway.)
1052 final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
1053 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1054 final ShortcutInfo shortcut = mShortcuts.valueAt(i);
1055
1056 if (shortcut.isManifestShortcut()) {
1057 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1058 } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
1059 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1060 }
1061 }
1062
1063 for (int i = newList.size() - 1; i >= 0; i--) {
1064 final ShortcutInfo newShortcut = newList.get(i);
1065 final ComponentName newActivity = newShortcut.getActivity();
1066 if (newActivity == null) {
1067 if (operation != ShortcutService.OPERATION_UPDATE) {
Makoto Onukib08790c2016-06-23 14:05:46 -07001068 service.wtf("Activity must not be null at this point");
1069 continue; // Just ignore this invalid case.
Makoto Onuki7001a612016-05-27 13:24:28 -07001070 }
1071 continue; // Activity can be null for update.
1072 }
1073
1074 final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
1075 if (original == null) {
1076 if (operation == ShortcutService.OPERATION_UPDATE) {
1077 continue; // When updating, ignore if there's no target.
1078 }
1079 // Add() or set(), and there's no existing shortcut with the same ID. We're
1080 // simply publishing (as opposed to updating) this shortcut, so just +1.
1081 incrementCountForActivity(counts, newActivity, 1);
1082 continue;
1083 }
1084 if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
1085 // Updating floating shortcuts doesn't affect the count, so ignore.
1086 continue;
1087 }
1088
1089 // If it's add() or update(), then need to decrement for the previous activity.
1090 // Skip it for set() since it's already been taken care of by not counting the original
1091 // dynamic shortcuts in the first loop.
1092 if (operation != ShortcutService.OPERATION_SET) {
1093 final ComponentName oldActivity = original.getActivity();
1094 if (!original.isFloating()) {
1095 incrementCountForActivity(counts, oldActivity, -1);
1096 }
1097 }
1098 incrementCountForActivity(counts, newActivity, 1);
1099 }
1100
1101 // Then make sure none of the activities have more than the max number of shortcuts.
1102 for (int i = counts.size() - 1; i >= 0; i--) {
1103 service.enforceMaxActivityShortcuts(counts.valueAt(i));
1104 }
1105 }
1106
Makoto Onuki157b1622016-06-02 16:13:10 -07001107 /**
1108 * For all the text fields, refresh the string values if they're from resources.
1109 */
1110 public void resolveResourceStrings() {
1111 final ShortcutService s = mShortcutUser.mService;
1112 boolean changed = false;
1113
1114 Resources publisherRes = null;
1115 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1116 final ShortcutInfo si = mShortcuts.valueAt(i);
1117
1118 if (si.hasStringResources()) {
1119 changed = true;
1120
1121 if (publisherRes == null) {
1122 publisherRes = getPackageResources();
1123 if (publisherRes == null) {
1124 break; // Resources couldn't be loaded.
1125 }
1126 }
1127
1128 si.resolveResourceStrings(publisherRes);
1129 si.setTimestamp(s.injectCurrentTimeMillis());
1130 }
1131 }
1132 if (changed) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001133 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001134 }
1135 }
1136
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001137 /** Clears the implicit ranks for all shortcuts. */
1138 public void clearAllImplicitRanks() {
1139 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1140 final ShortcutInfo si = mShortcuts.valueAt(i);
1141 si.clearImplicitRankAndRankChangedFlag();
1142 }
1143 }
1144
1145 /**
1146 * Used to sort shortcuts for rank auto-adjusting.
1147 */
1148 final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
1149 // First, sort by rank.
1150 int ret = Integer.compare(a.getRank(), b.getRank());
1151 if (ret != 0) {
1152 return ret;
1153 }
1154 // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
1155 // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
1156 // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
1157 // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
1158 // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
1159 if (a.isRankChanged() != b.isRankChanged()) {
1160 return a.isRankChanged() ? -1 : 1;
1161 }
1162 // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
1163 // they're passed to the API.
1164 ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
1165 if (ret != 0) {
1166 return ret;
1167 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001168 // If they're still tie, just sort by their IDs.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001169 // This may happen with updateShortcuts() -- see
1170 // the testUpdateShortcuts_noManifestShortcuts() test.
1171 return a.getId().compareTo(b.getId());
1172 };
1173
1174 /**
1175 * Re-calculate the ranks for all shortcuts.
1176 */
1177 public void adjustRanks() {
1178 final ShortcutService s = mShortcutUser.mService;
1179 final long now = s.injectCurrentTimeMillis();
1180
1181 // First, clear ranks for floating shortcuts.
1182 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1183 final ShortcutInfo si = mShortcuts.valueAt(i);
1184 if (si.isFloating()) {
1185 if (si.getRank() != 0) {
1186 si.setTimestamp(now);
1187 si.setRank(0);
1188 }
1189 }
1190 }
1191
1192 // Then adjust ranks. Ranks are unique for each activity, so we first need to sort
1193 // shortcuts to each activity.
1194 // Then sort the shortcuts within each activity with mShortcutRankComparator, and
1195 // assign ranks from 0.
1196 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1197 sortShortcutsToActivities();
1198 for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
1199 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1200
1201 // Sort by ranks and other signals.
1202 Collections.sort(list, mShortcutRankComparator);
1203
1204 int rank = 0;
1205
1206 final int size = list.size();
1207 for (int i = 0; i < size; i++) {
1208 final ShortcutInfo si = list.get(i);
1209 if (si.isManifestShortcut()) {
1210 // Don't adjust ranks for manifest shortcuts.
1211 continue;
1212 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001213 // At this point, it must be dynamic.
1214 if (!si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001215 s.wtf("Non-dynamic shortcut found.");
1216 continue;
1217 }
1218 final int thisRank = rank++;
1219 if (si.getRank() != thisRank) {
1220 si.setTimestamp(now);
1221 si.setRank(thisRank);
1222 }
1223 }
1224 }
1225 }
1226
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07001227 /** @return true if there's any shortcuts that are not manifest shortcuts. */
1228 public boolean hasNonManifestShortcuts() {
1229 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1230 final ShortcutInfo si = mShortcuts.valueAt(i);
1231 if (!si.isDeclaredInManifest()) {
1232 return true;
1233 }
1234 }
1235 return false;
1236 }
1237
Makoto Onuki20b82212017-10-04 15:03:50 -07001238 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -07001239 pw.println();
1240
1241 pw.print(prefix);
1242 pw.print("Package: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001243 pw.print(getPackageName());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001244 pw.print(" UID: ");
1245 pw.print(mPackageUid);
Makoto Onuki31459242016-03-22 11:12:18 -07001246 pw.println();
1247
1248 pw.print(prefix);
1249 pw.print(" ");
1250 pw.print("Calls: ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001251 pw.print(getApiCallCount());
Makoto Onuki31459242016-03-22 11:12:18 -07001252 pw.println();
1253
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001254 // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
1255 pw.print(prefix);
1256 pw.print(" ");
1257 pw.print("Last known FG: ");
1258 pw.print(mLastKnownForegroundElapsedTime);
1259 pw.println();
1260
Makoto Onuki31459242016-03-22 11:12:18 -07001261 // This should be after getApiCallCount(), which may update it.
1262 pw.print(prefix);
1263 pw.print(" ");
1264 pw.print("Last reset: [");
1265 pw.print(mLastResetTime);
1266 pw.print("] ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001267 pw.print(ShortcutService.formatTime(mLastResetTime));
Makoto Onuki31459242016-03-22 11:12:18 -07001268 pw.println();
1269
Makoto Onukic51b2872016-05-04 15:24:50 -07001270 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001271 pw.println();
1272
Makoto Onuki39686e82016-04-13 18:03:00 -07001273 pw.print(prefix);
1274 pw.println(" Shortcuts:");
Makoto Onuki31459242016-03-22 11:12:18 -07001275 long totalBitmapSize = 0;
1276 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1277 final int size = shortcuts.size();
1278 for (int i = 0; i < size; i++) {
1279 final ShortcutInfo si = shortcuts.valueAt(i);
Makoto Onuki6208c672017-10-02 16:20:35 -07001280 pw.println(si.toDumpString(prefix + " "));
Makoto Onuki31459242016-03-22 11:12:18 -07001281 if (si.getBitmapPath() != null) {
1282 final long len = new File(si.getBitmapPath()).length();
Makoto Onuki39686e82016-04-13 18:03:00 -07001283 pw.print(prefix);
1284 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001285 pw.print("bitmap size=");
1286 pw.println(len);
1287
1288 totalBitmapSize += len;
1289 }
1290 }
1291 pw.print(prefix);
1292 pw.print(" ");
1293 pw.print("Total bitmap size: ");
1294 pw.print(totalBitmapSize);
1295 pw.print(" (");
Makoto Onukic51b2872016-05-04 15:24:50 -07001296 pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
Makoto Onuki31459242016-03-22 11:12:18 -07001297 pw.println(")");
1298 }
1299
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001300 @Override
Makoto Onuki76269922016-07-15 14:58:54 -07001301 public JSONObject dumpCheckin(boolean clear) throws JSONException {
1302 final JSONObject result = super.dumpCheckin(clear);
1303
1304 int numDynamic = 0;
1305 int numPinned = 0;
1306 int numManifest = 0;
1307 int numBitmaps = 0;
1308 long totalBitmapSize = 0;
1309
1310 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1311 final int size = shortcuts.size();
1312 for (int i = 0; i < size; i++) {
1313 final ShortcutInfo si = shortcuts.valueAt(i);
1314
1315 if (si.isDynamic()) numDynamic++;
1316 if (si.isDeclaredInManifest()) numManifest++;
1317 if (si.isPinned()) numPinned++;
1318
1319 if (si.getBitmapPath() != null) {
1320 numBitmaps++;
1321 totalBitmapSize += new File(si.getBitmapPath()).length();
1322 }
1323 }
1324
1325 result.put(KEY_DYNAMIC, numDynamic);
1326 result.put(KEY_MANIFEST, numManifest);
1327 result.put(KEY_PINNED, numPinned);
1328 result.put(KEY_BITMAPS, numBitmaps);
1329 result.put(KEY_BITMAP_BYTES, totalBitmapSize);
1330
1331 // TODO Log update frequency too.
1332
1333 return result;
1334 }
1335
1336 @Override
Makoto Onuki0acbb142016-03-22 17:02:57 -07001337 public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
1338 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001339 final int size = mShortcuts.size();
1340
1341 if (size == 0 && mApiCallCount == 0) {
1342 return; // nothing to write.
1343 }
1344
1345 out.startTag(null, TAG_ROOT);
1346
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001347 ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
Makoto Onuki31459242016-03-22 11:12:18 -07001348 ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
1349 ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001350 getPackageInfo().saveToXml(out, forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001351
1352 for (int j = 0; j < size; j++) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001353 saveShortcut(out, mShortcuts.valueAt(j), forBackup,
1354 getPackageInfo().isBackupAllowed());
Makoto Onuki31459242016-03-22 11:12:18 -07001355 }
1356
1357 out.endTag(null, TAG_ROOT);
1358 }
1359
Makoto Onukia4f89b12017-10-05 10:37:55 -07001360 private void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup,
1361 boolean appSupportsBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001362 throws IOException, XmlPullParserException {
Makoto Onuki475c3652017-05-08 14:29:03 -07001363
1364 final ShortcutService s = mShortcutUser.mService;
1365
Makoto Onuki0acbb142016-03-22 17:02:57 -07001366 if (forBackup) {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001367 if (!(si.isPinned() && si.isEnabled())) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001368 // We only backup pinned shortcuts that are enabled.
1369 // Note, this means, shortcuts that are restored but are blocked restore, e.g. due
1370 // to a lower version code, will not be ported to a new device.
1371 return;
Makoto Onuki0acbb142016-03-22 17:02:57 -07001372 }
1373 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001374 final boolean shouldBackupDetails =
1375 !forBackup // It's not backup
1376 || appSupportsBackup; // Or, it's a backup and app supports backup.
1377
Makoto Onuki475c3652017-05-08 14:29:03 -07001378 // Note: at this point no shortcuts should have bitmaps pending save, but if they do,
1379 // just remove the bitmap.
1380 if (si.isIconPendingSave()) {
1381 s.removeIconLocked(si);
1382 }
Makoto Onuki31459242016-03-22 11:12:18 -07001383 out.startTag(null, TAG_SHORTCUT);
1384 ShortcutService.writeAttr(out, ATTR_ID, si.getId());
1385 // writeAttr(out, "package", si.getPackageName()); // not needed
Makoto Onuki22fcc682016-05-17 14:52:19 -07001386 ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
Makoto Onuki31459242016-03-22 11:12:18 -07001387 // writeAttr(out, "icon", si.getIcon()); // We don't save it.
1388 ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001389 ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001390 ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001391 ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001392 ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001393 ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
Makoto Onukia4f89b12017-10-05 10:37:55 -07001394 if (shouldBackupDetails) {
1395 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
1396 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
1397 si.getDisabledMessageResourceId());
1398 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
1399 si.getDisabledMessageResName());
1400 }
1401 ShortcutService.writeAttr(out, ATTR_DISABLED_REASON, si.getDisabledReason());
Makoto Onuki31459242016-03-22 11:12:18 -07001402 ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
1403 si.getLastChangedTimestamp());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001404 if (forBackup) {
1405 // Don't write icon information. Also drop the dynamic flag.
Makoto Onukia4f89b12017-10-05 10:37:55 -07001406
1407 int flags = si.getFlags() &
1408 ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
Makoto Onuki475c3652017-05-08 14:29:03 -07001409 | ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE
Makoto Onukia4f89b12017-10-05 10:37:55 -07001410 | ShortcutInfo.FLAG_DYNAMIC);
1411 ShortcutService.writeAttr(out, ATTR_FLAGS, flags);
1412
1413 // Set the publisher version code at every backup.
Dianne Hackborn3accca02013-09-20 09:32:11 -07001414 final long packageVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -07001415 if (packageVersionCode == 0) {
1416 s.wtf("Package version code should be available at this point.");
1417 // However, 0 is a valid version code, so we just go ahead with it...
1418 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001419 } else {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001420 // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
1421 // as dynamic.
1422 ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
1423
Makoto Onuki0acbb142016-03-22 17:02:57 -07001424 ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
Makoto Onuki157b1622016-06-02 16:13:10 -07001425 ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
1426 ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001427 ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
1428 }
Makoto Onuki31459242016-03-22 11:12:18 -07001429
Makoto Onukia4f89b12017-10-05 10:37:55 -07001430 if (shouldBackupDetails) {
1431 {
1432 final Set<String> cat = si.getCategories();
1433 if (cat != null && cat.size() > 0) {
1434 out.startTag(null, TAG_CATEGORIES);
1435 XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
1436 NAME_CATEGORIES, out);
1437 out.endTag(null, TAG_CATEGORIES);
1438 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001439 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001440 final Intent[] intentsNoExtras = si.getIntentsNoExtras();
1441 final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases();
1442 final int numIntents = intentsNoExtras.length;
1443 for (int i = 0; i < numIntents; i++) {
1444 out.startTag(null, TAG_INTENT);
1445 ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]);
1446 ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]);
1447 out.endTag(null, TAG_INTENT);
1448 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001449
Makoto Onukia4f89b12017-10-05 10:37:55 -07001450 ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
1451 }
Hakan Seyalioglu58fc95d2016-12-13 15:23:22 -08001452
Makoto Onuki31459242016-03-22 11:12:18 -07001453 out.endTag(null, TAG_SHORTCUT);
1454 }
1455
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001456 public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
1457 XmlPullParser parser, boolean fromBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001458 throws IOException, XmlPullParserException {
1459
1460 final String packageName = ShortcutService.parseStringAttribute(parser,
1461 ATTR_NAME);
1462
Makoto Onukic51b2872016-05-04 15:24:50 -07001463 final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001464 shortcutUser.getUserId(), packageName);
Makoto Onuki31459242016-03-22 11:12:18 -07001465
Makoto Onuki31459242016-03-22 11:12:18 -07001466 ret.mApiCallCount =
1467 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
1468 ret.mLastResetTime =
1469 ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
1470
Makoto Onukia4f89b12017-10-05 10:37:55 -07001471
Makoto Onuki31459242016-03-22 11:12:18 -07001472 final int outerDepth = parser.getDepth();
1473 int type;
1474 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1475 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1476 if (type != XmlPullParser.START_TAG) {
1477 continue;
1478 }
1479 final int depth = parser.getDepth();
1480 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001481 if (depth == outerDepth + 1) {
1482 switch (tag) {
1483 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -07001484 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001485
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001486 continue;
1487 case TAG_SHORTCUT:
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001488 final ShortcutInfo si = parseShortcut(parser, packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001489 shortcutUser.getUserId(), fromBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001490
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001491 // Don't use addShortcut(), we don't need to save the icon.
1492 ret.mShortcuts.put(si.getId(), si);
1493 continue;
1494 }
Makoto Onuki31459242016-03-22 11:12:18 -07001495 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001496 ShortcutService.warnForInvalidTag(depth, tag);
1497 }
Makoto Onuki31459242016-03-22 11:12:18 -07001498 return ret;
1499 }
1500
Makoto Onukiabe84422016-04-07 09:41:19 -07001501 private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001502 @UserIdInt int userId, boolean fromBackup)
1503 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001504 String id;
1505 ComponentName activityComponent;
1506 // Icon icon;
1507 String title;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001508 int titleResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001509 String titleResName;
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001510 String text;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001511 int textResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001512 String textResName;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001513 String disabledMessage;
1514 int disabledMessageResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001515 String disabledMessageResName;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001516 int disabledReason;
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001517 Intent intentLegacy;
1518 PersistableBundle intentPersistableExtrasLegacy = null;
1519 ArrayList<Intent> intents = new ArrayList<>();
Makoto Onuki20c95f82016-05-11 16:51:01 -07001520 int rank;
Makoto Onuki31459242016-03-22 11:12:18 -07001521 PersistableBundle extras = null;
1522 long lastChangedTimestamp;
1523 int flags;
Makoto Onuki157b1622016-06-02 16:13:10 -07001524 int iconResId;
1525 String iconResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001526 String bitmapPath;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001527 int backupVersionCode;
Makoto Onukibe73a802016-04-15 14:46:35 -07001528 ArraySet<String> categories = null;
Makoto Onuki31459242016-03-22 11:12:18 -07001529
1530 id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
1531 activityComponent = ShortcutService.parseComponentNameAttribute(parser,
1532 ATTR_ACTIVITY);
1533 title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001534 titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001535 titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001536 text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001537 textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001538 textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001539 disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
1540 disabledMessageResId = ShortcutService.parseIntAttribute(parser,
1541 ATTR_DISABLED_MESSAGE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001542 disabledMessageResName = ShortcutService.parseStringAttribute(parser,
1543 ATTR_DISABLED_MESSAGE_RES_NAME);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001544 disabledReason = ShortcutService.parseIntAttribute(parser, ATTR_DISABLED_REASON);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001545 intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001546 rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001547 lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
Makoto Onuki31459242016-03-22 11:12:18 -07001548 flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
Makoto Onuki157b1622016-06-02 16:13:10 -07001549 iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
1550 iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001551 bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
1552
1553 final int outerDepth = parser.getDepth();
1554 int type;
1555 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1556 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1557 if (type != XmlPullParser.START_TAG) {
1558 continue;
1559 }
1560 final int depth = parser.getDepth();
1561 final String tag = parser.getName();
1562 if (ShortcutService.DEBUG_LOAD) {
1563 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1564 depth, type, tag));
1565 }
1566 switch (tag) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001567 case TAG_INTENT_EXTRAS_LEGACY:
1568 intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser);
1569 continue;
1570 case TAG_INTENT:
1571 intents.add(parseIntent(parser));
Makoto Onuki31459242016-03-22 11:12:18 -07001572 continue;
1573 case TAG_EXTRAS:
1574 extras = PersistableBundle.restoreFromXml(parser);
1575 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001576 case TAG_CATEGORIES:
1577 // This just contains string-array.
1578 continue;
1579 case TAG_STRING_ARRAY_XMLUTILS:
1580 if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
1581 ATTR_NAME_XMLUTILS))) {
Makoto Onukibe73a802016-04-15 14:46:35 -07001582 final String[] ar = XmlUtils.readThisStringArrayXml(
1583 parser, TAG_STRING_ARRAY_XMLUTILS, null);
1584 categories = new ArraySet<>(ar.length);
1585 for (int i = 0; i < ar.length; i++) {
1586 categories.add(ar[i]);
1587 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001588 }
1589 continue;
Makoto Onuki31459242016-03-22 11:12:18 -07001590 }
1591 throw ShortcutService.throwForInvalidTag(depth, tag);
1592 }
Makoto Onukibe73a802016-04-15 14:46:35 -07001593
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001594 if (intentLegacy != null) {
1595 // For the legacy file format which supported only one intent per shortcut.
1596 ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy);
1597 intents.clear();
1598 intents.add(intentLegacy);
1599 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001600
Makoto Onukia4f89b12017-10-05 10:37:55 -07001601
1602 if ((disabledReason == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)
1603 && ((flags & ShortcutInfo.FLAG_DISABLED) != 0)) {
1604 // We didn't used to have the disabled reason, so if a shortcut is disabled
1605 // and has no reason, we assume it was disabled by publisher.
1606 disabledReason = ShortcutInfo.DISABLED_REASON_BY_APP;
1607 }
1608
1609 // All restored shortcuts are initially "shadow".
1610 if (fromBackup) {
1611 flags |= ShortcutInfo.FLAG_SHADOW;
1612 }
1613
Makoto Onuki31459242016-03-22 11:12:18 -07001614 return new ShortcutInfo(
Makoto Onuki20c95f82016-05-11 16:51:01 -07001615 userId, id, packageName, activityComponent, /* icon =*/ null,
Makoto Onuki157b1622016-06-02 16:13:10 -07001616 title, titleResId, titleResName, text, textResId, textResName,
1617 disabledMessage, disabledMessageResId, disabledMessageResName,
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001618 categories,
1619 intents.toArray(new Intent[intents.size()]),
1620 rank, extras, lastChangedTimestamp, flags,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001621 iconResId, iconResName, bitmapPath, disabledReason);
Makoto Onuki31459242016-03-22 11:12:18 -07001622 }
Makoto Onuki2e210c42016-03-30 08:30:36 -07001623
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001624 private static Intent parseIntent(XmlPullParser parser)
1625 throws IOException, XmlPullParserException {
1626
1627 Intent intent = ShortcutService.parseIntentAttribute(parser,
1628 ATTR_INTENT_NO_EXTRA);
1629
1630 final int outerDepth = parser.getDepth();
1631 int type;
1632 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1633 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1634 if (type != XmlPullParser.START_TAG) {
1635 continue;
1636 }
1637 final int depth = parser.getDepth();
1638 final String tag = parser.getName();
1639 if (ShortcutService.DEBUG_LOAD) {
1640 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1641 depth, type, tag));
1642 }
1643 switch (tag) {
1644 case TAG_EXTRAS:
1645 ShortcutInfo.setIntentExtras(intent,
1646 PersistableBundle.restoreFromXml(parser));
1647 continue;
1648 }
1649 throw ShortcutService.throwForInvalidTag(depth, tag);
1650 }
1651 return intent;
1652 }
1653
Makoto Onuki2e210c42016-03-30 08:30:36 -07001654 @VisibleForTesting
1655 List<ShortcutInfo> getAllShortcutsForTest() {
1656 return new ArrayList<>(mShortcuts.values());
1657 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001658
1659 @Override
1660 public void verifyStates() {
1661 super.verifyStates();
1662
1663 boolean failed = false;
1664
Makoto Onuki255461f2017-01-10 11:47:25 -08001665 final ShortcutService s = mShortcutUser.mService;
1666
Makoto Onuki7001a612016-05-27 13:24:28 -07001667 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1668 sortShortcutsToActivities();
1669
1670 // Make sure each activity won't have more than max shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001671 for (int outer = all.size() - 1; outer >= 0; outer--) {
1672 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1673 if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001674 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001675 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
1676 + " has " + all.valueAt(outer).size() + " shortcuts.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001677 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001678
1679 // Sort by rank.
1680 Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
1681
1682 // Split into two arrays for each kind.
1683 final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
1684 dynamicList.removeIf((si) -> !si.isDynamic());
1685
1686 final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
1687 dynamicList.removeIf((si) -> !si.isManifestShortcut());
1688
1689 verifyRanksSequential(dynamicList);
1690 verifyRanksSequential(manifestList);
Makoto Onuki7001a612016-05-27 13:24:28 -07001691 }
1692
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001693 // Verify each shortcut's status.
Makoto Onuki7001a612016-05-27 13:24:28 -07001694 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1695 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onuki99302b52017-03-29 12:42:26 -07001696 if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001697 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001698 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki99302b52017-03-29 12:42:26 -07001699 + " is not manifest, dynamic or pinned.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001700 }
Makoto Onukiff14f732016-06-30 17:07:25 -07001701 if (si.isDeclaredInManifest() && si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001702 failed = true;
1703 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1704 + " is both dynamic and manifest at the same time.");
1705 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001706 if (si.getActivity() == null && !si.isFloating()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001707 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001708 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki255461f2017-01-10 11:47:25 -08001709 + " has null activity, but not floating.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001710 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001711 if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001712 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001713 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001714 + " is not floating, but is disabled.");
1715 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001716 if (si.isFloating() && si.getRank() != 0) {
1717 failed = true;
1718 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1719 + " is floating, but has rank=" + si.getRank());
1720 }
Makoto Onukidd097812016-06-29 13:10:09 -07001721 if (si.getIcon() != null) {
1722 failed = true;
1723 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1724 + " still has an icon");
1725 }
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001726 if (si.hasAdaptiveBitmap() && !si.hasIconFile()) {
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001727 failed = true;
1728 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001729 + " has adaptive bitmap but was not saved to a file.");
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001730 }
Makoto Onukidd097812016-06-29 13:10:09 -07001731 if (si.hasIconFile() && si.hasIconResource()) {
1732 failed = true;
1733 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1734 + " has both resource and bitmap icons");
1735 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001736 if (si.isEnabled()
1737 != (si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)) {
1738 failed = true;
1739 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1740 + " isEnabled() and getDisabledReason() disagree: "
1741 + si.isEnabled() + " vs " + si.getDisabledReason());
1742 }
1743 if ((si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
1744 && (getPackageInfo().getBackupSourceVersionCode()
1745 == ShortcutInfo.VERSION_CODE_UNKNOWN)) {
1746 failed = true;
1747 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1748 + " RESTORED_VERSION_LOWER with no backup source version code.");
1749 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001750 if (s.isDummyMainActivity(si.getActivity())) {
1751 failed = true;
1752 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1753 + " has a dummy target activity");
1754 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001755 }
1756
1757 if (failed) {
Makoto Onuki9fd90192017-01-06 18:31:03 +00001758 throw new IllegalStateException("See logcat for errors");
Makoto Onuki7001a612016-05-27 13:24:28 -07001759 }
1760 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001761
1762 private boolean verifyRanksSequential(List<ShortcutInfo> list) {
1763 boolean failed = false;
1764
1765 for (int i = 0; i < list.size(); i++) {
1766 final ShortcutInfo si = list.get(i);
1767 if (si.getRank() != i) {
1768 failed = true;
1769 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1770 + " rank=" + si.getRank() + " but expected to be "+ i);
1771 }
1772 }
1773 return failed;
1774 }
Makoto Onuki31459242016-03-22 11:12:18 -07001775}