diff --git a/core/api/current.txt b/core/api/current.txt index 9ba615bbc6d71..7a00119faefaa 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. *