From 6526a250973833d672827bf10390de1018fc4035 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 20 Jul 2018 11:17:59 -0700 Subject: [PATCH] Semantics framework updates (flutter/engine#5601) --- engine/src/flutter/lib/ui/semantics.dart | 83 ++++- .../flutter/lib/ui/semantics/semantics_node.h | 5 + .../io/flutter/view/AccessibilityBridge.java | 291 +++++++++++------- .../framework/Source/accessibility_bridge.mm | 38 ++- 4 files changed, 300 insertions(+), 117 deletions(-) diff --git a/engine/src/flutter/lib/ui/semantics.dart b/engine/src/flutter/lib/ui/semantics.dart index 91766160d0..42494e56ae 100644 --- a/engine/src/flutter/lib/ui/semantics.dart +++ b/engine/src/flutter/lib/ui/semantics.dart @@ -27,6 +27,7 @@ class SemanticsAction { static const int _kDidGainAccessibilityFocusIndex = 1 << 15; static const int _kDidLoseAccessibilityFocusIndex = 1 << 16; static const int _kCustomAction = 1 << 17; + static const int _kDismissIndex = 1 << 18; /// The numerical value for this action. /// @@ -148,11 +149,20 @@ class SemanticsAction { static const SemanticsAction didLoseAccessibilityFocus = const SemanticsAction._(_kDidLoseAccessibilityFocusIndex); /// Indicates that the user has invoked a custom accessibility action. - /// + /// /// This handler is added automatically whenever a custom accessibility /// action is added to a semantics node. static const SemanticsAction customAction = const SemanticsAction._(_kCustomAction); + /// A request that the node should be dismissed. + /// + /// A [Snackbar], for example, may have a dismiss action to indicate to the + /// user that it can be removed after it is no longer relevant. On Android, + /// (with TalkBack) special hint text is spoken when focusing the node and + /// a custom action is availible in the local context menu. On iOS, + /// (with VoiceOver) users can perform a standard gesture to dismiss it. + static const SemanticsAction dismiss = const SemanticsAction._(_kDismissIndex); + /// The possible semantics actions. /// /// The map's key is the [index] of the action and the value is the action @@ -176,6 +186,7 @@ class SemanticsAction { _kDidGainAccessibilityFocusIndex: didGainAccessibilityFocus, _kDidLoseAccessibilityFocusIndex: didLoseAccessibilityFocus, _kCustomAction: customAction, + _kDismissIndex: dismiss, }; @override @@ -217,6 +228,8 @@ class SemanticsAction { return 'SemanticsAction.didLoseAccessibilityFocus'; case _kCustomAction: return 'SemanticsAction.customAction'; + case _kDismissIndex: + return 'SemanticsAction.dismiss'; } return null; } @@ -238,6 +251,10 @@ class SemanticsFlag { static const int _kScopesRouteIndex= 1 << 11; static const int _kNamesRouteIndex = 1 << 12; static const int _kIsHiddenIndex = 1 << 13; + static const int _kIsImageIndex = 1 << 14; + static const int _kIsLiveRegionIndex = 1 << 15; + static const int _kHasToggledStateIndex = 1 << 16; + static const int _kIsToggledIndex = 1 << 17; const SemanticsFlag._(this.index); @@ -248,7 +265,13 @@ class SemanticsFlag { /// The semantics node has the quality of either being "checked" or "unchecked". /// + /// This flag is mutually exclusive with [hasToggledState]. + /// /// For example, a checkbox or a radio button widget has checked state. + /// + /// See also: + /// + /// * [SemanticsFlag.isChecked], which controls whether the node is "checked" or "unchecked". static const SemanticsFlag hasCheckedState = const SemanticsFlag._(_kHasCheckedStateIndex); /// Whether a semantics node that [hasCheckedState] is checked. @@ -257,6 +280,10 @@ class SemanticsFlag { /// "unchecked". /// /// For example, if a checkbox has a visible checkmark, [isChecked] is true. + /// + /// See also: + /// + /// * [SemanticsFlag.hasCheckedState], which enables a checked state. static const SemanticsFlag isChecked = const SemanticsFlag._(_kIsCheckedIndex); @@ -376,6 +403,44 @@ class SemanticsFlag { /// used to implement accessibility scrolling on iOS. static const SemanticsFlag isHidden = const SemanticsFlag._(_kIsHiddenIndex); + /// Whether the semantics node represents an image. + /// + /// Both TalkBack and VoiceOver will inform the user the the semantics node + /// represents an image. + static const SemanticsFlag isImage = const SemanticsFlag._(_kIsImageIndex); + + /// Whether the semantics node is a live region. + /// + /// A live region indicates that updates to semantics node are important. + /// Platforms may use this information to make polite announcements to the + /// user to inform them of updates to this node. + /// + /// An example of a live region is a [SnackBar] widget. On Android, A live + /// region causes a polite announcement to be generated automatically, even + /// if the user does not have focus of the widget. + static const SemanticsFlag isLiveRegion = const SemanticsFlag._(_kIsLiveRegionIndex); + + /// The semantics node has the quality of either being "on" or "off". + /// + /// This flag is mutually exclusive with [hasCheckedState]. + /// + /// For example, a switch has toggled state. + /// + /// See also: + /// + /// * [SemanticsFlag.isToggled], which controls whether the node is "on" or "off". + static const SemanticsFlag hasToggledState = const SemanticsFlag._(_kHasToggledStateIndex); + + /// If true, the semantics node is "on". If false, the semantics node is + /// "off". + /// + /// For example, if a switch is in the on position, [isToggled] is true. + /// + /// See also: + /// + /// * [SemanticsFlag.hasToggledState], which enables a toggled state. + static const SemanticsFlag isToggled = const SemanticsFlag._(_kIsToggledIndex); + /// The possible semantics flags. /// /// The map's key is the [index] of the flag and the value is the flag itself. @@ -394,6 +459,10 @@ class SemanticsFlag { _kScopesRouteIndex: scopesRoute, _kNamesRouteIndex: namesRoute, _kIsHiddenIndex: isHidden, + _kIsImageIndex: isImage, + _kIsLiveRegionIndex: isLiveRegion, + _kHasToggledStateIndex: hasToggledState, + _kIsToggledIndex: isToggled, }; @override @@ -427,6 +496,14 @@ class SemanticsFlag { return 'SemanticsFlag.namesRoute'; case _kIsHiddenIndex: return 'SemanticsFlag.isHidden'; + case _kIsImageIndex: + return 'SemanticsFlag.isImage'; + case _kIsLiveRegionIndex: + return 'SemanticsFlag.isLiveRegion'; + case _kHasToggledStateIndex: + return 'SemanticsFlag.hasToggledState'; + case _kIsToggledIndex: + return 'SemanticsFlag.isToggled'; } return null; } @@ -563,8 +640,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 { ) native 'SemanticsUpdateBuilder_updateNode'; /// Update the custom accessibility action associated with the given `id`. - /// - /// The name of the action exposed to the user is the `label`. The text + /// + /// The name of the action exposed to the user is the `label`. The text /// direction of this label is the same as the global window. void updateCustomAction({int id, String label}) { assert(id != null); diff --git a/engine/src/flutter/lib/ui/semantics/semantics_node.h b/engine/src/flutter/lib/ui/semantics/semantics_node.h index e0ae2ba296..2ade1f49a9 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_node.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_node.h @@ -36,6 +36,7 @@ enum class SemanticsAction : int32_t { kDidGainAccessibilityFocus = 1 << 15, kDidLoseAccessibilityFocus = 1 << 16, kCustomAction = 1 << 17, + kDismiss = 1 << 18, }; const int kScrollableSemanticsActions = @@ -60,6 +61,10 @@ enum class SemanticsFlags : int32_t { kScopesRoute = 1 << 11, kNamesRoute = 1 << 12, kIsHidden = 1 << 13, + kIsImage = 1 << 14, + kIsLiveRegion = 1 << 15, + kHasToggledState = 1 << 16, + kIsToggled = 1 << 17, }; struct SemanticsNode { diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java b/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java index b0dddf8598..ecd78af4bb 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java @@ -19,7 +19,8 @@ import io.flutter.plugin.common.StandardMessageCodec; import java.nio.ByteBuffer; import java.util.*; -class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMessageChannel.MessageHandler { +class AccessibilityBridge + extends AccessibilityNodeProvider implements BasicMessageChannel.MessageHandler { private static final String TAG = "FlutterView"; // Constants from higher API levels. @@ -61,7 +62,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess PASTE(1 << 14), DID_GAIN_ACCESSIBILITY_FOCUS(1 << 15), DID_LOSE_ACCESSIBILITY_FOCUS(1 << 16), - CUSTOM_ACTION(1 << 17); + CUSTOM_ACTION(1 << 17), + DISMISS(1 << 18); Action(int value) { this.value = value; @@ -84,7 +86,11 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess IS_OBSCURED(1 << 10), SCOPES_ROUTE(1 << 11), NAMES_ROUTE(1 << 12), - IS_HIDDEN(1 << 13); + IS_HIDDEN(1 << 13), + IS_IMAGE(1 << 14), + IS_LIVE_REGION(1 << 15), + HAS_TOGGLED_STATE(1 << 16), + IS_TOGGLED(1 << 17); Flag(int value) { this.value = value; @@ -99,8 +105,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess mObjects = new HashMap(); mCustomAccessibilityActions = new HashMap(); previousRoutes = new ArrayList<>(); - mFlutterAccessibilityChannel = new BasicMessageChannel<>(owner, "flutter/accessibility", - StandardMessageCodec.INSTANCE); + mFlutterAccessibilityChannel = new BasicMessageChannel<>( + owner, "flutter/accessibility", StandardMessageCodec.INSTANCE); } void setAccessibilityEnabled(boolean accessibilityEnabled) { @@ -118,25 +124,29 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (virtualViewId == View.NO_ID) { AccessibilityNodeInfo result = AccessibilityNodeInfo.obtain(mOwner); mOwner.onInitializeAccessibilityNodeInfo(result); - if (mObjects.containsKey(ROOT_NODE_ID)) + if (mObjects.containsKey(ROOT_NODE_ID)) { result.addChild(mOwner, ROOT_NODE_ID); + } return result; } SemanticsObject object = mObjects.get(virtualViewId); - if (object == null) + if (object == null) { return null; + } AccessibilityNodeInfo result = AccessibilityNodeInfo.obtain(mOwner, virtualViewId); result.setPackageName(mOwner.getContext().getPackageName()); result.setClassName("android.view.View"); result.setSource(mOwner, virtualViewId); result.setFocusable(object.isFocusable()); - if (mInputFocusedObject != null) + if (mInputFocusedObject != null) { result.setFocused(mInputFocusedObject.id == virtualViewId); + } - if (mA11yFocusedObject != null) + if (mA11yFocusedObject != null) { result.setAccessibilityFocused(mA11yFocusedObject.id == virtualViewId); + } if (object.hasFlag(Flag.IS_TEXT_FIELD)) { result.setPassword(object.hasFlag(Flag.IS_OBSCURED)); @@ -146,6 +156,10 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (object.textSelectionBase != -1 && object.textSelectionExtent != -1) { result.setTextSelection(object.textSelectionBase, object.textSelectionExtent); } + // Text fields will always be created as a live region, so that updates to + // the label trigger polite announcements. This makes it easy to follow a11y + // guidelines for text fields on Android. + result.setLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); } // Cursor movements @@ -174,7 +188,16 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } if (object.hasFlag(Flag.IS_BUTTON)) { - result.setClassName("android.widget.Button"); + result.setClassName("android.widget.Button"); + } + if (object.hasFlag(Flag.IS_IMAGE)) { + result.setClassName("android.widget.ImageView"); + // TODO(jonahwilliams): Figure out a way conform to the expected id from TalkBack's + // CustomLabelManager. talkback/src/main/java/labeling/CustomLabelManager.java#L525 + } + if (object.hasAction(Action.DISMISS)) { + result.setDismissable(true); + result.addAction(AccessibilityNodeInfo.ACTION_DISMISS); } if (object.parent != null) { @@ -196,8 +219,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } result.setBoundsInScreen(bounds); result.setVisibleToUser(true); - result.setEnabled(!object.hasFlag(Flag.HAS_ENABLED_STATE) || - object.hasFlag(Flag.IS_ENABLED)); + result.setEnabled( + !object.hasFlag(Flag.HAS_ENABLED_STATE) || object.hasFlag(Flag.IS_ENABLED)); if (object.hasAction(Action.TAP)) { result.addAction(AccessibilityNodeInfo.ACTION_CLICK); @@ -224,6 +247,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } } if (object.hasAction(Action.INCREASE) || object.hasAction(Action.DECREASE)) { + // TODO(jonahwilliams): support AccessibilityAction.ACTION_SET_PROGRESS once SDK is + // updated. result.setClassName("android.widget.SeekBar"); if (object.hasAction(Action.INCREASE)) { result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); @@ -232,15 +257,23 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); } } + if (object.hasFlag(Flag.IS_LIVE_REGION)) { + result.setLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); + } boolean hasCheckedState = object.hasFlag(Flag.HAS_CHECKED_STATE); - result.setCheckable(hasCheckedState); + boolean hasToggledState = object.hasFlag(Flag.HAS_TOGGLED_STATE); + assert !(hasCheckedState && hasToggledState); + result.setCheckable(hasCheckedState || hasToggledState); if (hasCheckedState) { result.setChecked(object.hasFlag(Flag.IS_CHECKED)); if (object.hasFlag(Flag.IS_IN_MUTUALLY_EXCLUSIVE_GROUP)) result.setClassName("android.widget.RadioButton"); else result.setClassName("android.widget.CheckBox"); + } else if (hasToggledState) { + result.setChecked(object.hasFlag(Flag.IS_TOGGLED)); + result.setClassName("android.widget.Switch"); } result.setSelected(object.hasFlag(Flag.IS_SELECTED)); @@ -257,7 +290,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (Build.VERSION.SDK_INT >= 21) { if (object.customAccessibilityAction != null) { for (CustomAccessibilityAction action : object.customAccessibilityAction) { - result.addAction(new AccessibilityNodeInfo.AccessibilityAction(action.resourceId, action.label)); + result.addAction(new AccessibilityNodeInfo.AccessibilityAction( + action.resourceId, action.label)); } } } @@ -334,13 +368,15 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: { mOwner.dispatchSemanticsAction(virtualViewId, Action.DID_LOSE_ACCESSIBILITY_FOCUS); - sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); + sendAccessibilityEvent( + virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); mA11yFocusedObject = null; return true; } case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: { mOwner.dispatchSemanticsAction(virtualViewId, Action.DID_GAIN_ACCESSIBILITY_FOCUS); - sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED); + sendAccessibilityEvent( + virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED); if (mA11yFocusedObject == null) { // When Android focuses a node, it doesn't invalidate the view. @@ -364,15 +400,17 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess case AccessibilityNodeInfo.ACTION_SET_SELECTION: { final Map selection = new HashMap(); final boolean hasSelection = arguments != null - && arguments.containsKey( - AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT) - && arguments.containsKey( - AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT); + && arguments.containsKey( + AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT) + && arguments.containsKey( + AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT); if (hasSelection) { - selection.put("base", arguments.getInt( - AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT)); - selection.put("extent", arguments.getInt( - AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT)); + selection.put("base", + arguments.getInt( + AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT)); + selection.put("extent", + arguments.getInt( + AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT)); } else { // Clear the selection selection.put("base", object.textSelectionExtent); @@ -393,35 +431,44 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess mOwner.dispatchSemanticsAction(virtualViewId, Action.PASTE); return true; } + case AccessibilityNodeInfo.ACTION_DISMISS: { + mOwner.dispatchSemanticsAction(virtualViewId, Action.DISMISS); + return true; + } default: // might be a custom accessibility action. final int flutterId = action - firstResourceId; - CustomAccessibilityAction contextAction = mCustomAccessibilityActions.get(flutterId); + CustomAccessibilityAction contextAction = + mCustomAccessibilityActions.get(flutterId); if (contextAction != null) { - mOwner.dispatchSemanticsAction(virtualViewId, Action.CUSTOM_ACTION, contextAction.id); + mOwner.dispatchSemanticsAction( + virtualViewId, Action.CUSTOM_ACTION, contextAction.id); 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); + boolean performCursorMoveAction( + SemanticsObject object, int virtualViewId, Bundle arguments, boolean forward) { + final int granularity = + arguments.getInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT); final boolean extendSelection = arguments.getBoolean( - AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN); + 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, extendSelection); + mOwner.dispatchSemanticsAction(virtualViewId, + Action.MOVE_CURSOR_FORWARD_BY_CHARACTER, extendSelection); return true; } if (!forward && object.hasAction(Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER)) { - mOwner.dispatchSemanticsAction(virtualViewId, Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER, extendSelection); + mOwner.dispatchSemanticsAction(virtualViewId, + Action.MOVE_CURSOR_BACKWARD_BY_CHARACTER, extendSelection); return true; } } - // TODO(goderbauer): support other granularities. + // TODO(goderbauer): support other granularities. } return false; } @@ -444,20 +491,19 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess return null; } - private SemanticsObject getRootObject() { - assert mObjects.containsKey(0); - return mObjects.get(0); + assert mObjects.containsKey(0); + return mObjects.get(0); } private SemanticsObject getOrCreateObject(int id) { - SemanticsObject object = mObjects.get(id); - if (object == null) { - object = new SemanticsObject(); - object.id = id; - mObjects.put(id, object); - } - return object; + SemanticsObject object = mObjects.get(id); + if (object == null) { + object = new SemanticsObject(); + object.id = id; + mObjects.put(id, object); + } + return object; } private CustomAccessibilityAction getOrCreateAction(int id) { @@ -482,7 +528,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (mObjects.isEmpty()) { return; } - SemanticsObject newObject = getRootObject().hitTest(new float[]{ x, y, 0, 1 }); + SemanticsObject newObject = getRootObject().hitTest(new float[] {x, y, 0, 1}); if (newObject != mHoveredObject) { // sending ENTER before EXIT is how Android wants it if (newObject != null) { @@ -496,7 +542,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } void updateCustomAccessibilityActions(ByteBuffer buffer, String[] strings) { - ArrayList updatedActions = new ArrayList(); + ArrayList updatedActions = + new ArrayList(); while (buffer.hasRemaining()) { int id = buffer.getInt(); CustomAccessibilityAction action = getOrCreateAction(id); @@ -526,10 +573,10 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess SemanticsObject rootObject = getRootObject(); List newRoutes = new ArrayList<>(); if (rootObject != null) { - final float[] identity = new float[16]; - Matrix.setIdentityM(identity, 0); - rootObject.updateRecursively(identity, visitedObjects, false); - rootObject.collectRoutes(newRoutes); + final float[] identity = new float[16]; + Matrix.setIdentityM(identity, 0); + rootObject.updateRecursively(identity, visitedObjects, false); + rootObject.collectRoutes(newRoutes); } // Dispatch a TYPE_WINDOW_STATE_CHANGED event if the most recent route id changed from the @@ -602,6 +649,14 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } sendAccessibilityEvent(event); } + if (object.hasFlag(Flag.IS_LIVE_REGION) && !object.hadFlag(Flag.IS_LIVE_REGION)) { + sendAccessibilityEvent(object.id, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); + } else if (object.hasFlag(Flag.IS_TEXT_FIELD) && object.didChangeLabel() + && mInputFocusedObject != null && mInputFocusedObject.id == object.id) { + // Text fields should announce when their label changes while focused. We use a live + // region tag to do so, and this event triggers that update. + sendAccessibilityEvent(object.id, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); + } if (mA11yFocusedObject != null && mA11yFocusedObject.id == object.id && !object.hadFlag(Flag.IS_SELECTED) && object.hasFlag(Flag.IS_SELECTED)) { AccessibilityEvent event = @@ -610,8 +665,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess sendAccessibilityEvent(event); } if (mInputFocusedObject != null && mInputFocusedObject.id == object.id - && object.hadFlag(Flag.IS_TEXT_FIELD) - && object.hasFlag(Flag.IS_TEXT_FIELD)) { + && object.hadFlag(Flag.IS_TEXT_FIELD) && object.hasFlag(Flag.IS_TEXT_FIELD)) { String oldValue = object.previousValue != null ? object.previousValue : ""; String newValue = object.value != null ? object.value : ""; AccessibilityEvent event = createTextChangedEvent(object.id, oldValue, newValue); @@ -622,7 +676,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (object.previousTextSelectionBase != object.textSelectionBase || object.previousTextSelectionExtent != object.textSelectionExtent) { AccessibilityEvent selectionEvent = obtainAccessibilityEvent( - object.id, AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); + object.id, AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED); selectionEvent.getText().add(newValue); selectionEvent.setFromIndex(object.textSelectionBase); selectionEvent.setToIndex(object.textSelectionExtent); @@ -634,7 +688,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } private AccessibilityEvent createTextChangedEvent(int id, String oldValue, String newValue) { - AccessibilityEvent e = obtainAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); + AccessibilityEvent e = + obtainAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); e.setBeforeText(oldValue); e.getText().add(newValue); @@ -645,7 +700,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } } if (i >= oldValue.length() && i >= newValue.length()) { - return null; // Text did not change + return null; // Text did not change } int firstDifference = i; e.setFromIndex(firstDifference); @@ -694,10 +749,10 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess // Message Handler for [mFlutterAccessibilityChannel]. public void onMessage(Object message, BasicMessageChannel.Reply reply) { @SuppressWarnings("unchecked") - final HashMap annotatedEvent = (HashMap)message; - final String type = (String)annotatedEvent.get("type"); + final HashMap annotatedEvent = (HashMap) message; + final String type = (String) annotatedEvent.get("type"); @SuppressWarnings("unchecked") - final HashMap data = (HashMap)annotatedEvent.get("data"); + final HashMap data = (HashMap) annotatedEvent.get("data"); switch (type) { case "announce": @@ -720,15 +775,27 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess break; } case "tooltip": { - AccessibilityEvent e = obtainAccessibilityEvent(ROOT_NODE_ID, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); + AccessibilityEvent e = obtainAccessibilityEvent( + ROOT_NODE_ID, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); e.getText().add((String) data.get("message")); sendAccessibilityEvent(e); } + // Requires that the node id provided corresponds to a live region, or TalkBack will + // ignore the event. The event will cause talkback to read out the new label even + // if node is not focused. + case "updateLiveRegion": { + Integer nodeId = (Integer) annotatedEvent.get("nodeId"); + if (nodeId == null) { + return; + } + sendAccessibilityEvent(nodeId, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); + } } } private void createWindowChangeEvent(SemanticsObject route) { - AccessibilityEvent e = obtainAccessibilityEvent(route.id, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); + AccessibilityEvent e = + obtainAccessibilityEvent(route.id, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); String routeName = route.getRouteName(); e.getText().add(routeName); sendAccessibilityEvent(e); @@ -739,7 +806,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess assert mObjects.get(object.id) == object; object.parent = null; if (mA11yFocusedObject == object) { - sendAccessibilityEvent(mA11yFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); + sendAccessibilityEvent(mA11yFocusedObject.id, + AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); mA11yFocusedObject = null; } if (mInputFocusedObject == object) { @@ -753,14 +821,17 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess void reset() { mObjects.clear(); if (mA11yFocusedObject != null) - sendAccessibilityEvent(mA11yFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); + sendAccessibilityEvent(mA11yFocusedObject.id, + AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); mA11yFocusedObject = null; mHoveredObject = null; sendAccessibilityEvent(0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); } private enum TextDirection { - UNKNOWN, LTR, RTL; + UNKNOWN, + LTR, + RTL; public static TextDirection fromInt(int value) { switch (value) { @@ -775,7 +846,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess private class CustomAccessibilityAction { CustomAccessibilityAction() {} - + /// Resource id is the id of the custom action plus a minimum value so that the identifier /// does not collide with existing Android accessibility actions. int resourceId = -1; @@ -788,7 +859,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess static int firstResourceId = 267386881; private class SemanticsObject { - SemanticsObject() { } + SemanticsObject() {} int id = -1; @@ -815,6 +886,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess float previousScrollExtentMax; float previousScrollExtentMin; String previousValue; + String previousLabel; private float left; private float top; @@ -856,22 +928,32 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess && previousScrollPosition != scrollPosition; } + boolean didChangeLabel() { + if (label == null && previousLabel == null) { + return false; + } + return label == null || previousLabel == null || !label.equals(previousLabel); + } + void log(String indent, boolean recursive) { - Log.i(TAG, indent + "SemanticsObject id=" + id + " label=" + label + " actions=" + actions + " flags=" + flags + "\n" + - indent + " +-- textDirection=" + textDirection + "\n"+ - indent + " +-- rect.ltrb=(" + left + ", " + top + ", " + right + ", " + bottom + ")\n" + - indent + " +-- transform=" + Arrays.toString(transform) + "\n"); - if (childrenInTraversalOrder != null && recursive) { - String childIndent = indent + " "; - for (SemanticsObject child : childrenInTraversalOrder) { - child.log(childIndent, recursive); - } - } + Log.i(TAG, + indent + "SemanticsObject id=" + id + " label=" + label + " actions=" + actions + + " flags=" + flags + "\n" + indent + " +-- textDirection=" + + textDirection + "\n" + indent + " +-- rect.ltrb=(" + left + ", " + + top + ", " + right + ", " + bottom + ")\n" + indent + + " +-- transform=" + Arrays.toString(transform) + "\n"); + if (childrenInTraversalOrder != null && recursive) { + String childIndent = indent + " "; + for (SemanticsObject child : childrenInTraversalOrder) { + child.log(childIndent, recursive); + } + } } void updateWith(ByteBuffer buffer, String[] strings) { hadPreviousConfig = true; previousValue = value; + previousLabel = label; previousFlags = flags; previousActions = actions; previousTextSelectionBase = textSelectionBase; @@ -910,10 +992,12 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess right = buffer.getFloat(); bottom = buffer.getFloat(); - if (transform == null) + if (transform == null) { transform = new float[16]; - for (int i = 0; i < 16; ++i) + } + for (int i = 0; i < 16; ++i) { transform[i] = buffer.getFloat(); + } inverseTransformDirty = true; globalGeometryDirty = true; @@ -949,7 +1033,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess customAccessibilityAction = null; } else { if (customAccessibilityAction == null) - customAccessibilityAction = new ArrayList(actionCount); + customAccessibilityAction = + new ArrayList(actionCount); else customAccessibilityAction.clear(); @@ -961,13 +1046,16 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } private void ensureInverseTransform() { - if (!inverseTransformDirty) + if (!inverseTransformDirty) { return; + } inverseTransformDirty = false; - if (inverseTransform == null) + if (inverseTransform == null) { inverseTransform = new float[16]; - if (!Matrix.invertM(inverseTransform, 0, transform, 0)) + } + if (!Matrix.invertM(inverseTransform, 0, transform, 0)) { Arrays.fill(inverseTransform, 0); + } } Rect getGlobalRect() { @@ -979,8 +1067,7 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess final float w = point[3]; final float x = point[0] / w; final float y = point[1] / w; - if (x < left || x >= right || y < top || y >= bottom) - return null; + if (x < left || x >= right || y < top || y >= bottom) return null; if (childrenInHitTestOrder != null) { final float[] transformedPoint = new float[4]; for (int i = 0; i < childrenInHitTestOrder.size(); i += 1) { @@ -1009,11 +1096,9 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess } int scrollableActions = Action.SCROLL_RIGHT.value | Action.SCROLL_LEFT.value | Action.SCROLL_UP.value | Action.SCROLL_DOWN.value; - return (actions & ~scrollableActions) != 0 - || flags != 0 - || (label != null && !label.isEmpty()) - || (value != null && !value.isEmpty()) - || (hint != null && !hint.isEmpty()); + return (actions & ~scrollableActions) != 0 || flags != 0 + || (label != null && !label.isEmpty()) || (value != null && !value.isEmpty()) + || (hint != null && !hint.isEmpty()); } void collectRoutes(List edges) { @@ -1046,15 +1131,18 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess return null; } - void updateRecursively(float[] ancestorTransform, Set visitedObjects, boolean forceUpdate) { + void updateRecursively(float[] ancestorTransform, Set visitedObjects, + boolean forceUpdate) { visitedObjects.add(this); - if (globalGeometryDirty) + if (globalGeometryDirty) { forceUpdate = true; + } if (forceUpdate) { - if (globalTransform == null) + if (globalTransform == null) { globalTransform = new float[16]; + } Matrix.multiplyMM(globalTransform, 0, ancestorTransform, 0, transform, 0); final float[] sample = new float[4]; @@ -1082,15 +1170,12 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess sample[1] = bottom; transformPoint(point4, globalTransform, sample); - if (globalRect == null) - globalRect = new Rect(); + if (globalRect == null) globalRect = new Rect(); - globalRect.set( - Math.round(min(point1[0], point2[0], point3[0], point4[0])), - Math.round(min(point1[1], point2[1], point3[1], point4[1])), - Math.round(max(point1[0], point2[0], point3[0], point4[0])), - Math.round(max(point1[1], point2[1], point3[1], point4[1])) - ); + globalRect.set(Math.round(min(point1[0], point2[0], point3[0], point4[0])), + Math.round(min(point1[1], point2[1], point3[1], point4[1])), + Math.round(max(point1[0], point2[0], point3[0], point4[0])), + Math.round(max(point1[1], point2[1], point3[1], point4[1]))); globalGeometryDirty = false; } @@ -1100,7 +1185,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess if (childrenInTraversalOrder != null) { for (int i = 0; i < childrenInTraversalOrder.size(); ++i) { - childrenInTraversalOrder.get(i).updateRecursively(globalTransform, visitedObjects, forceUpdate); + childrenInTraversalOrder.get(i).updateRecursively( + globalTransform, visitedObjects, forceUpdate); } } } @@ -1124,11 +1210,10 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess private String getValueLabelHint() { StringBuilder sb = new StringBuilder(); - String[] array = { value, label, hint }; - for (String word: array) { + String[] array = {value, label, hint}; + for (String word : array) { if (word != null && word.length() > 0) { - if (sb.length() > 0) - sb.append(", "); + if (sb.length() > 0) sb.append(", "); sb.append(word); } } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm index 91bed1ced7..b619325a96 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm @@ -42,8 +42,7 @@ blink::SemanticsAction GetSemanticsActionForScrollDirection( } // namespace -@implementation FlutterCustomAccessibilityAction - { +@implementation FlutterCustomAccessibilityAction { } @end @@ -185,12 +184,12 @@ blink::SemanticsAction GetSemanticsActionForScrollDirection( return NO; int32_t action_id = action.uid; std::vector args; - args.push_back(3); // type=int32. + args.push_back(3); // type=int32. args.push_back(action_id); args.push_back(action_id >> 8); args.push_back(action_id >> 16); args.push_back(action_id >> 24); - [self bridge] ->DispatchSemanticsAction([self uid], blink::SemanticsAction::kCustomAction, args); + [self bridge] -> DispatchSemanticsAction([self uid], blink::SemanticsAction::kCustomAction, args); return YES; } @@ -313,6 +312,13 @@ blink::SemanticsAction GetSemanticsActionForScrollDirection( return YES; } +- (BOOL)accessibilityPerformEscape { + if (![self node].HasAction(blink::SemanticsAction::kDismiss)) + return NO; + [self bridge] -> DispatchSemanticsAction([self uid], blink::SemanticsAction::kDismiss); + return YES; +} + #pragma mark UIAccessibilityFocus overrides - (void)accessibilityElementDidBecomeFocused { @@ -361,7 +367,9 @@ blink::SemanticsAction GetSemanticsActionForScrollDirection( [self node].HasAction(blink::SemanticsAction::kDecrease)) { traits |= UIAccessibilityTraitAdjustable; } + // TODO(jonahwilliams): switches should have a value of "on" or "off" if ([self node].HasFlag(blink::SemanticsFlags::kIsSelected) || + [self node].HasFlag(blink::SemanticsFlags::kIsToggled) || [self node].HasFlag(blink::SemanticsFlags::kIsChecked)) { traits |= UIAccessibilityTraitSelected; } @@ -375,6 +383,12 @@ blink::SemanticsAction GetSemanticsActionForScrollDirection( if ([self node].HasFlag(blink::SemanticsFlags::kIsHeader)) { traits |= UIAccessibilityTraitHeader; } + if ([self node].HasFlag(blink::SemanticsFlags::kIsImage)) { + traits |= UIAccessibilityTraitImage; + } + if ([self node].HasFlag(blink::SemanticsFlags::kIsLiveRegion)) { + traits |= UIAccessibilityTraitUpdatesFrequently; + } return traits; } @@ -504,7 +518,7 @@ void AccessibilityBridge::UpdateSemantics(blink::SemanticsNodeUpdates nodes, blink::CustomAccessibilityActionUpdates actions) { BOOL layoutChanged = NO; BOOL scrollOccured = NO; - for (const auto& entry: actions) { + for (const auto& entry : actions) { const blink::CustomAccessibilityAction& action = entry.second; actions_[action.id] = action; } @@ -524,14 +538,16 @@ void AccessibilityBridge::UpdateSemantics(blink::SemanticsNodeUpdates nodes, } object.children = newChildren; if (node.customAccessibilityActions.size() > 0) { - NSMutableArray* accessibilityCustomActions = + NSMutableArray* accessibilityCustomActions = [[[NSMutableArray alloc] init] autorelease]; for (int32_t action_id : node.customAccessibilityActions) { blink::CustomAccessibilityAction& action = actions_[action_id]; NSString* label = @(action.label.data()); SEL selector = @selector(onCustomAccessibilityAction:); - FlutterCustomAccessibilityAction* customAction = - [[FlutterCustomAccessibilityAction alloc] initWithName:label target:object selector:selector]; + FlutterCustomAccessibilityAction* customAction = + [[FlutterCustomAccessibilityAction alloc] initWithName:label + target:object + selector:selector]; customAction.uid = action_id; [accessibilityCustomActions addObject:customAction]; } @@ -597,10 +613,10 @@ void AccessibilityBridge::DispatchSemanticsAction(int32_t uid, blink::SemanticsA platform_view_->DispatchSemanticsAction(uid, action, args); } -void AccessibilityBridge::DispatchSemanticsAction(int32_t uid, - blink::SemanticsAction action, +void AccessibilityBridge::DispatchSemanticsAction(int32_t uid, + blink::SemanticsAction action, std::vector args) { - platform_view_->DispatchSemanticsAction(uid, action, args); + platform_view_->DispatchSemanticsAction(uid, action, args); } SemanticsObject* AccessibilityBridge::GetOrCreateObject(int32_t uid,