Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2f4a38d

Browse files
Android embedding refactor pr3 add remaining systemchannels (#7892)
Merging back in after reversion. Fixed some messaging issues in FlutterNativeView and corrected some message parsing.
1 parent 8427d73 commit 2f4a38d

File tree

12 files changed

+1595
-641
lines changed

12 files changed

+1595
-641
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,15 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/D
447447
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java
448448
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java
449449
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java
450+
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java
450451
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java
451452
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java
452453
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LocalizationChannel.java
453454
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/NavigationChannel.java
454455
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java
455456
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java
456457
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SystemChannel.java
458+
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java
457459
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/ActivityLifecycleListener.java
458460
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java
459461
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BinaryCodec.java

shell/platform/android/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ java_library("flutter_shell_java") {
114114
"io/flutter/embedding/engine/dart/PlatformMessageHandler.java",
115115
"io/flutter/embedding/engine/renderer/FlutterRenderer.java",
116116
"io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java",
117+
"io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java",
117118
"io/flutter/embedding/engine/systemchannels/KeyEventChannel.java",
118119
"io/flutter/embedding/engine/systemchannels/LifecycleChannel.java",
119120
"io/flutter/embedding/engine/systemchannels/LocalizationChannel.java",
120121
"io/flutter/embedding/engine/systemchannels/NavigationChannel.java",
121122
"io/flutter/embedding/engine/systemchannels/PlatformChannel.java",
122123
"io/flutter/embedding/engine/systemchannels/SettingsChannel.java",
123124
"io/flutter/embedding/engine/systemchannels/SystemChannel.java",
125+
"io/flutter/embedding/engine/systemchannels/TextInputChannel.java",
124126
"io/flutter/plugin/common/ActivityLifecycleListener.java",
125127
"io/flutter/plugin/common/BasicMessageChannel.java",
126128
"io/flutter/plugin/common/BinaryCodec.java",
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

shell/platform/android/io/flutter/embedding/engine/systemchannels/LocalizationChannel.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
import android.support.annotation.NonNull;
88

9+
import java.util.ArrayList;
910
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.Locale;
1013

1114
import io.flutter.embedding.engine.dart.DartExecutor;
1215
import io.flutter.plugin.common.JSONMethodCodec;
1316
import io.flutter.plugin.common.MethodChannel;
1417

1518
/**
16-
* TODO(mattcarroll): fill in javadoc for LocalizationChannel.
19+
* Sends the platform's locales to Dart.
1720
*/
1821
public class LocalizationChannel {
1922

@@ -24,12 +27,18 @@ public LocalizationChannel(@NonNull DartExecutor dartExecutor) {
2427
this.channel = new MethodChannel(dartExecutor, "flutter/localization", JSONMethodCodec.INSTANCE);
2528
}
2629

27-
public void setLocale(String language, String country) {
28-
channel.invokeMethod("setLocale", Arrays.asList(language, country));
29-
}
30-
31-
public void setMethodCallHandler(MethodChannel.MethodCallHandler handler) {
32-
channel.setMethodCallHandler(handler);
30+
/**
31+
* Send the given {@code locales} to Dart.
32+
*/
33+
public void sendLocales(List<Locale> locales) {
34+
List<String> data = new ArrayList<>();
35+
for (Locale locale : locales) {
36+
data.add(locale.getLanguage());
37+
data.add(locale.getCountry());
38+
data.add(locale.getScript());
39+
data.add(locale.getVariant());
40+
}
41+
channel.invokeMethod("setLocale", data);
3342
}
3443

3544
}

0 commit comments

Comments
 (0)