A11y cursor movements for text fields on Android (flutter/engine#4419)

* a11y cursor movement by character

* focus fixes

* doc fixes
This commit is contained in:
Michael Goderbauer
2017-12-06 15:46:35 -08:00
committed by GitHub
parent c3a99d4fbb
commit 18e6fd711d
3 changed files with 181 additions and 73 deletions

View File

@@ -18,6 +18,8 @@ class SemanticsAction {
static const int _kIncreaseIndex = 1 << 6;
static const int _kDecreaseIndex = 1 << 7;
static const int _kShowOnScreen = 1 << 8;
static const int _kMoveCursorForwardByCharacter = 1 << 9;
static const int _kMoveCursorBackwardByCharacter = 1 << 10;
/// The numerical value for this action.
///
@@ -76,6 +78,17 @@ class SemanticsAction {
/// is partially off screen to bring it on screen.
static const SemanticsAction showOnScreen = const SemanticsAction._(_kShowOnScreen);
/// Move the cursor forward by one character.
///
/// This is for example used by the cursor control in text fields.
static const SemanticsAction moveCursorForwardByCharacter = const SemanticsAction._(_kMoveCursorForwardByCharacter);
/// Move the cursor backward by one character.
///
/// This is for example used by the cursor control in text fields.
static const SemanticsAction moveCursorBackardByCharacter = const SemanticsAction._(_kMoveCursorBackwardByCharacter);
/// The possible semantics actions.
///
/// The map's key is the [index] of the action and the value is the action
@@ -90,6 +103,8 @@ class SemanticsAction {
_kIncreaseIndex: increase,
_kDecreaseIndex: decrease,
_kShowOnScreen: showOnScreen,
_kMoveCursorForwardByCharacter: moveCursorForwardByCharacter,
_kMoveCursorBackwardByCharacter: moveCursorBackardByCharacter,
};
@override
@@ -113,6 +128,10 @@ class SemanticsAction {
return 'SemanticsAction.decrease';
case _kShowOnScreen:
return 'SemanticsAction.showOnScreen';
case _kMoveCursorForwardByCharacter:
return 'SemanticsAction.moveCursorForwardByCharacter';
case _kMoveCursorBackwardByCharacter:
return 'SemanticsAction.moveCursorBackardByCharacter';
}
return null;
}

View File

@@ -33,32 +33,46 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
private Map<Integer, SemanticsObject> mObjects;
private final FlutterView mOwner;
private boolean mAccessibilityEnabled = false;
private SemanticsObject mFocusedObject;
private SemanticsObject mA11yFocusedObject;
private SemanticsObject mInputFocusedObject;
private SemanticsObject mHoveredObject;
private final BasicMessageChannel<Object> mFlutterAccessibilityChannel;
private static final int SEMANTICS_ACTION_TAP = 1 << 0;
private static final int SEMANTICS_ACTION_LONG_PRESS = 1 << 1;
private static final int SEMANTICS_ACTION_SCROLL_LEFT = 1 << 2;
private static final int SEMANTICS_ACTION_SCROLL_RIGHT = 1 << 3;
private static final int SEMANTICS_ACTION_SCROLL_UP = 1 << 4;
private static final int SEMANTICS_ACTION_SCROLL_DOWN = 1 << 5;
private static final int SEMANTICS_ACTION_INCREASE = 1 << 6;
private static final int SEMANTICS_ACTION_DECREASE = 1 << 7;
private static final int SEMANTICS_ACTION_SHOW_ON_SCREEN = 1 << 8;
enum Action {
TAP(1 << 0),
LONG_PRESS(1 << 1),
SCROLL_LEFT(1 << 2),
SCROLL_RIGHT(1 << 3),
SCROLL_UP(1 << 4),
SCROLL_DOWN(1 << 5),
INCREASE(1 << 6),
DECREASE(1 << 7),
SHOW_ON_SCREEN(1 << 8),
MOVE_CURSOR_FORWARD_BY_CHARACTER(1 << 9),
MOVE_CURSOR_BACKWARD_BY_CHARACTER(1 << 10);
private static final int SEMANTICS_ACTION_SCROLLABLE = SEMANTICS_ACTION_SCROLL_LEFT |
SEMANTICS_ACTION_SCROLL_RIGHT |
SEMANTICS_ACTION_SCROLL_UP |
SEMANTICS_ACTION_SCROLL_DOWN;
Action(int value) {
this.value = value;
}
private static final int SEMANTICS_FLAG_HAS_CHECKED_STATE = 1 << 0;
private static final int SEMANTICS_FLAG_IS_CHECKED = 1 << 1;
private static final int SEMANTICS_FLAG_IS_SELECTED = 1 << 2;
private static final int SEMANTICS_FLAG_IS_BUTTON = 1 << 3;
private static final int SEMANTICS_FLAG_IS_TEXT_FIELD = 1 << 4;
private static final int SEMANTICS_FLAG_IS_FOCUSED = 1 << 5;
final int value;
}
enum Flag {
HAS_CHECKED_STATE(1 << 0),
IS_CHECKED(1 << 1),
IS_SELECTED(1 << 2),
IS_BUTTON(1 << 3),
IS_TEXT_FIELD(1 << 4),
IS_FOCUSED(1 << 5);
Flag(int value) {
this.value = value;
}
final int value;
}
AccessibilityBridge(FlutterView owner) {
assert owner != null;
@@ -97,13 +111,32 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
result.setClassName("Flutter"); // TODO(goderbauer): Set proper class names
result.setSource(mOwner, virtualViewId);
result.setFocusable(object.isFocusable());
result.setFocused((object.flags & SEMANTICS_FLAG_IS_FOCUSED) != 0);
if (mInputFocusedObject != null)
result.setFocused(mInputFocusedObject.id == virtualViewId);
if (mFocusedObject != null)
result.setAccessibilityFocused(mFocusedObject.id == virtualViewId);
if (mA11yFocusedObject != null)
result.setAccessibilityFocused(mA11yFocusedObject.id == virtualViewId);
if ((object.flags & SEMANTICS_FLAG_IS_TEXT_FIELD) != 0)
if (object.hasFlag(Flag.IS_TEXT_FIELD)) {
result.setClassName("android.widget.EditText");
result.setEditable(true);
// Cursor movements
int granularities = 0;
if (object.hasAction(Action.MOVE_CURSOR_FORWARD_BY_CHARACTER)) {
result.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
granularities |= AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER;
}
if (object.hasAction(Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER)) {
result.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
granularities |= AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER;
}
result.setMovementGranularities(granularities);
}
if (object.hasFlag(Flag.IS_BUTTON)) {
result.setClassName("android.widget.Button");
}
if (object.parent != null) {
assert object.id > 0;
@@ -126,15 +159,16 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
result.setVisibleToUser(true);
result.setEnabled(true); // TODO(ianh): Expose disabled subtrees
if ((object.actions & SEMANTICS_ACTION_TAP) != 0) {
if (object.hasAction(Action.TAP)) {
result.addAction(AccessibilityNodeInfo.ACTION_CLICK);
result.setClickable(true);
}
if ((object.actions & SEMANTICS_ACTION_LONG_PRESS) != 0) {
if (object.hasAction(Action.LONG_PRESS)) {
result.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
result.setLongClickable(true);
}
if ((object.actions & SEMANTICS_ACTION_SCROLLABLE) != 0) {
if (object.hasAction(Action.SCROLL_LEFT) || object.hasAction(Action.SCROLL_UP)
|| object.hasAction(Action.SCROLL_RIGHT) || object.hasAction(Action.SCROLL_DOWN)) {
result.setScrollable(true);
// This tells Android's a11y to send scroll events when reaching the end of
// the visible viewport of a scrollable.
@@ -142,37 +176,30 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
// TODO(ianh): Once we're on SDK v23+, call addAction to
// expose AccessibilityAction.ACTION_SCROLL_LEFT, _RIGHT,
// _UP, and _DOWN when appropriate.
if ((object.actions & SEMANTICS_ACTION_SCROLL_LEFT) != 0
|| (object.actions & SEMANTICS_ACTION_SCROLL_UP) != 0) {
if (object.hasAction(Action.SCROLL_LEFT) || object.hasAction(Action.SCROLL_UP)) {
result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
}
if ((object.actions & SEMANTICS_ACTION_SCROLL_RIGHT) != 0
|| (object.actions & SEMANTICS_ACTION_SCROLL_DOWN) != 0) {
if (object.hasAction(Action.SCROLL_RIGHT) || object.hasAction(Action.SCROLL_DOWN)) {
result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
}
}
if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0
|| (object.actions & SEMANTICS_ACTION_DECREASE) != 0 ) {
if (object.hasAction(Action.INCREASE) || object.hasAction(Action.DECREASE)) {
result.setClassName("android.widget.SeekBar");
if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0) {
if (object.hasAction(Action.INCREASE)) {
result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
}
if ((object.actions & SEMANTICS_ACTION_DECREASE) != 0) {
if (object.hasAction(Action.DECREASE)) {
result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
}
}
result.setCheckable((object.flags & SEMANTICS_FLAG_HAS_CHECKED_STATE) != 0);
result.setChecked((object.flags & SEMANTICS_FLAG_IS_CHECKED) != 0);
result.setSelected((object.flags & SEMANTICS_FLAG_IS_SELECTED) != 0);
result.setCheckable(object.hasFlag(Flag.HAS_CHECKED_STATE));
result.setChecked(object.hasFlag(Flag.IS_CHECKED));
result.setSelected(object.hasFlag(Flag.IS_SELECTED));
result.setText(object.getValueLabelHint());
if ((object.flags & SEMANTICS_FLAG_IS_BUTTON) != 0) {
result.setClassName("android.widget.Button");
}
// Accessibility Focus
if (mFocusedObject != null && mFocusedObject.id == virtualViewId) {
if (mA11yFocusedObject != null && mA11yFocusedObject.id == virtualViewId) {
result.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
} else {
result.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
@@ -204,62 +231,68 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
}
switch (action) {
case AccessibilityNodeInfo.ACTION_CLICK: {
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_TAP);
mOwner.dispatchSemanticsAction(virtualViewId, Action.TAP);
return true;
}
case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_LONG_PRESS);
mOwner.dispatchSemanticsAction(virtualViewId, Action.LONG_PRESS);
return true;
}
case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
if ((object.actions & SEMANTICS_ACTION_SCROLL_UP) != 0) {
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_UP);
} else if ((object.actions & SEMANTICS_ACTION_SCROLL_LEFT) != 0) {
if (object.hasAction(Action.SCROLL_UP)) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.SCROLL_UP);
} else if (object.hasAction(Action.SCROLL_LEFT)) {
// TODO(ianh): bidi support using textDirection
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_LEFT);
} else if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.SCROLL_LEFT);
} else if (object.hasAction(Action.INCREASE)) {
object.value = object.increasedValue;
// Event causes Android to read out the updated value.
sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_SELECTED);
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_INCREASE);
mOwner.dispatchSemanticsAction(virtualViewId, Action.INCREASE);
} else {
return false;
}
return true;
}
case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
if ((object.actions & SEMANTICS_ACTION_SCROLL_DOWN) != 0) {
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_DOWN);
} else if ((object.actions & SEMANTICS_ACTION_SCROLL_RIGHT) != 0) {
if (object.hasAction(Action.SCROLL_DOWN)) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.SCROLL_DOWN);
} else if (object.hasAction(Action.SCROLL_RIGHT)) {
// TODO(ianh): bidi support using textDirection
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_RIGHT);
} else if ((object.actions & SEMANTICS_ACTION_DECREASE) != 0) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.SCROLL_RIGHT);
} else if (object.hasAction(Action.DECREASE)) {
object.value = object.decreasedValue;
// Event causes Android to read out the updated value.
sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_SELECTED);
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_DECREASE);
mOwner.dispatchSemanticsAction(virtualViewId, Action.DECREASE);
} else {
return false;
}
return true;
}
case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
return performCursorMoveAction(object, virtualViewId, arguments, false);
}
case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
return performCursorMoveAction(object, virtualViewId, arguments, true);
}
case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mFocusedObject = null;
mA11yFocusedObject = null;
return true;
}
case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
if (mFocusedObject == null) {
if (mA11yFocusedObject == null) {
// When Android focuses a node, it doesn't invalidate the view.
// (It does when it sends ACTION_CLEAR_ACCESSIBILITY_FOCUS, so
// we only have to worry about this when the focused node is null.)
mOwner.invalidate();
}
mFocusedObject = object;
mA11yFocusedObject = object;
if ((object.actions & (SEMANTICS_ACTION_INCREASE | SEMANTICS_ACTION_DECREASE)) != 0) {
if (object.hasAction(Action.INCREASE) || object.hasAction(Action.DECREASE)) {
// SeekBars only announce themselves after this event.
sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_SELECTED);
}
@@ -269,15 +302,52 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
// TODO(goderbauer): Use ACTION_SHOW_ON_SCREEN from Android Support Library after
// https://github.com/flutter/flutter/issues/11099 is resolved.
case 16908342: { // ACTION_SHOW_ON_SCREEN, added in API level 23
mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SHOW_ON_SCREEN);
mOwner.dispatchSemanticsAction(virtualViewId, Action.SHOW_ON_SCREEN);
return true;
}
}
return false;
}
boolean performCursorMoveAction(SemanticsObject object, int virtualViewId, Bundle arguments, boolean forward) {
final int granularity = arguments.getInt(
AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
// TODO(goderbauer): support extending selections.
// final boolean extendSelection = arguments.getBoolean(
// AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
switch (granularity) {
case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
if (forward && object.hasAction(Action.MOVE_CURSOR_FORWARD_BY_CHARACTER)) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_FORWARD_BY_CHARACTER);
return true;
}
if (!forward && object.hasAction(Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER)) {
mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER);
return true;
}
}
// TODO(goderbauer): support other granularities.
}
return false;
}
// TODO(ianh): implement findAccessibilityNodeInfosByText()
// TODO(ianh): implement findFocus()
@Override
public AccessibilityNodeInfo findFocus(int focus) {
switch (focus) {
case AccessibilityNodeInfo.FOCUS_INPUT: {
if (mInputFocusedObject != null)
return createAccessibilityNodeInfo(mInputFocusedObject.id);
}
case AccessibilityNodeInfo.FOCUS_ACCESSIBILITY: {
if (mA11yFocusedObject != null)
return createAccessibilityNodeInfo(mA11yFocusedObject.id);
}
}
return null;
}
private SemanticsObject getRootObject() {
assert mObjects.containsKey(0);
@@ -322,7 +392,11 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
ArrayList<Integer> updated = new ArrayList<Integer>();
while (buffer.hasRemaining()) {
int id = buffer.getInt();
getOrCreateObject(id).updateWith(buffer, strings);
SemanticsObject object = getOrCreateObject(id);
object.updateWith(buffer, strings);
if (object.hasFlag(Flag.IS_FOCUSED)) {
mInputFocusedObject = object;
}
updated.add(id);
}
@@ -414,9 +488,12 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
assert mObjects.containsKey(object.id);
assert mObjects.get(object.id) == object;
object.parent = null;
if (mFocusedObject == object) {
sendAccessibilityEvent(mFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mFocusedObject = null;
if (mA11yFocusedObject == object) {
sendAccessibilityEvent(mA11yFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mA11yFocusedObject = null;
}
if (mInputFocusedObject == object) {
mInputFocusedObject = null;
}
if (mHoveredObject == object) {
mHoveredObject = null;
@@ -425,9 +502,9 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
void reset() {
mObjects.clear();
if (mFocusedObject != null)
sendAccessibilityEvent(mFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mFocusedObject = null;
if (mA11yFocusedObject != null)
sendAccessibilityEvent(mA11yFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mA11yFocusedObject = null;
mHoveredObject = null;
sendAccessibilityEvent(0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
}
@@ -476,6 +553,14 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
private float[] globalTransform;
private Rect globalRect;
boolean hasAction(Action action) {
return (actions & action.value) != 0;
}
boolean hasFlag(Flag flag) {
return (flags & flag.value) != 0;
}
void log(String indent) {
Log.i(TAG, indent + "SemanticsObject id=" + id + " label=" + label + " actions=" + actions + " flags=" + flags + "\n" +
indent + " +-- rect.ltrb=(" + left + ", " + top + ", " + right + ", " + bottom + ")\n" +
@@ -574,8 +659,12 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
return this;
}
// TODO(goderbauer): This should be decided by the framework once we have more information
// about focusability there.
boolean isFocusable() {
return flags != 0 || (label != null && !label.isEmpty()) || (actions & ~SEMANTICS_ACTION_SCROLLABLE) != 0;
int scrollableActions = Action.SCROLL_RIGHT.value | Action.SCROLL_LEFT.value
| Action.SCROLL_UP.value | Action.SCROLL_DOWN.value;
return flags != 0 || (label != null && !label.isEmpty()) || (actions & ~scrollableActions) != 0;
}
void updateRecursively(float[] ancestorTransform, Set<SemanticsObject> visitedObjects, boolean forceUpdate) {

View File

@@ -741,10 +741,10 @@ public class FlutterView extends SurfaceView
private boolean mTouchExplorationEnabled = false;
private TouchExplorationListener mTouchExplorationListener;
protected void dispatchSemanticsAction(int id, int action) {
protected void dispatchSemanticsAction(int id, AccessibilityBridge.Action action) {
if (!isAttached())
return;
nativeDispatchSemanticsAction(mNativeView.get(), id, action);
nativeDispatchSemanticsAction(mNativeView.get(), id, action.value);
}
@Override