Show error message when inputting invalid audio sharing password.

Test: atest
Bug: b/356071394
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: I6e1ff6d32175ad6e4286db922645d608a2708c45
This commit is contained in:
chelseahao
2024-10-28 17:14:19 +08:00
committed by Chelsea Hao
parent 75e2dc4b21
commit ae7acae24c
6 changed files with 82 additions and 4 deletions

View File

@@ -35,6 +35,17 @@
android:layout_marginEnd="20dp" android:layout_marginEnd="20dp"
android:minHeight="48dp" /> android:minHeight="48dp" />
<TextView
android:id="@+id/edit_alert_message"
style="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:visibility="gone"
android:text="@string/audio_streams_main_page_password_dialog_format_alert"
android:textColor="?android:attr/colorError" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@@ -13725,8 +13725,10 @@
<string name="audio_streams_main_page_password_dialog_join_button">Listen to stream</string> <string name="audio_streams_main_page_password_dialog_join_button">Listen to stream</string>
<!-- Le audio streams main page qr code scanner summary [CHAR LIMIT=NONE] --> <!-- Le audio streams main page qr code scanner summary [CHAR LIMIT=NONE] -->
<string name="audio_streams_main_page_qr_code_scanner_summary">Scan an audio stream QR code to listen with <xliff:g example="LE headset" id="device_name">%1$s</xliff:g></string> <string name="audio_streams_main_page_qr_code_scanner_summary">Scan an audio stream QR code to listen with <xliff:g example="LE headset" id="device_name">%1$s</xliff:g></string>
<!-- Le audio streams password dialog [CHAR LIMIT=NONE] --> <!-- Le audio streams password dialog not editable message [CHAR LIMIT=NONE] -->
<string name="audio_streams_main_page_password_dialog_cannot_edit">Can\u0027t edit password while sharing. To change the password, first turn off audio sharing.</string> <string name="audio_streams_main_page_password_dialog_cannot_edit">Can\u0027t edit password while sharing. To change the password, first turn off audio sharing.</string>
<!-- Le audio streams password dialog password format alert [CHAR LIMIT=NONE] -->
<string name="audio_streams_main_page_password_dialog_format_alert">Your password must contain 4-16 characters and use only letters, numbers and symbols</string>
<!-- Text for audio sharing qrcode scanner [CHAR LIMIT=none]--> <!-- Text for audio sharing qrcode scanner [CHAR LIMIT=none]-->
<string name="audio_streams_qr_code_scanner_label">QR code scanner</string> <string name="audio_streams_qr_code_scanner_label">QR code scanner</string>
<!-- Learn more link for audio sharing qrcode [CHAR LIMIT=none]--> <!-- Learn more link for audio sharing qrcode [CHAR LIMIT=none]-->

View File

