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