public class

SelectionBuilder

extends java.lang.Object

 java.lang.Object

↳androidx.slice.builders.SelectionBuilder

Gradle dependencies

compile group: 'androidx.slice', name: 'slice-builders', version: '1.1.0-alpha02'

  • groupId: androidx.slice
  • artifactId: slice-builders
  • version: 1.1.0-alpha02

Artifact androidx.slice:slice-builders:1.1.0-alpha02 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.slice:slice-builders com.android.support:slices-builders

Overview

Builder to construct a selection which can be added to a ListBuilder. A selection presents a list of options to the user and allows the user to select exactly one option.

Summary

Constructors
publicSelectionBuilder()

Creates a SelectionBuilder with no options.

Methods
public SelectionBuilderaddOption(java.lang.String optionKey, java.lang.CharSequence optionText)

Adds an option to this SelectionBuilder.

public voidcheck()

public java.lang.CharSequencegetContentDescription()

public PendingIntentgetInputAction()

public intgetLayoutDirection()

public java.util.List<Pair>getOptions()

public SliceActiongetPrimaryAction()

public java.lang.StringgetSelectedOption()

public java.lang.CharSequencegetSubtitle()

public java.lang.CharSequencegetTitle()

public SelectionBuildersetContentDescription(java.lang.CharSequence contentDescription)

Sets the content description.

public SelectionBuildersetInputAction(PendingIntent inputAction)

Sets the PendingIntent to send when the selection is made or changed.

public SelectionBuildersetLayoutDirection(int layoutDirection)

Sets the layout direction.

public SelectionBuildersetPrimaryAction(SliceAction primaryAction)

Sets the primary action for the selection slice.

public SelectionBuildersetSelectedOption(java.lang.String selectedOption)

Sets which option is selected by default.

public SelectionBuildersetSubtitle(java.lang.CharSequence subtitle)

Sets the subtitle.

public SelectionBuildersetTitle(java.lang.CharSequence title)

Sets the title.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SelectionBuilder()

Creates a SelectionBuilder with no options.

Methods

public SelectionBuilder addOption(java.lang.String optionKey, java.lang.CharSequence optionText)

Adds an option to this SelectionBuilder. The new option will be appended to the list of options.

Parameters:

optionKey: the key that will be returned if the user selects this option
optionText: the text that will be displayed to the user for this option

Returns:

this SelectionBuilder

public SelectionBuilder setPrimaryAction(SliceAction primaryAction)

Sets the primary action for the selection slice. The action specified here will be sent when the whole slice is clicked, or when the app presenting the slice is not capable of rendering a selection interface.

Parameters:

primaryAction: the action to trigger when the user clicks the whole slice

Returns:

this SelectionBuilder

public SelectionBuilder setInputAction(PendingIntent inputAction)

Sets the PendingIntent to send when the selection is made or changed. The intent will include an extra with the key Slice.EXTRA_SELECTION and a java.lang.String value containing the key of the key of the selected option.

Parameters:

inputAction: the intent to send when the user makes or changes the selection

Returns:

this SelectionBuilder

public SelectionBuilder setSelectedOption(java.lang.String selectedOption)

Sets which option is selected by default.

Parameters:

selectedOption: the key of the selected option

Returns:

this SelectionBuilder

public SelectionBuilder setTitle(java.lang.CharSequence title)

Sets the title.

Parameters:

title: the title

Returns:

this SelectionBuilder

public SelectionBuilder setSubtitle(java.lang.CharSequence subtitle)

Sets the subtitle.

Parameters:

subtitle: the subtitle

Returns:

this SelectionBuilder

public SelectionBuilder setContentDescription(java.lang.CharSequence contentDescription)

Sets the content description.

Parameters:

contentDescription: the content description

Returns:

this SelectionBuilder

public SelectionBuilder setLayoutDirection(int layoutDirection)

Sets the layout direction.

Parameters:

layoutDirection: the layout direction

Returns:

this SelectionBuilder

public java.util.List<Pair> getOptions()

public SliceAction getPrimaryAction()

public PendingIntent getInputAction()

public java.lang.String getSelectedOption()

public java.lang.CharSequence getTitle()

public java.lang.CharSequence getSubtitle()

public java.lang.CharSequence getContentDescription()

public int getLayoutDirection()

public void check()

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.slice.builders;

import static androidx.annotation.RestrictTo.Scope.LIBRARY;

import android.app.PendingIntent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.collection.ArraySet;
import androidx.core.util.Pair;
import androidx.remotecallback.RemoteCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * Builder to construct a selection which can be added to a {@link ListBuilder}.
 *
 * A selection presents a list of options to the user and allows the user to select exactly one
 * option.
 */
@RequiresApi(19)
public class SelectionBuilder {
    private final List<Pair<String, CharSequence>> mOptions;
    private final Set<String> mOptionKeys;
    private String mSelectedOption;
    private SliceAction mPrimaryAction;
    private PendingIntent mInputAction;

