Skip to content

Commit

Permalink
Android embedding refactor pr3 add remaining systemchannels (flutter#…
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-carroll authored Feb 15, 2019
1 parent 02d7ca3 commit 256db4b
Show file tree
Hide file tree
Showing 11 changed files with 1,570 additions and 562 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,15 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/D
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LocalizationChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/NavigationChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SystemChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/ActivityLifecycleListener.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BinaryCodec.java
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ java_library("flutter_shell_java") {
"io/flutter/embedding/engine/dart/PlatformMessageHandler.java",
"io/flutter/embedding/engine/renderer/FlutterRenderer.java",
"io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java",
"io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java",
"io/flutter/embedding/engine/systemchannels/KeyEventChannel.java",
"io/flutter/embedding/engine/systemchannels/LifecycleChannel.java",
"io/flutter/embedding/engine/systemchannels/LocalizationChannel.java",
"io/flutter/embedding/engine/systemchannels/NavigationChannel.java",
"io/flutter/embedding/engine/systemchannels/PlatformChannel.java",
"io/flutter/embedding/engine/systemchannels/SettingsChannel.java",
"io/flutter/embedding/engine/systemchannels/SystemChannel.java",
"io/flutter/embedding/engine/systemchannels/TextInputChannel.java",
"io/flutter/plugin/common/ActivityLifecycleListener.java",
"io/flutter/plugin/common/BasicMessageChannel.java",
"io/flutter/plugin/common/BinaryCodec.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.flutter.embedding.engine.systemchannels;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.HashMap;

import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StandardMessageCodec;

/**
* System channel that sends accessibility requests and events from Flutter to Android.
* <p>
* See {@link AccessibilityMessageHandler}, which lists all accessibility requests and
* events that might be sent from Flutter to the Android platform.
*/
public class AccessibilityChannel {
@NonNull
public BasicMessageChannel<Object> channel;
@Nullable
private AccessibilityMessageHandler handler;

private final BasicMessageChannel.MessageHandler<Object> parsingMessageHandler = new BasicMessageChannel.MessageHandler<Object>() {
@Override
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
// If there is no handler to respond to this message then we don't need to
// parse it. Return.
if (handler == null) {
return;
}

@SuppressWarnings("unchecked")
final HashMap<String, Object> annotatedEvent = (HashMap<String, Object>) message;
final String type = (String) annotatedEvent.get("type");
@SuppressWarnings("unchecked")
final HashMap<String, Object> data = (HashMap<String, Object>) annotatedEvent.get("data");

switch (type) {
case "announce":
String announceMessage = (String) data.get("message");
if (announceMessage != null) {
handler.announce(announceMessage);
}
break;
case "tap": {
Integer nodeId = (Integer) annotatedEvent.get("nodeId");
if (nodeId != null) {
handler.onTap(nodeId);
}
break;
}
case "longPress": {
Integer nodeId = (Integer) annotatedEvent.get("nodeId");
if (nodeId != null) {
handler.onLongPress(nodeId);
}
break;
}
case "tooltip": {
String tooltipMessage = (String) data.get("message");
if (tooltipMessage != null) {
handler.onTooltip(tooltipMessage);
}
break;
}
}
}
};

/**
* Constructs an {@code AccessibilityChannel} that connects Android to the Dart code
* running in {@code dartExecutor}.
*
* The given {@code dartExecutor} is permitted to be idle or executing code.
*
* See {@link DartExecutor}.
*/
public AccessibilityChannel(@NonNull DartExecutor dartExecutor) {
channel = new BasicMessageChannel<>(dartExecutor, "flutter/accessibility", StandardMessageCodec.INSTANCE);
channel.setMessageHandler(parsingMessageHandler);
}

/**
* Sets the {@link AccessibilityMessageHandler} which receives all events and requests
* that are parsed from the underlying accessibility channel.
*/
public void setAccessibilityMessageHandler(@Nullable AccessibilityMessageHandler handler) {
this.handler = handler;
}

/**
* Handler that receives accessibility messages sent from Flutter to Android
* through a given {@link AccessibilityChannel}.
*
* To register an {@code AccessibilityMessageHandler} with a {@link AccessibilityChannel},
* see {@link AccessibilityChannel#setAccessibilityMessageHandler(AccessibilityMessageHandler)}.
*/
public interface AccessibilityMessageHandler {
/**
* The Dart application would like the given {@code message} to be announced.
*/
void announce(@NonNull String message);

/**
* The user has tapped on the artifact with the given {@code nodeId}.
*/
void onTap(int nodeId);

/**
* The user has long pressed on the artifact with the given {@code nodeId}.
*/
void onLongPress(int nodeId);

/**
* The user has opened a popup window, menu, dialog, etc.
*/
void onTooltip(@NonNull String message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.MethodChannel;

/**
* TODO(mattcarroll): fill in javadoc for LocalizationChannel.
* Sends the platform's locales to Dart.
*/
public class LocalizationChannel {

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

public void setLocale(String language, String country) {
channel.invokeMethod("setLocale", Arrays.asList(language, country));
}

public void setMethodCallHandler(MethodChannel.MethodCallHandler handler) {
channel.setMethodCallHandler(handler);
/**
* Send the given {@code locales} to Dart.
*/
public void sendLocales(List<Locale> locales) {
List<String> data = new ArrayList<>();
for (Locale locale : locales) {
data.add(locale.getLanguage());
data.add(locale.getCountry());
data.add(locale.getScript());
data.add(locale.getVariant());
}
channel.invokeMethod("setLocale", data);
}

}
Loading

0 comments on commit 256db4b

Please sign in to comment.