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