Don't disable "Done" button when it cannot be pressed
Show an error on tap instead. According to a11y guidelines this is preferrable.
Fixes: 369942776
Test: atest ZenModeEditDonePreferenceControllerTest
Flag: android.app.modes_ui
Change-Id: Ib5e47a4151c1515e5085a776d538a27c2ef41574
diff --git a/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceController.java b/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceController.java
index de1adc7..cd358bf 100644
--- a/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceController.java
+++ b/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceController.java
@@ -17,7 +17,9 @@
package com.android.settings.notification.modes;
import android.content.Context;
+import android.view.View;
import android.widget.Button;
+import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -32,6 +34,7 @@
private final Runnable mConfirmSave;
@Nullable private Button mButton;
+ private boolean mHasValidName;
ZenModeEditDonePreferenceController(@NonNull Context context, @NonNull String key,
Runnable confirmSave) {
@@ -46,15 +49,22 @@
if (pref != null) {
mButton = pref.findViewById(R.id.done);
if (mButton != null) {
- mButton.setOnClickListener(v -> mConfirmSave.run());
+ mButton.setOnClickListener(this::onButtonClick);
}
}
}
+ private void onButtonClick(View view) {
+ if (mHasValidName) {
+ mConfirmSave.run();
+ } else {
+ Toast.makeText(mContext, R.string.zen_mode_edit_name_empty_error, Toast.LENGTH_SHORT)
+ .show();
+ }
+ }
+
@Override
void updateState(Preference preference, @NonNull ZenMode zenMode) {
- if (mButton != null) {
- mButton.setEnabled(!zenMode.getName().isBlank());
- }
+ mHasValidName = !zenMode.getName().isBlank();
}
}
diff --git a/tests/robotests/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceControllerTest.java
index 74328be..3d076a1 100644
--- a/tests/robotests/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/modes/ZenModeEditDonePreferenceControllerTest.java
@@ -43,6 +43,7 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
+import org.robolectric.shadows.ShadowToast;
@RunWith(RobolectricTestRunner.class)
@EnableFlags(Flags.FLAG_MODES_UI)
@@ -74,29 +75,23 @@
}
@Test
- public void updateState_nameNonEmpty_buttonEnabled() {
+ public void buttonClick_nameNonEmpty_buttonSaves() {
ZenMode mode = new TestModeBuilder().setName("Such a nice name").build();
mController.updateState(mPreference, mode);
-
- assertThat(mButton.isEnabled()).isTrue();
- verifyNoMoreInteractions(mConfirmSave);
- }
-
- @Test
- public void updateState_nameEmpty_buttonDisabled() {
- ZenMode aModeHasNoName = new TestModeBuilder().setName("").build();
-
- mController.updateState(mPreference, aModeHasNoName);
-
- assertThat(mButton.isEnabled()).isFalse();
- verifyNoMoreInteractions(mConfirmSave);
- }
-
- @Test
- public void onButtonClick_callsConfirmSave() {
mButton.performClick();
verify(mConfirmSave).run();
}
+
+ @Test
+ public void buttonClick_nameEmpty_buttonErrors() {
+ ZenMode aModeHasNoName = new TestModeBuilder().setName("").build();
+
+ mController.updateState(mPreference, aModeHasNoName);
+ mButton.performClick();
+
+ verifyNoMoreInteractions(mConfirmSave);
+ assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Mode name cannot be empty");
+ }
}