Use TextInputLayout for the name field in the create/rename mode page

In addition to looking nicer, this fixes two accessibility issues ("no hint when text is entered" and "reason for disabled button is unclear").

This requires a bit of theme merging black magic, because TextInputLayout requires a Theme.AppCompat descendant, which the Settings theme isn't.

Fixes: 356398157
Fixes: 370654542
Fixes: 369942776
Test: atest ZenModeEditNamePreferenceControllerTest
Flag: android.app.modes_ui
Change-Id: I92d8ec044dabc6daed5d755e206120ec7abc143e
This commit is contained in:
Matías Hernández
2024-10-03 13:21:34 +02:00
parent fd84f1203e
commit b1e28233c1
5 changed files with 75 additions and 16 deletions

View File

@@ -17,9 +17,11 @@
package com.android.settings.notification.modes;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
@@ -28,14 +30,18 @@ import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.widget.LayoutPreference;
import com.google.android.material.textfield.TextInputLayout;
import java.util.function.Consumer;
class ZenModeEditNamePreferenceController extends AbstractZenModePreferenceController {
private final Consumer<String> mModeNameSetter;
@Nullable private TextInputLayout mInputLayout;
@Nullable private EditText mEditText;
private boolean mIsSettingText;
@@ -50,7 +56,8 @@ class ZenModeEditNamePreferenceController extends AbstractZenModePreferenceContr
super.displayPreference(screen);
if (mEditText == null) {
LayoutPreference pref = checkNotNull(screen.findPreference(getPreferenceKey()));
mEditText = pref.findViewById(android.R.id.edit);
mInputLayout = checkNotNull(pref.findViewById(R.id.edit_input_layout));
mEditText = checkNotNull(pref.findViewById(android.R.id.edit));
mEditText.addTextChangedListener(new TextWatcher() {
@Override
@@ -61,9 +68,11 @@ class ZenModeEditNamePreferenceController extends AbstractZenModePreferenceContr
@Override
public void afterTextChanged(Editable s) {
if (!mIsSettingText) {
mModeNameSetter.accept(s.toString());
if (mIsSettingText) {
return;
}
mModeNameSetter.accept(s.toString());
updateErrorState(s.toString());
}
});
}
@@ -79,9 +88,20 @@ class ZenModeEditNamePreferenceController extends AbstractZenModePreferenceContr
if (!modeName.equals(currentText)) {
mEditText.setText(modeName);
}
updateErrorState(modeName);
} finally {
mIsSettingText = false;
}
}
}
private void updateErrorState(String currentName) {
checkState(mInputLayout != null);
if (TextUtils.isEmpty(currentName)) {
mInputLayout.setError(
mContext.getString(R.string.zen_mode_edit_name_empty_error));
} else {
mInputLayout.setError(null);
}
}
}