@@ -41,6 +41,7 @@ public class AudioSharingPasswordPreference extends ValidatedEditTextPreference
@Nullable private EditText mEditText; @Nullable private EditText mEditText;
@Nullable private CheckBox mCheckBox; @Nullable private CheckBox mCheckBox;
@Nullable private View mDialogMessage; @Nullable private View mDialogMessage;
@Nullable private View mEditTextFormatAlert;
private boolean mEditable = true; private boolean mEditable = true;
interface OnDialogEventListener { interface OnDialogEventListener {
@@ -77,6 +78,7 @@ public class AudioSharingPasswordPreference extends ValidatedEditTextPreference
mEditText = view.findViewById(android.R.id.edit); mEditText = view.findViewById(android.R.id.edit);
mCheckBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox); mCheckBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox);
mDialogMessage = view.findViewById(android.R.id.message); mDialogMessage = view.findViewById(android.R.id.message);
mEditTextFormatAlert = view.findViewById(R.id.edit_alert_message);
if (mEditText == null || mCheckBox == null || mDialogMessage == null) { if (mEditText == null || mCheckBox == null || mDialogMessage == null) {
Log.w(TAG, "onBindDialogView() : Invalid layout"); Log.w(TAG, "onBindDialogView() : Invalid layout");
@@ -123,6 +125,14 @@ public class AudioSharingPasswordPreference extends ValidatedEditTextPreference
mDialogMessage.setVisibility(editable ? GONE : VISIBLE); mDialogMessage.setVisibility(editable ? GONE : VISIBLE);
} }
void showEditTextFormatAlert(boolean show) {
if (mEditTextFormatAlert == null) {
Log.w(TAG, "showEditTextFormatAlert() : Invalid layout");
return;
}
mEditTextFormatAlert.setVisibility(show ? VISIBLE : GONE);
}
void setChecked(boolean checked) { void setChecked(boolean checked) {
if (mCheckBox == null) { if (mCheckBox == null) {
Log.w(TAG, "setChecked() : Invalid layout"); Log.w(TAG, "setChecked() : Invalid layout");

View File

@@ -136,7 +136,11 @@ public class AudioSharingPasswordPreferenceController extends BasePreferenceCont
@Override @Override
public boolean isTextValid(String value) { public boolean isTextValid(String value) {
return mAudioSharingPasswordValidator.isTextValid(value); boolean isValid = mAudioSharingPasswordValidator.isTextValid(value);
if (mPreference != null) {
mPreference.showEditTextFormatAlert(!isValid);
}
return isValid;
} }
@Override @Override

View File

@@ -320,16 +320,37 @@ public class AudioSharingPasswordPreferenceControllerTest {
} }
@Test @Test
public void idTextValid_emptyString() { public void isTextValid_emptyString() {
boolean valid = mController.isTextValid(""); boolean valid = mController.isTextValid("");
assertThat(valid).isFalse(); assertThat(valid).isFalse();
} }
@Test @Test
public void idTextValid_validPassword() { public void isTextValid_emptyString_showEditTextFormatAlert() {
mController.displayPreference(mScreen);
ShadowLooper.idleMainLooper();
boolean valid = mController.isTextValid("");
assertThat(valid).isFalse();
verify(mPreference).showEditTextFormatAlert(true);
}
@Test
public void isTextValid_validPassword() {
boolean valid = mController.isTextValid(BROADCAST_PASSWORD); boolean valid = mController.isTextValid(BROADCAST_PASSWORD);
assertThat(valid).isTrue(); assertThat(valid).isTrue();
} }
@Test
public void isTextValid_validPassword_hideEditTextFormatAlert() {
mController.displayPreference(mScreen);
ShadowLooper.idleMainLooper();
boolean valid = mController.isTextValid(BROADCAST_PASSWORD);
assertThat(valid).isTrue();
verify(mPreference).showEditTextFormatAlert(false);
}
} }

View File

@@ -71,10 +71,12 @@ public class AudioSharingPasswordPreferenceTest {
var editText = view.findViewById(android.R.id.edit); var editText = view.findViewById(android.R.id.edit);
var checkBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox); var checkBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox);
var dialogMessage = view.findViewById(android.R.id.message); var dialogMessage = view.findViewById(android.R.id.message);
var editTextAlertMessage = view.findViewById(R.id.edit_alert_message);
assertThat(editText).isNotNull(); assertThat(editText).isNotNull();
assertThat(checkBox).isNotNull(); assertThat(checkBox).isNotNull();
assertThat(dialogMessage).isNotNull(); assertThat(dialogMessage).isNotNull();
assertThat(editTextAlertMessage).isNotNull();
} }
@Test @Test
@@ -147,6 +149,34 @@ public class AudioSharingPasswordPreferenceTest {
assertThat(checkBox.isChecked()).isFalse(); assertThat(checkBox.isChecked()).isFalse();
} }
@Test
public void showEditTextFormatAlert_show() {
View view =
LayoutInflater.from(mContext).inflate(R.layout.audio_sharing_password_dialog, null);
mPreference.onBindDialogView(view);
var editTextAlertMessage = view.findViewById(R.id.edit_alert_message);
mPreference.showEditTextFormatAlert(true);
assertThat(editTextAlertMessage).isNotNull();
assertThat(editTextAlertMessage.getVisibility()).isEqualTo(VISIBLE);
}
@Test
public void showEditTextFormatAlert_hide() {
View view =
LayoutInflater.from(mContext).inflate(R.layout.audio_sharing_password_dialog, null);
mPreference.onBindDialogView(view);
var editTextAlertMessage = view.findViewById(R.id.edit_alert_message);
mPreference.showEditTextFormatAlert(false);
assertThat(editTextAlertMessage).isNotNull();
assertThat(editTextAlertMessage.getVisibility()).isEqualTo(GONE);
}
@Test @Test
public void onDialogEventListener_onClick_positiveButton() { public void onDialogEventListener_onClick_positiveButton() {
AudioSharingPasswordPreference.OnDialogEventListener listener = AudioSharingPasswordPreference.OnDialogEventListener listener =