    private CharSequence mTitle;
    private CharSequence mSubtitle;
    private CharSequence mContentDescription;
    private int mLayoutDirection;

    /**
     * Creates a SelectionBuilder with no options.
     */
    public SelectionBuilder() {
        mOptions = new ArrayList<>();
        mOptionKeys = new ArraySet<>();
        mSelectedOption = null;
        mLayoutDirection = -1;
    }

    /**
     * Adds an option to this SelectionBuilder.
     *
     * The new option will be appended to the list of options.
     *
     * @param optionKey the key that will be returned if the user selects this option
     * @param optionText the text that will be displayed to the user for this option
     * @return this SelectionBuilder
     */
    public SelectionBuilder addOption(String optionKey, CharSequence optionText) {
        if (mOptionKeys.contains(optionKey)) {
            throw new IllegalArgumentException("optionKey " + optionKey + " is a duplicate");
        }

        mOptions.add(new Pair<>(optionKey, optionText));
        mOptionKeys.add(optionKey);
        return this;
    }

    /**
     * Sets the primary action for the selection slice.
     *
     * The action specified here will be sent when the whole slice is clicked, or when the app
     * presenting the slice is not capable of rendering a selection interface.
     *
     * @param primaryAction the action to trigger when the user clicks the whole slice
     * @return this SelectionBuilder
     */
    public SelectionBuilder setPrimaryAction(@NonNull SliceAction primaryAction) {
        mPrimaryAction = primaryAction;
        return this;
    }

    /**
     * Sets the {@link PendingIntent} to send when the selection is made or changed.
     *
     * The intent will include an extra with the key {@link androidx.slice.Slice#EXTRA_SELECTION}
     * and a {@link String} value containing the key of the key of the selected option.
     *
     * @param inputAction the intent to send when the user makes or changes the selection
     * @return this SelectionBuilder
     */
    public SelectionBuilder setInputAction(@NonNull PendingIntent inputAction) {
        mInputAction = inputAction;
        return this;
    }

    /**
     * Sets the {@link RemoteCallback} to send when the selection is made or changed.
     *
     * The intent will include an extra with the key {@link androidx.slice.Slice#EXTRA_SELECTION}
     * and a {@link String} value containing the key of the key of the selected option.
     *
     * @param inputAction the intent to send when the user makes or changes the selection
     * @return this SelectionBuilder
     */
    public SelectionBuilder setInputAction(@NonNull RemoteCallback inputAction) {
        mInputAction = inputAction.toPendingIntent();
        return this;
    }

    /**
     * Sets which option is selected by default.
     *
     * @param selectedOption the key of the selected option
     * @return this SelectionBuilder
     */
    public SelectionBuilder setSelectedOption(String selectedOption) {
        mSelectedOption = selectedOption;
        return this;
    }

    /**
     * Sets the title.
     *
     * @param title the title
     * @return this SelectionBuilder
     */
    public SelectionBuilder setTitle(@Nullable CharSequence title) {
        mTitle = title;
        return this;
    }

    /**
     * Sets the subtitle.
     *
     * @param subtitle the subtitle
     * @return this SelectionBuilder
     */
    public SelectionBuilder setSubtitle(@Nullable CharSequence subtitle) {
        mSubtitle = subtitle;
        return this;
    }

    /**
     * Sets the content description.
     *
     * @param contentDescription the content description
     * @return this SelectionBuilder
     */
    public SelectionBuilder setContentDescription(@Nullable CharSequence contentDescription) {
        mContentDescription = contentDescription;
        return this;
    }

    /**
     * Sets the layout direction.
     *
     * @param layoutDirection the layout direction
     * @return this SelectionBuilder
     */
    public SelectionBuilder setLayoutDirection(
            @androidx.slice.builders.ListBuilder.LayoutDirection int layoutDirection) {
        mLayoutDirection = layoutDirection;
        return this;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public List<Pair<String, CharSequence>> getOptions() {
        return mOptions;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public SliceAction getPrimaryAction() {
        return mPrimaryAction;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public PendingIntent getInputAction() {
        return mInputAction;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public String getSelectedOption() {
        return mSelectedOption;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public CharSequence getTitle() {
        return mTitle;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public CharSequence getSubtitle() {
        return mSubtitle;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public CharSequence getContentDescription() {
        return mContentDescription;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public int getLayoutDirection() {
        return mLayoutDirection;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    public void check() {
        if (getPrimaryAction() == null) {
            throw new IllegalArgumentException("primaryAction must be set");
        }
        if (getInputAction() == null) {
            throw new IllegalArgumentException("inputAction must be set");
        }
        if (mSelectedOption != null && !mOptionKeys.contains(mSelectedOption)) {
            throw new IllegalArgumentException("selectedOption must be present in options");
        }
    }
}