SliceProvider

public abstract class SliceProvider extends ContentProvider


A SliceProvider allows an app to provide Slices to the Android OS. A slice is a piece of app content and actions that can be displayed outside of the app in Android system surfaces or within another app. Slices are identified by a Uri and a SliceProvider allows your app to provide a slice based on a uri.

The primary method to implement in SliceProvider is onBindSlice which is called whenever something wants to display a slice from your app. An app can have multiple slices all served from the same slice provider, the Uri passed to onBindSlice will identify the specific slice being requested.

public MySliceProvider extends SliceProvider {

     public Slice onBindSlice(Uri sliceUri) {
         String path = sliceUri.getPath();
         switch (path) {
             case "/weather":
                 return createWeatherSlice(sliceUri);
             case "/traffic":
                 return createTrafficSlice(sliceUri);
         }
         return null;
     }
}

Slices are constructed with androidx.slice.builders.TemplateSliceBuilders.

Slices are not currently live content. They are bound once and shown to the user. If the content in the slice changes due to user interaction or an update in the data being displayed, then notifyChange should be used to notify the system to request the latest slice from the app.

The provider needs to be declared in the manifest to provide the authority for the app. The authority for most slices is expected to match the package of the application.

<provider
    android:name="com.android.mypkg.MySliceProvider"
    android:authorities="com.android.mypkg" />

Slices can also be identified by an intent. To link an intent with a slice, the slice provider must have an IntentFilter matching the slice intent. When a slice is being requested via an intent, onMapIntentToUri will be called and is expected to return an appropriate Uri representing the slice.

<provider
    android:name="com.android.mypkg.MySliceProvider"
    android:authorities="com.android.mypkg">
    <intent-filter>
        <action android:name="android.intent.action.MY_SLICE_INTENT" />
        <category android:name="android.app.slice.category.SLICE" />
    </intent-filter>
</provider>
See also
Slice

Summary

Public constructors

SliceProvider(@NonNull String[] autoGrantPermissions)

A version of constructing a SliceProvider that allows autogranting slice permissions to apps that hold specific platform permissions.

Public methods

void
final int
bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values)
@Nullable Bundle
call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras)

Handles the call to SliceProvider.

final @Nullable Uri
@RequiresApi(value = 19)
canonicalize(@NonNull Uri url)
final int
delete(
    @NonNull Uri uri,
    @Nullable String selection,
    @Nullable String[] selectionArgs
)
@NonNull List<Uri>

Returns a list of slice URIs that are currently pinned.

final @Nullable String
final @Nullable Uri
abstract @Nullable Slice
@RequiresApi(value = 19)
onBindSlice(@NonNull Uri sliceUri)

Implemented to create a slice.

final boolean
@Nullable PendingIntent
onCreatePermissionRequest(
    @NonNull Uri sliceUri,
    @NonNull String callingPackage
)

Called when an app requests a slice it does not have write permission to the uri for.

abstract boolean

Implement this to initialize your slice provider on startup.

@NonNull Collection<Uri>

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

@NonNull Uri
@RequiresApi(value = 19)
onMapIntentToUri(@NonNull Intent intent)

This method must be overridden if an IntentFilter is specified on the SliceProvider.

void
@RequiresApi(value = 19)
onSlicePinned(@NonNull Uri sliceUri)

Called to inform an app that a slice has been pinned.

void
@RequiresApi(value = 19)
onSliceUnpinned(@NonNull Uri sliceUri)

Called to inform an app that a slices is no longer pinned.

final @Nullable Cursor
@RequiresApi(value = 28)
query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable Bundle queryArgs,
    @Nullable CancellationSignal cancellationSignal
)
final @Nullable Cursor
query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder
)
final @Nullable Cursor
@RequiresApi(value = 16)
query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder,
    @Nullable CancellationSignal cancellationSignal
)
final int
update(
    @NonNull Uri uri,
    @Nullable ContentValues values,
    @Nullable String selection,
    @Nullable String[] selectionArgs
)

Inherited Constants

From android.content.ComponentCallbacks2
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Inherited methods

From android.content.ContentProvider
ContentProviderResult[]
applyBatch(
    String authority,
    ArrayList<ContentProviderOperation> operations
)
Bundle
call(String authority, String method, String arg, Bundle extras)
final ContentProvider.CallingIdentity
void
dump(FileDescriptor fd, PrintWriter writer, String[] args)
final AttributionSource
final String
final String
final String
final Context
final PathPermission[]
final String
String[]
getStreamTypes(Uri uri, String mimeTypeFilter)
String
final String
boolean
void
void
void
void
onTrimMemory(int level)
AssetFileDescriptor
openAssetFile(Uri uri, String mode)
ParcelFileDescriptor
openFile(Uri uri, String mode)
final ParcelFileDescriptor
openFileHelper(Uri uri, String mode)
ParcelFileDescriptor
<T> openPipeHelper(
    Uri uri,
    String mimeType,
    Bundle opts,
    T args,
    ContentProvider.PipeDataWriter<T> func
)
AssetFileDescriptor
openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)
boolean
refresh(Uri uri, Bundle extras, CancellationSignal cancellationSignal)
final Context
final void
final void
setPathPermissions(PathPermission[] permissions)
final void
final void
void
Uri

Public constructors

SliceProvider

Added in 1.1.0-alpha02
public SliceProvider()

SliceProvider

public SliceProvider(@NonNull String[] autoGrantPermissions)

