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

Commit cd16c280 authored by SongFerng Wang's avatar SongFerng Wang Committed by Android (Google) Code Review
Browse files

Merge "Add the limitaion for broadcast name" into udc-dev

parents 482472ad cd4e8304
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -2724,8 +2724,8 @@
    <string name="media_output_broadcast_last_update_error">Can\u2019t save.</string>
    <!-- The hint message when Broadcast code is less than 4 characters [CHAR LIMIT=60] -->
    <string name="media_output_broadcast_code_hint_no_less_than_min">Use at least 4 characters</string>
    <!-- The hint message when Broadcast code is more than 16 characters [CHAR LIMIT=60] -->
    <string name="media_output_broadcast_code_hint_no_more_than_max">Use fewer than 16 characters</string>
    <!-- The hint message when Broadcast edit is more than 16/254 characters [CHAR LIMIT=60] -->
    <string name="media_output_broadcast_edit_hint_no_more_than_max">Use fewer than <xliff:g id="length" example="16">%1$d</xliff:g> characters</string>

    <!-- Label for clip data when copying the build number off QS [CHAR LIMIT=NONE]-->
    <string name="build_number_clip_data_label">Build number</string>
+55 −15
Original line number Diff line number Diff line
@@ -59,6 +59,17 @@ import com.google.zxing.WriterException;
public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
    private static final String TAG = "MediaOutputBroadcastDialog";

    static final int METADATA_BROADCAST_NAME = 0;
    static final int METADATA_BROADCAST_CODE = 1;

    private static final int MAX_BROADCAST_INFO_UPDATE = 3;
    @VisibleForTesting
    static final int BROADCAST_CODE_MAX_LENGTH = 16;
    @VisibleForTesting
    static final int BROADCAST_CODE_MIN_LENGTH = 4;
    @VisibleForTesting
    static final int BROADCAST_NAME_MAX_LENGTH = 254;

    private ViewStub mBroadcastInfoArea;
    private ImageView mBroadcastQrCodeView;
    private ImageView mBroadcastNotify;
@@ -68,14 +79,16 @@ public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
    private ImageView mBroadcastCodeEye;
    private Boolean mIsPasswordHide = true;
    private ImageView mBroadcastCodeEdit;
    private AlertDialog mAlertDialog;
    @VisibleForTesting
    AlertDialog mAlertDialog;
    private TextView mBroadcastErrorMessage;
    private int mRetryCount = 0;
    private String mCurrentBroadcastName;
    private String mCurrentBroadcastCode;
    private boolean mIsStopbyUpdateBroadcastCode = false;
    private boolean mIsLeBroadcastAssistantCallbackRegistered;

    private TextWatcher mTextWatcher = new TextWatcher() {
    private TextWatcher mBroadcastCodeTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Do nothing
@@ -103,7 +116,9 @@ public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
                        R.string.media_output_broadcast_code_hint_no_less_than_min);
            } else if (breakBroadcastCodeRuleTextLengthMoreThanMax) {
                mBroadcastErrorMessage.setText(
                        R.string.media_output_broadcast_code_hint_no_more_than_max);
                        mContext.getResources().getString(
                                R.string.media_output_broadcast_edit_hint_no_more_than_max,
                                BROADCAST_CODE_MAX_LENGTH));
            }

            mBroadcastErrorMessage.setVisibility(breakRule ? View.VISIBLE : View.INVISIBLE);
