ShortcutPackage.java revision 6c1dbd577bcf2b8bccb9a0d04d741ff7337898f2
1/*
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;
20import android.annotation.UserIdInt;
21import android.content.ComponentName;
22import android.content.Intent;
23import android.content.pm.ShortcutInfo;
24import android.os.PersistableBundle;
25import android.text.format.Formatter;
26import android.util.ArrayMap;
27import android.util.ArraySet;
28import android.util.Slog;
29
30import com.android.internal.annotations.VisibleForTesting;
31import com.android.internal.util.XmlUtils;
32
33import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35import org.xmlpull.v1.XmlSerializer;
36
37import java.io.File;
38import java.io.IOException;
39import java.io.PrintWriter;
40import java.util.ArrayList;
41import java.util.List;
42import java.util.Set;
43import java.util.function.Predicate;
44
45/**
46 * Package information used by {@link ShortcutService}.
47 */
48class ShortcutPackage extends ShortcutPackageItem {
49    private static final String TAG = ShortcutService.TAG;
50
51    static final String TAG_ROOT = "package";
52    private static final String TAG_INTENT_EXTRAS = "intent-extras";
53    private static final String TAG_EXTRAS = "extras";
54    private static final String TAG_SHORTCUT = "shortcut";
55    private static final String TAG_CATEGORIES = "categories";
56
57    private static final String ATTR_NAME = "name";
58    private static final String ATTR_DYNAMIC_COUNT = "dynamic-count";
59    private static final String ATTR_CALL_COUNT = "call-count";
60    private static final String ATTR_LAST_RESET = "last-reset";
61    private static final String ATTR_ID = "id";
62    private static final String ATTR_ACTIVITY = "activity";
63    private static final String ATTR_TITLE = "title";
64    private static final String ATTR_TEXT = "text";
65    private static final String ATTR_INTENT = "intent";
66    private static final String ATTR_WEIGHT = "weight";
67    private static final String ATTR_TIMESTAMP = "timestamp";
68    private static final String ATTR_FLAGS = "flags";
69    private static final String ATTR_ICON_RES = "icon-res";
70    private static final String ATTR_BITMAP_PATH = "bitmap-path";
71
72    private static final String NAME_CATEGORIES = "categories";
73
74    private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
75    private static final String ATTR_NAME_XMLUTILS = "name";
76
77    /**
78     * All the shortcuts from the package, keyed on IDs.
79     */
80    final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
81
82    /**
83     * # of dynamic shortcuts.
84     */
85    private int mDynamicShortcutCount = 0;
86
87    /**
88     * # of times the package has called rate-limited APIs.
89     */
90    private int mApiCallCount;
91
92    /**
93     * When {@link #mApiCallCount} was reset last time.
94     */
95    private long mLastResetTime;
96
97    private final int mPackageUid;
98
99    private long mLastKnownForegroundElapsedTime;
100
101    private ShortcutPackage(ShortcutService s, ShortcutUser shortcutUser,
102            int packageUserId, String packageName, ShortcutPackageInfo spi) {
103        super(shortcutUser, packageUserId, packageName,
104                spi != null ? spi : ShortcutPackageInfo.newEmpty());
105
106        mPackageUid = s.injectGetPackageUid(packageName, packageUserId);
107    }
108
109    public ShortcutPackage(ShortcutService s, ShortcutUser shortcutUser,
110            int packageUserId, String packageName) {
111        this(s, shortcutUser, packageUserId, packageName, null);
112    }
113
114    @Override
115    public int getOwnerUserId() {
116        // For packages, always owner user == package user.
117        return getPackageUserId();
118    }
119
120    public int getPackageUid() {
121        return mPackageUid;
122    }
123
124    /**
125     * Called when a shortcut is about to be published.  At this point we know the publisher package
126     * exists (as opposed to Launcher trying to fetch shortcuts from a non-existent package), so
127     * we do some initialization for the package.
128     */
129    private void onShortcutPublish(ShortcutService s) {
130        // Make sure we have the version code for the app.  We need the version code in
131        // handlePackageUpdated().
132        if (getPackageInfo().getVersionCode() < 0) {
133            final int versionCode = s.getApplicationVersionCode(getPackageName(), getOwnerUserId());
134            if (ShortcutService.DEBUG) {
135                Slog.d(TAG, String.format("Package %s version = %d", getPackageName(),
136                        versionCode));
137            }
138            if (versionCode >= 0) {
139                getPackageInfo().setVersionCode(versionCode);
140                s.scheduleSaveUser(getOwnerUserId());
141            }
142        }
143    }
144
145    @Override
146    protected void onRestoreBlocked(ShortcutService s) {
147        // Can't restore due to version/signature mismatch.  Remove all shortcuts.
148        mShortcuts.clear();
149    }
150
151    @Override
152    protected void onRestored(ShortcutService s) {
153        // Because some launchers may not have been restored (e.g. allowBackup=false),
154        // we need to re-calculate the pinned shortcuts.
155        refreshPinnedFlags(s);
156    }
157
158    /**
159     * Note this does *not* provide a correct view to the calling launcher.
160     */
161    @Nullable
162    public ShortcutInfo findShortcutById(String id) {
163        return mShortcuts.get(id);
164    }
165
166    private ShortcutInfo deleteShortcut(@NonNull ShortcutService s,
167            @NonNull String id) {
168        final ShortcutInfo shortcut = mShortcuts.remove(id);
169        if (shortcut != null) {
170            s.removeIcon(getPackageUserId(), shortcut);
171            shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED);
172        }
173        return shortcut;
174    }
175
176    void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) {
177        deleteShortcut(s, newShortcut.getId());
178        s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut);
179        mShortcuts.put(newShortcut.getId(), newShortcut);
180    }
181
182    /**
183     * Add a shortcut, or update one with the same ID, with taking over existing flags.
184     *
185     * It checks the max number of dynamic shortcuts.
186     */
187    public void addDynamicShortcut(@NonNull ShortcutService s,
188            @NonNull ShortcutInfo newShortcut) {
189
190        onShortcutPublish(s);
191
192        newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
193
194        final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
195
196        final boolean wasPinned;
197        final int newDynamicCount;
198
199        if (oldShortcut == null) {
200            wasPinned = false;
201            newDynamicCount = mDynamicShortcutCount + 1; // adding a dynamic shortcut.
202        } else {
203            wasPinned = oldShortcut.isPinned();
204            if (oldShortcut.isDynamic()) {
205                newDynamicCount = mDynamicShortcutCount; // not adding a dynamic shortcut.
206            } else {
207                newDynamicCount = mDynamicShortcutCount + 1; // adding a dynamic shortcut.
208            }
209        }
210
211        // Make sure there's still room.
212        s.enforceMaxDynamicShortcuts(newDynamicCount);
213
214        // Okay, make it dynamic and add.
215        if (wasPinned) {
216            newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
217        }
218
219        addShortcut(s, newShortcut);
220        mDynamicShortcutCount = newDynamicCount;
221    }
222
223    /**
224     * Remove all shortcuts that aren't pinned nor dynamic.
225     */
226    private void removeOrphans(@NonNull ShortcutService s) {
227        ArrayList<String> removeList = null; // Lazily initialize.
228
229        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
230            final ShortcutInfo si = mShortcuts.valueAt(i);
231
232            if (si.isPinned() || si.isDynamic()) continue;
233
234            if (removeList == null) {
235                removeList = new ArrayList<>();
236            }
237            removeList.add(si.getId());
238        }
239        if (removeList != null) {
240            for (int i = removeList.size() - 1; i >= 0; i--) {
241                deleteShortcut(s, removeList.get(i));
242            }
243        }
244    }
245
246    /**
247     * Remove all dynamic shortcuts.
248     */
249    public void deleteAllDynamicShortcuts(@NonNull ShortcutService s) {
250        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
251            mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_DYNAMIC);
252        }
253        removeOrphans(s);
254        mDynamicShortcutCount = 0;
255    }
256
257    /**
258     * Remove a dynamic shortcut by ID.
259     */
260    public void deleteDynamicWithId(@NonNull ShortcutService s, @NonNull String shortcutId) {
261        final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
262
263        if (oldShortcut == null) {
264            return;
265        }
266        if (oldShortcut.isDynamic()) {
267            mDynamicShortcutCount--;
268        }
269        if (oldShortcut.isPinned()) {
270            oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
271        } else {
272            deleteShortcut(s, shortcutId);
273        }
274    }
275
276    /**
277     * Called after a launcher updates the pinned set.  For each shortcut in this package,
278     * set FLAG_PINNED if any launcher has pinned it.  Otherwise, clear it.
279     *
280     * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
281     */
282    public void refreshPinnedFlags(@NonNull ShortcutService s) {
283        // First, un-pin all shortcuts
284        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
285            mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
286        }
287
288        // Then, for the pinned set for each launcher, set the pin flag one by one.
289        s.getUserShortcutsLocked(getPackageUserId()).forAllLaunchers(launcherShortcuts -> {
290            final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
291                    getPackageName(), getPackageUserId());
292
293            if (pinned == null || pinned.size() == 0) {
294                return;
295            }
296            for (int i = pinned.size() - 1; i >= 0; i--) {
297                final String id = pinned.valueAt(i);
298                final ShortcutInfo si = mShortcuts.get(id);
299                if (si == null) {
300                    // This happens if a launcher pinned shortcuts from this package, then backup&
301                    // restored, but this package doesn't allow backing up.
302                    // In that case the launcher ends up having a dangling pinned shortcuts.
303                    // That's fine, when the launcher is restored, we'll fix it.
304                    continue;
305                }
306                si.addFlags(ShortcutInfo.FLAG_PINNED);
307            }
308        });
309
310        // Lastly, remove the ones that are no longer pinned nor dynamic.
311        removeOrphans(s);
312    }
313
314    /**
315     * Number of calls that the caller has made, since the last reset.
316     *
317     * <p>This takes care of the resetting the counter for foreground apps as well as after
318     * locale changes.
319     */
320    public int getApiCallCount(@NonNull ShortcutService s) {
321        mShortcutUser.resetThrottlingIfNeeded(s);
322
323        // Reset the counter if:
324        // - the package is in foreground now.
325        // - the package is *not* in foreground now, but was in foreground at some point
326        // since the previous time it had been.
327        if (s.isUidForegroundLocked(mPackageUid)
328                || mLastKnownForegroundElapsedTime
329                    < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) {
330            mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
331            resetRateLimiting(s);
332        }
333
334        // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
335        // but we just can't return 0 at this point, because we may have to update
336        // mLastResetTime.
337
338        final long last = s.getLastResetTimeLocked();
339
340        final long now = s.injectCurrentTimeMillis();
341        if (ShortcutService.isClockValid(now) && mLastResetTime > now) {
342            Slog.w(TAG, "Clock rewound");
343            // Clock rewound.
344            mLastResetTime = now;
345            mApiCallCount = 0;
346            return mApiCallCount;
347        }
348
349        // If not reset yet, then reset.
350        if (mLastResetTime < last) {
351            if (ShortcutService.DEBUG) {
352                Slog.d(TAG, String.format("My last reset=%d, now=%d, last=%d: resetting",
353                        mLastResetTime, now, last));
354            }
355            mApiCallCount = 0;
356            mLastResetTime = last;
357        }
358        return mApiCallCount;
359    }
360
361    /**
362     * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount}
363     * and return true.  Otherwise just return false.
364     *
365     * <p>This takes care of the resetting the counter for foreground apps as well as after
366     * locale changes, which is done internally by {@link #getApiCallCount}.
367     */
368    public boolean tryApiCall(@NonNull ShortcutService s) {
369        if (getApiCallCount(s) >= s.mMaxUpdatesPerInterval) {
370            return false;
371        }
372        mApiCallCount++;
373        s.scheduleSaveUser(getOwnerUserId());
374        return true;
375    }
376
377    public void resetRateLimiting(@NonNull ShortcutService s) {
378        if (ShortcutService.DEBUG) {
379            Slog.d(TAG, "resetRateLimiting: " + getPackageName());
380        }
381        if (mApiCallCount > 0) {
382            mApiCallCount = 0;
383            s.scheduleSaveUser(getOwnerUserId());
384        }
385    }
386
387    public void resetRateLimitingForCommandLineNoSaving() {
388        mApiCallCount = 0;
389        mLastResetTime = 0;
390    }
391
392    /**
393     * Find all shortcuts that match {@code query}.
394     */
395    public void findAll(@NonNull ShortcutService s, @NonNull List<ShortcutInfo> result,
396            @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
397        findAll(s, result, query, cloneFlag, null, 0);
398    }
399
400    /**
401     * Find all shortcuts that match {@code query}.
402     *
403     * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned
404     * by the calling launcher will not be included in the result, and also "isPinned" will be
405     * adjusted for the caller too.
406     */
407    public void findAll(@NonNull ShortcutService s, @NonNull List<ShortcutInfo> result,
408            @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
409            @Nullable String callingLauncher, int launcherUserId) {
410        if (getPackageInfo().isShadow()) {
411            // Restored and the app not installed yet, so don't return any.
412            return;
413        }
414
415        // Set of pinned shortcuts by the calling launcher.
416        final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
417                : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
418                    .getPinnedShortcutIds(getPackageName(), getPackageUserId());
419
420        for (int i = 0; i < mShortcuts.size(); i++) {
421            final ShortcutInfo si = mShortcuts.valueAt(i);
422
423            // If it's called by non-launcher (i.e. publisher, always include -> true.
424            // Otherwise, only include non-dynamic pinned one, if the calling launcher has pinned
425            // it.
426            final boolean isPinnedByCaller = (callingLauncher == null)
427                    || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
428            if (!si.isDynamic()) {
429                if (!si.isPinned()) {
430                    s.wtf("Shortcut not pinned: package " + getPackageName()
431                            + ", user=" + getPackageUserId() + ", id=" + si.getId());
432                    continue;
433                }
434                if (!isPinnedByCaller) {
435                    continue;
436                }
437            }
438            final ShortcutInfo clone = si.clone(cloneFlag);
439            // Fix up isPinned for the caller.  Note we need to do it before the "test" callback,
440            // since it may check isPinned.
441            if (!isPinnedByCaller) {
442                clone.clearFlags(ShortcutInfo.FLAG_PINNED);
443            }
444            if (query == null || query.test(clone)) {
445                result.add(clone);
446            }
447        }
448    }
449
450    public void resetThrottling() {
451        mApiCallCount = 0;
452    }
453
454    /**
455     * Return the filenames (excluding path names) of icon bitmap files from this package.
456     */
457    public ArraySet<String> getUsedBitmapFiles() {
458        final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
459
460        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
461            final ShortcutInfo si = mShortcuts.valueAt(i);
462            if (si.getBitmapPath() != null) {
463                usedFiles.add(getFileName(si.getBitmapPath()));
464            }
465        }
466        return usedFiles;
467    }
468
469    private static String getFileName(@NonNull String path) {
470        final int sep = path.lastIndexOf(File.separatorChar);
471        if (sep == -1) {
472            return path;
473        } else {
474            return path.substring(sep + 1);
475        }
476    }
477
478    /**
479     * Called when the package is updated.  If there are shortcuts with resource icons, update
480     * their timestamps.
481     */
482    public void handlePackageUpdated(ShortcutService s, int newVersionCode) {
483        if (getPackageInfo().getVersionCode() >= newVersionCode) {
484            // Version hasn't changed; nothing to do.
485            return;
486        }
487        if (ShortcutService.DEBUG) {
488            Slog.d(TAG, String.format("Package %s updated, version %d -> %d", getPackageName(),
489                    getPackageInfo().getVersionCode(), newVersionCode));
490        }
491
492        getPackageInfo().setVersionCode(newVersionCode);
493
494        boolean changed = false;
495        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
496            final ShortcutInfo si = mShortcuts.valueAt(i);
497
498            if (si.hasIconResource()) {
499                changed = true;
500                si.setTimestamp(s.injectCurrentTimeMillis());
501            }
502        }
503        if (changed) {
504            // This will send a notification to the launcher, and also save .
505            s.packageShortcutsChanged(getPackageName(), getPackageUserId());
506        } else {
507            // Still save the version code.
508            s.scheduleSaveUser(getPackageUserId());
509        }
510    }
511
512    public void dump(@NonNull ShortcutService s, @NonNull PrintWriter pw, @NonNull String prefix) {
513        pw.println();
514
515        pw.print(prefix);
516        pw.print("Package: ");
517        pw.print(getPackageName());
518        pw.print("  UID: ");
519        pw.print(mPackageUid);
520        pw.println();
521
522        pw.print(prefix);
523        pw.print("  ");
524        pw.print("Calls: ");
525        pw.print(getApiCallCount(s));
526        pw.println();
527
528        // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
529        pw.print(prefix);
530        pw.print("  ");
531        pw.print("Last known FG: ");
532        pw.print(mLastKnownForegroundElapsedTime);
533        pw.println();
534
535        // This should be after getApiCallCount(), which may update it.
536        pw.print(prefix);
537        pw.print("  ");
538        pw.print("Last reset: [");
539        pw.print(mLastResetTime);
540        pw.print("] ");
541        pw.print(s.formatTime(mLastResetTime));
542        pw.println();
543
544        getPackageInfo().dump(s, pw, prefix + "  ");
545        pw.println();
546
547        pw.print(prefix);
548        pw.println("  Shortcuts:");
549        long totalBitmapSize = 0;
550        final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
551        final int size = shortcuts.size();
552        for (int i = 0; i < size; i++) {
553            final ShortcutInfo si = shortcuts.valueAt(i);
554            pw.print(prefix);
555            pw.print("    ");
556            pw.println(si.toInsecureString());
557            if (si.getBitmapPath() != null) {
558                final long len = new File(si.getBitmapPath()).length();
559                pw.print(prefix);
560                pw.print("      ");
561                pw.print("bitmap size=");
562                pw.println(len);
563
564                totalBitmapSize += len;
565            }
566        }
567        pw.print(prefix);
568        pw.print("  ");
569        pw.print("Total bitmap size: ");
570        pw.print(totalBitmapSize);
571        pw.print(" (");
572        pw.print(Formatter.formatFileSize(s.mContext, totalBitmapSize));
573        pw.println(")");
574    }
575
576    @Override
577    public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
578            throws IOException, XmlPullParserException {
579        final int size = mShortcuts.size();
580
581        if (size == 0 && mApiCallCount == 0) {
582            return; // nothing to write.
583        }
584
585        out.startTag(null, TAG_ROOT);
586
587        ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
588        ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount);
589        ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
590        ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
591        getPackageInfo().saveToXml(out);
592
593        for (int j = 0; j < size; j++) {
594            saveShortcut(out, mShortcuts.valueAt(j), forBackup);
595        }
596
597        out.endTag(null, TAG_ROOT);
598    }
599
600    private static void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup)
601            throws IOException, XmlPullParserException {
602        if (forBackup) {
603            if (!si.isPinned()) {
604                return; // Backup only pinned icons.
605            }
606        }
607        out.startTag(null, TAG_SHORTCUT);
608        ShortcutService.writeAttr(out, ATTR_ID, si.getId());
609        // writeAttr(out, "package", si.getPackageName()); // not needed
610        ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivityComponent());
611        // writeAttr(out, "icon", si.getIcon());  // We don't save it.
612        ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
613        ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
614        ShortcutService.writeAttr(out, ATTR_INTENT, si.getIntentNoExtras());
615        ShortcutService.writeAttr(out, ATTR_WEIGHT, si.getWeight());
616        ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
617                si.getLastChangedTimestamp());
618        if (forBackup) {
619            // Don't write icon information.  Also drop the dynamic flag.
620            ShortcutService.writeAttr(out, ATTR_FLAGS,
621                    si.getFlags() &
622                            ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
623                            | ShortcutInfo.FLAG_DYNAMIC));
624        } else {
625            ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
626            ShortcutService.writeAttr(out, ATTR_ICON_RES, si.getIconResourceId());
627            ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
628        }
629
630        {
631            final Set<String> cat = si.getCategories();
632            if (cat != null && cat.size() > 0) {
633                out.startTag(null, TAG_CATEGORIES);
634                XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]),
635                        NAME_CATEGORIES, out);
636                out.endTag(null, TAG_CATEGORIES);
637            }
638        }
639
640        ShortcutService.writeTagExtra(out, TAG_INTENT_EXTRAS,
641                si.getIntentPersistableExtras());
642        ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
643
644        out.endTag(null, TAG_SHORTCUT);
645    }
646
647    public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser,
648            XmlPullParser parser, boolean fromBackup)
649            throws IOException, XmlPullParserException {
650
651        final String packageName = ShortcutService.parseStringAttribute(parser,
652                ATTR_NAME);
653
654        final ShortcutPackage ret = new ShortcutPackage(s, shortcutUser,
655                shortcutUser.getUserId(), packageName);
656
657        ret.mDynamicShortcutCount =
658                ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT);
659        ret.mApiCallCount =
660                ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
661        ret.mLastResetTime =
662                ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET);
663
664        final int outerDepth = parser.getDepth();
665        int type;
666        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
667                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
668            if (type != XmlPullParser.START_TAG) {
669                continue;
670            }
671            final int depth = parser.getDepth();
672            final String tag = parser.getName();
673            if (depth == outerDepth + 1) {
674                switch (tag) {
675                    case ShortcutPackageInfo.TAG_ROOT:
676                        ret.getPackageInfo().loadFromXml(parser, fromBackup);
677                        continue;
678                    case TAG_SHORTCUT:
679                        final ShortcutInfo si = parseShortcut(parser, packageName,
680                                shortcutUser.getUserId());
681
682                        // Don't use addShortcut(), we don't need to save the icon.
683                        ret.mShortcuts.put(si.getId(), si);
684                        continue;
685                }
686            }
687            ShortcutService.warnForInvalidTag(depth, tag);
688        }
689        return ret;
690    }
691
692    private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName,
693            @UserIdInt int userId) throws IOException, XmlPullParserException {
694        String id;
695        ComponentName activityComponent;
696        // Icon icon;
697        String title;
698        String text;
699        Intent intent;
700        PersistableBundle intentPersistableExtras = null;
701        int weight;
702        PersistableBundle extras = null;
703        long lastChangedTimestamp;
704        int flags;
705        int iconRes;
706        String bitmapPath;
707        ArraySet<String> categories = null;
708
709        id = ShortcutService.parseStringAttribute(parser, ATTR_ID);
710        activityComponent = ShortcutService.parseComponentNameAttribute(parser,
711                ATTR_ACTIVITY);
712        title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
713        text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
714        intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT);
715        weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT);
716        lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
717        flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
718        iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES);
719        bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
720
721        final int outerDepth = parser.getDepth();
722        int type;
723        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
724                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
725            if (type != XmlPullParser.START_TAG) {
726                continue;
727            }
728            final int depth = parser.getDepth();
729            final String tag = parser.getName();
730            if (ShortcutService.DEBUG_LOAD) {
731                Slog.d(TAG, String.format("  depth=%d type=%d name=%s",
732                        depth, type, tag));
733            }
734            switch (tag) {
735                case TAG_INTENT_EXTRAS:
736                    intentPersistableExtras = PersistableBundle.restoreFromXml(parser);
737                    continue;
738                case TAG_EXTRAS:
739                    extras = PersistableBundle.restoreFromXml(parser);
740                    continue;
741                case TAG_CATEGORIES:
742                    // This just contains string-array.
743                    continue;
744                case TAG_STRING_ARRAY_XMLUTILS:
745                    if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser,
746                            ATTR_NAME_XMLUTILS))) {
747                        final String[] ar = XmlUtils.readThisStringArrayXml(
748                                parser, TAG_STRING_ARRAY_XMLUTILS, null);
749                        categories = new ArraySet<>(ar.length);
750                        for (int i = 0; i < ar.length; i++) {
751                            categories.add(ar[i]);
752                        }
753                    }
754                    continue;
755            }
756            throw ShortcutService.throwForInvalidTag(depth, tag);
757        }
758
759        return new ShortcutInfo(
760                userId, id, packageName, activityComponent, /* icon =*/ null, title, text,
761                categories, intent,
762                intentPersistableExtras, weight, extras, lastChangedTimestamp, flags,
763                iconRes, bitmapPath);
764    }
765
766    @VisibleForTesting
767    List<ShortcutInfo> getAllShortcutsForTest() {
768        return new ArrayList<>(mShortcuts.values());
769    }
770}
771