ShortcutManagerTest1.java revision 82fb2eb057032bb7f38f547fb3667c7bf2547517
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 static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull;
20import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllChooser;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDisabled;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamic;
23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamicOrPinned;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllEnabled;
25import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveIntents;
26import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveTitle;
27import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllImmutable;
28import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllKeyFieldsOnly;
29import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllManifest;
30import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveIntents;
31import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveTitle;
32import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotKeyFieldsOnly;
33import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotManifest;
34import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllPinned;
35import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllUnique;
36import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBitmapSize;
37import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundleEmpty;
38import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackNotReceived;
39import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackReceived;
40import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCannotUpdateImmutable;
41import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicAndPinned;
42import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicOnly;
43import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded;
44import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertEmpty;
45import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
46import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertForLauncherCallback;
47import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertShortcutIds;
48import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
49import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.filterByActivity;
50import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.findShortcut;
51import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
52import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
53import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
54import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.pfdToBitmap;
55import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetAll;
56import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
57import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.waitOnMainThread;
58
59import static org.mockito.Matchers.any;
60import static org.mockito.Matchers.anyInt;
61import static org.mockito.Matchers.anyString;
62import static org.mockito.Matchers.eq;
63import static org.mockito.Mockito.doReturn;
64import static org.mockito.Mockito.mock;
65import static org.mockito.Mockito.reset;
66import static org.mockito.Mockito.times;
67import static org.mockito.Mockito.verify;
68
69import android.Manifest.permission;
70import android.app.ActivityManager;
71import android.content.ActivityNotFoundException;
72import android.content.ComponentName;
73import android.content.Intent;
74import android.content.pm.ApplicationInfo;
75import android.content.pm.LauncherApps;
76import android.content.pm.LauncherApps.ShortcutQuery;
77import android.content.pm.ShortcutInfo;
78import android.graphics.Bitmap;
79import android.graphics.Bitmap.CompressFormat;
80import android.graphics.BitmapFactory;
81import android.graphics.drawable.AdaptiveIconDrawable;
82import android.graphics.drawable.Drawable;
83import android.graphics.drawable.Icon;
84import android.net.Uri;
85import android.os.Bundle;
86import android.os.Handler;
87import android.os.Looper;
88import android.os.Process;
89import android.os.UserHandle;
90import android.test.suitebuilder.annotation.SmallTest;
91import android.util.Log;
92import android.util.SparseArray;
93
94import com.android.frameworks.servicestests.R;
95import com.android.server.pm.ShortcutService.ConfigConstants;
96import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
97import com.android.server.pm.ShortcutUser.PackageWithUser;
98
99import org.mockito.ArgumentCaptor;
100
101import java.io.File;
102import java.io.IOException;
103import java.util.List;
104import java.util.Locale;
105import java.util.function.BiConsumer;
106import java.util.function.BiPredicate;
107
108/**
109 * Tests for ShortcutService and ShortcutManager.
110 *
111 m FrameworksServicesTests &&
112 adb install \
113 -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
114 adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 \
115 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
116 */
117@SmallTest
118public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
119
120    /**
121     * Test for the first launch path, no settings file available.
122     */
123    public void testFirstInitialize() {
124        assertResetTimes(START_TIME, START_TIME + INTERVAL);
125    }
126
127    /**
128     * Test for {@link ShortcutService#getLastResetTimeLocked()} and
129     * {@link ShortcutService#getNextResetTimeLocked()}.
130     */
131    public void testUpdateAndGetNextResetTimeLocked() {
132        assertResetTimes(START_TIME, START_TIME + INTERVAL);
133
134        // Advance clock.
135        mInjectedCurrentTimeMillis += 100;
136
137        // Shouldn't have changed.
138        assertResetTimes(START_TIME, START_TIME + INTERVAL);
139
140        // Advance clock, almost the reset time.
141        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
142
143        // Shouldn't have changed.
144        assertResetTimes(START_TIME, START_TIME + INTERVAL);
145
146        // Advance clock.
147        mInjectedCurrentTimeMillis += 1;
148
149        assertResetTimes(START_TIME + INTERVAL, START_TIME + 2 * INTERVAL);
150
151        // Advance further; 4 hours since start.
152        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
153
154        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
155    }
156
157    /**
158     * Test for the restoration from saved file.
159     */
160    public void testInitializeFromSavedFile() {
161
162        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
163        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
164
165        mService.saveBaseStateLocked();
166
167        dumpBaseStateFile();
168
169        mService.saveDirtyInfo();
170
171        // Restore.
172        initService();
173
174        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
175    }
176
177    /**
178     * Test for the restoration from restored file.
179     */
180    public void testLoadFromBrokenFile() {
181        // TODO Add various broken cases.
182    }
183
184    public void testLoadConfig() {
185        mService.updateConfigurationLocked(
186                ConfigConstants.KEY_RESET_INTERVAL_SEC + "=123,"
187                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=4,"
188                        + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=5,"
189                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
190                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
191                        + ConfigConstants.KEY_ICON_FORMAT + "=WEBP,"
192                        + ConfigConstants.KEY_ICON_QUALITY + "=75");
193        assertEquals(123000, mService.getResetIntervalForTest());
194        assertEquals(4, mService.getMaxShortcutsForTest());
195        assertEquals(5, mService.getMaxUpdatesPerIntervalForTest());
196        assertEquals(100, mService.getMaxIconDimensionForTest());
197        assertEquals(CompressFormat.WEBP, mService.getIconPersistFormatForTest());
198        assertEquals(75, mService.getIconPersistQualityForTest());
199
200        mInjectedIsLowRamDevice = true;
201        mService.updateConfigurationLocked(
202                ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
203                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
204                        + ConfigConstants.KEY_ICON_FORMAT + "=JPEG");
205        assertEquals(ShortcutService.DEFAULT_RESET_INTERVAL_SEC * 1000,
206                mService.getResetIntervalForTest());
207
208        assertEquals(ShortcutService.DEFAULT_MAX_SHORTCUTS_PER_APP,
209                mService.getMaxShortcutsForTest());
210
211        assertEquals(ShortcutService.DEFAULT_MAX_UPDATES_PER_INTERVAL,
212                mService.getMaxUpdatesPerIntervalForTest());
213
214        assertEquals(50, mService.getMaxIconDimensionForTest());
215
216        assertEquals(CompressFormat.JPEG, mService.getIconPersistFormatForTest());
217
218        assertEquals(ShortcutService.DEFAULT_ICON_PERSIST_QUALITY,
219                mService.getIconPersistQualityForTest());
220    }
221
222    // === Test for app side APIs ===
223
224    /** Test for {@link android.content.pm.ShortcutManager#getMaxShortcutCountForActivity()} */
225    public void testGetMaxDynamicShortcutCount() {
226        assertEquals(MAX_SHORTCUTS, mManager.getMaxShortcutCountForActivity());
227    }
228
229    /** Test for {@link android.content.pm.ShortcutManager#getRemainingCallCount()} */
230    public void testGetRemainingCallCount() {
231        assertEquals(MAX_UPDATES_PER_INTERVAL, mManager.getRemainingCallCount());
232    }
233
234    public void testGetIconMaxDimensions() {
235        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxWidth());
236        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxHeight());
237    }
238
239    /** Test for {@link android.content.pm.ShortcutManager#getRateLimitResetTime()} */
240    public void testGetRateLimitResetTime() {
241        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
242
243        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
244
245        assertEquals(START_TIME + 5 * INTERVAL, mManager.getRateLimitResetTime());
246    }
247
248    public void testSetDynamicShortcuts() {
249        setCaller(CALLING_PACKAGE_1, USER_0);
250
251        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.icon1);
252        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
253                getTestContext().getResources(), R.drawable.icon2));
254        final Icon icon3 = Icon.createWithAdaptiveBitmap(BitmapFactory.decodeResource(
255            getTestContext().getResources(), R.drawable.icon2));
256
257        final ShortcutInfo si1 = makeShortcut(
258                "shortcut1",
259                "Title 1",
260                makeComponent(ShortcutActivity.class),
261                icon1,
262                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
263                        "key1", "val1", "nest", makeBundle("key", 123)),
264                /* weight */ 10,
265                /* chooserFilter=*/ null,
266                /* chooserComponentNames=*/ null);
267
268        final ShortcutInfo si2 = makeShortcut(
269                "shortcut2",
270                "Title 2",
271                /* activity */ null,
272                icon2,
273                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
274                /* weight */ 12,
275                /* chooserFilter=*/ null,
276                /* chooserComponentNames=*/ null);
277        final ShortcutInfo si3 = makeShortcut(
278                "shortcut3",
279                "Title 3",
280                /* activity */ null,
281                icon3,
282                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
283                /* weight */ 13,
284                /* chooserFilter=*/ null,
285                /* chooserComponentNames=*/ null);
286
287        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
288        assertShortcutIds(assertAllNotKeyFieldsOnly(
289                mManager.getDynamicShortcuts()),
290                "shortcut1", "shortcut2", "shortcut3");
291        assertEquals(2, mManager.getRemainingCallCount());
292
293        // TODO: Check fields
294
295        assertTrue(mManager.setDynamicShortcuts(list(si1)));
296        assertShortcutIds(assertAllNotKeyFieldsOnly(
297                mManager.getDynamicShortcuts()),
298                "shortcut1");
299        assertEquals(1, mManager.getRemainingCallCount());
300
301        assertTrue(mManager.setDynamicShortcuts(list()));
302        assertEquals(0, mManager.getDynamicShortcuts().size());
303        assertEquals(0, mManager.getRemainingCallCount());
304
305        dumpsysOnLogcat();
306
307        mInjectedCurrentTimeMillis++; // Need to advance the clock for reset to work.
308        mService.resetThrottlingInner(UserHandle.USER_SYSTEM);
309
310        dumpsysOnLogcat();
311
312        assertTrue(mManager.setDynamicShortcuts(list(si2, si3)));
313        assertEquals(2, mManager.getDynamicShortcuts().size());
314
315        // TODO Check max number
316
317        mRunningUsers.put(USER_10, true);
318
319        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
320            assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
321        });
322    }
323
324    public void testAddDynamicShortcuts() {
325        setCaller(CALLING_PACKAGE_1, USER_0);
326
327        final ShortcutInfo si1 = makeShortcut("shortcut1");
328        final ShortcutInfo si2 = makeShortcut("shortcut2");
329        final ShortcutInfo si3 = makeShortcut("shortcut3");
330
331        assertEquals(3, mManager.getRemainingCallCount());
332
333        assertTrue(mManager.setDynamicShortcuts(list(si1)));
334        assertEquals(2, mManager.getRemainingCallCount());
335        assertShortcutIds(assertAllNotKeyFieldsOnly(
336                mManager.getDynamicShortcuts()),
337                "shortcut1");
338
339        assertTrue(mManager.addDynamicShortcuts(list(si2, si3)));
340        assertEquals(1, mManager.getRemainingCallCount());
341        assertShortcutIds(assertAllNotKeyFieldsOnly(
342                mManager.getDynamicShortcuts()),
343                "shortcut1", "shortcut2", "shortcut3");
344
345        // This should not crash.  It'll still consume the quota.
346        assertTrue(mManager.addDynamicShortcuts(list()));
347        assertEquals(0, mManager.getRemainingCallCount());
348        assertShortcutIds(assertAllNotKeyFieldsOnly(
349                mManager.getDynamicShortcuts()),
350                "shortcut1", "shortcut2", "shortcut3");
351
352        mInjectedCurrentTimeMillis += INTERVAL; // reset
353
354        // Add with the same ID
355        assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("shortcut1"))));
356        assertEquals(2, mManager.getRemainingCallCount());
357        assertShortcutIds(assertAllNotKeyFieldsOnly(
358                mManager.getDynamicShortcuts()),
359                "shortcut1", "shortcut2", "shortcut3");
360
361        // TODO Check max number
362
363        // TODO Check fields.
364
365        mRunningUsers.put(USER_10, true);
366
367        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
368            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
369        });
370    }
371
372    public void testPublishWithNoActivity() {
373        // If activity is not explicitly set, use the default one.
374
375        mRunningUsers.put(USER_10, true);
376
377        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
378            // s1 and s3 has no activities.
379            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
380                    .setShortLabel("label1")
381                    .setIntent(new Intent("action1"))
382                    .build();
383            final ShortcutInfo si2 = new ShortcutInfo.Builder(mClientContext, "si2")
384                    .setShortLabel("label2")
385                    .setActivity(new ComponentName(getCallingPackage(), "abc"))
386                    .setIntent(new Intent("action2"))
387                    .build();
388            final ShortcutInfo si3 = new ShortcutInfo.Builder(mClientContext, "si3")
389                    .setShortLabel("label3")
390                    .setIntent(new Intent("action3"))
391                    .build();
392
393            // Set test 1
394            assertTrue(mManager.setDynamicShortcuts(list(si1)));
395
396            assertWith(getCallerShortcuts())
397                    .haveIds("si1")
398                    .forShortcutWithId("si1", si -> {
399                        assertEquals(new ComponentName(getCallingPackage(),
400                                MAIN_ACTIVITY_CLASS), si.getActivity());
401                    });
402
403            // Set test 2
404            assertTrue(mManager.setDynamicShortcuts(list(si2, si1)));
405
406            assertWith(getCallerShortcuts())
407                    .haveIds("si1", "si2")
408                    .forShortcutWithId("si1", si -> {
409                        assertEquals(new ComponentName(getCallingPackage(),
410                                MAIN_ACTIVITY_CLASS), si.getActivity());
411                    })
412                    .forShortcutWithId("si2", si -> {
413                        assertEquals(new ComponentName(getCallingPackage(),
414                                "abc"), si.getActivity());
415                    });
416
417
418            // Set test 3
419            assertTrue(mManager.setDynamicShortcuts(list(si3, si1)));
420
421            assertWith(getCallerShortcuts())
422                    .haveIds("si1", "si3")
423                    .forShortcutWithId("si1", si -> {
424                        assertEquals(new ComponentName(getCallingPackage(),
425                                MAIN_ACTIVITY_CLASS), si.getActivity());
426                    })
427                    .forShortcutWithId("si3", si -> {
428                        assertEquals(new ComponentName(getCallingPackage(),
429                                MAIN_ACTIVITY_CLASS), si.getActivity());
430                    });
431
432            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
433
434            // Add test 1
435            mManager.removeAllDynamicShortcuts();
436            assertTrue(mManager.addDynamicShortcuts(list(si1)));
437
438            assertWith(getCallerShortcuts())
439                    .haveIds("si1")
440                    .forShortcutWithId("si1", si -> {
441                        assertEquals(new ComponentName(getCallingPackage(),
442                                MAIN_ACTIVITY_CLASS), si.getActivity());
443                    });
444
445            // Add test 2
446            mManager.removeAllDynamicShortcuts();
447            assertTrue(mManager.addDynamicShortcuts(list(si2, si1)));
448
449            assertWith(getCallerShortcuts())
450                    .haveIds("si1", "si2")
451                    .forShortcutWithId("si1", si -> {
452                        assertEquals(new ComponentName(getCallingPackage(),
453                                MAIN_ACTIVITY_CLASS), si.getActivity());
454                    })
455                    .forShortcutWithId("si2", si -> {
456                        assertEquals(new ComponentName(getCallingPackage(),
457                                "abc"), si.getActivity());
458                    });
459
460
461            // Add test 3
462            mManager.removeAllDynamicShortcuts();
463            assertTrue(mManager.addDynamicShortcuts(list(si3, si1)));
464
465            assertWith(getCallerShortcuts())
466                    .haveIds("si1", "si3")
467                    .forShortcutWithId("si1", si -> {
468                        assertEquals(new ComponentName(getCallingPackage(),
469                                MAIN_ACTIVITY_CLASS), si.getActivity());
470                    })
471                    .forShortcutWithId("si3", si -> {
472                        assertEquals(new ComponentName(getCallingPackage(),
473                                MAIN_ACTIVITY_CLASS), si.getActivity());
474                    });
475        });
476    }
477
478    public void testPublishWithNoActivity_noMainActivityInPackage() {
479        mRunningUsers.put(USER_10, true);
480
481        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
482            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
483                    .setShortLabel("label1")
484                    .setIntent(new Intent("action1"))
485                    .build();
486
487            // Returning null means there's no main activity in this package.
488            mMainActivityFetcher = (packageName, userId) -> null;
489
490            assertExpectException(
491                    RuntimeException.class, "Launcher activity not found for", () -> {
492                        assertTrue(mManager.setDynamicShortcuts(list(si1)));
493                    });
494        });
495    }
496
497    public void testDeleteDynamicShortcuts() {
498        final ShortcutInfo si1 = makeShortcut("shortcut1");
499        final ShortcutInfo si2 = makeShortcut("shortcut2");
500        final ShortcutInfo si3 = makeShortcut("shortcut3");
501        final ShortcutInfo si4 = makeShortcut("shortcut4");
502
503        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3, si4)));
504        assertShortcutIds(assertAllNotKeyFieldsOnly(
505                mManager.getDynamicShortcuts()),
506                "shortcut1", "shortcut2", "shortcut3", "shortcut4");
507
508        assertEquals(2, mManager.getRemainingCallCount());
509
510        mManager.removeDynamicShortcuts(list("shortcut1"));
511        assertShortcutIds(assertAllNotKeyFieldsOnly(
512                mManager.getDynamicShortcuts()),
513                "shortcut2", "shortcut3", "shortcut4");
514
515        mManager.removeDynamicShortcuts(list("shortcut1"));
516        assertShortcutIds(assertAllNotKeyFieldsOnly(
517                mManager.getDynamicShortcuts()),
518                "shortcut2", "shortcut3", "shortcut4");
519
520        mManager.removeDynamicShortcuts(list("shortcutXXX"));
521        assertShortcutIds(assertAllNotKeyFieldsOnly(
522                mManager.getDynamicShortcuts()),
523                "shortcut2", "shortcut3", "shortcut4");
524
525        mManager.removeDynamicShortcuts(list("shortcut2", "shortcut4"));
526        assertShortcutIds(assertAllNotKeyFieldsOnly(
527                mManager.getDynamicShortcuts()),
528                "shortcut3");
529
530        mManager.removeDynamicShortcuts(list("shortcut3"));
531        assertShortcutIds(assertAllNotKeyFieldsOnly(
532                mManager.getDynamicShortcuts()));
533
534        // Still 2 calls left.
535        assertEquals(2, mManager.getRemainingCallCount());
536    }
537
538    public void testDeleteAllDynamicShortcuts() {
539        final ShortcutInfo si1 = makeShortcut("shortcut1");
540        final ShortcutInfo si2 = makeShortcut("shortcut2");
541        final ShortcutInfo si3 = makeShortcut("shortcut3");
542
543        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
544        assertShortcutIds(assertAllNotKeyFieldsOnly(
545                mManager.getDynamicShortcuts()),
546                "shortcut1", "shortcut2", "shortcut3");
547
548        assertEquals(2, mManager.getRemainingCallCount());
549
550        mManager.removeAllDynamicShortcuts();
551        assertEquals(0, mManager.getDynamicShortcuts().size());
552        assertEquals(2, mManager.getRemainingCallCount());
553
554        // Note delete shouldn't affect throttling, so...
555        assertEquals(0, mManager.getDynamicShortcuts().size());
556        assertEquals(0, mManager.getDynamicShortcuts().size());
557        assertEquals(0, mManager.getDynamicShortcuts().size());
558
559        // This should still work.
560        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
561        assertEquals(3, mManager.getDynamicShortcuts().size());
562
563        // Still 1 call left
564        assertEquals(1, mManager.getRemainingCallCount());
565    }
566
567    public void testIcons() throws IOException {
568        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
569        final Icon res64x64 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
570        final Icon res512x512 = Icon.createWithResource(getTestContext(), R.drawable.black_512x512);
571
572        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
573                getTestContext().getResources(), R.drawable.black_32x32));
574        final Icon bmp64x64_maskable = Icon.createWithAdaptiveBitmap(BitmapFactory.decodeResource(
575                getTestContext().getResources(), R.drawable.black_64x64));
576        final Icon bmp512x512 = Icon.createWithBitmap(BitmapFactory.decodeResource(
577                getTestContext().getResources(), R.drawable.black_512x512));
578
579        // Set from package 1
580        setCaller(CALLING_PACKAGE_1);
581        assertTrue(mManager.setDynamicShortcuts(list(
582                makeShortcutWithIcon("res32x32", res32x32),
583                makeShortcutWithIcon("res64x64", res64x64),
584                makeShortcutWithIcon("bmp32x32", bmp32x32),
585                makeShortcutWithIcon("bmp64x64", bmp64x64_maskable),
586                makeShortcutWithIcon("bmp512x512", bmp512x512),
587                makeShortcut("none")
588        )));
589
590        // getDynamicShortcuts() shouldn't return icons, thus assertAllNotHaveIcon().
591        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
592                "res32x32",
593                "res64x64",
594                "bmp32x32",
595                "bmp64x64",
596                "bmp512x512",
597                "none");
598
599        // Call from another caller with the same ID, just to make sure storage is per-package.
600        setCaller(CALLING_PACKAGE_2);
601        assertTrue(mManager.setDynamicShortcuts(list(
602                makeShortcutWithIcon("res32x32", res512x512),
603                makeShortcutWithIcon("res64x64", res512x512),
604                makeShortcutWithIcon("none", res512x512)
605        )));
606        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
607                "res32x32",
608                "res64x64",
609                "none");
610
611        // Different profile.  Note the names and the contents don't match.
612        setCaller(CALLING_PACKAGE_1, USER_P0);
613        assertTrue(mManager.setDynamicShortcuts(list(
614                makeShortcutWithIcon("res32x32", res512x512),
615                makeShortcutWithIcon("bmp32x32", bmp512x512)
616        )));
617        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
618                "res32x32",
619                "bmp32x32");
620
621        // Re-initialize and load from the files.
622        mService.saveDirtyInfo();
623        initService();
624
625        // Load from launcher.
626        Bitmap bmp;
627
628        setCaller(LAUNCHER_1);
629        // Check hasIconResource()/hasIconFile().
630        assertShortcutIds(assertAllHaveIconResId(
631                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0))),
632                "res32x32");
633
634        assertShortcutIds(assertAllHaveIconResId(
635                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0))),
636                "res64x64");
637
638        assertShortcutIds(assertAllHaveIconFile(
639                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0))),
640                "bmp32x32");
641
642        assertShortcutIds(assertAllHaveIconFile(
643                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0))),
644                "bmp64x64");
645
646        assertShortcutIds(assertAllHaveIconFile(
647                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0))),
648                "bmp512x512");
649
650        assertShortcutIds(assertAllHaveIconResId(
651                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0))),
652                "res32x32");
653        assertShortcutIds(assertAllHaveIconFile(
654                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0))),
655                "bmp32x32");
656
657        // Check
658        assertEquals(
659                R.drawable.black_32x32,
660                mLauncherApps.getShortcutIconResId(
661                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0)));
662
663        assertEquals(
664                R.drawable.black_64x64,
665                mLauncherApps.getShortcutIconResId(
666                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0)));
667
668        assertEquals(
669                0, // because it's not a resource
670                mLauncherApps.getShortcutIconResId(
671                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
672        assertEquals(
673                0, // because it's not a resource
674                mLauncherApps.getShortcutIconResId(
675                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
676        assertEquals(
677                0, // because it's not a resource
678                mLauncherApps.getShortcutIconResId(
679                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
680
681        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
682                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
683        assertBitmapSize(32, 32, bmp);
684
685        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
686                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
687        assertBitmapSize(64, 64, bmp);
688
689        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
690                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
691        assertBitmapSize(128, 128, bmp);
692
693        assertEquals(
694                R.drawable.black_512x512,
695                mLauncherApps.getShortcutIconResId(
696                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0)));
697        // Should be 512x512, so shrunk.
698        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
699                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0)));
700        assertBitmapSize(128, 128, bmp);
701
702        // Also check the overload APIs too.
703        assertEquals(
704                R.drawable.black_32x32,
705                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_0));
706        assertEquals(
707                R.drawable.black_64x64,
708                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res64x64", HANDLE_USER_0));
709        assertEquals(
710                R.drawable.black_512x512,
711                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_P0));
712        bmp = pfdToBitmap(
713                mLauncherApps.getShortcutIconFd(CALLING_PACKAGE_1, "bmp32x32", HANDLE_USER_P0));
714        assertBitmapSize(128, 128, bmp);
715
716        Drawable dr = mLauncherApps.getShortcutIconDrawable(
717            makeShortcutWithIcon("bmp64x64", bmp64x64_maskable), 0);
718        assertTrue(dr instanceof AdaptiveIconDrawable);
719        float viewportPercentage = 1 / (1 + 2 * AdaptiveIconDrawable.getExtraInsetPercentage());
720        assertEquals((int) (bmp64x64_maskable.getBitmap().getWidth() * viewportPercentage),
721            dr.getIntrinsicWidth());
722        assertEquals((int) (bmp64x64_maskable.getBitmap().getHeight() * viewportPercentage),
723            dr.getIntrinsicHeight());
724    }
725
726    public void testCleanupDanglingBitmaps() throws Exception {
727        assertBitmapDirectories(USER_0, EMPTY_STRINGS);
728        assertBitmapDirectories(USER_10, EMPTY_STRINGS);
729
730        // Make some shortcuts with bitmap icons.
731        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
732                getTestContext().getResources(), R.drawable.black_32x32));
733
734        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
735            mManager.setDynamicShortcuts(list(
736                    makeShortcutWithIcon("s1", bmp32x32),
737                    makeShortcutWithIcon("s2", bmp32x32),
738                    makeShortcutWithIcon("s3", bmp32x32)
739            ));
740        });
741
742        // Increment the time (which actually we don't have to), which is used for filenames.
743        mInjectedCurrentTimeMillis++;
744
745        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
746            mManager.setDynamicShortcuts(list(
747                    makeShortcutWithIcon("s4", bmp32x32),
748                    makeShortcutWithIcon("s5", bmp32x32),
749                    makeShortcutWithIcon("s6", bmp32x32)
750            ));
751        });
752
753        // Increment the time, which is used for filenames.
754        mInjectedCurrentTimeMillis++;
755
756        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
757            mManager.setDynamicShortcuts(list(
758            ));
759        });
760
761        // For USER-10, let's try without updating the times.
762        mRunningUsers.put(USER_10, true);
763
764        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
765            mManager.setDynamicShortcuts(list(
766                    makeShortcutWithIcon("10s1", bmp32x32),
767                    makeShortcutWithIcon("10s2", bmp32x32),
768                    makeShortcutWithIcon("10s3", bmp32x32)
769            ));
770        });
771        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
772            mManager.setDynamicShortcuts(list(
773                    makeShortcutWithIcon("10s4", bmp32x32),
774                    makeShortcutWithIcon("10s5", bmp32x32),
775                    makeShortcutWithIcon("10s6", bmp32x32)
776            ));
777        });
778        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
779            mManager.setDynamicShortcuts(list(
780            ));
781        });
782
783        dumpsysOnLogcat();
784
785        // Check files and directories.
786        // Package 3 has no bitmaps, so we don't create a directory.
787        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
788        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
789
790        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
791                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
792                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
793                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
794        );
795        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
796                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
797                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
798                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
799        );
800        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
801                EMPTY_STRINGS
802        );
803        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
804                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
805                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
806                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
807        );
808        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
809                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
810                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
811                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
812        );
813        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
814                EMPTY_STRINGS
815        );
816
817        // Then create random directories and files.
818        makeFile(mService.getUserBitmapFilePath(USER_0), "a.b.c").mkdir();
819        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f").mkdir();
820        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "123").createNewFile();
821        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "456").createNewFile();
822
823        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_3).mkdir();
824
825        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "1").createNewFile();
826        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "2").createNewFile();
827        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "3").createNewFile();
828        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "4").createNewFile();
829
830        makeFile(mService.getUserBitmapFilePath(USER_10), "10a.b.c").mkdir();
831        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f").mkdir();
832        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "123").createNewFile();
833        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "456").createNewFile();
834
835        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "1").createNewFile();
836        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "2").createNewFile();
837        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "3").createNewFile();
838        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "4").createNewFile();
839
840        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
841                "a.b.c", "d.e.f");
842
843        // Save and load.  When a user is loaded, we do the cleanup.
844        mService.saveDirtyInfo();
845        initService();
846
847        mService.handleUnlockUser(USER_0);
848        mService.handleUnlockUser(USER_10);
849        mService.handleUnlockUser(20); // Make sure the logic will still work for nonexistent user.
850
851        // The below check is the same as above, except this time USER_0 use the CALLING_PACKAGE_3
852        // directory.
853
854        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3);
855        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
856
857        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
858                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
859                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
860                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
861        );
862        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
863                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
864                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
865                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
866        );
867        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
868                EMPTY_STRINGS
869        );
870        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
871                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
872                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
873                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
874        );
875        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
876                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
877                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
878                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
879        );
880        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
881                EMPTY_STRINGS
882        );
883    }
884
885    protected void checkShrinkBitmap(int expectedWidth, int expectedHeight, int resId, int maxSize) {
886        assertBitmapSize(expectedWidth, expectedHeight,
887                ShortcutService.shrinkBitmap(BitmapFactory.decodeResource(
888                        getTestContext().getResources(), resId),
889                        maxSize));
890    }
891
892    public void testShrinkBitmap() {
893        checkShrinkBitmap(32, 32, R.drawable.black_512x512, 32);
894        checkShrinkBitmap(511, 511, R.drawable.black_512x512, 511);
895        checkShrinkBitmap(512, 512, R.drawable.black_512x512, 512);
896
897        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4096);
898        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4100);
899        checkShrinkBitmap(512, 2048, R.drawable.black_1024x4096, 2048);
900
901        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4096);
902        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4100);
903        checkShrinkBitmap(2048, 512, R.drawable.black_4096x1024, 2048);
904    }
905
906    protected File openIconFileForWriteAndGetPath(int userId, String packageName)
907            throws IOException {
908        // Shortcut IDs aren't used in the path, so just pass the same ID.
909        final FileOutputStreamWithPath out =
910                mService.openIconFileForWrite(userId, makePackageShortcut(packageName, "id"));
911        out.close();
912        return out.getFile();
913    }
914
915    public void testOpenIconFileForWrite() throws IOException {
916        mInjectedCurrentTimeMillis = 1000;
917
918        final File p10_1_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
919        final File p10_1_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
920
921        final File p10_2_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
922        final File p10_2_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
923
924        final File p11_1_1 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
925        final File p11_1_2 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
926
927        mInjectedCurrentTimeMillis++;
928
929        final File p10_1_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
930        final File p10_1_4 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
931        final File p10_1_5 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
932
933        final File p10_2_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
934        final File p11_1_3 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
935
936        // Make sure their paths are all unique
937        assertAllUnique(list(
938                p10_1_1,
939                p10_1_2,
940                p10_1_3,
941                p10_1_4,
942                p10_1_5,
943
944                p10_2_1,
945                p10_2_2,
946                p10_2_3,
947
948                p11_1_1,
949                p11_1_2,
950                p11_1_3
951        ));
952
953        // Check each set has the same parent.
954        assertEquals(p10_1_1.getParent(), p10_1_2.getParent());
955        assertEquals(p10_1_1.getParent(), p10_1_3.getParent());
956        assertEquals(p10_1_1.getParent(), p10_1_4.getParent());
957        assertEquals(p10_1_1.getParent(), p10_1_5.getParent());
958
959        assertEquals(p10_2_1.getParent(), p10_2_2.getParent());
960        assertEquals(p10_2_1.getParent(), p10_2_3.getParent());
961
962        assertEquals(p11_1_1.getParent(), p11_1_2.getParent());
963        assertEquals(p11_1_1.getParent(), p11_1_3.getParent());
964
965        // Check the parents are still unique.
966        assertAllUnique(list(
967                p10_1_1.getParent(),
968                p10_2_1.getParent(),
969                p11_1_1.getParent()
970        ));
971
972        // All files created at the same time for the same package/user, expcet for the first ones,
973        // will have "_" in the path.
974        assertFalse(p10_1_1.getName().contains("_"));
975        assertTrue(p10_1_2.getName().contains("_"));
976        assertFalse(p10_1_3.getName().contains("_"));
977        assertTrue(p10_1_4.getName().contains("_"));
978        assertTrue(p10_1_5.getName().contains("_"));
979
980        assertFalse(p10_2_1.getName().contains("_"));
981        assertTrue(p10_2_2.getName().contains("_"));
982        assertFalse(p10_2_3.getName().contains("_"));
983
984        assertFalse(p11_1_1.getName().contains("_"));
985        assertTrue(p11_1_2.getName().contains("_"));
986        assertFalse(p11_1_3.getName().contains("_"));
987    }
988
989    public void testUpdateShortcuts() {
990        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
991            assertTrue(mManager.setDynamicShortcuts(list(
992                    makeShortcut("s1"),
993                    makeShortcut("s2"),
994                    makeShortcut("s3"),
995                    makeShortcut("s4"),
996                    makeShortcut("s5"),
997                    makeChooserShortcut("s6", 2, true),
998                    makeChooserShortcut("s7", 2, true),
999                    makeChooserShortcut("s8", 1, true))));
1000        });
1001        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1002            assertTrue(mManager.setDynamicShortcuts(list(
1003                    makeShortcut("s1"),
1004                    makeShortcut("s2"),
1005                    makeShortcut("s3"),
1006                    makeShortcut("s4"),
1007                    makeShortcut("s5"),
1008                    makeChooserShortcut("s6", 2, true),
1009                    makeChooserShortcut("s7", 2, true),
1010                    makeChooserShortcut("s8", 1, true))));
1011        });
1012        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
1013            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s3", "s6"),
1014                    getCallingUser());
1015            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s4", "s5"),
1016                    getCallingUser());
1017        });
1018        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1019            mManager.removeDynamicShortcuts(list("s1"));
1020            mManager.removeDynamicShortcuts(list("s2"));
1021        });
1022        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1023            mManager.removeDynamicShortcuts(list("s1"));
1024            mManager.removeDynamicShortcuts(list("s3"));
1025            mManager.removeDynamicShortcuts(list("s5"));
1026            mManager.removeDynamicShortcuts(list("s7"));
1027        });
1028        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1029            assertShortcutIds(assertAllDynamic(
1030                    mManager.getDynamicShortcuts()),
1031                    "s3", "s4", "s5", "s6", "s7", "s8");
1032            assertShortcutIds(assertAllPinned(
1033                    mManager.getPinnedShortcuts()),
1034                    "s2", "s3", "s6");
1035        });
1036        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1037            assertShortcutIds(assertAllDynamic(
1038                    mManager.getDynamicShortcuts()),
1039                    "s2", "s4", "s6", "s8");
1040            assertShortcutIds(assertAllPinned(
1041                    mManager.getPinnedShortcuts()),
1042                    "s4", "s5");
1043        });
1044
1045        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1046            ShortcutInfo s2 = makeShortcutBuilder()
1047                    .setId("s2")
1048                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1049                    .build();
1050
1051            ShortcutInfo s4 = makeShortcutBuilder()
1052                    .setId("s4")
1053                    .setTitle("new title")
1054                    .build();
1055
1056            mManager.updateShortcuts(list(s2, s4));
1057        });
1058        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1059            ShortcutInfo s2 = makeShortcutBuilder()
1060                    .setId("s2")
1061                    .setIntent(makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
1062                            "key1", "val1"))
1063                    .build();
1064
1065            ShortcutInfo s4 = makeShortcutBuilder()
1066                    .setId("s4")
1067                    .setIntent(new Intent(Intent.ACTION_ALL_APPS))
1068                    .build();
1069
1070            mManager.updateShortcuts(list(s2, s4));
1071        });
1072
1073        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1074            assertShortcutIds(assertAllDynamic(
1075                    mManager.getDynamicShortcuts()),
1076                    "s3", "s4", "s5", "s6", "s7", "s8");
1077            assertShortcutIds(assertAllPinned(
1078                    mManager.getPinnedShortcuts()),
1079                    "s2", "s3", "s6");
1080
1081            ShortcutInfo s = getCallerShortcut("s2");
1082            assertTrue(s.hasIconResource());
1083            assertEquals(R.drawable.black_32x32, s.getIconResourceId());
1084            assertEquals("string/r" + R.drawable.black_32x32, s.getIconResName());
1085            assertEquals("Title-s2", s.getTitle());
1086
1087            s = getCallerShortcut("s4");
1088            assertFalse(s.hasIconResource());
1089            assertEquals(0, s.getIconResourceId());
1090            assertEquals("new title", s.getTitle());
1091        });
1092        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1093            assertShortcutIds(assertAllDynamic(
1094                    mManager.getDynamicShortcuts()),
1095                    "s2", "s4", "s6", "s8");
1096            assertShortcutIds(assertAllPinned(
1097                    mManager.getPinnedShortcuts()),
1098                    "s4", "s5");
1099
1100            ShortcutInfo s = getCallerShortcut("s2");
1101            assertFalse(s.hasIconResource());
1102            assertEquals(0, s.getIconResourceId());
1103            assertEquals("Title-s2", s.getTitle());
1104            assertEquals(Intent.ACTION_ANSWER, s.getIntent().getAction());
1105            assertEquals(1, s.getIntent().getExtras().size());
1106
1107            s = getCallerShortcut("s4");
1108            assertFalse(s.hasIconResource());
1109            assertEquals(0, s.getIconResourceId());
1110            assertEquals("Title-s4", s.getTitle());
1111            assertEquals(Intent.ACTION_ALL_APPS, s.getIntent().getAction());
1112            assertBundleEmpty(s.getIntent().getExtras());
1113        });
1114        // TODO Check with other fields too.
1115
1116        // TODO Check bitmap removal too.
1117
1118        mRunningUsers.put(USER_11, true);
1119
1120        runWithCaller(CALLING_PACKAGE_2, USER_11, () -> {
1121            mManager.updateShortcuts(list());
1122        });
1123    }
1124
1125    public void testUpdateShortcuts_icons() {
1126        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1127            assertTrue(mManager.setDynamicShortcuts(list(
1128                    makeShortcut("s1")
1129            )));
1130
1131            // Set resource icon
1132            assertTrue(mManager.updateShortcuts(list(
1133                    new ShortcutInfo.Builder(mClientContext, "s1")
1134                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1135                    .build()
1136            )));
1137
1138            assertWith(getCallerShortcuts())
1139                    .forShortcutWithId("s1", si -> {
1140                        assertTrue(si.hasIconResource());
1141                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1142                    });
1143
1144            // Set bitmap icon
1145            assertTrue(mManager.updateShortcuts(list(
1146                    new ShortcutInfo.Builder(mClientContext, "s1")
1147                    .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1148                            getTestContext().getResources(), R.drawable.black_64x64)))
1149                    .build()
1150            )));
1151
1152            assertWith(getCallerShortcuts())
1153                    .forShortcutWithId("s1", si -> {
1154                        assertTrue(si.hasIconFile());
1155                    });
1156
1157            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1158
1159            // Do it again, with the reverse order (bitmap -> icon)
1160            assertTrue(mManager.setDynamicShortcuts(list(
1161                    makeShortcut("s1")
1162            )));
1163
1164            // Set bitmap icon
1165            assertTrue(mManager.updateShortcuts(list(
1166                    new ShortcutInfo.Builder(mClientContext, "s1")
1167                            .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1168                                    getTestContext().getResources(), R.drawable.black_64x64)))
1169                            .build()
1170            )));
1171
1172            assertWith(getCallerShortcuts())
1173                    .forShortcutWithId("s1", si -> {
1174                        assertTrue(si.hasIconFile());
1175                    });
1176
1177            // Set resource icon
1178            assertTrue(mManager.updateShortcuts(list(
1179                    new ShortcutInfo.Builder(mClientContext, "s1")
1180                            .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1181                            .build()
1182            )));
1183
1184            assertWith(getCallerShortcuts())
1185                    .forShortcutWithId("s1", si -> {
1186                        assertTrue(si.hasIconResource());
1187                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1188                    });
1189        });
1190    }
1191
1192    public void testUpdateShortcuts_chooser() {
1193        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1194            assertTrue(mManager.setDynamicShortcuts(list(
1195                    makeShortcut("s1"),
1196                    makeChooserShortcut("s2", 2, false),
1197                    makeChooserShortcut("s3", 2, false)
1198            )));
1199
1200            assertFalse(getCallerShortcut("s1").isChooser());
1201            assertTrue(getCallerShortcut("s2").isChooser());
1202            assertTrue(getCallerShortcut("s3").isChooser());
1203
1204            ShortcutInfo s = getCallerShortcut("s1");
1205            assertNull(s.getChooserIntentFilters());
1206            assertNull(s.getChooserComponentNames());
1207
1208            assertTrue(getCallerShortcut("s1").isDynamic());
1209            assertFalse(getCallerShortcut("s2").isDynamic());
1210            assertFalse(getCallerShortcut("s3").isDynamic());
1211
1212
1213            // Replace 2 with a chooser shortcut
1214            mManager.updateShortcuts(list(makeChooserShortcut("s1", 2, true)));
1215
1216            s = getCallerShortcut("s1");
1217            assertEquals(2, s.getChooserIntentFilters().length);
1218            assertEquals(2, s.getChooserComponentNames().length);
1219
1220            assertShortcutIds(assertAllChooser(
1221                    mManager.getDynamicShortcuts()),
1222                    "s1", "s2", "s3");
1223
1224            assertTrue(getCallerShortcut("s1").isDynamic());
1225            assertFalse(getCallerShortcut("s2").isDynamic());
1226            assertFalse(getCallerShortcut("s3").isDynamic());
1227        });
1228    }
1229
1230
1231            // === Test for launcher side APIs ===
1232
1233    public void testGetShortcuts() {
1234
1235        // Set up shortcuts.
1236
1237        setCaller(CALLING_PACKAGE_1);
1238        final ShortcutInfo s1_1 = makeShortcut("s1");
1239        final ShortcutInfo s1_2 = makeShortcut("s2");
1240
1241        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1242
1243        // Because setDynamicShortcuts will update the timestamps when ranks are changing,
1244        // we explicitly set timestamps here.
1245        getCallerShortcut("s1").setTimestamp(5000);
1246        getCallerShortcut("s2").setTimestamp(1000);
1247
1248        setCaller(CALLING_PACKAGE_2);
1249        final ShortcutInfo s2_2 = makeShortcut("s2");
1250        final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
1251                makeComponent(ShortcutActivity2.class));
1252        final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
1253                makeComponent(ShortcutActivity.class));
1254        assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1255
1256        getCallerShortcut("s2").setTimestamp(1500);
1257        getCallerShortcut("s3").setTimestamp(3000);
1258        getCallerShortcut("s4").setTimestamp(500);
1259
1260        setCaller(CALLING_PACKAGE_3);
1261        final ShortcutInfo s3_2 = makeShortcut("s3");
1262        assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1263
1264        getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
1265
1266        setCaller(LAUNCHER_1);
1267
1268        // Get dynamic
1269        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
1270                assertShortcutIds(
1271                        assertAllNotKeyFieldsOnly(
1272                                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1273                        /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1274                        "s1", "s2")))));
1275
1276        // Get pinned
1277        assertShortcutIds(
1278                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1279                        /* activity =*/ null,
1280                        ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
1281                /* none */);
1282
1283        // Get both, with timestamp
1284        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1285                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1286                        /* time =*/ 1000, CALLING_PACKAGE_2,
1287                        /* activity =*/ null,
1288                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1289                        getCallingUser())),
1290                "s2", "s3"))));
1291
1292        // FLAG_GET_KEY_FIELDS_ONLY
1293        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1294                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1295                        /* time =*/ 1000, CALLING_PACKAGE_2,
1296                        /* activity =*/ null,
1297                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1298                        getCallingUser())),
1299                "s2", "s3"))));
1300
1301        // Filter by activity
1302        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1303                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1304                        /* time =*/ 0, CALLING_PACKAGE_2,
1305                        new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
1306                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1307                        getCallingUser())),
1308                "s4"))));
1309
1310        // With ID.
1311        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1312                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1313                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"),
1314                        /* activity =*/ null,
1315                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1316                        getCallingUser())),
1317                "s3"))));
1318        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1319                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1320                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
1321                        /* activity =*/ null,
1322                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1323                        getCallingUser())),
1324                "s2", "s3"))));
1325        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1326                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1327                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
1328                        /* activity =*/ null,
1329                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1330                        getCallingUser()))
1331                /* empty */))));
1332        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1333                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1334                        /* time =*/ 1000, CALLING_PACKAGE_2, list(),
1335                        /* activity =*/ null,
1336                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1337                        getCallingUser()))
1338                /* empty */))));
1339
1340        // Pin some shortcuts.
1341        mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1342                list("s3", "s4"), getCallingUser());
1343
1344        // Pinned ones only
1345        assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1346                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1347                        /* time =*/ 1000, CALLING_PACKAGE_2,
1348                        /* activity =*/ null,
1349                        ShortcutQuery.FLAG_GET_PINNED),
1350                        getCallingUser())),
1351                "s3"))));
1352
1353        // All packages.
1354        assertShortcutIds(assertAllNotKeyFieldsOnly(
1355                mLauncherApps.getShortcuts(buildQuery(
1356                        /* time =*/ 5000, /* package= */ null,
1357                        /* activity =*/ null,
1358                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
1359                        getCallingUser())),
1360                "s1", "s3");
1361
1362        assertExpectException(
1363                IllegalArgumentException.class, "package name must also be set", () -> {
1364                    mLauncherApps.getShortcuts(buildQuery(
1365                    /* time =*/ 0, /* package= */ null, list("id"),
1366                    /* activity =*/ null, /* flags */ 0), getCallingUser());
1367                });
1368
1369        // TODO More tests: pinned but dynamic.
1370    }
1371
1372    public void testGetShortcuts_shortcutKinds() throws Exception {
1373        // Create 3 manifest and 3 dynamic shortcuts
1374        addManifestShortcutResource(
1375                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1376                R.xml.shortcut_3);
1377        updatePackageVersion(CALLING_PACKAGE_1, 1);
1378        mService.mPackageMonitor.onReceive(getTestContext(),
1379                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1380
1381        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1382            assertTrue(mManager.setDynamicShortcuts(list(
1383                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1384        });
1385
1386        // Pin 2 and 3
1387        runWithCaller(LAUNCHER_1, USER_0, () -> {
1388            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1389                    HANDLE_USER_0);
1390        });
1391
1392        // Remove ms3 and s3
1393        addManifestShortcutResource(
1394                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1395                R.xml.shortcut_2);
1396        updatePackageVersion(CALLING_PACKAGE_1, 1);
1397        mService.mPackageMonitor.onReceive(getTestContext(),
1398                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1399
1400        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1401            assertTrue(mManager.setDynamicShortcuts(list(
1402                    makeShortcut("s1"), makeShortcut("s2"))));
1403        });
1404
1405        // Check their status.
1406        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1407            assertWith(getCallerShortcuts())
1408                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
1409
1410                    .selectByIds("ms1", "ms2")
1411                    .areAllManifest()
1412                    .areAllImmutable()
1413                    .areAllNotDynamic()
1414
1415                    .revertToOriginalList()
1416                    .selectByIds("ms3")
1417                    .areAllNotManifest()
1418                    .areAllImmutable()
1419                    .areAllDisabled()
1420                    .areAllNotDynamic()
1421
1422                    .revertToOriginalList()
1423                    .selectByIds("s1", "s2")
1424                    .areAllNotManifest()
1425                    .areAllMutable()
1426                    .areAllDynamic()
1427
1428                    .revertToOriginalList()
1429                    .selectByIds("s3")
1430                    .areAllNotManifest()
1431                    .areAllMutable()
1432                    .areAllEnabled()
1433                    .areAllNotDynamic()
1434
1435                    .revertToOriginalList()
1436                    .selectByIds("s1", "ms1")
1437                    .areAllNotPinned()
1438
1439                    .revertToOriginalList()
1440                    .selectByIds("s2", "s3", "ms2", "ms3")
1441                    .areAllPinned()
1442            ;
1443        });
1444
1445        // Finally, actual tests.
1446        runWithCaller(LAUNCHER_1, USER_0, () -> {
1447            assertWith(mLauncherApps.getShortcuts(
1448                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
1449                    .haveIds("s1", "s2");
1450            assertWith(mLauncherApps.getShortcuts(
1451                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
1452                    .haveIds("ms1", "ms2");
1453            assertWith(mLauncherApps.getShortcuts(
1454                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
1455                    .haveIds("s2", "s3", "ms2", "ms3");
1456
1457            assertWith(mLauncherApps.getShortcuts(
1458                    buildQueryWithFlags(
1459                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
1460                    ), HANDLE_USER_0))
1461                    .haveIds("s1", "s2", "s3", "ms2", "ms3");
1462
1463            assertWith(mLauncherApps.getShortcuts(
1464                    buildQueryWithFlags(
1465                            ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
1466                    ), HANDLE_USER_0))
1467                    .haveIds("ms1", "s2", "s3", "ms2", "ms3");
1468
1469            assertWith(mLauncherApps.getShortcuts(
1470                    buildQueryWithFlags(
1471                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
1472                    ), HANDLE_USER_0))
1473                    .haveIds("ms1", "ms2", "s1", "s2");
1474
1475            assertWith(mLauncherApps.getShortcuts(
1476                    buildQueryWithFlags(
1477                            ShortcutQuery.FLAG_GET_ALL_KINDS
1478                    ), HANDLE_USER_0))
1479                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
1480        });
1481    }
1482
1483    public void testGetShortcuts_resolveStrings() throws Exception {
1484        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1485            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1486                    .setId("id")
1487                    .setActivity(new ComponentName(mClientContext, "dummy"))
1488                    .setTitleResId(10)
1489                    .setTextResId(11)
1490                    .setDisabledMessageResId(12)
1491                    .setIntent(makeIntent("action", ShortcutActivity.class))
1492                    .build();
1493            mManager.setDynamicShortcuts(list(si));
1494        });
1495        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1496            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1497                    .setId("id")
1498                    .setActivity(new ComponentName(mClientContext, "dummy"))
1499                    .setTitleResId(10)
1500                    .setTextResId(11)
1501                    .setDisabledMessageResId(12)
1502                    .setIntent(makeIntent("action", ShortcutActivity.class))
1503                    .build();
1504            mManager.setDynamicShortcuts(list(si));
1505        });
1506
1507        runWithCaller(LAUNCHER_1, USER_0, () -> {
1508            final ShortcutQuery q = new ShortcutQuery();
1509            q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
1510
1511            // USER 0
1512            List<ShortcutInfo> ret = assertShortcutIds(
1513                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
1514                    "id");
1515            assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
1516            assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
1517            assertEquals("string-com.android.test.1-user:0-res:12/en",
1518                    ret.get(0).getDisabledMessage());
1519
1520            // USER P0
1521            ret = assertShortcutIds(
1522                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
1523                    "id");
1524            assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
1525            assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
1526            assertEquals("string-com.android.test.1-user:20-res:12/en",
1527                    ret.get(0).getDisabledMessage());
1528        });
1529    }
1530
1531    // TODO resource
1532    public void testGetShortcutInfo() {
1533        // Create shortcuts.
1534        setCaller(CALLING_PACKAGE_1);
1535        final ShortcutInfo s1_1 = makeShortcut(
1536                "s1",
1537                "Title 1",
1538                makeComponent(ShortcutActivity.class),
1539                /* icon =*/ null,
1540                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
1541                        "key1", "val1", "nest", makeBundle("key", 123)),
1542                        /* weight */ 10, /* chooserFilter=*/ null,
1543                        /* chooserComponentNames=*/ null);
1544
1545        final ShortcutInfo s1_2 = makeShortcut(
1546                "s2", "Title 2",
1547                /* activity */ null,
1548                /* icon =*/ null,
1549                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
1550                /* weight */ 12,
1551                /* chooserFilter=*/ null,
1552                /* chooserComponentNames=*/ null);
1553
1554        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1555        dumpsysOnLogcat();
1556
1557        setCaller(CALLING_PACKAGE_2);
1558        final ShortcutInfo s2_1 = makeShortcut(
1559                "s1",
1560                "ABC",
1561                makeComponent(ShortcutActivity2.class),
1562                /* icon =*/ null,
1563                makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
1564                        "key1", "val1", "nest", makeBundle("key", 123)),
1565                /* weight */ 10,
1566                /* chooserFilter=*/ null,
1567                /* chooserComponentNames=*/ null);
1568        assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
1569        dumpsysOnLogcat();
1570
1571        // Pin some.
1572        setCaller(LAUNCHER_1);
1573
1574        mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1575                list("s2"), getCallingUser());
1576
1577        dumpsysOnLogcat();
1578
1579        // Delete some.
1580        setCaller(CALLING_PACKAGE_1);
1581        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1582        mManager.removeDynamicShortcuts(list("s2"));
1583        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1584
1585        dumpsysOnLogcat();
1586
1587        setCaller(LAUNCHER_1);
1588        List<ShortcutInfo> list;
1589
1590        // Note we don't guarantee the orders.
1591        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1592                assertAllNotKeyFieldsOnly(
1593                        mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1594                                list("s2", "s1", "s3", null), getCallingUser())))),
1595                "s1", "s2");
1596        assertEquals("Title 1", findById(list, "s1").getTitle());
1597        assertEquals("Title 2", findById(list, "s2").getTitle());
1598
1599        assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1600                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1601                        list("s3"), getCallingUser())))
1602                /* none */);
1603
1604        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1605                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
1606                        list("s1", "s2", "s3"), getCallingUser()))),
1607                "s1");
1608        assertEquals("ABC", findById(list, "s1").getTitle());
1609    }
1610
1611    public void testPinShortcutAndGetPinnedShortcuts() {
1612        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1613            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1614            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1615
1616            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1617        });
1618
1619        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1620            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1621            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1622            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1623            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1624        });
1625
1626        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1627            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1628            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1629        });
1630
1631        // Pin some.
1632        runWithCaller(LAUNCHER_1, USER_0, () -> {
1633            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1634                    list("s2", "s3"), getCallingUser());
1635
1636            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1637                    list("s3", "s4", "s5"), getCallingUser());
1638
1639            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1640                    list("s3"), getCallingUser());  // Note ID doesn't exist
1641        });
1642
1643        // Delete some.
1644        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1645            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1646            mManager.removeDynamicShortcuts(list("s2"));
1647            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1648
1649            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1650        });
1651
1652        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1653            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1654            mManager.removeDynamicShortcuts(list("s3"));
1655            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1656
1657            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1658        });
1659
1660        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1661            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1662            mManager.removeDynamicShortcuts(list("s2"));
1663            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1664
1665            assertEmpty(mManager.getDynamicShortcuts());
1666        });
1667
1668        // Get pinned shortcuts from launcher
1669        runWithCaller(LAUNCHER_1, USER_0, () -> {
1670            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1671            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1672                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1673                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1674                    "s2");
1675
1676            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1677                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1678                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1679                    "s3", "s4");
1680
1681            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1682                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1683                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1684                    /* none */);
1685        });
1686    }
1687
1688    /**
1689     * This is similar to the above test, except it used "disable" instead of "remove".  It also
1690     * does "enable".
1691     */
1692    public void testDisableAndEnableShortcuts() {
1693        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1694            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1695            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1696
1697            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1698        });
1699
1700        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1701            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1702            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1703            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1704            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1705        });
1706
1707        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1708            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1709            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1710        });
1711
1712        // Pin some.
1713        runWithCaller(LAUNCHER_1, USER_0, () -> {
1714            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1715                    list("s2", "s3"), getCallingUser());
1716
1717            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1718                    list("s3", "s4", "s5"), getCallingUser());
1719
1720            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1721                    list("s3"), getCallingUser());  // Note ID doesn't exist
1722        });
1723
1724        // Disable some.
1725        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1726            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1727
1728            mManager.disableShortcuts(list("s2"));
1729
1730            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1731            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1732        });
1733
1734        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1735            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1736
1737            // disable should work even if a shortcut is not dynamic, so try calling "remove" first
1738            // here.
1739            mManager.removeDynamicShortcuts(list("s3"));
1740            mManager.disableShortcuts(list("s3"));
1741
1742            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1743            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1744        });
1745
1746        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1747            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1748
1749            mManager.disableShortcuts(list("s2"));
1750
1751            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1752
1753            assertEmpty(mManager.getDynamicShortcuts());
1754            assertEmpty(getCallerShortcuts());
1755        });
1756
1757        // Get pinned shortcuts from launcher
1758        runWithCaller(LAUNCHER_1, USER_0, () -> {
1759            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
1760            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1761                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1762                    .haveIds("s2")
1763                    .areAllPinned()
1764                    .areAllNotWithKeyFieldsOnly()
1765                    .areAllDisabled();
1766            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
1767                    ActivityNotFoundException.class);
1768
1769            // Here, s4 is still enabled and launchable, but s3 is disabled.
1770            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1771                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1772                    .haveIds("s3", "s4")
1773                    .areAllPinned()
1774                    .areAllNotWithKeyFieldsOnly()
1775
1776                    .selectByIds("s3")
1777                    .areAllDisabled()
1778
1779                    .revertToOriginalList()
1780                    .selectByIds("s4")
1781                    .areAllEnabled();
1782
1783            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
1784                    ActivityNotFoundException.class);
1785            assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
1786
1787            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1788                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1789                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1790                    /* none */);
1791        });
1792
1793        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1794            mManager.enableShortcuts(list("s2"));
1795
1796            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1797            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1798        });
1799        runWithCaller(LAUNCHER_1, USER_0, () -> {
1800            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1801            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1802                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1803                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1804                    "s2");
1805            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
1806        });
1807    }
1808
1809    public void testDisableShortcuts_thenRepublish() {
1810        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1811            assertTrue(mManager.setDynamicShortcuts(list(
1812                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1813
1814            runWithCaller(LAUNCHER_1, USER_0, () -> {
1815                mLauncherApps.pinShortcuts(
1816                        CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
1817            });
1818
1819            mManager.disableShortcuts(list("s1", "s2", "s3"));
1820
1821            assertWith(getCallerShortcuts())
1822                    .haveIds("s1", "s2", "s3")
1823                    .areAllNotDynamic()
1824                    .areAllPinned()
1825                    .areAllDisabled();
1826
1827            // Make sure updateShortcuts() will not re-enable them.
1828            assertTrue(mManager.updateShortcuts(list(
1829                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1830
1831            assertWith(getCallerShortcuts())
1832                    .haveIds("s1", "s2", "s3")
1833                    .areAllNotDynamic()
1834                    .areAllPinned()
1835                    .areAllDisabled();
1836
1837            // Re-publish s1 with setDynamicShortcuts.
1838            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1839
1840            assertTrue(mManager.setDynamicShortcuts(list(
1841                    makeShortcut("s1"))));
1842
1843            assertWith(getCallerShortcuts())
1844                    .haveIds("s1", "s2", "s3")
1845
1846                    .selectByIds("s1")
1847                    .areAllDynamic()
1848                    .areAllPinned()
1849                    .areAllEnabled()
1850
1851                    .revertToOriginalList()
1852                    .selectByIds("s2", "s3")
1853                    .areAllNotDynamic()
1854                    .areAllPinned()
1855                    .areAllDisabled();
1856
1857            // Re-publish s2 with addDynamicShortcuts.
1858            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1859
1860            assertTrue(mManager.addDynamicShortcuts(list(
1861                    makeShortcut("s2"))));
1862
1863            assertWith(getCallerShortcuts())
1864                    .haveIds("s1", "s2", "s3")
1865
1866                    .selectByIds("s1", "s2")
1867                    .areAllDynamic()
1868                    .areAllPinned()
1869                    .areAllEnabled()
1870
1871                    .revertToOriginalList()
1872                    .selectByIds("s3")
1873                    .areAllNotDynamic()
1874                    .areAllPinned()
1875                    .areAllDisabled();
1876        });
1877    }
1878
1879    public void testPinShortcutAndGetPinnedShortcuts_multi() {
1880        // Create some shortcuts.
1881        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1882            assertTrue(mManager.setDynamicShortcuts(list(
1883                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1884        });
1885
1886        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1887            assertTrue(mManager.setDynamicShortcuts(list(
1888                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1889        });
1890
1891        dumpsysOnLogcat();
1892
1893        // Pin some.
1894        runWithCaller(LAUNCHER_1, USER_0, () -> {
1895            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1896                    list("s3", "s4"), getCallingUser());
1897
1898            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1899                    list("s1", "s2", "s4"), getCallingUser());
1900        });
1901
1902        dumpsysOnLogcat();
1903
1904        // Delete some.
1905        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1906            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1907            mManager.removeDynamicShortcuts(list("s3"));
1908            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1909        });
1910
1911        dumpsysOnLogcat();
1912
1913        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1914            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1915            mManager.removeDynamicShortcuts(list("s1"));
1916            mManager.removeDynamicShortcuts(list("s3"));
1917            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1918        });
1919
1920        dumpsysOnLogcat();
1921
1922        // Get pinned shortcuts from launcher
1923        runWithCaller(LAUNCHER_1, USER_0, () -> {
1924            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1925                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1926                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1927                    "s3");
1928
1929            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1930                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1931                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1932                    "s1", "s2");
1933
1934            assertShortcutIds(assertAllDynamicOrPinned(
1935                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1936                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1937                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1938                    "s1", "s2", "s3");
1939
1940            assertShortcutIds(assertAllDynamicOrPinned(
1941                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1942                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1943                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1944                    "s1", "s2");
1945        });
1946
1947        dumpsysOnLogcat();
1948
1949        runWithCaller(LAUNCHER_2, USER_0, () -> {
1950            // Launcher2 still has no pinned ones.
1951            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1952                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1953                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1954                    /* none */);
1955            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1956                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1957                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1958                    /* none */);
1959
1960            assertShortcutIds(assertAllDynamic(
1961                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1962                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1963                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1964                    "s1", "s2");
1965            assertShortcutIds(assertAllDynamic(
1966                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1967                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1968                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1969                    "s2");
1970
1971            // Now pin some.
1972            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1973                    list("s1", "s2"), getCallingUser());
1974
1975            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1976                    list("s1", "s2"), getCallingUser());
1977
1978            assertShortcutIds(assertAllDynamic(
1979                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1980                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1981                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1982                    "s1", "s2");
1983
1984            // S1 was not visible to it, so shouldn't be pinned.
1985            assertShortcutIds(assertAllDynamic(
1986                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1987                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1988                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1989                    "s2");
1990        });
1991
1992        // Re-initialize and load from the files.
1993        mService.saveDirtyInfo();
1994        initService();
1995
1996        // Load from file.
1997        mService.handleUnlockUser(USER_0);
1998
1999        // Make sure package info is restored too.
2000        runWithCaller(LAUNCHER_1, USER_0, () -> {
2001            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2002                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2003                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2004                    "s3");
2005            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2006                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2007                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2008                    "s1", "s2");
2009        });
2010        runWithCaller(LAUNCHER_2, USER_0, () -> {
2011            assertShortcutIds(assertAllDynamic(
2012                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2013                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2014                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2015                    "s1", "s2");
2016            assertShortcutIds(assertAllDynamic(
2017                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2018                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2019                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2020                    "s2");
2021        });
2022
2023        // Delete all dynamic.
2024        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2025            mManager.removeAllDynamicShortcuts();
2026
2027            assertEquals(0, mManager.getDynamicShortcuts().size());
2028            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2029        });
2030        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2031            mManager.removeAllDynamicShortcuts();
2032
2033            assertEquals(0, mManager.getDynamicShortcuts().size());
2034            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
2035        });
2036
2037        runWithCaller(LAUNCHER_1, USER_0, () -> {
2038            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2039                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2040                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2041                    "s3");
2042
2043            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2044                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2045                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2046                    "s1", "s2");
2047
2048            // from all packages.
2049            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2050                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
2051                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2052                    "s1", "s2", "s3");
2053
2054            // Update pined.  Note s2 and s3 are actually available, but not visible to this
2055            // launcher, so still can't be pinned.
2056            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
2057                    getCallingUser());
2058
2059            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2060                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2061                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2062                    "s3");
2063        });
2064        // Re-publish s1.
2065        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2066            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
2067
2068            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2069            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2070        });
2071        runWithCaller(LAUNCHER_1, USER_0, () -> {
2072            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2073                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2074                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2075                    "s3");
2076
2077            // Now "s1" is visible, so can be pinned.
2078            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
2079                    getCallingUser());
2080
2081            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2082                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2083                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2084                    "s1", "s3");
2085        });
2086
2087        // Now clear pinned shortcuts.  First, from launcher 1.
2088        runWithCaller(LAUNCHER_1, USER_0, () -> {
2089            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2090            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2091
2092            assertEquals(0,
2093                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2094                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2095            assertEquals(0,
2096                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2097                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2098        });
2099        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2100            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2101            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
2102        });
2103        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2104            assertEquals(0, mManager.getDynamicShortcuts().size());
2105            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
2106        });
2107
2108        // Clear all pins from launcher 2.
2109        runWithCaller(LAUNCHER_2, USER_0, () -> {
2110            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2111            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2112
2113            assertEquals(0,
2114                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2115                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2116            assertEquals(0,
2117                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2118                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2119        });
2120        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2121            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2122            assertEquals(0, mManager.getPinnedShortcuts().size());
2123        });
2124        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2125            assertEquals(0, mManager.getDynamicShortcuts().size());
2126            assertEquals(0, mManager.getPinnedShortcuts().size());
2127        });
2128    }
2129
2130    public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
2131        // Create some shortcuts.
2132        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2133            assertTrue(mManager.setDynamicShortcuts(list(
2134                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2135        });
2136        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2137            assertTrue(mManager.setDynamicShortcuts(list(
2138                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2139        });
2140
2141        mRunningUsers.put(USER_10, true);
2142
2143        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2144            assertTrue(mManager.setDynamicShortcuts(list(
2145                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2146                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2147        });
2148
2149        // Pin some shortcuts and see the result.
2150
2151        runWithCaller(LAUNCHER_1, USER_0, () -> {
2152            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2153                    list("s1"), HANDLE_USER_0);
2154
2155            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2156                    list("s1", "s2", "s3"), HANDLE_USER_0);
2157        });
2158
2159        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2160            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2161                    list("s2"), HANDLE_USER_0);
2162
2163            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2164                    list("s2", "s3"), HANDLE_USER_0);
2165        });
2166
2167        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2168            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2169                    list("s3"), HANDLE_USER_0);
2170
2171            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2172                    list("s3"), HANDLE_USER_0);
2173        });
2174
2175        runWithCaller(LAUNCHER_2, USER_10, () -> {
2176            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2177                    list("s1", "s2", "s3"), HANDLE_USER_10);
2178        });
2179
2180        // First, make sure managed profile can't see other profiles.
2181        runWithCaller(LAUNCHER_1, USER_P1, () -> {
2182
2183            final ShortcutQuery q = new ShortcutQuery().setQueryFlags(
2184                    ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_PINNED
2185                    | ShortcutQuery.FLAG_MATCH_MANIFEST);
2186
2187            // No shortcuts are visible.
2188            assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2189
2190            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2191
2192            // Should have no effects.
2193            assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2194
2195            assertShortcutNotLaunched(CALLING_PACKAGE_1, "s1", USER_0);
2196        });
2197
2198        // Cross profile pinning.
2199        final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
2200
2201        runWithCaller(LAUNCHER_1, USER_0, () -> {
2202            assertShortcutIds(assertAllPinned(
2203                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2204                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2205                    "s1");
2206            assertShortcutIds(assertAllDynamic(
2207                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2208                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2209                    "s1", "s2", "s3");
2210            assertShortcutIds(assertAllDynamicOrPinned(
2211                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2212                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2213                    "s1", "s2", "s3");
2214
2215            assertShortcutIds(assertAllPinned(
2216                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2217                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2218                    "s1", "s2", "s3");
2219            assertShortcutIds(assertAllDynamic(
2220                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2221                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2222                    "s1", "s2", "s3");
2223            assertShortcutIds(assertAllDynamicOrPinned(
2224                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2225                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2226                    "s1", "s2", "s3");
2227
2228            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2229            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2230            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2231
2232            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2233            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2234            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2235
2236            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2237                    SecurityException.class);
2238            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2239                    SecurityException.class);
2240            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2241                    SecurityException.class);
2242            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2243                    SecurityException.class);
2244            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2245                    SecurityException.class);
2246            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2247                    SecurityException.class);
2248        });
2249        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2250            assertShortcutIds(assertAllPinned(
2251                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2252                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2253                    "s2");
2254            assertShortcutIds(assertAllDynamic(
2255                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2256                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2257                    "s1", "s2", "s3");
2258            assertShortcutIds(assertAllDynamicOrPinned(
2259                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2260                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2261                    "s1", "s2", "s3");
2262
2263            assertShortcutIds(assertAllPinned(
2264                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2265                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2266                    "s2", "s3");
2267            assertShortcutIds(assertAllDynamic(
2268                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2269                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2270                    "s1", "s2", "s3");
2271            assertShortcutIds(assertAllDynamicOrPinned(
2272                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2273                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2274                    "s1", "s2", "s3");
2275
2276            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2277            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2278            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2279
2280            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2281            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2282            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2283
2284            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2285                    SecurityException.class);
2286            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2287                    SecurityException.class);
2288            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2289                    SecurityException.class);
2290            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2291                    SecurityException.class);
2292            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2293                    SecurityException.class);
2294            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2295                    SecurityException.class);
2296        });
2297        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2298            assertShortcutIds(assertAllPinned(
2299                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2300                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2301                    "s3");
2302            assertShortcutIds(assertAllDynamic(
2303                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2304                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2305                    "s1", "s2", "s3");
2306            assertShortcutIds(assertAllDynamicOrPinned(
2307                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2308                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2309                    "s1", "s2", "s3");
2310
2311            assertShortcutIds(assertAllPinned(
2312                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2313                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2314                    "s3");
2315            assertShortcutIds(assertAllDynamic(
2316                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2317                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2318                    "s1", "s2", "s3");
2319            assertShortcutIds(assertAllDynamicOrPinned(
2320                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2321                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2322                    "s1", "s2", "s3");
2323
2324            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2325            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2326            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2327
2328            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2329            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2330            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2331
2332            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2333                    SecurityException.class);
2334            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2335                    SecurityException.class);
2336            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2337                    SecurityException.class);
2338            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2339                    SecurityException.class);
2340            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2341                    SecurityException.class);
2342            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2343                    SecurityException.class);
2344        });
2345        runWithCaller(LAUNCHER_2, USER_10, () -> {
2346            assertShortcutIds(assertAllPinned(
2347                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2348                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2349                    "s1", "s2", "s3");
2350            assertShortcutIds(assertAllDynamic(
2351                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2352                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2353                    "s1", "s2", "s3", "s4", "s5", "s6");
2354            assertShortcutIds(assertAllDynamicOrPinned(
2355                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2356                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2357                    "s1", "s2", "s3", "s4", "s5", "s6");
2358        });
2359
2360        // Remove some dynamic shortcuts.
2361
2362        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2363            assertTrue(mManager.setDynamicShortcuts(list(
2364                    makeShortcut("s1"))));
2365        });
2366        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2367            assertTrue(mManager.setDynamicShortcuts(list(
2368                    makeShortcut("s1"))));
2369        });
2370        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2371            assertTrue(mManager.setDynamicShortcuts(list(
2372                    makeShortcut("s1"))));
2373        });
2374
2375        runWithCaller(LAUNCHER_1, USER_0, () -> {
2376            assertShortcutIds(assertAllPinned(
2377                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2378                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2379                    "s1");
2380            assertShortcutIds(assertAllDynamic(
2381                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2382                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2383                    "s1");
2384            assertShortcutIds(assertAllDynamicOrPinned(
2385                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2386                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2387                    "s1");
2388
2389            assertShortcutIds(assertAllPinned(
2390                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2391                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2392                    "s1", "s2", "s3");
2393            assertShortcutIds(assertAllDynamic(
2394                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2395                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2396                    "s1");
2397            assertShortcutIds(assertAllDynamicOrPinned(
2398                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2399                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2400                    "s1", "s2", "s3");
2401
2402            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2403            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2404                    ActivityNotFoundException.class);
2405            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2406                    ActivityNotFoundException.class);
2407
2408            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2409            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2410            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2411
2412            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2413                    SecurityException.class);
2414            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2415                    SecurityException.class);
2416            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2417                    SecurityException.class);
2418            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2419                    SecurityException.class);
2420            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2421                    SecurityException.class);
2422            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2423                    SecurityException.class);
2424        });
2425        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2426            assertShortcutIds(assertAllPinned(
2427                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2428                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2429                    "s2");
2430            assertShortcutIds(assertAllDynamic(
2431                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2432                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2433                    "s1");
2434            assertShortcutIds(assertAllDynamicOrPinned(
2435                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2436                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2437                    "s1", "s2");
2438
2439            assertShortcutIds(assertAllPinned(
2440                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2441                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2442                    "s2", "s3");
2443            assertShortcutIds(assertAllDynamic(
2444                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2445                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2446                    "s1");
2447            assertShortcutIds(assertAllDynamicOrPinned(
2448                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2449                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2450                    "s1", "s2", "s3");
2451
2452            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2453            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2454            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2455                    ActivityNotFoundException.class);
2456
2457            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2458            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2459            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2460
2461            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2462                    SecurityException.class);
2463            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2464                    SecurityException.class);
2465            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2466                    SecurityException.class);
2467            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2468                    SecurityException.class);
2469            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2470                    SecurityException.class);
2471            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2472                    SecurityException.class);
2473        });
2474        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2475            assertShortcutIds(assertAllPinned(
2476                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2477                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2478                    "s3");
2479            assertShortcutIds(assertAllDynamic(
2480                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2481                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2482                    "s1");
2483            assertShortcutIds(assertAllDynamicOrPinned(
2484                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2485                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2486                    "s1", "s3");
2487
2488            assertShortcutIds(assertAllPinned(
2489                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2490                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2491                    "s3");
2492            assertShortcutIds(assertAllDynamic(
2493                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2494                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2495                    "s1");
2496            assertShortcutIds(assertAllDynamicOrPinned(
2497                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2498                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2499                    "s1", "s3");
2500
2501            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2502            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2503                    ActivityNotFoundException.class);
2504            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2505
2506            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2507            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2508                    ActivityNotFoundException.class);
2509            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2510
2511            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2512                    SecurityException.class);
2513            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2514                    SecurityException.class);
2515            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2516                    SecurityException.class);
2517            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2518                    SecurityException.class);
2519            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2520                    SecurityException.class);
2521            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2522                    SecurityException.class);
2523        });
2524        runWithCaller(LAUNCHER_2, USER_10, () -> {
2525            assertShortcutIds(assertAllPinned(
2526                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2527                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2528                    "s1", "s2", "s3");
2529            assertShortcutIds(assertAllDynamic(
2530                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2531                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2532                    "s1");
2533            assertShortcutIds(assertAllDynamicOrPinned(
2534                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2535                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2536                    "s1", "s2", "s3");
2537
2538            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2539                    SecurityException.class);
2540            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2541                    SecurityException.class);
2542            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2543                    SecurityException.class);
2544
2545            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
2546                    SecurityException.class);
2547            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2548                    SecurityException.class);
2549            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
2550                    SecurityException.class);
2551
2552            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
2553            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
2554            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
2555            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2556                    ActivityNotFoundException.class);
2557            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2558                    ActivityNotFoundException.class);
2559            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2560                    ActivityNotFoundException.class);
2561        });
2562
2563        // Save & load and make sure we still have the same information.
2564        mService.saveDirtyInfo();
2565        initService();
2566        mService.handleUnlockUser(USER_0);
2567
2568        runWithCaller(LAUNCHER_1, USER_0, () -> {
2569            assertShortcutIds(assertAllPinned(
2570                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2571                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2572                    "s1");
2573            assertShortcutIds(assertAllDynamic(
2574                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2575                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2576                    "s1");
2577            assertShortcutIds(assertAllDynamicOrPinned(
2578                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2579                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2580                    "s1");
2581
2582            assertShortcutIds(assertAllPinned(
2583                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2584                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2585                    "s1", "s2", "s3");
2586            assertShortcutIds(assertAllDynamic(
2587                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2588                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2589                    "s1");
2590            assertShortcutIds(assertAllDynamicOrPinned(
2591                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2592                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2593                    "s1", "s2", "s3");
2594
2595            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2596            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2597                    ActivityNotFoundException.class);
2598            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2599                    ActivityNotFoundException.class);
2600
2601            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2602            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2603            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2604
2605            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2606                    SecurityException.class);
2607            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2608                    SecurityException.class);
2609            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2610                    SecurityException.class);
2611            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2612                    SecurityException.class);
2613            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2614                    SecurityException.class);
2615            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2616                    SecurityException.class);
2617        });
2618        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2619            assertShortcutIds(assertAllPinned(
2620                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2621                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2622                    "s2");
2623            assertShortcutIds(assertAllDynamic(
2624                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2625                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2626                    "s1");
2627            assertShortcutIds(assertAllDynamicOrPinned(
2628                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2629                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2630                    "s1", "s2");
2631
2632            assertShortcutIds(assertAllPinned(
2633                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2634                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2635                    "s2", "s3");
2636            assertShortcutIds(assertAllDynamic(
2637                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2638                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2639                    "s1");
2640            assertShortcutIds(assertAllDynamicOrPinned(
2641                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2642                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2643                    "s1", "s2", "s3");
2644
2645            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2646            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2647            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2648                    ActivityNotFoundException.class);
2649
2650            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2651            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2652            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2653
2654            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2655                    SecurityException.class);
2656            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2657                    SecurityException.class);
2658            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2659                    SecurityException.class);
2660            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2661                    SecurityException.class);
2662            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2663                    SecurityException.class);
2664            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2665                    SecurityException.class);
2666        });
2667        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2668            assertShortcutIds(assertAllPinned(
2669                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2670                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2671                    "s3");
2672            assertShortcutIds(assertAllDynamic(
2673                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2674                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2675                    "s1");
2676            assertShortcutIds(assertAllDynamicOrPinned(
2677                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2678                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2679                    "s1", "s3");
2680
2681            assertShortcutIds(assertAllPinned(
2682                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2683                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2684                    "s3");
2685            assertShortcutIds(assertAllDynamic(
2686                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2687                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2688                    "s1");
2689            assertShortcutIds(assertAllDynamicOrPinned(
2690                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2691                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2692                    "s1", "s3");
2693
2694            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2695            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2696                    ActivityNotFoundException.class);
2697            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2698
2699            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2700            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2701                    ActivityNotFoundException.class);
2702            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2703
2704            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2705                    SecurityException.class);
2706            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2707                    SecurityException.class);
2708            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2709                    SecurityException.class);
2710            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2711                    SecurityException.class);
2712            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2713                    SecurityException.class);
2714            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2715                    SecurityException.class);
2716        });
2717    }
2718
2719    public void testStartShortcut() {
2720        // Create some shortcuts.
2721        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2722            final ShortcutInfo s1_1 = makeShortcut(
2723                    "s1",
2724                    "Title 1",
2725                    makeComponent(ShortcutActivity.class),
2726                    /* icon =*/ null,
2727                    new Intent[] {makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
2728                            "key1", "val1", "nest", makeBundle("key", 123))
2729                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
2730                    new Intent("act2").setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)},
2731                    /* rank */ 10);
2732
2733            final ShortcutInfo s1_2 = makeShortcut(
2734                    "s2",
2735                    "Title 2",
2736                    /* activity */ null,
2737                    /* icon =*/ null,
2738                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
2739                    /* rank */ 12,
2740                    /* chooserFilter=*/ null,
2741                    /* chooserComponentNames=*/ null);
2742
2743            final ShortcutInfo s1_3 = makeShortcut("s3");
2744
2745            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
2746        });
2747
2748        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2749            final ShortcutInfo s2_1 = makeShortcut(
2750                    "s1",
2751                    "ABC",
2752                    makeComponent(ShortcutActivity.class),
2753                    /* icon =*/ null,
2754                    makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
2755                            "key1", "val1", "nest", makeBundle("key", 123)),
2756                    /* weight */ 10,
2757                    /* chooserFilter=*/ null,
2758                    /* chooserComponentNames=*/ null);
2759            assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
2760        });
2761
2762        // Pin some.
2763        runWithCaller(LAUNCHER_1, USER_0, () -> {
2764            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2765                    list("s1", "s2"), getCallingUser());
2766
2767            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2768                    list("s1"), getCallingUser());
2769        });
2770
2771        // Just to make it complicated, delete some.
2772        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2773            mManager.removeDynamicShortcuts(list("s2"));
2774        });
2775
2776        runWithCaller(LAUNCHER_1, USER_0, () -> {
2777            final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
2778            assertEquals(ShortcutActivity2.class.getName(),
2779                    intents[0].getComponent().getClassName());
2780            assertEquals(Intent.ACTION_ASSIST,
2781                    intents[0].getAction());
2782            assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
2783                    intents[0].getFlags());
2784
2785            assertEquals("act2",
2786                    intents[1].getAction());
2787            assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
2788                    intents[1].getFlags());
2789
2790            assertEquals(
2791                    ShortcutActivity3.class.getName(),
2792                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2793                            .getComponent().getClassName());
2794            assertEquals(
2795                    ShortcutActivity.class.getName(),
2796                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2797                            .getComponent().getClassName());
2798
2799            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2800
2801            assertShortcutNotLaunched("no-such-package", "s2", USER_0);
2802            assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
2803        });
2804
2805        // LAUNCHER_1 is no longer the default launcher
2806        setDefaultLauncherChecker((pkg, userId) -> false);
2807
2808        runWithCaller(LAUNCHER_1, USER_0, () -> {
2809            // Not the default launcher, but pinned shortcuts are still lauchable.
2810            final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
2811            assertEquals(ShortcutActivity2.class.getName(),
2812                    intents[0].getComponent().getClassName());
2813            assertEquals(Intent.ACTION_ASSIST,
2814                    intents[0].getAction());
2815            assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
2816                    intents[0].getFlags());
2817
2818            assertEquals("act2",
2819                    intents[1].getAction());
2820            assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
2821                    intents[1].getFlags());
2822            assertEquals(
2823                    ShortcutActivity3.class.getName(),
2824                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2825                            .getComponent().getClassName());
2826            assertEquals(
2827                    ShortcutActivity.class.getName(),
2828                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2829                            .getComponent().getClassName());
2830
2831            // Not pinned, so not lauchable.
2832        });
2833
2834        // Test inner errors.
2835        runWithCaller(LAUNCHER_1, USER_0, () -> {
2836            // Not launchable.
2837            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2838                    .when(mMockActivityManagerInternal).startActivitiesAsPackage(
2839                            anyStringOrNull(), anyInt(),
2840                            anyOrNull(Intent[].class), anyOrNull(Bundle.class));
2841            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2842                    ActivityNotFoundException.class);
2843
2844            // Still not launchable.
2845            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2846                    .when(mMockActivityManagerInternal)
2847                    .startActivitiesAsPackage(
2848                            anyStringOrNull(), anyInt(),
2849                            anyOrNull(Intent[].class), anyOrNull(Bundle.class));
2850            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2851                    ActivityNotFoundException.class);
2852        });
2853
2854
2855        // TODO Check extra, etc
2856    }
2857
2858    public void testLauncherCallback() throws Throwable {
2859        // Disable throttling for this test.
2860        mService.updateConfigurationLocked(
2861                ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
2862                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
2863        );
2864
2865        setCaller(LAUNCHER_1, USER_0);
2866
2867        assertForLauncherCallback(mLauncherApps, () -> {
2868            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2869                assertTrue(mManager.setDynamicShortcuts(list(
2870                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2871            });
2872        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2873                .haveIds("s1", "s2", "s3")
2874                .areAllWithKeyFieldsOnly()
2875                .areAllDynamic();
2876
2877        // From different package.
2878        assertForLauncherCallback(mLauncherApps, () -> {
2879            runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2880                assertTrue(mManager.setDynamicShortcuts(list(
2881                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2882            });
2883        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
2884                .haveIds("s1", "s2", "s3")
2885                .areAllWithKeyFieldsOnly()
2886                .areAllDynamic();
2887
2888        mRunningUsers.put(USER_10, true);
2889
2890        // Different user, callback shouldn't be called.
2891        assertForLauncherCallback(mLauncherApps, () -> {
2892            runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2893                assertTrue(mManager.setDynamicShortcuts(list(
2894                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2895            });
2896        }).assertNoCallbackCalled();
2897
2898
2899        // Test for addDynamicShortcuts.
2900        assertForLauncherCallback(mLauncherApps, () -> {
2901            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2902                assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
2903            });
2904        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2905                .haveIds("s1", "s2", "s3", "s4")
2906                .areAllWithKeyFieldsOnly()
2907                .areAllDynamic();
2908
2909        // Test for remove
2910        assertForLauncherCallback(mLauncherApps, () -> {
2911            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2912                mManager.removeDynamicShortcuts(list("s1"));
2913            });
2914        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2915                .haveIds("s2", "s3", "s4")
2916                .areAllWithKeyFieldsOnly()
2917                .areAllDynamic();
2918
2919        // Test for update
2920        assertForLauncherCallback(mLauncherApps, () -> {
2921            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2922                assertTrue(mManager.updateShortcuts(list(
2923                        makeShortcut("s1"), makeShortcut("s2"))));
2924            });
2925        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2926                // All remaining shortcuts will be passed regardless of what's been updated.
2927                .haveIds("s2", "s3", "s4")
2928                .areAllWithKeyFieldsOnly()
2929                .areAllDynamic();
2930
2931        // Test for deleteAll
2932        assertForLauncherCallback(mLauncherApps, () -> {
2933            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2934                mManager.removeAllDynamicShortcuts();
2935            });
2936        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2937                .isEmpty();
2938
2939        // Update package1 with manifest shortcuts
2940        assertForLauncherCallback(mLauncherApps, () -> {
2941            addManifestShortcutResource(
2942                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2943                    R.xml.shortcut_2);
2944            updatePackageVersion(CALLING_PACKAGE_1, 1);
2945            mService.mPackageMonitor.onReceive(getTestContext(),
2946                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2947        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2948                .areAllManifest()
2949                .areAllWithKeyFieldsOnly()
2950                .haveIds("ms1", "ms2");
2951
2952        // Make sure pinned shortcuts are passed too.
2953        // 1. Add dynamic shortcuts.
2954        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2955            assertTrue(mManager.setDynamicShortcuts(list(
2956                    makeShortcut("s1"), makeShortcut("s2"))));
2957        });
2958
2959        // 2. Pin some.
2960        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
2961            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
2962        });
2963        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2964            assertWith(getCallerShortcuts())
2965                    .haveIds("ms1", "ms2", "s1", "s2")
2966                    .areAllEnabled()
2967
2968                    .selectByIds("ms1", "ms2")
2969                    .areAllManifest()
2970
2971                    .revertToOriginalList()
2972                    .selectByIds("s1", "s2")
2973                    .areAllDynamic()
2974                    ;
2975        });
2976
2977        // 3 Update the app with no manifest shortcuts.  (Pinned one will survive.)
2978        addManifestShortcutResource(
2979                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2980                R.xml.shortcut_0);
2981        updatePackageVersion(CALLING_PACKAGE_1, 1);
2982        mService.mPackageMonitor.onReceive(getTestContext(),
2983                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2984
2985        assertForLauncherCallback(mLauncherApps, () -> {
2986            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2987                mManager.removeDynamicShortcuts(list("s2"));
2988
2989                assertWith(getCallerShortcuts())
2990                        .haveIds("ms2", "s1", "s2")
2991
2992                        .selectByIds("ms2")
2993                        .areAllNotManifest()
2994                        .areAllPinned()
2995                        .areAllImmutable()
2996                        .areAllDisabled()
2997
2998                        .revertToOriginalList()
2999                        .selectByIds("s1")
3000                        .areAllDynamic()
3001                        .areAllNotPinned()
3002                        .areAllEnabled()
3003
3004                        .revertToOriginalList()
3005                        .selectByIds("s2")
3006                        .areAllNotDynamic()
3007                        .areAllPinned()
3008                        .areAllEnabled()
3009                ;
3010            });
3011        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
3012                .haveIds("ms2", "s1", "s2")
3013                .areAllWithKeyFieldsOnly();
3014
3015        // Remove CALLING_PACKAGE_2
3016        assertForLauncherCallback(mLauncherApps, () -> {
3017            uninstallPackage(USER_0, CALLING_PACKAGE_2);
3018            mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
3019                    /* appStillExists = */ false);
3020        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
3021                .isEmpty();
3022    }
3023
3024    public void testLauncherCallback_crossProfile() throws Throwable {
3025        prepareCrossProfileDataSet();
3026
3027        final Handler h = new Handler(Looper.getMainLooper());
3028
3029        final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
3030        final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
3031        final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
3032        final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
3033
3034        final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
3035        final LauncherApps.Callback cP1_1 = mock(LauncherApps.Callback.class);
3036        final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
3037        final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
3038        final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
3039
3040        final List<LauncherApps.Callback> all =
3041                list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
3042
3043        setDefaultLauncherChecker((pkg, userId) -> {
3044            switch (userId) {
3045                case USER_0:
3046                    return LAUNCHER_2.equals(pkg);
3047                case USER_P0:
3048                    return LAUNCHER_1.equals(pkg);
3049                case USER_P1:
3050                    return LAUNCHER_1.equals(pkg);
3051                case USER_10:
3052                    return LAUNCHER_1.equals(pkg);
3053                case USER_11:
3054                    return LAUNCHER_1.equals(pkg);
3055                default:
3056                    return false;
3057            }
3058        });
3059
3060        runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
3061        runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
3062        runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
3063        runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
3064        runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
3065        runWithCaller(LAUNCHER_1, USER_P1, () -> mLauncherApps.registerCallback(cP1_1, h));
3066        runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
3067        runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
3068        runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
3069
3070        // User 0.
3071
3072        resetAll(all);
3073        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3074            mManager.removeDynamicShortcuts(list());
3075        });
3076        waitOnMainThread();
3077
3078        assertCallbackNotReceived(c0_1);
3079        assertCallbackNotReceived(c0_3);
3080        assertCallbackNotReceived(c0_4);
3081        assertCallbackNotReceived(c10_1);
3082        assertCallbackNotReceived(c10_2);
3083        assertCallbackNotReceived(c11_1);
3084        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
3085        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
3086        assertCallbackNotReceived(cP1_1);
3087
3088        // User 0, different package.
3089
3090        resetAll(all);
3091        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
3092            mManager.removeDynamicShortcuts(list());
3093        });
3094        waitOnMainThread();
3095
3096        assertCallbackNotReceived(c0_1);
3097        assertCallbackNotReceived(c0_3);
3098        assertCallbackNotReceived(c0_4);
3099        assertCallbackNotReceived(c10_1);
3100        assertCallbackNotReceived(c10_2);
3101        assertCallbackNotReceived(c11_1);
3102        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
3103        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
3104                "s1", "s2", "s3", "s4", "s5", "s6");
3105        assertCallbackNotReceived(cP1_1);
3106
3107        // Work profile.
3108        resetAll(all);
3109        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3110            mManager.removeDynamicShortcuts(list());
3111        });
3112        waitOnMainThread();
3113
3114        assertCallbackNotReceived(c0_1);
3115        assertCallbackNotReceived(c0_3);
3116        assertCallbackNotReceived(c0_4);
3117        assertCallbackNotReceived(c10_1);
3118        assertCallbackNotReceived(c10_2);
3119        assertCallbackNotReceived(c11_1);
3120        assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
3121        assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
3122        assertCallbackNotReceived(cP1_1);
3123
3124        // Normal secondary user.
3125        mRunningUsers.put(USER_10, true);
3126
3127        resetAll(all);
3128        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3129            mManager.removeDynamicShortcuts(list());
3130        });
3131        waitOnMainThread();
3132
3133        assertCallbackNotReceived(c0_1);
3134        assertCallbackNotReceived(c0_2);
3135        assertCallbackNotReceived(c0_3);
3136        assertCallbackNotReceived(c0_4);
3137        assertCallbackNotReceived(cP0_1);
3138        assertCallbackNotReceived(c10_2);
3139        assertCallbackNotReceived(c11_1);
3140        assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
3141                "x1", "x2", "x3", "x4", "x5");
3142        assertCallbackNotReceived(cP1_1);
3143    }
3144
3145    // === Test for persisting ===
3146
3147    public void testSaveAndLoadUser_empty() {
3148        assertTrue(mManager.setDynamicShortcuts(list()));
3149
3150        Log.i(TAG, "Saved state");
3151        dumpsysOnLogcat();
3152        dumpUserFile(0);
3153
3154        // Restore.
3155        mService.saveDirtyInfo();
3156        initService();
3157
3158        assertEquals(0, mManager.getDynamicShortcuts().size());
3159    }
3160
3161    /**
3162     * Try save and load, also stop/start the user.
3163     */
3164    public void testSaveAndLoadUser() {
3165        // First, create some shortcuts and save.
3166        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3167            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
3168            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3169                    getTestContext().getResources(), R.drawable.icon2));
3170
3171            final ShortcutInfo si1 = makeShortcut(
3172                    "s1",
3173                    "title1-1",
3174                    makeComponent(ShortcutActivity.class),
3175                    icon1,
3176                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3177                            "key1", "val1", "nest", makeBundle("key", 123)),
3178                        /* weight */ 10,
3179                    /* chooserFilter=*/ null,
3180                    /* chooserComponentNames=*/ null);
3181
3182            final ShortcutInfo si2 = makeShortcut(
3183                    "s2",
3184                    "title1-2",
3185                        /* activity */ null,
3186                    icon2,
3187                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3188                        /* weight */ 12,
3189                    /* chooserFilter=*/ null,
3190                    /* chooserComponentNames=*/ null);
3191
3192            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3193
3194            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3195            assertEquals(2, mManager.getRemainingCallCount());
3196        });
3197        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3198            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
3199            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3200                    getTestContext().getResources(), R.drawable.icon2));
3201
3202            final ShortcutInfo si1 = makeShortcut(
3203                    "s1",
3204                    "title2-1",
3205                    makeComponent(ShortcutActivity.class),
3206                    icon1,
3207                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3208                            "key1", "val1", "nest", makeBundle("key", 123)), /* weight */ 10,
3209                            /* chooserFilter=*/ null, /* chooserComponentNames=*/ null);
3210
3211            final ShortcutInfo si2 = makeShortcut(
3212                    "s2",
3213                    "title2-2",
3214                        /* activity */ null,
3215                    icon2,
3216                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3217                        /* weight */ 12, /* chooserFilter=*/ null,
3218                        /* chooserComponentNames=*/ null);
3219
3220            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3221
3222            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3223            assertEquals(2, mManager.getRemainingCallCount());
3224        });
3225
3226        mRunningUsers.put(USER_10, true);
3227
3228        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3229            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
3230            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3231                    getTestContext().getResources(), R.drawable.icon2));
3232
3233            final ShortcutInfo si1 = makeShortcut(
3234                    "s1",
3235                    "title10-1-1",
3236                    makeComponent(ShortcutActivity.class),
3237                    icon1,
3238                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3239                            "key1", "val1", "nest", makeBundle("key", 123)),
3240                            /* weight */ 10, /* chooserFilter=*/ null,
3241                            /* chooserComponentNames=*/ null);
3242
3243            final ShortcutInfo si2 = makeShortcut(
3244                    "s2",
3245                    "title10-1-2",
3246                        /* activity */ null,
3247                    icon2,
3248                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3249                            /* weight */ 12, /* chooserFilter=*/ null,
3250                            /* chooserComponentNames=*/ null);
3251
3252            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3253
3254            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3255            assertEquals(2, mManager.getRemainingCallCount());
3256        });
3257
3258        mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
3259                new ComponentName("pkg1", "class"));
3260
3261        // Restore.
3262        mService.saveDirtyInfo();
3263        initService();
3264
3265        // Before the load, the map should be empty.
3266        assertEquals(0, mService.getShortcutsForTest().size());
3267
3268        // this will pre-load the per-user info.
3269        mService.handleUnlockUser(UserHandle.USER_SYSTEM);
3270
3271        // Now it's loaded.
3272        assertEquals(1, mService.getShortcutsForTest().size());
3273
3274        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3275            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3276                    mManager.getDynamicShortcuts()))), "s1", "s2");
3277            assertEquals(2, mManager.getRemainingCallCount());
3278
3279            assertEquals("title1-1", getCallerShortcut("s1").getTitle());
3280            assertEquals("title1-2", getCallerShortcut("s2").getTitle());
3281        });
3282        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3283            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3284                    mManager.getDynamicShortcuts()))), "s1", "s2");
3285            assertEquals(2, mManager.getRemainingCallCount());
3286
3287            assertEquals("title2-1", getCallerShortcut("s1").getTitle());
3288            assertEquals("title2-2", getCallerShortcut("s2").getTitle());
3289        });
3290
3291        assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
3292                .getLastKnownLauncher().getPackageName());
3293
3294        // Start another user
3295        mService.handleUnlockUser(USER_10);
3296
3297        // Now the size is 2.
3298        assertEquals(2, mService.getShortcutsForTest().size());
3299
3300        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3301            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3302                    mManager.getDynamicShortcuts()))), "s1", "s2");
3303            assertEquals(2, mManager.getRemainingCallCount());
3304
3305            assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
3306            assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
3307        });
3308        assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
3309
3310        // Try stopping the user
3311        mService.handleCleanupUser(USER_10);
3312
3313        // Now it's unloaded.
3314        assertEquals(1, mService.getShortcutsForTest().size());
3315
3316        // TODO Check all other fields
3317    }
3318
3319    public void testCleanupPackage() {
3320        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3321            assertTrue(mManager.setDynamicShortcuts(list(
3322                    makeShortcut("s0_1"))));
3323        });
3324        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3325            assertTrue(mManager.setDynamicShortcuts(list(
3326                    makeShortcut("s0_2"))));
3327        });
3328        runWithCaller(LAUNCHER_1, USER_0, () -> {
3329            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3330                    HANDLE_USER_0);
3331            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3332                    HANDLE_USER_0);
3333        });
3334        runWithCaller(LAUNCHER_2, USER_0, () -> {
3335            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3336                    HANDLE_USER_0);
3337            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3338                    HANDLE_USER_0);
3339        });
3340
3341        mRunningUsers.put(USER_10, true);
3342
3343        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3344            assertTrue(mManager.setDynamicShortcuts(list(
3345                    makeShortcut("s10_1"))));
3346        });
3347        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3348            assertTrue(mManager.setDynamicShortcuts(list(
3349                    makeShortcut("s10_2"))));
3350        });
3351        runWithCaller(LAUNCHER_1, USER_10, () -> {
3352            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3353                    HANDLE_USER_10);
3354            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3355                    HANDLE_USER_10);
3356        });
3357        runWithCaller(LAUNCHER_2, USER_10, () -> {
3358            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3359                    HANDLE_USER_10);
3360            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3361                    HANDLE_USER_10);
3362        });
3363
3364        // Remove all dynamic shortcuts; now all shortcuts are just pinned.
3365        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3366            mManager.removeAllDynamicShortcuts();
3367        });
3368        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3369            mManager.removeAllDynamicShortcuts();
3370        });
3371        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3372            mManager.removeAllDynamicShortcuts();
3373        });
3374        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3375            mManager.removeAllDynamicShortcuts();
3376        });
3377
3378
3379        final SparseArray<ShortcutUser> users =  mService.getShortcutsForTest();
3380        assertEquals(2, users.size());
3381        assertEquals(USER_0, users.keyAt(0));
3382        assertEquals(USER_10, users.keyAt(1));
3383
3384        final ShortcutUser user0 =  users.get(USER_0);
3385        final ShortcutUser user10 =  users.get(USER_10);
3386
3387
3388        // Check the registered packages.
3389        dumpsysOnLogcat();
3390        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3391                hashSet(user0.getAllPackagesForTest().keySet()));
3392        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3393                hashSet(user10.getAllPackagesForTest().keySet()));
3394        assertEquals(
3395                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3396                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3397                hashSet(user0.getAllLaunchersForTest().keySet()));
3398        assertEquals(
3399                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3400                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3401                hashSet(user10.getAllLaunchersForTest().keySet()));
3402        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3403                "s0_1", "s0_2");
3404        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3405                "s0_1", "s0_2");
3406        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3407                "s10_1", "s10_2");
3408        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3409                "s10_1", "s10_2");
3410        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3411        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3412        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3413        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3414
3415        mService.saveDirtyInfo();
3416
3417        // Nonexistent package.
3418        uninstallPackage(USER_0, "abc");
3419        mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
3420
3421        // No changes.
3422        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3423                hashSet(user0.getAllPackagesForTest().keySet()));
3424        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3425                hashSet(user10.getAllPackagesForTest().keySet()));
3426        assertEquals(
3427                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3428                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3429                hashSet(user0.getAllLaunchersForTest().keySet()));
3430        assertEquals(
3431                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3432                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3433                hashSet(user10.getAllLaunchersForTest().keySet()));
3434        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3435                "s0_1", "s0_2");
3436        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3437                "s0_1", "s0_2");
3438        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3439                "s10_1", "s10_2");
3440        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3441                "s10_1", "s10_2");
3442        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3443        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3444        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3445        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3446
3447        mService.saveDirtyInfo();
3448
3449        // Remove a package.
3450        uninstallPackage(USER_0, CALLING_PACKAGE_1);
3451        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3452                /* appStillExists = */ false);
3453
3454        assertEquals(set(CALLING_PACKAGE_2),
3455                hashSet(user0.getAllPackagesForTest().keySet()));
3456        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3457                hashSet(user10.getAllPackagesForTest().keySet()));
3458        assertEquals(
3459                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3460                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3461                hashSet(user0.getAllLaunchersForTest().keySet()));
3462        assertEquals(
3463                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3464                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3465                hashSet(user10.getAllLaunchersForTest().keySet()));
3466        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3467                "s0_2");
3468        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3469                "s0_2");
3470        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3471                "s10_1", "s10_2");
3472        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3473                "s10_1", "s10_2");
3474        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3475        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3476        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3477        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3478
3479        mService.saveDirtyInfo();
3480
3481        // Remove a launcher.
3482        uninstallPackage(USER_10, LAUNCHER_1);
3483        mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
3484
3485        assertEquals(set(CALLING_PACKAGE_2),
3486                hashSet(user0.getAllPackagesForTest().keySet()));
3487        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3488                hashSet(user10.getAllPackagesForTest().keySet()));
3489        assertEquals(
3490                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3491                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3492                hashSet(user0.getAllLaunchersForTest().keySet()));
3493        assertEquals(
3494                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3495                hashSet(user10.getAllLaunchersForTest().keySet()));
3496        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3497                "s0_2");
3498        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3499                "s0_2");
3500        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3501                "s10_1", "s10_2");
3502        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3503        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3504        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3505        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3506
3507        mService.saveDirtyInfo();
3508
3509        // Remove a package.
3510        uninstallPackage(USER_10, CALLING_PACKAGE_2);
3511        mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
3512                /* appStillExists = */ false);
3513
3514        assertEquals(set(CALLING_PACKAGE_2),
3515                hashSet(user0.getAllPackagesForTest().keySet()));
3516        assertEquals(set(CALLING_PACKAGE_1),
3517                hashSet(user10.getAllPackagesForTest().keySet()));
3518        assertEquals(
3519                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3520                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3521                hashSet(user0.getAllLaunchersForTest().keySet()));
3522        assertEquals(
3523                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3524                hashSet(user10.getAllLaunchersForTest().keySet()));
3525        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3526                "s0_2");
3527        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3528                "s0_2");
3529        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3530                "s10_1");
3531        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3532        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3533        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3534        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3535
3536        mService.saveDirtyInfo();
3537
3538        // Remove the other launcher from user 10 too.
3539        uninstallPackage(USER_10, LAUNCHER_2);
3540        mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
3541                /* appStillExists = */ false);
3542
3543        assertEquals(set(CALLING_PACKAGE_2),
3544                hashSet(user0.getAllPackagesForTest().keySet()));
3545        assertEquals(set(CALLING_PACKAGE_1),
3546                hashSet(user10.getAllPackagesForTest().keySet()));
3547        assertEquals(
3548                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3549                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3550                hashSet(user0.getAllLaunchersForTest().keySet()));
3551        assertEquals(
3552                set(),
3553                hashSet(user10.getAllLaunchersForTest().keySet()));
3554        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3555                "s0_2");
3556        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3557                "s0_2");
3558
3559        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3560        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3561        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3562        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3563        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3564
3565        mService.saveDirtyInfo();
3566
3567        // More remove.
3568        uninstallPackage(USER_10, CALLING_PACKAGE_1);
3569        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
3570                /* appStillExists = */ false);
3571
3572        assertEquals(set(CALLING_PACKAGE_2),
3573                hashSet(user0.getAllPackagesForTest().keySet()));
3574        assertEquals(set(),
3575                hashSet(user10.getAllPackagesForTest().keySet()));
3576        assertEquals(
3577                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3578                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3579                hashSet(user0.getAllLaunchersForTest().keySet()));
3580        assertEquals(set(),
3581                hashSet(user10.getAllLaunchersForTest().keySet()));
3582        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3583                "s0_2");
3584        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3585                "s0_2");
3586
3587        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3588        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3589        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3590        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3591        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3592
3593        mService.saveDirtyInfo();
3594    }
3595
3596    public void testCleanupPackage_republishManifests() {
3597        addManifestShortcutResource(
3598                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3599                R.xml.shortcut_2);
3600        updatePackageVersion(CALLING_PACKAGE_1, 1);
3601                mService.mPackageMonitor.onReceive(getTestContext(),
3602                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3603
3604        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3605            assertTrue(mManager.setDynamicShortcuts(list(
3606                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3607        });
3608        runWithCaller(LAUNCHER_1, USER_0, () -> {
3609            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3610                    list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
3611        });
3612
3613        // Remove ms2 from manifest.
3614        addManifestShortcutResource(
3615                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3616                R.xml.shortcut_1);
3617        updatePackageVersion(CALLING_PACKAGE_1, 1);
3618                mService.mPackageMonitor.onReceive(getTestContext(),
3619                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3620
3621        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3622            assertTrue(mManager.setDynamicShortcuts(list(
3623                    makeShortcut("s1"), makeShortcut("s2"))));
3624
3625            // Make sure the shortcuts are in the intended state.
3626            assertWith(getCallerShortcuts())
3627                    .haveIds("ms1", "ms2", "s1", "s2", "s3")
3628
3629                    .selectByIds("ms1")
3630                    .areAllManifest()
3631                    .areAllPinned()
3632
3633                    .revertToOriginalList()
3634                    .selectByIds("ms2")
3635                    .areAllNotManifest()
3636                    .areAllPinned()
3637
3638                    .revertToOriginalList()
3639                    .selectByIds("s1")
3640                    .areAllDynamic()
3641                    .areAllNotPinned()
3642
3643                    .revertToOriginalList()
3644                    .selectByIds("s2")
3645                    .areAllDynamic()
3646                    .areAllPinned()
3647
3648                    .revertToOriginalList()
3649                    .selectByIds("s3")
3650                    .areAllNotDynamic()
3651                    .areAllPinned();
3652        });
3653
3654        // Clean up + re-publish manifests.
3655        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3656                /* appStillExists = */ true);
3657        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3658            assertWith(getCallerShortcuts())
3659                    .haveIds("ms1")
3660                    .areAllManifest();
3661        });
3662    }
3663
3664    public void testHandleGonePackage_crossProfile() {
3665        // Create some shortcuts.
3666        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3667            assertTrue(mManager.setDynamicShortcuts(list(
3668                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3669        });
3670        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3671            assertTrue(mManager.setDynamicShortcuts(list(
3672                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3673        });
3674        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3675            assertTrue(mManager.setDynamicShortcuts(list(
3676                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3677        });
3678
3679        mRunningUsers.put(USER_10, true);
3680
3681        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3682            assertTrue(mManager.setDynamicShortcuts(list(
3683                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3684        });
3685
3686        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3687        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3688        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3689
3690        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3691        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3692        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3693
3694        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3695        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3696        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3697
3698        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3699        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3700        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3701
3702        // Pin some.
3703
3704        runWithCaller(LAUNCHER_1, USER_0, () -> {
3705            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3706                    list("s1"), HANDLE_USER_0);
3707
3708            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3709                    list("s2"), UserHandle.of(USER_P0));
3710
3711            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3712                    list("s3"), HANDLE_USER_0);
3713        });
3714
3715        runWithCaller(LAUNCHER_1, USER_P0, () -> {
3716            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3717                    list("s2"), HANDLE_USER_0);
3718
3719            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3720                    list("s3"), UserHandle.of(USER_P0));
3721
3722            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3723                    list("s1"), HANDLE_USER_0);
3724        });
3725
3726        runWithCaller(LAUNCHER_1, USER_10, () -> {
3727            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3728                    list("s3"), HANDLE_USER_10);
3729        });
3730
3731        // Check the state.
3732
3733        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3734        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3735        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3736
3737        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3738        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3739        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3740
3741        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3742        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3743        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3744
3745        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3746        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3747        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3748
3749        // Make sure all the information is persisted.
3750        mService.saveDirtyInfo();
3751        initService();
3752        mService.handleUnlockUser(USER_0);
3753        mService.handleUnlockUser(USER_P0);
3754        mService.handleUnlockUser(USER_10);
3755
3756        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3757        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3758        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3759
3760        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3761        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3762        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3763
3764        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3765        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3766        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3767
3768        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3769        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3770        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3771
3772        // Start uninstalling.
3773        uninstallPackage(USER_10, LAUNCHER_1);
3774        mService.checkPackageChanges(USER_10);
3775
3776        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3777        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3778        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3779
3780        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3781        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3782        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3783
3784        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3785        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3786        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3787
3788        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3789        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3790        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3791
3792        // Uninstall.
3793        uninstallPackage(USER_10, CALLING_PACKAGE_1);
3794        mService.checkPackageChanges(USER_10);
3795
3796        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3797        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3798        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3799
3800        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3801        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3802        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3803
3804        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3805        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3806        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3807
3808        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3809        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3810        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3811
3812        uninstallPackage(USER_P0, LAUNCHER_1);
3813        mService.checkPackageChanges(USER_0);
3814
3815        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3816        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3817        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3818
3819        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3820        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3821        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3822
3823        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3824        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3825        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3826
3827        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3828        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3829        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3830
3831        mService.checkPackageChanges(USER_P0);
3832
3833        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3834        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3835        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3836
3837        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3838        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3839        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3840
3841        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3842        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3843        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3844
3845        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3846        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3847        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3848
3849        uninstallPackage(USER_P0, CALLING_PACKAGE_1);
3850
3851        mService.saveDirtyInfo();
3852        initService();
3853        mService.handleUnlockUser(USER_0);
3854        mService.handleUnlockUser(USER_P0);
3855        mService.handleUnlockUser(USER_10);
3856
3857        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3858        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3859        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3860
3861        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3862        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3863        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3864
3865        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3866        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3867        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3868
3869        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3870        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3871        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3872
3873        // Uninstall
3874        uninstallPackage(USER_0, LAUNCHER_1);
3875
3876        mService.saveDirtyInfo();
3877        initService();
3878        mService.handleUnlockUser(USER_0);
3879        mService.handleUnlockUser(USER_P0);
3880        mService.handleUnlockUser(USER_10);
3881
3882        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3883        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3884        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3885
3886        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3887        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3888        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3889
3890        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3891        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3892        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3893
3894        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3895        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3896        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3897
3898        uninstallPackage(USER_0, CALLING_PACKAGE_2);
3899
3900        mService.saveDirtyInfo();
3901        initService();
3902        mService.handleUnlockUser(USER_0);
3903        mService.handleUnlockUser(USER_P0);
3904        mService.handleUnlockUser(USER_10);
3905
3906        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3907        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3908        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3909
3910        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3911        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3912        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3913
3914        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3915        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3916        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3917
3918        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3919        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3920        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3921    }
3922
3923    protected void checkCanRestoreTo(boolean expected, ShortcutPackageInfo spi,
3924            int version, String... signatures) {
3925        assertEquals(expected, spi.canRestoreTo(mService, genPackage(
3926                "dummy", /* uid */ 0, version, signatures)));
3927    }
3928
3929    public void testCanRestoreTo() {
3930        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
3931        addPackage(CALLING_PACKAGE_2, CALLING_UID_1, 10, "sig1", "sig2");
3932
3933        final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackageForTest(
3934                mService, CALLING_PACKAGE_1, USER_0);
3935        final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackageForTest(
3936                mService, CALLING_PACKAGE_2, USER_0);
3937
3938        checkCanRestoreTo(true, spi1, 10, "sig1");
3939        checkCanRestoreTo(true, spi1, 10, "x", "sig1");
3940        checkCanRestoreTo(true, spi1, 10, "sig1", "y");
3941        checkCanRestoreTo(true, spi1, 10, "x", "sig1", "y");
3942        checkCanRestoreTo(true, spi1, 11, "sig1");
3943
3944        checkCanRestoreTo(false, spi1, 10 /* empty */);
3945        checkCanRestoreTo(false, spi1, 10, "x");
3946        checkCanRestoreTo(false, spi1, 10, "x", "y");
3947        checkCanRestoreTo(false, spi1, 10, "x");
3948        checkCanRestoreTo(false, spi1, 9, "sig1");
3949
3950        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2");
3951        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1");
3952        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2");
3953        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1");
3954        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2", "y");
3955        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1", "y");
3956        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2", "y");
3957        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1", "y");
3958        checkCanRestoreTo(true, spi2, 11, "x", "sig2", "sig1", "y");
3959
3960        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x");
3961        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x");
3962        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2");
3963        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1");
3964        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x", "y");
3965        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x", "y");
3966        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2", "y");
3967        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1", "y");
3968        checkCanRestoreTo(false, spi2, 11, "x", "sig2x", "sig1", "y");
3969    }
3970
3971    public void testHandlePackageDelete() {
3972        checkHandlePackageDeleteInner((userId, packageName) -> {
3973            uninstallPackage(userId, packageName);
3974            mService.mPackageMonitor.onReceive(getTestContext(),
3975                    genPackageDeleteIntent(packageName, userId));
3976        });
3977    }
3978
3979    public void testHandlePackageDisable() {
3980        checkHandlePackageDeleteInner((userId, packageName) -> {
3981            disablePackage(userId, packageName);
3982            mService.mPackageMonitor.onReceive(getTestContext(),
3983                    genPackageChangedIntent(packageName, userId));
3984        });
3985    }
3986
3987    private void checkHandlePackageDeleteInner(BiConsumer<Integer, String> remover) {
3988        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3989                getTestContext().getResources(), R.drawable.black_32x32));
3990        setCaller(CALLING_PACKAGE_1, USER_0);
3991        assertTrue(mManager.addDynamicShortcuts(list(
3992                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
3993        )));
3994        // Also add a manifest shortcut, which should be removed too.
3995        addManifestShortcutResource(
3996                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3997                R.xml.shortcut_1);
3998        updatePackageVersion(CALLING_PACKAGE_1, 1);
3999                mService.mPackageMonitor.onReceive(getTestContext(),
4000                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
4001        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4002            assertWith(getCallerShortcuts())
4003                    .haveIds("s1", "s2", "ms1")
4004
4005                    .selectManifest()
4006                    .haveIds("ms1");
4007        });
4008
4009        setCaller(CALLING_PACKAGE_2, USER_0);
4010        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4011
4012        setCaller(CALLING_PACKAGE_3, USER_0);
4013        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4014
4015        mRunningUsers.put(USER_10, true);
4016
4017        setCaller(CALLING_PACKAGE_1, USER_10);
4018        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4019
4020        setCaller(CALLING_PACKAGE_2, USER_10);
4021        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4022
4023        setCaller(CALLING_PACKAGE_3, USER_10);
4024        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4025
4026        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4027        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4028        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4029        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4030        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4031        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4032
4033        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4034        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4035        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4036        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4037        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4038        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4039
4040        remover.accept(USER_0, CALLING_PACKAGE_1);
4041
4042        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4043        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4044        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4045        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4046        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4047        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4048
4049        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4050        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4051        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4052        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4053        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4054        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4055
4056        mRunningUsers.put(USER_10, true);
4057
4058        remover.accept(USER_10, CALLING_PACKAGE_2);
4059
4060        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4061        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4062        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4063        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4064        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4065        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4066
4067        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4068        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4069        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4070        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4071        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4072        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4073
4074        mInjectedPackages.remove(CALLING_PACKAGE_1);
4075        mInjectedPackages.remove(CALLING_PACKAGE_3);
4076
4077        mService.checkPackageChanges(USER_0);
4078
4079        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4080        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4081        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));  // ---------------
4082        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4083        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4084        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4085
4086        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4087        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4088        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4089        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4090        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4091        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4092
4093        mService.checkPackageChanges(USER_10);
4094
4095        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4096        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4097        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4098        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4099        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4100        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4101
4102        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4103        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4104        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4105        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4106        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4107        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4108    }
4109
4110    /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
4111    public void testHandlePackageClearData() {
4112        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4113                getTestContext().getResources(), R.drawable.black_32x32));
4114        setCaller(CALLING_PACKAGE_1, USER_0);
4115        assertTrue(mManager.addDynamicShortcuts(list(
4116                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
4117        )));
4118
4119        setCaller(CALLING_PACKAGE_2, USER_0);
4120        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4121
4122        setCaller(CALLING_PACKAGE_3, USER_0);
4123        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4124
4125        mRunningUsers.put(USER_10, true);
4126
4127        setCaller(CALLING_PACKAGE_1, USER_10);
4128        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4129
4130        setCaller(CALLING_PACKAGE_2, USER_10);
4131        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4132
4133        setCaller(CALLING_PACKAGE_3, USER_10);
4134        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4135
4136        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4137        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4138        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4139        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4140        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4141        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4142
4143        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4144        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4145        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4146        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4147        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4148        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4149
4150                mService.mPackageMonitor.onReceive(getTestContext(),
4151                genPackageDataClear(CALLING_PACKAGE_1, USER_0));
4152
4153        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4154        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4155        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4156        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4157        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4158        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4159
4160        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4161        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4162        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4163        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4164        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4165        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4166
4167        mRunningUsers.put(USER_10, true);
4168
4169                mService.mPackageMonitor.onReceive(getTestContext(),
4170                genPackageDataClear(CALLING_PACKAGE_2, USER_10));
4171
4172        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4173        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4174        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4175        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4176        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4177        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4178
4179        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4180        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4181        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4182        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4183        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4184        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4185    }
4186
4187    public void testHandlePackageClearData_manifestRepublished() {
4188
4189        mRunningUsers.put(USER_10, true);
4190
4191        // Add two manifests and two dynamics.
4192        addManifestShortcutResource(
4193                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4194                R.xml.shortcut_2);
4195        updatePackageVersion(CALLING_PACKAGE_1, 1);
4196                mService.mPackageMonitor.onReceive(getTestContext(),
4197                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4198
4199        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4200            assertTrue(mManager.addDynamicShortcuts(list(
4201                    makeShortcut("s1"), makeShortcut("s2"))));
4202        });
4203        runWithCaller(LAUNCHER_1, USER_10, () -> {
4204            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
4205        });
4206
4207        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4208            assertWith(getCallerShortcuts())
4209                    .haveIds("ms1", "ms2", "s1", "s2")
4210                    .areAllEnabled()
4211
4212                    .selectPinned()
4213                    .haveIds("ms2", "s2");
4214        });
4215
4216        // Clear data
4217        mService.mPackageMonitor.onReceive(getTestContext(),
4218                genPackageDataClear(CALLING_PACKAGE_1, USER_10));
4219
4220        // Only manifest shortcuts will remain, and are no longer pinned.
4221        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4222            assertWith(getCallerShortcuts())
4223                    .haveIds("ms1", "ms2")
4224                    .areAllEnabled()
4225                    .areAllNotPinned();
4226        });
4227    }
4228
4229    public void testHandlePackageUpdate() throws Throwable {
4230        // Set up shortcuts and launchers.
4231
4232        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
4233        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4234                getTestContext().getResources(), R.drawable.black_32x32));
4235
4236        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4237            assertTrue(mManager.setDynamicShortcuts(list(
4238                    makeShortcut("s1"),
4239                    makeShortcutWithIcon("s2", res32x32),
4240                    makeShortcutWithIcon("s3", res32x32),
4241                    makeShortcutWithIcon("s4", bmp32x32))));
4242        });
4243        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4244            assertTrue(mManager.setDynamicShortcuts(list(
4245                    makeShortcut("s1"),
4246                    makeShortcutWithIcon("s2", bmp32x32))));
4247        });
4248        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4249            assertTrue(mManager.setDynamicShortcuts(list(
4250                    makeShortcutWithIcon("s1", res32x32))));
4251        });
4252
4253        mRunningUsers.put(USER_10, true);
4254
4255        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4256            assertTrue(mManager.setDynamicShortcuts(list(
4257                    makeShortcutWithIcon("s1", res32x32),
4258                    makeShortcutWithIcon("s2", res32x32))));
4259        });
4260        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
4261            assertTrue(mManager.setDynamicShortcuts(list(
4262                    makeShortcutWithIcon("s1", bmp32x32),
4263                    makeShortcutWithIcon("s2", bmp32x32))));
4264        });
4265
4266        LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
4267        LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
4268
4269        runWithCaller(LAUNCHER_1, USER_0, () -> {
4270            mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
4271        });
4272        runWithCaller(LAUNCHER_1, USER_10, () -> {
4273            mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
4274        });
4275
4276        mInjectedCurrentTimeMillis = START_TIME + 100;
4277
4278        ArgumentCaptor<List> shortcuts;
4279
4280        // Update the version info for package 1.
4281        reset(c0);
4282        reset(c10);
4283        updatePackageVersion(CALLING_PACKAGE_1, 1);
4284
4285        // Then send the broadcast, to only user-0.
4286        mService.mPackageMonitor.onReceive(getTestContext(),
4287                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4288
4289        waitOnMainThread();
4290
4291        // User-0 should get the notification.
4292        shortcuts = ArgumentCaptor.forClass(List.class);
4293        verify(c0).onShortcutsChanged(
4294                eq(CALLING_PACKAGE_1),
4295                shortcuts.capture(),
4296                eq(HANDLE_USER_0));
4297
4298        // User-10 shouldn't yet get the notification.
4299        verify(c10, times(0)).onShortcutsChanged(
4300                eq(CALLING_PACKAGE_1),
4301                any(List.class),
4302                any(UserHandle.class));
4303        assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
4304        assertEquals(START_TIME,
4305                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4306        assertEquals(START_TIME + 100,
4307                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4308        assertEquals(START_TIME + 100,
4309                findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
4310        assertEquals(START_TIME,
4311                findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
4312
4313        // Next, send an unlock event on user-10.  Now we scan packages on this user and send a
4314        // notification to the launcher.
4315        mInjectedCurrentTimeMillis = START_TIME + 200;
4316
4317        mRunningUsers.put(USER_10, true);
4318        mUnlockedUsers.put(USER_10, true);
4319
4320        reset(c0);
4321        reset(c10);
4322        setPackageLastUpdateTime(CALLING_PACKAGE_1, mInjectedCurrentTimeMillis);
4323        mService.handleUnlockUser(USER_10);
4324        mService.checkPackageChanges(USER_10);
4325
4326        waitOnMainThread();
4327
4328        shortcuts = ArgumentCaptor.forClass(List.class);
4329        verify(c0, times(0)).onShortcutsChanged(
4330                eq(CALLING_PACKAGE_1),
4331                any(List.class),
4332                any(UserHandle.class));
4333
4334        verify(c10).onShortcutsChanged(
4335                eq(CALLING_PACKAGE_1),
4336                shortcuts.capture(),
4337                eq(HANDLE_USER_10));
4338
4339        assertShortcutIds(shortcuts.getValue(), "s1", "s2");
4340        assertEquals(START_TIME + 200,
4341                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4342        assertEquals(START_TIME + 200,
4343                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4344
4345
4346        // Do the same thing for package 2, which doesn't have resource icons.
4347        mInjectedCurrentTimeMillis = START_TIME + 300;
4348
4349        reset(c0);
4350        reset(c10);
4351        updatePackageVersion(CALLING_PACKAGE_2, 10);
4352
4353        // Then send the broadcast, to only user-0.
4354        mService.mPackageMonitor.onReceive(getTestContext(),
4355                genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
4356
4357        waitOnMainThread();
4358
4359        verify(c0, times(0)).onShortcutsChanged(
4360                eq(CALLING_PACKAGE_1),
4361                any(List.class),
4362                any(UserHandle.class));
4363
4364        verify(c10, times(0)).onShortcutsChanged(
4365                eq(CALLING_PACKAGE_1),
4366                any(List.class),
4367                any(UserHandle.class));
4368
4369        // Do the same thing for package 3
4370        mInjectedCurrentTimeMillis = START_TIME + 400;
4371
4372        reset(c0);
4373        reset(c10);
4374        updatePackageVersion(CALLING_PACKAGE_3, 100);
4375
4376        // Then send the broadcast, to only user-0.
4377        mService.mPackageMonitor.onReceive(getTestContext(),
4378                genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
4379        mService.checkPackageChanges(USER_10);
4380
4381        waitOnMainThread();
4382
4383        shortcuts = ArgumentCaptor.forClass(List.class);
4384        verify(c0).onShortcutsChanged(
4385                eq(CALLING_PACKAGE_3),
4386                shortcuts.capture(),
4387                eq(HANDLE_USER_0));
4388
4389        // User 10 doesn't have package 3, so no callback.
4390        verify(c10, times(0)).onShortcutsChanged(
4391                eq(CALLING_PACKAGE_3),
4392                any(List.class),
4393                any(UserHandle.class));
4394
4395        assertShortcutIds(shortcuts.getValue(), "s1");
4396        assertEquals(START_TIME + 400,
4397                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4398    }
4399
4400    /**
4401     * Test the case where an updated app has resource IDs changed.
4402     */
4403    public void testHandlePackageUpdate_resIdChanged() throws Exception {
4404        final Icon icon1 = Icon.createWithResource(getTestContext(), /* res ID */ 1000);
4405        final Icon icon2 = Icon.createWithResource(getTestContext(), /* res ID */ 1001);
4406
4407        // Set up shortcuts.
4408        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4409            // Note resource strings are not officially supported (they're hidden), but
4410            // should work.
4411
4412            final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
4413                    .setId("s1")
4414                    .setActivity(makeComponent(ShortcutActivity.class))
4415                    .setIntent(new Intent(Intent.ACTION_VIEW))
4416                    .setIcon(icon1)
4417                    .setTitleResId(10000)
4418                    .setTextResId(10001)
4419                    .setDisabledMessageResId(10002)
4420                    .build();
4421
4422            final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
4423                    .setId("s2")
4424                    .setActivity(makeComponent(ShortcutActivity.class))
4425                    .setIntent(new Intent(Intent.ACTION_VIEW))
4426                    .setIcon(icon2)
4427                    .setTitleResId(20000)
4428                    .build();
4429
4430            assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
4431        });
4432
4433        // Verify.
4434        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4435            final ShortcutInfo s1 = getCallerShortcut("s1");
4436            final ShortcutInfo s2 = getCallerShortcut("s2");
4437
4438            assertEquals(1000, s1.getIconResourceId());
4439            assertEquals(10000, s1.getTitleResId());
4440            assertEquals(10001, s1.getTextResId());
4441            assertEquals(10002, s1.getDisabledMessageResourceId());
4442
4443            assertEquals(1001, s2.getIconResourceId());
4444            assertEquals(20000, s2.getTitleResId());
4445            assertEquals(0, s2.getTextResId());
4446            assertEquals(0, s2.getDisabledMessageResourceId());
4447        });
4448
4449        mService.saveDirtyInfo();
4450        initService();
4451
4452        // Set up the mock resources again, with an "adjustment".
4453        // When the package is updated, the service will fetch the updated res-IDs with res-names,
4454        // and the new IDs will have this offset.
4455        setUpAppResources(10);
4456
4457        // Update the package.
4458        updatePackageVersion(CALLING_PACKAGE_1, 1);
4459                mService.mPackageMonitor.onReceive(getTestContext(),
4460                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4461
4462        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4463            final ShortcutInfo s1 = getCallerShortcut("s1");
4464            final ShortcutInfo s2 = getCallerShortcut("s2");
4465
4466            assertEquals(1010, s1.getIconResourceId());
4467            assertEquals(10010, s1.getTitleResId());
4468            assertEquals(10011, s1.getTextResId());
4469            assertEquals(10012, s1.getDisabledMessageResourceId());
4470
4471            assertEquals(1011, s2.getIconResourceId());
4472            assertEquals(20010, s2.getTitleResId());
4473            assertEquals(0, s2.getTextResId());
4474            assertEquals(0, s2.getDisabledMessageResourceId());
4475        });
4476    }
4477
4478    public void testHandlePackageUpdate_systemAppUpdate() {
4479
4480        // Package1 is a system app.  Package 2 is not a system app, so it's not scanned
4481        // in this test at all.
4482        mSystemPackages.add(CALLING_PACKAGE_1);
4483
4484        // Initial state: no shortcuts.
4485        mService.checkPackageChanges(USER_0);
4486
4487        assertEquals(mInjectedCurrentTimeMillis,
4488                mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4489        assertEquals(mInjectedBuildFingerprint,
4490                mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4491
4492        // They have no shortcuts.
4493        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4494            assertWith(getCallerShortcuts())
4495                    .isEmpty();
4496        });
4497
4498        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4499            assertWith(getCallerShortcuts())
4500                    .isEmpty();
4501        });
4502
4503        // Next.
4504        // Update the packages -- now they have 1 manifest shortcut.
4505        // But checkPackageChanges() don't notice it, since their version code / timestamp haven't
4506        // changed.
4507        addManifestShortcutResource(
4508                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4509                R.xml.shortcut_1);
4510        addManifestShortcutResource(
4511                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4512                R.xml.shortcut_1);
4513        mInjectedCurrentTimeMillis += 1000;
4514        mService.checkPackageChanges(USER_0);
4515
4516        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4517            assertWith(getCallerShortcuts())
4518                    .isEmpty();
4519        });
4520        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4521            assertWith(getCallerShortcuts())
4522                    .isEmpty();
4523        });
4524
4525        // Next.
4526        // Update the build finger print.  All apps will be scanned now.
4527        mInjectedBuildFingerprint = "update1";
4528        mInjectedCurrentTimeMillis += 1000;
4529        mService.checkPackageChanges(USER_0);
4530
4531        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4532            assertWith(getCallerShortcuts())
4533                    .haveIds("ms1");
4534        });
4535        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4536            assertWith(getCallerShortcuts())
4537                    .haveIds("ms1");
4538        });
4539
4540        // Next.
4541        // Update manifest shortcuts.
4542        addManifestShortcutResource(
4543                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4544                R.xml.shortcut_2);
4545        addManifestShortcutResource(
4546                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4547                R.xml.shortcut_2);
4548        mInjectedCurrentTimeMillis += 1000;
4549        mService.checkPackageChanges(USER_0);
4550
4551        // Fingerprint hasn't changed, so there packages weren't scanned.
4552        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4553            assertWith(getCallerShortcuts())
4554                    .haveIds("ms1");
4555        });
4556        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4557            assertWith(getCallerShortcuts())
4558                    .haveIds("ms1");
4559        });
4560
4561        // Update the fingerprint.  CALLING_PACKAGE_1's version code hasn't changed, but we scan
4562        // all apps anyway.
4563        mInjectedBuildFingerprint = "update2";
4564        mInjectedCurrentTimeMillis += 1000;
4565        mService.checkPackageChanges(USER_0);
4566
4567        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4568            assertWith(getCallerShortcuts())
4569                    .haveIds("ms1", "ms2");
4570        });
4571        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4572            assertWith(getCallerShortcuts())
4573                    .haveIds("ms1", "ms2");
4574        });
4575
4576        // Make sure getLastAppScanTime / getLastAppScanOsFingerprint are persisted.
4577        initService();
4578        assertEquals(mInjectedCurrentTimeMillis,
4579                mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4580        assertEquals(mInjectedBuildFingerprint,
4581                mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4582    }
4583
4584    public void testHandlePackageChanged() {
4585        final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
4586        final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
4587
4588        addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
4589        addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
4590
4591        mRunningUsers.put(USER_10, true);
4592
4593        updatePackageVersion(CALLING_PACKAGE_1, 1);
4594                mService.mPackageMonitor.onReceive(getTestContext(),
4595                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4596
4597        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4598            assertTrue(mManager.addDynamicShortcuts(list(
4599                    makeShortcutWithActivity("s1", ACTIVITY1),
4600                    makeShortcutWithActivity("s2", ACTIVITY2)
4601            )));
4602        });
4603        runWithCaller(LAUNCHER_1, USER_10, () -> {
4604            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
4605        });
4606
4607        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4608            assertWith(getCallerShortcuts())
4609                    .haveIds("ms1", "ms1-alt", "s1", "s2")
4610                    .areAllEnabled()
4611
4612                    .selectPinned()
4613                    .haveIds("ms1-alt", "s2")
4614
4615                    .revertToOriginalList()
4616                    .selectByIds("ms1", "s1")
4617                    .areAllWithActivity(ACTIVITY1)
4618
4619                    .revertToOriginalList()
4620                    .selectByIds("ms1-alt", "s2")
4621                    .areAllWithActivity(ACTIVITY2)
4622                    ;
4623        });
4624
4625        // First, no changes.
4626                mService.mPackageMonitor.onReceive(getTestContext(),
4627                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4628
4629        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4630            assertWith(getCallerShortcuts())
4631                    .haveIds("ms1", "ms1-alt", "s1", "s2")
4632                    .areAllEnabled()
4633
4634                    .selectPinned()
4635                    .haveIds("ms1-alt", "s2")
4636
4637                    .revertToOriginalList()
4638                    .selectByIds("ms1", "s1")
4639                    .areAllWithActivity(ACTIVITY1)
4640
4641                    .revertToOriginalList()
4642                    .selectByIds("ms1-alt", "s2")
4643                    .areAllWithActivity(ACTIVITY2)
4644            ;
4645        });
4646
4647        // Disable activity 1
4648        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
4649                mService.mPackageMonitor.onReceive(getTestContext(),
4650                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4651
4652        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4653            assertWith(getCallerShortcuts())
4654                    .haveIds("ms1-alt", "s2")
4655                    .areAllEnabled()
4656
4657                    .selectPinned()
4658                    .haveIds("ms1-alt", "s2")
4659
4660                    .revertToOriginalList()
4661                    .selectByIds("ms1-alt", "s2")
4662                    .areAllWithActivity(ACTIVITY2)
4663            ;
4664        });
4665
4666        // Re-enable activity 1.
4667        // Manifest shortcuts will be re-published, but dynamic ones are not.
4668        mEnabledActivityChecker = (activity, userId) -> true;
4669                mService.mPackageMonitor.onReceive(getTestContext(),
4670                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4671
4672        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4673            assertWith(getCallerShortcuts())
4674                    .haveIds("ms1", "ms1-alt", "s2")
4675                    .areAllEnabled()
4676
4677                    .selectPinned()
4678                    .haveIds("ms1-alt", "s2")
4679
4680                    .revertToOriginalList()
4681                    .selectByIds("ms1")
4682                    .areAllWithActivity(ACTIVITY1)
4683
4684                    .revertToOriginalList()
4685                    .selectByIds("ms1-alt", "s2")
4686                    .areAllWithActivity(ACTIVITY2)
4687                    ;
4688        });
4689
4690        // Disable activity 2
4691        // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
4692        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
4693                mService.mPackageMonitor.onReceive(getTestContext(),
4694                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4695
4696        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4697            assertWith(getCallerShortcuts())
4698                    .haveIds("ms1", "ms1-alt", "s2")
4699
4700                    .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
4701
4702                    .selectPinned()
4703                    .haveIds("ms1-alt", "s2")
4704                    .areAllDisabled()
4705
4706                    .revertToOriginalList()
4707                    .selectByIds("ms1")
4708                    .areAllWithActivity(ACTIVITY1)
4709                    .areAllEnabled()
4710            ;
4711        });
4712    }
4713
4714    public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
4715        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4716            assertTrue(mManager.setDynamicShortcuts(list(
4717                    makeShortcutWithActivity("s1a",
4718                            new ComponentName(getCallingPackage(), "act1")),
4719                    makeShortcutWithActivity("s1b",
4720                            new ComponentName(getCallingPackage(), "act1")),
4721                    makeShortcutWithActivity("s2a",
4722                            new ComponentName(getCallingPackage(), "act2")),
4723                    makeShortcutWithActivity("s2b",
4724                            new ComponentName(getCallingPackage(), "act2")),
4725                    makeShortcutWithActivity("s3a",
4726                            new ComponentName(getCallingPackage(), "act3")),
4727                    makeShortcutWithActivity("s3b",
4728                            new ComponentName(getCallingPackage(), "act3"))
4729            )));
4730            assertWith(getCallerShortcuts())
4731                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4732                    .areAllDynamic();
4733        });
4734        runWithCaller(LAUNCHER_1, USER_0, () -> {
4735            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4736                    list("s1b", "s2b", "s3b"),
4737                    HANDLE_USER_0);
4738        });
4739        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4740            assertWith(getCallerShortcuts())
4741                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4742                    .areAllDynamic()
4743
4744                    .selectByIds("s1b", "s2b", "s3b")
4745                    .areAllPinned();
4746        });
4747
4748        // Update the app and act2 and act3 are no longer main.
4749        mMainActivityChecker = (activity, userId) -> {
4750            return activity.getClassName().equals("act1");
4751        };
4752
4753        setCaller(LAUNCHER_1, USER_0);
4754        assertForLauncherCallback(mLauncherApps, () -> {
4755            updatePackageVersion(CALLING_PACKAGE_1, 1);
4756                    mService.mPackageMonitor.onReceive(getTestContext(),
4757                    genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4758        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
4759                // Make sure the launcher gets callbacks.
4760                .haveIds("s1a", "s1b", "s2b", "s3b")
4761                .areAllWithKeyFieldsOnly();
4762
4763        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4764            // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
4765            // disabled.
4766            assertWith(getCallerShortcuts())
4767                    .haveIds("s1a", "s1b", "s2b", "s3b")
4768
4769                    .selectByIds("s1a", "s1b")
4770                    .areAllDynamic()
4771                    .areAllEnabled()
4772
4773                    .revertToOriginalList()
4774                    .selectByIds("s2b", "s3b")
4775                    .areAllNotDynamic()
4776                    .areAllDisabled()
4777                    .areAllPinned()
4778                    ;
4779        });
4780    }
4781
4782    protected void prepareForBackupTest() {
4783
4784        prepareCrossProfileDataSet();
4785
4786        backupAndRestore();
4787    }
4788
4789    /**
4790     * Make sure the backup data doesn't have the following information:
4791     * - Launchers on other users.
4792     * - Non-backup app information.
4793     *
4794     * But restores all other infomation.
4795     *
4796     * It also omits the following pieces of information, but that's tested in
4797     * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
4798     * - Unpinned dynamic shortcuts
4799     * - Bitmaps
4800     */
4801    public void testBackupAndRestore() {
4802        prepareForBackupTest();
4803
4804        checkBackupAndRestore_success();
4805    }
4806
4807    public void testBackupAndRestore_backupRestoreTwice() {
4808        prepareForBackupTest();
4809
4810        // Note doing a backup & restore again here shouldn't affect the result.
4811        dumpsysOnLogcat("Before second backup");
4812
4813        backupAndRestore();
4814
4815        dumpsysOnLogcat("After second backup");
4816
4817        checkBackupAndRestore_success();
4818    }
4819
4820    public void testBackupAndRestore_backupRestoreMultiple() {
4821        prepareForBackupTest();
4822
4823        // Note doing a backup & restore again here shouldn't affect the result.
4824        backupAndRestore();
4825
4826        // This also shouldn't affect the result.
4827        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4828            assertTrue(mManager.setDynamicShortcuts(list(
4829                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
4830                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
4831        });
4832
4833        backupAndRestore();
4834
4835        checkBackupAndRestore_success();
4836    }
4837
4838    public void testBackupAndRestore_restoreToNewVersion() {
4839        prepareForBackupTest();
4840
4841        // Note doing a backup & restore again here shouldn't affect the result.
4842        backupAndRestore();
4843
4844        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
4845        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
4846
4847        checkBackupAndRestore_success();
4848    }
4849
4850    public void testBackupAndRestore_restoreToSuperSetSignatures() {
4851        prepareForBackupTest();
4852
4853        // Note doing a backup & restore again here shouldn't affect the result.
4854        backupAndRestore();
4855
4856        // Change package signatures.
4857        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
4858        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
4859
4860        checkBackupAndRestore_success();
4861    }
4862
4863    protected void checkBackupAndRestore_success() {
4864        // Make sure non-system user is not restored.
4865        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
4866        assertEquals(0, userP0.getAllPackagesForTest().size());
4867        assertEquals(0, userP0.getAllLaunchersForTest().size());
4868
4869        // Make sure only "allowBackup" apps are restored, and are shadow.
4870        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
4871        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
4872        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
4873        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4874                PackageWithUser.of(USER_0, LAUNCHER_1)));
4875        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4876                PackageWithUser.of(USER_0, LAUNCHER_2)));
4877
4878        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
4879        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
4880        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
4881
4882        installPackage(USER_0, CALLING_PACKAGE_1);
4883        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4884            assertWith(getCallerVisibleShortcuts())
4885                    .selectDynamic()
4886                    .isEmpty()
4887
4888                    .revertToOriginalList()
4889                    .selectPinned()
4890                    .haveIds("s1", "s2");
4891        });
4892
4893        installPackage(USER_0, LAUNCHER_1);
4894        runWithCaller(LAUNCHER_1, USER_0, () -> {
4895            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4896                    .areAllPinned()
4897                    .haveIds("s1");
4898
4899            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4900                    .isEmpty();
4901
4902            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4903                    .isEmpty();
4904
4905            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4906                    .isEmpty();
4907        });
4908
4909        installPackage(USER_0, CALLING_PACKAGE_2);
4910        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4911            assertWith(getCallerVisibleShortcuts())
4912                    .selectDynamic()
4913                    .isEmpty()
4914
4915                    .revertToOriginalList()
4916                    .selectPinned()
4917                    .haveIds("s1", "s2", "s3");
4918        });
4919
4920        runWithCaller(LAUNCHER_1, USER_0, () -> {
4921            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4922                    .areAllPinned()
4923                    .haveIds("s1");
4924
4925            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4926                    .areAllPinned()
4927                    .haveIds("s1", "s2");
4928
4929            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4930                    .isEmpty();
4931
4932            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4933                    .isEmpty();
4934        });
4935
4936        // 3 shouldn't be backed up, so no pinned shortcuts.
4937        installPackage(USER_0, CALLING_PACKAGE_3);
4938        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4939            assertWith(getCallerVisibleShortcuts())
4940                    .isEmpty();
4941        });
4942
4943        // Launcher on a different profile shouldn't be restored.
4944        runWithCaller(LAUNCHER_1, USER_P0, () -> {
4945            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4946                    .isEmpty();
4947            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4948                    .isEmpty();
4949            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4950                    .isEmpty();
4951        });
4952
4953        // Package on a different profile, no restore.
4954        installPackage(USER_P0, CALLING_PACKAGE_1);
4955        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
4956            assertWith(getCallerVisibleShortcuts())
4957                    .isEmpty();
4958        });
4959
4960        // Restore launcher 2 on user 0.
4961        installPackage(USER_0, LAUNCHER_2);
4962        runWithCaller(LAUNCHER_2, USER_0, () -> {
4963            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4964                    .areAllPinned()
4965                    .haveIds("s2");
4966
4967            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4968                    .areAllPinned()
4969                    .haveIds("s2", "s3");
4970
4971            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4972                    .isEmpty();
4973
4974            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4975                    .isEmpty();
4976        });
4977
4978
4979        // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
4980        // make sure they still have the same result.
4981        installPackage(USER_0, CALLING_PACKAGE_1);
4982        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4983            assertWith(getCallerVisibleShortcuts())
4984                    .areAllPinned()
4985                    .haveIds("s1", "s2");
4986        });
4987
4988        installPackage(USER_0, LAUNCHER_1);
4989        runWithCaller(LAUNCHER_1, USER_0, () -> {
4990            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4991                    .areAllPinned()
4992                    .haveIds("s1");
4993
4994            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4995                    .areAllPinned()
4996                    .haveIds("s1", "s2");
4997
4998            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4999                    .isEmpty();
5000
5001            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5002                    .isEmpty();
5003        });
5004
5005        installPackage(USER_0, CALLING_PACKAGE_2);
5006        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5007            assertWith(getCallerVisibleShortcuts())
5008                    .areAllPinned()
5009                    .haveIds("s1", "s2", "s3");
5010        });
5011    }
5012
5013    public void testBackupAndRestore_publisherLowerVersion() {
5014        prepareForBackupTest();
5015
5016        // Note doing a backup & restore again here shouldn't affect the result.
5017        backupAndRestore();
5018
5019        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
5020
5021        checkBackupAndRestore_publisherNotRestored();
5022    }
5023
5024    public void testBackupAndRestore_publisherWrongSignature() {
5025        prepareForBackupTest();
5026
5027        // Note doing a backup & restore again here shouldn't affect the result.
5028        backupAndRestore();
5029
5030        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
5031
5032        checkBackupAndRestore_publisherNotRestored();
5033    }
5034
5035    public void testBackupAndRestore_publisherNoLongerBackupTarget() {
5036        prepareForBackupTest();
5037
5038        // Note doing a backup & restore again here shouldn't affect the result.
5039        backupAndRestore();
5040
5041        updatePackageInfo(CALLING_PACKAGE_1,
5042                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5043
5044        checkBackupAndRestore_publisherNotRestored();
5045    }
5046
5047    protected void checkBackupAndRestore_publisherNotRestored() {
5048        installPackage(USER_0, CALLING_PACKAGE_1);
5049        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5050            assertEquals(0, mManager.getDynamicShortcuts().size());
5051            assertEquals(0, mManager.getPinnedShortcuts().size());
5052        });
5053        assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
5054                .getPackageInfo().isShadow());
5055
5056
5057        installPackage(USER_0, CALLING_PACKAGE_2);
5058        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5059            assertEquals(0, mManager.getDynamicShortcuts().size());
5060            assertShortcutIds(assertAllPinned(
5061                    mManager.getPinnedShortcuts()),
5062                    "s1", "s2", "s3");
5063        });
5064        assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, USER_0)
5065                .getPackageInfo().isShadow());
5066
5067        installPackage(USER_0, LAUNCHER_1);
5068        runWithCaller(LAUNCHER_1, USER_0, () -> {
5069            assertShortcutIds(assertAllPinned(
5070                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5071                    /* empty */);
5072            assertShortcutIds(assertAllPinned(
5073                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5074                    "s1", "s2");
5075            assertShortcutIds(assertAllPinned(
5076                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5077                    /* empty */);
5078        });
5079        installPackage(USER_0, LAUNCHER_2);
5080        runWithCaller(LAUNCHER_2, USER_0, () -> {
5081            assertShortcutIds(assertAllPinned(
5082                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5083                    /* empty */);
5084            assertShortcutIds(assertAllPinned(
5085                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5086                    "s2", "s3");
5087            assertShortcutIds(assertAllPinned(
5088                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5089                    /* empty */);
5090        });
5091
5092        installPackage(USER_0, CALLING_PACKAGE_3);
5093        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5094            assertEquals(0, mManager.getDynamicShortcuts().size());
5095            assertEquals(0, mManager.getPinnedShortcuts().size());
5096        });
5097
5098        runWithCaller(LAUNCHER_1, USER_0, () -> {
5099            assertShortcutIds(assertAllPinned(
5100                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5101                    /* empty */);
5102            assertShortcutIds(assertAllPinned(
5103                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5104                    "s1", "s2");
5105            assertShortcutIds(assertAllPinned(
5106                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5107                    /* empty */);
5108        });
5109        runWithCaller(LAUNCHER_2, USER_0, () -> {
5110            assertShortcutIds(assertAllPinned(
5111                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5112                    /* empty */);
5113            assertShortcutIds(assertAllPinned(
5114                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5115                    "s2", "s3");
5116            assertShortcutIds(assertAllPinned(
5117                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5118                    /* empty */);
5119        });
5120    }
5121
5122    public void testBackupAndRestore_launcherLowerVersion() {
5123        prepareForBackupTest();
5124
5125        // Note doing a backup & restore again here shouldn't affect the result.
5126        backupAndRestore();
5127
5128        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
5129
5130        checkBackupAndRestore_launcherNotRestored();
5131    }
5132
5133    public void testBackupAndRestore_launcherWrongSignature() {
5134        prepareForBackupTest();
5135
5136        // Note doing a backup & restore again here shouldn't affect the result.
5137        backupAndRestore();
5138
5139        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
5140
5141        checkBackupAndRestore_launcherNotRestored();
5142    }
5143
5144    public void testBackupAndRestore_launcherNoLongerBackupTarget() {
5145        prepareForBackupTest();
5146
5147        // Note doing a backup & restore again here shouldn't affect the result.
5148        backupAndRestore();
5149
5150        updatePackageInfo(LAUNCHER_1,
5151                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5152
5153        checkBackupAndRestore_launcherNotRestored();
5154    }
5155
5156    protected void checkBackupAndRestore_launcherNotRestored() {
5157        installPackage(USER_0, CALLING_PACKAGE_1);
5158        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5159            assertEquals(0, mManager.getDynamicShortcuts().size());
5160
5161            // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
5162            assertShortcutIds(assertAllPinned(
5163                    mManager.getPinnedShortcuts()),
5164                    "s1", "s2");
5165        });
5166
5167        installPackage(USER_0, CALLING_PACKAGE_2);
5168        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5169            assertEquals(0, mManager.getDynamicShortcuts().size());
5170            assertShortcutIds(assertAllPinned(
5171                    mManager.getPinnedShortcuts()),
5172                    "s1", "s2", "s3");
5173        });
5174
5175        // Now we try to restore launcher 1.  Then we realize it's not restorable, so L1 has no pinned
5176        // shortcuts.
5177        installPackage(USER_0, LAUNCHER_1);
5178        runWithCaller(LAUNCHER_1, USER_0, () -> {
5179            assertShortcutIds(assertAllPinned(
5180                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5181                    /* empty */);
5182            assertShortcutIds(assertAllPinned(
5183                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5184                    /* empty */);
5185            assertShortcutIds(assertAllPinned(
5186                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5187                    /* empty */);
5188        });
5189        assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
5190                .getPackageInfo().isShadow());
5191
5192        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5193            assertEquals(0, mManager.getDynamicShortcuts().size());
5194
5195            // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
5196            assertShortcutIds(assertAllPinned(
5197                    mManager.getPinnedShortcuts()),
5198                    "s2");
5199        });
5200
5201        installPackage(USER_0, LAUNCHER_2);
5202        runWithCaller(LAUNCHER_2, USER_0, () -> {
5203            assertShortcutIds(assertAllPinned(
5204                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5205                    "s2");
5206            assertShortcutIds(assertAllPinned(
5207                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5208                    "s2", "s3");
5209            assertShortcutIds(assertAllPinned(
5210                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5211                    /* empty */);
5212        });
5213        assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_2, USER_0)
5214                .getPackageInfo().isShadow());
5215
5216        installPackage(USER_0, CALLING_PACKAGE_3);
5217        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5218            assertEquals(0, mManager.getDynamicShortcuts().size());
5219            assertEquals(0, mManager.getPinnedShortcuts().size());
5220        });
5221
5222        runWithCaller(LAUNCHER_1, USER_0, () -> {
5223            assertShortcutIds(assertAllPinned(
5224                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5225                    /* empty */);
5226            assertShortcutIds(assertAllPinned(
5227                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5228                    /* empty */);
5229            assertShortcutIds(assertAllPinned(
5230                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5231                    /* empty */);
5232        });
5233        runWithCaller(LAUNCHER_2, USER_0, () -> {
5234            assertShortcutIds(assertAllPinned(
5235                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5236                    "s2");
5237            assertShortcutIds(assertAllPinned(
5238                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5239                    "s2", "s3");
5240            assertShortcutIds(assertAllPinned(
5241                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5242                    /* empty */);
5243        });
5244    }
5245
5246    public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
5247        prepareForBackupTest();
5248
5249        // Note doing a backup & restore again here shouldn't affect the result.
5250        backupAndRestore();
5251
5252        updatePackageInfo(CALLING_PACKAGE_1,
5253                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5254
5255        updatePackageInfo(LAUNCHER_1,
5256                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5257
5258        checkBackupAndRestore_publisherAndLauncherNotRestored();
5259    }
5260
5261    protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
5262        installPackage(USER_0, CALLING_PACKAGE_1);
5263        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5264            assertEquals(0, mManager.getDynamicShortcuts().size());
5265            assertEquals(0, mManager.getPinnedShortcuts().size());
5266        });
5267
5268        installPackage(USER_0, CALLING_PACKAGE_2);
5269        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5270            assertEquals(0, mManager.getDynamicShortcuts().size());
5271            assertShortcutIds(assertAllPinned(
5272                    mManager.getPinnedShortcuts()),
5273                    "s1", "s2", "s3");
5274        });
5275
5276        installPackage(USER_0, LAUNCHER_1);
5277        runWithCaller(LAUNCHER_1, USER_0, () -> {
5278            assertShortcutIds(assertAllPinned(
5279                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5280                    /* empty */);
5281            assertShortcutIds(assertAllPinned(
5282                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5283                    /* empty */);
5284            assertShortcutIds(assertAllPinned(
5285                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5286                    /* empty */);
5287        });
5288        installPackage(USER_0, LAUNCHER_2);
5289        runWithCaller(LAUNCHER_2, USER_0, () -> {
5290            assertShortcutIds(assertAllPinned(
5291                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5292                    /* empty */);
5293            assertShortcutIds(assertAllPinned(
5294                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5295                    "s2", "s3");
5296            assertShortcutIds(assertAllPinned(
5297                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5298                    /* empty */);
5299        });
5300
5301        // Because launcher 1 wasn't restored, "s1" is no longer pinned.
5302        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5303            assertEquals(0, mManager.getDynamicShortcuts().size());
5304            assertShortcutIds(assertAllPinned(
5305                    mManager.getPinnedShortcuts()),
5306                    "s2", "s3");
5307        });
5308
5309        installPackage(USER_0, CALLING_PACKAGE_3);
5310        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5311            assertEquals(0, mManager.getDynamicShortcuts().size());
5312            assertEquals(0, mManager.getPinnedShortcuts().size());
5313        });
5314
5315        runWithCaller(LAUNCHER_1, USER_0, () -> {
5316            assertShortcutIds(assertAllPinned(
5317                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5318                    /* empty */);
5319            assertShortcutIds(assertAllPinned(
5320                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5321                    /* empty */);
5322            assertShortcutIds(assertAllPinned(
5323                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5324                    /* empty */);
5325        });
5326        runWithCaller(LAUNCHER_2, USER_0, () -> {
5327            assertShortcutIds(assertAllPinned(
5328                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5329                    /* empty */);
5330            assertShortcutIds(assertAllPinned(
5331                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5332                    "s2", "s3");
5333            assertShortcutIds(assertAllPinned(
5334                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5335                    /* empty */);
5336        });
5337    }
5338
5339    public void testBackupAndRestore_disabled() {
5340        prepareCrossProfileDataSet();
5341
5342        // Before doing backup & restore, disable s1.
5343        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5344            mManager.disableShortcuts(list("s1"));
5345        });
5346
5347        backupAndRestore();
5348
5349        // Below is copied from checkBackupAndRestore_success.
5350
5351        // Make sure non-system user is not restored.
5352        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
5353        assertEquals(0, userP0.getAllPackagesForTest().size());
5354        assertEquals(0, userP0.getAllLaunchersForTest().size());
5355
5356        // Make sure only "allowBackup" apps are restored, and are shadow.
5357        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
5358        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5359        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
5360        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5361                PackageWithUser.of(USER_0, LAUNCHER_1)));
5362        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5363                PackageWithUser.of(USER_0, LAUNCHER_2)));
5364
5365        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
5366        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5367        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
5368
5369        installPackage(USER_0, CALLING_PACKAGE_1);
5370        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5371            assertWith(getCallerVisibleShortcuts())
5372                    .areAllEnabled() // disabled shortcuts shouldn't be restored.
5373
5374                    .selectDynamic()
5375                    .isEmpty()
5376
5377                    .revertToOriginalList()
5378                    .selectPinned()
5379                    // s1 is not restored.
5380                    .haveIds("s2");
5381        });
5382
5383        installPackage(USER_0, LAUNCHER_1);
5384        runWithCaller(LAUNCHER_1, USER_0, () -> {
5385            // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
5386            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5387                    .isEmpty();
5388
5389            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5390                    .isEmpty();
5391
5392            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5393                    .isEmpty();
5394
5395            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5396                    .isEmpty();
5397        });
5398    }
5399
5400
5401    public void testBackupAndRestore_manifestRePublished() {
5402        // Publish two manifest shortcuts.
5403        addManifestShortcutResource(
5404                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5405                R.xml.shortcut_2);
5406        updatePackageVersion(CALLING_PACKAGE_1, 1);
5407        mService.mPackageMonitor.onReceive(mServiceContext,
5408                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5409
5410        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5411            assertTrue(mManager.setDynamicShortcuts(list(
5412                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5413        });
5414
5415        // Pin from launcher 1.
5416        runWithCaller(LAUNCHER_1, USER_0, () -> {
5417            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5418                    list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5419        });
5420
5421        // Update and now ms2 is gone -> disabled.
5422        addManifestShortcutResource(
5423                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5424                R.xml.shortcut_1);
5425        updatePackageVersion(CALLING_PACKAGE_1, 1);
5426        mService.mPackageMonitor.onReceive(mServiceContext,
5427                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5428
5429        // Make sure the manifest shortcuts have been published.
5430        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5431            assertWith(getCallerShortcuts())
5432                    .selectManifest()
5433                    .haveIds("ms1")
5434
5435                    .revertToOriginalList()
5436                    .selectDynamic()
5437                    .haveIds("s1", "s2", "s3")
5438
5439                    .revertToOriginalList()
5440                    .selectPinned()
5441                    .haveIds("ms1", "ms2", "s1", "s2")
5442
5443                    .revertToOriginalList()
5444                    .selectByIds("ms1")
5445                    .areAllManifest()
5446                    .areAllEnabled()
5447
5448                    .revertToOriginalList()
5449                    .selectByIds("ms2")
5450                    .areAllNotManifest()
5451                    .areAllDisabled();
5452        });
5453
5454        backupAndRestore();
5455
5456        // When re-installing the app, the manifest shortcut should be re-published.
5457        mService.mPackageMonitor.onReceive(mServiceContext,
5458                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5459        mService.mPackageMonitor.onReceive(mServiceContext,
5460                genPackageAddIntent(LAUNCHER_1, USER_0));
5461
5462        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5463            assertWith(getCallerVisibleShortcuts())
5464                    .selectPinned()
5465                    // ms2 was disabled, so not restored.
5466                    .haveIds("ms1", "s1", "s2")
5467                    .areAllEnabled()
5468
5469                    .revertToOriginalList()
5470                    .selectByIds("ms1")
5471                    .areAllManifest()
5472
5473                    .revertToOriginalList()
5474                    .selectByIds("s1", "s2")
5475                    .areAllNotDynamic()
5476                    ;
5477        });
5478    }
5479
5480    /**
5481     * It's the case with preintalled apps -- when applyRestore() is called, the system
5482     * apps are already installed, so manifest shortcuts need to be re-published.
5483     *
5484     * Also, when a restore target app is already installed, and
5485     * - if it has allowBackup=true, we'll restore normally, so all existing shortcuts will be
5486     * replaced. (but manifest shortcuts will be re-published anyway.)  We log a warning on
5487     * logcat.
5488     * - if it has allowBackup=false, we don't touch any of the existing shortcuts.
5489     */
5490    public void testBackupAndRestore_appAlreadyInstalledWhenRestored() {
5491        // Pre-backup.  Same as testBackupAndRestore_manifestRePublished().
5492
5493        // Publish two manifest shortcuts.
5494        addManifestShortcutResource(
5495                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5496                R.xml.shortcut_2);
5497        updatePackageVersion(CALLING_PACKAGE_1, 1);
5498        mService.mPackageMonitor.onReceive(mServiceContext,
5499                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5500
5501        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5502            assertTrue(mManager.setDynamicShortcuts(list(
5503                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5504        });
5505
5506        // Pin from launcher 1.
5507        runWithCaller(LAUNCHER_1, USER_0, () -> {
5508            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5509                    list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5510        });
5511
5512        // Update and now ms2 is gone -> disabled.
5513        addManifestShortcutResource(
5514                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5515                R.xml.shortcut_1);
5516        updatePackageVersion(CALLING_PACKAGE_1, 1);
5517        mService.mPackageMonitor.onReceive(mServiceContext,
5518                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5519
5520        // Set up shortcuts for package 3, which won't be backed up / restored.
5521        addManifestShortcutResource(
5522                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5523                R.xml.shortcut_1);
5524        updatePackageVersion(CALLING_PACKAGE_3, 1);
5525        mService.mPackageMonitor.onReceive(mServiceContext,
5526                genPackageAddIntent(CALLING_PACKAGE_3, USER_0));
5527
5528        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5529            assertTrue(getManager().setDynamicShortcuts(list(
5530                    makeShortcut("s1"))));
5531        });
5532
5533        // Make sure the manifest shortcuts have been published.
5534        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5535            assertWith(getCallerShortcuts())
5536                    .selectManifest()
5537                    .haveIds("ms1")
5538
5539                    .revertToOriginalList()
5540                    .selectDynamic()
5541                    .haveIds("s1", "s2", "s3")
5542
5543                    .revertToOriginalList()
5544                    .selectPinned()
5545                    .haveIds("ms1", "ms2", "s1", "s2")
5546
5547                    .revertToOriginalList()
5548                    .selectByIds("ms1")
5549                    .areAllManifest()
5550                    .areAllEnabled()
5551
5552                    .revertToOriginalList()
5553                    .selectByIds("ms2")
5554                    .areAllNotManifest()
5555                    .areAllDisabled();
5556        });
5557
5558        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5559            assertWith(getCallerShortcuts())
5560                    .haveIds("s1", "ms1");
5561        });
5562
5563        // Backup and *without restarting the service, just call applyRestore()*.
5564        {
5565            int prevUid = mInjectedCallingUid;
5566            mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
5567
5568            dumpsysOnLogcat("Before backup");
5569
5570            final byte[] payload = mService.getBackupPayload(USER_0);
5571            if (ENABLE_DUMP) {
5572                final String xml = new String(payload);
5573                Log.v(TAG, "Backup payload:");
5574                for (String line : xml.split("\n")) {
5575                    Log.v(TAG, line);
5576                }
5577            }
5578            mService.applyRestore(payload, USER_0);
5579
5580            dumpsysOnLogcat("After restore");
5581
5582            mInjectedCallingUid = prevUid;
5583        }
5584
5585        // The check is also the same as testBackupAndRestore_manifestRePublished().
5586        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5587            assertWith(getCallerVisibleShortcuts())
5588                    .selectPinned()
5589                    // ms2 was disabled, so not restored.
5590                    .haveIds("ms1", "s1", "s2")
5591                    .areAllEnabled()
5592
5593                    .revertToOriginalList()
5594                    .selectByIds("ms1")
5595                    .areAllManifest()
5596
5597                    .revertToOriginalList()
5598                    .selectByIds("s1", "s2")
5599                    .areAllNotDynamic()
5600            ;
5601        });
5602
5603        // Package 3 still has the same shortcuts.
5604        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5605            assertWith(getCallerShortcuts())
5606                    .haveIds("s1", "ms1");
5607        });
5608    }
5609
5610    public void testSaveAndLoad_crossProfile() {
5611        prepareCrossProfileDataSet();
5612
5613        dumpsysOnLogcat("Before save & load");
5614
5615        mService.saveDirtyInfo();
5616        initService();
5617
5618        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5619            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5620                    "s1", "s2", "s3");
5621            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5622                    "s1", "s2", "s3", "s4");
5623        });
5624        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5625            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5626                    "s1", "s2", "s3");
5627            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5628                    "s1", "s2", "s3", "s4", "s5");
5629        });
5630        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5631            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5632                    "s1", "s2", "s3");
5633            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5634                    "s1", "s2", "s3", "s4", "s5", "s6");
5635        });
5636        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
5637            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5638                    /* empty */);
5639            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5640                    /* empty */);
5641        });
5642        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
5643            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5644                    "s1", "s2", "s3");
5645            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5646                    "s1", "s2", "s3", "s4", "s5", "s6");
5647        });
5648        runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
5649            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5650                    /* empty */);
5651            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5652                    /* empty */);
5653        });
5654        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5655            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5656                    "x1", "x2", "x3");
5657            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5658                    "x4", "x5");
5659        });
5660        runWithCaller(LAUNCHER_1, USER_0, () -> {
5661            assertShortcutIds(
5662                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5663                    "s1");
5664            assertShortcutIds(
5665                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5666                    "s1", "s2");
5667            assertShortcutIds(
5668                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5669                    "s1", "s2", "s3");
5670            assertShortcutIds(
5671                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5672                    /* empty */);
5673            assertShortcutIds(
5674                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5675                    "s1", "s4");
5676            assertShortcutIds(
5677                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5678                    /* empty */);
5679            assertExpectException(
5680                    SecurityException.class, "", () -> {
5681                        mLauncherApps.getShortcuts(
5682                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5683                    });
5684        });
5685        runWithCaller(LAUNCHER_2, USER_0, () -> {
5686            assertShortcutIds(
5687                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5688                    "s2");
5689            assertShortcutIds(
5690                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5691                    "s2", "s3");
5692            assertShortcutIds(
5693                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5694                    "s2", "s3", "s4");
5695            assertShortcutIds(
5696                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5697                    /* empty */);
5698            assertShortcutIds(
5699                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5700                    "s2", "s5");
5701            assertShortcutIds(
5702                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5703                    /* empty */);
5704        });
5705        runWithCaller(LAUNCHER_3, USER_0, () -> {
5706            assertShortcutIds(
5707                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5708                    "s3");
5709            assertShortcutIds(
5710                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5711                    "s3", "s4");
5712            assertShortcutIds(
5713                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5714                    "s3", "s4", "s5");
5715            assertShortcutIds(
5716                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5717                    /* empty */);
5718            assertShortcutIds(
5719                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5720                    "s3", "s6");
5721            assertShortcutIds(
5722                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5723                    /* empty */);
5724        });
5725        runWithCaller(LAUNCHER_4, USER_0, () -> {
5726            assertShortcutIds(
5727                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
5728                    /* empty */);
5729            assertShortcutIds(
5730                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
5731                    /* empty */);
5732            assertShortcutIds(
5733                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
5734                    /* empty */);
5735            assertShortcutIds(
5736                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5737                    /* empty */);
5738            assertShortcutIds(
5739                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
5740                    /* empty */);
5741            assertShortcutIds(
5742                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5743                    /* empty */);
5744        });
5745        runWithCaller(LAUNCHER_1, USER_P0, () -> {
5746            assertShortcutIds(
5747                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5748                    "s3", "s4");
5749            assertShortcutIds(
5750                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5751                    "s3", "s4", "s5");
5752            assertShortcutIds(
5753                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5754                    "s3", "s4", "s5", "s6");
5755            assertShortcutIds(
5756                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5757                    "s1", "s4");
5758            assertExpectException(
5759                    SecurityException.class, "unrelated profile", () -> {
5760                        mLauncherApps.getShortcuts(
5761                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5762                    });
5763        });
5764        runWithCaller(LAUNCHER_1, USER_10, () -> {
5765            assertShortcutIds(
5766                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
5767                    "x4", "x5");
5768            assertShortcutIds(
5769                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
5770                    /* empty */);
5771            assertShortcutIds(
5772                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
5773                    /* empty */);
5774            assertExpectException(
5775                    SecurityException.class, "unrelated profile", () -> {
5776                        mLauncherApps.getShortcuts(
5777                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
5778                    });
5779            assertExpectException(
5780                    SecurityException.class, "unrelated profile", () -> {
5781                        mLauncherApps.getShortcuts(
5782                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
5783                    });
5784        });
5785        // Check the user-IDs.
5786        assertEquals(USER_0,
5787                mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
5788                        .getOwnerUserId());
5789        assertEquals(USER_0,
5790                mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
5791                        .getPackageUserId());
5792        assertEquals(USER_P0,
5793                mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
5794                        .getOwnerUserId());
5795        assertEquals(USER_P0,
5796                mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
5797                        .getPackageUserId());
5798
5799        assertEquals(USER_0,
5800                mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
5801                        .getOwnerUserId());
5802        assertEquals(USER_0,
5803                mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
5804                        .getPackageUserId());
5805        assertEquals(USER_P0,
5806                mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
5807                        .getOwnerUserId());
5808        assertEquals(USER_0,
5809                mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
5810                        .getPackageUserId());
5811    }
5812
5813    public void testOnApplicationActive_permission() {
5814        assertExpectException(SecurityException.class, "Missing permission", () ->
5815                mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
5816
5817        // Has permission, now it should pass.
5818        mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
5819        mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
5820    }
5821
5822    public void testDumpsys_crossProfile() {
5823        prepareCrossProfileDataSet();
5824        dumpsysOnLogcat("test1", /* force= */ true);
5825    }
5826
5827    public void testDumpsys_withIcons() throws IOException {
5828        testIcons();
5829        // Dump after having some icons.
5830        dumpsysOnLogcat("test1", /* force= */ true);
5831    }
5832
5833    public void testManifestShortcut_publishOnUnlockUser() {
5834        addManifestShortcutResource(
5835                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5836                R.xml.shortcut_1);
5837        addManifestShortcutResource(
5838                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5839                R.xml.shortcut_2);
5840        addManifestShortcutResource(
5841                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5842                R.xml.shortcut_5);
5843
5844        // Unlock user-0.
5845        mInjectedCurrentTimeMillis += 100;
5846        mService.handleUnlockUser(USER_0);
5847
5848        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5849            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5850                    mManager.getManifestShortcuts()))),
5851                    "ms1");
5852            assertEmpty(mManager.getPinnedShortcuts());
5853        });
5854
5855        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5856            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5857                    mManager.getManifestShortcuts()))),
5858                    "ms1", "ms2");
5859            assertEmpty(mManager.getPinnedShortcuts());
5860        });
5861
5862        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5863            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5864                    mManager.getManifestShortcuts()))),
5865                    "ms1", "ms2", "ms3", "ms4", "ms5");
5866            assertEmpty(mManager.getPinnedShortcuts());
5867        });
5868
5869        // Try on another user, with some packages uninstalled.
5870        mRunningUsers.put(USER_10, true);
5871
5872        uninstallPackage(USER_10, CALLING_PACKAGE_1);
5873        uninstallPackage(USER_10, CALLING_PACKAGE_3);
5874
5875        mInjectedCurrentTimeMillis += 100;
5876        mService.handleUnlockUser(USER_10);
5877
5878        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5879            assertEmpty(mManager.getManifestShortcuts());
5880            assertEmpty(mManager.getPinnedShortcuts());
5881        });
5882
5883        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5884            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5885                    mManager.getManifestShortcuts()))),
5886                    "ms1", "ms2");
5887            assertEmpty(mManager.getPinnedShortcuts());
5888        });
5889
5890        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
5891            assertEmpty(mManager.getManifestShortcuts());
5892            assertEmpty(mManager.getPinnedShortcuts());
5893        });
5894
5895        // Now change the resources for package 1, and unlock again.
5896        // But we still see *old* shortcuts, because the package version and install time
5897        // hasn't changed.
5898        shutdownServices();
5899
5900        mInjectedCurrentTimeMillis += 100;
5901
5902        addManifestShortcutResource(
5903                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5904                R.xml.shortcut_5);
5905        addManifestShortcutResource(
5906                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5907                R.xml.shortcut_1);
5908
5909        initService();
5910        mService.handleUnlockUser(USER_0);
5911
5912        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5913            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled( // FAIL
5914                    mManager.getManifestShortcuts()))),
5915                    "ms1");
5916            assertEmpty(mManager.getPinnedShortcuts());
5917        });
5918
5919        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5920            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5921                    mManager.getManifestShortcuts()))),
5922                    "ms1", "ms2");
5923            assertEmpty(mManager.getPinnedShortcuts());
5924        });
5925
5926        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5927            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5928                    mManager.getManifestShortcuts()))),
5929                    "ms1", "ms2", "ms3", "ms4", "ms5");
5930            assertEmpty(mManager.getPinnedShortcuts());
5931        });
5932
5933        // Do it again, but this time we change the app version, so we do detect the changes.
5934        shutdownServices();
5935
5936        mInjectedCurrentTimeMillis += 100;
5937
5938        updatePackageVersion(CALLING_PACKAGE_1, 1);
5939        updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
5940
5941        initService();
5942        mService.handleUnlockUser(USER_0);
5943
5944        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5945            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5946                    mManager.getManifestShortcuts()))),
5947                    "ms1", "ms2", "ms3", "ms4", "ms5");
5948            assertEmpty(mManager.getPinnedShortcuts());
5949        });
5950
5951        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5952            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5953                    mManager.getManifestShortcuts()))),
5954                    "ms1", "ms2");
5955            assertEmpty(mManager.getPinnedShortcuts());
5956        });
5957
5958        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5959            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5960                    mManager.getManifestShortcuts()))),
5961                    "ms1");
5962            assertEmpty(mManager.getPinnedShortcuts());
5963        });
5964
5965        // Next, try removing all shortcuts, with some of them pinned.
5966        runWithCaller(LAUNCHER_1, USER_0, () -> {
5967            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
5968            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
5969            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
5970        });
5971        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5972            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5973                    mManager.getManifestShortcuts()))),
5974                    "ms1", "ms2", "ms3", "ms4", "ms5");
5975            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5976                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5977                    "ms3");
5978        });
5979
5980        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5981            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5982                    mManager.getManifestShortcuts()))),
5983                    "ms1", "ms2");
5984            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5985                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5986                    "ms2");
5987        });
5988
5989        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5990            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5991                    mManager.getManifestShortcuts()))),
5992                    "ms1");
5993            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5994                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5995                    "ms1");
5996        });
5997
5998        shutdownServices();
5999
6000        mInjectedCurrentTimeMillis += 100;
6001
6002        addManifestShortcutResource(
6003                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6004                R.xml.shortcut_0);
6005        addManifestShortcutResource(
6006                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6007                R.xml.shortcut_1);
6008        addManifestShortcutResource(
6009                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6010                R.xml.shortcut_0);
6011
6012        updatePackageVersion(CALLING_PACKAGE_1, 1);
6013        updatePackageVersion(CALLING_PACKAGE_2, 1);
6014        updatePackageVersion(CALLING_PACKAGE_3, 1);
6015
6016        initService();
6017        mService.handleUnlockUser(USER_0);
6018
6019        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6020            assertEmpty(mManager.getManifestShortcuts());
6021            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6022                    assertAllDisabled(mManager.getPinnedShortcuts())))),
6023                    "ms3");
6024        });
6025
6026        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6027            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6028                    mManager.getManifestShortcuts()))),
6029                    "ms1");
6030            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6031                    assertAllDisabled(mManager.getPinnedShortcuts())))),
6032                    "ms2");
6033        });
6034
6035        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6036            assertEmpty(mManager.getManifestShortcuts());
6037            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6038                    assertAllDisabled(mManager.getPinnedShortcuts())))),
6039                    "ms1");
6040        });
6041
6042        // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
6043        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
6044        assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
6045    }
6046
6047    public void testManifestShortcut_publishOnBroadcast() {
6048        // First, no packages are installed.
6049        uninstallPackage(USER_0, CALLING_PACKAGE_1);
6050        uninstallPackage(USER_0, CALLING_PACKAGE_2);
6051        uninstallPackage(USER_0, CALLING_PACKAGE_3);
6052        uninstallPackage(USER_0, CALLING_PACKAGE_4);
6053        uninstallPackage(USER_10, CALLING_PACKAGE_1);
6054        uninstallPackage(USER_10, CALLING_PACKAGE_2);
6055        uninstallPackage(USER_10, CALLING_PACKAGE_3);
6056        uninstallPackage(USER_10, CALLING_PACKAGE_4);
6057
6058        mService.handleUnlockUser(USER_0);
6059
6060        mRunningUsers.put(USER_10, true);
6061        mService.handleUnlockUser(USER_10);
6062
6063        // Originally no manifest shortcuts.
6064        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6065            assertEmpty(mManager.getManifestShortcuts());
6066            assertEmpty(mManager.getPinnedShortcuts());
6067        });
6068
6069        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6070            assertEmpty(mManager.getManifestShortcuts());
6071            assertEmpty(mManager.getPinnedShortcuts());
6072        });
6073
6074        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6075            assertEmpty(mManager.getManifestShortcuts());
6076            assertEmpty(mManager.getPinnedShortcuts());
6077        });
6078
6079        // Package 1 updated, with manifest shortcuts.
6080        addManifestShortcutResource(
6081                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6082                R.xml.shortcut_1);
6083        updatePackageVersion(CALLING_PACKAGE_1, 1);
6084                mService.mPackageMonitor.onReceive(getTestContext(),
6085                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6086
6087        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6088            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6089                    mManager.getManifestShortcuts()))),
6090                    "ms1");
6091            assertEmpty(mManager.getPinnedShortcuts());
6092        });
6093
6094        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6095            assertEmpty(mManager.getManifestShortcuts());
6096            assertEmpty(mManager.getPinnedShortcuts());
6097        });
6098
6099        // Package 2 updated, with manifest shortcuts.
6100
6101        addManifestShortcutResource(
6102                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6103                R.xml.shortcut_5);
6104        updatePackageVersion(CALLING_PACKAGE_2, 1);
6105                mService.mPackageMonitor.onReceive(getTestContext(),
6106                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6107
6108        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6109            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6110                    mManager.getManifestShortcuts()))),
6111                    "ms1");
6112            assertEmpty(mManager.getPinnedShortcuts());
6113        });
6114
6115        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6116            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6117                    mManager.getManifestShortcuts()))),
6118                    "ms1", "ms2", "ms3", "ms4", "ms5");
6119            assertWith(getCallerShortcuts()).selectManifest()
6120                    .selectByActivity(
6121                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6122                    .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
6123            assertEmpty(mManager.getPinnedShortcuts());
6124        });
6125
6126        // Package 2 updated, with less manifest shortcuts.
6127        // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
6128
6129        dumpsysOnLogcat("Before pinning");
6130
6131        // Also pin some.
6132        runWithCaller(LAUNCHER_1, USER_0, () -> {
6133            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
6134        });
6135
6136        dumpsysOnLogcat("After pinning");
6137
6138        addManifestShortcutResource(
6139                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6140                R.xml.shortcut_2);
6141        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
6142                mService.mPackageMonitor.onReceive(getTestContext(),
6143                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6144
6145        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6146            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6147                    mManager.getManifestShortcuts()))),
6148                    "ms1");
6149            assertEmpty(mManager.getPinnedShortcuts());
6150        });
6151
6152        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6153            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6154                    mManager.getManifestShortcuts()))),
6155                    "ms1", "ms2");
6156            assertWith(getCallerShortcuts()).selectManifest()
6157                    .selectByActivity(
6158                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6159                    .haveRanksInOrder("ms1", "ms2");
6160            assertShortcutIds(assertAllImmutable(assertAllPinned(
6161                    mManager.getPinnedShortcuts())),
6162                    "ms2", "ms3");
6163            // ms3 is no longer in manifest, so should be disabled.
6164            // but ms1 and ms2 should be enabled.
6165            assertAllEnabled(list(getCallerShortcut("ms1")));
6166            assertAllEnabled(list(getCallerShortcut("ms2")));
6167            assertAllDisabled(list(getCallerShortcut("ms3")));
6168        });
6169
6170        // Package 2 on user 10 has no shortcuts yet.
6171        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6172            assertEmpty(mManager.getManifestShortcuts());
6173            assertEmpty(mManager.getPinnedShortcuts());
6174        });
6175        // Send add broadcast, but the user is not running, so should be ignored.
6176        mService.handleCleanupUser(USER_10);
6177        mRunningUsers.put(USER_10, false);
6178        mUnlockedUsers.put(USER_10, false);
6179
6180        mService.mPackageMonitor.onReceive(getTestContext(),
6181                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6182        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6183            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6184            // update check.
6185            // So look the internal data directly using getCallerShortcuts().
6186            assertEmpty(getCallerShortcuts());
6187        });
6188
6189        // Try again, but the user is locked, so still ignored.
6190        mRunningUsers.put(USER_10, true);
6191                mService.mPackageMonitor.onReceive(getTestContext(),
6192                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6193        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6194            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6195            // update check.
6196            // So look the internal data directly using getCallerShortcuts().
6197            assertEmpty(getCallerShortcuts());
6198        });
6199
6200        // Unlock the user, now it should work.
6201        mUnlockedUsers.put(USER_10, true);
6202
6203        // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
6204                mService.mPackageMonitor.onReceive(getTestContext(),
6205                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6206
6207        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6208            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6209                    mManager.getManifestShortcuts()))),
6210                    "ms1", "ms2");
6211            assertWith(getCallerShortcuts()).selectManifest()
6212                    .selectByActivity(
6213                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6214                    .haveRanksInOrder("ms1", "ms2");
6215            assertEmpty(mManager.getPinnedShortcuts());
6216        });
6217
6218        // But it shouldn't affect user-0.
6219        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6220            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6221                    mManager.getManifestShortcuts()))),
6222                    "ms1", "ms2");
6223            assertWith(getCallerShortcuts()).selectManifest()
6224                    .selectByActivity(
6225                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6226                    .haveRanksInOrder("ms1", "ms2");
6227            assertShortcutIds(assertAllImmutable(assertAllPinned(
6228                    mManager.getPinnedShortcuts())),
6229                    "ms2", "ms3");
6230            assertAllEnabled(list(getCallerShortcut("ms1")));
6231            assertAllEnabled(list(getCallerShortcut("ms2")));
6232            assertAllDisabled(list(getCallerShortcut("ms3")));
6233        });
6234
6235        // Multiple activities.
6236        // Add shortcuts on activity 2 for package 2.
6237        addManifestShortcutResource(
6238                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6239                R.xml.shortcut_5_alt);
6240        addManifestShortcutResource(
6241                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6242                R.xml.shortcut_5_reverse);
6243
6244        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
6245                mService.mPackageMonitor.onReceive(getTestContext(),
6246                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6247
6248        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6249            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6250                    mManager.getManifestShortcuts()))),
6251                    "ms1", "ms2", "ms3", "ms4", "ms5",
6252                    "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6253
6254            // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
6255            assertWith(getCallerShortcuts()).selectManifest()
6256                    .selectByActivity(
6257                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6258                    .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6259            assertWith(getCallerShortcuts()).selectManifest()
6260                    .selectByActivity(
6261                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
6262                    .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
6263        });
6264
6265        // Package 2 now has no manifest shortcuts.
6266        addManifestShortcutResource(
6267                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6268                R.xml.shortcut_0);
6269        addManifestShortcutResource(
6270                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6271                R.xml.shortcut_0);
6272        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
6273                mService.mPackageMonitor.onReceive(getTestContext(),
6274                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6275
6276        // No manifest shortcuts, and pinned ones are disabled.
6277        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6278            assertEmpty(mManager.getManifestShortcuts());
6279            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
6280                    mManager.getPinnedShortcuts()))),
6281                    "ms2", "ms3");
6282        });
6283    }
6284
6285    public void testManifestShortcuts_missingMandatoryFields() {
6286        // Start with no apps installed.
6287        uninstallPackage(USER_0, CALLING_PACKAGE_1);
6288        uninstallPackage(USER_0, CALLING_PACKAGE_2);
6289        uninstallPackage(USER_0, CALLING_PACKAGE_3);
6290        uninstallPackage(USER_0, CALLING_PACKAGE_4);
6291
6292        mService.handleUnlockUser(USER_0);
6293
6294        // Make sure no manifest shortcuts.
6295        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6296            assertEmpty(mManager.getManifestShortcuts());
6297        });
6298
6299        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6300        addManifestShortcutResource(
6301                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6302                R.xml.shortcut_error_1);
6303        updatePackageVersion(CALLING_PACKAGE_1, 1);
6304                mService.mPackageMonitor.onReceive(getTestContext(),
6305                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6306
6307        // Only the valid one is published.
6308        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6309            assertWith(getCallerShortcuts())
6310                    .areAllManifest()
6311                    .areAllImmutable()
6312                    .areAllEnabled()
6313                    .haveIds("x1");
6314        });
6315
6316        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6317        addManifestShortcutResource(
6318                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6319                R.xml.shortcut_error_2);
6320        updatePackageVersion(CALLING_PACKAGE_1, 1);
6321                mService.mPackageMonitor.onReceive(getTestContext(),
6322                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6323
6324        // Only the valid one is published.
6325        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6326            assertWith(getCallerShortcuts())
6327                    .areAllManifest()
6328                    .areAllImmutable()
6329                    .areAllEnabled()
6330                    .haveIds("x2");
6331        });
6332
6333        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6334        addManifestShortcutResource(
6335                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6336                R.xml.shortcut_error_3);
6337        updatePackageVersion(CALLING_PACKAGE_1, 1);
6338                mService.mPackageMonitor.onReceive(getTestContext(),
6339                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6340
6341        // Only the valid one is published.
6342        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6343            assertWith(getCallerShortcuts())
6344                    .areAllManifest()
6345                    .areAllImmutable()
6346                    .areAllEnabled()
6347                    .haveIds("x3")
6348                    .forShortcutWithId("x3", si -> {
6349                        assertEquals(set("cat2"), si.getCategories());
6350                     });
6351        });
6352    }
6353
6354    public void testManifestShortcuts_intentDefinitions() {
6355        addManifestShortcutResource(
6356                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6357                R.xml.shortcut_error_4);
6358        updatePackageVersion(CALLING_PACKAGE_1, 1);
6359                mService.mPackageMonitor.onReceive(getTestContext(),
6360                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6361
6362        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6363            // Make sure invalid ones are not published.
6364            // Note that at this point disabled ones don't show up because they weren't pinned.
6365            assertWith(getCallerShortcuts())
6366                    .haveIds("ms1", "ms2")
6367                    .areAllManifest()
6368                    .areAllNotDynamic()
6369                    .areAllNotPinned()
6370                    .areAllImmutable()
6371                    .areAllEnabled()
6372                    .forShortcutWithId("ms1", si -> {
6373                        assertTrue(si.isEnabled());
6374                        assertEquals(1, si.getIntents().length);
6375
6376                        assertEquals("action1", si.getIntent().getAction());
6377                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
6378                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6379                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
6380                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
6381
6382                        assertEquals("action1", si.getIntents()[0].getAction());
6383                        assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
6384                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6385                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
6386                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
6387                    })
6388                    .forShortcutWithId("ms2", si -> {
6389                        assertTrue(si.isEnabled());
6390                        assertEquals(2, si.getIntents().length);
6391
6392                        // getIntent will return the last one.
6393                        assertEquals("action2_2", si.getIntent().getAction());
6394                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
6395                        assertEquals(0, si.getIntent().getFlags());
6396
6397                        final Intent i1 = si.getIntents()[0];
6398                        final Intent i2 = si.getIntents()[1];
6399
6400                        assertEquals("action2_1", i1.getAction());
6401                        assertEquals("value1", i1.getStringExtra("key1"));
6402                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6403                                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
6404                                        Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
6405
6406                        assertEquals("action2_2", i2.getAction());
6407                        assertEquals("value2", i2.getStringExtra("key2"));
6408                        assertEquals(0, i2.getFlags());
6409                    });
6410        });
6411
6412        // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
6413        addManifestShortcutResource(
6414                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6415                R.xml.shortcut_5);
6416        updatePackageVersion(CALLING_PACKAGE_1, 1);
6417                mService.mPackageMonitor.onReceive(getTestContext(),
6418                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6419
6420        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6421            // Make sure 5 manifest shortcuts are published.
6422            assertWith(getCallerShortcuts())
6423                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6424                    .areAllManifest()
6425                    .areAllNotDynamic()
6426                    .areAllNotPinned()
6427                    .areAllImmutable()
6428                    .areAllEnabled();
6429        });
6430
6431        runWithCaller(LAUNCHER_1, USER_0, () -> {
6432            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
6433                    list("ms3", "ms4", "ms5"), HANDLE_USER_0);
6434        });
6435
6436        // Make sure they're pinned.
6437        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6438            assertWith(getCallerShortcuts())
6439                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6440                    .selectByIds("ms1", "ms2")
6441                    .areAllNotPinned()
6442                    .areAllEnabled()
6443
6444                    .revertToOriginalList()
6445                    .selectByIds("ms3", "ms4", "ms5")
6446                    .areAllPinned()
6447                    .areAllEnabled();
6448        });
6449
6450        // Update the app.
6451        addManifestShortcutResource(
6452                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6453                R.xml.shortcut_error_4);
6454        updatePackageVersion(CALLING_PACKAGE_1, 1);
6455                mService.mPackageMonitor.onReceive(getTestContext(),
6456                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6457
6458        // Make sure 3, 4 and 5 still exist but disabled.
6459        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6460            assertWith(getCallerShortcuts())
6461                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6462                    .areAllNotDynamic()
6463                    .areAllImmutable()
6464
6465                    .selectByIds("ms1", "ms2")
6466                    .areAllManifest()
6467                    .areAllNotPinned()
6468                    .areAllEnabled()
6469
6470                    .revertToOriginalList()
6471                    .selectByIds("ms3", "ms4", "ms5")
6472                    .areAllNotManifest()
6473                    .areAllPinned()
6474                    .areAllDisabled()
6475
6476                    .revertToOriginalList()
6477                    .forShortcutWithId("ms1", si -> {
6478                        assertEquals(si.getId(), "action1", si.getIntent().getAction());
6479                    })
6480                    .forShortcutWithId("ms2", si -> {
6481                        // getIntent returns the last one.
6482                        assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
6483                    })
6484                    .forShortcutWithId("ms3", si -> {
6485                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6486                    })
6487                    .forShortcutWithId("ms4", si -> {
6488                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6489                    })
6490                    .forShortcutWithId("ms5", si -> {
6491                        assertEquals(si.getId(), "action", si.getIntent().getAction());
6492                    });
6493        });
6494    }
6495
6496    public void testManifestShortcuts_checkAllFields() {
6497        mService.handleUnlockUser(USER_0);
6498
6499        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6500        addManifestShortcutResource(
6501                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6502                R.xml.shortcut_5);
6503        updatePackageVersion(CALLING_PACKAGE_1, 1);
6504                mService.mPackageMonitor.onReceive(getTestContext(),
6505                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6506
6507        // Only the valid one is published.
6508        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6509            assertWith(getCallerShortcuts())
6510                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6511                    .areAllManifest()
6512                    .areAllImmutable()
6513                    .areAllEnabled()
6514                    .areAllNotPinned()
6515                    .areAllNotDynamic()
6516
6517                    .forShortcutWithId("ms1", si -> {
6518                        assertEquals(R.drawable.icon1, si.getIconResourceId());
6519                        assertEquals(new ComponentName(CALLING_PACKAGE_1,
6520                                ShortcutActivity.class.getName()),
6521                                si.getActivity());
6522
6523                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
6524                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6525                        assertEquals(R.string.shortcut_text1, si.getTextResId());
6526                        assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
6527                        assertEquals(R.string.shortcut_disabled_message1,
6528                                si.getDisabledMessageResourceId());
6529                        assertEquals("r" + R.string.shortcut_disabled_message1,
6530                                si.getDisabledMessageResName());
6531
6532                        assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
6533                                si.getCategories());
6534                        assertEquals("action1", si.getIntent().getAction());
6535                        assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
6536                    })
6537
6538                    .forShortcutWithId("ms2", si -> {
6539                        assertEquals("ms2", si.getId());
6540                        assertEquals(R.drawable.icon2, si.getIconResourceId());
6541
6542                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
6543                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6544                        assertEquals(R.string.shortcut_text2, si.getTextResId());
6545                        assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
6546                        assertEquals(R.string.shortcut_disabled_message2,
6547                                si.getDisabledMessageResourceId());
6548                        assertEquals("r" + R.string.shortcut_disabled_message2,
6549                                si.getDisabledMessageResName());
6550
6551                        assertEquals(set("android.shortcut.conversation"), si.getCategories());
6552                        assertEquals("action2", si.getIntent().getAction());
6553                        assertEquals(null, si.getIntent().getData());
6554                    })
6555
6556                    .forShortcutWithId("ms3", si -> {
6557                        assertEquals(0, si.getIconResourceId());
6558                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
6559                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6560
6561                        assertEquals(0, si.getTextResId());
6562                        assertEquals(null, si.getTextResName());
6563                        assertEquals(0, si.getDisabledMessageResourceId());
6564                        assertEquals(null, si.getDisabledMessageResName());
6565
6566                        assertEmpty(si.getCategories());
6567                        assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
6568                        assertEquals(null, si.getIntent().getData());
6569                    })
6570
6571                    .forShortcutWithId("ms4", si -> {
6572                        assertEquals(0, si.getIconResourceId());
6573                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
6574                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6575
6576                        assertEquals(0, si.getTextResId());
6577                        assertEquals(null, si.getTextResName());
6578                        assertEquals(0, si.getDisabledMessageResourceId());
6579                        assertEquals(null, si.getDisabledMessageResName());
6580
6581                        assertEquals(set("cat"), si.getCategories());
6582                        assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
6583                        assertEquals(null, si.getIntent().getData());
6584                    })
6585
6586                    .forShortcutWithId("ms5", si -> {
6587                        si = getCallerShortcut("ms5");
6588                        assertEquals("action", si.getIntent().getAction());
6589                        assertEquals("http://www/", si.getIntent().getData().toString());
6590                        assertEquals("foo/bar", si.getIntent().getType());
6591                        assertEquals(
6592                                new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
6593
6594                        assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
6595                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
6596                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
6597                    });
6598        });
6599    }
6600
6601    public void testManifestShortcuts_localeChange() throws InterruptedException {
6602        mService.handleUnlockUser(USER_0);
6603
6604        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6605        addManifestShortcutResource(
6606                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6607                R.xml.shortcut_2);
6608        updatePackageVersion(CALLING_PACKAGE_1, 1);
6609                mService.mPackageMonitor.onReceive(getTestContext(),
6610                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6611
6612        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6613            mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
6614
6615            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6616                    mManager.getManifestShortcuts()))),
6617                    "ms1", "ms2");
6618
6619            // check first shortcut.
6620            ShortcutInfo si = getCallerShortcut("ms1");
6621
6622            assertEquals("ms1", si.getId());
6623            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
6624                    si.getTitle());
6625            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
6626                    si.getText());
6627            assertEquals("string-com.android.test.1-user:0-res:"
6628                            + R.string.shortcut_disabled_message1 + "/en",
6629                    si.getDisabledMessage());
6630            assertEquals(START_TIME, si.getLastChangedTimestamp());
6631
6632            // check another
6633            si = getCallerShortcut("ms2");
6634
6635            assertEquals("ms2", si.getId());
6636            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
6637                    si.getTitle());
6638            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
6639                    si.getText());
6640            assertEquals("string-com.android.test.1-user:0-res:"
6641                            + R.string.shortcut_disabled_message2 + "/en",
6642                    si.getDisabledMessage());
6643            assertEquals(START_TIME, si.getLastChangedTimestamp());
6644
6645            // Check the dynamic one.
6646            si = getCallerShortcut("s1");
6647
6648            assertEquals("s1", si.getId());
6649            assertEquals("title", si.getTitle());
6650            assertEquals(null, si.getText());
6651            assertEquals(null, si.getDisabledMessage());
6652            assertEquals(START_TIME, si.getLastChangedTimestamp());
6653        });
6654
6655        mInjectedCurrentTimeMillis++;
6656
6657        // Change the locale and send the broadcast, make sure the launcher gets a callback too.
6658        mInjectedLocale = Locale.JAPANESE;
6659
6660        setCaller(LAUNCHER_1, USER_0);
6661
6662        assertForLauncherCallback(mLauncherApps, () -> {
6663            mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
6664        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
6665                .haveIds("ms1", "ms2", "s1");
6666
6667        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6668            // check first shortcut.
6669            ShortcutInfo si = getCallerShortcut("ms1");
6670
6671            assertEquals("ms1", si.getId());
6672            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
6673                    si.getTitle());
6674            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
6675                    si.getText());
6676            assertEquals("string-com.android.test.1-user:0-res:"
6677                            + R.string.shortcut_disabled_message1 + "/ja",
6678                    si.getDisabledMessage());
6679            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6680
6681            // check another
6682            si = getCallerShortcut("ms2");
6683
6684            assertEquals("ms2", si.getId());
6685            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
6686                    si.getTitle());
6687            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
6688                    si.getText());
6689            assertEquals("string-com.android.test.1-user:0-res:"
6690                            + R.string.shortcut_disabled_message2 + "/ja",
6691                    si.getDisabledMessage());
6692            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6693
6694            // Check the dynamic one.  (locale change shouldn't affect.)
6695            si = getCallerShortcut("s1");
6696
6697            assertEquals("s1", si.getId());
6698            assertEquals("title", si.getTitle());
6699            assertEquals(null, si.getText());
6700            assertEquals(null, si.getDisabledMessage());
6701            assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
6702        });
6703    }
6704
6705    public void testManifestShortcuts_updateAndDisabled_notPinned() {
6706        mService.handleUnlockUser(USER_0);
6707
6708        // First, just publish a manifest shortcut.
6709        addManifestShortcutResource(
6710                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6711                R.xml.shortcut_1);
6712        updatePackageVersion(CALLING_PACKAGE_1, 1);
6713                mService.mPackageMonitor.onReceive(getTestContext(),
6714                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6715
6716        // Only the valid one is published.
6717        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6718            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6719                    mManager.getManifestShortcuts()))),
6720                    "ms1");
6721            assertEmpty(mManager.getPinnedShortcuts());
6722
6723            // Make sure there's no other dangling shortcuts.
6724            assertShortcutIds(getCallerShortcuts(), "ms1");
6725        });
6726
6727        // Now version up, the manifest shortcut is disabled now.
6728        addManifestShortcutResource(
6729                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6730                R.xml.shortcut_1_disable);
6731        updatePackageVersion(CALLING_PACKAGE_1, 1);
6732                mService.mPackageMonitor.onReceive(getTestContext(),
6733                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6734
6735        // Because shortcut 1 wasn't pinned, it'll just go away.
6736        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6737            assertEmpty(mManager.getManifestShortcuts());
6738            assertEmpty(mManager.getPinnedShortcuts());
6739
6740            // Make sure there's no other dangling shortcuts.
6741            assertEmpty(getCallerShortcuts());
6742        });
6743    }
6744
6745    public void testManifestShortcuts_updateAndDisabled_pinned() {
6746        mService.handleUnlockUser(USER_0);
6747
6748        // First, just publish a manifest shortcut.
6749        addManifestShortcutResource(
6750                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6751                R.xml.shortcut_1);
6752        updatePackageVersion(CALLING_PACKAGE_1, 1);
6753                mService.mPackageMonitor.onReceive(getTestContext(),
6754                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6755
6756        // Only the valid one is published.
6757        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6758            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6759                    mManager.getManifestShortcuts()))),
6760                    "ms1");
6761            assertEmpty(mManager.getPinnedShortcuts());
6762
6763            // Make sure there's no other dangling shortcuts.
6764            assertShortcutIds(getCallerShortcuts(), "ms1");
6765        });
6766
6767        runWithCaller(LAUNCHER_1, USER_0, () -> {
6768            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
6769        });
6770
6771        // Now upgrade, the manifest shortcut is disabled now.
6772        addManifestShortcutResource(
6773                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6774                R.xml.shortcut_1_disable);
6775        updatePackageVersion(CALLING_PACKAGE_1, 1);
6776                mService.mPackageMonitor.onReceive(getTestContext(),
6777                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6778
6779        // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
6780        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6781            assertEmpty(mManager.getManifestShortcuts());
6782            assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
6783                    mManager.getPinnedShortcuts()))),
6784                    "ms1");
6785
6786            // Make sure the fields are updated.
6787            ShortcutInfo si = getCallerShortcut("ms1");
6788
6789            assertEquals("ms1", si.getId());
6790            assertEquals(R.drawable.icon2, si.getIconResourceId());
6791            assertEquals(R.string.shortcut_title2, si.getTitleResId());
6792            assertEquals(R.string.shortcut_text2, si.getTextResId());
6793            assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
6794            assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
6795
6796            // Make sure there's no other dangling shortcuts.
6797            assertShortcutIds(getCallerShortcuts(), "ms1");
6798        });
6799    }
6800
6801    public void testManifestShortcuts_duplicateInSingleActivity() {
6802        mService.handleUnlockUser(USER_0);
6803
6804        // The XML has two shortcuts with the same ID.
6805        addManifestShortcutResource(
6806                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6807                R.xml.shortcut_2_duplicate);
6808        updatePackageVersion(CALLING_PACKAGE_1, 1);
6809                mService.mPackageMonitor.onReceive(getTestContext(),
6810                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6811
6812        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6813            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6814                    mManager.getManifestShortcuts()))),
6815                    "ms1");
6816
6817            // Make sure the first one has survived.  (the second one has a different title.)
6818            ShortcutInfo si = getCallerShortcut("ms1");
6819            assertEquals(R.string.shortcut_title1, si.getTitleResId());
6820
6821            // Make sure there's no other dangling shortcuts.
6822            assertShortcutIds(getCallerShortcuts(), "ms1");
6823        });
6824    }
6825
6826    public void testManifestShortcuts_duplicateInTwoActivities() {
6827        mService.handleUnlockUser(USER_0);
6828
6829        // ShortcutActivity has shortcut ms1
6830        addManifestShortcutResource(
6831                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6832                R.xml.shortcut_1);
6833
6834        // ShortcutActivity2 has two shortcuts, ms1 and ms2.
6835        addManifestShortcutResource(
6836                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6837                R.xml.shortcut_5);
6838        updatePackageVersion(CALLING_PACKAGE_1, 1);
6839                mService.mPackageMonitor.onReceive(getTestContext(),
6840                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6841
6842        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6843            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6844                    mManager.getManifestShortcuts()))),
6845                    "ms1", "ms2", "ms3", "ms4", "ms5");
6846
6847            // ms1 should belong to ShortcutActivity.
6848            ShortcutInfo si = getCallerShortcut("ms1");
6849            assertEquals(R.string.shortcut_title1, si.getTitleResId());
6850            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6851                    si.getActivity());
6852            assertEquals(0, si.getRank());
6853
6854            // ms2 should belong to ShortcutActivity*2*.
6855            si = getCallerShortcut("ms2");
6856            assertEquals(R.string.shortcut_title2, si.getTitleResId());
6857            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6858                    si.getActivity());
6859
6860            // Also check the ranks
6861            assertWith(getCallerShortcuts()).selectManifest()
6862                    .selectByActivity(
6863                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
6864                    .haveRanksInOrder("ms1");
6865            assertWith(getCallerShortcuts()).selectManifest()
6866                    .selectByActivity(
6867                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
6868                    .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
6869
6870            // Make sure there's no other dangling shortcuts.
6871            assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
6872        });
6873    }
6874
6875    /**
6876     * Manifest shortcuts cannot override shortcuts that were published via the APIs.
6877     */
6878    public void testManifestShortcuts_cannotOverrideNonManifest() {
6879        mService.handleUnlockUser(USER_0);
6880
6881        // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
6882
6883        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6884            mManager.setDynamicShortcuts(list(
6885                    makeShortcut("ms1", "title1",
6886                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6887                            /* icon */ null, new Intent("action1"), /* rank */ 0,
6888                            /* chooserFilter=*/ null, /* chooserComponentNames=*/ null),
6889                    makeShortcut("ms2", "title2",
6890                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6891                            /* icon */ null, new Intent("action1"), /* rank */ 0, /* chooserFilter=*/ null,
6892                            /* chooserComponentNames=*/ null)));
6893        });
6894
6895        runWithCaller(LAUNCHER_1, USER_0, () -> {
6896            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6897        });
6898
6899        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6900            mManager.removeDynamicShortcuts(list("ms2"));
6901
6902            assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
6903            assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
6904            assertEmpty(mManager.getManifestShortcuts());
6905        });
6906
6907        // Then update the app with 5 manifest shortcuts.
6908        // Make sure "ms1" and "ms2" won't be replaced.
6909        addManifestShortcutResource(
6910                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6911                R.xml.shortcut_5);
6912        updatePackageVersion(CALLING_PACKAGE_1, 1);
6913                mService.mPackageMonitor.onReceive(getTestContext(),
6914                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6915
6916        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6917            assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
6918            assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
6919            assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
6920                    "ms3", "ms4", "ms5");
6921
6922            // ms1 and ms2 shouold keep the original title.
6923            ShortcutInfo si = getCallerShortcut("ms1");
6924            assertEquals("title1", si.getTitle());
6925
6926            si = getCallerShortcut("ms2");
6927            assertEquals("title2", si.getTitle());
6928        });
6929    }
6930
6931    protected void checkManifestShortcuts_immutable_verify() {
6932        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6933            assertShortcutIds(assertAllNotManifest(assertAllEnabled(
6934                    mManager.getDynamicShortcuts())),
6935                    "s1");
6936            assertShortcutIds(assertAllManifest(assertAllEnabled(
6937                    mManager.getManifestShortcuts())),
6938                    "ms1");
6939            assertShortcutIds(assertAllNotManifest(assertAllDisabled(
6940                    mManager.getPinnedShortcuts())),
6941                    "ms2");
6942
6943            assertEquals("t1", getCallerShortcut("s1").getTitle());
6944
6945            // Make sure there are no other shortcuts.
6946            assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
6947        });
6948    }
6949
6950    /**
6951     * Make sure the APIs won't work on manifest shortcuts.
6952     */
6953    public void testManifestShortcuts_immutable() {
6954        mService.handleUnlockUser(USER_0);
6955
6956        // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
6957        // a manifest shortcut, as well as a dynamic shortcut.
6958
6959        addManifestShortcutResource(
6960                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6961                R.xml.shortcut_2);
6962        updatePackageVersion(CALLING_PACKAGE_1, 1);
6963                mService.mPackageMonitor.onReceive(getTestContext(),
6964                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6965
6966        runWithCaller(LAUNCHER_1, USER_0, () -> {
6967            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6968        });
6969
6970        addManifestShortcutResource(
6971                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6972                R.xml.shortcut_1);
6973        updatePackageVersion(CALLING_PACKAGE_1, 1);
6974                mService.mPackageMonitor.onReceive(getTestContext(),
6975                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6976
6977        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6978            mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
6979        });
6980
6981        checkManifestShortcuts_immutable_verify();
6982
6983        // Note that even though the first argument is not immutable and only the second one
6984        // is immutable, the first argument should not be executed either.
6985
6986        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6987            assertCannotUpdateImmutable(() -> {
6988                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6989            });
6990            assertCannotUpdateImmutable(() -> {
6991                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
6992            });
6993        });
6994        checkManifestShortcuts_immutable_verify();
6995
6996        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6997            assertCannotUpdateImmutable(() -> {
6998                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6999            });
7000            assertCannotUpdateImmutable(() -> {
7001                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
7002            });
7003        });
7004        checkManifestShortcuts_immutable_verify();
7005
7006
7007        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7008            assertCannotUpdateImmutable(() -> {
7009                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
7010            });
7011            assertCannotUpdateImmutable(() -> {
7012                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
7013            });
7014        });
7015        checkManifestShortcuts_immutable_verify();
7016
7017        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7018            assertCannotUpdateImmutable(() -> {
7019                mManager.removeDynamicShortcuts(list("s1", "ms1"));
7020            });
7021            assertCannotUpdateImmutable(() -> {
7022                mManager.removeDynamicShortcuts(list("s2", "ms2"));
7023            });
7024        });
7025        checkManifestShortcuts_immutable_verify();
7026
7027        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7028            assertCannotUpdateImmutable(() -> {
7029                mManager.disableShortcuts(list("s1", "ms1"));
7030            });
7031        });
7032        checkManifestShortcuts_immutable_verify();
7033
7034        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7035            assertCannotUpdateImmutable(() -> {
7036                mManager.enableShortcuts(list("s1", "ms2"));
7037            });
7038        });
7039        checkManifestShortcuts_immutable_verify();
7040    }
7041
7042
7043    /**
7044     * Make sure the APIs won't work on manifest shortcuts.
7045     */
7046    public void testManifestShortcuts_tooMany() {
7047        // Change the max number of shortcuts.
7048        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7049
7050        mService.handleUnlockUser(USER_0);
7051
7052        addManifestShortcutResource(
7053                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7054                R.xml.shortcut_5);
7055        updatePackageVersion(CALLING_PACKAGE_1, 1);
7056                mService.mPackageMonitor.onReceive(getTestContext(),
7057                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7058
7059        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7060            // Only the first 3 should be published.
7061            assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
7062        });
7063    }
7064
7065    public void testMaxShortcutCount_set() {
7066        // Change the max number of shortcuts.
7067        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7068
7069        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7070            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7071            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7072            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7073            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7074            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7075            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7076            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7077            final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
7078            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7079            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7080            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7081            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7082
7083            // 3 shortcuts for 2 activities -> okay
7084            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7085            assertShortcutIds(mManager.getDynamicShortcuts(),
7086                    "s11", "s12", "s13", "s21", "s22", "s23");
7087
7088            mManager.removeAllDynamicShortcuts();
7089
7090            // 4 shortcut for activity 1 -> too many.
7091            assertDynamicShortcutCountExceeded(() -> {
7092                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7093            });
7094            assertEmpty(mManager.getDynamicShortcuts());
7095
7096            // 4 shortcut for activity 2 -> too many.
7097            assertDynamicShortcutCountExceeded(() -> {
7098                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7099            });
7100            assertEmpty(mManager.getDynamicShortcuts());
7101
7102            // First, set 3.  Then set 4, which should be ignored.
7103            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7104            assertShortcutIds(mManager.getDynamicShortcuts(),
7105                    "s11", "s12", "s13");
7106            assertDynamicShortcutCountExceeded(() -> {
7107                mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
7108            });
7109            assertShortcutIds(mManager.getDynamicShortcuts(),
7110                    "s11", "s12", "s13");
7111
7112            // Set will remove the old dynamic set, unlike add, so the following should pass.
7113            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7114            assertShortcutIds(mManager.getDynamicShortcuts(),
7115                    "s11", "s12", "s13");
7116            mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
7117            assertShortcutIds(mManager.getDynamicShortcuts(),
7118                    "s14", "s15", "s16");
7119
7120            // Now, test with 2 manifest shortcuts.
7121            mManager.removeAllDynamicShortcuts();
7122            addManifestShortcutResource(
7123                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7124                    R.xml.shortcut_2);
7125            updatePackageVersion(CALLING_PACKAGE_1, 1);
7126                    mService.mPackageMonitor.onReceive(getTestContext(),
7127                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7128            assertEquals(2, mManager.getManifestShortcuts().size());
7129
7130            // Setting 1 to activity 1 will work.
7131            mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7132            assertShortcutIds(mManager.getDynamicShortcuts(),
7133                    "s11", "s21", "s22", "s23");
7134            assertEquals(2, mManager.getManifestShortcuts().size());
7135
7136            // But setting 2 will not.
7137            mManager.removeAllDynamicShortcuts();
7138            assertDynamicShortcutCountExceeded(() -> {
7139                mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
7140            });
7141            assertEmpty(mManager.getDynamicShortcuts());
7142            assertEquals(2, mManager.getManifestShortcuts().size());
7143        });
7144    }
7145
7146    public void testMaxShortcutCount_add() {
7147        // Change the max number of shortcuts.
7148        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7149
7150        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7151            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7152            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7153            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7154            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7155            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7156            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7157            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7158            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7159            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7160            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7161
7162            // 3 shortcuts for 2 activities -> okay
7163            mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7164            assertShortcutIds(mManager.getDynamicShortcuts(),
7165                    "s11", "s12", "s13", "s21", "s22", "s23");
7166
7167            mManager.removeAllDynamicShortcuts();
7168
7169            // 4 shortcut for activity 1 -> too many.
7170            assertDynamicShortcutCountExceeded(() -> {
7171                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7172            });
7173            assertEmpty(mManager.getDynamicShortcuts());
7174
7175            // 4 shortcut for activity 2 -> too many.
7176            assertDynamicShortcutCountExceeded(() -> {
7177                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7178            });
7179            assertEmpty(mManager.getDynamicShortcuts());
7180
7181            // First, set 3.  Then add 1 more, which should be ignored.
7182            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7183            assertShortcutIds(mManager.getDynamicShortcuts(),
7184                    "s11", "s12", "s13");
7185            assertDynamicShortcutCountExceeded(() -> {
7186                mManager.addDynamicShortcuts(list(s1_4, s2_1));
7187            });
7188            assertShortcutIds(mManager.getDynamicShortcuts(),
7189                    "s11", "s12", "s13");
7190
7191            // Update existing one, which should work.
7192            mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
7193                    "s11", a1, "xxx"), s2_1));
7194            assertShortcutIds(mManager.getDynamicShortcuts(),
7195                    "s11", "s12", "s13", "s21");
7196            assertEquals("xxx", getCallerShortcut("s11").getTitle());
7197
7198            // Make sure pinned shortcuts won't affect.
7199            // - Pin s11 - s13, and remove all dynamic.
7200            runWithCaller(LAUNCHER_1, USER_0, () -> {
7201                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7202                        HANDLE_USER_0);
7203            });
7204            mManager.removeAllDynamicShortcuts();
7205
7206            assertEmpty(mManager.getDynamicShortcuts());
7207            assertShortcutIds(mManager.getPinnedShortcuts(),
7208                    "s11", "s12", "s13");
7209
7210            // Then add dynamic.
7211            mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
7212
7213            assertShortcutIds(mManager.getDynamicShortcuts(),
7214                    "s14", "s21", "s22", "s23");
7215            assertShortcutIds(mManager.getPinnedShortcuts(),
7216                    "s11", "s12", "s13");
7217
7218            // Adding "s11" and "s12" back, should work
7219            mManager.addDynamicShortcuts(list(s1_1, s1_2));
7220
7221            assertShortcutIds(mManager.getDynamicShortcuts(),
7222                    "s14", "s11", "s12", "s21", "s22", "s23");
7223            assertShortcutIds(mManager.getPinnedShortcuts(),
7224                    "s11", "s12", "s13");
7225
7226            // Adding back s13 doesn't work.
7227            assertDynamicShortcutCountExceeded(() -> {
7228                mManager.addDynamicShortcuts(list(s1_3));
7229            });
7230
7231            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7232                    "s11", "s12", "s14");
7233            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7234                    "s21", "s22", "s23");
7235
7236            // Now swap the activities.
7237            mManager.updateShortcuts(list(
7238                    makeShortcutWithActivity("s11", a2),
7239                    makeShortcutWithActivity("s21", a1)));
7240
7241            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7242                    "s21", "s12", "s14");
7243            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7244                    "s11", "s22", "s23");
7245
7246            // Now, test with 2 manifest shortcuts.
7247            mManager.removeAllDynamicShortcuts();
7248            addManifestShortcutResource(
7249                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7250                    R.xml.shortcut_2);
7251            updatePackageVersion(CALLING_PACKAGE_1, 1);
7252                    mService.mPackageMonitor.onReceive(getTestContext(),
7253                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7254
7255            assertEquals(2, mManager.getManifestShortcuts().size());
7256
7257            // Adding one shortcut to activity 1 works fine.
7258            mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7259            assertShortcutIds(mManager.getDynamicShortcuts(),
7260                    "s11", "s21", "s22", "s23");
7261            assertEquals(2, mManager.getManifestShortcuts().size());
7262
7263            // But adding one more doesn't.
7264            assertDynamicShortcutCountExceeded(() -> {
7265                mManager.addDynamicShortcuts(list(s1_4, s2_1));
7266            });
7267            assertShortcutIds(mManager.getDynamicShortcuts(),
7268                    "s11", "s21", "s22", "s23");
7269            assertEquals(2, mManager.getManifestShortcuts().size());
7270        });
7271    }
7272
7273    public void testMaxShortcutCount_update() {
7274        // Change the max number of shortcuts.
7275        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7276
7277        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7278            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7279            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7280            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7281            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7282            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7283            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7284            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7285            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7286            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7287            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7288            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7289
7290            // 3 shortcuts for 2 activities -> okay
7291            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7292            assertShortcutIds(mManager.getDynamicShortcuts(),
7293                    "s11", "s12", "s13", "s21", "s22", "s23");
7294
7295            // Trying to move s11 from a1 to a2 should fail.
7296            assertDynamicShortcutCountExceeded(() -> {
7297                mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
7298            });
7299            assertShortcutIds(mManager.getDynamicShortcuts(),
7300                    "s11", "s12", "s13", "s21", "s22", "s23");
7301
7302            // Trying to move s21 from a2 to a1 should also fail.
7303            assertDynamicShortcutCountExceeded(() -> {
7304                mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
7305            });
7306            assertShortcutIds(mManager.getDynamicShortcuts(),
7307                    "s11", "s12", "s13", "s21", "s22", "s23");
7308
7309            // But, if we do these two at the same time, it should work.
7310            mManager.updateShortcuts(list(
7311                    makeShortcutWithActivity("s11", a2),
7312                    makeShortcutWithActivity("s21", a1)));
7313            assertShortcutIds(mManager.getDynamicShortcuts(),
7314                    "s11", "s12", "s13", "s21", "s22", "s23");
7315            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7316                    "s21", "s12", "s13");
7317            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7318                    "s11", "s22", "s23");
7319
7320            // Then reset.
7321            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7322            assertShortcutIds(mManager.getDynamicShortcuts(),
7323                    "s11", "s12", "s13", "s21", "s22", "s23");
7324
7325            // Pin some to have more shortcuts for a1.
7326            runWithCaller(LAUNCHER_1, USER_0, () -> {
7327                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7328                        HANDLE_USER_0);
7329            });
7330            mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
7331            assertShortcutIds(mManager.getDynamicShortcuts(),
7332                    "s14", "s15", "s21", "s22", "s23");
7333            assertShortcutIds(mManager.getPinnedShortcuts(),
7334                    "s11", "s12", "s13");
7335
7336            // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
7337            // But that doesn't matter for update -- the following should still work.
7338            mManager.updateShortcuts(list(
7339                    makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
7340                    makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
7341                    makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
7342                    makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
7343                    makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
7344            // All the shortcuts should still exist they all belong on same activities,
7345            // with the updated titles.
7346            assertShortcutIds(mManager.getDynamicShortcuts(),
7347                    "s14", "s15", "s21", "s22", "s23");
7348            assertShortcutIds(mManager.getPinnedShortcuts(),
7349                    "s11", "s12", "s13");
7350
7351            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7352                    "s14", "s15");
7353            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7354                    "s21", "s22", "s23");
7355
7356            assertEquals("xxx1", getCallerShortcut("s11").getTitle());
7357            assertEquals("xxx2", getCallerShortcut("s12").getTitle());
7358            assertEquals("xxx3", getCallerShortcut("s13").getTitle());
7359            assertEquals("xxx4", getCallerShortcut("s14").getTitle());
7360            assertEquals("xxx5", getCallerShortcut("s15").getTitle());
7361        });
7362    }
7363
7364    public void testShortcutsPushedOutByManifest() {
7365        // Change the max number of shortcuts.
7366        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7367
7368        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7369            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7370            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7371            final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
7372            final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
7373            final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
7374            final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
7375            final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
7376            final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
7377            final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
7378            final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
7379            final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
7380            final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
7381
7382            // Initial state.
7383            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7384            runWithCaller(LAUNCHER_1, USER_0, () -> {
7385                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
7386                        HANDLE_USER_0);
7387            });
7388            mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
7389            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7390                    "s12", "s13", "s14",
7391                    "s22", "s23", "s24");
7392            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7393                    "s11", "s12",
7394                    "s21", "s22");
7395
7396            // Add 1 manifest shortcut to a1.
7397            addManifestShortcutResource(
7398                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7399                    R.xml.shortcut_1);
7400            updatePackageVersion(CALLING_PACKAGE_1, 1);
7401                    mService.mPackageMonitor.onReceive(getTestContext(),
7402                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7403            assertEquals(1, mManager.getManifestShortcuts().size());
7404
7405            // s12 removed.
7406            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7407                    "s13", "s14",
7408                    "s22", "s23", "s24");
7409            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7410                    "s11", "s12",
7411                    "s21", "s22");
7412
7413            // Add more manifest shortcuts.
7414            addManifestShortcutResource(
7415                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7416                    R.xml.shortcut_2);
7417            addManifestShortcutResource(
7418                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7419                    R.xml.shortcut_1_alt);
7420            updatePackageVersion(CALLING_PACKAGE_1, 1);
7421                    mService.mPackageMonitor.onReceive(getTestContext(),
7422                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7423            assertEquals(3, mManager.getManifestShortcuts().size());
7424
7425            // Note the ones with the highest rank values (== least important) will be removed.
7426            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7427                    "s14",
7428                    "s22", "s23");
7429            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7430                    "s11", "s12",
7431                    "s21", "s22");
7432
7433            // Add more manifest shortcuts.
7434            addManifestShortcutResource(
7435                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7436                    R.xml.shortcut_2);
7437            addManifestShortcutResource(
7438                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7439                    R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
7440            updatePackageVersion(CALLING_PACKAGE_1, 1);
7441                    mService.mPackageMonitor.onReceive(getTestContext(),
7442                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7443            assertEquals(5, mManager.getManifestShortcuts().size());
7444
7445            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7446                    "s14" // a1 has 1 dynamic
7447            ); // a2 has no dynamic
7448            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7449                    "s11", "s12",
7450                    "s21", "s22");
7451
7452            // Update, no manifest shortucts.  This doesn't affect anything.
7453            addManifestShortcutResource(
7454                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7455                    R.xml.shortcut_0);
7456            addManifestShortcutResource(
7457                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7458                    R.xml.shortcut_0);
7459            updatePackageVersion(CALLING_PACKAGE_1, 1);
7460                    mService.mPackageMonitor.onReceive(getTestContext(),
7461                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7462            assertEquals(0, mManager.getManifestShortcuts().size());
7463
7464            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7465                    "s14");
7466            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7467                    "s11", "s12",
7468                    "s21", "s22");
7469        });
7470    }
7471}
7472