Commit 72c9e9b1 authored by Theresa's avatar Theresa Committed by Commit Bot

Add content description to password EditText fields

On older versions of Android (M-), the hint text isn't announced for
accessibility. The content description, however, is announced. Behavior
is unchanged on newer versions of Android (N+).

BUG=911762

Change-Id: I8c5a2e90c14d08c925875f4b76af03de9a3f1178
Reviewed-on: https://chromium-review.googlesource.com/c/1392172Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarBecky Zhou <huayinz@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619775}
parent ee5a0f02
...@@ -40,11 +40,14 @@ import android.support.annotation.Nullable; ...@@ -40,11 +40,14 @@ import android.support.annotation.Nullable;
import android.support.v4.widget.ImageViewCompat; import android.support.v4.widget.ImageViewCompat;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import android.view.textclassifier.TextClassifier; import android.view.textclassifier.TextClassifier;
import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.TextView; import android.widget.TextView;
...@@ -768,6 +771,31 @@ public class ApiCompatibilityUtils { ...@@ -768,6 +771,31 @@ public class ApiCompatibilityUtils {
return new TransitionDrawable(layers); return new TransitionDrawable(layers);
} }
/**
* Adds a content description to the provided EditText password field on versions of Android
* where the hint text is not used for accessibility. Does nothing if the EditText field does
* not have a password input type or the hint text is empty. See https://crbug.com/911762.
*
* @param view The EditText password field.
*/
public static void setPasswordEditTextContentDescription(EditText view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) return;
if (isPasswordInputType(view.getInputType()) && !TextUtils.isEmpty(view.getHint())) {
view.setContentDescription(view.getHint());
}
}
private static boolean isPasswordInputType(int inputType) {
final int variation =
inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
return variation == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)
|| variation
== (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD)
|| variation
== (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
}
private static class LayerDrawableCompat extends LayerDrawable { private static class LayerDrawableCompat extends LayerDrawable {
private boolean mMutated; private boolean mMutated;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
android:id="@+id/passphrase" android:id="@+id/passphrase"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/sync_enter_custom_passphrase_hint"
android:inputType="textPassword" android:inputType="textPassword"
android:singleLine="true" android:singleLine="true"
android:imeOptions="actionNext" /> android:imeOptions="actionNext" />
......
...@@ -107,7 +107,6 @@ public class PassphraseDialogFragment extends DialogFragment implements OnClickL ...@@ -107,7 +107,6 @@ public class PassphraseDialogFragment extends DialogFragment implements OnClickL
mVerifyingTextView = (TextView) v.findViewById(R.id.verifying); mVerifyingTextView = (TextView) v.findViewById(R.id.verifying);
mPassphraseEditText = (EditText) v.findViewById(R.id.passphrase); mPassphraseEditText = (EditText) v.findViewById(R.id.passphrase);
mPassphraseEditText.setHint(R.string.sync_enter_custom_passphrase_hint);
mPassphraseEditText.setOnEditorActionListener(new OnEditorActionListener() { mPassphraseEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override @Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
......
...@@ -13,13 +13,19 @@ import android.util.AttributeSet; ...@@ -13,13 +13,19 @@ import android.util.AttributeSet;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.EditText;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R; import org.chromium.chrome.R;
/** /**
* EditText to use in AlertDialog needed due to b/20882793. This class should be removed * EditText to use in AlertDialog needed due to b/20882793 and b/122113958. This class should be
* when we roll to AppCompat with a fix. * removed when we roll to AppCompat with a fix for both issues.
*
* Note that for password fields the hint text is expected to be set in XML so that it is available
* during inflation. If the hint text or content description is changed programatically, consider
* calling {@link ApiCompatibilityUtils#setPasswordEditTextContentDescription(EditText)} after
* the change.
*/ */
public class AlertDialogEditText extends AppCompatEditText { public class AlertDialogEditText extends AppCompatEditText {
...@@ -30,7 +36,11 @@ public class AlertDialogEditText extends AppCompatEditText { ...@@ -30,7 +36,11 @@ public class AlertDialogEditText extends AppCompatEditText {
@Override @Override
protected void onFinishInflate() { protected void onFinishInflate() {
super.onFinishInflate(); super.onFinishInflate();
ApiCompatibilityUtils.setPasswordEditTextContentDescription(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return;
setCustomSelectionActionModeCallback(new ActionMode.Callback() { setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override @Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
...@@ -60,5 +70,4 @@ public class AlertDialogEditText extends AppCompatEditText { ...@@ -60,5 +70,4 @@ public class AlertDialogEditText extends AppCompatEditText {
} }
}); });
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment