Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

SliceManager

public class SliceManager
extends Object

java.lang.Object
   ↳ android.app.slice.SliceManager


Class to handle interactions with Slices.

The SliceManager manages permissions and pinned state for slices.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument SliceManager.class.

Summary

Constants

String CATEGORY_SLICE

Category used to resolve intents that can be rendered as slices.

String SLICE_METADATA_KEY

The meta-data key that allows an activity to easily be linked directly to a slice.

Public methods

Slice bindSlice(Uri uri, Set<SliceSpec> supportedSpecs)

Turns a slice Uri into slice content.

Slice bindSlice(Intent intent, Set<SliceSpec> supportedSpecs)

Turns a slice intent into slice content.

int checkSlicePermission(Uri uri, int pid, int uid)

Determine whether a particular process and user ID has been granted permission to access a specific slice URI.

List<Uri> getPinnedSlices()

Get the list of currently pinned slices for this app.

Set<SliceSpec> getPinnedSpecs(Uri uri)

Get the current set of specs for a pinned slice.

Collection<Uri> getSliceDescendants(Uri uri)

Obtains a list of slices that are descendants of the specified Uri.

void grantSlicePermission(String toPackage, Uri uri)

Grant permission to access a specific slice Uri to another package.

Uri mapIntentToUri(Intent intent)

Turns a slice intent into a slice uri.

void pinSlice(Uri uri, Set<SliceSpec> specs)

Ensures that a slice is in a pinned state.

void revokeSlicePermission(String toPackage, Uri uri)

Remove permissions to access a particular content provider Uri that were previously added with grantSlicePermission(String, Uri) for a specific target package.

void unpinSlice(Uri uri)

Remove a pin for a slice.

Inherited methods

Constants

CATEGORY_SLICE

added in API level 28
public static final String CATEGORY_SLICE

Category used to resolve intents that can be rendered as slices.

This category should be included on intent filters on providers that extend SliceProvider.

Constant Value: "android.app.slice.category.SLICE"

SLICE_METADATA_KEY

added in API level 28
public static final String SLICE_METADATA_KEY

The meta-data key that allows an activity to easily be linked directly to a slice.

An activity can be statically linked to a slice uri by including a meta-data item for this key that contains a valid slice uri for the same application declaring the activity.

 <activity android:name="com.example.mypkg.MyActivity">
     <meta-data android:name="android.metadata.SLICE_URI"
                android:value="content://com.example.mypkg/main_slice" />
  </activity>
 

Constant Value: "android.metadata.SLICE_URI"

Public methods

bindSlice

added in API level 28
public Slice bindSlice (Uri uri, 
                Set<SliceSpec> supportedSpecs)

Turns a slice Uri into slice content.

Parameters
uri Uri: The URI to a slice provider

This value must never be null.

supportedSpecs Set: List of supported specs.

This value must never be null.

Returns
Slice The Slice provided by the app or null if none is given.

See also:

bindSlice

added in API level 28
public Slice bindSlice (Intent intent, 
                Set<SliceSpec> supportedSpecs)

Turns a slice intent into slice content. Is a shortcut to perform the action of both mapIntentToUri(Intent) and bindSlice(Uri, Set) at once.

Parameters
intent Intent: The intent associated with a slice.

This value must never be null.

supportedSpecs Set: List of supported specs.

This value must never be null.

Returns
Slice The Slice provided by the app or null if none is given.

checkSlicePermission

added in API level 28
public int checkSlicePermission (Uri uri, 
                int pid, 
                int uid)

Determine whether a particular process and user ID has been granted permission to access a specific slice URI.

Parameters
uri Uri: The uri that is being checked.

This value must never be null.

pid int: The process ID being checked against. Must be > 0.

uid int: The user ID being checked against. A uid of 0 is the root user, which will pass every permission check.

Returns
int PackageManager.PERMISSION_GRANTED if the given pid/uid is allowed to access that uri, or PackageManager.PERMISSION_DENIED if it is not.

Value is PERMISSION_GRANTED or PERMISSION_DENIED.

getPinnedSlices

added in API level 28
public List<Uri> getPinnedSlices ()

Get the list of currently pinned slices for this app.

Returns
List<Uri>

This value will never be null.

getPinnedSpecs

added in API level 28
public Set<SliceSpec> getPinnedSpecs (Uri uri)

Get the current set of specs for a pinned slice.

This is the set of specs supported for a specific pinned slice. It will take into account all clients and returns only specs supported by all.

Parameters
uri Uri

Returns
Set<SliceSpec>

This value will never be null.

See also:

getSliceDescendants

added in API level 28
public Collection<Uri> getSliceDescendants (Uri uri)

Obtains a list of slices that are descendants of the specified Uri.

Not all slice providers will implement this functionality, in which case, an empty collection will be returned.

This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
uri Uri: The uri to look for descendants under.

This value must never be null.

Returns
Collection<Uri> All slices within the space.

This value will never be null.

grantSlicePermission

added in API level 28
public void grantSlicePermission (String toPackage, 
                Uri uri)

Grant permission to access a specific slice Uri to another package.

Parameters
toPackage String: The package you would like to allow to access the Uri.

This value must never be null.

uri Uri: The Uri you would like to grant access to.

This value must never be null.

mapIntentToUri

added in API level 28
public Uri mapIntentToUri (Intent intent)

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.

  1. If the intent contains data that ContentResolver.getType(Uri) is SliceProvider.SLICE_TYPE then the data will be returned.
  2. If the intent explicitly points at an activity, and that activity has meta-data for key SLICE_METADATA_KEY, then the Uri specified there will be returned.
  3. Lastly, if the intent with CATEGORY_SLICE added resolves to a provider, then the provider will be asked to SliceProvider.onMapIntentToUri(Intent) and that result will be returned.
  4. If no slice is found, then null is returned.

Parameters
intent Intent: The intent associated with a slice.

This value must never be null.

Returns
Uri The Slice Uri provided by the app or null if none exists.

pinSlice

added in API level 28
public void pinSlice (Uri uri, 
                Set<SliceSpec> specs)

Ensures that a slice is in a pinned state.

Pinned state is not persisted across reboots, so apps are expected to re-pin any slices they still care about after a reboot.

This may only be called by apps that are the default launcher for the device or the default voice interaction service. Otherwise will throw SecurityException.

Parameters
uri Uri: The uri of the slice being pinned.

This value must never be null.

specs Set: The list of supported SliceSpecs of the callback.

This value must never be null.

revokeSlicePermission

added in API level 28
public void revokeSlicePermission (String toPackage, 
                Uri uri)

Remove permissions to access a particular content provider Uri that were previously added with grantSlicePermission(String, Uri) for a specific target package. The given Uri will match all previously granted Uris that are the same or a sub-path of the given Uri. That is, revoking "content://foo/target" will revoke both "content://foo/target" and "content://foo/target/sub", but not "content://foo". It will not remove any prefix grants that exist at a higher level.

Parameters
toPackage String: The package you would like to allow to access the Uri.

This value must never be null.

uri Uri: The Uri you would like to revoke access to.

This value must never be null.

unpinSlice

added in API level 28
public void unpinSlice (Uri uri)

Remove a pin for a slice.

If the slice has no other pins/callbacks then the slice will be unpinned.

This may only be called by apps that are the default launcher for the device or the default voice interaction service. Otherwise will throw SecurityException.

Parameters
uri Uri: The uri of the slice being unpinned.

This value must never be null.