SliceUtils

@RequiresApi(value = 19)
public class SliceUtils


Utilities for dealing with slices.

Summary

Nested types

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

A listener used to receive events on slices parsed with parseSlice.

Exception thrown during parseSlice.

Public methods

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

Parse a slice that has been previously serialized.

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

Serialize a slice to an OutputStream.

static @NonNull Slice
stripSlice(@NonNull Slice s, int mode, boolean isScrollable)

Attempt to remove any extra content from the slice.

Public methods

parseSlice

Added in 1.1.0-alpha02
public static @NonNull Slice parseSlice(
    @NonNull Context context,
    @NonNull InputStream input,
    @NonNull String encoding,
    @NonNull SliceUtils.SliceActionListener listener
)

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 unwrap.

Parameters
@NonNull InputStream input

The input stream to read from.

@NonNull String encoding

The encoding to read as.

@NonNull SliceUtils.SliceActionListener listener

Listener used to handle actions when reconstructing the slice.

Throws
androidx.slice.SliceUtils.SliceParseException

if the InputStream cannot be parsed.

java.io.IOException

serializeSlice

Added in 1.1.0-alpha02
public static void serializeSlice(
    @NonNull Slice s,
    @NonNull Context context,
    @NonNull OutputStream output,
    @NonNull SliceUtils.SerializeOptions options
)

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
@NonNull Slice s

The slice to serialize.

@NonNull Context context

Context used to load any resources in the slice.

@NonNull OutputStream output

The output of the serialization.

@NonNull SliceUtils.SerializeOptions options

Options defining how to handle non-serializable items.

Throws
java.lang.IllegalArgumentException

if the slice cannot be serialized using the given options.

stripSlice

Added in 1.1.0-alpha02
public static @NonNull Slice stripSlice(@NonNull Slice s, int mode, boolean isScrollable)

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
@NonNull Slice s

The slice to strip.

int mode

The mode that will be used with setMode to display the slice.

boolean isScrollable

The value that will be used with setScrollable to display the slice.

Returns
@NonNull Slice

returns A new smaller slice if stripping can be done, or the original slice if no content can be removed.