From 5ffacd06acd5bec7722cb1a3fccd392ee189ed06 Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Tue, 10 Apr 2018 15:00:18 -0400 Subject: [PATCH] Fix docs and bug in SliceManager#bindSlice Test: cts Change-Id: I812af1eda072c2af0b29454a05ea777d8f4c3c62 Fixes: 73123733 --- core/java/android/app/slice/SliceManager.java | 107 ++++++++++++-------------- 1 file changed, 48 insertions(+), 59 deletions(-) diff --git a/core/java/android/app/slice/SliceManager.java b/core/java/android/app/slice/SliceManager.java index dc8a3b92eaa9..ad49437fd31f 100644 --- a/core/java/android/app/slice/SliceManager.java +++ b/core/java/android/app/slice/SliceManager.java @@ -277,12 +277,12 @@ public class SliceManager { *
    *
  1. If the intent contains data that {@link ContentResolver#getType} is * {@link SliceProvider#SLICE_TYPE} then the data will be returned.
  2. - *
  3. 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.
  4. - *
  5. Lastly, if the intent explicitly points at an activity, and that activity has + *
  6. 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.
  7. + *
  8. Lastly, 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.
  9. *
  10. If no slice is found, then {@code null} is returned.
  11. *
* @param intent The intent associated with a slice. @@ -292,37 +292,12 @@ public class SliceManager { * @see Intent */ public @Nullable Uri mapIntentToUri(@NonNull Intent intent) { - Preconditions.checkNotNull(intent, "intent"); - Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null - || intent.getData() != null, - "Slice intent must be explicit %s", intent); ContentResolver resolver = mContext.getContentResolver(); - - // Check if the intent has data for the slice uri on it and use that - final Uri intentData = intent.getData(); - if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { - return intentData; - } + final Uri staticUri = resolveStatic(intent, resolver); + if (staticUri != null) return staticUri; // Otherwise ask the app - Intent queryIntent = new Intent(intent); - if (!queryIntent.hasCategory(CATEGORY_SLICE)) { - queryIntent.addCategory(CATEGORY_SLICE); - } - List providers = - 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, - PackageManager.GET_META_DATA); - if (resolve != null && resolve.activityInfo != null - && resolve.activityInfo.metaData != null - && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) { - return Uri.parse( - resolve.activityInfo.metaData.getString(SLICE_METADATA_KEY)); - } - return null; - } - String authority = providers.get(0).providerInfo.authority; + String authority = getAuthority(intent); + if (authority == null) return null; Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) .authority(authority).build(); try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { @@ -343,10 +318,43 @@ public class SliceManager { } } + private String getAuthority(Intent intent) { + Intent queryIntent = new Intent(intent); + if (!queryIntent.hasCategory(CATEGORY_SLICE)) { + queryIntent.addCategory(CATEGORY_SLICE); + } + List providers = + mContext.getPackageManager().queryIntentContentProviders(queryIntent, 0); + return providers != null && !providers.isEmpty() ? providers.get(0).providerInfo.authority + : null; + } + + private Uri resolveStatic(@NonNull Intent intent, ContentResolver resolver) { + Preconditions.checkNotNull(intent, "intent"); + Preconditions.checkArgument(intent.getComponent() != null || intent.getPackage() != null + || intent.getData() != null, + "Slice intent must be explicit %s", intent); + + // Check if the intent has data for the slice uri on it and use that + final Uri intentData = intent.getData(); + if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { + return intentData; + } + // There are no providers, see if this activity has a direct link. + ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent, + PackageManager.GET_META_DATA); + if (resolve != null && resolve.activityInfo != null + && resolve.activityInfo.metaData != null + && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) { + return Uri.parse( + resolve.activityInfo.metaData.getString(SLICE_METADATA_KEY)); + } + return null; + } + /** - * Turns a slice intent into slice content. Expects an explicit intent. If there is no - * {@link android.content.ContentProvider} associated with the given intent this will throw - * {@link IllegalArgumentException}. + * Turns a slice intent into slice content. Is a shortcut to perform the action + * of both {@link #mapIntentToUri(Intent)} and {@link #bindSlice(Uri, List)} at once. * * @param intent The intent associated with a slice. * @param supportedSpecs List of supported specs. @@ -362,28 +370,11 @@ public class SliceManager { || intent.getData() != null, "Slice intent must be explicit %s", intent); ContentResolver resolver = mContext.getContentResolver(); - - // Check if the intent has data for the slice uri on it and use that - final Uri intentData = intent.getData(); - if (intentData != null && SliceProvider.SLICE_TYPE.equals(resolver.getType(intentData))) { - return bindSlice(intentData, supportedSpecs); - } + final Uri staticUri = resolveStatic(intent, resolver); + if (staticUri != null) return bindSlice(staticUri, supportedSpecs); // Otherwise ask the app - List providers = - mContext.getPackageManager().queryIntentContentProviders(intent, 0); - if (providers == null || providers.isEmpty()) { - // There are no providers, see if this activity has a direct link. - ResolveInfo resolve = mContext.getPackageManager().resolveActivity(intent, - PackageManager.GET_META_DATA); - if (resolve != null && resolve.activityInfo != null - && resolve.activityInfo.metaData != null - && resolve.activityInfo.metaData.containsKey(SLICE_METADATA_KEY)) { - return bindSlice(Uri.parse(resolve.activityInfo.metaData - .getString(SLICE_METADATA_KEY)), supportedSpecs); - } - return null; - } - String authority = providers.get(0).providerInfo.authority; + String authority = getAuthority(intent); + if (authority == null) return null; Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) .authority(authority).build(); try (ContentProviderClient provider = resolver.acquireContentProviderClient(uri)) { @@ -392,8 +383,6 @@ public class SliceManager { } Bundle extras = new Bundle(); extras.putParcelable(SliceProvider.EXTRA_INTENT, intent); - extras.putParcelableArrayList(SliceProvider.EXTRA_SUPPORTED_SPECS, - new ArrayList<>(supportedSpecs)); final Bundle res = provider.call(SliceProvider.METHOD_MAP_INTENT, null, extras); if (res == null) { return null; -- 2.11.0