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