|
| 1 | +package io.flutter.embedding.engine.systemchannels; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | + |
| 8 | +import io.flutter.embedding.engine.dart.DartExecutor; |
| 9 | +import io.flutter.plugin.common.BasicMessageChannel; |
| 10 | +import io.flutter.plugin.common.StandardMessageCodec; |
| 11 | + |
| 12 | +/** |
| 13 | + * System channel that sends accessibility requests and events from Flutter to Android. |
| 14 | + * <p> |
| 15 | + * See {@link AccessibilityMessageHandler}, which lists all accessibility requests and |
| 16 | + * events that might be sent from Flutter to the Android platform. |
| 17 | + */ |
| 18 | +public class AccessibilityChannel { |
| 19 | + @NonNull |
| 20 | + public BasicMessageChannel<Object> channel; |
| 21 | + @Nullable |
| 22 | + private AccessibilityMessageHandler handler; |
| 23 | + |
| 24 | + private final BasicMessageChannel.MessageHandler<Object> parsingMessageHandler = new BasicMessageChannel.MessageHandler<Object>() { |
| 25 | + @Override |
| 26 | + public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) { |
| 27 | + // If there is no handler to respond to this message then we don't need to |
| 28 | + // parse it. Return. |
| 29 | + if (handler == null) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + @SuppressWarnings("unchecked") |
| 34 | + final HashMap<String, Object> annotatedEvent = (HashMap<String, Object>) message; |
| 35 | + final String type = (String) annotatedEvent.get("type"); |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + final HashMap<String, Object> data = (HashMap<String, Object>) annotatedEvent.get("data"); |
| 38 | + |
| 39 | + switch (type) { |
| 40 | + case "announce": |
| 41 | + String announceMessage = (String) data.get("message"); |
| 42 | + if (announceMessage != null) { |
| 43 | + handler.announce(announceMessage); |
| 44 | + } |
| 45 | + break; |
| 46 | + case "tap": { |
| 47 | + Integer nodeId = (Integer) annotatedEvent.get("nodeId"); |
| 48 | + if (nodeId != null) { |
| 49 | + handler.onTap(nodeId); |
| 50 | + } |
| 51 | + break; |
| 52 | + } |
| 53 | + case "longPress": { |
| 54 | + Integer nodeId = (Integer) annotatedEvent.get("nodeId"); |
| 55 | + if (nodeId != null) { |
| 56 | + handler.onLongPress(nodeId); |
| 57 | + } |
| 58 | + break; |
| 59 | + } |
| 60 | + case "tooltip": { |
| 61 | + String tooltipMessage = (String) data.get("message"); |
| 62 | + if (tooltipMessage != null) { |
| 63 | + handler.onTooltip(tooltipMessage); |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + /** |
| 72 | + * Constructs an {@code AccessibilityChannel} that connects Android to the Dart code |
| 73 | + * running in {@code dartExecutor}. |
| 74 | + * |
| 75 | + * The given {@code dartExecutor} is permitted to be idle or executing code. |
| 76 | + * |
| 77 | + * See {@link DartExecutor}. |
| 78 | + */ |
| 79 | + public AccessibilityChannel(@NonNull DartExecutor dartExecutor) { |
| 80 | + channel = new BasicMessageChannel<>(dartExecutor, "flutter/accessibility", StandardMessageCodec.INSTANCE); |
| 81 | + channel.setMessageHandler(parsingMessageHandler); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets the {@link AccessibilityMessageHandler} which receives all events and requests |
| 86 | + * that are parsed from the underlying accessibility channel. |
| 87 | + */ |
| 88 | + public void setAccessibilityMessageHandler(@Nullable AccessibilityMessageHandler handler) { |
| 89 | + this.handler = handler; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Handler that receives accessibility messages sent from Flutter to Android |
| 94 | + * through a given {@link AccessibilityChannel}. |
| 95 | + * |
| 96 | + * To register an {@code AccessibilityMessageHandler} with a {@link AccessibilityChannel}, |
| 97 | + * see {@link AccessibilityChannel#setAccessibilityMessageHandler(AccessibilityMessageHandler)}. |
| 98 | + */ |
| 99 | + public interface AccessibilityMessageHandler { |
| 100 | + /** |
| 101 | + * The Dart application would like the given {@code message} to be announced. |
| 102 | + */ |
| 103 | + void announce(@NonNull String message); |
| 104 | + |
| 105 | + /** |
| 106 | + * The user has tapped on the artifact with the given {@code nodeId}. |
| 107 | + */ |
| 108 | + void onTap(int nodeId); |
| 109 | + |
| 110 | + /** |
| 111 | + * The user has long pressed on the artifact with the given {@code nodeId}. |
| 112 | + */ |
| 113 | + void onLongPress(int nodeId); |
| 114 | + |
| 115 | + /** |
| 116 | + * The user has opened a popup window, menu, dialog, etc. |
| 117 | + */ |
| 118 | + void onTooltip(@NonNull String message); |
| 119 | + } |
| 120 | +} |
0 commit comments