Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d84f1d4 authored by Hao Dong's avatar Hao Dong
Browse files

Log a warning if content view exceeds char limit

1. Remove the exception for content view char limit and log a warning
   instead
2. Truncate and ellipsize if it exceeds the char limit

Flag: android.hardware.biometrics.custom_biometric_prompt
Test: atest PromptContentViewScreenshotTest
Bug: 359957211
Change-Id: I76921d118f9510457a8cb953a350259bd42eb17c
parent 4c218880
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.DialogInterface;
import android.hardware.biometrics.BiometricPrompt.ButtonInfo;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;

@@ -62,6 +63,7 @@ import java.util.concurrent.Executor;
 */
@FlaggedApi(FLAG_CUSTOM_BIOMETRIC_PROMPT)
public final class PromptContentViewWithMoreOptionsButton implements PromptContentViewParcelable {
    private static final String TAG = "PromptContentViewWithMoreOptionsButton";
    @VisibleForTesting
    static final int MAX_DESCRIPTION_CHARACTER_NUMBER = 225;

@@ -149,13 +151,12 @@ public final class PromptContentViewWithMoreOptionsButton implements PromptConte
         *
         * @param description The description to display.
         * @return This builder.
         * @throws IllegalArgumentException If description exceeds certain character limit.
         */
        @NonNull
        @RequiresPermission(SET_BIOMETRIC_DIALOG_ADVANCED)
        public Builder setDescription(@NonNull String description) {
            if (description.length() > MAX_DESCRIPTION_CHARACTER_NUMBER) {
                throw new IllegalArgumentException("The character number of description exceeds "
                Log.w(TAG, "The character number of description exceeds "
                        + MAX_DESCRIPTION_CHARACTER_NUMBER);
            }
            mDescription = description;
+7 −9
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;

@@ -49,6 +50,7 @@ import java.util.List;
 */
@FlaggedApi(FLAG_CUSTOM_BIOMETRIC_PROMPT)
public final class PromptVerticalListContentView implements PromptContentViewParcelable {
    private static final String TAG = "PromptVerticalListContentView";
    @VisibleForTesting
    static final int MAX_ITEM_NUMBER = 20;
    @VisibleForTesting
@@ -155,12 +157,11 @@ public final class PromptVerticalListContentView implements PromptContentViewPar
         *
         * @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 "
                Log.w(TAG, "The character number of description exceeds "
                        + MAX_DESCRIPTION_CHARACTER_NUMBER);
            }
            mDescription = description;
@@ -172,8 +173,7 @@ public final class PromptVerticalListContentView implements PromptContentViewPar
         *
         * @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.
         * @throws IllegalArgumentException If the number of list items exceeds certain limit.
         */
        @NonNull
        public Builder addListItem(@NonNull PromptContentItem listItem) {
@@ -188,8 +188,7 @@ public final class PromptVerticalListContentView implements PromptContentViewPar
         * @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.
         * @throws IllegalArgumentException If the number of list items exceeds certain limit.
         */
        @NonNull
        public Builder addListItem(@NonNull PromptContentItem listItem, int index) {
@@ -200,8 +199,7 @@ public final class PromptVerticalListContentView implements PromptContentViewPar

        private void checkItemLimits(@NonNull PromptContentItem listItem) {
            if (doesListItemExceedsCharLimit(listItem)) {
                throw new IllegalArgumentException(
                        "The character number of list item exceeds "
                Log.w(TAG, "The character number of list item exceeds "
                        + MAX_EACH_ITEM_CHARACTER_NUMBER);
            }
            if (mContentList.size() > MAX_ITEM_NUMBER) {
+0 −36
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package android.hardware.biometrics;

import static android.hardware.biometrics.PromptContentViewWithMoreOptionsButton.MAX_DESCRIPTION_CHARACTER_NUMBER;
import static android.hardware.biometrics.PromptVerticalListContentView.MAX_EACH_ITEM_CHARACTER_NUMBER;
import static android.hardware.biometrics.PromptVerticalListContentView.MAX_ITEM_NUMBER;

import static com.google.common.truth.Truth.assertThat;
@@ -119,17 +117,6 @@ public class BiometricPromptTest {
        assertThat(e).hasMessageThat().isEqualTo("Logo description passed in can not be null");
    }

    @Test
    public void testMoreOptionsButton_descriptionCharLimit() {
        IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
                () -> new PromptContentViewWithMoreOptionsButton.Builder().setDescription(
                        generateRandomString(MAX_DESCRIPTION_CHARACTER_NUMBER + 1))
        );

        assertThat(e).hasMessageThat().contains(
                "The character number of description exceeds ");
    }

    @Test
    public void testMoreOptionsButton_ExecutorNull() {
        PromptContentViewWithMoreOptionsButton.Builder builder =
@@ -157,29 +144,6 @@ public class BiometricPromptTest {
                "The listener of more options button on prompt content must be set");
    }

    @Test
    public void testVerticalList_descriptionCharLimit() {
        IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
                () -> new PromptVerticalListContentView.Builder().setDescription(
                        generateRandomString(MAX_DESCRIPTION_CHARACTER_NUMBER + 1))
        );

        assertThat(e).hasMessageThat().contains(
                "The character number of description exceeds ");
    }

    @Test
    public void testVerticalList_itemCharLimit() {
        IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
                () -> new PromptVerticalListContentView.Builder().addListItem(
                        new PromptContentItemBulletedText(
                                generateRandomString(MAX_EACH_ITEM_CHARACTER_NUMBER + 1)))
        );

        assertThat(e).hasMessageThat().contains(
                "The character number of list item exceeds ");
    }

    @Test
    public void testVerticalList_itemNumLimit() {
        PromptVerticalListContentView.Builder builder = new PromptVerticalListContentView.Builder();
+4 −0
Original line number Diff line number Diff line
@@ -153,6 +153,10 @@ object Utils {
        return bitmap
    }

    @JvmStatic
    fun String.ellipsize(cutOffLength: Int) =
        if (length <= cutOffLength) this else replaceRange(cutOffLength, length, "...")

    // LINT.IfChange
    @JvmStatic
    /**
+8 −3
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import com.android.settingslib.Utils
import com.android.systemui.biometrics.Utils.ellipsize
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.res.R
import kotlin.math.ceil
@@ -46,6 +47,8 @@ private const val TAG = "BiometricCustomizedViewBinder"

/** Sub-binder for Biometric Prompt Customized View */
object BiometricCustomizedViewBinder {
    const val MAX_DESCRIPTION_CHARACTER_NUMBER = 225

    fun bind(
        customizedViewContainer: LinearLayout,
        contentView: PromptContentView?,
@@ -90,7 +93,8 @@ private fun LayoutInflater.inflateContentView(id: Int, description: String?): Li

    val descriptionView = contentView.requireViewById<TextView>(R.id.customized_view_description)
    if (!description.isNullOrEmpty()) {
        descriptionView.text = description
        descriptionView.text =
            description.ellipsize(BiometricCustomizedViewBinder.MAX_DESCRIPTION_CHARACTER_NUMBER)
    } else {
        descriptionView.visibility = View.GONE
    }
@@ -218,13 +222,14 @@ private fun PromptContentItem.toView(
        inflater.inflate(R.layout.biometric_prompt_content_row_item_text_view, null) as TextView
    val lp = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f)
    textView.layoutParams = lp
    val maxCharNumber = PromptVerticalListContentView.getMaxEachItemCharacterNumber()

    when (this) {
        is PromptContentItemPlainText -> {
            textView.text = text
            textView.text = text.ellipsize(maxCharNumber)
        }
        is PromptContentItemBulletedText -> {
            val bulletedText = SpannableString(text)
            val bulletedText = SpannableString(text.ellipsize(maxCharNumber))
            val span =
                BulletSpan(
                    getListItemBulletGapWidth(resources),
Loading