blob: 9782648efb6b1d82b11827f467447eada313e988 [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;
Makoto Onukibe73a802016-04-15 14:46:35 -070058import java.util.Set;
Makoto Onuki31459242016-03-22 11:12:18 -070059import java.util.function.Predicate;
60
61/**
62 * Package information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070063 * User information used by {@link ShortcutService}.
64 *
65 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070066 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070067class ShortcutPackage extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070068 private static final String TAG = ShortcutService.TAG;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070069 private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
Makoto Onuki31459242016-03-22 11:12:18 -070070
71 static final String TAG_ROOT = "package";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070072 private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras";
73 private static final String TAG_INTENT = "intent";
Makoto Onuki31459242016-03-22 11:12:18 -070074 private static final String TAG_EXTRAS = "extras";
75 private static final String TAG_SHORTCUT = "shortcut";
Makoto Onukib6d35232016-04-04 15:57:17 -070076 private static final String TAG_CATEGORIES = "categories";
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080077 private static final String TAG_PERSON = "person";
Makoto Onuki31459242016-03-22 11:12:18 -070078
79 private static final String ATTR_NAME = "name";
Makoto Onuki31459242016-03-22 11:12:18 -070080 private static final String ATTR_CALL_COUNT = "call-count";
81 private static final String ATTR_LAST_RESET = "last-reset";
82 private static final String ATTR_ID = "id";
83 private static final String ATTR_ACTIVITY = "activity";
84 private static final String ATTR_TITLE = "title";
Makoto Onuki20c95f82016-05-11 16:51:01 -070085 private static final String ATTR_TITLE_RES_ID = "titleid";
Makoto Onuki157b1622016-06-02 16:13:10 -070086 private static final String ATTR_TITLE_RES_NAME = "titlename";
Makoto Onukie3ae7ec2016-03-29 15:45:25 -070087 private static final String ATTR_TEXT = "text";
Makoto Onuki20c95f82016-05-11 16:51:01 -070088 private static final String ATTR_TEXT_RES_ID = "textid";
Makoto Onuki157b1622016-06-02 16:13:10 -070089 private static final String ATTR_TEXT_RES_NAME = "textname";
Makoto Onuki20c95f82016-05-11 16:51:01 -070090 private static final String ATTR_DISABLED_MESSAGE = "dmessage";
91 private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
Makoto Onuki157b1622016-06-02 16:13:10 -070092 private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
Makoto Onukia4f89b12017-10-05 10:37:55 -070093 private static final String ATTR_DISABLED_REASON = "disabled-reason";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070094 private static final String ATTR_INTENT_LEGACY = "intent";
95 private static final String ATTR_INTENT_NO_EXTRA = "intent-base";
Makoto Onuki20c95f82016-05-11 16:51:01 -070096 private static final String ATTR_RANK = "rank";
Makoto Onuki31459242016-03-22 11:12:18 -070097 private static final String ATTR_TIMESTAMP = "timestamp";
98 private static final String ATTR_FLAGS = "flags";
Makoto Onuki157b1622016-06-02 16:13:10 -070099 private static final String ATTR_ICON_RES_ID = "icon-res";
100 private static final String ATTR_ICON_RES_NAME = "icon-resname";
Makoto Onuki31459242016-03-22 11:12:18 -0700101 private static final String ATTR_BITMAP_PATH = "bitmap-path";
Adam Hed4586b12019-03-19 12:01:00 -0700102 private static final String ATTR_LOCUS_ID = "locus-id";
Makoto Onuki31459242016-03-22 11:12:18 -0700103
Mehdi Alizadehebb4b602019-02-05 15:52:18 -0800104 private static final String ATTR_PERSON_NAME = "name";
105 private static final String ATTR_PERSON_URI = "uri";
106 private static final String ATTR_PERSON_KEY = "key";
107 private static final String ATTR_PERSON_IS_BOT = "is-bot";
108 private static final String ATTR_PERSON_IS_IMPORTANT = "is-important";
109
Makoto Onukib6d35232016-04-04 15:57:17 -0700110 private static final String NAME_CATEGORIES = "categories";
111
112 private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
113 private static final String ATTR_NAME_XMLUTILS = "name";
114
Makoto Onuki76269922016-07-15 14:58:54 -0700115 private static final String KEY_DYNAMIC = "dynamic";
116 private static final String KEY_MANIFEST = "manifest";
117 private static final String KEY_PINNED = "pinned";
118 private static final String KEY_BITMAPS = "bitmaps";
119 private static final String KEY_BITMAP_BYTES = "bitmapBytes";
120
Makoto Onuki31459242016-03-22 11:12:18 -0700121 /**
122 * All the shortcuts from the package, keyed on IDs.
123 */
124 final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
125
126 /**
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800127 * All the share targets from the package
128 */
129 private final ArrayList<ShareTargetInfo> mShareTargets = new ArrayList<>(0);
130
131 /**
Makoto Onuki31459242016-03-22 11:12:18 -0700132 * # of times the package has called rate-limited APIs.
133 */
134 private int mApiCallCount;
135
136 /**
137 * When {@link #mApiCallCount} was reset last time.
138 */
139 private long mLastResetTime;
140
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700141 private final int mPackageUid;
142
143 private long mLastKnownForegroundElapsedTime;
144
Makoto Onukic51b2872016-05-04 15:24:50 -0700145 private ShortcutPackage(ShortcutUser shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700146 int packageUserId, String packageName, ShortcutPackageInfo spi) {
147 super(shortcutUser, packageUserId, packageName,
148 spi != null ? spi : ShortcutPackageInfo.newEmpty());
149
Makoto Onukic51b2872016-05-04 15:24:50 -0700150 mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700151 }
152
Makoto Onukic51b2872016-05-04 15:24:50 -0700153 public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
154 this(shortcutUser, packageUserId, packageName, null);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700155 }
156
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700157 @Override
158 public int getOwnerUserId() {
159 // For packages, always owner user == package user.
160 return getPackageUserId();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700161 }
162
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700163 public int getPackageUid() {
164 return mPackageUid;
165 }
166
Makoto Onuki157b1622016-06-02 16:13:10 -0700167 @Nullable
168 public Resources getPackageResources() {
169 return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
170 getPackageName(), getPackageUserId());
171 }
172
Makoto Onuki50a320e2017-05-31 14:38:42 -0700173 public int getShortcutCount() {
174 return mShortcuts.size();
175 }
176
Makoto Onuki2e210c42016-03-30 08:30:36 -0700177 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700178 protected boolean canRestoreAnyVersion() {
179 return false;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700180 }
181
182 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700183 protected void onRestored(int restoreBlockReason) {
184 // Shortcuts have been restored.
185 // - Unshadow all shortcuts.
186 // - Set disabled reason.
187 // - Disable if needed.
188 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
189 ShortcutInfo si = mShortcuts.valueAt(i);
190 si.clearFlags(ShortcutInfo.FLAG_SHADOW);
191
192 si.setDisabledReason(restoreBlockReason);
193 if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
194 si.addFlags(ShortcutInfo.FLAG_DISABLED);
195 }
196 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700197 // Because some launchers may not have been restored (e.g. allowBackup=false),
198 // we need to re-calculate the pinned shortcuts.
Makoto Onukic51b2872016-05-04 15:24:50 -0700199 refreshPinnedFlags();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700200 }
201
Makoto Onukid99c6f02016-03-28 11:02:54 -0700202 /**
203 * Note this does *not* provide a correct view to the calling launcher.
204 */
Makoto Onuki31459242016-03-22 11:12:18 -0700205 @Nullable
206 public ShortcutInfo findShortcutById(String id) {
207 return mShortcuts.get(id);
208 }
209
Makoto Onukia4f89b12017-10-05 10:37:55 -0700210 public boolean isShortcutExistsAndInvisibleToPublisher(String id) {
211 ShortcutInfo si = findShortcutById(id);
212 return si != null && !si.isVisibleToPublisher();
213 }
214
215 public boolean isShortcutExistsAndVisibleToPublisher(String id) {
216 ShortcutInfo si = findShortcutById(id);
217 return si != null && si.isVisibleToPublisher();
218 }
219
220 private void ensureNotImmutable(@Nullable ShortcutInfo shortcut, boolean ignoreInvisible) {
221 if (shortcut != null && shortcut.isImmutable()
222 && (!ignoreInvisible || shortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700223 throw new IllegalArgumentException(
224 "Manifest shortcut ID=" + shortcut.getId()
225 + " may not be manipulated via APIs");
226 }
227 }
228
Makoto Onukia4f89b12017-10-05 10:37:55 -0700229 public void ensureNotImmutable(@NonNull String id, boolean ignoreInvisible) {
230 ensureNotImmutable(mShortcuts.get(id), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700231 }
232
Makoto Onukia4f89b12017-10-05 10:37:55 -0700233 public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds,
234 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700235 for (int i = shortcutIds.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700236 ensureNotImmutable(shortcutIds.get(i), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700237 }
238 }
239
Makoto Onukia4f89b12017-10-05 10:37:55 -0700240 public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts,
241 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700242 for (int i = shortcuts.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700243 ensureNotImmutable(shortcuts.get(i).getId(), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700244 }
245 }
246
Makoto Onukia4f89b12017-10-05 10:37:55 -0700247 /**
248 * Delete a shortcut by ID. This will *always* remove it even if it's immutable or invisible.
249 */
250 private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) {
Makoto Onuki31459242016-03-22 11:12:18 -0700251 final ShortcutInfo shortcut = mShortcuts.remove(id);
252 if (shortcut != null) {
Makoto Onuki475c3652017-05-08 14:29:03 -0700253 mShortcutUser.mService.removeIconLocked(shortcut);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700254 shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
Makoto Onuki99302b52017-03-29 12:42:26 -0700255 | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki31459242016-03-22 11:12:18 -0700256 }
257 return shortcut;
258 }
259
Makoto Onukia4f89b12017-10-05 10:37:55 -0700260 /**
261 * Force replace a shortcut. If there's already a shortcut with the same ID, it'll be removed,
262 * even if it's invisible.
263 */
264 private void forceReplaceShortcutInner(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700265 final ShortcutService s = mShortcutUser.mService;
266
Makoto Onukia4f89b12017-10-05 10:37:55 -0700267 forceDeleteShortcutInner(newShortcut.getId());
Makoto Onuki157b1622016-06-02 16:13:10 -0700268
269 // Extract Icon and update the icon res ID and the bitmap path.
Makoto Onuki475c3652017-05-08 14:29:03 -0700270 s.saveIconAndFixUpShortcutLocked(newShortcut);
Makoto Onuki157b1622016-06-02 16:13:10 -0700271 s.fixUpShortcutResourceNamesAndValues(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700272 mShortcuts.put(newShortcut.getId(), newShortcut);
273 }
274
275 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700276 * Add a shortcut. If there's already a one with the same ID, it'll be removed, even if it's
277 * invisible.
Makoto Onuki31459242016-03-22 11:12:18 -0700278 *
279 * It checks the max number of dynamic shortcuts.
280 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700281 public void addOrReplaceDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki39686e82016-04-13 18:03:00 -0700282
Makoto Onuki22fcc682016-05-17 14:52:19 -0700283 Preconditions.checkArgument(newShortcut.isEnabled(),
284 "add/setDynamicShortcuts() cannot publish disabled shortcuts");
285
Makoto Onuki99302b52017-03-29 12:42:26 -0700286 newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki31459242016-03-22 11:12:18 -0700287
288 final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
289
290 final boolean wasPinned;
Makoto Onuki31459242016-03-22 11:12:18 -0700291
292 if (oldShortcut == null) {
293 wasPinned = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700294 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700295 // It's an update case.
296 // Make sure the target is updatable. (i.e. should be mutable.)
Makoto Onukia4f89b12017-10-05 10:37:55 -0700297 oldShortcut.ensureUpdatableWith(newShortcut, /*isUpdating=*/ false);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700298
Makoto Onuki31459242016-03-22 11:12:18 -0700299 wasPinned = oldShortcut.isPinned();
Makoto Onuki31459242016-03-22 11:12:18 -0700300 }
301
Makoto Onuki157b1622016-06-02 16:13:10 -0700302 // If it was originally pinned, the new one should be pinned too.
Makoto Onuki31459242016-03-22 11:12:18 -0700303 if (wasPinned) {
304 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
305 }
306
Makoto Onukia4f89b12017-10-05 10:37:55 -0700307 forceReplaceShortcutInner(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700308 }
309
310 /**
311 * Remove all shortcuts that aren't pinned nor dynamic.
312 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700313 private void removeOrphans() {
Makoto Onuki31459242016-03-22 11:12:18 -0700314 ArrayList<String> removeList = null; // Lazily initialize.
315
316 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
317 final ShortcutInfo si = mShortcuts.valueAt(i);
318
Makoto Onuki22fcc682016-05-17 14:52:19 -0700319 if (si.isAlive()) continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700320
321 if (removeList == null) {
322 removeList = new ArrayList<>();
323 }
324 removeList.add(si.getId());
325 }
326 if (removeList != null) {
327 for (int i = removeList.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700328 forceDeleteShortcutInner(removeList.get(i));
Makoto Onuki31459242016-03-22 11:12:18 -0700329 }
330 }
331 }
332
333 /**
334 * Remove all dynamic shortcuts.
335 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700336 public void deleteAllDynamicShortcuts(boolean ignoreInvisible) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700337 final long now = mShortcutUser.mService.injectCurrentTimeMillis();
338
Makoto Onuki22fcc682016-05-17 14:52:19 -0700339 boolean changed = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700340 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700341 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700342 if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700343 changed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700344
345 si.setTimestamp(now);
Makoto Onuki99302b52017-03-29 12:42:26 -0700346 si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700347 si.setRank(0); // It may still be pinned, so clear the rank.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700348 }
Makoto Onuki31459242016-03-22 11:12:18 -0700349 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700350 if (changed) {
351 removeOrphans();
352 }
Makoto Onuki31459242016-03-22 11:12:18 -0700353 }
354
355 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700356 * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
357 * is pinned, it'll remain as a pinned shortcut, and is still enabled.
Makoto Onukib08790c2016-06-23 14:05:46 -0700358 *
359 * @return true if it's actually removed because it wasn't pinned, or false if it's still
360 * pinned.
Makoto Onuki31459242016-03-22 11:12:18 -0700361 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700362 public boolean deleteDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700363 final ShortcutInfo removed = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700364 shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false, ignoreInvisible,
365 ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukib08790c2016-06-23 14:05:46 -0700366 return removed == null;
367 }
368
369 /**
370 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
371 * is pinned, it'll remain as a pinned shortcut, but will be disabled.
372 *
373 * @return true if it's actually removed because it wasn't pinned, or false if it's still
374 * pinned.
375 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700376 private boolean disableDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible,
377 int disabledReason) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700378 final ShortcutInfo disabled = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700379 shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false, ignoreInvisible,
380 disabledReason);
Makoto Onukib08790c2016-06-23 14:05:46 -0700381 return disabled == null;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700382 }
383
Makoto Onuki7001a612016-05-27 13:24:28 -0700384 /**
385 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
386 * is pinned, it'll remain as a pinned shortcut but will be disabled.
387 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700388 public void disableWithId(@NonNull String shortcutId, String disabledMessage,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700389 int disabledMessageResId, boolean overrideImmutable, boolean ignoreInvisible,
390 int disabledReason) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700391 final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700392 overrideImmutable, ignoreInvisible, disabledReason);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700393
394 if (disabled != null) {
395 if (disabledMessage != null) {
396 disabled.setDisabledMessage(disabledMessage);
397 } else if (disabledMessageResId != 0) {
398 disabled.setDisabledMessageResId(disabledMessageResId);
Makoto Onuki157b1622016-06-02 16:13:10 -0700399
400 mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700401 }
402 }
403 }
404
405 @Nullable
406 private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700407 boolean overrideImmutable, boolean ignoreInvisible, int disabledReason) {
408 Preconditions.checkState(
409 (disable == (disabledReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED)),
410 "disable and disabledReason disagree: " + disable + " vs " + disabledReason);
Makoto Onuki31459242016-03-22 11:12:18 -0700411 final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
412
Makoto Onukia4f89b12017-10-05 10:37:55 -0700413 if (oldShortcut == null || !oldShortcut.isEnabled()
414 && (ignoreInvisible && !oldShortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700415 return null; // Doesn't exist or already disabled.
Makoto Onuki31459242016-03-22 11:12:18 -0700416 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700417 if (!overrideImmutable) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700418 ensureNotImmutable(oldShortcut, /*ignoreInvisible=*/ true);
Makoto Onuki31459242016-03-22 11:12:18 -0700419 }
420 if (oldShortcut.isPinned()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700421
422 oldShortcut.setRank(0);
Makoto Onuki99302b52017-03-29 12:42:26 -0700423 oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700424 if (disable) {
425 oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700426 // Do not overwrite the disabled reason if one is alreay set.
427 if (oldShortcut.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
428 oldShortcut.setDisabledReason(disabledReason);
429 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700430 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700431 oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
432
Makoto Onuki255461f2017-01-10 11:47:25 -0800433 // See ShortcutRequestPinProcessor.directPinShortcut().
434 if (mShortcutUser.mService.isDummyMainActivity(oldShortcut.getActivity())) {
435 oldShortcut.setActivity(null);
436 }
437
Makoto Onuki22fcc682016-05-17 14:52:19 -0700438 return oldShortcut;
Makoto Onuki31459242016-03-22 11:12:18 -0700439 } else {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700440 forceDeleteShortcutInner(shortcutId);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700441 return null;
442 }
443 }
444
445 public void enableWithId(@NonNull String shortcutId) {
446 final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
447 if (shortcut != null) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700448 ensureNotImmutable(shortcut, /*ignoreInvisible=*/ true);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700449 shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700450 shortcut.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onuki31459242016-03-22 11:12:18 -0700451 }
452 }
453
Makoto Onukia4f89b12017-10-05 10:37:55 -0700454 public void updateInvisibleShortcutForPinRequestWith(@NonNull ShortcutInfo shortcut) {
455 final ShortcutInfo source = mShortcuts.get(shortcut.getId());
456 Preconditions.checkNotNull(source);
457
458 mShortcutUser.mService.validateShortcutForPinRequest(shortcut);
459
460 shortcut.addFlags(ShortcutInfo.FLAG_PINNED);
461
462 forceReplaceShortcutInner(shortcut);
463
464 adjustRanks();
465 }
466
Makoto Onuki31459242016-03-22 11:12:18 -0700467 /**
468 * Called after a launcher updates the pinned set. For each shortcut in this package,
469 * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it.
470 *
471 * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
472 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700473 public void refreshPinnedFlags() {
Makoto Onuki31459242016-03-22 11:12:18 -0700474 // First, un-pin all shortcuts
475 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
476 mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
477 }
478
479 // Then, for the pinned set for each launcher, set the pin flag one by one.
Makoto Onuki47ed1712017-12-20 14:40:20 +0900480 mShortcutUser.forAllLaunchers(launcherShortcuts -> {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700481 final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
Makoto Onuki2e210c42016-03-30 08:30:36 -0700482 getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700483
484 if (pinned == null || pinned.size() == 0) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700485 return;
Makoto Onuki31459242016-03-22 11:12:18 -0700486 }
487 for (int i = pinned.size() - 1; i >= 0; i--) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700488 final String id = pinned.valueAt(i);
489 final ShortcutInfo si = mShortcuts.get(id);
Makoto Onuki31459242016-03-22 11:12:18 -0700490 if (si == null) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700491 // This happens if a launcher pinned shortcuts from this package, then backup&
492 // restored, but this package doesn't allow backing up.
493 // In that case the launcher ends up having a dangling pinned shortcuts.
494 // That's fine, when the launcher is restored, we'll fix it.
495 continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700496 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700497 si.addFlags(ShortcutInfo.FLAG_PINNED);
Makoto Onuki31459242016-03-22 11:12:18 -0700498 }
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700499 });
Makoto Onuki31459242016-03-22 11:12:18 -0700500
501 // Lastly, remove the ones that are no longer pinned nor dynamic.
Makoto Onukic51b2872016-05-04 15:24:50 -0700502 removeOrphans();
Makoto Onuki31459242016-03-22 11:12:18 -0700503 }
504
505 /**
506 * Number of calls that the caller has made, since the last reset.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700507 *
508 * <p>This takes care of the resetting the counter for foreground apps as well as after
509 * locale changes.
Makoto Onuki31459242016-03-22 11:12:18 -0700510 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800511 public int getApiCallCount(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700512 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700513
514 // Reset the counter if:
515 // - the package is in foreground now.
516 // - the package is *not* in foreground now, but was in foreground at some point
517 // since the previous time it had been.
518 if (s.isUidForegroundLocked(mPackageUid)
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800519 || (mLastKnownForegroundElapsedTime
520 < s.getUidLastForegroundElapsedTimeLocked(mPackageUid))
521 || unlimited) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700522 mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
Makoto Onukic51b2872016-05-04 15:24:50 -0700523 resetRateLimiting();
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700524 }
525
526 // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
527 // but we just can't return 0 at this point, because we may have to update
528 // mLastResetTime.
529
Makoto Onuki31459242016-03-22 11:12:18 -0700530 final long last = s.getLastResetTimeLocked();
531
532 final long now = s.injectCurrentTimeMillis();
533 if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
534 Slog.w(TAG, "Clock rewound");
535 // Clock rewound.
536 mLastResetTime = now;
537 mApiCallCount = 0;
538 return mApiCallCount;
539 }
540
541 // If not reset yet, then reset.
542 if (mLastResetTime < last) {
543 if (ShortcutService.DEBUG) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700544 Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
545 getPackageName(), mLastResetTime, now, last));
Makoto Onuki31459242016-03-22 11:12:18 -0700546 }
547 mApiCallCount = 0;
548 mLastResetTime = last;
549 }
550 return mApiCallCount;
551 }
552
553 /**
554 * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
555 * and return true. Otherwise just return false.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700556 *
557 * <p>This takes care of the resetting the counter for foreground apps as well as after
558 * locale changes, which is done internally by {@link #getApiCallCount}.
Makoto Onuki31459242016-03-22 11:12:18 -0700559 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800560 public boolean tryApiCall(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700561 final ShortcutService s = mShortcutUser.mService;
562
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800563 if (getApiCallCount(unlimited) >= s.mMaxUpdatesPerInterval) {
Makoto Onuki31459242016-03-22 11:12:18 -0700564 return false;
565 }
566 mApiCallCount++;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700567 s.scheduleSaveUser(getOwnerUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700568 return true;
569 }
570
Makoto Onukic51b2872016-05-04 15:24:50 -0700571 public void resetRateLimiting() {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700572 if (ShortcutService.DEBUG) {
573 Slog.d(TAG, "resetRateLimiting: " + getPackageName());
574 }
575 if (mApiCallCount > 0) {
576 mApiCallCount = 0;
Makoto Onukic51b2872016-05-04 15:24:50 -0700577 mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700578 }
579 }
580
581 public void resetRateLimitingForCommandLineNoSaving() {
Makoto Onuki31459242016-03-22 11:12:18 -0700582 mApiCallCount = 0;
583 mLastResetTime = 0;
584 }
585
586 /**
587 * Find all shortcuts that match {@code query}.
588 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700589 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700590 @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700591 findAll(result, query, cloneFlag, null, 0, /*getPinnedByAnyLauncher=*/ false);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700592 }
593
594 /**
595 * Find all shortcuts that match {@code query}.
596 *
597 * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
598 * by the calling launcher will not be included in the result, and also "isPinned" will be
599 * adjusted for the caller too.
600 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700601 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onuki31459242016-03-22 11:12:18 -0700602 @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
Makoto Onuki634cecb2017-10-13 17:10:48 -0700603 @Nullable String callingLauncher, int launcherUserId, boolean getPinnedByAnyLauncher) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700604 if (getPackageInfo().isShadow()) {
605 // Restored and the app not installed yet, so don't return any.
606 return;
607 }
Makoto Onuki31459242016-03-22 11:12:18 -0700608
Makoto Onukic51b2872016-05-04 15:24:50 -0700609 final ShortcutService s = mShortcutUser.mService;
610
Makoto Onuki31459242016-03-22 11:12:18 -0700611 // Set of pinned shortcuts by the calling launcher.
612 final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
Makoto Onuki2e210c42016-03-30 08:30:36 -0700613 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
614 .getPinnedShortcutIds(getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700615
616 for (int i = 0; i < mShortcuts.size(); i++) {
617 final ShortcutInfo si = mShortcuts.valueAt(i);
618
Makoto Onuki22fcc682016-05-17 14:52:19 -0700619 // Need to adjust PINNED flag depending on the caller.
620 // Basically if the caller is a launcher (callingLauncher != null) and the launcher
621 // isn't pinning it, then we need to clear PINNED for this caller.
Makoto Onuki31459242016-03-22 11:12:18 -0700622 final boolean isPinnedByCaller = (callingLauncher == null)
623 || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700624
Makoto Onuki634cecb2017-10-13 17:10:48 -0700625 if (!getPinnedByAnyLauncher) {
626 if (si.isFloating()) {
627 if (!isPinnedByCaller) {
628 continue;
629 }
Makoto Onuki31459242016-03-22 11:12:18 -0700630 }
631 }
632 final ShortcutInfo clone = si.clone(cloneFlag);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700633
Makoto Onuki31459242016-03-22 11:12:18 -0700634 // Fix up isPinned for the caller. Note we need to do it before the "test" callback,
635 // since it may check isPinned.
Makoto Onuki35559d62017-11-06 16:26:32 -0800636 // However, if getPinnedByAnyLauncher is set, we do it after the test.
637 if (!getPinnedByAnyLauncher) {
638 if (!isPinnedByCaller) {
639 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
640 }
Makoto Onuki31459242016-03-22 11:12:18 -0700641 }
642 if (query == null || query.test(clone)) {
Makoto Onuki35559d62017-11-06 16:26:32 -0800643 if (!isPinnedByCaller) {
644 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
645 }
Makoto Onuki31459242016-03-22 11:12:18 -0700646 result.add(clone);
647 }
648 }
649 }
650
651 public void resetThrottling() {
652 mApiCallCount = 0;
653 }
654
Makoto Onuki39686e82016-04-13 18:03:00 -0700655 /**
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800656 * Returns a list of ShortcutInfos that match the given intent filter and the category of
657 * available ShareTarget definitions in this package.
658 */
659 public List<ShortcutManager.ShareShortcutInfo> getMatchingShareTargets(
660 @NonNull IntentFilter filter) {
661 final List<ShareTargetInfo> matchedTargets = new ArrayList<>();
662 for (int i = 0; i < mShareTargets.size(); i++) {
663 final ShareTargetInfo target = mShareTargets.get(i);
664 for (ShareTargetInfo.TargetData data : target.mTargetData) {
665 if (filter.hasDataType(data.mMimeType)) {
666 // Matched at least with one data type
667 matchedTargets.add(target);
668 break;
669 }
670 }
671 }
672
673 if (matchedTargets.isEmpty()) {
674 return new ArrayList<>();
675 }
676
677 // Get the list of all dynamic shortcuts in this package
678 final ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
679 findAll(shortcuts, ShortcutInfo::isDynamicVisible, ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
680
681 final List<ShortcutManager.ShareShortcutInfo> result = new ArrayList<>();
682 for (int i = 0; i < shortcuts.size(); i++) {
683 final ShortcutInfo si = shortcuts.get(i);
684 for (int j = 0; j < matchedTargets.size(); j++) {
685 // Shortcut must have all of share target categories
686 boolean hasAllCategories = true;
687 final ShareTargetInfo target = matchedTargets.get(j);
688 for (int q = 0; q < target.mCategories.length; q++) {
689 if (!si.getCategories().contains(target.mCategories[q])) {
690 hasAllCategories = false;
691 break;
692 }
693 }
694 if (hasAllCategories) {
695 result.add(new ShortcutManager.ShareShortcutInfo(si, new ComponentName(
696 getPackageName(), target.mTargetClass)));
697 break;
698 }
699 }
700 }
701 return result;
702 }
703
Mehdi Alizadeh85fd3d52019-01-23 12:49:53 -0800704 public boolean hasShareTargets() {
705 return !mShareTargets.isEmpty();
706 }
707
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800708 /**
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700709 * Return the filenames (excluding path names) of icon bitmap files from this package.
710 */
711 public ArraySet<String> getUsedBitmapFiles() {
712 final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
713
714 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
715 final ShortcutInfo si = mShortcuts.valueAt(i);
716 if (si.getBitmapPath() != null) {
717 usedFiles.add(getFileName(si.getBitmapPath()));
718 }
719 }
720 return usedFiles;
721 }
722
723 private static String getFileName(@NonNull String path) {
724 final int sep = path.lastIndexOf(File.separatorChar);
725 if (sep == -1) {
726 return path;
727 } else {
728 return path.substring(sep + 1);
729 }
730 }
731
732 /**
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700733 * @return false if any of the target activities are no longer enabled.
734 */
735 private boolean areAllActivitiesStillEnabled() {
736 if (mShortcuts.size() == 0) {
737 return true;
738 }
739 final ShortcutService s = mShortcutUser.mService;
740
741 // Normally the number of target activities is 1 or so, so no need to use a complex
742 // structure like a set.
743 final ArrayList<ComponentName> checked = new ArrayList<>(4);
744
745 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
746 final ShortcutInfo si = mShortcuts.valueAt(i);
747 final ComponentName activity = si.getActivity();
748
749 if (checked.contains(activity)) {
750 continue; // Already checked.
751 }
752 checked.add(activity);
753
Makoto Onuki40dc2112017-10-18 12:52:45 -0700754 if ((activity != null)
755 && !s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700756 return false;
757 }
758 }
759 return true;
760 }
761
762 /**
763 * Called when the package may be added or updated, or its activities may be disabled, and
764 * if so, rescan the package and do the necessary stuff.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700765 *
766 * Add case:
767 * - Publish manifest shortcuts.
768 *
769 * Update case:
770 * - Re-publish manifest shortcuts.
771 * - If there are shortcuts with resources (icons or strings), update their timestamps.
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700772 * - Disable shortcuts whose target activities are disabled.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700773 *
774 * @return TRUE if any shortcuts have been changed.
Makoto Onuki39686e82016-04-13 18:03:00 -0700775 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700776 public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) {
777 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki84d59342018-02-02 09:22:38 -0800778 final long start = s.getStatStartTime();
Makoto Onuki39686e82016-04-13 18:03:00 -0700779
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700780 final PackageInfo pi;
781 try {
782 pi = mShortcutUser.mService.getPackageInfo(
783 getPackageName(), getPackageUserId());
784 if (pi == null) {
785 return false; // Shouldn't happen.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700786 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700787
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700788 if (!isNewApp && !forceRescan) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700789 // Return if the package hasn't changed, ie:
790 // - version code hasn't change
791 // - lastUpdateTime hasn't change
792 // - all target activities are still enabled.
Makoto Onuki33663282016-08-22 16:19:04 -0700793
794 // Note, system apps timestamps do *not* change after OTAs. (But they do
795 // after an adb sync or a local flash.)
796 // This means if a system app's version code doesn't change on an OTA,
797 // we don't notice it's updated. But that's fine since their version code *should*
798 // really change on OTAs.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700799 if ((getPackageInfo().getVersionCode() == pi.getLongVersionCode())
Makoto Onuki64183d52016-08-08 14:11:34 -0700800 && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime)
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700801 && areAllActivitiesStillEnabled()) {
802 return false;
803 }
804 }
805 } finally {
806 s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700807 }
808
809 // Now prepare to publish manifest shortcuts.
810 List<ShortcutInfo> newManifestShortcutList = null;
811 try {
812 newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800813 getPackageName(), getPackageUserId(), mShareTargets);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700814 } catch (IOException|XmlPullParserException e) {
815 Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
816 }
817 final int manifestShortcutSize = newManifestShortcutList == null ? 0
818 : newManifestShortcutList.size();
819 if (ShortcutService.DEBUG) {
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800820 Slog.d(TAG,
821 String.format("Package %s has %d manifest shortcut(s), and %d share target(s)",
822 getPackageName(), manifestShortcutSize, mShareTargets.size()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700823 }
824 if (isNewApp && (manifestShortcutSize == 0)) {
825 // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
826
827 // If it's an update, then it may already have manifest shortcuts, which need to be
828 // disabled.
829 return false;
830 }
831 if (ShortcutService.DEBUG) {
832 Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
833 (isNewApp ? "added" : "updated"),
Dianne Hackborn3accca02013-09-20 09:32:11 -0700834 getPackageInfo().getVersionCode(), pi.getLongVersionCode()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700835 }
836
Makoto Onukia4f89b12017-10-05 10:37:55 -0700837 getPackageInfo().updateFromPackageInfo(pi);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700838 final long newVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700839
840 // See if there are any shortcuts that were prevented restoring because the app was of a
841 // lower version, and re-enable them.
842 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
843 final ShortcutInfo si = mShortcuts.valueAt(i);
844 if (si.getDisabledReason() != ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
845 continue;
846 }
847 if (getPackageInfo().getBackupSourceVersionCode() > newVersionCode) {
848 if (ShortcutService.DEBUG) {
849 Slog.d(TAG, String.format("Shortcut %s require version %s, still not restored.",
850 si.getId(), getPackageInfo().getBackupSourceVersionCode()));
851 }
852 continue;
853 }
854 Slog.i(TAG, String.format("Restoring shortcut: %s", si.getId()));
855 si.clearFlags(ShortcutInfo.FLAG_DISABLED);
856 si.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
857 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700858
Makoto Onuki22fcc682016-05-17 14:52:19 -0700859 // For existing shortcuts, update timestamps if they have any resources.
Makoto Onukib08790c2016-06-23 14:05:46 -0700860 // Also check if shortcuts' activities are still main activities. Otherwise, disable them.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700861 if (!isNewApp) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700862 Resources publisherRes = null;
863
Makoto Onuki22fcc682016-05-17 14:52:19 -0700864 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
865 final ShortcutInfo si = mShortcuts.valueAt(i);
866
Makoto Onuki2d895c32016-12-02 15:48:40 -0800867 // Disable dynamic shortcuts whose target activity is gone.
Makoto Onukib08790c2016-06-23 14:05:46 -0700868 if (si.isDynamic()) {
Makoto Onuki34145532017-03-14 17:58:36 -0700869 if (si.getActivity() == null) {
870 // Note if it's dynamic, it must have a target activity, but b/36228253.
871 s.wtf("null activity detected.");
872 // TODO Maybe remove it?
873 } else if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700874 Slog.w(TAG, String.format(
875 "%s is no longer main activity. Disabling shorcut %s.",
876 getPackageName(), si.getId()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700877 if (disableDynamicWithId(si.getId(), /*ignoreInvisible*/ false,
878 ShortcutInfo.DISABLED_REASON_APP_CHANGED)) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700879 continue; // Actually removed.
880 }
881 // Still pinned, so fall-through and possibly update the resources.
882 }
Makoto Onukib08790c2016-06-23 14:05:46 -0700883 }
884
Makoto Onuki22fcc682016-05-17 14:52:19 -0700885 if (si.hasAnyResources()) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700886 if (!si.isOriginallyFromManifest()) {
887 if (publisherRes == null) {
888 publisherRes = getPackageResources();
889 if (publisherRes == null) {
890 break; // Resources couldn't be loaded.
891 }
892 }
893
894 // If this shortcut is not from a manifest, then update all resource IDs
895 // from resource names. (We don't allow resource strings for
896 // non-manifest at the moment, but icons can still be resources.)
897 si.lookupAndFillInResourceIds(publisherRes);
898 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700899 si.setTimestamp(s.injectCurrentTimeMillis());
900 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700901 }
902 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700903
904 // (Re-)publish manifest shortcut.
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700905 publishManifestShortcuts(newManifestShortcutList);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700906
Makoto Onuki7001a612016-05-27 13:24:28 -0700907 if (newManifestShortcutList != null) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700908 pushOutExcessShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -0700909 }
910
Makoto Onukidf6da042016-06-16 09:51:40 -0700911 s.verifyStates();
912
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700913 // This will send a notification to the launcher, and also save .
914 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
915 return true; // true means changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700916 }
917
918 private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
919 if (ShortcutService.DEBUG) {
920 Slog.d(TAG, String.format(
921 "Package %s: publishing manifest shortcuts", getPackageName()));
922 }
923 boolean changed = false;
924
Makoto Onuki22fcc682016-05-17 14:52:19 -0700925 // Keep the previous IDs.
926 ArraySet<String> toDisableList = null;
927 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
928 final ShortcutInfo si = mShortcuts.valueAt(i);
929
930 if (si.isManifestShortcut()) {
931 if (toDisableList == null) {
932 toDisableList = new ArraySet<>();
933 }
934 toDisableList.add(si.getId());
935 }
936 }
937
938 // Publish new ones.
939 if (newManifestShortcutList != null) {
940 final int newListSize = newManifestShortcutList.size();
941
942 for (int i = 0; i < newListSize; i++) {
943 changed = true;
944
945 final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
946 final boolean newDisabled = !newShortcut.isEnabled();
947
Makoto Onuki7001a612016-05-27 13:24:28 -0700948 final String id = newShortcut.getId();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700949 final ShortcutInfo oldShortcut = mShortcuts.get(id);
950
951 boolean wasPinned = false;
952
953 if (oldShortcut != null) {
954 if (!oldShortcut.isOriginallyFromManifest()) {
955 Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
956 + " exists but is not from AndroidManifest.xml, not updating.");
957 continue;
958 }
959 // Take over the pinned flag.
960 if (oldShortcut.isPinned()) {
961 wasPinned = true;
962 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
963 }
964 }
965 if (newDisabled && !wasPinned) {
966 // If the shortcut is disabled, and it was *not* pinned, then this
967 // just doesn't have to be published.
968 // Just keep it in toDisableList, so the previous one would be removed.
969 continue;
970 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700971
972 // Note even if enabled=false, we still need to update all fields, so do it
973 // regardless.
Makoto Onukia4f89b12017-10-05 10:37:55 -0700974 forceReplaceShortcutInner(newShortcut); // This will clean up the old one too.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700975
976 if (!newDisabled && toDisableList != null) {
977 // Still alive, don't remove.
978 toDisableList.remove(id);
979 }
980 }
981 }
982
983 // Disable the previous manifest shortcuts that are no longer in the manifest.
984 if (toDisableList != null) {
985 if (ShortcutService.DEBUG) {
986 Slog.d(TAG, String.format(
987 "Package %s: disabling %d stale shortcuts", getPackageName(),
988 toDisableList.size()));
989 }
990 for (int i = toDisableList.size() - 1; i >= 0; i--) {
991 changed = true;
992
993 final String id = toDisableList.valueAt(i);
994
995 disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700996 /* overrideImmutable=*/ true, /*ignoreInvisible=*/ false,
997 ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700998 }
999 removeOrphans();
1000 }
Makoto Onukidf6da042016-06-16 09:51:40 -07001001 adjustRanks();
Makoto Onuki22fcc682016-05-17 14:52:19 -07001002 return changed;
Makoto Onuki39686e82016-04-13 18:03:00 -07001003 }
1004
Makoto Onuki7001a612016-05-27 13:24:28 -07001005 /**
1006 * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
1007 * If too many, we'll remove the dynamic with the lowest ranks.
1008 */
1009 private boolean pushOutExcessShortcuts() {
1010 final ShortcutService service = mShortcutUser.mService;
1011 final int maxShortcuts = service.getMaxActivityShortcuts();
1012
1013 boolean changed = false;
1014
1015 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1016 sortShortcutsToActivities();
1017 for (int outer = all.size() - 1; outer >= 0; outer--) {
1018 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1019 if (list.size() <= maxShortcuts) {
1020 continue;
1021 }
1022 // Sort by isManifestShortcut() and getRank().
1023 Collections.sort(list, mShortcutTypeAndRankComparator);
1024
1025 // Keep [0 .. max), and remove (as dynamic) [max .. size)
1026 for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
1027 final ShortcutInfo shortcut = list.get(inner);
1028
1029 if (shortcut.isManifestShortcut()) {
1030 // This shouldn't happen -- excess shortcuts should all be non-manifest.
1031 // But just in case.
1032 service.wtf("Found manifest shortcuts in excess list.");
1033 continue;
1034 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001035 deleteDynamicWithId(shortcut.getId(), /*ignoreInvisible=*/ true);
Makoto Onuki7001a612016-05-27 13:24:28 -07001036 }
1037 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001038
1039 return changed;
1040 }
1041
1042 /**
1043 * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before
1044 * non-manifest shortcuts, then sort by rank.
1045 *
1046 * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
1047 * manifest shortcuts than before and as a result we need to remove some of the dynamic
1048 * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with
1049 * the last ones.
1050 *
1051 * (Note the number of manifest shortcuts is always <= the max number, because if there are
1052 * more, ShortcutParser would ignore the rest.)
1053 */
1054 final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
1055 ShortcutInfo b) -> {
1056 if (a.isManifestShortcut() && !b.isManifestShortcut()) {
1057 return -1;
1058 }
1059 if (!a.isManifestShortcut() && b.isManifestShortcut()) {
1060 return 1;
1061 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001062 return Integer.compare(a.getRank(), b.getRank());
Makoto Onuki7001a612016-05-27 13:24:28 -07001063 };
1064
1065 /**
1066 * Build a list of shortcuts for each target activity and return as a map. The result won't
1067 * contain "floating" shortcuts because they don't belong on any activities.
1068 */
1069 private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
Makoto Onuki7001a612016-05-27 13:24:28 -07001070 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
1071 = new ArrayMap<>();
1072 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1073 final ShortcutInfo si = mShortcuts.valueAt(i);
1074 if (si.isFloating()) {
1075 continue; // Ignore floating shortcuts, which are not tied to any activities.
1076 }
1077
1078 final ComponentName activity = si.getActivity();
Makoto Onuki34145532017-03-14 17:58:36 -07001079 if (activity == null) {
1080 mShortcutUser.mService.wtf("null activity detected.");
1081 continue;
1082 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001083
1084 ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
1085 if (list == null) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001086 list = new ArrayList<>();
Makoto Onuki7001a612016-05-27 13:24:28 -07001087 activitiesToShortcuts.put(activity, list);
1088 }
1089 list.add(si);
1090 }
1091 return activitiesToShortcuts;
1092 }
1093
1094 /** Used by {@link #enforceShortcutCountsBeforeOperation} */
1095 private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
1096 ComponentName cn, int increment) {
1097 Integer oldValue = counts.get(cn);
1098 if (oldValue == null) {
1099 oldValue = 0;
1100 }
1101
1102 counts.put(cn, oldValue + increment);
1103 }
1104
1105 /**
1106 * Called by
1107 * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
1108 * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
1109 * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
1110 * the operation to make sure the operation wouldn't result in the target activities having
1111 * more than the allowed number of dynamic/manifest shortcuts.
1112 *
1113 * @param newList shortcut list passed to set, add or updateShortcuts().
1114 * @param operation add, set or update.
1115 * @throws IllegalArgumentException if the operation would result in going over the max
1116 * shortcut count for any activity.
1117 */
1118 public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
1119 @ShortcutOperation int operation) {
1120 final ShortcutService service = mShortcutUser.mService;
1121
1122 // Current # of dynamic / manifest shortcuts for each activity.
1123 // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
1124 // anyway.)
1125 final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
1126 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1127 final ShortcutInfo shortcut = mShortcuts.valueAt(i);
1128
1129 if (shortcut.isManifestShortcut()) {
1130 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1131 } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
1132 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1133 }
1134 }
1135
1136 for (int i = newList.size() - 1; i >= 0; i--) {
1137 final ShortcutInfo newShortcut = newList.get(i);
1138 final ComponentName newActivity = newShortcut.getActivity();
1139 if (newActivity == null) {
1140 if (operation != ShortcutService.OPERATION_UPDATE) {
Makoto Onukib08790c2016-06-23 14:05:46 -07001141 service.wtf("Activity must not be null at this point");
1142 continue; // Just ignore this invalid case.
Makoto Onuki7001a612016-05-27 13:24:28 -07001143 }
1144 continue; // Activity can be null for update.
1145 }
1146
1147 final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
1148 if (original == null) {
1149 if (operation == ShortcutService.OPERATION_UPDATE) {
1150 continue; // When updating, ignore if there's no target.
1151 }
1152 // Add() or set(), and there's no existing shortcut with the same ID. We're
1153 // simply publishing (as opposed to updating) this shortcut, so just +1.
1154 incrementCountForActivity(counts, newActivity, 1);
1155 continue;
1156 }
1157 if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
1158 // Updating floating shortcuts doesn't affect the count, so ignore.
1159 continue;
1160 }
1161
1162 // If it's add() or update(), then need to decrement for the previous activity.
1163 // Skip it for set() since it's already been taken care of by not counting the original
1164 // dynamic shortcuts in the first loop.
1165 if (operation != ShortcutService.OPERATION_SET) {
1166 final ComponentName oldActivity = original.getActivity();
1167 if (!original.isFloating()) {
1168 incrementCountForActivity(counts, oldActivity, -1);
1169 }
1170 }
1171 incrementCountForActivity(counts, newActivity, 1);
1172 }
1173
1174 // Then make sure none of the activities have more than the max number of shortcuts.
1175 for (int i = counts.size() - 1; i >= 0; i--) {
1176 service.enforceMaxActivityShortcuts(counts.valueAt(i));
1177 }
1178 }
1179
Makoto Onuki157b1622016-06-02 16:13:10 -07001180 /**
1181 * For all the text fields, refresh the string values if they're from resources.
1182 */
1183 public void resolveResourceStrings() {
1184 final ShortcutService s = mShortcutUser.mService;
1185 boolean changed = false;
1186
1187 Resources publisherRes = null;
1188 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1189 final ShortcutInfo si = mShortcuts.valueAt(i);
1190
1191 if (si.hasStringResources()) {
1192 changed = true;
1193
1194 if (publisherRes == null) {
1195 publisherRes = getPackageResources();
1196 if (publisherRes == null) {
1197 break; // Resources couldn't be loaded.
1198 }
1199 }
1200
1201 si.resolveResourceStrings(publisherRes);
1202 si.setTimestamp(s.injectCurrentTimeMillis());
1203 }
1204 }
1205 if (changed) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001206 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001207 }
1208 }
1209
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001210 /** Clears the implicit ranks for all shortcuts. */
1211 public void clearAllImplicitRanks() {
1212 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1213 final ShortcutInfo si = mShortcuts.valueAt(i);
1214 si.clearImplicitRankAndRankChangedFlag();
1215 }
1216 }
1217
1218 /**
1219 * Used to sort shortcuts for rank auto-adjusting.
1220 */
1221 final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
1222 // First, sort by rank.
1223 int ret = Integer.compare(a.getRank(), b.getRank());
1224 if (ret != 0) {
1225 return ret;
1226 }
1227 // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
1228 // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
1229 // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
1230 // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
1231 // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
1232 if (a.isRankChanged() != b.isRankChanged()) {
1233 return a.isRankChanged() ? -1 : 1;
1234 }
1235 // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
1236 // they're passed to the API.
1237 ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
1238 if (ret != 0) {
1239 return ret;
1240 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001241 // If they're still tie, just sort by their IDs.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001242 // This may happen with updateShortcuts() -- see
1243 // the testUpdateShortcuts_noManifestShortcuts() test.
1244 return a.getId().compareTo(b.getId());
1245 };
1246
1247 /**
1248 * Re-calculate the ranks for all shortcuts.
1249 */
1250 public void adjustRanks() {
1251 final ShortcutService s = mShortcutUser.mService;
1252 final long now = s.injectCurrentTimeMillis();
1253
1254 // First, clear ranks for floating shortcuts.
1255 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1256 final ShortcutInfo si = mShortcuts.valueAt(i);
1257 if (si.isFloating()) {
1258 if (si.getRank() != 0) {
1259 si.setTimestamp(now);
1260 si.setRank(0);
1261 }
1262 }
1263 }
1264
1265 // Then adjust ranks. Ranks are unique for each activity, so we first need to sort
1266 // shortcuts to each activity.
1267 // Then sort the shortcuts within each activity with mShortcutRankComparator, and
1268 // assign ranks from 0.
1269 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1270 sortShortcutsToActivities();
1271 for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
1272 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1273
1274 // Sort by ranks and other signals.
1275 Collections.sort(list, mShortcutRankComparator);
1276
1277 int rank = 0;
1278
1279 final int size = list.size();
1280 for (int i = 0; i < size; i++) {
1281 final ShortcutInfo si = list.get(i);
1282 if (si.isManifestShortcut()) {
1283 // Don't adjust ranks for manifest shortcuts.
1284 continue;
1285 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001286 // At this point, it must be dynamic.
1287 if (!si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001288 s.wtf("Non-dynamic shortcut found.");
1289 continue;
1290 }
1291 final int thisRank = rank++;
1292 if (si.getRank() != thisRank) {
1293 si.setTimestamp(now);
1294 si.setRank(thisRank);
1295 }
1296 }
1297 }
1298 }
1299
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07001300 /** @return true if there's any shortcuts that are not manifest shortcuts. */
1301 public boolean hasNonManifestShortcuts() {
1302 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1303 final ShortcutInfo si = mShortcuts.valueAt(i);
1304 if (!si.isDeclaredInManifest()) {
1305 return true;
1306 }
1307 }
1308 return false;
1309 }
1310
Makoto Onuki20b82212017-10-04 15:03:50 -07001311 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -07001312 pw.println();
1313
1314 pw.print(prefix);
1315 pw.print("Package: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001316 pw.print(getPackageName());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001317 pw.print(" UID: ");
1318 pw.print(mPackageUid);
Makoto Onuki31459242016-03-22 11:12:18 -07001319 pw.println();
1320
1321 pw.print(prefix);
1322 pw.print(" ");
1323 pw.print("Calls: ");
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001324 pw.print(getApiCallCount(/*unlimited=*/ false));
Makoto Onuki31459242016-03-22 11:12:18 -07001325 pw.println();
1326
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001327 // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
1328 pw.print(prefix);
1329 pw.print(" ");
1330 pw.print("Last known FG: ");
1331 pw.print(mLastKnownForegroundElapsedTime);
1332 pw.println();
1333
Makoto Onuki31459242016-03-22 11:12:18 -07001334 // This should be after getApiCallCount(), which may update it.
1335 pw.print(prefix);
1336 pw.print(" ");
1337 pw.print("Last reset: [");
1338 pw.print(mLastResetTime);
1339 pw.print("] ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001340 pw.print(ShortcutService.formatTime(mLastResetTime));
Makoto Onuki31459242016-03-22 11:12:18 -07001341 pw.println();
1342
Makoto Onukic51b2872016-05-04 15:24:50 -07001343 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001344 pw.println();
1345
Makoto Onuki39686e82016-04-13 18:03:00 -07001346 pw.print(prefix);
1347 pw.println(" Shortcuts:");
Makoto Onuki31459242016-03-22 11:12:18 -07001348 long totalBitmapSize = 0;
1349 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1350 final int size = shortcuts.size();
1351 for (int i = 0; i < size; i++) {
1352 final ShortcutInfo si = shortcuts.valueAt(i);
Makoto Onuki6208c672017-10-02 16:20:35 -07001353 pw.println(si.toDumpString(prefix + " "));
Makoto Onuki31459242016-03-22 11:12:18 -07001354 if (si.getBitmapPath() != null) {
1355 final long len = new File(si.getBitmapPath()).length();
Makoto Onuki39686e82016-04-13 18:03:00 -07001356 pw.print(prefix);
1357 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001358 pw.print("bitmap size=");
1359 pw.println(len);
1360
1361 totalBitmapSize += len;
1362 }
1363 }
1364 pw.print(prefix);
1365 pw.print(" ");
1366 pw.print("Total bitmap size: ");
1367 pw.print(totalBitmapSize);
1368 pw.print(" (");
Makoto Onukic51b2872016-05-04 15:24:50 -07001369 pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
Makoto Onuki31459242016-03-22 11:12:18 -07001370 pw.println(")");
1371 }
1372
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001373 @Override
Makoto Onuki76269922016-07-15 14:58:54 -07001374 public JSONObject dumpCheckin(boolean clear) throws JSONException {
1375 final JSONObject result = super.dumpCheckin(clear);
1376
1377 int numDynamic = 0;
1378 int numPinned = 0;
1379 int numManifest = 0;
1380 int numBitmaps = 0;
1381 long totalBitmapSize = 0;
1382
1383 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1384 final int size = shortcuts.size();
1385 for (int i = 0; i < size; i++) {
1386 final ShortcutInfo si = shortcuts.valueAt(i);
1387
1388 if (si.isDynamic()) numDynamic++;
1389 if (si.isDeclaredInManifest()) numManifest++;
1390 if (si.isPinned()) numPinned++;
1391
1392 if (si.getBitmapPath() != null) {
1393 numBitmaps++;
1394 totalBitmapSize += new File(si.getBitmapPath()).length();
1395 }
1396 }
1397
1398 result.put(KEY_DYNAMIC, numDynamic);
1399 result.put(KEY_MANIFEST, numManifest);
1400 result.put(KEY_PINNED, numPinned);
1401 result.put(KEY_BITMAPS, numBitmaps);
1402 result.put(KEY_BITMAP_BYTES, totalBitmapSize);
1403
1404 // TODO Log update frequency too.
1405
1406 return result;
1407 }
1408
1409 @Override
Makoto Onuki0acbb142016-03-22 17:02:57 -07001410 public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
1411 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001412 final int size = mShortcuts.size();
1413
1414 if (size == 0 && mApiCallCount == 0) {
1415 return; // nothing to write.
1416 }
1417
1418 out.startTag(null, TAG_ROOT);
1419
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001420 ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
Makoto Onuki31459242016-03-22 11:12:18 -07001421 ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
1422 ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
Makoto Onukie3fffa92018-02-28 16:25:40 -08001423 getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001424
1425 for (int j = 0; j < size; j++) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001426 saveShortcut(out, mShortcuts.valueAt(j), forBackup,
1427 getPackageInfo().isBackupAllowed());
Makoto Onuki31459242016-03-22 11:12:18 -07001428 }
1429
1430 out.endTag(null, TAG_ROOT);
1431 }
1432
Makoto Onukia4f89b12017-10-05 10:37:55 -07001433 private void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup,
1434 boolean appSupportsBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001435 throws IOException, XmlPullParserException {
Makoto Onuki475c3652017-05-08 14:29:03 -07001436
1437 final ShortcutService s = mShortcutUser.mService;
1438
Makoto Onuki0acbb142016-03-22 17:02:57 -07001439 if (forBackup) {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001440 if (!(si.isPinned() && si.isEnabled())) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001441 // We only backup pinned shortcuts that are enabled.
1442 // Note, this means, shortcuts that are restored but are blocked restore, e.g. due
1443 // to a lower version code, will not be ported to a new device.
1444 return;
Makoto Onuki0acbb142016-03-22 17:02:57 -07001445 }
1446 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001447 final boolean shouldBackupDetails =
1448 !forBackup // It's not backup
1449 || appSupportsBackup; // Or, it's a backup and app supports backup.
1450
Makoto Onuki475c3652017-05-08 14:29:03 -07001451 // Note: at this point no shortcuts should have bitmaps pending save, but if they do,
1452 // just remove the bitmap.
1453 if (si.isIconPendingSave()) {
1454 s.removeIconLocked(si);
1455 }
Makoto Onuki31459242016-03-22 11:12:18 -07001456 out.startTag(null, TAG_SHORTCUT);
1457 ShortcutService.writeAttr(out, ATTR_ID, si.getId());
1458 // writeAttr(out, "package", si.getPackageName()); // not needed
Makoto Onuki22fcc682016-05-17 14:52:19 -07001459 ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
Makoto Onuki31459242016-03-22 11:12:18 -07001460 // writeAttr(out, "icon", si.getIcon()); // We don't save it.
1461 ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001462 ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001463 ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001464 ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001465 ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001466 ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
Makoto Onukia4f89b12017-10-05 10:37:55 -07001467 if (shouldBackupDetails) {
1468 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
1469 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
1470 si.getDisabledMessageResourceId());
1471 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
1472 si.getDisabledMessageResName());
1473 }
1474 ShortcutService.writeAttr(out, ATTR_DISABLED_REASON, si.getDisabledReason());
Makoto Onuki31459242016-03-22 11:12:18 -07001475 ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
1476 si.getLastChangedTimestamp());
Adam Hed4586b12019-03-19 12:01:00 -07001477 final LocusId locusId = si.getLocusId();
1478 if (locusId != null) {
1479 ShortcutService.writeAttr(out, ATTR_LOCUS_ID, si.getLocusId().getId());
1480 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001481 if (forBackup) {
1482 // Don't write icon information. Also drop the dynamic flag.
Makoto Onukia4f89b12017-10-05 10:37:55 -07001483
1484 int flags = si.getFlags() &
1485 ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
Makoto Onuki475c3652017-05-08 14:29:03 -07001486 | ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE
Makoto Onukia4f89b12017-10-05 10:37:55 -07001487 | ShortcutInfo.FLAG_DYNAMIC);
1488 ShortcutService.writeAttr(out, ATTR_FLAGS, flags);
1489
1490 // Set the publisher version code at every backup.
Dianne Hackborn3accca02013-09-20 09:32:11 -07001491 final long packageVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -07001492 if (packageVersionCode == 0) {
1493 s.wtf("Package version code should be available at this point.");
1494 // However, 0 is a valid version code, so we just go ahead with it...
1495 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001496 } else {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001497 // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
1498 // as dynamic.
1499 ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
1500
Makoto Onuki0acbb142016-03-22 17:02:57 -07001501 ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
Makoto Onuki157b1622016-06-02 16:13:10 -07001502 ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
1503 ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001504 ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
1505 }
Makoto Onuki31459242016-03-22 11:12:18 -07001506
Makoto Onukia4f89b12017-10-05 10:37:55 -07001507 if (shouldBackupDetails) {
1508 {
1509 final Set<String> cat = si.getCategories();
1510 if (cat != null && cat.size() > 0) {
1511 out.startTag(null, TAG_CATEGORIES);
1512 XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
1513 NAME_CATEGORIES, out);
1514 out.endTag(null, TAG_CATEGORIES);
1515 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001516 }
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001517 if (!forBackup) { // Don't backup the persons field.
1518 final Person[] persons = si.getPersons();
1519 if (!ArrayUtils.isEmpty(persons)) {
1520 for (int i = 0; i < persons.length; i++) {
1521 final Person p = persons[i];
1522
1523 out.startTag(null, TAG_PERSON);
1524 ShortcutService.writeAttr(out, ATTR_PERSON_NAME, p.getName());
1525 ShortcutService.writeAttr(out, ATTR_PERSON_URI, p.getUri());
1526 ShortcutService.writeAttr(out, ATTR_PERSON_KEY, p.getKey());
1527 ShortcutService.writeAttr(out, ATTR_PERSON_IS_BOT, p.isBot());
1528 ShortcutService.writeAttr(out, ATTR_PERSON_IS_IMPORTANT, p.isImportant());
1529 out.endTag(null, TAG_PERSON);
1530 }
1531 }
1532 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001533 final Intent[] intentsNoExtras = si.getIntentsNoExtras();
1534 final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases();
1535 final int numIntents = intentsNoExtras.length;
1536 for (int i = 0; i < numIntents; i++) {
1537 out.startTag(null, TAG_INTENT);
1538 ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]);
1539 ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]);
1540 out.endTag(null, TAG_INTENT);
1541 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001542
Makoto Onukia4f89b12017-10-05 10:37:55 -07001543 ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
1544 }
Hakan Seyalioglu58fc95d2016-12-13 15:23:22 -08001545
Makoto Onuki31459242016-03-22 11:12:18 -07001546 out.endTag(null, TAG_SHORTCUT);
1547 }
1548
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001549 public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
1550 XmlPullParser parser, boolean fromBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001551 throws IOException, XmlPullParserException {
1552
1553 final String packageName = ShortcutService.parseStringAttribute(parser,
1554 ATTR_NAME);
1555
Makoto Onukic51b2872016-05-04 15:24:50 -07001556 final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001557 shortcutUser.getUserId(), packageName);
Makoto Onuki31459242016-03-22 11:12:18 -07001558
Makoto Onuki31459242016-03-22 11:12:18 -07001559 ret.mApiCallCount =
1560 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
1561 ret.mLastResetTime =
1562 ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
1563
Makoto Onukia4f89b12017-10-05 10:37:55 -07001564
Makoto Onuki31459242016-03-22 11:12:18 -07001565 final int outerDepth = parser.getDepth();
1566 int type;
1567 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1568 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1569 if (type != XmlPullParser.START_TAG) {
1570 continue;
1571 }
1572 final int depth = parser.getDepth();
1573 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001574 if (depth == outerDepth + 1) {
1575 switch (tag) {
1576 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -07001577 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001578
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001579 continue;
1580 case TAG_SHORTCUT:
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001581 final ShortcutInfo si = parseShortcut(parser, packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001582 shortcutUser.getUserId(), fromBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001583
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001584 // Don't use addShortcut(), we don't need to save the icon.
1585 ret.mShortcuts.put(si.getId(), si);
1586 continue;
1587 }
Makoto Onuki31459242016-03-22 11:12:18 -07001588 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001589 ShortcutService.warnForInvalidTag(depth, tag);
1590 }
Makoto Onuki31459242016-03-22 11:12:18 -07001591 return ret;
1592 }
1593
Makoto Onukiabe84422016-04-07 09:41:19 -07001594 private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001595 @UserIdInt int userId, boolean fromBackup)
1596 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001597 String id;
1598 ComponentName activityComponent;
1599 // Icon icon;
1600 String title;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001601 int titleResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001602 String titleResName;
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001603 String text;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001604 int textResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001605 String textResName;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001606 String disabledMessage;
1607 int disabledMessageResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001608 String disabledMessageResName;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001609 int disabledReason;
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001610 Intent intentLegacy;
1611 PersistableBundle intentPersistableExtrasLegacy = null;
1612 ArrayList<Intent> intents = new ArrayList<>();
Makoto Onuki20c95f82016-05-11 16:51:01 -07001613 int rank;
Makoto Onuki31459242016-03-22 11:12:18 -07001614 PersistableBundle extras = null;
1615 long lastChangedTimestamp;
1616 int flags;
Makoto Onuki157b1622016-06-02 16:13:10 -07001617 int iconResId;
1618 String iconResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001619 String bitmapPath;
Adam Hed4586b12019-03-19 12:01:00 -07001620 final String locusIdString;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001621 int backupVersionCode;
Makoto Onukibe73a802016-04-15 14:46:35 -07001622 ArraySet<String> categories = null;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001623 ArrayList<Person> persons = new ArrayList<>();
Makoto Onuki31459242016-03-22 11:12:18 -07001624
1625 id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
1626 activityComponent = ShortcutService.parseComponentNameAttribute(parser,
1627 ATTR_ACTIVITY);
1628 title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001629 titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001630 titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001631 text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001632 textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001633 textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001634 disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
1635 disabledMessageResId = ShortcutService.parseIntAttribute(parser,
1636 ATTR_DISABLED_MESSAGE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001637 disabledMessageResName = ShortcutService.parseStringAttribute(parser,
1638 ATTR_DISABLED_MESSAGE_RES_NAME);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001639 disabledReason = ShortcutService.parseIntAttribute(parser, ATTR_DISABLED_REASON);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001640 intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001641 rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001642 lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
Makoto Onuki31459242016-03-22 11:12:18 -07001643 flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
Makoto Onuki157b1622016-06-02 16:13:10 -07001644 iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
1645 iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001646 bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
Adam Hed4586b12019-03-19 12:01:00 -07001647 locusIdString = ShortcutService.parseStringAttribute(parser, ATTR_LOCUS_ID);
Makoto Onuki31459242016-03-22 11:12:18 -07001648
1649 final int outerDepth = parser.getDepth();
1650 int type;
1651 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1652 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1653 if (type != XmlPullParser.START_TAG) {
1654 continue;
1655 }
1656 final int depth = parser.getDepth();
1657 final String tag = parser.getName();
1658 if (ShortcutService.DEBUG_LOAD) {
1659 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1660 depth, type, tag));
1661 }
1662 switch (tag) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001663 case TAG_INTENT_EXTRAS_LEGACY:
1664 intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser);
1665 continue;
1666 case TAG_INTENT:
1667 intents.add(parseIntent(parser));
Makoto Onuki31459242016-03-22 11:12:18 -07001668 continue;
1669 case TAG_EXTRAS:
1670 extras = PersistableBundle.restoreFromXml(parser);
1671 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001672 case TAG_CATEGORIES:
1673 // This just contains string-array.
1674 continue;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001675 case TAG_PERSON:
1676 persons.add(parsePerson(parser));
1677 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001678 case TAG_STRING_ARRAY_XMLUTILS:
1679 if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
1680 ATTR_NAME_XMLUTILS))) {
Makoto Onukibe73a802016-04-15 14:46:35 -07001681 final String[] ar = XmlUtils.readThisStringArrayXml(
1682 parser, TAG_STRING_ARRAY_XMLUTILS, null);
1683 categories = new ArraySet<>(ar.length);
1684 for (int i = 0; i < ar.length; i++) {
1685 categories.add(ar[i]);
1686 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001687 }
1688 continue;
Makoto Onuki31459242016-03-22 11:12:18 -07001689 }
1690 throw ShortcutService.throwForInvalidTag(depth, tag);
1691 }
Makoto Onukibe73a802016-04-15 14:46:35 -07001692
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001693 if (intentLegacy != null) {
1694 // For the legacy file format which supported only one intent per shortcut.
1695 ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy);
1696 intents.clear();
1697 intents.add(intentLegacy);
1698 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001699
Makoto Onukia4f89b12017-10-05 10:37:55 -07001700
1701 if ((disabledReason == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)
1702 && ((flags & ShortcutInfo.FLAG_DISABLED) != 0)) {
1703 // We didn't used to have the disabled reason, so if a shortcut is disabled
1704 // and has no reason, we assume it was disabled by publisher.
1705 disabledReason = ShortcutInfo.DISABLED_REASON_BY_APP;
1706 }
1707
1708 // All restored shortcuts are initially "shadow".
1709 if (fromBackup) {
1710 flags |= ShortcutInfo.FLAG_SHADOW;
1711 }
1712
Adam Hed4586b12019-03-19 12:01:00 -07001713 final LocusId locusId = locusIdString == null ? null : new LocusId(locusIdString);
Felipe Leme90205ef2019-03-05 09:59:52 -08001714
Makoto Onuki31459242016-03-22 11:12:18 -07001715 return new ShortcutInfo(
Felipe Leme90205ef2019-03-05 09:59:52 -08001716 userId, id, packageName, activityComponent, /* icon= */ null,
Makoto Onuki157b1622016-06-02 16:13:10 -07001717 title, titleResId, titleResName, text, textResId, textResName,
1718 disabledMessage, disabledMessageResId, disabledMessageResName,
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001719 categories,
1720 intents.toArray(new Intent[intents.size()]),
1721 rank, extras, lastChangedTimestamp, flags,
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001722 iconResId, iconResName, bitmapPath, disabledReason,
Felipe Leme90205ef2019-03-05 09:59:52 -08001723 persons.toArray(new Person[persons.size()]), locusId);
Makoto Onuki31459242016-03-22 11:12:18 -07001724 }
Makoto Onuki2e210c42016-03-30 08:30:36 -07001725
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001726 private static Intent parseIntent(XmlPullParser parser)
1727 throws IOException, XmlPullParserException {
1728
1729 Intent intent = ShortcutService.parseIntentAttribute(parser,
1730 ATTR_INTENT_NO_EXTRA);
1731
1732 final int outerDepth = parser.getDepth();
1733 int type;
1734 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1735 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1736 if (type != XmlPullParser.START_TAG) {
1737 continue;
1738 }
1739 final int depth = parser.getDepth();
1740 final String tag = parser.getName();
1741 if (ShortcutService.DEBUG_LOAD) {
1742 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1743 depth, type, tag));
1744 }
1745 switch (tag) {
1746 case TAG_EXTRAS:
1747 ShortcutInfo.setIntentExtras(intent,
1748 PersistableBundle.restoreFromXml(parser));
1749 continue;
1750 }
1751 throw ShortcutService.throwForInvalidTag(depth, tag);
1752 }
1753 return intent;
1754 }
1755
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001756 private static Person parsePerson(XmlPullParser parser)
1757 throws IOException, XmlPullParserException {
1758 CharSequence name = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_NAME);
1759 String uri = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_URI);
1760 String key = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_KEY);
1761 boolean isBot = ShortcutService.parseBooleanAttribute(parser, ATTR_PERSON_IS_BOT);
1762 boolean isImportant = ShortcutService.parseBooleanAttribute(parser,
1763 ATTR_PERSON_IS_IMPORTANT);
1764
1765 Person.Builder builder = new Person.Builder();
1766 builder.setName(name).setUri(uri).setKey(key).setBot(isBot).setImportant(isImportant);
1767 return builder.build();
1768 }
1769
Makoto Onuki2e210c42016-03-30 08:30:36 -07001770 @VisibleForTesting
1771 List<ShortcutInfo> getAllShortcutsForTest() {
1772 return new ArrayList<>(mShortcuts.values());
1773 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001774
Mehdi Alizadeh32774622018-11-05 17:32:01 -08001775 @VisibleForTesting
1776 List<ShareTargetInfo> getAllShareTargetsForTest() {
1777 return new ArrayList<>(mShareTargets);
1778 }
1779
Makoto Onuki7001a612016-05-27 13:24:28 -07001780 @Override
1781 public void verifyStates() {
1782 super.verifyStates();
1783
1784 boolean failed = false;
1785
Makoto Onuki255461f2017-01-10 11:47:25 -08001786 final ShortcutService s = mShortcutUser.mService;
1787
Makoto Onuki7001a612016-05-27 13:24:28 -07001788 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1789 sortShortcutsToActivities();
1790
1791 // Make sure each activity won't have more than max shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001792 for (int outer = all.size() - 1; outer >= 0; outer--) {
1793 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1794 if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001795 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001796 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
1797 + " has " + all.valueAt(outer).size() + " shortcuts.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001798 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001799
1800 // Sort by rank.
1801 Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
1802
1803 // Split into two arrays for each kind.
1804 final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
1805 dynamicList.removeIf((si) -> !si.isDynamic());
1806
1807 final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
1808 dynamicList.removeIf((si) -> !si.isManifestShortcut());
1809
1810 verifyRanksSequential(dynamicList);
1811 verifyRanksSequential(manifestList);
Makoto Onuki7001a612016-05-27 13:24:28 -07001812 }
1813
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001814 // Verify each shortcut's status.
Makoto Onuki7001a612016-05-27 13:24:28 -07001815 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1816 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onuki99302b52017-03-29 12:42:26 -07001817 if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001818 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001819 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki99302b52017-03-29 12:42:26 -07001820 + " is not manifest, dynamic or pinned.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001821 }
Makoto Onukiff14f732016-06-30 17:07:25 -07001822 if (si.isDeclaredInManifest() && si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001823 failed = true;
1824 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1825 + " is both dynamic and manifest at the same time.");
1826 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001827 if (si.getActivity() == null && !si.isFloating()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001828 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001829 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki255461f2017-01-10 11:47:25 -08001830 + " has null activity, but not floating.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001831 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001832 if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001833 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001834 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001835 + " is not floating, but is disabled.");
1836 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001837 if (si.isFloating() && si.getRank() != 0) {
1838 failed = true;
1839 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1840 + " is floating, but has rank=" + si.getRank());
1841 }
Makoto Onukidd097812016-06-29 13:10:09 -07001842 if (si.getIcon() != null) {
1843 failed = true;
1844 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1845 + " still has an icon");
1846 }
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001847 if (si.hasAdaptiveBitmap() && !si.hasIconFile()) {
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001848 failed = true;
1849 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001850 + " has adaptive bitmap but was not saved to a file.");
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001851 }
Makoto Onukidd097812016-06-29 13:10:09 -07001852 if (si.hasIconFile() && si.hasIconResource()) {
1853 failed = true;
1854 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1855 + " has both resource and bitmap icons");
1856 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001857 if (si.isEnabled()
1858 != (si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)) {
1859 failed = true;
1860 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1861 + " isEnabled() and getDisabledReason() disagree: "
1862 + si.isEnabled() + " vs " + si.getDisabledReason());
1863 }
1864 if ((si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
1865 && (getPackageInfo().getBackupSourceVersionCode()
1866 == ShortcutInfo.VERSION_CODE_UNKNOWN)) {
1867 failed = true;
1868 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1869 + " RESTORED_VERSION_LOWER with no backup source version code.");
1870 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001871 if (s.isDummyMainActivity(si.getActivity())) {
1872 failed = true;
1873 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1874 + " has a dummy target activity");
1875 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001876 }
1877
1878 if (failed) {
Makoto Onuki9fd90192017-01-06 18:31:03 +00001879 throw new IllegalStateException("See logcat for errors");
Makoto Onuki7001a612016-05-27 13:24:28 -07001880 }
1881 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001882
1883 private boolean verifyRanksSequential(List<ShortcutInfo> list) {
1884 boolean failed = false;
1885
1886 for (int i = 0; i < list.size(); i++) {
1887 final ShortcutInfo si = list.get(i);
1888 if (si.getRank() != i) {
1889 failed = true;
1890 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1891 + " rank=" + si.getRank() + " but expected to be "+ i);
1892 }
1893 }
1894 return failed;
1895 }
Makoto Onuki31459242016-03-22 11:12:18 -07001896}