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