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