@@ -114,7 +129,40 @@ public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
        }
    };

    private boolean mIsLeBroadcastAssistantCallbackRegistered;
    private TextWatcher mBroadcastNameTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Do nothing
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mAlertDialog == null || mBroadcastErrorMessage == null) {
                return;
            }
            boolean breakBroadcastNameRuleTextLengthMoreThanMax =
                    s.length() > BROADCAST_NAME_MAX_LENGTH;
            boolean breakRule = breakBroadcastNameRuleTextLengthMoreThanMax || (s.length() == 0);

            if (breakBroadcastNameRuleTextLengthMoreThanMax) {
                mBroadcastErrorMessage.setText(
                        mContext.getResources().getString(
                                R.string.media_output_broadcast_edit_hint_no_more_than_max,
                                BROADCAST_NAME_MAX_LENGTH));
            }
            mBroadcastErrorMessage.setVisibility(
                    breakBroadcastNameRuleTextLengthMoreThanMax ? View.VISIBLE : View.INVISIBLE);
            Button positiveBtn = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            if (positiveBtn != null) {
                positiveBtn.setEnabled(breakRule ? false : true);
            }
        }
    };

    private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
            new BluetoothLeBroadcastAssistant.Callback() {
@@ -187,13 +235,6 @@ public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
                }
            };

    static final int METADATA_BROADCAST_NAME = 0;
    static final int METADATA_BROADCAST_CODE = 1;

    private static final int MAX_BROADCAST_INFO_UPDATE = 3;
    private static final int BROADCAST_CODE_MAX_LENGTH = 16;
    private static final int BROADCAST_CODE_MIN_LENGTH = 4;

    MediaOutputBroadcastDialog(Context context, boolean aboveStatusbar,
            BroadcastSender broadcastSender, MediaOutputController mediaOutputController) {
        super(context, broadcastSender, mediaOutputController);
@@ -392,9 +433,8 @@ public class MediaOutputBroadcastDialog extends MediaOutputBaseDialog {
                R.layout.media_output_broadcast_update_dialog, null);
        final EditText editText = layout.requireViewById(R.id.broadcast_edit_text);
        editText.setText(editString);
        if (isBroadcastCode) {
            editText.addTextChangedListener(mTextWatcher);
        }
        editText.addTextChangedListener(
                isBroadcastCode ? mBroadcastCodeTextWatcher : mBroadcastNameTextWatcher);
        mBroadcastErrorMessage = layout.requireViewById(R.id.broadcast_error_message);
        mAlertDialog = new Builder(mContext)
                .setTitle(isBroadcastCode ? R.string.media_output_broadcast_code
+106 −0
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ import android.media.session.MediaSessionManager;
import android.os.PowerExemptionManager;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

@@ -58,6 +60,8 @@ import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;

import com.google.common.base.Strings;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -252,4 +256,106 @@ public class MediaOutputBroadcastDialogTest extends SysuiTestCase {
        mMediaOutputBroadcastDialog.updateBroadcastInfo(true, BROADCAST_CODE_UPDATE_TEST);
        assertThat(mMediaOutputBroadcastDialog.getRetryCount()).isEqualTo(0);
    }

    @Test
    public void afterTextChanged_nameLengthMoreThanMax_showErrorMessage() {
        ImageView broadcastNameEdit = mMediaOutputBroadcastDialog.mDialogView
                .requireViewById(R.id.broadcast_name_edit);
        TextView broadcastName = mMediaOutputBroadcastDialog.mDialogView.requireViewById(
                R.id.broadcast_name_summary);
        broadcastName.setText(BROADCAST_NAME_TEST);
        when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
                mLocalBluetoothLeBroadcast);
        broadcastNameEdit.callOnClick();
        EditText editText = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_edit_text);
        TextView broadcastErrorMessage = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_error_message);
        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.INVISIBLE);

        // input the invalid text
        String moreThanMax = Strings.repeat("a",
                MediaOutputBroadcastDialog.BROADCAST_NAME_MAX_LENGTH + 3);
        editText.setText(moreThanMax);

        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.VISIBLE);
    }

    @Test
    public void afterTextChanged_enterValidNameAfterLengthMoreThanMax_noErrorMessage() {
        ImageView broadcastNameEdit = mMediaOutputBroadcastDialog.mDialogView
                .requireViewById(R.id.broadcast_name_edit);
        TextView broadcastName = mMediaOutputBroadcastDialog.mDialogView.requireViewById(
                R.id.broadcast_name_summary);
        broadcastName.setText(BROADCAST_NAME_TEST);
        when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
                mLocalBluetoothLeBroadcast);
        broadcastNameEdit.callOnClick();
        EditText editText = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_edit_text);
        TextView broadcastErrorMessage = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_error_message);
        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.INVISIBLE);

        // input the invalid text
        String testString = Strings.repeat("a",
                MediaOutputBroadcastDialog.BROADCAST_NAME_MAX_LENGTH + 2);
        editText.setText(testString);
        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.VISIBLE);

        // input the valid text
        testString = Strings.repeat("b",
                MediaOutputBroadcastDialog.BROADCAST_NAME_MAX_LENGTH - 100);
        editText.setText(testString);

        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.INVISIBLE);
    }

    @Test
    public void afterTextChanged_codeLengthMoreThanMax_showErrorMessage() {
        ImageView broadcastCodeEdit = mMediaOutputBroadcastDialog.mDialogView
                .requireViewById(R.id.broadcast_code_edit);
        TextView broadcastCode = mMediaOutputBroadcastDialog.mDialogView.requireViewById(
                R.id.broadcast_code_summary);
        broadcastCode.setText(BROADCAST_CODE_UPDATE_TEST);
        when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
                mLocalBluetoothLeBroadcast);
        broadcastCodeEdit.callOnClick();
        EditText editText = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_edit_text);
        TextView broadcastErrorMessage = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_error_message);
        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.INVISIBLE);

        // input the invalid text
        String moreThanMax = Strings.repeat("a",
                MediaOutputBroadcastDialog.BROADCAST_CODE_MAX_LENGTH + 1);
        editText.setText(moreThanMax);

        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.VISIBLE);
    }

    @Test
    public void afterTextChanged_codeLengthLessThanMin_showErrorMessage() {
        ImageView broadcastCodeEdit = mMediaOutputBroadcastDialog.mDialogView
                .requireViewById(R.id.broadcast_code_edit);
        TextView broadcastCode = mMediaOutputBroadcastDialog.mDialogView.requireViewById(
                R.id.broadcast_code_summary);
        broadcastCode.setText(BROADCAST_CODE_UPDATE_TEST);
        when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
                mLocalBluetoothLeBroadcast);
        broadcastCodeEdit.callOnClick();
        EditText editText = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_edit_text);
        TextView broadcastErrorMessage = mMediaOutputBroadcastDialog.mAlertDialog.findViewById(
                R.id.broadcast_error_message);
        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.INVISIBLE);

        // input the invalid text
        String moreThanMax = Strings.repeat("a",
                MediaOutputBroadcastDialog.BROADCAST_CODE_MIN_LENGTH - 1);
        editText.setText(moreThanMax);

        assertThat(broadcastErrorMessage.getVisibility()).isEqualTo(View.VISIBLE);
    }
}