A version of constructing a SliceProvider that allows autogranting slice permissions to apps that hold specific platform permissions.

When an app tries to bind a slice from a provider that it does not have access to, the provider will check if the caller holds permissions to any of the autoGrantPermissions specified, if they do they will be granted persisted uri access to all slices of this provider.

Parameters
@NonNull String[] autoGrantPermissions

List of permissions that holders are auto-granted access to slices.

Public methods

attachInfo

public void attachInfo(@Nullable Context context, @Nullable ProviderInfo info)

bulkInsert

Added in 1.1.0-alpha02
public final int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values)

call

public @Nullable Bundle call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras)

Handles the call to SliceProvider.

This function is unsupported for sdk <19. For sdk 28 and above the call is handled by android.app.slice.SliceProvider

canonicalize

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public final @Nullable Uri canonicalize(@NonNull Uri url)

delete

Added in 1.1.0-alpha02
public final int delete(
    @NonNull Uri uri,
    @Nullable String selection,
    @Nullable String[] selectionArgs
)

getPinnedSlices

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public @NonNull List<UrigetPinnedSlices()

Returns a list of slice URIs that are currently pinned.

Returns
@NonNull List<Uri>

All pinned slices.

getType

Added in 1.1.0-alpha02
public final @Nullable String getType(@NonNull Uri uri)

insert

Added in 1.1.0-alpha02
public final @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values)

onBindSlice

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public abstract @Nullable Slice onBindSlice(@NonNull Uri sliceUri)

Implemented to create a slice.

onBindSlice should return as quickly as possible so that the UI tied to this slice can be responsive. No network or other IO will be allowed during onBindSlice. Any loading that needs to be done should happen in the background with a call to notifyChange when the app is ready to provide the complete data in onBindSlice.

See also
Slice
HINT_PARTIAL

onCreate

Added in 1.1.0-alpha02
public final boolean onCreate()

onCreatePermissionRequest

Added in 1.1.0-alpha02
public @Nullable PendingIntent onCreatePermissionRequest(
    @NonNull Uri sliceUri,
    @NonNull String callingPackage
)

Called when an app requests a slice it does not have write permission to the uri for.

The return value will be the action on a slice that prompts the user that the calling app wants to show slices from this app. Returning null will use the default implementation that launches a dialog that allows the user to grant access to this slice. Apps that do not want to allow this user grant, can override this and instead launch their own dialog with different behavior.

Parameters
@NonNull Uri sliceUri

the Uri of the slice attempting to be bound.

@NonNull String callingPackage

the packageName of the app requesting the slice

onCreateSliceProvider

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public abstract boolean onCreateSliceProvider()

Implement this to initialize your slice provider on startup. This method is called for all registered slice providers on the application main thread at application launch time. It must not perform lengthy operations, or application startup will be delayed.

You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the slice provider is used (via #onBindSlice, etc). Deferred initialization keeps application startup fast, avoids unnecessary work if the provider turns out not to be needed, and stops database errors (such as a full disk) from halting application launch.

Returns
boolean

true if the provider was successfully loaded, false otherwise

onGetSliceDescendants

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public @NonNull Collection<UrionGetSliceDescendants(@NonNull Uri uri)

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

Implementing this is optional for a SliceProvider, but does provide a good discovery mechanism for finding slice Uris.

Parameters
@NonNull Uri uri

The uri to look for descendants under.

Returns
@NonNull Collection<Uri>

All slices within the space.

onMapIntentToUri

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public @NonNull Uri onMapIntentToUri(@NonNull Intent intent)

This method must be overridden if an IntentFilter is specified on the SliceProvider. In that case, this method can be called and is expected to return a non-null Uri representing a slice. Otherwise this will throw UnsupportedOperationException.

Returns
@NonNull Uri

Uri representing the slice associated with the provided intent.

See also
Slice

onSlicePinned

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public void onSlicePinned(@NonNull Uri sliceUri)

Called to inform an app that a slice has been pinned.

Pinning is a way that slice hosts use to notify apps of which slices they care about updates for. When a slice is pinned the content is expected to be relatively fresh and kept up to date.

Being pinned does not provide any escalated privileges for the slice provider. So apps should do things such as turn on syncing or schedule a job in response to a onSlicePinned.

Pinned state is not persisted through a reboot, and apps can expect a new call to onSlicePinned for any slices that should remain pinned after a reboot occurs.

Parameters
@NonNull Uri sliceUri

The uri of the slice being unpinned.

See also
onSliceUnpinned

onSliceUnpinned

Added in 1.1.0-alpha02
@RequiresApi(value = 19)
public void onSliceUnpinned(@NonNull Uri sliceUri)

Called to inform an app that a slices is no longer pinned.

This means that no other apps on the device care about updates to this slice anymore and therefore it is not important to be updated. Any syncs or jobs related to this slice should be cancelled.

See also
onSlicePinned

query

Added in 1.1.0-alpha02
@RequiresApi(value = 28)
public final @Nullable Cursor query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable Bundle queryArgs,
    @Nullable CancellationSignal cancellationSignal
)

query

Added in 1.1.0-alpha02
public final @Nullable Cursor query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder
)

query

Added in 1.1.0-alpha02
@RequiresApi(value = 16)
public final @Nullable Cursor query(
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder,
    @Nullable CancellationSignal cancellationSignal
)

update

Added in 1.1.0-alpha02
public final int update(
    @NonNull Uri uri,
    @Nullable ContentValues values,
    @Nullable String selection,
    @Nullable String[] selectionArgs
)