Skip to content

Commit

Permalink
Wire up basic a11y channel for a11y events (flutter#4054)
Browse files Browse the repository at this point in the history
  • Loading branch information
goderbauer authored Sep 1, 2017
1 parent c2e8a2e commit d60d630
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion shell/platform/android/io/flutter/view/AccessibilityBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -25,7 +29,7 @@
import java.util.Map;
import java.util.Set;

class AccessibilityBridge extends AccessibilityNodeProvider {
class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMessageChannel.MessageHandler<Object> {
private static final String TAG = "FlutterView";

private Map<Integer, SemanticsObject> mObjects;
Expand All @@ -34,6 +38,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider {
private SemanticsObject mFocusedObject;
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;
Expand All @@ -57,10 +63,17 @@ class AccessibilityBridge extends AccessibilityNodeProvider {
assert owner != null;
mOwner = owner;
mObjects = new HashMap<Integer, SemanticsObject>();
mFlutterAccessibilityChannel = new BasicMessageChannel<>(owner, "flutter/accessibility",
JSONMessageCodec.INSTANCE);
}

void setAccessibilityEnabled(boolean accessibilityEnabled) {
mAccessibilityEnabled = accessibilityEnabled;
if (accessibilityEnabled) {
mFlutterAccessibilityChannel.setMessageHandler(this);
} else {
mFlutterAccessibilityChannel.setMessageHandler(null);
}
}

@Override
Expand Down Expand Up @@ -325,6 +338,27 @@ private void sendAccessibilityEvent(int virtualViewId, int eventType) {
}
}

// Message Handler for [mFlutterAccessibilityChannel].
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
@SuppressWarnings("unchecked")
final JSONObject annotatedEvent = (JSONObject)message;
try {
final int nodeId = annotatedEvent.getInt("nodeId");
final JSONObject event = annotatedEvent.getJSONObject("data");
final String type = event.getString("type");

switch (type) {
case "scroll":
sendAccessibilityEvent(nodeId, AccessibilityEvent.TYPE_VIEW_SCROLLED);
break;
default:
assert false;
}
} catch (JSONException e) {
throw new IllegalArgumentException("Invalid JSON", e);
}
}

private void willRemoveSemanticsObject(SemanticsObject object) {
assert mObjects.containsKey(object.id);
assert mObjects.get(object.id) == object;
Expand Down

0 comments on commit d60d630

Please sign in to comment.