From 13260b6ff0438a02bd0f1c5bbcfb4fef71c82135 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Wed, 13 Jul 2016 18:03:13 -0700 Subject: [PATCH] Don't allow shortcuts with non-main activities Bug 30122758 Change-Id: Id9ead6706d5460fec3d68c7457e3097f30a9a080 --- .../android/server/pm/ShortcutService.java | 4 +++ .../server/pm/ShortcutManagerTest2.java | 30 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index 5f8cbbf0544b5..730217db9501a 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -1444,6 +1444,10 @@ public class ShortcutService extends IShortcutService.Stub { shortcut.getPackage().equals(shortcut.getActivity().getPackageName()), "Cannot publish shortcut: activity " + shortcut.getActivity() + " does not" + " belong to package " + shortcut.getPackage()); + Preconditions.checkState( + injectIsMainActivity(shortcut.getActivity(), shortcut.getUserId()), + "Cannot publish shortcut: activity " + shortcut.getActivity() + " is not" + + " main activity"); } if (!forUpdate) { diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java index f570ff24ce361..d5460929960b5 100644 --- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java +++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java @@ -130,7 +130,6 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest { assertExpectException(NullPointerException.class, "action must be set", () -> new ShortcutInfo.Builder(getTestContext(), "id").setIntent(new Intent())); - // same for add. assertExpectException( IllegalArgumentException.class, "Short label must be provided", () -> { ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") @@ -139,6 +138,7 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest { assertTrue(getManager().setDynamicShortcuts(list(si))); }); + // same for add. assertExpectException( IllegalArgumentException.class, "Short label must be provided", () -> { ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") @@ -147,7 +147,6 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest { assertTrue(getManager().addDynamicShortcuts(list(si))); }); - // same for add. assertExpectException(NullPointerException.class, "Intent must be provided", () -> { ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") .setActivity(new ComponentName(getTestContext().getPackageName(), "s")) @@ -181,6 +180,33 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest { .build(); assertTrue(getManager().addDynamicShortcuts(list(si))); }); + + // Now all activities are not main. + mMainActivityChecker = (component, userId) -> false; + + assertExpectException( + IllegalStateException.class, "is not main", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") + .setActivity(new ComponentName(getTestContext(), "s")) + .build(); + assertTrue(getManager().setDynamicShortcuts(list(si))); + }); + // For add + assertExpectException( + IllegalStateException.class, "is not main", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") + .setActivity(new ComponentName(getTestContext(), "s")) + .build(); + assertTrue(getManager().addDynamicShortcuts(list(si))); + }); + // For update + assertExpectException( + IllegalStateException.class, "is not main", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id") + .setActivity(new ComponentName(getTestContext(), "s")) + .build(); + assertTrue(getManager().updateShortcuts(list(si))); + }); } public void testShortcutInfoParcel() {