The second Android 11 Developer Preview is now available, test it out and share your feedback.

SliceUtils

open class SliceUtils
kotlin.Any
   ↳ androidx.slice.SliceUtils

Utilities for dealing with slices.

Summary

Nested classes

open

Holds options for how to handle SliceItems that cannot be serialized.

abstract

A listener used to receive events on slices parsed with parseSlice(Context, InputStream, String, SliceActionListener).

open

Exception thrown during parseSlice(Context, InputStream, String, SliceActionListener).

Public methods

open static Slice
parseSlice(@NonNull context: Context, @NonNull input: InputStream, @NonNull encoding: String, @NonNull listener: SliceUtils.SliceActionListener)

Parse a slice that has been previously serialized.

open static Unit
serializeSlice(@NonNull s: Slice, @NonNull context: Context, @NonNull output: OutputStream, @NonNull options: SliceUtils.SerializeOptions)

Serialize a slice to an OutputStream.

open static Slice
stripSlice(@NonNull s: Slice, mode: Int, isScrollable: Boolean)

Attempt to remove any extra content from the slice.

Public methods

parseSlice

@NonNull open static fun parseSlice(
    @NonNull context: Context,
    @NonNull input: InputStream,
    @NonNull encoding: String,
    @NonNull listener: SliceUtils.SliceActionListener
): Slice

Parse a slice that has been previously serialized.

Parses a slice that was serialized with serializeSlice.

Note: Slices returned by this cannot be passed to SliceConvert#unwrap(Slice).

Parameters
input InputStream: The input stream to read from.
encoding String: The encoding to read as.
listener SliceUtils.SliceActionListener: Listener used to handle actions when reconstructing the slice.
Exceptions
SliceParseException if the InputStream cannot be parsed.

serializeSlice

open static fun serializeSlice(
    @NonNull s: Slice,
    @NonNull context: Context,
    @NonNull output: OutputStream,
    @NonNull options: SliceUtils.SerializeOptions
): Unit

Serialize a slice to an OutputStream.

The serialized slice can later be read into slice form again with parseSlice. Some slice types cannot be serialized, their handling is controlled by SerializeOptions.

Parameters
s Slice: The slice to serialize.
context Context: Context used to load any resources in the slice.
output OutputStream: The output of the serialization.
options SliceUtils.SerializeOptions: Options defining how to handle non-serializable items.
Exceptions
IllegalArgumentException if the slice cannot be serialized using the given options.

stripSlice

@NonNull open static fun stripSlice(
    @NonNull s: Slice,
    mode: Int,
    isScrollable: Boolean
): Slice

Attempt to remove any extra content from the slice.

This is meant to be used alongside serializeSlice to reduce the size of a slice by only including data for the slice to be displayed in a specific mode.

Parameters
s Slice: The slice to strip.
mode Int: The mode that will be used with SliceView#setMode to display the slice.
isScrollable Boolean: The value that will be used with SliceView#setScrollable to display the slice.
Return
Slice returns A new smaller slice if stripping can be done, or the original slice if no content can be removed.