public final class

PromptVerticalListContentView

extends java.lang.Object

implements PromptContentView

 java.lang.Object

↳androidx.biometric.PromptVerticalListContentView

Gradle dependencies

compile group: 'androidx.biometric', name: 'biometric', version: '1.4.0-alpha02'

  • groupId: androidx.biometric
  • artifactId: biometric
  • version: 1.4.0-alpha02

Artifact androidx.biometric:biometric:1.4.0-alpha02 it located at Google repository (https://maven.google.com/)

Overview

Contains the information of the template of vertical list content view for Biometric Prompt.

Here's how you'd set a PromptVerticalListContentView on a Biometric Prompt:

 BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
     .setTitle(...)
     .setSubTitle(...)
     .setContentView(
         new PromptVerticalListContentView.Builder()
             .setDescription("test description")
             .addListItem(new PromptContentItemPlainText("test item 1"))
             .addListItem(new PromptContentItemPlainText("test item 2"))
             .addListItem(new PromptContentItemBulletedText("test item 3"))
             .build()
      )
     .build();
 

Summary

Methods
public java.lang.StringgetDescription()

Gets the description for the content view, as set by PromptVerticalListContentView.Builder.setDescription(String).

public java.util.List<PromptContentItem>getListItems()

Gets the list of items on the content view, as set by PromptVerticalListContentView.Builder.addListItem(PromptContentItem).

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

Methods

public java.lang.String getDescription()

Gets the description for the content view, as set by PromptVerticalListContentView.Builder.setDescription(String).

Returns:

The description for the content view, or null if the content view has no description.

public java.util.List<PromptContentItem> getListItems()

Gets the list of items on the content view, as set by PromptVerticalListContentView.Builder.addListItem(PromptContentItem).

Returns:

The item list on the content view.

Source

/*
 * Copyright 2024 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.biometric;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

/**
 * Contains the information of the template of vertical list content view for Biometric Prompt.
 * <p>
 * Here's how you'd set a <code>PromptVerticalListContentView</code> on a Biometric Prompt:
 * <pre class="prettyprint">
 * BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
 *     .setTitle(...)
 *     .setSubTitle(...)
 *     .setContentView(
 *         new PromptVerticalListContentView.Builder()
 *             .setDescription("test description")
 *             .addListItem(new PromptContentItemPlainText("test item 1"))
 *             .addListItem(new PromptContentItemPlainText("test item 2"))
 *             .addListItem(new PromptContentItemBulletedText("test item 3"))
 *             .build()
 *      )
 *     .build();
 * </pre>
 */
public final class PromptVerticalListContentView implements PromptContentView {
    static final int MAX_ITEM_NUMBER = 20;
    static final int MAX_EACH_ITEM_CHARACTER_NUMBER = 640;
    static final int MAX_DESCRIPTION_CHARACTER_NUMBER = 225;

    private final List<PromptContentItem> mContentList;
    private final String mDescription;

    private PromptVerticalListContentView(@NonNull List<PromptContentItem> contentList,
            @NonNull String description) {
        mContentList = contentList;
        mDescription = description;
    }

    /**
     * Gets the description for the content view, as set by
     * {@link PromptVerticalListContentView.Builder#setDescription(String)}.
     *
     * @return The description for the content view, or null if the content view has no description.
     */
    @Nullable
    public String getDescription() {
        return mDescription;
    }

    /**
     * Gets the list of items on the content view, as set by
     * {@link PromptVerticalListContentView.Builder#addListItem(PromptContentItem)}.
     *
     * @return The item list on the content view.
     */
    @NonNull
    public List<PromptContentItem> getListItems() {
        return new ArrayList<>(mContentList);
    }

    /**
     * A builder used to set individual options for the {@link PromptVerticalListContentView} class.
     */
    public static final class Builder {
        private final List<PromptContentItem> mContentList = new ArrayList<>();
        private String mDescription;

        /**
         * Optional: Sets a description that will be shown on the content view.
         *
         * @param description The description to display.
         * @return This builder.
         * @throws IllegalArgumentException If description exceeds certain character limit.
         */
        @NonNull
        public Builder setDescription(@NonNull String description) {
            if (description.length() > MAX_DESCRIPTION_CHARACTER_NUMBER) {
                throw new IllegalArgumentException("The character number of description exceeds "
                        + MAX_DESCRIPTION_CHARACTER_NUMBER);
            }
            mDescription = description;
            return this;
        }

        /**
         * Optional: Adds a list item in the current row.
         *
         * @param listItem The list item view to display
         * @return This builder.
         * @throws IllegalArgumentException If this list item exceeds certain character limits or
         *                                  the number of list items exceeds certain limit.
         */
        @NonNull
        public Builder addListItem(@NonNull PromptContentItem listItem) {
            mContentList.add(listItem);
            checkItemLimits(listItem);
            return this;
        }

        /**
         * Optional: Adds a list item in the current row.
         *
         * @param listItem The list item view to display
         * @param index    The position at which to add the item
         * @return This builder.
         * @throws IllegalArgumentException If this list item exceeds certain character limits or
         *                                  the number of list items exceeds certain limit.
         */
        @NonNull
        public Builder addListItem(@NonNull PromptContentItem listItem, int index) {
            mContentList.add(index, listItem);
            checkItemLimits(listItem);
            return this;
        }

        private void checkItemLimits(@NonNull PromptContentItem listItem) {
            if (doesListItemExceedsCharLimit(listItem)) {
                throw new IllegalArgumentException(
                        "The character number of list item exceeds "
                                + MAX_EACH_ITEM_CHARACTER_NUMBER);
            }
            if (mContentList.size() > MAX_ITEM_NUMBER) {
                throw new IllegalArgumentException(
                        "The number of list items exceeds " + MAX_ITEM_NUMBER);
            }
        }

        private boolean doesListItemExceedsCharLimit(PromptContentItem listItem) {
            if (listItem instanceof PromptContentItemPlainText) {
                return ((PromptContentItemPlainText) listItem).getText().length()
                        > MAX_EACH_ITEM_CHARACTER_NUMBER;
            } else if (listItem instanceof PromptContentItemBulletedText) {
                return ((PromptContentItemBulletedText) listItem).getText().length()
                        > MAX_EACH_ITEM_CHARACTER_NUMBER;
            } else {
                return false;
            }
        }


        /**
         * Creates a {@link PromptVerticalListContentView}.
         *
         * @return An instance of {@link PromptVerticalListContentView}.
         */
        @NonNull
        public PromptVerticalListContentView build() {
            return new PromptVerticalListContentView(mContentList, mDescription);
        }
    }
}