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