diff options
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/app/slice/SliceManager.java | 32 | ||||
| -rw-r--r-- | 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<android.app.slice.SliceSpec>); 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; @@ -60,6 +62,18 @@ public class SliceManager { "android.intent.action.REQUEST_SLICE_PERMISSION"; /** + * Category used to resolve intents that can be rendered as slices. + * <p> + * 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. * <p> * An activity can be statically linked to a slice uri by including a meta-data item @@ -226,6 +240,18 @@ public class SliceManager { /** * Turns a slice intent into a slice uri. Expects an explicit intent. + * <p> + * 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<ResolveInfo> 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"> * <intent-filter> * <action android:name="com.example.mypkg.intent.action.MY_SLICE_INTENT" /> + * <category android:name="android.app.slice.category.SLICE" /> * </intent-filter> * </provider>} * </pre> @@ -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( |