From ca09f5b941e3aaff21c0fce98987f0f84ed9180c Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 1 Jul 2021 12:57:27 -0600 Subject: [PATCH] Bring Predicate to IntentFilter. It's what IntentFilter is actually designed to do internally, so give developers a useful functional interface. Continue offering nuance of using MIME types as-is or offering to resolve them first through two different asPredicate() methods. Bug: 161252188 Test: atest IntentFilterTest Change-Id: Iefd9e03369d72676809ddc8d924703ad7921fde5 --- core/api/current.txt | 2 ++ core/java/android/content/IntentFilter.java | 34 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index 3d88122cd11bc..9929b37f532fa 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -11376,6 +11376,8 @@ package android.content { method public final void addDataScheme(String); method public final void addDataSchemeSpecificPart(String, int); method public final void addDataType(String) throws android.content.IntentFilter.MalformedMimeTypeException; + method @NonNull public java.util.function.Predicate asPredicate(); + method @NonNull public java.util.function.Predicate asPredicateWithTypeResolution(@NonNull android.content.ContentResolver); method public final java.util.Iterator authoritiesIterator(); method public final java.util.Iterator categoriesIterator(); method public final int countActions(); diff --git a/core/java/android/content/IntentFilter.java b/core/java/android/content/IntentFilter.java index c5badb9148ea7..32827ae11e0b3 100644 --- a/core/java/android/content/IntentFilter.java +++ b/core/java/android/content/IntentFilter.java @@ -17,6 +17,7 @@ package android.content; import android.annotation.IntDef; +import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SystemApi; import android.compat.annotation.UnsupportedAppUsage; @@ -44,8 +45,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.function.BiConsumer; +import java.util.function.Predicate; /** * Structured description of Intent values to be matched. An IntentFilter can @@ -147,6 +150,8 @@ import java.util.function.BiConsumer; * will only match an Intent that does not have any categories. */ public class IntentFilter implements Parcelable { + private static final String TAG = "IntentFilter"; + private static final String AGLOB_STR = "aglob"; private static final String SGLOB_STR = "sglob"; private static final String PREFIX_STR = "prefix"; @@ -1760,6 +1765,35 @@ public class IntentFilter implements Parcelable { return null; } + /** + * Return a {@link Predicate} which tests whether this filter matches the + * given intent. + *

+ * The intent's type will always be tested using a simple + * {@link Intent#getType()} check. To instead perform a detailed type + * resolution before matching, use + * {@link #asPredicateWithTypeResolution(ContentResolver)}. + */ + public @NonNull Predicate asPredicate() { + return i -> match(null, i, false, TAG) >= 0; + } + + /** + * Return a {@link Predicate} which tests whether this filter matches the + * given intent. + *

+ * The intent's type will always be resolved by calling + * {@link Intent#resolveType(ContentResolver)} before matching. + * + * @param resolver to be used when calling + * {@link Intent#resolveType(ContentResolver)} before matching. + */ + public @NonNull Predicate asPredicateWithTypeResolution( + @NonNull ContentResolver resolver) { + Objects.requireNonNull(resolver); + return i -> match(resolver, i, true, TAG) >= 0; + } + /** * Test whether this filter matches the given intent. *