Fixes Android text field to use hint text for accessibility (flutter/engine#36846)

This commit is contained in:
chunhtai
2022-10-26 11:25:10 -07:00
committed by GitHub
parent 98681213e6
commit 0a923d0c24
2 changed files with 45 additions and 12 deletions

View File

@@ -885,7 +885,8 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
// Scopes routes are not focusable, only need to set the content
// for non-scopes-routes semantics nodes.
if (semanticsNode.hasFlag(Flag.IS_TEXT_FIELD)) {
result.setText(semanticsNode.getValueLabelHint());
result.setText(semanticsNode.getValue());
result.setHintText(semanticsNode.getTextFieldHint());
} else if (!semanticsNode.hasFlag(Flag.SCOPES_ROUTE)) {
CharSequence content = semanticsNode.getValueLabelHint();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
@@ -2773,18 +2774,47 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
private CharSequence getValueLabelHint() {
CharSequence[] array;
private CharSequence getValue() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
array = new CharSequence[] {value, label, hint};
return value;
} else {
array =
new CharSequence[] {
createSpannableString(value, valueAttributes),
createSpannableString(label, labelAttributes),
createSpannableString(hint, hintAttributes),
};
return createSpannableString(value, valueAttributes);
}
}
private CharSequence getLabel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return label;
} else {
return createSpannableString(label, labelAttributes);
}
}
private CharSequence getHint() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return hint;
} else {
return createSpannableString(hint, hintAttributes);
}
}
private CharSequence getValueLabelHint() {
CharSequence[] array = new CharSequence[] {getValue(), getLabel(), getHint()};
CharSequence result = null;
for (CharSequence word : array) {
if (word != null && word.length() > 0) {
if (result == null || result.length() == 0) {
result = word;
} else {
result = TextUtils.concat(result, ", ", word);
}
}
}
return result;
}
private CharSequence getTextFieldHint() {
CharSequence[] array = new CharSequence[] {getLabel(), getHint()};
CharSequence result = null;
for (CharSequence word : array) {
if (word != null && word.length() > 0) {

View File

@@ -77,11 +77,13 @@ public class AccessibilityBridgeTest {
}
@Test
public void itDescribesTextFieldsWithText() {
public void itDescribesTextFieldsWithTextAndHint() {
AccessibilityBridge accessibilityBridge = setUpBridge();
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
testSemanticsNode.label = "Hello, World";
testSemanticsNode.value = "Hello, World";
testSemanticsNode.label = "some label";
testSemanticsNode.hint = "some hint";
testSemanticsNode.addFlag(AccessibilityBridge.Flag.IS_TEXT_FIELD);
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
@@ -89,6 +91,7 @@ public class AccessibilityBridgeTest {
assertEquals(nodeInfo.getContentDescription(), null);
assertEquals(nodeInfo.getText().toString(), "Hello, World");
assertEquals(nodeInfo.getHintText().toString(), "some label, some hint");
}
@Test