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