Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixes Android text field to use hint text for accessibility
  • Loading branch information
chunhtai committed Oct 26, 2022
commit d601780f335263a913401c7aed8ef6e069a27552
52 changes: 42 additions & 10 deletions shell/platform/android/io/flutter/view/AccessibilityBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ && shouldSetCollectionInfo(semanticsNode)) {
// 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) {
Expand Down Expand Up @@ -2773,18 +2774,49 @@ private float max(float a, float b, float c, float d) {
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) {
return value;
} else {
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) {
array = new CharSequence[] {value, label, hint};
return hint;
} else {
array =
new CharSequence[] {
createSpannableString(value, valueAttributes),
createSpannableString(label, labelAttributes),
createSpannableString(hint, hintAttributes),
};
return createSpannableString(hint, hintAttributes);
}
}

private CharSequence getValueLabelHint() {
CharSequence[] array = new CharSequence[] {getValue(), getLabel(), getHint()};
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, extra semicolon?

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()};
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

CharSequence result = null;
for (CharSequence word : array) {
if (word != null && word.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@ public void itDescribesNonTextFieldsWithAContentDescription() {
}

@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);
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);

assertEquals(nodeInfo.getContentDescription(), null);
assertEquals(nodeInfo.getText().toString(), "Hello, World");
assertEquals(nodeInfo.getHintText().toString(), "some label, some hint");
}

@Test
Expand Down