diff options
3 files changed, 163 insertions, 17 deletions
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index b48296fe54be..58be3e9d4e2f 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -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> diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialog.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialog.java index 529b980a6e38..b4578e97eda2 100644 --- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialog.java +++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialog.java @@ -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,13 +433,12 @@ 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 - : R.string.media_output_broadcast_name) + : R.string.media_output_broadcast_name) .setView(layout) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(R.string.media_output_broadcast_dialog_save, diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialogTest.java index 205fa0f11a26..9dba9b5b3c3e 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialogTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBroadcastDialogTest.java @@ -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); + } } |