diff --git a/core/java/android/content/pm/ShortcutServiceInternal.java b/core/java/android/content/pm/ShortcutServiceInternal.java index 435c70ae999b8..eee91ce173dc3 100644 --- a/core/java/android/content/pm/ShortcutServiceInternal.java +++ b/core/java/android/content/pm/ShortcutServiceInternal.java @@ -109,4 +109,8 @@ public abstract class ShortcutServiceInternal { */ public abstract String getShortcutIconUri(int launcherUserId, @NonNull String launcherPackage, @NonNull String packageName, @NonNull String shortcutId, int userId); + + public abstract boolean isSharingShortcut(int callingUserId, @NonNull String callingPackage, + @NonNull String packageName, @NonNull String shortcutId, int userId, + @NonNull IntentFilter filter); } diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index 8768ab0a683b8..8d53d15546194 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -2385,6 +2385,30 @@ public class ShortcutService extends IShortcutService.Stub { } } + public boolean isSharingShortcut(int callingUserId, @NonNull String callingPackage, + @NonNull String packageName, @NonNull String shortcutId, int userId, + @NonNull IntentFilter filter) { + verifyCaller(callingPackage, callingUserId); + enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APP_PREDICTIONS, + "isSharingShortcut"); + + synchronized (mLock) { + throwIfUserLockedL(userId); + throwIfUserLockedL(callingUserId); + + final List matchedTargets = + getPackageShortcutsLocked(packageName, userId) + .getMatchingShareTargets(filter); + final int matchedSize = matchedTargets.size(); + for (int i = 0; i < matchedSize; i++) { + if (matchedTargets.get(i).getShortcutInfo().getId().equals(shortcutId)) { + return true; + } + } + } + return false; + } + @GuardedBy("mLock") private ParceledListSlice getShortcutsWithQueryLocked(@NonNull String packageName, @UserIdInt int userId, int cloneFlags, @NonNull Predicate query) { @@ -2969,6 +2993,18 @@ public class ShortcutService extends IShortcutService.Stub { callingPackage, intentFilter, userId).getList(); } + @Override + public boolean isSharingShortcut(int callingUserId, @NonNull String callingPackage, + @NonNull String packageName, @NonNull String shortcutId, int userId, + @NonNull IntentFilter filter) { + Preconditions.checkStringNotEmpty(callingPackage, "callingPackage"); + Preconditions.checkStringNotEmpty(packageName, "packageName"); + Preconditions.checkStringNotEmpty(shortcutId, "shortcutId"); + + return ShortcutService.this.isSharingShortcut(callingUserId, callingPackage, + packageName, shortcutId, userId, filter); + } + private void updateCachedShortcutsInternal(int launcherUserId, @NonNull String callingPackage, @NonNull String packageName, @NonNull List shortcutIds, int userId, boolean doCache) { diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java index 2cbb6d5c5bd6f..06b344b3b94ff 100644 --- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java +++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java @@ -8595,6 +8595,56 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest { } } + public void testIsSharingShortcut() throws IntentFilter.MalformedMimeTypeException { + addManifestShortcutResource( + new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()), + R.xml.shortcut_share_targets); + updatePackageVersion(CALLING_PACKAGE_1, 1); + mService.mPackageMonitor.onReceive(getTestContext(), + genPackageAddIntent(CALLING_PACKAGE_1, USER_0)); + + setCaller(CALLING_PACKAGE_1, USER_0); + + final ShortcutInfo s1 = makeShortcutWithCategory("s1", + set("com.test.category.CATEGORY1", "com.test.category.CATEGORY2")); + final ShortcutInfo s2 = makeShortcutWithCategory("s2", + set("com.test.category.CATEGORY5", "com.test.category.CATEGORY6")); + final ShortcutInfo s3 = makeShortcut("s3"); + + assertTrue(mManager.setDynamicShortcuts(list(s1, s2, s3))); + assertShortcutIds(assertAllNotKeyFieldsOnly(mManager.getDynamicShortcuts()), + "s1", "s2", "s3"); + + IntentFilter filter_cat1 = new IntentFilter(); + filter_cat1.addDataType("text/plain"); + IntentFilter filter_cat5 = new IntentFilter(); + filter_cat5.addDataType("video/*"); + IntentFilter filter_any = new IntentFilter(); + filter_any.addDataType("*/*"); + + setCaller(LAUNCHER_1, USER_0); + mCallerPermissions.add(permission.MANAGE_APP_PREDICTIONS); + + assertTrue(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s1", USER_0, + filter_cat1)); + assertFalse(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s1", USER_0, + filter_cat5)); + assertTrue(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s1", USER_0, + filter_any)); + + assertFalse(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s2", USER_0, + filter_cat1)); + assertTrue(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s2", USER_0, + filter_cat5)); + assertTrue(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s2", USER_0, + filter_any)); + + assertFalse(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s3", USER_0, + filter_any)); + assertFalse(mInternal.isSharingShortcut(USER_0, LAUNCHER_1, CALLING_PACKAGE_1, "s4", USER_0, + filter_any)); + } + private Uri getFileUriFromResource(String fileName, int resId) throws IOException { File file = new File(getTestContext().getFilesDir(), fileName); // Make sure we are not leaving phantom files behind.