blob: d9a5eb901344d7da73958845a36b5502b02e863f [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;
Makoto Onuki22fcc682016-05-17 14:52:19 -070025import android.content.pm.PackageInfo;
Makoto Onuki31459242016-03-22 11:12:18 -070026import android.content.pm.ShortcutInfo;
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -080027import android.content.pm.ShortcutManager;
Makoto Onuki157b1622016-06-02 16:13:10 -070028import android.content.res.Resources;
Makoto Onuki31459242016-03-22 11:12:18 -070029import android.os.PersistableBundle;
30import android.text.format.Formatter;
31import android.util.ArrayMap;
32import android.util.ArraySet;
Makoto Onuki7001a612016-05-27 13:24:28 -070033import android.util.Log;
Makoto Onuki31459242016-03-22 11:12:18 -070034import android.util.Slog;
35
Makoto Onuki2e210c42016-03-30 08:30:36 -070036import com.android.internal.annotations.VisibleForTesting;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080037import com.android.internal.util.ArrayUtils;
Makoto Onuki22fcc682016-05-17 14:52:19 -070038import com.android.internal.util.Preconditions;
Makoto Onukib6d35232016-04-04 15:57:17 -070039import com.android.internal.util.XmlUtils;
Makoto Onuki20b82212017-10-04 15:03:50 -070040import com.android.server.pm.ShortcutService.DumpFilter;
Makoto Onuki7001a612016-05-27 13:24:28 -070041import com.android.server.pm.ShortcutService.ShortcutOperation;
Makoto Onuki4e6cef42016-07-13 16:14:01 -070042import com.android.server.pm.ShortcutService.Stats;
Makoto Onuki2e210c42016-03-30 08:30:36 -070043
Makoto Onuki76269922016-07-15 14:58:54 -070044import org.json.JSONException;
45import org.json.JSONObject;
Makoto Onuki31459242016-03-22 11:12:18 -070046import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48import org.xmlpull.v1.XmlSerializer;
49
50import java.io.File;
51import java.io.IOException;
52import java.io.PrintWriter;
53import java.util.ArrayList;
Makoto Onuki7001a612016-05-27 13:24:28 -070054import java.util.Collections;
55import java.util.Comparator;
Makoto Onuki31459242016-03-22 11:12:18 -070056import java.util.List;
Makoto Onukibe73a802016-04-15 14:46:35 -070057import java.util.Set;
Makoto Onuki31459242016-03-22 11:12:18 -070058import java.util.function.Predicate;
59
60/**
61 * Package information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070062 * User information used by {@link ShortcutService}.
63 *
64 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070065 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070066class ShortcutPackage extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070067 private static final String TAG = ShortcutService.TAG;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070068 private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
Makoto Onuki31459242016-03-22 11:12:18 -070069
70 static final String TAG_ROOT = "package";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070071 private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras";
72 private static final String TAG_INTENT = "intent";
Makoto Onuki31459242016-03-22 11:12:18 -070073 private static final String TAG_EXTRAS = "extras";
74 private static final String TAG_SHORTCUT = "shortcut";
Makoto Onukib6d35232016-04-04 15:57:17 -070075 private static final String TAG_CATEGORIES = "categories";
Mehdi Alizadehebb4b602019-02-05 15:52:18 -080076 private static final String TAG_PERSON = "person";
Makoto Onuki31459242016-03-22 11:12:18 -070077
78 private static final String ATTR_NAME = "name";
Makoto Onuki31459242016-03-22 11:12:18 -070079 private static final String ATTR_CALL_COUNT = "call-count";
80 private static final String ATTR_LAST_RESET = "last-reset";
81 private static final String ATTR_ID = "id";
82 private static final String ATTR_ACTIVITY = "activity";
83 private static final String ATTR_TITLE = "title";
Makoto Onuki20c95f82016-05-11 16:51:01 -070084 private static final String ATTR_TITLE_RES_ID = "titleid";
Makoto Onuki157b1622016-06-02 16:13:10 -070085 private static final String ATTR_TITLE_RES_NAME = "titlename";
Makoto Onukie3ae7ec2016-03-29 15:45:25 -070086 private static final String ATTR_TEXT = "text";
Makoto Onuki20c95f82016-05-11 16:51:01 -070087 private static final String ATTR_TEXT_RES_ID = "textid";
Makoto Onuki157b1622016-06-02 16:13:10 -070088 private static final String ATTR_TEXT_RES_NAME = "textname";
Makoto Onuki20c95f82016-05-11 16:51:01 -070089 private static final String ATTR_DISABLED_MESSAGE = "dmessage";
90 private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
Makoto Onuki157b1622016-06-02 16:13:10 -070091 private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
Makoto Onukia4f89b12017-10-05 10:37:55 -070092 private static final String ATTR_DISABLED_REASON = "disabled-reason";
Makoto Onuki440a1ea2016-07-20 14:21:18 -070093 private static final String ATTR_INTENT_LEGACY = "intent";
94 private static final String ATTR_INTENT_NO_EXTRA = "intent-base";
Makoto Onuki20c95f82016-05-11 16:51:01 -070095 private static final String ATTR_RANK = "rank";
Makoto Onuki31459242016-03-22 11:12:18 -070096 private static final String ATTR_TIMESTAMP = "timestamp";
97 private static final String ATTR_FLAGS = "flags";
Makoto Onuki157b1622016-06-02 16:13:10 -070098 private static final String ATTR_ICON_RES_ID = "icon-res";
99 private static final String ATTR_ICON_RES_NAME = "icon-resname";
Makoto Onuki31459242016-03-22 11:12:18 -0700100 private static final String ATTR_BITMAP_PATH = "bitmap-path";
101
Mehdi Alizadehebb4b602019-02-05 15:52:18 -0800102 private static final String ATTR_PERSON_NAME = "name";
103 private static final String ATTR_PERSON_URI = "uri";
104 private static final String ATTR_PERSON_KEY = "key";
105 private static final String ATTR_PERSON_IS_BOT = "is-bot";
106 private static final String ATTR_PERSON_IS_IMPORTANT = "is-important";
107
Makoto Onukib6d35232016-04-04 15:57:17 -0700108 private static final String NAME_CATEGORIES = "categories";
109
110 private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
111 private static final String ATTR_NAME_XMLUTILS = "name";
112
Makoto Onuki76269922016-07-15 14:58:54 -0700113 private static final String KEY_DYNAMIC = "dynamic";
114 private static final String KEY_MANIFEST = "manifest";
115 private static final String KEY_PINNED = "pinned";
116 private static final String KEY_BITMAPS = "bitmaps";
117 private static final String KEY_BITMAP_BYTES = "bitmapBytes";
118
Makoto Onuki31459242016-03-22 11:12:18 -0700119 /**
120 * All the shortcuts from the package, keyed on IDs.
121 */
122 final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
123
124 /**
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800125 * All the share targets from the package
126 */
127 private final ArrayList<ShareTargetInfo> mShareTargets = new ArrayList<>(0);
128
129 /**
Makoto Onuki31459242016-03-22 11:12:18 -0700130 * # of times the package has called rate-limited APIs.
131 */
132 private int mApiCallCount;
133
134 /**
135 * When {@link #mApiCallCount} was reset last time.
136 */
137 private long mLastResetTime;
138
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700139 private final int mPackageUid;
140
141 private long mLastKnownForegroundElapsedTime;
142
Makoto Onukic51b2872016-05-04 15:24:50 -0700143 private ShortcutPackage(ShortcutUser shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700144 int packageUserId, String packageName, ShortcutPackageInfo spi) {
145 super(shortcutUser, packageUserId, packageName,
146 spi != null ? spi : ShortcutPackageInfo.newEmpty());
147
Makoto Onukic51b2872016-05-04 15:24:50 -0700148 mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700149 }
150
Makoto Onukic51b2872016-05-04 15:24:50 -0700151 public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
152 this(shortcutUser, packageUserId, packageName, null);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700153 }
154
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700155 @Override
156 public int getOwnerUserId() {
157 // For packages, always owner user == package user.
158 return getPackageUserId();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700159 }
160
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700161 public int getPackageUid() {
162 return mPackageUid;
163 }
164
Makoto Onuki157b1622016-06-02 16:13:10 -0700165 @Nullable
166 public Resources getPackageResources() {
167 return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
168 getPackageName(), getPackageUserId());
169 }
170
Makoto Onuki50a320e2017-05-31 14:38:42 -0700171 public int getShortcutCount() {
172 return mShortcuts.size();
173 }
174
Makoto Onuki2e210c42016-03-30 08:30:36 -0700175 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700176 protected boolean canRestoreAnyVersion() {
177 return false;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700178 }
179
180 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700181 protected void onRestored(int restoreBlockReason) {
182 // Shortcuts have been restored.
183 // - Unshadow all shortcuts.
184 // - Set disabled reason.
185 // - Disable if needed.
186 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
187 ShortcutInfo si = mShortcuts.valueAt(i);
188 si.clearFlags(ShortcutInfo.FLAG_SHADOW);
189
190 si.setDisabledReason(restoreBlockReason);
191 if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
192 si.addFlags(ShortcutInfo.FLAG_DISABLED);
193 }
194 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700195 // Because some launchers may not have been restored (e.g. allowBackup=false),
196 // we need to re-calculate the pinned shortcuts.
Makoto Onukic51b2872016-05-04 15:24:50 -0700197 refreshPinnedFlags();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700198 }
199
Makoto Onukid99c6f02016-03-28 11:02:54 -0700200 /**
201 * Note this does *not* provide a correct view to the calling launcher.
202 */
Makoto Onuki31459242016-03-22 11:12:18 -0700203 @Nullable
204 public ShortcutInfo findShortcutById(String id) {
205 return mShortcuts.get(id);
206 }
207
Makoto Onukia4f89b12017-10-05 10:37:55 -0700208 public boolean isShortcutExistsAndInvisibleToPublisher(String id) {
209 ShortcutInfo si = findShortcutById(id);
210 return si != null && !si.isVisibleToPublisher();
211 }
212
213 public boolean isShortcutExistsAndVisibleToPublisher(String id) {
214 ShortcutInfo si = findShortcutById(id);
215 return si != null && si.isVisibleToPublisher();
216 }
217
218 private void ensureNotImmutable(@Nullable ShortcutInfo shortcut, boolean ignoreInvisible) {
219 if (shortcut != null && shortcut.isImmutable()
220 && (!ignoreInvisible || shortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700221 throw new IllegalArgumentException(
222 "Manifest shortcut ID=" + shortcut.getId()
223 + " may not be manipulated via APIs");
224 }
225 }
226
Makoto Onukia4f89b12017-10-05 10:37:55 -0700227 public void ensureNotImmutable(@NonNull String id, boolean ignoreInvisible) {
228 ensureNotImmutable(mShortcuts.get(id), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700229 }
230
Makoto Onukia4f89b12017-10-05 10:37:55 -0700231 public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds,
232 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700233 for (int i = shortcutIds.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700234 ensureNotImmutable(shortcutIds.get(i), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700235 }
236 }
237
Makoto Onukia4f89b12017-10-05 10:37:55 -0700238 public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts,
239 boolean ignoreInvisible) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700240 for (int i = shortcuts.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700241 ensureNotImmutable(shortcuts.get(i).getId(), ignoreInvisible);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700242 }
243 }
244
Makoto Onukia4f89b12017-10-05 10:37:55 -0700245 /**
246 * Delete a shortcut by ID. This will *always* remove it even if it's immutable or invisible.
247 */
248 private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) {
Makoto Onuki31459242016-03-22 11:12:18 -0700249 final ShortcutInfo shortcut = mShortcuts.remove(id);
250 if (shortcut != null) {
Makoto Onuki475c3652017-05-08 14:29:03 -0700251 mShortcutUser.mService.removeIconLocked(shortcut);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700252 shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
Makoto Onuki99302b52017-03-29 12:42:26 -0700253 | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki31459242016-03-22 11:12:18 -0700254 }
255 return shortcut;
256 }
257
Makoto Onukia4f89b12017-10-05 10:37:55 -0700258 /**
259 * Force replace a shortcut. If there's already a shortcut with the same ID, it'll be removed,
260 * even if it's invisible.
261 */
262 private void forceReplaceShortcutInner(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700263 final ShortcutService s = mShortcutUser.mService;
264
Makoto Onukia4f89b12017-10-05 10:37:55 -0700265 forceDeleteShortcutInner(newShortcut.getId());
Makoto Onuki157b1622016-06-02 16:13:10 -0700266
267 // Extract Icon and update the icon res ID and the bitmap path.
Makoto Onuki475c3652017-05-08 14:29:03 -0700268 s.saveIconAndFixUpShortcutLocked(newShortcut);
Makoto Onuki157b1622016-06-02 16:13:10 -0700269 s.fixUpShortcutResourceNamesAndValues(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700270 mShortcuts.put(newShortcut.getId(), newShortcut);
271 }
272
273 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700274 * Add a shortcut. If there's already a one with the same ID, it'll be removed, even if it's
275 * invisible.
Makoto Onuki31459242016-03-22 11:12:18 -0700276 *
277 * It checks the max number of dynamic shortcuts.
278 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700279 public void addOrReplaceDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki39686e82016-04-13 18:03:00 -0700280
Makoto Onuki22fcc682016-05-17 14:52:19 -0700281 Preconditions.checkArgument(newShortcut.isEnabled(),
282 "add/setDynamicShortcuts() cannot publish disabled shortcuts");
283
Makoto Onuki99302b52017-03-29 12:42:26 -0700284 newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki31459242016-03-22 11:12:18 -0700285
286 final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
287
288 final boolean wasPinned;
Makoto Onuki31459242016-03-22 11:12:18 -0700289
290 if (oldShortcut == null) {
291 wasPinned = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700292 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700293 // It's an update case.
294 // Make sure the target is updatable. (i.e. should be mutable.)
Makoto Onukia4f89b12017-10-05 10:37:55 -0700295 oldShortcut.ensureUpdatableWith(newShortcut, /*isUpdating=*/ false);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700296
Makoto Onuki31459242016-03-22 11:12:18 -0700297 wasPinned = oldShortcut.isPinned();
Makoto Onuki31459242016-03-22 11:12:18 -0700298 }
299
Makoto Onuki157b1622016-06-02 16:13:10 -0700300 // If it was originally pinned, the new one should be pinned too.
Makoto Onuki31459242016-03-22 11:12:18 -0700301 if (wasPinned) {
302 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
303 }
304
Makoto Onukia4f89b12017-10-05 10:37:55 -0700305 forceReplaceShortcutInner(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700306 }
307
308 /**
309 * Remove all shortcuts that aren't pinned nor dynamic.
310 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700311 private void removeOrphans() {
Makoto Onuki31459242016-03-22 11:12:18 -0700312 ArrayList<String> removeList = null; // Lazily initialize.
313
314 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
315 final ShortcutInfo si = mShortcuts.valueAt(i);
316
Makoto Onuki22fcc682016-05-17 14:52:19 -0700317 if (si.isAlive()) continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700318
319 if (removeList == null) {
320 removeList = new ArrayList<>();
321 }
322 removeList.add(si.getId());
323 }
324 if (removeList != null) {
325 for (int i = removeList.size() - 1; i >= 0; i--) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700326 forceDeleteShortcutInner(removeList.get(i));
Makoto Onuki31459242016-03-22 11:12:18 -0700327 }
328 }
329 }
330
331 /**
332 * Remove all dynamic shortcuts.
333 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700334 public void deleteAllDynamicShortcuts(boolean ignoreInvisible) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700335 final long now = mShortcutUser.mService.injectCurrentTimeMillis();
336
Makoto Onuki22fcc682016-05-17 14:52:19 -0700337 boolean changed = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700338 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700339 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700340 if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700341 changed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700342
343 si.setTimestamp(now);
Makoto Onuki99302b52017-03-29 12:42:26 -0700344 si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700345 si.setRank(0); // It may still be pinned, so clear the rank.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700346 }
Makoto Onuki31459242016-03-22 11:12:18 -0700347 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700348 if (changed) {
349 removeOrphans();
350 }
Makoto Onuki31459242016-03-22 11:12:18 -0700351 }
352
353 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700354 * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
355 * is pinned, it'll remain as a pinned shortcut, and is still enabled.
Makoto Onukib08790c2016-06-23 14:05:46 -0700356 *
357 * @return true if it's actually removed because it wasn't pinned, or false if it's still
358 * pinned.
Makoto Onuki31459242016-03-22 11:12:18 -0700359 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700360 public boolean deleteDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700361 final ShortcutInfo removed = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700362 shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false, ignoreInvisible,
363 ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukib08790c2016-06-23 14:05:46 -0700364 return removed == null;
365 }
366
367 /**
368 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
369 * is pinned, it'll remain as a pinned shortcut, but will be disabled.
370 *
371 * @return true if it's actually removed because it wasn't pinned, or false if it's still
372 * pinned.
373 */
Makoto Onukia4f89b12017-10-05 10:37:55 -0700374 private boolean disableDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible,
375 int disabledReason) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700376 final ShortcutInfo disabled = deleteOrDisableWithId(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700377 shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false, ignoreInvisible,
378 disabledReason);
Makoto Onukib08790c2016-06-23 14:05:46 -0700379 return disabled == null;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700380 }
381
Makoto Onuki7001a612016-05-27 13:24:28 -0700382 /**
383 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
384 * is pinned, it'll remain as a pinned shortcut but will be disabled.
385 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700386 public void disableWithId(@NonNull String shortcutId, String disabledMessage,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700387 int disabledMessageResId, boolean overrideImmutable, boolean ignoreInvisible,
388 int disabledReason) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700389 final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700390 overrideImmutable, ignoreInvisible, disabledReason);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700391
392 if (disabled != null) {
393 if (disabledMessage != null) {
394 disabled.setDisabledMessage(disabledMessage);
395 } else if (disabledMessageResId != 0) {
396 disabled.setDisabledMessageResId(disabledMessageResId);
Makoto Onuki157b1622016-06-02 16:13:10 -0700397
398 mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700399 }
400 }
401 }
402
403 @Nullable
404 private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700405 boolean overrideImmutable, boolean ignoreInvisible, int disabledReason) {
406 Preconditions.checkState(
407 (disable == (disabledReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED)),
408 "disable and disabledReason disagree: " + disable + " vs " + disabledReason);
Makoto Onuki31459242016-03-22 11:12:18 -0700409 final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
410
Makoto Onukia4f89b12017-10-05 10:37:55 -0700411 if (oldShortcut == null || !oldShortcut.isEnabled()
412 && (ignoreInvisible && !oldShortcut.isVisibleToPublisher())) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700413 return null; // Doesn't exist or already disabled.
Makoto Onuki31459242016-03-22 11:12:18 -0700414 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700415 if (!overrideImmutable) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700416 ensureNotImmutable(oldShortcut, /*ignoreInvisible=*/ true);
Makoto Onuki31459242016-03-22 11:12:18 -0700417 }
418 if (oldShortcut.isPinned()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700419
420 oldShortcut.setRank(0);
Makoto Onuki99302b52017-03-29 12:42:26 -0700421 oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700422 if (disable) {
423 oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700424 // Do not overwrite the disabled reason if one is alreay set.
425 if (oldShortcut.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
426 oldShortcut.setDisabledReason(disabledReason);
427 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700428 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700429 oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
430
Makoto Onuki255461f2017-01-10 11:47:25 -0800431 // See ShortcutRequestPinProcessor.directPinShortcut().
432 if (mShortcutUser.mService.isDummyMainActivity(oldShortcut.getActivity())) {
433 oldShortcut.setActivity(null);
434 }
435
Makoto Onuki22fcc682016-05-17 14:52:19 -0700436 return oldShortcut;
Makoto Onuki31459242016-03-22 11:12:18 -0700437 } else {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700438 forceDeleteShortcutInner(shortcutId);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700439 return null;
440 }
441 }
442
443 public void enableWithId(@NonNull String shortcutId) {
444 final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
445 if (shortcut != null) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700446 ensureNotImmutable(shortcut, /*ignoreInvisible=*/ true);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700447 shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700448 shortcut.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onuki31459242016-03-22 11:12:18 -0700449 }
450 }
451
Makoto Onukia4f89b12017-10-05 10:37:55 -0700452 public void updateInvisibleShortcutForPinRequestWith(@NonNull ShortcutInfo shortcut) {
453 final ShortcutInfo source = mShortcuts.get(shortcut.getId());
454 Preconditions.checkNotNull(source);
455
456 mShortcutUser.mService.validateShortcutForPinRequest(shortcut);
457
458 shortcut.addFlags(ShortcutInfo.FLAG_PINNED);
459
460 forceReplaceShortcutInner(shortcut);
461
462 adjustRanks();
463 }
464
Makoto Onuki31459242016-03-22 11:12:18 -0700465 /**
466 * Called after a launcher updates the pinned set. For each shortcut in this package,
467 * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it.
468 *
469 * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
470 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700471 public void refreshPinnedFlags() {
Makoto Onuki31459242016-03-22 11:12:18 -0700472 // First, un-pin all shortcuts
473 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
474 mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
475 }
476
477 // Then, for the pinned set for each launcher, set the pin flag one by one.
Makoto Onuki47ed1712017-12-20 14:40:20 +0900478 mShortcutUser.forAllLaunchers(launcherShortcuts -> {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700479 final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
Makoto Onuki2e210c42016-03-30 08:30:36 -0700480 getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700481
482 if (pinned == null || pinned.size() == 0) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700483 return;
Makoto Onuki31459242016-03-22 11:12:18 -0700484 }
485 for (int i = pinned.size() - 1; i >= 0; i--) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700486 final String id = pinned.valueAt(i);
487 final ShortcutInfo si = mShortcuts.get(id);
Makoto Onuki31459242016-03-22 11:12:18 -0700488 if (si == null) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700489 // This happens if a launcher pinned shortcuts from this package, then backup&
490 // restored, but this package doesn't allow backing up.
491 // In that case the launcher ends up having a dangling pinned shortcuts.
492 // That's fine, when the launcher is restored, we'll fix it.
493 continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700494 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700495 si.addFlags(ShortcutInfo.FLAG_PINNED);
Makoto Onuki31459242016-03-22 11:12:18 -0700496 }
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700497 });
Makoto Onuki31459242016-03-22 11:12:18 -0700498
499 // Lastly, remove the ones that are no longer pinned nor dynamic.
Makoto Onukic51b2872016-05-04 15:24:50 -0700500 removeOrphans();
Makoto Onuki31459242016-03-22 11:12:18 -0700501 }
502
503 /**
504 * Number of calls that the caller has made, since the last reset.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700505 *
506 * <p>This takes care of the resetting the counter for foreground apps as well as after
507 * locale changes.
Makoto Onuki31459242016-03-22 11:12:18 -0700508 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800509 public int getApiCallCount(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700510 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700511
512 // Reset the counter if:
513 // - the package is in foreground now.
514 // - the package is *not* in foreground now, but was in foreground at some point
515 // since the previous time it had been.
516 if (s.isUidForegroundLocked(mPackageUid)
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800517 || (mLastKnownForegroundElapsedTime
518 < s.getUidLastForegroundElapsedTimeLocked(mPackageUid))
519 || unlimited) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700520 mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
Makoto Onukic51b2872016-05-04 15:24:50 -0700521 resetRateLimiting();
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700522 }
523
524 // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
525 // but we just can't return 0 at this point, because we may have to update
526 // mLastResetTime.
527
Makoto Onuki31459242016-03-22 11:12:18 -0700528 final long last = s.getLastResetTimeLocked();
529
530 final long now = s.injectCurrentTimeMillis();
531 if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
532 Slog.w(TAG, "Clock rewound");
533 // Clock rewound.
534 mLastResetTime = now;
535 mApiCallCount = 0;
536 return mApiCallCount;
537 }
538
539 // If not reset yet, then reset.
540 if (mLastResetTime < last) {
541 if (ShortcutService.DEBUG) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700542 Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
543 getPackageName(), mLastResetTime, now, last));
Makoto Onuki31459242016-03-22 11:12:18 -0700544 }
545 mApiCallCount = 0;
546 mLastResetTime = last;
547 }
548 return mApiCallCount;
549 }
550
551 /**
552 * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
553 * and return true. Otherwise just return false.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700554 *
555 * <p>This takes care of the resetting the counter for foreground apps as well as after
556 * locale changes, which is done internally by {@link #getApiCallCount}.
Makoto Onuki31459242016-03-22 11:12:18 -0700557 */
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800558 public boolean tryApiCall(boolean unlimited) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700559 final ShortcutService s = mShortcutUser.mService;
560
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800561 if (getApiCallCount(unlimited) >= s.mMaxUpdatesPerInterval) {
Makoto Onuki31459242016-03-22 11:12:18 -0700562 return false;
563 }
564 mApiCallCount++;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700565 s.scheduleSaveUser(getOwnerUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700566 return true;
567 }
568
Makoto Onukic51b2872016-05-04 15:24:50 -0700569 public void resetRateLimiting() {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700570 if (ShortcutService.DEBUG) {
571 Slog.d(TAG, "resetRateLimiting: " + getPackageName());
572 }
573 if (mApiCallCount > 0) {
574 mApiCallCount = 0;
Makoto Onukic51b2872016-05-04 15:24:50 -0700575 mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700576 }
577 }
578
579 public void resetRateLimitingForCommandLineNoSaving() {
Makoto Onuki31459242016-03-22 11:12:18 -0700580 mApiCallCount = 0;
581 mLastResetTime = 0;
582 }
583
584 /**
585 * Find all shortcuts that match {@code query}.
586 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700587 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700588 @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700589 findAll(result, query, cloneFlag, null, 0, /*getPinnedByAnyLauncher=*/ false);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700590 }
591
592 /**
593 * Find all shortcuts that match {@code query}.
594 *
595 * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
596 * by the calling launcher will not be included in the result, and also "isPinned" will be
597 * adjusted for the caller too.
598 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700599 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onuki31459242016-03-22 11:12:18 -0700600 @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
Makoto Onuki634cecb2017-10-13 17:10:48 -0700601 @Nullable String callingLauncher, int launcherUserId, boolean getPinnedByAnyLauncher) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700602 if (getPackageInfo().isShadow()) {
603 // Restored and the app not installed yet, so don't return any.
604 return;
605 }
Makoto Onuki31459242016-03-22 11:12:18 -0700606
Makoto Onukic51b2872016-05-04 15:24:50 -0700607 final ShortcutService s = mShortcutUser.mService;
608
Makoto Onuki31459242016-03-22 11:12:18 -0700609 // Set of pinned shortcuts by the calling launcher.
610 final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
Makoto Onuki2e210c42016-03-30 08:30:36 -0700611 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
612 .getPinnedShortcutIds(getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700613
614 for (int i = 0; i < mShortcuts.size(); i++) {
615 final ShortcutInfo si = mShortcuts.valueAt(i);
616
Makoto Onuki22fcc682016-05-17 14:52:19 -0700617 // Need to adjust PINNED flag depending on the caller.
618 // Basically if the caller is a launcher (callingLauncher != null) and the launcher
619 // isn't pinning it, then we need to clear PINNED for this caller.
Makoto Onuki31459242016-03-22 11:12:18 -0700620 final boolean isPinnedByCaller = (callingLauncher == null)
621 || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700622
Makoto Onuki634cecb2017-10-13 17:10:48 -0700623 if (!getPinnedByAnyLauncher) {
624 if (si.isFloating()) {
625 if (!isPinnedByCaller) {
626 continue;
627 }
Makoto Onuki31459242016-03-22 11:12:18 -0700628 }
629 }
630 final ShortcutInfo clone = si.clone(cloneFlag);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700631
Makoto Onuki31459242016-03-22 11:12:18 -0700632 // Fix up isPinned for the caller. Note we need to do it before the "test" callback,
633 // since it may check isPinned.
Makoto Onuki35559d62017-11-06 16:26:32 -0800634 // However, if getPinnedByAnyLauncher is set, we do it after the test.
635 if (!getPinnedByAnyLauncher) {
636 if (!isPinnedByCaller) {
637 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
638 }
Makoto Onuki31459242016-03-22 11:12:18 -0700639 }
640 if (query == null || query.test(clone)) {
Makoto Onuki35559d62017-11-06 16:26:32 -0800641 if (!isPinnedByCaller) {
642 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
643 }
Makoto Onuki31459242016-03-22 11:12:18 -0700644 result.add(clone);
645 }
646 }
647 }
648
649 public void resetThrottling() {
650 mApiCallCount = 0;
651 }
652
Makoto Onuki39686e82016-04-13 18:03:00 -0700653 /**
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800654 * Returns a list of ShortcutInfos that match the given intent filter and the category of
655 * available ShareTarget definitions in this package.
656 */
657 public List<ShortcutManager.ShareShortcutInfo> getMatchingShareTargets(
658 @NonNull IntentFilter filter) {
659 final List<ShareTargetInfo> matchedTargets = new ArrayList<>();
660 for (int i = 0; i < mShareTargets.size(); i++) {
661 final ShareTargetInfo target = mShareTargets.get(i);
662 for (ShareTargetInfo.TargetData data : target.mTargetData) {
663 if (filter.hasDataType(data.mMimeType)) {
664 // Matched at least with one data type
665 matchedTargets.add(target);
666 break;
667 }
668 }
669 }
670
671 if (matchedTargets.isEmpty()) {
672 return new ArrayList<>();
673 }
674
675 // Get the list of all dynamic shortcuts in this package
676 final ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
677 findAll(shortcuts, ShortcutInfo::isDynamicVisible, ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
678
679 final List<ShortcutManager.ShareShortcutInfo> result = new ArrayList<>();
680 for (int i = 0; i < shortcuts.size(); i++) {
681 final ShortcutInfo si = shortcuts.get(i);
682 for (int j = 0; j < matchedTargets.size(); j++) {
683 // Shortcut must have all of share target categories
684 boolean hasAllCategories = true;
685 final ShareTargetInfo target = matchedTargets.get(j);
686 for (int q = 0; q < target.mCategories.length; q++) {
687 if (!si.getCategories().contains(target.mCategories[q])) {
688 hasAllCategories = false;
689 break;
690 }
691 }
692 if (hasAllCategories) {
693 result.add(new ShortcutManager.ShareShortcutInfo(si, new ComponentName(
694 getPackageName(), target.mTargetClass)));
695 break;
696 }
697 }
698 }
699 return result;
700 }
701
Mehdi Alizadeh85fd3d52019-01-23 12:49:53 -0800702 public boolean hasShareTargets() {
703 return !mShareTargets.isEmpty();
704 }
705
Mehdi Alizadeh406e8b32018-12-11 18:21:49 -0800706 /**
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700707 * Return the filenames (excluding path names) of icon bitmap files from this package.
708 */
709 public ArraySet<String> getUsedBitmapFiles() {
710 final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
711
712 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
713 final ShortcutInfo si = mShortcuts.valueAt(i);
714 if (si.getBitmapPath() != null) {
715 usedFiles.add(getFileName(si.getBitmapPath()));
716 }
717 }
718 return usedFiles;
719 }
720
721 private static String getFileName(@NonNull String path) {
722 final int sep = path.lastIndexOf(File.separatorChar);
723 if (sep == -1) {
724 return path;
725 } else {
726 return path.substring(sep + 1);
727 }
728 }
729
730 /**
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700731 * @return false if any of the target activities are no longer enabled.
732 */
733 private boolean areAllActivitiesStillEnabled() {
734 if (mShortcuts.size() == 0) {
735 return true;
736 }
737 final ShortcutService s = mShortcutUser.mService;
738
739 // Normally the number of target activities is 1 or so, so no need to use a complex
740 // structure like a set.
741 final ArrayList<ComponentName> checked = new ArrayList<>(4);
742
743 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
744 final ShortcutInfo si = mShortcuts.valueAt(i);
745 final ComponentName activity = si.getActivity();
746
747 if (checked.contains(activity)) {
748 continue; // Already checked.
749 }
750 checked.add(activity);
751
Makoto Onuki40dc2112017-10-18 12:52:45 -0700752 if ((activity != null)
753 && !s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700754 return false;
755 }
756 }
757 return true;
758 }
759
760 /**
761 * Called when the package may be added or updated, or its activities may be disabled, and
762 * if so, rescan the package and do the necessary stuff.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700763 *
764 * Add case:
765 * - Publish manifest shortcuts.
766 *
767 * Update case:
768 * - Re-publish manifest shortcuts.
769 * - If there are shortcuts with resources (icons or strings), update their timestamps.
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700770 * - Disable shortcuts whose target activities are disabled.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700771 *
772 * @return TRUE if any shortcuts have been changed.
Makoto Onuki39686e82016-04-13 18:03:00 -0700773 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700774 public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) {
775 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki84d59342018-02-02 09:22:38 -0800776 final long start = s.getStatStartTime();
Makoto Onuki39686e82016-04-13 18:03:00 -0700777
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700778 final PackageInfo pi;
779 try {
780 pi = mShortcutUser.mService.getPackageInfo(
781 getPackageName(), getPackageUserId());
782 if (pi == null) {
783 return false; // Shouldn't happen.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700784 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700785
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700786 if (!isNewApp && !forceRescan) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700787 // Return if the package hasn't changed, ie:
788 // - version code hasn't change
789 // - lastUpdateTime hasn't change
790 // - all target activities are still enabled.
Makoto Onuki33663282016-08-22 16:19:04 -0700791
792 // Note, system apps timestamps do *not* change after OTAs. (But they do
793 // after an adb sync or a local flash.)
794 // This means if a system app's version code doesn't change on an OTA,
795 // we don't notice it's updated. But that's fine since their version code *should*
796 // really change on OTAs.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700797 if ((getPackageInfo().getVersionCode() == pi.getLongVersionCode())
Makoto Onuki64183d52016-08-08 14:11:34 -0700798 && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime)
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700799 && areAllActivitiesStillEnabled()) {
800 return false;
801 }
802 }
803 } finally {
804 s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700805 }
806
807 // Now prepare to publish manifest shortcuts.
808 List<ShortcutInfo> newManifestShortcutList = null;
809 try {
810 newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800811 getPackageName(), getPackageUserId(), mShareTargets);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700812 } catch (IOException|XmlPullParserException e) {
813 Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
814 }
815 final int manifestShortcutSize = newManifestShortcutList == null ? 0
816 : newManifestShortcutList.size();
817 if (ShortcutService.DEBUG) {
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800818 Slog.d(TAG,
819 String.format("Package %s has %d manifest shortcut(s), and %d share target(s)",
820 getPackageName(), manifestShortcutSize, mShareTargets.size()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700821 }
822 if (isNewApp && (manifestShortcutSize == 0)) {
823 // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
824
825 // If it's an update, then it may already have manifest shortcuts, which need to be
826 // disabled.
827 return false;
828 }
829 if (ShortcutService.DEBUG) {
830 Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
831 (isNewApp ? "added" : "updated"),
Dianne Hackborn3accca02013-09-20 09:32:11 -0700832 getPackageInfo().getVersionCode(), pi.getLongVersionCode()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700833 }
834
Makoto Onukia4f89b12017-10-05 10:37:55 -0700835 getPackageInfo().updateFromPackageInfo(pi);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700836 final long newVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700837
838 // See if there are any shortcuts that were prevented restoring because the app was of a
839 // lower version, and re-enable them.
840 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
841 final ShortcutInfo si = mShortcuts.valueAt(i);
842 if (si.getDisabledReason() != ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
843 continue;
844 }
845 if (getPackageInfo().getBackupSourceVersionCode() > newVersionCode) {
846 if (ShortcutService.DEBUG) {
847 Slog.d(TAG, String.format("Shortcut %s require version %s, still not restored.",
848 si.getId(), getPackageInfo().getBackupSourceVersionCode()));
849 }
850 continue;
851 }
852 Slog.i(TAG, String.format("Restoring shortcut: %s", si.getId()));
853 si.clearFlags(ShortcutInfo.FLAG_DISABLED);
854 si.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
855 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700856
Makoto Onuki22fcc682016-05-17 14:52:19 -0700857 // For existing shortcuts, update timestamps if they have any resources.
Makoto Onukib08790c2016-06-23 14:05:46 -0700858 // Also check if shortcuts' activities are still main activities. Otherwise, disable them.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700859 if (!isNewApp) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700860 Resources publisherRes = null;
861
Makoto Onuki22fcc682016-05-17 14:52:19 -0700862 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
863 final ShortcutInfo si = mShortcuts.valueAt(i);
864
Makoto Onuki2d895c32016-12-02 15:48:40 -0800865 // Disable dynamic shortcuts whose target activity is gone.
Makoto Onukib08790c2016-06-23 14:05:46 -0700866 if (si.isDynamic()) {
Makoto Onuki34145532017-03-14 17:58:36 -0700867 if (si.getActivity() == null) {
868 // Note if it's dynamic, it must have a target activity, but b/36228253.
869 s.wtf("null activity detected.");
870 // TODO Maybe remove it?
871 } else if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700872 Slog.w(TAG, String.format(
873 "%s is no longer main activity. Disabling shorcut %s.",
874 getPackageName(), si.getId()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700875 if (disableDynamicWithId(si.getId(), /*ignoreInvisible*/ false,
876 ShortcutInfo.DISABLED_REASON_APP_CHANGED)) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700877 continue; // Actually removed.
878 }
879 // Still pinned, so fall-through and possibly update the resources.
880 }
Makoto Onukib08790c2016-06-23 14:05:46 -0700881 }
882
Makoto Onuki22fcc682016-05-17 14:52:19 -0700883 if (si.hasAnyResources()) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700884 if (!si.isOriginallyFromManifest()) {
885 if (publisherRes == null) {
886 publisherRes = getPackageResources();
887 if (publisherRes == null) {
888 break; // Resources couldn't be loaded.
889 }
890 }
891
892 // If this shortcut is not from a manifest, then update all resource IDs
893 // from resource names. (We don't allow resource strings for
894 // non-manifest at the moment, but icons can still be resources.)
895 si.lookupAndFillInResourceIds(publisherRes);
896 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700897 si.setTimestamp(s.injectCurrentTimeMillis());
898 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700899 }
900 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700901
902 // (Re-)publish manifest shortcut.
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700903 publishManifestShortcuts(newManifestShortcutList);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700904
Makoto Onuki7001a612016-05-27 13:24:28 -0700905 if (newManifestShortcutList != null) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700906 pushOutExcessShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -0700907 }
908
Makoto Onukidf6da042016-06-16 09:51:40 -0700909 s.verifyStates();
910
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700911 // This will send a notification to the launcher, and also save .
912 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
913 return true; // true means changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700914 }
915
916 private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
917 if (ShortcutService.DEBUG) {
918 Slog.d(TAG, String.format(
919 "Package %s: publishing manifest shortcuts", getPackageName()));
920 }
921 boolean changed = false;
922
Makoto Onuki22fcc682016-05-17 14:52:19 -0700923 // Keep the previous IDs.
924 ArraySet<String> toDisableList = null;
925 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
926 final ShortcutInfo si = mShortcuts.valueAt(i);
927
928 if (si.isManifestShortcut()) {
929 if (toDisableList == null) {
930 toDisableList = new ArraySet<>();
931 }
932 toDisableList.add(si.getId());
933 }
934 }
935
936 // Publish new ones.
937 if (newManifestShortcutList != null) {
938 final int newListSize = newManifestShortcutList.size();
939
940 for (int i = 0; i < newListSize; i++) {
941 changed = true;
942
943 final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
944 final boolean newDisabled = !newShortcut.isEnabled();
945
Makoto Onuki7001a612016-05-27 13:24:28 -0700946 final String id = newShortcut.getId();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700947 final ShortcutInfo oldShortcut = mShortcuts.get(id);
948
949 boolean wasPinned = false;
950
951 if (oldShortcut != null) {
952 if (!oldShortcut.isOriginallyFromManifest()) {
953 Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
954 + " exists but is not from AndroidManifest.xml, not updating.");
955 continue;
956 }
957 // Take over the pinned flag.
958 if (oldShortcut.isPinned()) {
959 wasPinned = true;
960 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
961 }
962 }
963 if (newDisabled && !wasPinned) {
964 // If the shortcut is disabled, and it was *not* pinned, then this
965 // just doesn't have to be published.
966 // Just keep it in toDisableList, so the previous one would be removed.
967 continue;
968 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700969
970 // Note even if enabled=false, we still need to update all fields, so do it
971 // regardless.
Makoto Onukia4f89b12017-10-05 10:37:55 -0700972 forceReplaceShortcutInner(newShortcut); // This will clean up the old one too.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700973
974 if (!newDisabled && toDisableList != null) {
975 // Still alive, don't remove.
976 toDisableList.remove(id);
977 }
978 }
979 }
980
981 // Disable the previous manifest shortcuts that are no longer in the manifest.
982 if (toDisableList != null) {
983 if (ShortcutService.DEBUG) {
984 Slog.d(TAG, String.format(
985 "Package %s: disabling %d stale shortcuts", getPackageName(),
986 toDisableList.size()));
987 }
988 for (int i = toDisableList.size() - 1; i >= 0; i--) {
989 changed = true;
990
991 final String id = toDisableList.valueAt(i);
992
993 disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700994 /* overrideImmutable=*/ true, /*ignoreInvisible=*/ false,
995 ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700996 }
997 removeOrphans();
998 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700999 adjustRanks();
Makoto Onuki22fcc682016-05-17 14:52:19 -07001000 return changed;
Makoto Onuki39686e82016-04-13 18:03:00 -07001001 }
1002
Makoto Onuki7001a612016-05-27 13:24:28 -07001003 /**
1004 * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
1005 * If too many, we'll remove the dynamic with the lowest ranks.
1006 */
1007 private boolean pushOutExcessShortcuts() {
1008 final ShortcutService service = mShortcutUser.mService;
1009 final int maxShortcuts = service.getMaxActivityShortcuts();
1010
1011 boolean changed = false;
1012
1013 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1014 sortShortcutsToActivities();
1015 for (int outer = all.size() - 1; outer >= 0; outer--) {
1016 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1017 if (list.size() <= maxShortcuts) {
1018 continue;
1019 }
1020 // Sort by isManifestShortcut() and getRank().
1021 Collections.sort(list, mShortcutTypeAndRankComparator);
1022
1023 // Keep [0 .. max), and remove (as dynamic) [max .. size)
1024 for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
1025 final ShortcutInfo shortcut = list.get(inner);
1026
1027 if (shortcut.isManifestShortcut()) {
1028 // This shouldn't happen -- excess shortcuts should all be non-manifest.
1029 // But just in case.
1030 service.wtf("Found manifest shortcuts in excess list.");
1031 continue;
1032 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001033 deleteDynamicWithId(shortcut.getId(), /*ignoreInvisible=*/ true);
Makoto Onuki7001a612016-05-27 13:24:28 -07001034 }
1035 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001036
1037 return changed;
1038 }
1039
1040 /**
1041 * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before
1042 * non-manifest shortcuts, then sort by rank.
1043 *
1044 * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
1045 * manifest shortcuts than before and as a result we need to remove some of the dynamic
1046 * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with
1047 * the last ones.
1048 *
1049 * (Note the number of manifest shortcuts is always <= the max number, because if there are
1050 * more, ShortcutParser would ignore the rest.)
1051 */
1052 final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
1053 ShortcutInfo b) -> {
1054 if (a.isManifestShortcut() && !b.isManifestShortcut()) {
1055 return -1;
1056 }
1057 if (!a.isManifestShortcut() && b.isManifestShortcut()) {
1058 return 1;
1059 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001060 return Integer.compare(a.getRank(), b.getRank());
Makoto Onuki7001a612016-05-27 13:24:28 -07001061 };
1062
1063 /**
1064 * Build a list of shortcuts for each target activity and return as a map. The result won't
1065 * contain "floating" shortcuts because they don't belong on any activities.
1066 */
1067 private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
Makoto Onuki7001a612016-05-27 13:24:28 -07001068 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
1069 = new ArrayMap<>();
1070 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1071 final ShortcutInfo si = mShortcuts.valueAt(i);
1072 if (si.isFloating()) {
1073 continue; // Ignore floating shortcuts, which are not tied to any activities.
1074 }
1075
1076 final ComponentName activity = si.getActivity();
Makoto Onuki34145532017-03-14 17:58:36 -07001077 if (activity == null) {
1078 mShortcutUser.mService.wtf("null activity detected.");
1079 continue;
1080 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001081
1082 ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
1083 if (list == null) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001084 list = new ArrayList<>();
Makoto Onuki7001a612016-05-27 13:24:28 -07001085 activitiesToShortcuts.put(activity, list);
1086 }
1087 list.add(si);
1088 }
1089 return activitiesToShortcuts;
1090 }
1091
1092 /** Used by {@link #enforceShortcutCountsBeforeOperation} */
1093 private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
1094 ComponentName cn, int increment) {
1095 Integer oldValue = counts.get(cn);
1096 if (oldValue == null) {
1097 oldValue = 0;
1098 }
1099
1100 counts.put(cn, oldValue + increment);
1101 }
1102
1103 /**
1104 * Called by
1105 * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
1106 * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
1107 * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
1108 * the operation to make sure the operation wouldn't result in the target activities having
1109 * more than the allowed number of dynamic/manifest shortcuts.
1110 *
1111 * @param newList shortcut list passed to set, add or updateShortcuts().
1112 * @param operation add, set or update.
1113 * @throws IllegalArgumentException if the operation would result in going over the max
1114 * shortcut count for any activity.
1115 */
1116 public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
1117 @ShortcutOperation int operation) {
1118 final ShortcutService service = mShortcutUser.mService;
1119
1120 // Current # of dynamic / manifest shortcuts for each activity.
1121 // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
1122 // anyway.)
1123 final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
1124 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1125 final ShortcutInfo shortcut = mShortcuts.valueAt(i);
1126
1127 if (shortcut.isManifestShortcut()) {
1128 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1129 } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
1130 incrementCountForActivity(counts, shortcut.getActivity(), 1);
1131 }
1132 }
1133
1134 for (int i = newList.size() - 1; i >= 0; i--) {
1135 final ShortcutInfo newShortcut = newList.get(i);
1136 final ComponentName newActivity = newShortcut.getActivity();
1137 if (newActivity == null) {
1138 if (operation != ShortcutService.OPERATION_UPDATE) {
Makoto Onukib08790c2016-06-23 14:05:46 -07001139 service.wtf("Activity must not be null at this point");
1140 continue; // Just ignore this invalid case.
Makoto Onuki7001a612016-05-27 13:24:28 -07001141 }
1142 continue; // Activity can be null for update.
1143 }
1144
1145 final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
1146 if (original == null) {
1147 if (operation == ShortcutService.OPERATION_UPDATE) {
1148 continue; // When updating, ignore if there's no target.
1149 }
1150 // Add() or set(), and there's no existing shortcut with the same ID. We're
1151 // simply publishing (as opposed to updating) this shortcut, so just +1.
1152 incrementCountForActivity(counts, newActivity, 1);
1153 continue;
1154 }
1155 if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
1156 // Updating floating shortcuts doesn't affect the count, so ignore.
1157 continue;
1158 }
1159
1160 // If it's add() or update(), then need to decrement for the previous activity.
1161 // Skip it for set() since it's already been taken care of by not counting the original
1162 // dynamic shortcuts in the first loop.
1163 if (operation != ShortcutService.OPERATION_SET) {
1164 final ComponentName oldActivity = original.getActivity();
1165 if (!original.isFloating()) {
1166 incrementCountForActivity(counts, oldActivity, -1);
1167 }
1168 }
1169 incrementCountForActivity(counts, newActivity, 1);
1170 }
1171
1172 // Then make sure none of the activities have more than the max number of shortcuts.
1173 for (int i = counts.size() - 1; i >= 0; i--) {
1174 service.enforceMaxActivityShortcuts(counts.valueAt(i));
1175 }
1176 }
1177
Makoto Onuki157b1622016-06-02 16:13:10 -07001178 /**
1179 * For all the text fields, refresh the string values if they're from resources.
1180 */
1181 public void resolveResourceStrings() {
1182 final ShortcutService s = mShortcutUser.mService;
1183 boolean changed = false;
1184
1185 Resources publisherRes = null;
1186 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1187 final ShortcutInfo si = mShortcuts.valueAt(i);
1188
1189 if (si.hasStringResources()) {
1190 changed = true;
1191
1192 if (publisherRes == null) {
1193 publisherRes = getPackageResources();
1194 if (publisherRes == null) {
1195 break; // Resources couldn't be loaded.
1196 }
1197 }
1198
1199 si.resolveResourceStrings(publisherRes);
1200 si.setTimestamp(s.injectCurrentTimeMillis());
1201 }
1202 }
1203 if (changed) {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001204 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001205 }
1206 }
1207
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001208 /** Clears the implicit ranks for all shortcuts. */
1209 public void clearAllImplicitRanks() {
1210 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1211 final ShortcutInfo si = mShortcuts.valueAt(i);
1212 si.clearImplicitRankAndRankChangedFlag();
1213 }
1214 }
1215
1216 /**
1217 * Used to sort shortcuts for rank auto-adjusting.
1218 */
1219 final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
1220 // First, sort by rank.
1221 int ret = Integer.compare(a.getRank(), b.getRank());
1222 if (ret != 0) {
1223 return ret;
1224 }
1225 // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
1226 // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
1227 // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
1228 // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
1229 // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
1230 if (a.isRankChanged() != b.isRankChanged()) {
1231 return a.isRankChanged() ? -1 : 1;
1232 }
1233 // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
1234 // they're passed to the API.
1235 ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
1236 if (ret != 0) {
1237 return ret;
1238 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001239 // If they're still tie, just sort by their IDs.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001240 // This may happen with updateShortcuts() -- see
1241 // the testUpdateShortcuts_noManifestShortcuts() test.
1242 return a.getId().compareTo(b.getId());
1243 };
1244
1245 /**
1246 * Re-calculate the ranks for all shortcuts.
1247 */
1248 public void adjustRanks() {
1249 final ShortcutService s = mShortcutUser.mService;
1250 final long now = s.injectCurrentTimeMillis();
1251
1252 // First, clear ranks for floating shortcuts.
1253 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1254 final ShortcutInfo si = mShortcuts.valueAt(i);
1255 if (si.isFloating()) {
1256 if (si.getRank() != 0) {
1257 si.setTimestamp(now);
1258 si.setRank(0);
1259 }
1260 }
1261 }
1262
1263 // Then adjust ranks. Ranks are unique for each activity, so we first need to sort
1264 // shortcuts to each activity.
1265 // Then sort the shortcuts within each activity with mShortcutRankComparator, and
1266 // assign ranks from 0.
1267 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1268 sortShortcutsToActivities();
1269 for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
1270 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1271
1272 // Sort by ranks and other signals.
1273 Collections.sort(list, mShortcutRankComparator);
1274
1275 int rank = 0;
1276
1277 final int size = list.size();
1278 for (int i = 0; i < size; i++) {
1279 final ShortcutInfo si = list.get(i);
1280 if (si.isManifestShortcut()) {
1281 // Don't adjust ranks for manifest shortcuts.
1282 continue;
1283 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001284 // At this point, it must be dynamic.
1285 if (!si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001286 s.wtf("Non-dynamic shortcut found.");
1287 continue;
1288 }
1289 final int thisRank = rank++;
1290 if (si.getRank() != thisRank) {
1291 si.setTimestamp(now);
1292 si.setRank(thisRank);
1293 }
1294 }
1295 }
1296 }
1297
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07001298 /** @return true if there's any shortcuts that are not manifest shortcuts. */
1299 public boolean hasNonManifestShortcuts() {
1300 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1301 final ShortcutInfo si = mShortcuts.valueAt(i);
1302 if (!si.isDeclaredInManifest()) {
1303 return true;
1304 }
1305 }
1306 return false;
1307 }
1308
Makoto Onuki20b82212017-10-04 15:03:50 -07001309 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -07001310 pw.println();
1311
1312 pw.print(prefix);
1313 pw.print("Package: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001314 pw.print(getPackageName());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001315 pw.print(" UID: ");
1316 pw.print(mPackageUid);
Makoto Onuki31459242016-03-22 11:12:18 -07001317 pw.println();
1318
1319 pw.print(prefix);
1320 pw.print(" ");
1321 pw.print("Calls: ");
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001322 pw.print(getApiCallCount(/*unlimited=*/ false));
Makoto Onuki31459242016-03-22 11:12:18 -07001323 pw.println();
1324
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001325 // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
1326 pw.print(prefix);
1327 pw.print(" ");
1328 pw.print("Last known FG: ");
1329 pw.print(mLastKnownForegroundElapsedTime);
1330 pw.println();
1331
Makoto Onuki31459242016-03-22 11:12:18 -07001332 // This should be after getApiCallCount(), which may update it.
1333 pw.print(prefix);
1334 pw.print(" ");
1335 pw.print("Last reset: [");
1336 pw.print(mLastResetTime);
1337 pw.print("] ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001338 pw.print(ShortcutService.formatTime(mLastResetTime));
Makoto Onuki31459242016-03-22 11:12:18 -07001339 pw.println();
1340
Makoto Onukic51b2872016-05-04 15:24:50 -07001341 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001342 pw.println();
1343
Makoto Onuki39686e82016-04-13 18:03:00 -07001344 pw.print(prefix);
1345 pw.println(" Shortcuts:");
Makoto Onuki31459242016-03-22 11:12:18 -07001346 long totalBitmapSize = 0;
1347 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1348 final int size = shortcuts.size();
1349 for (int i = 0; i < size; i++) {
1350 final ShortcutInfo si = shortcuts.valueAt(i);
Makoto Onuki6208c672017-10-02 16:20:35 -07001351 pw.println(si.toDumpString(prefix + " "));
Makoto Onuki31459242016-03-22 11:12:18 -07001352 if (si.getBitmapPath() != null) {
1353 final long len = new File(si.getBitmapPath()).length();
Makoto Onuki39686e82016-04-13 18:03:00 -07001354 pw.print(prefix);
1355 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001356 pw.print("bitmap size=");
1357 pw.println(len);
1358
1359 totalBitmapSize += len;
1360 }
1361 }
1362 pw.print(prefix);
1363 pw.print(" ");
1364 pw.print("Total bitmap size: ");
1365 pw.print(totalBitmapSize);
1366 pw.print(" (");
Makoto Onukic51b2872016-05-04 15:24:50 -07001367 pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
Makoto Onuki31459242016-03-22 11:12:18 -07001368 pw.println(")");
1369 }
1370
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001371 @Override
Makoto Onuki76269922016-07-15 14:58:54 -07001372 public JSONObject dumpCheckin(boolean clear) throws JSONException {
1373 final JSONObject result = super.dumpCheckin(clear);
1374
1375 int numDynamic = 0;
1376 int numPinned = 0;
1377 int numManifest = 0;
1378 int numBitmaps = 0;
1379 long totalBitmapSize = 0;
1380
1381 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1382 final int size = shortcuts.size();
1383 for (int i = 0; i < size; i++) {
1384 final ShortcutInfo si = shortcuts.valueAt(i);
1385
1386 if (si.isDynamic()) numDynamic++;
1387 if (si.isDeclaredInManifest()) numManifest++;
1388 if (si.isPinned()) numPinned++;
1389
1390 if (si.getBitmapPath() != null) {
1391 numBitmaps++;
1392 totalBitmapSize += new File(si.getBitmapPath()).length();
1393 }
1394 }
1395
1396 result.put(KEY_DYNAMIC, numDynamic);
1397 result.put(KEY_MANIFEST, numManifest);
1398 result.put(KEY_PINNED, numPinned);
1399 result.put(KEY_BITMAPS, numBitmaps);
1400 result.put(KEY_BITMAP_BYTES, totalBitmapSize);
1401
1402 // TODO Log update frequency too.
1403
1404 return result;
1405 }
1406
1407 @Override
Makoto Onuki0acbb142016-03-22 17:02:57 -07001408 public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
1409 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001410 final int size = mShortcuts.size();
1411
1412 if (size == 0 && mApiCallCount == 0) {
1413 return; // nothing to write.
1414 }
1415
1416 out.startTag(null, TAG_ROOT);
1417
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001418 ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
Makoto Onuki31459242016-03-22 11:12:18 -07001419 ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
1420 ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
Makoto Onukie3fffa92018-02-28 16:25:40 -08001421 getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001422
1423 for (int j = 0; j < size; j++) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001424 saveShortcut(out, mShortcuts.valueAt(j), forBackup,
1425 getPackageInfo().isBackupAllowed());
Makoto Onuki31459242016-03-22 11:12:18 -07001426 }
1427
1428 out.endTag(null, TAG_ROOT);
1429 }
1430
Makoto Onukia4f89b12017-10-05 10:37:55 -07001431 private void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup,
1432 boolean appSupportsBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001433 throws IOException, XmlPullParserException {
Makoto Onuki475c3652017-05-08 14:29:03 -07001434
1435 final ShortcutService s = mShortcutUser.mService;
1436
Makoto Onuki0acbb142016-03-22 17:02:57 -07001437 if (forBackup) {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001438 if (!(si.isPinned() && si.isEnabled())) {
Makoto Onukia4f89b12017-10-05 10:37:55 -07001439 // We only backup pinned shortcuts that are enabled.
1440 // Note, this means, shortcuts that are restored but are blocked restore, e.g. due
1441 // to a lower version code, will not be ported to a new device.
1442 return;
Makoto Onuki0acbb142016-03-22 17:02:57 -07001443 }
1444 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001445 final boolean shouldBackupDetails =
1446 !forBackup // It's not backup
1447 || appSupportsBackup; // Or, it's a backup and app supports backup.
1448
Makoto Onuki475c3652017-05-08 14:29:03 -07001449 // Note: at this point no shortcuts should have bitmaps pending save, but if they do,
1450 // just remove the bitmap.
1451 if (si.isIconPendingSave()) {
1452 s.removeIconLocked(si);
1453 }
Makoto Onuki31459242016-03-22 11:12:18 -07001454 out.startTag(null, TAG_SHORTCUT);
1455 ShortcutService.writeAttr(out, ATTR_ID, si.getId());
1456 // writeAttr(out, "package", si.getPackageName()); // not needed
Makoto Onuki22fcc682016-05-17 14:52:19 -07001457 ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
Makoto Onuki31459242016-03-22 11:12:18 -07001458 // writeAttr(out, "icon", si.getIcon()); // We don't save it.
1459 ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001460 ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001461 ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001462 ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001463 ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001464 ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
Makoto Onukia4f89b12017-10-05 10:37:55 -07001465 if (shouldBackupDetails) {
1466 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
1467 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
1468 si.getDisabledMessageResourceId());
1469 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
1470 si.getDisabledMessageResName());
1471 }
1472 ShortcutService.writeAttr(out, ATTR_DISABLED_REASON, si.getDisabledReason());
Makoto Onuki31459242016-03-22 11:12:18 -07001473 ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
1474 si.getLastChangedTimestamp());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001475 if (forBackup) {
1476 // Don't write icon information. Also drop the dynamic flag.
Makoto Onukia4f89b12017-10-05 10:37:55 -07001477
1478 int flags = si.getFlags() &
1479 ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
Makoto Onuki475c3652017-05-08 14:29:03 -07001480 | ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE
Makoto Onukia4f89b12017-10-05 10:37:55 -07001481 | ShortcutInfo.FLAG_DYNAMIC);
1482 ShortcutService.writeAttr(out, ATTR_FLAGS, flags);
1483
1484 // Set the publisher version code at every backup.
Dianne Hackborn3accca02013-09-20 09:32:11 -07001485 final long packageVersionCode = getPackageInfo().getVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -07001486 if (packageVersionCode == 0) {
1487 s.wtf("Package version code should be available at this point.");
1488 // However, 0 is a valid version code, so we just go ahead with it...
1489 }
Makoto Onuki0acbb142016-03-22 17:02:57 -07001490 } else {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001491 // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
1492 // as dynamic.
1493 ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
1494
Makoto Onuki0acbb142016-03-22 17:02:57 -07001495 ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
Makoto Onuki157b1622016-06-02 16:13:10 -07001496 ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
1497 ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001498 ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
1499 }
Makoto Onuki31459242016-03-22 11:12:18 -07001500
Makoto Onukia4f89b12017-10-05 10:37:55 -07001501 if (shouldBackupDetails) {
1502 {
1503 final Set<String> cat = si.getCategories();
1504 if (cat != null && cat.size() > 0) {
1505 out.startTag(null, TAG_CATEGORIES);
1506 XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
1507 NAME_CATEGORIES, out);
1508 out.endTag(null, TAG_CATEGORIES);
1509 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001510 }
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001511 if (!forBackup) { // Don't backup the persons field.
1512 final Person[] persons = si.getPersons();
1513 if (!ArrayUtils.isEmpty(persons)) {
1514 for (int i = 0; i < persons.length; i++) {
1515 final Person p = persons[i];
1516
1517 out.startTag(null, TAG_PERSON);
1518 ShortcutService.writeAttr(out, ATTR_PERSON_NAME, p.getName());
1519 ShortcutService.writeAttr(out, ATTR_PERSON_URI, p.getUri());
1520 ShortcutService.writeAttr(out, ATTR_PERSON_KEY, p.getKey());
1521 ShortcutService.writeAttr(out, ATTR_PERSON_IS_BOT, p.isBot());
1522 ShortcutService.writeAttr(out, ATTR_PERSON_IS_IMPORTANT, p.isImportant());
1523 out.endTag(null, TAG_PERSON);
1524 }
1525 }
1526 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001527 final Intent[] intentsNoExtras = si.getIntentsNoExtras();
1528 final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases();
1529 final int numIntents = intentsNoExtras.length;
1530 for (int i = 0; i < numIntents; i++) {
1531 out.startTag(null, TAG_INTENT);
1532 ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]);
1533 ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]);
1534 out.endTag(null, TAG_INTENT);
1535 }
Makoto Onuki99302b52017-03-29 12:42:26 -07001536
Makoto Onukia4f89b12017-10-05 10:37:55 -07001537 ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
1538 }
Hakan Seyalioglu58fc95d2016-12-13 15:23:22 -08001539
Makoto Onuki31459242016-03-22 11:12:18 -07001540 out.endTag(null, TAG_SHORTCUT);
1541 }
1542
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001543 public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
1544 XmlPullParser parser, boolean fromBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001545 throws IOException, XmlPullParserException {
1546
1547 final String packageName = ShortcutService.parseStringAttribute(parser,
1548 ATTR_NAME);
1549
Makoto Onukic51b2872016-05-04 15:24:50 -07001550 final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001551 shortcutUser.getUserId(), packageName);
Makoto Onuki31459242016-03-22 11:12:18 -07001552
Makoto Onuki31459242016-03-22 11:12:18 -07001553 ret.mApiCallCount =
1554 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
1555 ret.mLastResetTime =
1556 ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
1557
Makoto Onukia4f89b12017-10-05 10:37:55 -07001558
Makoto Onuki31459242016-03-22 11:12:18 -07001559 final int outerDepth = parser.getDepth();
1560 int type;
1561 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1562 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1563 if (type != XmlPullParser.START_TAG) {
1564 continue;
1565 }
1566 final int depth = parser.getDepth();
1567 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001568 if (depth == outerDepth + 1) {
1569 switch (tag) {
1570 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -07001571 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001572
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001573 continue;
1574 case TAG_SHORTCUT:
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001575 final ShortcutInfo si = parseShortcut(parser, packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001576 shortcutUser.getUserId(), fromBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001577
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001578 // Don't use addShortcut(), we don't need to save the icon.
1579 ret.mShortcuts.put(si.getId(), si);
1580 continue;
1581 }
Makoto Onuki31459242016-03-22 11:12:18 -07001582 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001583 ShortcutService.warnForInvalidTag(depth, tag);
1584 }
Makoto Onuki31459242016-03-22 11:12:18 -07001585 return ret;
1586 }
1587
Makoto Onukiabe84422016-04-07 09:41:19 -07001588 private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
Makoto Onukia4f89b12017-10-05 10:37:55 -07001589 @UserIdInt int userId, boolean fromBackup)
1590 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001591 String id;
1592 ComponentName activityComponent;
1593 // Icon icon;
1594 String title;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001595 int titleResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001596 String titleResName;
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001597 String text;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001598 int textResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001599 String textResName;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001600 String disabledMessage;
1601 int disabledMessageResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001602 String disabledMessageResName;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001603 int disabledReason;
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001604 Intent intentLegacy;
1605 PersistableBundle intentPersistableExtrasLegacy = null;
1606 ArrayList<Intent> intents = new ArrayList<>();
Makoto Onuki20c95f82016-05-11 16:51:01 -07001607 int rank;
Makoto Onuki31459242016-03-22 11:12:18 -07001608 PersistableBundle extras = null;
1609 long lastChangedTimestamp;
1610 int flags;
Makoto Onuki157b1622016-06-02 16:13:10 -07001611 int iconResId;
1612 String iconResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001613 String bitmapPath;
Makoto Onukia4f89b12017-10-05 10:37:55 -07001614 int backupVersionCode;
Makoto Onukibe73a802016-04-15 14:46:35 -07001615 ArraySet<String> categories = null;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001616 ArrayList<Person> persons = new ArrayList<>();
Makoto Onuki31459242016-03-22 11:12:18 -07001617
1618 id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
1619 activityComponent = ShortcutService.parseComponentNameAttribute(parser,
1620 ATTR_ACTIVITY);
1621 title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001622 titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001623 titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001624 text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001625 textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001626 textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001627 disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
1628 disabledMessageResId = ShortcutService.parseIntAttribute(parser,
1629 ATTR_DISABLED_MESSAGE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001630 disabledMessageResName = ShortcutService.parseStringAttribute(parser,
1631 ATTR_DISABLED_MESSAGE_RES_NAME);
Makoto Onukia4f89b12017-10-05 10:37:55 -07001632 disabledReason = ShortcutService.parseIntAttribute(parser, ATTR_DISABLED_REASON);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001633 intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001634 rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001635 lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
Makoto Onuki31459242016-03-22 11:12:18 -07001636 flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
Makoto Onuki157b1622016-06-02 16:13:10 -07001637 iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
1638 iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001639 bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
1640
1641 final int outerDepth = parser.getDepth();
1642 int type;
1643 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1644 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1645 if (type != XmlPullParser.START_TAG) {
1646 continue;
1647 }
1648 final int depth = parser.getDepth();
1649 final String tag = parser.getName();
1650 if (ShortcutService.DEBUG_LOAD) {
1651 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1652 depth, type, tag));
1653 }
1654 switch (tag) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001655 case TAG_INTENT_EXTRAS_LEGACY:
1656 intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser);
1657 continue;
1658 case TAG_INTENT:
1659 intents.add(parseIntent(parser));
Makoto Onuki31459242016-03-22 11:12:18 -07001660 continue;
1661 case TAG_EXTRAS:
1662 extras = PersistableBundle.restoreFromXml(parser);
1663 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001664 case TAG_CATEGORIES:
1665 // This just contains string-array.
1666 continue;
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001667 case TAG_PERSON:
1668 persons.add(parsePerson(parser));
1669 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001670 case TAG_STRING_ARRAY_XMLUTILS:
1671 if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
1672 ATTR_NAME_XMLUTILS))) {
Makoto Onukibe73a802016-04-15 14:46:35 -07001673 final String[] ar = XmlUtils.readThisStringArrayXml(
1674 parser, TAG_STRING_ARRAY_XMLUTILS, null);
1675 categories = new ArraySet<>(ar.length);
1676 for (int i = 0; i < ar.length; i++) {
1677 categories.add(ar[i]);
1678 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001679 }
1680 continue;
Makoto Onuki31459242016-03-22 11:12:18 -07001681 }
1682 throw ShortcutService.throwForInvalidTag(depth, tag);
1683 }
Makoto Onukibe73a802016-04-15 14:46:35 -07001684
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001685 if (intentLegacy != null) {
1686 // For the legacy file format which supported only one intent per shortcut.
1687 ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy);
1688 intents.clear();
1689 intents.add(intentLegacy);
1690 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001691
Makoto Onukia4f89b12017-10-05 10:37:55 -07001692
1693 if ((disabledReason == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)
1694 && ((flags & ShortcutInfo.FLAG_DISABLED) != 0)) {
1695 // We didn't used to have the disabled reason, so if a shortcut is disabled
1696 // and has no reason, we assume it was disabled by publisher.
1697 disabledReason = ShortcutInfo.DISABLED_REASON_BY_APP;
1698 }
1699
1700 // All restored shortcuts are initially "shadow".
1701 if (fromBackup) {
1702 flags |= ShortcutInfo.FLAG_SHADOW;
1703 }
1704
Makoto Onuki31459242016-03-22 11:12:18 -07001705 return new ShortcutInfo(
Makoto Onuki20c95f82016-05-11 16:51:01 -07001706 userId, id, packageName, activityComponent, /* icon =*/ null,
Makoto Onuki157b1622016-06-02 16:13:10 -07001707 title, titleResId, titleResName, text, textResId, textResName,
1708 disabledMessage, disabledMessageResId, disabledMessageResName,
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001709 categories,
1710 intents.toArray(new Intent[intents.size()]),
1711 rank, extras, lastChangedTimestamp, flags,
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001712 iconResId, iconResName, bitmapPath, disabledReason,
1713 persons.toArray(new Person[persons.size()]));
Makoto Onuki31459242016-03-22 11:12:18 -07001714 }
Makoto Onuki2e210c42016-03-30 08:30:36 -07001715
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001716 private static Intent parseIntent(XmlPullParser parser)
1717 throws IOException, XmlPullParserException {
1718
1719 Intent intent = ShortcutService.parseIntentAttribute(parser,
1720 ATTR_INTENT_NO_EXTRA);
1721
1722 final int outerDepth = parser.getDepth();
1723 int type;
1724 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1725 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1726 if (type != XmlPullParser.START_TAG) {
1727 continue;
1728 }
1729 final int depth = parser.getDepth();
1730 final String tag = parser.getName();
1731 if (ShortcutService.DEBUG_LOAD) {
1732 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1733 depth, type, tag));
1734 }
1735 switch (tag) {
1736 case TAG_EXTRAS:
1737 ShortcutInfo.setIntentExtras(intent,
1738 PersistableBundle.restoreFromXml(parser));
1739 continue;
1740 }
1741 throw ShortcutService.throwForInvalidTag(depth, tag);
1742 }
1743 return intent;
1744 }
1745
Mehdi Alizadehebb4b602019-02-05 15:52:18 -08001746 private static Person parsePerson(XmlPullParser parser)
1747 throws IOException, XmlPullParserException {
1748 CharSequence name = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_NAME);
1749 String uri = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_URI);
1750 String key = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_KEY);
1751 boolean isBot = ShortcutService.parseBooleanAttribute(parser, ATTR_PERSON_IS_BOT);
1752 boolean isImportant = ShortcutService.parseBooleanAttribute(parser,
1753 ATTR_PERSON_IS_IMPORTANT);
1754
1755 Person.Builder builder = new Person.Builder();
1756 builder.setName(name).setUri(uri).setKey(key).setBot(isBot).setImportant(isImportant);
1757 return builder.build();
1758 }
1759
Makoto Onuki2e210c42016-03-30 08:30:36 -07001760 @VisibleForTesting
1761 List<ShortcutInfo> getAllShortcutsForTest() {
1762 return new ArrayList<>(mShortcuts.values());
1763 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001764
Mehdi Alizadeh32774622018-11-05 17:32:01 -08001765 @VisibleForTesting
1766 List<ShareTargetInfo> getAllShareTargetsForTest() {
1767 return new ArrayList<>(mShareTargets);
1768 }
1769
Makoto Onuki7001a612016-05-27 13:24:28 -07001770 @Override
1771 public void verifyStates() {
1772 super.verifyStates();
1773
1774 boolean failed = false;
1775
Makoto Onuki255461f2017-01-10 11:47:25 -08001776 final ShortcutService s = mShortcutUser.mService;
1777
Makoto Onuki7001a612016-05-27 13:24:28 -07001778 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1779 sortShortcutsToActivities();
1780
1781 // Make sure each activity won't have more than max shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001782 for (int outer = all.size() - 1; outer >= 0; outer--) {
1783 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1784 if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001785 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001786 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
1787 + " has " + all.valueAt(outer).size() + " shortcuts.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001788 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001789
1790 // Sort by rank.
1791 Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
1792
1793 // Split into two arrays for each kind.
1794 final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
1795 dynamicList.removeIf((si) -> !si.isDynamic());
1796
1797 final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
1798 dynamicList.removeIf((si) -> !si.isManifestShortcut());
1799
1800 verifyRanksSequential(dynamicList);
1801 verifyRanksSequential(manifestList);
Makoto Onuki7001a612016-05-27 13:24:28 -07001802 }
1803
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001804 // Verify each shortcut's status.
Makoto Onuki7001a612016-05-27 13:24:28 -07001805 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1806 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onuki99302b52017-03-29 12:42:26 -07001807 if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001808 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001809 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki99302b52017-03-29 12:42:26 -07001810 + " is not manifest, dynamic or pinned.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001811 }
Makoto Onukiff14f732016-06-30 17:07:25 -07001812 if (si.isDeclaredInManifest() && si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001813 failed = true;
1814 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1815 + " is both dynamic and manifest at the same time.");
1816 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001817 if (si.getActivity() == null && !si.isFloating()) {
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 Onuki255461f2017-01-10 11:47:25 -08001820 + " has null activity, but not floating.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001821 }
Makoto Onuki9fd90192017-01-06 18:31:03 +00001822 if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001823 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001824 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001825 + " is not floating, but is disabled.");
1826 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001827 if (si.isFloating() && si.getRank() != 0) {
1828 failed = true;
1829 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1830 + " is floating, but has rank=" + si.getRank());
1831 }
Makoto Onukidd097812016-06-29 13:10:09 -07001832 if (si.getIcon() != null) {
1833 failed = true;
1834 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1835 + " still has an icon");
1836 }
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001837 if (si.hasAdaptiveBitmap() && !si.hasIconFile()) {
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001838 failed = true;
1839 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Hyunyoung Songe4179e22017-03-01 12:51:26 -08001840 + " has adaptive bitmap but was not saved to a file.");
Hyunyoung Songf281e7a2017-02-13 10:57:42 -08001841 }
Makoto Onukidd097812016-06-29 13:10:09 -07001842 if (si.hasIconFile() && si.hasIconResource()) {
1843 failed = true;
1844 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1845 + " has both resource and bitmap icons");
1846 }
Makoto Onukia4f89b12017-10-05 10:37:55 -07001847 if (si.isEnabled()
1848 != (si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)) {
1849 failed = true;
1850 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1851 + " isEnabled() and getDisabledReason() disagree: "
1852 + si.isEnabled() + " vs " + si.getDisabledReason());
1853 }
1854 if ((si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
1855 && (getPackageInfo().getBackupSourceVersionCode()
1856 == ShortcutInfo.VERSION_CODE_UNKNOWN)) {
1857 failed = true;
1858 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1859 + " RESTORED_VERSION_LOWER with no backup source version code.");
1860 }
Makoto Onuki255461f2017-01-10 11:47:25 -08001861 if (s.isDummyMainActivity(si.getActivity())) {
1862 failed = true;
1863 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1864 + " has a dummy target activity");
1865 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001866 }
1867
1868 if (failed) {
Makoto Onuki9fd90192017-01-06 18:31:03 +00001869 throw new IllegalStateException("See logcat for errors");
Makoto Onuki7001a612016-05-27 13:24:28 -07001870 }
1871 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001872
1873 private boolean verifyRanksSequential(List<ShortcutInfo> list) {
1874 boolean failed = false;
1875
1876 for (int i = 0; i < list.size(); i++) {
1877 final ShortcutInfo si = list.get(i);
1878 if (si.getRank() != i) {
1879 failed = true;
1880 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1881 + " rank=" + si.getRank() + " but expected to be "+ i);
1882 }
1883 }
1884 return failed;
1885 }
Makoto Onuki31459242016-03-22 11:12:18 -07001886}