From 5e676a22a0c0cadd215be24d3bb692dba2bfef35 Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Thu, 8 Mar 2018 14:18:55 -0500 Subject: Add intent category for slices to help identify them Test: cts, testMapIntentToSlice Bug: 73123733 Change-Id: I75c3e132861d04301f5c856c235eadfc4be8f9bc --- api/current.txt | 1 + core/java/android/app/slice/SliceManager.java | 32 +++++++++++++++++++++++++- core/java/android/app/slice/SliceProvider.java | 8 ++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/api/current.txt b/api/current.txt index 7e30e857eec5..75c983cea4c1 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7270,6 +7270,7 @@ package android.app.slice { method public android.net.Uri mapIntentToUri(android.content.Intent); method public void pinSlice(android.net.Uri, java.util.List); method public void unpinSlice(android.net.Uri); + field public static final java.lang.String CATEGORY_SLICE = "android.app.slice.category.SLICE"; field public static final java.lang.String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; } diff --git a/core/java/android/app/slice/SliceManager.java b/core/java/android/app/slice/SliceManager.java index 9f9ce8d3f386..4f3cd63841eb 100644 --- a/core/java/android/app/slice/SliceManager.java +++ b/core/java/android/app/slice/SliceManager.java @@ -18,6 +18,8 @@ package android.app.slice; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.SdkConstant; +import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SystemService; import android.content.ContentProviderClient; import android.content.ContentResolver; @@ -59,6 +61,18 @@ public class SliceManager { public static final String ACTION_REQUEST_SLICE_PERMISSION = "android.intent.action.REQUEST_SLICE_PERMISSION"; + /** + * Category used to resolve intents that can be rendered as slices. + *

+ * This category should be included on intent filters on providers that extend + * {@link SliceProvider}. + * @see SliceProvider + * @see SliceProvider#onMapIntentToUri(Intent) + * @see #mapIntentToUri(Intent) + */ + @SdkConstant(SdkConstantType.INTENT_CATEGORY) + public static final String CATEGORY_SLICE = "android.app.slice.category.SLICE"; + /** * The meta-data key that allows an activity to easily be linked directly to a slice. *

@@ -226,6 +240,18 @@ public class SliceManager { /** * Turns a slice intent into a slice uri. Expects an explicit intent. + *

+ * This goes through a several stage resolution process to determine if any slice + * can represent this intent. + * - If the intent contains data that {@link ContentResolver#getType} is + * {@link SliceProvider#SLICE_TYPE} then the data will be returned. + * - If the intent with {@link #CATEGORY_SLICE} added resolves to a provider, then + * the provider will be asked to {@link SliceProvider#onMapIntentToUri} and that result + * will be returned. + * - Lastly, if the intent explicitly points at an activity, and that activity has + * meta-data for key {@link #SLICE_METADATA_KEY}, then the Uri specified there will be + * returned. + * - If no slice is found, then {@code null} is returned. * * @param intent The intent associated with a slice. * @return The Slice Uri provided by the app or null if none exists. @@ -245,8 +271,12 @@ public class SliceManager { return intentData; } // Otherwise ask the app + Intent queryIntent = new Intent(intent); + if (!queryIntent.hasCategory(CATEGORY_SLICE)) { + queryIntent.addCategory(CATEGORY_SLICE); + } List providers = - mContext.getPackageManager().queryIntentContentProviders(intent, 0); + mContext.getPackageManager().queryIntentContentProviders(queryIntent, 0); if (providers == null || providers.isEmpty()) { // There are no providers, see if this activity has a direct link. ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent, diff --git a/core/java/android/app/slice/SliceProvider.java b/core/java/android/app/slice/SliceProvider.java index 64a5181668b3..df32fb9c2e75 100644 --- a/core/java/android/app/slice/SliceProvider.java +++ b/core/java/android/app/slice/SliceProvider.java @@ -81,6 +81,7 @@ import java.util.concurrent.CountDownLatch; * android:authorities="com.example.mypkg"> * * + * * * } * @@ -253,8 +254,13 @@ public abstract class SliceProvider extends ContentProvider { * In that case, this method can be called and is expected to return a non-null Uri representing * a slice. Otherwise this will throw {@link UnsupportedOperationException}. * + * Any intent filter added to a slice provider should also contain + * {@link SliceManager#CATEGORY_SLICE}, because otherwise it will not be detected by + * {@link SliceManager#mapIntentToUri(Intent)}. + * * @return Uri representing the slice associated with the provided intent. - * @see {@link Slice} + * @see Slice + * @see SliceManager#mapIntentToUri(Intent) */ public @NonNull Uri onMapIntentToUri(Intent intent) { throw new UnsupportedOperationException( -- cgit v1.2.3-59-g8ed1b