blob: b94d0f0dbd1c77dc08ab87c5ff336cd927ce3352 [file] [log] [blame]
Makoto Onuki31459242016-03-22 11:12:18 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.pm;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
Makoto Onukiabe84422016-04-07 09:41:19 -070020import android.annotation.UserIdInt;
Makoto Onuki31459242016-03-22 11:12:18 -070021import android.content.ComponentName;
22import android.content.Intent;
Makoto Onuki22fcc682016-05-17 14:52:19 -070023import android.content.pm.PackageInfo;
Makoto Onuki31459242016-03-22 11:12:18 -070024import android.content.pm.ShortcutInfo;
Makoto Onuki157b1622016-06-02 16:13:10 -070025import android.content.res.Resources;
Makoto Onuki31459242016-03-22 11:12:18 -070026import android.os.PersistableBundle;
27import android.text.format.Formatter;
28import android.util.ArrayMap;
29import android.util.ArraySet;
Makoto Onuki7001a612016-05-27 13:24:28 -070030import android.util.Log;
Makoto Onuki31459242016-03-22 11:12:18 -070031import android.util.Slog;
32
Makoto Onuki2e210c42016-03-30 08:30:36 -070033import com.android.internal.annotations.VisibleForTesting;
Makoto Onuki22fcc682016-05-17 14:52:19 -070034import com.android.internal.util.Preconditions;
Makoto Onukib6d35232016-04-04 15:57:17 -070035import com.android.internal.util.XmlUtils;
Makoto Onuki7001a612016-05-27 13:24:28 -070036import com.android.server.pm.ShortcutService.ShortcutOperation;
Makoto Onuki2e210c42016-03-30 08:30:36 -070037
Makoto Onuki31459242016-03-22 11:12:18 -070038import org.xmlpull.v1.XmlPullParser;
39import org.xmlpull.v1.XmlPullParserException;
40import org.xmlpull.v1.XmlSerializer;
41
42import java.io.File;
43import java.io.IOException;
44import java.io.PrintWriter;
45import java.util.ArrayList;
Makoto Onuki7001a612016-05-27 13:24:28 -070046import java.util.Collections;
47import java.util.Comparator;
Makoto Onuki31459242016-03-22 11:12:18 -070048import java.util.List;
Makoto Onukibe73a802016-04-15 14:46:35 -070049import java.util.Set;
Makoto Onuki31459242016-03-22 11:12:18 -070050import java.util.function.Predicate;
51
52/**
53 * Package information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070054 * User information used by {@link ShortcutService}.
55 *
56 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070057 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070058class ShortcutPackage extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070059 private static final String TAG = ShortcutService.TAG;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070060 private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
Makoto Onuki31459242016-03-22 11:12:18 -070061
62 static final String TAG_ROOT = "package";
63 private static final String TAG_INTENT_EXTRAS = "intent-extras";
64 private static final String TAG_EXTRAS = "extras";
65 private static final String TAG_SHORTCUT = "shortcut";
Makoto Onukib6d35232016-04-04 15:57:17 -070066 private static final String TAG_CATEGORIES = "categories";
Makoto Onuki31459242016-03-22 11:12:18 -070067
68 private static final String ATTR_NAME = "name";
Makoto Onuki31459242016-03-22 11:12:18 -070069 private static final String ATTR_CALL_COUNT = "call-count";
70 private static final String ATTR_LAST_RESET = "last-reset";
71 private static final String ATTR_ID = "id";
72 private static final String ATTR_ACTIVITY = "activity";
73 private static final String ATTR_TITLE = "title";
Makoto Onuki20c95f82016-05-11 16:51:01 -070074 private static final String ATTR_TITLE_RES_ID = "titleid";
Makoto Onuki157b1622016-06-02 16:13:10 -070075 private static final String ATTR_TITLE_RES_NAME = "titlename";
Makoto Onukie3ae7ec2016-03-29 15:45:25 -070076 private static final String ATTR_TEXT = "text";
Makoto Onuki20c95f82016-05-11 16:51:01 -070077 private static final String ATTR_TEXT_RES_ID = "textid";
Makoto Onuki157b1622016-06-02 16:13:10 -070078 private static final String ATTR_TEXT_RES_NAME = "textname";
Makoto Onuki20c95f82016-05-11 16:51:01 -070079 private static final String ATTR_DISABLED_MESSAGE = "dmessage";
80 private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
Makoto Onuki157b1622016-06-02 16:13:10 -070081 private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
Makoto Onuki31459242016-03-22 11:12:18 -070082 private static final String ATTR_INTENT = "intent";
Makoto Onuki20c95f82016-05-11 16:51:01 -070083 private static final String ATTR_RANK = "rank";
Makoto Onuki31459242016-03-22 11:12:18 -070084 private static final String ATTR_TIMESTAMP = "timestamp";
85 private static final String ATTR_FLAGS = "flags";
Makoto Onuki157b1622016-06-02 16:13:10 -070086 private static final String ATTR_ICON_RES_ID = "icon-res";
87 private static final String ATTR_ICON_RES_NAME = "icon-resname";
Makoto Onuki31459242016-03-22 11:12:18 -070088 private static final String ATTR_BITMAP_PATH = "bitmap-path";
89
Makoto Onukib6d35232016-04-04 15:57:17 -070090 private static final String NAME_CATEGORIES = "categories";
91
92 private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
93 private static final String ATTR_NAME_XMLUTILS = "name";
94
Makoto Onuki31459242016-03-22 11:12:18 -070095 /**
96 * All the shortcuts from the package, keyed on IDs.
97 */
98 final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
99
100 /**
Makoto Onuki31459242016-03-22 11:12:18 -0700101 * # of times the package has called rate-limited APIs.
102 */
103 private int mApiCallCount;
104
105 /**
106 * When {@link #mApiCallCount} was reset last time.
107 */
108 private long mLastResetTime;
109
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700110 private final int mPackageUid;
111
112 private long mLastKnownForegroundElapsedTime;
113
Makoto Onukic51b2872016-05-04 15:24:50 -0700114 private ShortcutPackage(ShortcutUser shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700115 int packageUserId, String packageName, ShortcutPackageInfo spi) {
116 super(shortcutUser, packageUserId, packageName,
117 spi != null ? spi : ShortcutPackageInfo.newEmpty());
118
Makoto Onukic51b2872016-05-04 15:24:50 -0700119 mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700120 }
121
Makoto Onukic51b2872016-05-04 15:24:50 -0700122 public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
123 this(shortcutUser, packageUserId, packageName, null);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700124 }
125
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700126 @Override
127 public int getOwnerUserId() {
128 // For packages, always owner user == package user.
129 return getPackageUserId();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700130 }
131
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700132 public int getPackageUid() {
133 return mPackageUid;
134 }
135
Makoto Onuki39686e82016-04-13 18:03:00 -0700136 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700137 * Called when a shortcut is about to be published. At this point we know the publisher
138 * package
Makoto Onuki39686e82016-04-13 18:03:00 -0700139 * exists (as opposed to Launcher trying to fetch shortcuts from a non-existent package), so
140 * we do some initialization for the package.
141 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700142 private void ensurePackageVersionInfo() {
Makoto Onuki39686e82016-04-13 18:03:00 -0700143 // Make sure we have the version code for the app. We need the version code in
144 // handlePackageUpdated().
145 if (getPackageInfo().getVersionCode() < 0) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700146 final ShortcutService s = mShortcutUser.mService;
147
Makoto Onuki22fcc682016-05-17 14:52:19 -0700148 final PackageInfo pi = s.getPackageInfo(getPackageName(), getOwnerUserId());
149 if (pi != null) {
150 if (ShortcutService.DEBUG) {
151 Slog.d(TAG, String.format("Package %s version = %d", getPackageName(),
152 pi.versionCode));
153 }
154 getPackageInfo().updateVersionInfo(pi);
Makoto Onuki39686e82016-04-13 18:03:00 -0700155 s.scheduleSaveUser(getOwnerUserId());
156 }
157 }
158 }
159
Makoto Onuki157b1622016-06-02 16:13:10 -0700160 @Nullable
161 public Resources getPackageResources() {
162 return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
163 getPackageName(), getPackageUserId());
164 }
165
Makoto Onuki2e210c42016-03-30 08:30:36 -0700166 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -0700167 protected void onRestoreBlocked() {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700168 // Can't restore due to version/signature mismatch. Remove all shortcuts.
169 mShortcuts.clear();
170 }
171
172 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -0700173 protected void onRestored() {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700174 // Because some launchers may not have been restored (e.g. allowBackup=false),
175 // we need to re-calculate the pinned shortcuts.
Makoto Onukic51b2872016-05-04 15:24:50 -0700176 refreshPinnedFlags();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700177 }
178
Makoto Onukid99c6f02016-03-28 11:02:54 -0700179 /**
180 * Note this does *not* provide a correct view to the calling launcher.
181 */
Makoto Onuki31459242016-03-22 11:12:18 -0700182 @Nullable
183 public ShortcutInfo findShortcutById(String id) {
184 return mShortcuts.get(id);
185 }
186
Makoto Onuki22fcc682016-05-17 14:52:19 -0700187 private void ensureNotImmutable(@Nullable ShortcutInfo shortcut) {
188 if (shortcut != null && shortcut.isImmutable()) {
189 throw new IllegalArgumentException(
190 "Manifest shortcut ID=" + shortcut.getId()
191 + " may not be manipulated via APIs");
192 }
193 }
194
195 private void ensureNotImmutable(@NonNull String id) {
196 ensureNotImmutable(mShortcuts.get(id));
197 }
198
199 public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds) {
200 for (int i = shortcutIds.size() - 1; i >= 0; i--) {
201 ensureNotImmutable(shortcutIds.get(i));
202 }
203 }
204
205 public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts) {
206 for (int i = shortcuts.size() - 1; i >= 0; i--) {
207 ensureNotImmutable(shortcuts.get(i).getId());
208 }
209 }
210
211 private ShortcutInfo deleteShortcutInner(@NonNull String id) {
Makoto Onuki31459242016-03-22 11:12:18 -0700212 final ShortcutInfo shortcut = mShortcuts.remove(id);
213 if (shortcut != null) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700214 mShortcutUser.mService.removeIcon(getPackageUserId(), shortcut);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700215 shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
216 | ShortcutInfo.FLAG_MANIFEST);
Makoto Onuki31459242016-03-22 11:12:18 -0700217 }
218 return shortcut;
219 }
220
Makoto Onuki22fcc682016-05-17 14:52:19 -0700221 private void addShortcutInner(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700222 final ShortcutService s = mShortcutUser.mService;
223
Makoto Onuki22fcc682016-05-17 14:52:19 -0700224 deleteShortcutInner(newShortcut.getId());
Makoto Onuki157b1622016-06-02 16:13:10 -0700225
226 // Extract Icon and update the icon res ID and the bitmap path.
227 s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut);
228 s.fixUpShortcutResourceNamesAndValues(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700229 mShortcuts.put(newShortcut.getId(), newShortcut);
230 }
231
232 /**
233 * Add a shortcut, or update one with the same ID, with taking over existing flags.
234 *
235 * It checks the max number of dynamic shortcuts.
236 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700237 public void addOrUpdateDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
Makoto Onuki39686e82016-04-13 18:03:00 -0700238
Makoto Onuki22fcc682016-05-17 14:52:19 -0700239 Preconditions.checkArgument(newShortcut.isEnabled(),
240 "add/setDynamicShortcuts() cannot publish disabled shortcuts");
241
242 ensurePackageVersionInfo();
Makoto Onuki39686e82016-04-13 18:03:00 -0700243
Makoto Onuki31459242016-03-22 11:12:18 -0700244 newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
245
246 final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
247
248 final boolean wasPinned;
Makoto Onuki31459242016-03-22 11:12:18 -0700249
250 if (oldShortcut == null) {
251 wasPinned = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700252 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700253 // It's an update case.
254 // Make sure the target is updatable. (i.e. should be mutable.)
255 oldShortcut.ensureUpdatableWith(newShortcut);
256
Makoto Onuki31459242016-03-22 11:12:18 -0700257 wasPinned = oldShortcut.isPinned();
Makoto Onuki31459242016-03-22 11:12:18 -0700258 }
259
Makoto Onuki157b1622016-06-02 16:13:10 -0700260 // If it was originally pinned, the new one should be pinned too.
Makoto Onuki31459242016-03-22 11:12:18 -0700261 if (wasPinned) {
262 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
263 }
264
Makoto Onuki22fcc682016-05-17 14:52:19 -0700265 addShortcutInner(newShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700266 }
267
268 /**
269 * Remove all shortcuts that aren't pinned nor dynamic.
270 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700271 private void removeOrphans() {
Makoto Onuki31459242016-03-22 11:12:18 -0700272 ArrayList<String> removeList = null; // Lazily initialize.
273
274 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
275 final ShortcutInfo si = mShortcuts.valueAt(i);
276
Makoto Onuki22fcc682016-05-17 14:52:19 -0700277 if (si.isAlive()) continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700278
279 if (removeList == null) {
280 removeList = new ArrayList<>();
281 }
282 removeList.add(si.getId());
283 }
284 if (removeList != null) {
285 for (int i = removeList.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700286 deleteShortcutInner(removeList.get(i));
Makoto Onuki31459242016-03-22 11:12:18 -0700287 }
288 }
289 }
290
291 /**
292 * Remove all dynamic shortcuts.
293 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700294 public void deleteAllDynamicShortcuts() {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700295 final long now = mShortcutUser.mService.injectCurrentTimeMillis();
296
Makoto Onuki22fcc682016-05-17 14:52:19 -0700297 boolean changed = false;
Makoto Onuki31459242016-03-22 11:12:18 -0700298 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700299 final ShortcutInfo si = mShortcuts.valueAt(i);
300 if (si.isDynamic()) {
301 changed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700302
303 si.setTimestamp(now);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700304 si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700305 si.setRank(0); // It may still be pinned, so clear the rank.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700306 }
Makoto Onuki31459242016-03-22 11:12:18 -0700307 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700308 if (changed) {
309 removeOrphans();
310 }
Makoto Onuki31459242016-03-22 11:12:18 -0700311 }
312
313 /**
Makoto Onuki7001a612016-05-27 13:24:28 -0700314 * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
315 * is pinned, it'll remain as a pinned shortcut, and is still enabled.
Makoto Onukib08790c2016-06-23 14:05:46 -0700316 *
317 * @return true if it's actually removed because it wasn't pinned, or false if it's still
318 * pinned.
Makoto Onuki31459242016-03-22 11:12:18 -0700319 */
Makoto Onukib08790c2016-06-23 14:05:46 -0700320 public boolean deleteDynamicWithId(@NonNull String shortcutId) {
321 final ShortcutInfo removed = deleteOrDisableWithId(
322 shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false);
323 return removed == null;
324 }
325
326 /**
327 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
328 * is pinned, it'll remain as a pinned shortcut, but will be disabled.
329 *
330 * @return true if it's actually removed because it wasn't pinned, or false if it's still
331 * pinned.
332 */
333 private boolean disableDynamicWithId(@NonNull String shortcutId) {
334 final ShortcutInfo disabled = deleteOrDisableWithId(
335 shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false);
336 return disabled == null;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700337 }
338
Makoto Onuki7001a612016-05-27 13:24:28 -0700339 /**
340 * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut
341 * is pinned, it'll remain as a pinned shortcut but will be disabled.
342 */
Makoto Onuki22fcc682016-05-17 14:52:19 -0700343 public void disableWithId(@NonNull String shortcutId, String disabledMessage,
344 int disabledMessageResId, boolean overrideImmutable) {
345 final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
346 overrideImmutable);
347
348 if (disabled != null) {
349 if (disabledMessage != null) {
350 disabled.setDisabledMessage(disabledMessage);
351 } else if (disabledMessageResId != 0) {
352 disabled.setDisabledMessageResId(disabledMessageResId);
Makoto Onuki157b1622016-06-02 16:13:10 -0700353
354 mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700355 }
356 }
357 }
358
359 @Nullable
360 private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
361 boolean overrideImmutable) {
Makoto Onuki31459242016-03-22 11:12:18 -0700362 final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
363
Makoto Onuki22fcc682016-05-17 14:52:19 -0700364 if (oldShortcut == null || !oldShortcut.isEnabled()) {
365 return null; // Doesn't exist or already disabled.
Makoto Onuki31459242016-03-22 11:12:18 -0700366 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700367 if (!overrideImmutable) {
368 ensureNotImmutable(oldShortcut);
Makoto Onuki31459242016-03-22 11:12:18 -0700369 }
370 if (oldShortcut.isPinned()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700371
372 oldShortcut.setRank(0);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700373 oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
374 if (disable) {
375 oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
376 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700377 oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
378
Makoto Onuki22fcc682016-05-17 14:52:19 -0700379 return oldShortcut;
Makoto Onuki31459242016-03-22 11:12:18 -0700380 } else {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700381 deleteShortcutInner(shortcutId);
382 return null;
383 }
384 }
385
386 public void enableWithId(@NonNull String shortcutId) {
387 final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
388 if (shortcut != null) {
389 ensureNotImmutable(shortcut);
390 shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
Makoto Onuki31459242016-03-22 11:12:18 -0700391 }
392 }
393
394 /**
395 * Called after a launcher updates the pinned set. For each shortcut in this package,
396 * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it.
397 *
398 * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
399 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700400 public void refreshPinnedFlags() {
Makoto Onuki31459242016-03-22 11:12:18 -0700401 // First, un-pin all shortcuts
402 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
403 mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
404 }
405
406 // Then, for the pinned set for each launcher, set the pin flag one by one.
Makoto Onukic51b2872016-05-04 15:24:50 -0700407 mShortcutUser.mService.getUserShortcutsLocked(getPackageUserId())
408 .forAllLaunchers(launcherShortcuts -> {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700409 final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
Makoto Onuki2e210c42016-03-30 08:30:36 -0700410 getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700411
412 if (pinned == null || pinned.size() == 0) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700413 return;
Makoto Onuki31459242016-03-22 11:12:18 -0700414 }
415 for (int i = pinned.size() - 1; i >= 0; i--) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700416 final String id = pinned.valueAt(i);
417 final ShortcutInfo si = mShortcuts.get(id);
Makoto Onuki31459242016-03-22 11:12:18 -0700418 if (si == null) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700419 // This happens if a launcher pinned shortcuts from this package, then backup&
420 // restored, but this package doesn't allow backing up.
421 // In that case the launcher ends up having a dangling pinned shortcuts.
422 // That's fine, when the launcher is restored, we'll fix it.
423 continue;
Makoto Onuki31459242016-03-22 11:12:18 -0700424 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700425 si.addFlags(ShortcutInfo.FLAG_PINNED);
Makoto Onuki31459242016-03-22 11:12:18 -0700426 }
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700427 });
Makoto Onuki31459242016-03-22 11:12:18 -0700428
429 // Lastly, remove the ones that are no longer pinned nor dynamic.
Makoto Onukic51b2872016-05-04 15:24:50 -0700430 removeOrphans();
Makoto Onuki31459242016-03-22 11:12:18 -0700431 }
432
433 /**
434 * Number of calls that the caller has made, since the last reset.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700435 *
436 * <p>This takes care of the resetting the counter for foreground apps as well as after
437 * locale changes.
Makoto Onuki31459242016-03-22 11:12:18 -0700438 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700439 public int getApiCallCount() {
440 mShortcutUser.resetThrottlingIfNeeded();
441
442 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700443
444 // Reset the counter if:
445 // - the package is in foreground now.
446 // - the package is *not* in foreground now, but was in foreground at some point
447 // since the previous time it had been.
448 if (s.isUidForegroundLocked(mPackageUid)
449 || mLastKnownForegroundElapsedTime
450 < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) {
451 mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
Makoto Onukic51b2872016-05-04 15:24:50 -0700452 resetRateLimiting();
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700453 }
454
455 // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
456 // but we just can't return 0 at this point, because we may have to update
457 // mLastResetTime.
458
Makoto Onuki31459242016-03-22 11:12:18 -0700459 final long last = s.getLastResetTimeLocked();
460
461 final long now = s.injectCurrentTimeMillis();
462 if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
463 Slog.w(TAG, "Clock rewound");
464 // Clock rewound.
465 mLastResetTime = now;
466 mApiCallCount = 0;
467 return mApiCallCount;
468 }
469
470 // If not reset yet, then reset.
471 if (mLastResetTime < last) {
472 if (ShortcutService.DEBUG) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700473 Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
474 getPackageName(), mLastResetTime, now, last));
Makoto Onuki31459242016-03-22 11:12:18 -0700475 }
476 mApiCallCount = 0;
477 mLastResetTime = last;
478 }
479 return mApiCallCount;
480 }
481
482 /**
483 * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
484 * and return true. Otherwise just return false.
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700485 *
486 * <p>This takes care of the resetting the counter for foreground apps as well as after
487 * locale changes, which is done internally by {@link #getApiCallCount}.
Makoto Onuki31459242016-03-22 11:12:18 -0700488 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700489 public boolean tryApiCall() {
490 final ShortcutService s = mShortcutUser.mService;
491
492 if (getApiCallCount() >= s.mMaxUpdatesPerInterval) {
Makoto Onuki31459242016-03-22 11:12:18 -0700493 return false;
494 }
495 mApiCallCount++;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700496 s.scheduleSaveUser(getOwnerUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700497 return true;
498 }
499
Makoto Onukic51b2872016-05-04 15:24:50 -0700500 public void resetRateLimiting() {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700501 if (ShortcutService.DEBUG) {
502 Slog.d(TAG, "resetRateLimiting: " + getPackageName());
503 }
504 if (mApiCallCount > 0) {
505 mApiCallCount = 0;
Makoto Onukic51b2872016-05-04 15:24:50 -0700506 mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700507 }
508 }
509
510 public void resetRateLimitingForCommandLineNoSaving() {
Makoto Onuki31459242016-03-22 11:12:18 -0700511 mApiCallCount = 0;
512 mLastResetTime = 0;
513 }
514
515 /**
516 * Find all shortcuts that match {@code query}.
517 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700518 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700519 @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
Makoto Onukic51b2872016-05-04 15:24:50 -0700520 findAll(result, query, cloneFlag, null, 0);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700521 }
522
523 /**
524 * Find all shortcuts that match {@code query}.
525 *
526 * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
527 * by the calling launcher will not be included in the result, and also "isPinned" will be
528 * adjusted for the caller too.
529 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700530 public void findAll(@NonNull List<ShortcutInfo> result,
Makoto Onuki31459242016-03-22 11:12:18 -0700531 @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
Makoto Onukid99c6f02016-03-28 11:02:54 -0700532 @Nullable String callingLauncher, int launcherUserId) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700533 if (getPackageInfo().isShadow()) {
534 // Restored and the app not installed yet, so don't return any.
535 return;
536 }
Makoto Onuki31459242016-03-22 11:12:18 -0700537
Makoto Onukic51b2872016-05-04 15:24:50 -0700538 final ShortcutService s = mShortcutUser.mService;
539
Makoto Onuki31459242016-03-22 11:12:18 -0700540 // Set of pinned shortcuts by the calling launcher.
541 final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
Makoto Onuki2e210c42016-03-30 08:30:36 -0700542 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
543 .getPinnedShortcutIds(getPackageName(), getPackageUserId());
Makoto Onuki31459242016-03-22 11:12:18 -0700544
545 for (int i = 0; i < mShortcuts.size(); i++) {
546 final ShortcutInfo si = mShortcuts.valueAt(i);
547
Makoto Onuki22fcc682016-05-17 14:52:19 -0700548 // Need to adjust PINNED flag depending on the caller.
549 // Basically if the caller is a launcher (callingLauncher != null) and the launcher
550 // isn't pinning it, then we need to clear PINNED for this caller.
Makoto Onuki31459242016-03-22 11:12:18 -0700551 final boolean isPinnedByCaller = (callingLauncher == null)
552 || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
Makoto Onuki22fcc682016-05-17 14:52:19 -0700553
554 if (si.isFloating()) {
Makoto Onuki31459242016-03-22 11:12:18 -0700555 if (!isPinnedByCaller) {
556 continue;
557 }
558 }
559 final ShortcutInfo clone = si.clone(cloneFlag);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700560
Makoto Onuki31459242016-03-22 11:12:18 -0700561 // Fix up isPinned for the caller. Note we need to do it before the "test" callback,
562 // since it may check isPinned.
563 if (!isPinnedByCaller) {
564 clone.clearFlags(ShortcutInfo.FLAG_PINNED);
565 }
566 if (query == null || query.test(clone)) {
567 result.add(clone);
568 }
569 }
570 }
571
572 public void resetThrottling() {
573 mApiCallCount = 0;
574 }
575
Makoto Onuki39686e82016-04-13 18:03:00 -0700576 /**
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700577 * Return the filenames (excluding path names) of icon bitmap files from this package.
578 */
579 public ArraySet<String> getUsedBitmapFiles() {
580 final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
581
582 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
583 final ShortcutInfo si = mShortcuts.valueAt(i);
584 if (si.getBitmapPath() != null) {
585 usedFiles.add(getFileName(si.getBitmapPath()));
586 }
587 }
588 return usedFiles;
589 }
590
591 private static String getFileName(@NonNull String path) {
592 final int sep = path.lastIndexOf(File.separatorChar);
593 if (sep == -1) {
594 return path;
595 } else {
596 return path.substring(sep + 1);
597 }
598 }
599
600 /**
Makoto Onuki22fcc682016-05-17 14:52:19 -0700601 * Called when the package is updated or added.
602 *
603 * Add case:
604 * - Publish manifest shortcuts.
605 *
606 * Update case:
607 * - Re-publish manifest shortcuts.
608 * - If there are shortcuts with resources (icons or strings), update their timestamps.
609 *
610 * @return TRUE if any shortcuts have been changed.
Makoto Onuki39686e82016-04-13 18:03:00 -0700611 */
Makoto Onukib08790c2016-06-23 14:05:46 -0700612 public boolean handlePackageAddedOrUpdated(boolean isNewApp, boolean forceRescan) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700613 final PackageInfo pi = mShortcutUser.mService.getPackageInfo(
614 getPackageName(), getPackageUserId());
615 if (pi == null) {
616 return false; // Shouldn't happen.
Makoto Onuki39686e82016-04-13 18:03:00 -0700617 }
618
Makoto Onukib08790c2016-06-23 14:05:46 -0700619 if (!isNewApp && !forceRescan) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700620 // Make sure the version code or last update time has changed.
621 // Otherwise, nothing to do.
622 if (getPackageInfo().getVersionCode() >= pi.versionCode
623 && getPackageInfo().getLastUpdateTime() >= pi.lastUpdateTime) {
624 return false;
625 }
626 }
627
628 // Now prepare to publish manifest shortcuts.
629 List<ShortcutInfo> newManifestShortcutList = null;
630 try {
631 newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
632 getPackageName(), getPackageUserId());
633 } catch (IOException|XmlPullParserException e) {
634 Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
635 }
636 final int manifestShortcutSize = newManifestShortcutList == null ? 0
637 : newManifestShortcutList.size();
638 if (ShortcutService.DEBUG) {
639 Slog.d(TAG, String.format("Package %s has %d manifest shortcut(s)",
640 getPackageName(), manifestShortcutSize));
641 }
642 if (isNewApp && (manifestShortcutSize == 0)) {
643 // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
644
645 // If it's an update, then it may already have manifest shortcuts, which need to be
646 // disabled.
647 return false;
648 }
649 if (ShortcutService.DEBUG) {
650 Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
651 (isNewApp ? "added" : "updated"),
652 getPackageInfo().getVersionCode(), pi.versionCode));
653 }
654
655 getPackageInfo().updateVersionInfo(pi);
Makoto Onuki39686e82016-04-13 18:03:00 -0700656
Makoto Onukic51b2872016-05-04 15:24:50 -0700657 final ShortcutService s = mShortcutUser.mService;
658
Makoto Onuki39686e82016-04-13 18:03:00 -0700659 boolean changed = false;
Makoto Onuki39686e82016-04-13 18:03:00 -0700660
Makoto Onuki22fcc682016-05-17 14:52:19 -0700661 // For existing shortcuts, update timestamps if they have any resources.
Makoto Onukib08790c2016-06-23 14:05:46 -0700662 // Also check if shortcuts' activities are still main activities. Otherwise, disable them.
Makoto Onuki22fcc682016-05-17 14:52:19 -0700663 if (!isNewApp) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700664 Resources publisherRes = null;
665
Makoto Onuki22fcc682016-05-17 14:52:19 -0700666 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
667 final ShortcutInfo si = mShortcuts.valueAt(i);
668
Makoto Onukib08790c2016-06-23 14:05:46 -0700669 if (si.isDynamic()) {
670 if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
671 Slog.w(TAG, String.format(
672 "%s is no longer main activity. Disabling shorcut %s.",
673 getPackageName(), si.getId()));
674 if (disableDynamicWithId(si.getId())) {
675 continue; // Actually removed.
676 }
677 // Still pinned, so fall-through and possibly update the resources.
678 }
679 changed = true;
680 }
681
Makoto Onuki22fcc682016-05-17 14:52:19 -0700682 if (si.hasAnyResources()) {
Makoto Onuki157b1622016-06-02 16:13:10 -0700683 if (!si.isOriginallyFromManifest()) {
684 if (publisherRes == null) {
685 publisherRes = getPackageResources();
686 if (publisherRes == null) {
687 break; // Resources couldn't be loaded.
688 }
689 }
690
691 // If this shortcut is not from a manifest, then update all resource IDs
692 // from resource names. (We don't allow resource strings for
693 // non-manifest at the moment, but icons can still be resources.)
694 si.lookupAndFillInResourceIds(publisherRes);
695 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700696 changed = true;
697 si.setTimestamp(s.injectCurrentTimeMillis());
698 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700699 }
700 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700701
702 // (Re-)publish manifest shortcut.
703 changed |= publishManifestShortcuts(newManifestShortcutList);
704
Makoto Onuki7001a612016-05-27 13:24:28 -0700705 if (newManifestShortcutList != null) {
706 changed |= pushOutExcessShortcuts();
707 }
708
Makoto Onukidf6da042016-06-16 09:51:40 -0700709 s.verifyStates();
710
Makoto Onuki39686e82016-04-13 18:03:00 -0700711 if (changed) {
712 // This will send a notification to the launcher, and also save .
713 s.packageShortcutsChanged(getPackageName(), getPackageUserId());
714 } else {
715 // Still save the version code.
716 s.scheduleSaveUser(getPackageUserId());
717 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700718 return changed;
719 }
720
721 private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
722 if (ShortcutService.DEBUG) {
723 Slog.d(TAG, String.format(
724 "Package %s: publishing manifest shortcuts", getPackageName()));
725 }
726 boolean changed = false;
727
Makoto Onuki22fcc682016-05-17 14:52:19 -0700728 // Keep the previous IDs.
729 ArraySet<String> toDisableList = null;
730 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
731 final ShortcutInfo si = mShortcuts.valueAt(i);
732
733 if (si.isManifestShortcut()) {
734 if (toDisableList == null) {
735 toDisableList = new ArraySet<>();
736 }
737 toDisableList.add(si.getId());
738 }
739 }
740
741 // Publish new ones.
742 if (newManifestShortcutList != null) {
743 final int newListSize = newManifestShortcutList.size();
744
745 for (int i = 0; i < newListSize; i++) {
746 changed = true;
747
748 final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
749 final boolean newDisabled = !newShortcut.isEnabled();
750
Makoto Onuki7001a612016-05-27 13:24:28 -0700751 final String id = newShortcut.getId();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700752 final ShortcutInfo oldShortcut = mShortcuts.get(id);
753
754 boolean wasPinned = false;
755
756 if (oldShortcut != null) {
757 if (!oldShortcut.isOriginallyFromManifest()) {
758 Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
759 + " exists but is not from AndroidManifest.xml, not updating.");
760 continue;
761 }
762 // Take over the pinned flag.
763 if (oldShortcut.isPinned()) {
764 wasPinned = true;
765 newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
766 }
767 }
768 if (newDisabled && !wasPinned) {
769 // If the shortcut is disabled, and it was *not* pinned, then this
770 // just doesn't have to be published.
771 // Just keep it in toDisableList, so the previous one would be removed.
772 continue;
773 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700774
775 // Note even if enabled=false, we still need to update all fields, so do it
776 // regardless.
777 addShortcutInner(newShortcut); // This will clean up the old one too.
778
779 if (!newDisabled && toDisableList != null) {
780 // Still alive, don't remove.
781 toDisableList.remove(id);
782 }
783 }
784 }
785
786 // Disable the previous manifest shortcuts that are no longer in the manifest.
787 if (toDisableList != null) {
788 if (ShortcutService.DEBUG) {
789 Slog.d(TAG, String.format(
790 "Package %s: disabling %d stale shortcuts", getPackageName(),
791 toDisableList.size()));
792 }
793 for (int i = toDisableList.size() - 1; i >= 0; i--) {
794 changed = true;
795
796 final String id = toDisableList.valueAt(i);
797
798 disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
799 /* overrideImmutable=*/ true);
800 }
801 removeOrphans();
802 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700803 adjustRanks();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700804 return changed;
Makoto Onuki39686e82016-04-13 18:03:00 -0700805 }
806
Makoto Onuki7001a612016-05-27 13:24:28 -0700807 /**
808 * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
809 * If too many, we'll remove the dynamic with the lowest ranks.
810 */
811 private boolean pushOutExcessShortcuts() {
812 final ShortcutService service = mShortcutUser.mService;
813 final int maxShortcuts = service.getMaxActivityShortcuts();
814
815 boolean changed = false;
816
817 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
818 sortShortcutsToActivities();
819 for (int outer = all.size() - 1; outer >= 0; outer--) {
820 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
821 if (list.size() <= maxShortcuts) {
822 continue;
823 }
824 // Sort by isManifestShortcut() and getRank().
825 Collections.sort(list, mShortcutTypeAndRankComparator);
826
827 // Keep [0 .. max), and remove (as dynamic) [max .. size)
828 for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
829 final ShortcutInfo shortcut = list.get(inner);
830
831 if (shortcut.isManifestShortcut()) {
832 // This shouldn't happen -- excess shortcuts should all be non-manifest.
833 // But just in case.
834 service.wtf("Found manifest shortcuts in excess list.");
835 continue;
836 }
837 deleteDynamicWithId(shortcut.getId());
838 }
839 }
Makoto Onuki7001a612016-05-27 13:24:28 -0700840
841 return changed;
842 }
843
844 /**
845 * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before
846 * non-manifest shortcuts, then sort by rank.
847 *
848 * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
849 * manifest shortcuts than before and as a result we need to remove some of the dynamic
850 * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with
851 * the last ones.
852 *
853 * (Note the number of manifest shortcuts is always <= the max number, because if there are
854 * more, ShortcutParser would ignore the rest.)
855 */
856 final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
857 ShortcutInfo b) -> {
858 if (a.isManifestShortcut() && !b.isManifestShortcut()) {
859 return -1;
860 }
861 if (!a.isManifestShortcut() && b.isManifestShortcut()) {
862 return 1;
863 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700864 return Integer.compare(a.getRank(), b.getRank());
Makoto Onuki7001a612016-05-27 13:24:28 -0700865 };
866
867 /**
868 * Build a list of shortcuts for each target activity and return as a map. The result won't
869 * contain "floating" shortcuts because they don't belong on any activities.
870 */
871 private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
Makoto Onuki7001a612016-05-27 13:24:28 -0700872 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
873 = new ArrayMap<>();
874 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
875 final ShortcutInfo si = mShortcuts.valueAt(i);
876 if (si.isFloating()) {
877 continue; // Ignore floating shortcuts, which are not tied to any activities.
878 }
879
880 final ComponentName activity = si.getActivity();
881
882 ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
883 if (list == null) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700884 list = new ArrayList<>();
Makoto Onuki7001a612016-05-27 13:24:28 -0700885 activitiesToShortcuts.put(activity, list);
886 }
887 list.add(si);
888 }
889 return activitiesToShortcuts;
890 }
891
892 /** Used by {@link #enforceShortcutCountsBeforeOperation} */
893 private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
894 ComponentName cn, int increment) {
895 Integer oldValue = counts.get(cn);
896 if (oldValue == null) {
897 oldValue = 0;
898 }
899
900 counts.put(cn, oldValue + increment);
901 }
902
903 /**
904 * Called by
905 * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
906 * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
907 * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
908 * the operation to make sure the operation wouldn't result in the target activities having
909 * more than the allowed number of dynamic/manifest shortcuts.
910 *
911 * @param newList shortcut list passed to set, add or updateShortcuts().
912 * @param operation add, set or update.
913 * @throws IllegalArgumentException if the operation would result in going over the max
914 * shortcut count for any activity.
915 */
916 public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
917 @ShortcutOperation int operation) {
918 final ShortcutService service = mShortcutUser.mService;
919
920 // Current # of dynamic / manifest shortcuts for each activity.
921 // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
922 // anyway.)
923 final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
924 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
925 final ShortcutInfo shortcut = mShortcuts.valueAt(i);
926
927 if (shortcut.isManifestShortcut()) {
928 incrementCountForActivity(counts, shortcut.getActivity(), 1);
929 } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
930 incrementCountForActivity(counts, shortcut.getActivity(), 1);
931 }
932 }
933
934 for (int i = newList.size() - 1; i >= 0; i--) {
935 final ShortcutInfo newShortcut = newList.get(i);
936 final ComponentName newActivity = newShortcut.getActivity();
937 if (newActivity == null) {
938 if (operation != ShortcutService.OPERATION_UPDATE) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700939 service.wtf("Activity must not be null at this point");
940 continue; // Just ignore this invalid case.
Makoto Onuki7001a612016-05-27 13:24:28 -0700941 }
942 continue; // Activity can be null for update.
943 }
944
945 final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
946 if (original == null) {
947 if (operation == ShortcutService.OPERATION_UPDATE) {
948 continue; // When updating, ignore if there's no target.
949 }
950 // Add() or set(), and there's no existing shortcut with the same ID. We're
951 // simply publishing (as opposed to updating) this shortcut, so just +1.
952 incrementCountForActivity(counts, newActivity, 1);
953 continue;
954 }
955 if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
956 // Updating floating shortcuts doesn't affect the count, so ignore.
957 continue;
958 }
959
960 // If it's add() or update(), then need to decrement for the previous activity.
961 // Skip it for set() since it's already been taken care of by not counting the original
962 // dynamic shortcuts in the first loop.
963 if (operation != ShortcutService.OPERATION_SET) {
964 final ComponentName oldActivity = original.getActivity();
965 if (!original.isFloating()) {
966 incrementCountForActivity(counts, oldActivity, -1);
967 }
968 }
969 incrementCountForActivity(counts, newActivity, 1);
970 }
971
972 // Then make sure none of the activities have more than the max number of shortcuts.
973 for (int i = counts.size() - 1; i >= 0; i--) {
974 service.enforceMaxActivityShortcuts(counts.valueAt(i));
975 }
976 }
977
Makoto Onuki157b1622016-06-02 16:13:10 -0700978 /**
979 * For all the text fields, refresh the string values if they're from resources.
980 */
981 public void resolveResourceStrings() {
982 final ShortcutService s = mShortcutUser.mService;
983 boolean changed = false;
984
985 Resources publisherRes = null;
986 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
987 final ShortcutInfo si = mShortcuts.valueAt(i);
988
989 if (si.hasStringResources()) {
990 changed = true;
991
992 if (publisherRes == null) {
993 publisherRes = getPackageResources();
994 if (publisherRes == null) {
995 break; // Resources couldn't be loaded.
996 }
997 }
998
999 si.resolveResourceStrings(publisherRes);
1000 si.setTimestamp(s.injectCurrentTimeMillis());
1001 }
1002 }
1003 if (changed) {
1004 s.scheduleSaveUser(getPackageUserId());
1005 }
1006 }
1007
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001008 /** Clears the implicit ranks for all shortcuts. */
1009 public void clearAllImplicitRanks() {
1010 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1011 final ShortcutInfo si = mShortcuts.valueAt(i);
1012 si.clearImplicitRankAndRankChangedFlag();
1013 }
1014 }
1015
1016 /**
1017 * Used to sort shortcuts for rank auto-adjusting.
1018 */
1019 final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
1020 // First, sort by rank.
1021 int ret = Integer.compare(a.getRank(), b.getRank());
1022 if (ret != 0) {
1023 return ret;
1024 }
1025 // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
1026 // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
1027 // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
1028 // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
1029 // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
1030 if (a.isRankChanged() != b.isRankChanged()) {
1031 return a.isRankChanged() ? -1 : 1;
1032 }
1033 // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
1034 // they're passed to the API.
1035 ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
1036 if (ret != 0) {
1037 return ret;
1038 }
1039 // If they're stil tie, just sort by their IDs.
1040 // This may happen with updateShortcuts() -- see
1041 // the testUpdateShortcuts_noManifestShortcuts() test.
1042 return a.getId().compareTo(b.getId());
1043 };
1044
1045 /**
1046 * Re-calculate the ranks for all shortcuts.
1047 */
1048 public void adjustRanks() {
1049 final ShortcutService s = mShortcutUser.mService;
1050 final long now = s.injectCurrentTimeMillis();
1051
1052 // First, clear ranks for floating shortcuts.
1053 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1054 final ShortcutInfo si = mShortcuts.valueAt(i);
1055 if (si.isFloating()) {
1056 if (si.getRank() != 0) {
1057 si.setTimestamp(now);
1058 si.setRank(0);
1059 }
1060 }
1061 }
1062
1063 // Then adjust ranks. Ranks are unique for each activity, so we first need to sort
1064 // shortcuts to each activity.
1065 // Then sort the shortcuts within each activity with mShortcutRankComparator, and
1066 // assign ranks from 0.
1067 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1068 sortShortcutsToActivities();
1069 for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
1070 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1071
1072 // Sort by ranks and other signals.
1073 Collections.sort(list, mShortcutRankComparator);
1074
1075 int rank = 0;
1076
1077 final int size = list.size();
1078 for (int i = 0; i < size; i++) {
1079 final ShortcutInfo si = list.get(i);
1080 if (si.isManifestShortcut()) {
1081 // Don't adjust ranks for manifest shortcuts.
1082 continue;
1083 }
1084 // At this point, it must be dynamic.
1085 if (!si.isDynamic()) {
1086 s.wtf("Non-dynamic shortcut found.");
1087 continue;
1088 }
1089 final int thisRank = rank++;
1090 if (si.getRank() != thisRank) {
1091 si.setTimestamp(now);
1092 si.setRank(thisRank);
1093 }
1094 }
1095 }
1096 }
1097
Makoto Onukic51b2872016-05-04 15:24:50 -07001098 public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
Makoto Onuki31459242016-03-22 11:12:18 -07001099 pw.println();
1100
1101 pw.print(prefix);
1102 pw.print("Package: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001103 pw.print(getPackageName());
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001104 pw.print(" UID: ");
1105 pw.print(mPackageUid);
Makoto Onuki31459242016-03-22 11:12:18 -07001106 pw.println();
1107
1108 pw.print(prefix);
1109 pw.print(" ");
1110 pw.print("Calls: ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001111 pw.print(getApiCallCount());
Makoto Onuki31459242016-03-22 11:12:18 -07001112 pw.println();
1113
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001114 // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
1115 pw.print(prefix);
1116 pw.print(" ");
1117 pw.print("Last known FG: ");
1118 pw.print(mLastKnownForegroundElapsedTime);
1119 pw.println();
1120
Makoto Onuki31459242016-03-22 11:12:18 -07001121 // This should be after getApiCallCount(), which may update it.
1122 pw.print(prefix);
1123 pw.print(" ");
1124 pw.print("Last reset: [");
1125 pw.print(mLastResetTime);
1126 pw.print("] ");
Makoto Onukic51b2872016-05-04 15:24:50 -07001127 pw.print(ShortcutService.formatTime(mLastResetTime));
Makoto Onuki31459242016-03-22 11:12:18 -07001128 pw.println();
1129
Makoto Onukic51b2872016-05-04 15:24:50 -07001130 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001131 pw.println();
1132
Makoto Onuki39686e82016-04-13 18:03:00 -07001133 pw.print(prefix);
1134 pw.println(" Shortcuts:");
Makoto Onuki31459242016-03-22 11:12:18 -07001135 long totalBitmapSize = 0;
1136 final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
1137 final int size = shortcuts.size();
1138 for (int i = 0; i < size; i++) {
1139 final ShortcutInfo si = shortcuts.valueAt(i);
Makoto Onuki39686e82016-04-13 18:03:00 -07001140 pw.print(prefix);
1141 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001142 pw.println(si.toInsecureString());
1143 if (si.getBitmapPath() != null) {
1144 final long len = new File(si.getBitmapPath()).length();
Makoto Onuki39686e82016-04-13 18:03:00 -07001145 pw.print(prefix);
1146 pw.print(" ");
Makoto Onuki31459242016-03-22 11:12:18 -07001147 pw.print("bitmap size=");
1148 pw.println(len);
1149
1150 totalBitmapSize += len;
1151 }
1152 }
1153 pw.print(prefix);
1154 pw.print(" ");
1155 pw.print("Total bitmap size: ");
1156 pw.print(totalBitmapSize);
1157 pw.print(" (");
Makoto Onukic51b2872016-05-04 15:24:50 -07001158 pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
Makoto Onuki31459242016-03-22 11:12:18 -07001159 pw.println(")");
1160 }
1161
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001162 @Override
Makoto Onuki0acbb142016-03-22 17:02:57 -07001163 public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
1164 throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001165 final int size = mShortcuts.size();
1166
1167 if (size == 0 && mApiCallCount == 0) {
1168 return; // nothing to write.
1169 }
1170
1171 out.startTag(null, TAG_ROOT);
1172
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001173 ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
Makoto Onuki31459242016-03-22 11:12:18 -07001174 ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
1175 ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001176 getPackageInfo().saveToXml(out);
Makoto Onuki31459242016-03-22 11:12:18 -07001177
1178 for (int j = 0; j < size; j++) {
Makoto Onuki0acbb142016-03-22 17:02:57 -07001179 saveShortcut(out, mShortcuts.valueAt(j), forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -07001180 }
1181
1182 out.endTag(null, TAG_ROOT);
1183 }
1184
Makoto Onuki0acbb142016-03-22 17:02:57 -07001185 private static void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001186 throws IOException, XmlPullParserException {
Makoto Onuki0acbb142016-03-22 17:02:57 -07001187 if (forBackup) {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001188 if (!(si.isPinned() && si.isEnabled())) {
1189 return; // We only backup pinned shortcuts that are enabled.
Makoto Onuki0acbb142016-03-22 17:02:57 -07001190 }
1191 }
Makoto Onuki31459242016-03-22 11:12:18 -07001192 out.startTag(null, TAG_SHORTCUT);
1193 ShortcutService.writeAttr(out, ATTR_ID, si.getId());
1194 // writeAttr(out, "package", si.getPackageName()); // not needed
Makoto Onuki22fcc682016-05-17 14:52:19 -07001195 ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
Makoto Onuki31459242016-03-22 11:12:18 -07001196 // writeAttr(out, "icon", si.getIcon()); // We don't save it.
1197 ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001198 ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001199 ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001200 ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001201 ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001202 ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
Makoto Onuki20c95f82016-05-11 16:51:01 -07001203 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
Makoto Onukieddbfec2016-05-31 17:04:34 -07001204 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
1205 si.getDisabledMessageResourceId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001206 ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
1207 si.getDisabledMessageResName());
Makoto Onuki31459242016-03-22 11:12:18 -07001208 ShortcutService.writeAttr(out, ATTR_INTENT, si.getIntentNoExtras());
Makoto Onuki31459242016-03-22 11:12:18 -07001209 ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
1210 si.getLastChangedTimestamp());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001211 if (forBackup) {
1212 // Don't write icon information. Also drop the dynamic flag.
1213 ShortcutService.writeAttr(out, ATTR_FLAGS,
1214 si.getFlags() &
1215 ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
1216 | ShortcutInfo.FLAG_DYNAMIC));
1217 } else {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001218 // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
1219 // as dynamic.
1220 ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
1221
Makoto Onuki0acbb142016-03-22 17:02:57 -07001222 ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
Makoto Onuki157b1622016-06-02 16:13:10 -07001223 ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
1224 ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001225 ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
1226 }
Makoto Onuki31459242016-03-22 11:12:18 -07001227
Makoto Onukib6d35232016-04-04 15:57:17 -07001228 {
Makoto Onukibe73a802016-04-15 14:46:35 -07001229 final Set<String> cat = si.getCategories();
Makoto Onukib6d35232016-04-04 15:57:17 -07001230 if (cat != null && cat.size() > 0) {
1231 out.startTag(null, TAG_CATEGORIES);
1232 XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
1233 NAME_CATEGORIES, out);
1234 out.endTag(null, TAG_CATEGORIES);
1235 }
1236 }
1237
Makoto Onuki31459242016-03-22 11:12:18 -07001238 ShortcutService.writeTagExtra(out, TAG_INTENT_EXTRAS,
1239 si.getIntentPersistableExtras());
1240 ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
1241
1242 out.endTag(null, TAG_SHORTCUT);
1243 }
1244
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001245 public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
1246 XmlPullParser parser, boolean fromBackup)
Makoto Onuki31459242016-03-22 11:12:18 -07001247 throws IOException, XmlPullParserException {
1248
1249 final String packageName = ShortcutService.parseStringAttribute(parser,
1250 ATTR_NAME);
1251
Makoto Onukic51b2872016-05-04 15:24:50 -07001252 final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001253 shortcutUser.getUserId(), packageName);
Makoto Onuki31459242016-03-22 11:12:18 -07001254
Makoto Onuki31459242016-03-22 11:12:18 -07001255 ret.mApiCallCount =
1256 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
1257 ret.mLastResetTime =
1258 ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
1259
1260 final int outerDepth = parser.getDepth();
1261 int type;
1262 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1263 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1264 if (type != XmlPullParser.START_TAG) {
1265 continue;
1266 }
1267 final int depth = parser.getDepth();
1268 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001269 if (depth == outerDepth + 1) {
1270 switch (tag) {
1271 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -07001272 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001273 continue;
1274 case TAG_SHORTCUT:
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07001275 final ShortcutInfo si = parseShortcut(parser, packageName,
1276 shortcutUser.getUserId());
Makoto Onuki31459242016-03-22 11:12:18 -07001277
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001278 // Don't use addShortcut(), we don't need to save the icon.
1279 ret.mShortcuts.put(si.getId(), si);
1280 continue;
1281 }
Makoto Onuki31459242016-03-22 11:12:18 -07001282 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001283 ShortcutService.warnForInvalidTag(depth, tag);
1284 }
Makoto Onuki31459242016-03-22 11:12:18 -07001285 return ret;
1286 }
1287
Makoto Onukiabe84422016-04-07 09:41:19 -07001288 private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
1289 @UserIdInt int userId) throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -07001290 String id;
1291 ComponentName activityComponent;
1292 // Icon icon;
1293 String title;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001294 int titleResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001295 String titleResName;
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001296 String text;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001297 int textResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001298 String textResName;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001299 String disabledMessage;
1300 int disabledMessageResId;
Makoto Onuki157b1622016-06-02 16:13:10 -07001301 String disabledMessageResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001302 Intent intent;
1303 PersistableBundle intentPersistableExtras = null;
Makoto Onuki20c95f82016-05-11 16:51:01 -07001304 int rank;
Makoto Onuki31459242016-03-22 11:12:18 -07001305 PersistableBundle extras = null;
1306 long lastChangedTimestamp;
1307 int flags;
Makoto Onuki157b1622016-06-02 16:13:10 -07001308 int iconResId;
1309 String iconResName;
Makoto Onuki31459242016-03-22 11:12:18 -07001310 String bitmapPath;
Makoto Onukibe73a802016-04-15 14:46:35 -07001311 ArraySet<String> categories = null;
Makoto Onuki31459242016-03-22 11:12:18 -07001312
1313 id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
1314 activityComponent = ShortcutService.parseComponentNameAttribute(parser,
1315 ATTR_ACTIVITY);
1316 title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001317 titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001318 titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
Makoto Onukie3ae7ec2016-03-29 15:45:25 -07001319 text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001320 textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001321 textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001322 disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
1323 disabledMessageResId = ShortcutService.parseIntAttribute(parser,
1324 ATTR_DISABLED_MESSAGE_RES_ID);
Makoto Onuki157b1622016-06-02 16:13:10 -07001325 disabledMessageResName = ShortcutService.parseStringAttribute(parser,
1326 ATTR_DISABLED_MESSAGE_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001327 intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT);
Makoto Onuki20c95f82016-05-11 16:51:01 -07001328 rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07001329 lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
Makoto Onuki31459242016-03-22 11:12:18 -07001330 flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
Makoto Onuki157b1622016-06-02 16:13:10 -07001331 iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
1332 iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
Makoto Onuki31459242016-03-22 11:12:18 -07001333 bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
1334
1335 final int outerDepth = parser.getDepth();
1336 int type;
1337 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1338 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1339 if (type != XmlPullParser.START_TAG) {
1340 continue;
1341 }
1342 final int depth = parser.getDepth();
1343 final String tag = parser.getName();
1344 if (ShortcutService.DEBUG_LOAD) {
1345 Slog.d(TAG, String.format(" depth=%d type=%d name=%s",
1346 depth, type, tag));
1347 }
1348 switch (tag) {
1349 case TAG_INTENT_EXTRAS:
1350 intentPersistableExtras = PersistableBundle.restoreFromXml(parser);
1351 continue;
1352 case TAG_EXTRAS:
1353 extras = PersistableBundle.restoreFromXml(parser);
1354 continue;
Makoto Onukib6d35232016-04-04 15:57:17 -07001355 case TAG_CATEGORIES:
1356 // This just contains string-array.
1357 continue;
1358 case TAG_STRING_ARRAY_XMLUTILS:
1359 if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
1360 ATTR_NAME_XMLUTILS))) {
Makoto Onukibe73a802016-04-15 14:46:35 -07001361 final String[] ar = XmlUtils.readThisStringArrayXml(
1362 parser, TAG_STRING_ARRAY_XMLUTILS, null);
1363 categories = new ArraySet<>(ar.length);
1364 for (int i = 0; i < ar.length; i++) {
1365 categories.add(ar[i]);
1366 }
Makoto Onukib6d35232016-04-04 15:57:17 -07001367 }
1368 continue;
Makoto Onuki31459242016-03-22 11:12:18 -07001369 }
1370 throw ShortcutService.throwForInvalidTag(depth, tag);
1371 }
Makoto Onukibe73a802016-04-15 14:46:35 -07001372
Makoto Onuki31459242016-03-22 11:12:18 -07001373 return new ShortcutInfo(
Makoto Onuki20c95f82016-05-11 16:51:01 -07001374 userId, id, packageName, activityComponent, /* icon =*/ null,
Makoto Onuki157b1622016-06-02 16:13:10 -07001375 title, titleResId, titleResName, text, textResId, textResName,
1376 disabledMessage, disabledMessageResId, disabledMessageResName,
Makoto Onukibe73a802016-04-15 14:46:35 -07001377 categories, intent,
Makoto Onuki20c95f82016-05-11 16:51:01 -07001378 intentPersistableExtras, rank, extras, lastChangedTimestamp, flags,
Makoto Onuki157b1622016-06-02 16:13:10 -07001379 iconResId, iconResName, bitmapPath);
Makoto Onuki31459242016-03-22 11:12:18 -07001380 }
Makoto Onuki2e210c42016-03-30 08:30:36 -07001381
1382 @VisibleForTesting
1383 List<ShortcutInfo> getAllShortcutsForTest() {
1384 return new ArrayList<>(mShortcuts.values());
1385 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001386
1387 @Override
1388 public void verifyStates() {
1389 super.verifyStates();
1390
1391 boolean failed = false;
1392
1393 final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
1394 sortShortcutsToActivities();
1395
1396 // Make sure each activity won't have more than max shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001397 for (int outer = all.size() - 1; outer >= 0; outer--) {
1398 final ArrayList<ShortcutInfo> list = all.valueAt(outer);
1399 if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001400 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001401 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
1402 + " has " + all.valueAt(outer).size() + " shortcuts.");
Makoto Onuki7001a612016-05-27 13:24:28 -07001403 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001404
1405 // Sort by rank.
1406 Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
1407
1408 // Split into two arrays for each kind.
1409 final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
1410 dynamicList.removeIf((si) -> !si.isDynamic());
1411
1412 final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
1413 dynamicList.removeIf((si) -> !si.isManifestShortcut());
1414
1415 verifyRanksSequential(dynamicList);
1416 verifyRanksSequential(manifestList);
Makoto Onuki7001a612016-05-27 13:24:28 -07001417 }
1418
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001419 // Verify each shortcut's status.
Makoto Onuki7001a612016-05-27 13:24:28 -07001420 for (int i = mShortcuts.size() - 1; i >= 0; i--) {
1421 final ShortcutInfo si = mShortcuts.valueAt(i);
Makoto Onukiff14f732016-06-30 17:07:25 -07001422 if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
Makoto Onuki7001a612016-05-27 13:24:28 -07001423 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001424 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001425 + " is not manifest, dynamic or pinned.");
1426 }
Makoto Onukiff14f732016-06-30 17:07:25 -07001427 if (si.isDeclaredInManifest() && si.isDynamic()) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001428 failed = true;
1429 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1430 + " is both dynamic and manifest at the same time.");
1431 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001432 if (si.getActivity() == null) {
1433 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001434 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001435 + " has null activity.");
1436 }
1437 if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
1438 failed = true;
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001439 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
Makoto Onuki7001a612016-05-27 13:24:28 -07001440 + " is not floating, but is disabled.");
1441 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001442 if (si.isFloating() && si.getRank() != 0) {
1443 failed = true;
1444 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1445 + " is floating, but has rank=" + si.getRank());
1446 }
Makoto Onukidd097812016-06-29 13:10:09 -07001447 if (si.getIcon() != null) {
1448 failed = true;
1449 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1450 + " still has an icon");
1451 }
1452 if (si.hasIconFile() && si.hasIconResource()) {
1453 failed = true;
1454 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1455 + " has both resource and bitmap icons");
1456 }
Makoto Onuki7001a612016-05-27 13:24:28 -07001457 }
1458
1459 if (failed) {
1460 throw new IllegalStateException("See logcat for errors");
1461 }
1462 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001463
1464 private boolean verifyRanksSequential(List<ShortcutInfo> list) {
1465 boolean failed = false;
1466
1467 for (int i = 0; i < list.size(); i++) {
1468 final ShortcutInfo si = list.get(i);
1469 if (si.getRank() != i) {
1470 failed = true;
1471 Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
1472 + " rank=" + si.getRank() + " but expected to be "+ i);
1473 }
1474 }
1475 return failed;
1476 }
Makoto Onuki31459242016-03-22 11:12:18 -07001477}