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