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