Make naming consistent across channel APIs (flutter/engine#3574)

This commit is contained in:
Mikkel Nygaard Ravn
2017-04-18 14:30:31 +02:00
committed by GitHub
parent 926c6c3794
commit 4c457165be
27 changed files with 477 additions and 466 deletions

View File

@@ -78,16 +78,17 @@ java_library("flutter_shell_java") {
"io/flutter/app/FlutterActivity.java",
"io/flutter/app/FlutterApplication.java",
"io/flutter/plugin/common/ActivityLifecycleListener.java",
"io/flutter/plugin/common/BasicMessageChannel.java",
"io/flutter/plugin/common/BinaryCodec.java",
"io/flutter/plugin/common/BinaryMessenger.java",
"io/flutter/plugin/common/FlutterException.java",
"io/flutter/plugin/common/FlutterEventChannel.java",
"io/flutter/plugin/common/FlutterMessageChannel.java",
"io/flutter/plugin/common/FlutterMethodChannel.java",
"io/flutter/plugin/common/EventChannel.java",
"io/flutter/plugin/common/JSONMessageCodec.java",
"io/flutter/plugin/common/JSONMethodCodec.java",
"io/flutter/plugin/common/JSONUtil.java",
"io/flutter/plugin/common/MessageCodec.java",
"io/flutter/plugin/common/MethodCall.java",
"io/flutter/plugin/common/MethodChannel.java",
"io/flutter/plugin/common/MethodCodec.java",
"io/flutter/plugin/common/StandardMessageCodec.java",
"io/flutter/plugin/common/StandardMethodCodec.java",

View File

@@ -5,11 +5,9 @@
package io.flutter.plugin.common;
import android.util.Log;
import io.flutter.view.FlutterView;
import io.flutter.view.FlutterView.BinaryMessageReplyCallback;
import io.flutter.view.FlutterView.BinaryMessageResponse;
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
import java.nio.ByteBuffer;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
/**
* A named channel for communicating with the Flutter application using semi-structured messages.
@@ -20,31 +18,30 @@ import java.nio.ByteBuffer;
* counterpart of this channel on the Dart side. The static Java type of messages sent and received
* is Object, but only values supported by the specified {@link MessageCodec} can be used.
*
* The channel supports basic message send/receive operations, handling incoming method
* invocations, and emitting event streams. All communication is asynchronous.
* The channel supports basic message send/receive operations. All communication is asynchronous.
*
* Identically named channels may interfere with each other's communication.
*/
public final class FlutterMessageChannel<T> {
public final class BasicMessageChannel<T> {
private static final String TAG = "FlutterMessageChannel#";
private final FlutterView view;
private final BinaryMessenger messenger;
private final String name;
private final MessageCodec<T> codec;
/**
* Creates a new channel associated with the specified {@link FlutterView} and with the
* specified name and {@link MessageCodec}.
* Creates a new channel associated with the specified {@link BinaryMessenger}
* and with the specified name and {@link MessageCodec}.
*
* @param view a {@link FlutterView}.
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
* @param codec a {@link MessageCodec}.
*/
public FlutterMessageChannel(FlutterView view, String name, MessageCodec<T> codec) {
assert view != null;
public BasicMessageChannel(BinaryMessenger messenger, String name, MessageCodec<T> codec) {
assert messenger != null;
assert name != null;
assert codec != null;
this.view = view;
this.messenger = messenger;
this.name = name;
this.codec = codec;
}
@@ -62,11 +59,11 @@ public final class FlutterMessageChannel<T> {
* Sends the specified message to the Flutter application, optionally expecting a reply.
*
* @param message the message, possibly null.
* @param handler a {@link ReplyHandler} call-back, possibly null.
* @param callback a {@link Reply} callback, possibly null.
*/
public void send(T message, final ReplyHandler<T> handler) {
view.sendBinaryMessage(name, codec.encodeMessage(message),
handler == null ? null : new ReplyCallback(handler));
public void send(T message, final Reply<T> callback) {
messenger.send(name, codec.encodeMessage(message),
callback == null ? null : new IncomingReplyHandler(callback));
}
/**
@@ -77,87 +74,80 @@ public final class FlutterMessageChannel<T> {
* @param handler a {@link MessageHandler}, or null to deregister.
*/
public void setMessageHandler(final MessageHandler<T> handler) {
view.addOnBinaryMessageListenerAsync(name,
handler == null ? null : new MessageListener(handler));
messenger.setMessageHandler(name,
handler == null ? null : new IncomingMessageHandler(handler));
}
/**
* A call-back interface for handling replies to outgoing messages.
*/
public interface ReplyHandler<T> {
/**
* Handle the specified reply.
*
* @param reply the reply, possibly null.
*/
void onReply(T reply);
}
/**
* A call-back interface for handling incoming messages.
* A handler of incoming messages.
*/
public interface MessageHandler<T> {
/**
* Handles the specified message.
*
* @param message The message, possibly null.
* @param reply A {@link Reply} for providing a single message reply.
* @param message the message, possibly null.
* @param reply a {@link Reply} for providing a single message reply.
*/
void onMessage(T message, Reply<T> reply);
}
/**
* Response interface for sending results back to Flutter.
* Message reply callback. Used to submit a reply to an incoming
* message from Flutter. Also used in the dual capacity to handle a reply
* received from Flutter after sending a message.
*/
public interface Reply<T> {
/**
* Submits a reply.
* Handles the specified message reply.
*
* @param reply The result, possibly null.
* @param reply the reply, possibly null.
*/
void send(T reply);
void reply(T reply);
}
private final class ReplyCallback implements BinaryMessageReplyCallback {
private final ReplyHandler<T> handler;
private final class IncomingReplyHandler implements BinaryReply {
private final Reply<T> callback;
private ReplyCallback(ReplyHandler<T> handler) {
this.handler = handler;
private IncomingReplyHandler(Reply<T> callback) {
this.callback = callback;
}
@Override
public void onReply(ByteBuffer reply) {
handler.onReply(codec.decodeMessage(reply));
public void reply(ByteBuffer reply) {
try {
callback.reply(codec.decodeMessage(reply));
} catch (Exception e) {
Log.e(TAG + name, "Failed to handle message reply", e);
}
}
}
private final class MessageListener implements OnBinaryMessageListenerAsync {
private final class IncomingMessageHandler implements BinaryMessageHandler {
private final MessageHandler<T> handler;
private MessageListener(MessageHandler<T> handler) {
private IncomingMessageHandler(MessageHandler<T> handler) {
this.handler = handler;
}
@Override
public void onMessage(FlutterView view, ByteBuffer message,
final BinaryMessageResponse response) {
public void onMessage(ByteBuffer message, final BinaryReply callback) {
try {
handler.onMessage(codec.decodeMessage(message), new Reply<T>() {
private boolean done = false;
@Override
public void send(T reply) {
public void reply(T reply) {
if (done) {
throw new IllegalStateException("Call result already provided");
}
response.send(codec.encodeMessage(reply));
callback.reply(codec.encodeMessage(reply));
done = true;
}
});
} catch (Exception e) {
Log.e(TAG + name, "Failed to handle message", e);
response.send(null);
callback.reply(null);
}
}
}

View File

@@ -0,0 +1,75 @@
package io.flutter.plugin.common;
import java.nio.ByteBuffer;
/**
* Facility for communicating with Flutter using asynchronous message passing with binary messages.
* The Flutter Dart code can use {@code BinaryMessages} to participate.
*
* @see BasicMessageChannel , which supports message passing with Strings and semi-structured messages.
* @see MethodChannel , which supports communication using asynchronous method invocation.
* @see EventChannel , which supports communication using event streams.
*/
public interface BinaryMessenger {
/**
* Sends a binary message to the Flutter application. The Flutter Dart code can register a
* platform message handler with {@code BinaryMessages} that will receive these messages.
*
* @param channel the name {@link String} of the channel that will receive this message.
* @param message the message payload, a {@link ByteBuffer} with the message bytes between position
* zero and current position, or null.
*/
void send(String channel, ByteBuffer message);
/**
* Sends a binary message to the Flutter application,
*
* @param channel the name {@link String} of the channel that will receive this message.
* @param message the message payload, a {@link ByteBuffer} with the message bytes between position
* zero and current position, or null.
* @param callback a {@link BinaryReply} callback invoked when the Flutter application responds to the
* message, possibly null.
*/
void send(String channel, ByteBuffer message, BinaryReply callback);
/**
* Registers a handler to be invoked when the Flutter application sends a message
* to its host platform.
*
* Registration overwrites any previous registration for the same channel name.
* Use a {@code null} handler to unregister.
*
* @param channel the name {@link String} of the channel.
* @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages.
*/
void setMessageHandler(String channel, BinaryMessageHandler handler);
/**
* Handler for incoming binary messages from Flutter.
*/
interface BinaryMessageHandler {
/**
* Handles the specified message.
*
* @param message the message {@link ByteBuffer} payload.
* @param reply A {@link BinaryReply} used for submitting a reply back to Flutter.
*/
void onMessage(ByteBuffer message, BinaryReply reply);
}
/**
* Binary message reply callback. Used to submit a reply to an incoming
* message from Flutter. Also used in the dual capacity to handle a reply
* received from Flutter after sending a message.
*/
interface BinaryReply {
/**
* Handles the specified reply.
*
* @param reply the reply payload, a {@link ByteBuffer} or null. Senders of outgoing
* replies must place the reply bytes between position zero and current position.
* Reply receivers can read from the buffer directly.
*/
void reply(ByteBuffer reply);
}
}

View File

@@ -5,9 +5,8 @@
package io.flutter.plugin.common;
import android.util.Log;
import io.flutter.view.FlutterView;
import io.flutter.view.FlutterView.BinaryMessageResponse;
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;
@@ -27,37 +26,37 @@ import java.util.concurrent.atomic.AtomicReference;
* The identity of the channel is given by its name, so other uses of that name
* with may interfere with this channel's communication.
*/
public final class FlutterEventChannel {
private static final String TAG = "FlutterEventChannel#";
public final class EventChannel {
private static final String TAG = "EventChannel#";
private final FlutterView view;
private final BinaryMessenger messenger;
private final String name;
private final MethodCodec codec;
/**
* Creates a new channel associated with the specified {@link FlutterView} and with the
* specified name and the standard {@link MethodCodec}.
* Creates a new channel associated with the specified {@link BinaryMessenger}
* and with the specified name and the standard {@link MethodCodec}.
*
* @param view a {@link FlutterView}.
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
*/
public FlutterEventChannel(FlutterView view, String name) {
this(view, name, StandardMethodCodec.INSTANCE);
public EventChannel(BinaryMessenger messenger, String name) {
this(messenger, name, StandardMethodCodec.INSTANCE);
}
/**
* Creates a new channel associated with the specified {@link FlutterView} and with the
* specified name and {@link MethodCodec}.
* Creates a new channel associated with the specified {@link BinaryMessenger}
* and with the specified name and {@link MethodCodec}.
*
* @param view a {@link FlutterView}.
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
* @param codec a {@link MessageCodec}.
*/
public FlutterEventChannel(FlutterView view, String name, MethodCodec codec) {
assert view != null;
public EventChannel(BinaryMessenger messenger, String name, MethodCodec codec) {
assert messenger != null;
assert name != null;
assert codec != null;
this.view = view;
this.messenger = messenger;
this.name = name;
this.codec = codec;
}
@@ -70,15 +69,13 @@ public final class FlutterEventChannel {
* @param handler a {@link StreamHandler}, or null to deregister.
*/
public void setStreamHandler(final StreamHandler handler) {
view.addOnBinaryMessageListenerAsync(name,
handler == null ? null : new StreamListener(handler));
messenger.setMessageHandler(name, handler == null ? null : new IncomingStreamRequestHandler(handler));
}
/**
* Strategy for handling event streams. Supports dual use:
* Producers of events to be sent to Flutter act as clients of this interface
* for sending events. Consumers of events sent from Flutter implement
* this interface for handling received events.
* Event callback. Supports dual use: Producers of events to be sent to Flutter
* act as clients of this interface for sending events. Consumers of events sent
* from Flutter implement this interface for handling received events.
*/
public interface EventSink {
/**
@@ -106,16 +103,16 @@ public final class FlutterEventChannel {
}
/**
* A call-back interface for handling stream setup and tear-down requests.
* Handler of stream setup and tear-down requests.
*/
public interface StreamHandler {
/**
* Handles a request to set up an event stream.
*
* @param arguments Stream configuration arguments, possibly null.
* @param eventSink An {@link EventSink} for sending events to the Flutter receiver.
* @param events An {@link EventSink} for emitting events to the Flutter receiver.
*/
void onListen(Object arguments, EventSink eventSink);
void onListen(Object arguments, EventSink events);
/**
* Handles a request to tear down an event stream.
@@ -125,55 +122,54 @@ public final class FlutterEventChannel {
void onCancel(Object arguments);
}
private final class StreamListener implements OnBinaryMessageListenerAsync {
private final class IncomingStreamRequestHandler implements BinaryMessageHandler {
private final StreamHandler handler;
private final AtomicReference<EventSink> activeSink = new AtomicReference<>(null);
StreamListener(StreamHandler handler) {
IncomingStreamRequestHandler(StreamHandler handler) {
this.handler = handler;
}
@Override
public void onMessage(FlutterView view, ByteBuffer message,
final BinaryMessageResponse response) {
public void onMessage(ByteBuffer message, final BinaryReply reply) {
final MethodCall call = codec.decodeMethodCall(message);
if (call.method.equals("listen")) {
onListen(call.arguments, response);
onListen(call.arguments, reply);
} else if (call.method.equals("cancel")) {
onCancel(call.arguments, response);
onCancel(call.arguments, reply);
} else {
response.send(null);
reply.reply(null);
}
}
private void onListen(Object arguments, BinaryMessageResponse response) {
private void onListen(Object arguments, BinaryReply callback) {
final EventSink eventSink = new EventSinkImplementation();
if (activeSink.compareAndSet(null, eventSink)) {
try {
handler.onListen(arguments, eventSink);
response.send(codec.encodeSuccessEnvelope(null));
callback.reply(codec.encodeSuccessEnvelope(null));
} catch (Exception e) {
activeSink.set(null);
Log.e(TAG + name, "Failed to open event stream", e);
response.send(codec.encodeErrorEnvelope("error", e.getMessage(), null));
callback.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
} else {
response.send(codec.encodeErrorEnvelope("error", "Stream already active", null));
callback.reply(codec.encodeErrorEnvelope("error", "Stream already active", null));
}
}
private void onCancel(Object arguments, BinaryMessageResponse response) {
private void onCancel(Object arguments, BinaryReply callback) {
final EventSink oldSink = activeSink.getAndSet(null);
if (oldSink != null) {
try {
handler.onCancel(arguments);
response.send(codec.encodeSuccessEnvelope(null));
callback.reply(codec.encodeSuccessEnvelope(null));
} catch (Exception e) {
Log.e(TAG + name, "Failed to close event stream", e);
response.send(codec.encodeErrorEnvelope("error", e.getMessage(), null));
callback.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
} else {
response.send(codec.encodeErrorEnvelope("error", "No active stream to cancel", null));
callback.reply(codec.encodeErrorEnvelope("error", "No active stream to cancel", null));
}
}
@@ -183,10 +179,9 @@ public final class FlutterEventChannel {
if (activeSink.get() != this) {
return;
}
FlutterEventChannel.this.view.sendBinaryMessage(
EventChannel.this.messenger.send(
name,
codec.encodeSuccessEnvelope(event),
null);
codec.encodeSuccessEnvelope(event));
}
@Override
@@ -195,10 +190,9 @@ public final class FlutterEventChannel {
if (activeSink.get() != this) {
return;
}
FlutterEventChannel.this.view.sendBinaryMessage(
EventChannel.this.messenger.send(
name,
codec.encodeErrorEnvelope(errorCode, errorMessage, errorDetails),
null);
codec.encodeErrorEnvelope(errorCode, errorMessage, errorDetails));
}
@Override
@@ -206,7 +200,7 @@ public final class FlutterEventChannel {
if (activeSink.get() != this) {
return;
}
FlutterEventChannel.this.view.sendBinaryMessage(name, null, null);
EventChannel.this.messenger.send(name, null);
}
}
}

View File

@@ -8,7 +8,7 @@ import java.util.Map;
import org.json.JSONObject;
/**
* Command object representing a method call on a {@link FlutterMethodChannel}.
* Command object representing a method call on a {@link MethodChannel}.
*/
public final class MethodCall {
/**

View File

@@ -5,10 +5,8 @@
package io.flutter.plugin.common;
import android.util.Log;
import io.flutter.view.FlutterView;
import io.flutter.view.FlutterView.BinaryMessageReplyCallback;
import io.flutter.view.FlutterView.BinaryMessageResponse;
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import java.nio.ByteBuffer;
/**
@@ -25,37 +23,37 @@ import java.nio.ByteBuffer;
* The identity of the channel is given by its name, so other uses of that name
* with may interfere with this channel's communication.
*/
public final class FlutterMethodChannel {
private static final String TAG = "FlutterMethodChannel#";
public final class MethodChannel {
private static final String TAG = "MethodChannel#";
private final FlutterView view;
private final BinaryMessenger messenger;
private final String name;
private final MethodCodec codec;
/**
* Creates a new channel associated with the specified {@link FlutterView} and with the
* specified name and the standard {@link MethodCodec}.
* Creates a new channel associated with the specified {@link BinaryMessenger}
* and with the specified name and the standard {@link MethodCodec}.
*
* @param view a {@link FlutterView}.
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
*/
public FlutterMethodChannel(FlutterView view, String name) {
this(view, name, StandardMethodCodec.INSTANCE);
public MethodChannel(BinaryMessenger messenger, String name) {
this(messenger, name, StandardMethodCodec.INSTANCE);
}
/**
* Creates a new channel associated with the specified {@link FlutterView} and with the
* Creates a new channel associated with the specified {@link BinaryMessenger} and with the
* specified name and {@link MethodCodec}.
*
* @param view a {@link FlutterView}.
* @param messenger a {@link BinaryMessenger}.
* @param name a channel name String.
* @param codec a {@link MessageCodec}.
*/
public FlutterMethodChannel(FlutterView view, String name, MethodCodec codec) {
assert view != null;
public MethodChannel(BinaryMessenger messenger, String name, MethodCodec codec) {
assert messenger != null;
assert name != null;
assert codec != null;
this.view = view;
this.messenger = messenger;
this.name = name;
this.codec = codec;
}
@@ -75,11 +73,11 @@ public final class FlutterMethodChannel {
*
* @param method the name String of the method.
* @param arguments the arguments for the invocation, possibly null.
* @param handler a {@link Response} handler for the invocation result.
* @param callback a {@link Result} callback for the invocation result.
*/
public void invokeMethod(String method, Object arguments, Response handler) {
view.sendBinaryMessage(name, codec.encodeMethodCall(new MethodCall(method, arguments)),
handler == null ? null : new MethodCallResultCallback(handler));
public void invokeMethod(String method, Object arguments, Result callback) {
messenger.send(name, codec.encodeMethodCall(new MethodCall(method, arguments)),
callback == null ? null : new IncomingResultHandler(callback));
}
/**
@@ -90,17 +88,17 @@ public final class FlutterMethodChannel {
* @param handler a {@link MethodCallHandler}, or null to deregister.
*/
public void setMethodCallHandler(final MethodCallHandler handler) {
view.addOnBinaryMessageListenerAsync(name,
handler == null ? null : new MethodCallListener(handler));
messenger.setMessageHandler(name,
handler == null ? null : new IncomingMethodCallHandler(handler));
}
/**
* Strategy for handling the result of a method call. Supports dual use:
* Implementations of methods to be invoked by Flutter act as clients of this interface
* for sending results back to Flutter. Invokers of Flutter methods provide
* implementations of this interface for handling results received from Flutter.
* Method call result callback. Supports dual use: Implementations of methods
* to be invoked by Flutter act as clients of this interface for sending results
* back to Flutter. Invokers of Flutter methods provide implementations of this
* interface for handling results received from Flutter.
*/
public interface Response {
public interface Result {
/**
* Handles a successful result.
*
@@ -124,66 +122,65 @@ public final class FlutterMethodChannel {
}
/**
* A call-back interface for handling incoming method calls.
* A handler of incoming method calls.
*/
public interface MethodCallHandler {
/**
* Handles the specified method call.
*
* @param call A {@link MethodCall}.
* @param response A {@link Response} for providing a single method call result.
* @param result A {@link Result} used for submitting the result of the call.
*/
void onMethodCall(MethodCall call, Response response);
void onMethodCall(MethodCall call, Result result);
}
private final class MethodCallResultCallback implements BinaryMessageReplyCallback {
private final Response handler;
private final class IncomingResultHandler implements BinaryReply {
private final Result callback;
MethodCallResultCallback(Response handler) {
this.handler = handler;
IncomingResultHandler(Result callback) {
this.callback = callback;
}
@Override
public void onReply(ByteBuffer reply) {
public void reply(ByteBuffer reply) {
if (reply == null) {
handler.notImplemented();
callback.notImplemented();
} else {
try {
final Object result = codec.decodeEnvelope(reply);
handler.success(result);
callback.success(result);
} catch (FlutterException e) {
handler.error(e.code, e.getMessage(), e.details);
callback.error(e.code, e.getMessage(), e.details);
}
}
}
}
private final class MethodCallListener implements OnBinaryMessageListenerAsync {
private final class IncomingMethodCallHandler implements BinaryMessageHandler {
private final MethodCallHandler handler;
MethodCallListener(MethodCallHandler handler) {
IncomingMethodCallHandler(MethodCallHandler handler) {
this.handler = handler;
}
@Override
public void onMessage(FlutterView view, ByteBuffer message,
final BinaryMessageResponse response) {
public void onMessage(ByteBuffer message, final BinaryReply reply) {
final MethodCall call = codec.decodeMethodCall(message);
try {
handler.onMethodCall(call, new Response() {
handler.onMethodCall(call, new Result() {
private boolean done = false;
@Override
public void success(Object result) {
checkDone();
response.send(codec.encodeSuccessEnvelope(result));
reply.reply(codec.encodeSuccessEnvelope(result));
done = true;
}
@Override
public void error(String errorCode, String errorMessage, Object errorDetails) {
checkDone();
response.send(codec.encodeErrorEnvelope(
reply.reply(codec.encodeErrorEnvelope(
errorCode, errorMessage, errorDetails));
done = true;
}
@@ -191,7 +188,7 @@ public final class FlutterMethodChannel {
@Override
public void notImplemented() {
checkDone();
response.send(null);
reply.reply(null);
done = true;
}
@@ -203,7 +200,7 @@ public final class FlutterMethodChannel {
});
} catch (Exception e) {
Log.e(TAG + name, "Failed to handle method call", e);
response.send(codec.encodeErrorEnvelope("error", e.getMessage(), null));
reply.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
}
}

View File

@@ -9,7 +9,7 @@ import android.text.Selection;
import android.view.inputmethod.BaseInputConnection;
import android.view.KeyEvent;
import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterView;
import java.util.Arrays;
@@ -19,11 +19,11 @@ import java.util.Map;
class InputConnectionAdaptor extends BaseInputConnection {
private final int mClient;
private final TextInputPlugin mPlugin;
private final FlutterMethodChannel mFlutterChannel;
private final MethodChannel mFlutterChannel;
private final Map<String, Object> mOutgoingState;
public InputConnectionAdaptor(FlutterView view, int client,
TextInputPlugin plugin, FlutterMethodChannel flutterChannel) {
TextInputPlugin plugin, MethodChannel flutterChannel) {
super(view, true);
mClient = client;
mPlugin = plugin;

View File

@@ -11,9 +11,9 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
import io.flutter.plugin.common.FlutterMethodChannel.Response;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.JSONUtil;
import io.flutter.plugin.common.MethodCall;
@@ -31,7 +31,7 @@ public class TextInputPlugin implements MethodCallHandler {
private final Activity mActivity;
private final FlutterView mView;
private final FlutterMethodChannel mFlutterChannel;
private final MethodChannel mFlutterChannel;
private int mClient = 0;
private JSONObject mConfiguration;
private JSONObject mLatestState;
@@ -39,37 +39,37 @@ public class TextInputPlugin implements MethodCallHandler {
public TextInputPlugin(Activity activity, FlutterView view) {
mActivity = activity;
mView = view;
mFlutterChannel = new FlutterMethodChannel(view, "flutter/textinput",
mFlutterChannel = new MethodChannel(view, "flutter/textinput",
JSONMethodCodec.INSTANCE);
mFlutterChannel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(MethodCall call, Response response) {
public void onMethodCall(MethodCall call, Result result) {
String method = call.method;
Object args = call.arguments;
try {
if (method.equals("TextInput.show")) {
showTextInput(mView);
response.success(null);
result.success(null);
} else if (method.equals("TextInput.hide")) {
hideTextInput(mView);
response.success(null);
result.success(null);
} else if (method.equals("TextInput.setClient")) {
final JSONArray argumentList = (JSONArray) args;
setTextInputClient(mView, argumentList.getInt(0), argumentList.getJSONObject(1));
response.success(null);
result.success(null);
} else if (method.equals("TextInput.setEditingState")) {
setTextInputEditingState(mView, (JSONObject) args);
response.success(null);
result.success(null);
} else if (method.equals("TextInput.clearClient")) {
clearTextInputClient();
response.success(null);
result.success(null);
} else {
response.notImplemented();
result.notImplemented();
}
} catch (JSONException e) {
response.error("error", "JSON error: " + e.getMessage(), null);
result.error("error", "JSON error: " + e.getMessage(), null);
}
}

View File

@@ -17,12 +17,11 @@ import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;
import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.util.PathUtils;
import io.flutter.plugin.common.ActivityLifecycleListener;
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
import io.flutter.plugin.common.FlutterMethodChannel.Response;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import org.json.JSONArray;
@@ -44,48 +43,48 @@ public class PlatformPlugin implements MethodCallHandler, ActivityLifecycleListe
}
@Override
public void onMethodCall(MethodCall call, Response response) {
public void onMethodCall(MethodCall call, Result result) {
String method = call.method;
Object arguments = call.arguments;
try {
if (method.equals("SystemSound.play")) {
playSystemSound((String) arguments);
response.success(null);
result.success(null);
} else if (method.equals("HapticFeedback.vibrate")) {
vibrateHapticFeedback();
response.success(null);
result.success(null);
} else if (method.equals("UrlLauncher.launch")) {
launchURL((String) arguments);
response.success(null);
result.success(null);
} else if (method.equals("SystemChrome.setPreferredOrientations")) {
setSystemChromePreferredOrientations((JSONArray) arguments);
response.success(null);
result.success(null);
} else if (method.equals("SystemChrome.setApplicationSwitcherDescription")) {
setSystemChromeApplicationSwitcherDescription((JSONObject) arguments);
response.success(null);
result.success(null);
} else if (method.equals("SystemChrome.setEnabledSystemUIOverlays")) {
setSystemChromeEnabledSystemUIOverlays((JSONArray) arguments);
response.success(null);
result.success(null);
} else if (method.equals("SystemChrome.setSystemUIOverlayStyle")) {
setSystemChromeSystemUIOverlayStyle((String) arguments);
response.success(null);
result.success(null);
} else if (method.equals("SystemNavigator.pop")) {
popSystemNavigator();
response.success(null);
result.success(null);
} else if (method.equals("Clipboard.getData")) {
response.success(getClipboardData((String) arguments));
result.success(getClipboardData((String) arguments));
} else if (method.equals("Clipboard.setData")) {
setClipboardData((JSONObject) arguments);
response.success(null);
result.success(null);
} else if (method.equals("PathProvider.getTemporaryDirectory")) {
response.success(getPathProviderTemporaryDirectory());
result.success(getPathProviderTemporaryDirectory());
} else if (method.equals("PathProvider.getApplicationDocumentsDirectory")) {
response.success(getPathProviderApplicationDocumentsDirectory());
result.success(getPathProviderApplicationDocumentsDirectory());
} else {
response.notImplemented();
result.notImplemented();
}
} catch (JSONException e) {
response.error("error", "JSON error: " + e.getMessage(), null);
result.error("error", "JSON error: " + e.getMessage(), null);
}
}

View File

@@ -28,12 +28,8 @@ import android.view.accessibility.AccessibilityNodeProvider;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import io.flutter.plugin.common.ActivityLifecycleListener;
import io.flutter.plugin.common.FlutterMessageChannel;
import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.StringCodec;
import io.flutter.plugin.common.*;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.editing.TextInputPlugin;
import io.flutter.plugin.platform.PlatformPlugin;
@@ -54,7 +50,7 @@ import java.util.Map;
* An Android view containing a Flutter app.
*/
public class FlutterView extends SurfaceView
implements AccessibilityManager.AccessibilityStateChangeListener {
implements BinaryMessenger, AccessibilityManager.AccessibilityStateChangeListener {
private static final String TAG = "FlutterView";
@@ -72,15 +68,15 @@ public class FlutterView extends SurfaceView
}
private final TextInputPlugin mTextInputPlugin;
private final Map<String, OnBinaryMessageListenerAsync> mMessageListeners;
private final Map<String, BinaryMessageHandler> mMessageHandlers;
private final SurfaceHolder.Callback mSurfaceCallback;
private final ViewportMetrics mMetrics;
private final AccessibilityManager mAccessibilityManager;
private final FlutterMethodChannel mFlutterLocalizationChannel;
private final FlutterMethodChannel mFlutterNavigationChannel;
private final FlutterMessageChannel<Object> mFlutterKeyEventChannel;
private final FlutterMessageChannel<String> mFlutterLifecycleChannel;
private final FlutterMessageChannel<Object> mFlutterSystemChannel;
private final MethodChannel mFlutterLocalizationChannel;
private final MethodChannel mFlutterNavigationChannel;
private final BasicMessageChannel<Object> mFlutterKeyEventChannel;
private final BasicMessageChannel<String> mFlutterLifecycleChannel;
private final BasicMessageChannel<Object> mFlutterSystemChannel;
private final BroadcastReceiver mDiscoveryReceiver;
private final List<ActivityLifecycleListener> mActivityLifecycleListeners;
private long mNativePlatformView;
@@ -134,22 +130,22 @@ public class FlutterView extends SurfaceView
mAccessibilityManager = (AccessibilityManager) getContext()
.getSystemService(Context.ACCESSIBILITY_SERVICE);
mMessageListeners = new HashMap<>();
mMessageHandlers = new HashMap<>();
mActivityLifecycleListeners = new ArrayList<>();
// Configure the platform plugins and flutter channels.
mFlutterLocalizationChannel = new FlutterMethodChannel(this, "flutter/localization",
mFlutterLocalizationChannel = new MethodChannel(this, "flutter/localization",
JSONMethodCodec.INSTANCE);
mFlutterNavigationChannel = new FlutterMethodChannel(this, "flutter/navigation",
mFlutterNavigationChannel = new MethodChannel(this, "flutter/navigation",
JSONMethodCodec.INSTANCE);
mFlutterKeyEventChannel = new FlutterMessageChannel<>(this, "flutter/keyevent",
mFlutterKeyEventChannel = new BasicMessageChannel<>(this, "flutter/keyevent",
JSONMessageCodec.INSTANCE);
mFlutterLifecycleChannel = new FlutterMessageChannel<>(this, "flutter/lifecycle",
mFlutterLifecycleChannel = new BasicMessageChannel<>(this, "flutter/lifecycle",
StringCodec.INSTANCE);
mFlutterSystemChannel = new FlutterMessageChannel<>(this, "flutter/system",
mFlutterSystemChannel = new BasicMessageChannel<>(this, "flutter/system",
JSONMessageCodec.INSTANCE);
PlatformPlugin platformPlugin = new PlatformPlugin((Activity) getContext());
FlutterMethodChannel flutterPlatformChannel = new FlutterMethodChannel(this,
MethodChannel flutterPlatformChannel = new MethodChannel(this,
"flutter/platform", JSONMethodCodec.INSTANCE);
flutterPlatformChannel.setMethodCallHandler(platformPlugin);
addActivityLifecycleListener(platformPlugin);
@@ -604,44 +600,44 @@ public class FlutterView extends SurfaceView
}
// Called by native to send us a platform message.
private void handlePlatformMessage(String channel, byte[] message, final int responseId) {
OnBinaryMessageListenerAsync listener = mMessageListeners.get(channel);
if (listener != null) {
private void handlePlatformMessage(String channel, byte[] message, final int replyId) {
BinaryMessageHandler handler = mMessageHandlers.get(channel);
if (handler != null) {
try {
final ByteBuffer buffer = (message == null ? null : ByteBuffer.wrap(message));
listener.onMessage(this, buffer,
new BinaryMessageResponse() {
handler.onMessage(buffer,
new BinaryReply() {
@Override
public void send(ByteBuffer response) {
if (response == null) {
public void reply(ByteBuffer reply) {
if (reply == null) {
nativeInvokePlatformMessageEmptyResponseCallback(mNativePlatformView,
responseId);
replyId);
} else {
nativeInvokePlatformMessageResponseCallback(mNativePlatformView,
responseId, response, response.position());
replyId, reply, reply.position());
}
}
});
} catch (Exception ex) {
Log.e(TAG, "Uncaught exception in binary message listener", ex);
nativeInvokePlatformMessageEmptyResponseCallback(mNativePlatformView, responseId);
nativeInvokePlatformMessageEmptyResponseCallback(mNativePlatformView, replyId);
}
return;
}
nativeInvokePlatformMessageEmptyResponseCallback(mNativePlatformView, responseId);
nativeInvokePlatformMessageEmptyResponseCallback(mNativePlatformView, replyId);
}
private int mNextResponseId = 1;
private final Map<Integer, BinaryMessageReplyCallback> mPendingResponses = new HashMap<>();
private int mNextReplyId = 1;
private final Map<Integer, BinaryReply> mPendingReplies = new HashMap<>();
// Called by native to respond to a platform message that we sent.
private void handlePlatformMessageResponse(int responseId, byte[] response) {
BinaryMessageReplyCallback callback = mPendingResponses.remove(responseId);
private void handlePlatformMessageResponse(int replyId, byte[] reply) {
BinaryReply callback = mPendingReplies.remove(replyId);
if (callback != null) {
try {
callback.onReply(response == null ? null : ByteBuffer.wrap(response));
callback.reply(reply == null ? null : ByteBuffer.wrap(reply));
} catch (Exception ex) {
Log.e(TAG, "Uncaught exception in binary message listener reply", ex);
Log.e(TAG, "Uncaught exception in binary message handler reply", ex);
}
}
}
@@ -764,71 +760,35 @@ public class FlutterView extends SurfaceView
return true;
}
/**
* Send a binary message to the Flutter application. The Flutter Dart code can register a
* platform message handler that will receive these messages.
*
* @param channel Name of the channel that will receive this message.
* @param message Message payload, a {@link ByteBuffer} with the message bytes between position
* zero and current position, or null.
* @param callback Callback that receives a reply from the Flutter application.
*/
public void sendBinaryMessage(String channel, ByteBuffer message,
BinaryMessageReplyCallback callback) {
int responseId = 0;
@Override
public void send(String channel, ByteBuffer message) {
send(channel, message, null);
}
@Override
public void send(String channel, ByteBuffer message, BinaryReply callback) {
int replyId = 0;
if (callback != null) {
responseId = mNextResponseId++;
mPendingResponses.put(responseId, callback);
replyId = mNextReplyId++;
mPendingReplies.put(replyId, callback);
}
if (message == null) {
nativeDispatchEmptyPlatformMessage(mNativePlatformView, channel, responseId);
nativeDispatchEmptyPlatformMessage(mNativePlatformView, channel, replyId);
} else {
nativeDispatchPlatformMessage(mNativePlatformView, channel, message,
message.position(), responseId);
message.position(), replyId);
}
}
/**
* Callback invoked when the app replies to a binary message sent with sendBinaryMessage.
*/
public interface BinaryMessageReplyCallback {
void onReply(ByteBuffer reply);
}
/**
* Register a callback to be invoked when the Flutter application sends a message
* to its host. The reply to the message can be provided asynchronously.
*
* @param channel Name of the channel used by the application.
* @param listener Called when messages arrive.
*/
public void addOnBinaryMessageListenerAsync(String channel,
OnBinaryMessageListenerAsync listener) {
if (listener == null) {
mMessageListeners.remove(channel);
@Override
public void setMessageHandler(String channel, BinaryMessageHandler handler) {
if (handler == null) {
mMessageHandlers.remove(channel);
} else {
mMessageListeners.put(channel, listener);
mMessageHandlers.put(channel, handler);
}
}
public interface OnBinaryMessageListenerAsync {
/**
* Called when a message is received from the Flutter app.
*
* @param view The Flutter view hosting the app.
* @param message Message payload.
* @param response Used to send a reply back to the app.
*/
void onMessage(FlutterView view, ByteBuffer message, BinaryMessageResponse response);
}
public interface BinaryMessageResponse {
void send(ByteBuffer reply);
}
/**
* Broadcast receiver used to discover active Flutter instances.
*/

View File

@@ -11,25 +11,35 @@
NS_ASSUME_NONNULL_BEGIN
/**
A strategy for handling a binary message reply.
A message reply callback.
Used for submitting a binary reply back to a Flutter message sender. Also used
in the dual capacity for handling a binary message reply received from Flutter.
- Parameters:
- reply: The reply.
*/
typedef void (^FlutterBinaryReplyHandler)(NSData* _Nullable reply);
typedef void (^FlutterBinaryReply)(NSData* _Nullable reply);
/**
A strategy for handling incoming binary messages and to send asynchronous
replies.
A strategy for handling incoming binary messages from Flutter and to send
asynchronous replies back to Flutter.
- Parameters:
- message: The message.
- reply: A callback for submitting a reply to the sender.
*/
typedef void (^FlutterBinaryMessageHandler)(
NSData* _Nullable message,
FlutterBinaryReplyHandler replyHandler);
typedef void (^FlutterBinaryMessageHandler)(NSData* _Nullable message, FlutterBinaryReply reply);
/**
A facility for communicating with the Flutter side using asynchronous message
passing with binary messages.
- SeeAlso:
- `FlutterMessageChannel`, which supports communication using structured messages.
- `FlutterMethodChannel`, which supports communication using asynchronous method calls.
- `FlutterBasicMessageChannel`, which supports communication using structured
messages.
- `FlutterMethodChannel`, which supports communication using asynchronous
method calls.
- `FlutterEventChannel`, which supports commuication using event streams.
*/
FLUTTER_EXPORT
@@ -39,24 +49,23 @@ FLUTTER_EXPORT
no reply.
- Parameters:
- channel: The channel name.
- message: The message.
- channelName: The channel name.
*/
- (void)sendBinaryMessage:(NSData* _Nullable)message
channelName:(NSString*)channelName;
- (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message;
/**
Sends a binary message to the Flutter side on the specified channel, expecting
an asynchronous reply.
- Parameters:
- channel: The channel name.
- message: The message.
- channelName: The channel name.
- handler: A reply handler.
- callback: A callback for receiving a reply.
*/
- (void)sendBinaryMessage:(NSData* _Nullable)message
channelName:(NSString*)channelName
binaryReplyHandler:(FlutterBinaryReplyHandler _Nullable)handler;
- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)callback;
/**
Registers a message handler for incoming binary messages from the Flutter side
@@ -66,11 +75,11 @@ FLUTTER_EXPORT
existing handler.
- Parameters:
- channelName: The channel name.
- channel: The channel name.
- handler: The message handler.
*/
- (void)setBinaryMessageHandlerOnChannel:(NSString*)channelName
binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler;
- (void)setMessageHandlerOnChannel:(NSString*)channel
binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler;
@end
NS_ASSUME_NONNULL_END
#endif // FLUTTER_FLUTTERBINARYMESSENGER_H_

View File

@@ -10,30 +10,34 @@
NS_ASSUME_NONNULL_BEGIN
/**
A strategy for handling a message reply.
A message reply callback.
Used for submitting a reply back to a Flutter message sender. Also used in
the dual capacity for handling a message reply received from Flutter.
- Parameter reply: The reply.
*/
typedef void (^FlutterReplyHandler)(id _Nullable reply);
typedef void (^FlutterReply)(id _Nullable reply);
/**
A strategy for handling a message.
A strategy for handling incoming messages from Flutter and to send
asynchronous replies back to Flutter.
- Parameters:
- message: The incoming message.
- replyHandler: A call-back to asynchronously supply a reply to the message.
- message: The message.
- reply: A callback for submitting a reply to the sender.
*/
typedef void (^FlutterMessageHandler)(id _Nullable message,
FlutterReplyHandler replyHandler);
typedef void (^FlutterMessageHandler)(id _Nullable message, FlutterReply callback);
/**
A channel for communicating with the Flutter side using asynchronous message
passing.
A channel for communicating with the Flutter side using basic, asynchronous
message passing.
*/
FLUTTER_EXPORT
@interface FlutterMessageChannel : NSObject
@interface FlutterBasicMessageChannel : NSObject
/**
Creates a `FlutterMessageChannel` with the specified name and binary messenger.
Creates a `FlutterBasicMessageChannel` with the specified name and binary
messenger.
The channel name logically identifies the channel; identically named channels
interfere with each other's communication.
@@ -51,7 +55,8 @@ FLUTTER_EXPORT
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger;
/**
Creates a `FlutterMessageChannel` with the specified name, binary messenger,
Creates a `FlutterBasicMessageChannel` with the specified name, binary
messenger,
and message codec.
The channel name logically identifies the channel; identically named channels
@@ -70,8 +75,8 @@ FLUTTER_EXPORT
codec:(NSObject<FlutterMessageCodec>*)codec;
/**
Initializes a `FlutterMessageChannel` with the specified nane, binary messenger,
and message codec.
Initializes a `FlutterBasicMessageChannel` with the specified name, binary
messenger, and message codec.
The channel name logically identifies the channel; identically named channels
interfere with each other's communication.
@@ -91,18 +96,20 @@ FLUTTER_EXPORT
/**
Sends the specified message to the Flutter side, ignoring any reply.
- Parameter message: The message. Must be supported by the codec of this channel.
- Parameter message: The message. Must be supported by the codec of this
channel.
*/
- (void)sendMessage:(id _Nullable)message;
/**
Sends the specified message to the Flutter side, expecting an asynchronous reply.
Sends the specified message to the Flutter side, expecting an asynchronous
reply.
- Parameters:
- message: The message. Must be supported by the codec of this channel.
- handler: The reply handler.
- callback: A callback to be invoked with the message reply from Flutter.
*/
- (void)sendMessage:(id _Nullable)message replyHandler:(FlutterReplyHandler _Nullable)handler;
- (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback;
/**
Registers a message handler with this channel.
@@ -116,29 +123,27 @@ FLUTTER_EXPORT
@end
/**
A receiver of the result of a method call.
A method call result callback.
- Parameter result: The result. Will be a `FlutterError` instance, if the method
call resulted in an error on the Flutter side. Will be
`FlutterMethodNotImplemented`, if the method called was not implemented on
the Flutter side. All other values, including `nil` should be interpreted
as successful results.
Used for submitting a method call result back to a Flutter caller. Also used in
the dual capacity for handling a method call result received from Flutter.
- Parameter result: The result.
*/
typedef void (^FlutterResultReceiver)(id _Nullable result);
typedef void (^FlutterResult)(id _Nullable result);
/**
A strategy for handling method calls.
- Parameters:
- call: The incoming method call.
- resultReceiver: A call-back to asynchronously supply the result of the call.
Invoke the call-back with a `FlutterError` to indicate that the call failed.
Invoke the call-back with `FlutterMethodNotImplemented` to indicate that the
method was unknown. Any other values, including `nil` are interpreted as
- result: A callback to asynchronously submit the result of the call.
Invoke the callback with a `FlutterError` to indicate that the call failed.
Invoke the callback with `FlutterMethodNotImplemented` to indicate that the
method was unknown. Any other values, including `nil`, are interpreted as
successful results.
*/
typedef void (^FlutterMethodCallHandler)(FlutterMethodCall* call,
FlutterResultReceiver resultReceiver);
typedef void (^FlutterMethodCallHandler)(FlutterMethodCall* call, FlutterResult result);
/**
A constant used with `FlutterMethodCallHandler` to respond to the call of an
@@ -147,7 +152,6 @@ typedef void (^FlutterMethodCallHandler)(FlutterMethodCall* call,
FLUTTER_EXPORT
extern NSObject const* FlutterMethodNotImplemented;
/**
A channel for communicating with the Flutter side using invocation of
asynchronous methods.
@@ -190,7 +194,7 @@ FLUTTER_EXPORT
*/
+ (instancetype)methodChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
codec:(NSObject<FlutterMethodCodec>*)codec;
codec:(NSObject<FlutterMethodCodec>*)codec;
/**
Initializes a `FlutterMethodChannel` with the specified name, binary messenger,
@@ -230,15 +234,15 @@ FLUTTER_EXPORT
- method: The name of the method to invoke.
- arguments: The arguments. Must be a value supported by the codec of this
channel.
- resultReceiver: A call-back for receipt of an asynchronous result.
- result: A callback that will be invoked with the asynchronous result.
The result will be a `FlutterError` instance, if the method call resulted
in an error on the Flutter side. Will be `FlutterMethodNotImplemented`, if
the method called was not implemented on the Flutter side. Any other value,
including `nil` should be interpreted as successful results.
including `nil`, should be interpreted as successful results.
*/
- (void)invokeMethod:(NSString*)method
arguments:(id _Nullable)arguments
resultReceiver:(FlutterResultReceiver _Nullable)resultReceiver;
result:(FlutterResult _Nullable)callback;
/**
Registers a handler for method calls from the Flutter side.
@@ -252,14 +256,11 @@ FLUTTER_EXPORT
@end
/**
A strategy for consuming events.
An event sink callback.
- Parameter event: The event. Will be a `FlutterError` instance, if the
event represents an error. Will be `FlutterEndOfEventStream`, if no more
events will be emitted. All other values, including `nil` should be
interpreted as success events.
- Parameter event: The event.
*/
typedef void (^FlutterEventReceiver)(id _Nullable event);
typedef void (^FlutterEventSink)(id _Nullable event);
/**
A strategy for exposing an event stream to the Flutter side.
@@ -274,15 +275,15 @@ FLUTTER_EXPORT
- Parameters:
- arguments: Arguments for the stream.
- eventReceiver: A call-back to asynchronously emit events. Invoke the
call-back with a `FlutterError` to emit an error event. Invoke the
call-back with `FlutterEndOfEventStream` to indicate that no more
- events: A callback to asynchronously emit events. Invoke the
callback with a `FlutterError` to emit an error event. Invoke the
callback with `FlutterEndOfEventStream` to indicate that no more
events will be emitted. Any other value, including `nil` are emitted as
successful events.
- Returns: A FlutterError instance, if setup fails.
*/
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
eventReceiver:(FlutterEventReceiver)eventReceiver;
eventSink:(FlutterEventSink)events;
/**
Tears down an event stream.
@@ -372,7 +373,7 @@ FLUTTER_EXPORT
- Parameter handler: The stream handler.
*/
- (void)setStreamHandler:(NSObject<FlutterStreamHandler>* _Nullable)streamHandler;
- (void)setStreamHandler:(NSObject<FlutterStreamHandler>* _Nullable)handler;
@end
NS_ASSUME_NONNULL_END

View File

@@ -95,8 +95,7 @@ FLUTTER_EXPORT
- method: the name of the method to call.
- arguments: the arguments value.
*/
+ (instancetype)methodCallWithMethodName:(NSString*)method
arguments:(id _Nullable)arguments;
+ (instancetype)methodCallWithMethodName:(NSString*)method arguments:(id _Nullable)arguments;
/**
The method name.

View File

@@ -12,16 +12,13 @@
FLUTTER_EXPORT
@interface FlutterDartProject : NSObject
- (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFLXArchive:(NSURL*)archiveURL
dartMain:(NSURL*)dartMainURL
packages:(NSURL*)dartPackages
NS_DESIGNATED_INITIALIZER;
packages:(NSURL*)dartPackages NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFLXArchiveWithScriptSnapshot:(NSURL*)archiveURL
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFLXArchiveWithScriptSnapshot:(NSURL*)archiveURL NS_DESIGNATED_INITIALIZER;
- (instancetype)initFromDefaultSourceForConfiguration;

View File

@@ -17,7 +17,7 @@
#ifndef NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#define NS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
#endif // defined(NS_ASSUME_NONNULL_BEGIN)
#define NS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
#endif // defined(NS_ASSUME_NONNULL_BEGIN)
#endif // FLUTTER_FLUTTERMACROS_H_

View File

@@ -17,8 +17,7 @@ FLUTTER_EXPORT
- (instancetype)initWithProject:(FlutterDartProject*)project
nibName:(NSString*)nibNameOrNil
bundle:(NSBundle*)nibBundleOrNil
NS_DESIGNATED_INITIALIZER;
bundle:(NSBundle*)nibBundleOrNil NS_DESIGNATED_INITIALIZER;
- (void)handleStatusBarTouches:(UIEvent*)event;
@end

View File

@@ -6,7 +6,7 @@
#pragma mark - Basic message channel
@implementation FlutterMessageChannel {
@implementation FlutterBasicMessageChannel {
NSObject<FlutterBinaryMessenger>* _messenger;
NSString* _name;
NSObject<FlutterMessageCodec>* _codec;
@@ -14,13 +14,16 @@
+ (instancetype)messageChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
NSObject<FlutterMessageCodec>* codec = [FlutterStandardMessageCodec sharedInstance];
return [FlutterMessageChannel messageChannelWithName:name binaryMessenger:messenger codec:codec];
return [FlutterBasicMessageChannel messageChannelWithName:name
binaryMessenger:messenger
codec:codec];
}
+ (instancetype)messageChannelWithName:(NSString*)name
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
codec:(NSObject<FlutterMessageCodec>*)codec {
return [[[FlutterMessageChannel alloc] initWithName:name binaryMessenger:messenger codec:codec]
autorelease];
return
[[[FlutterBasicMessageChannel alloc] initWithName:name binaryMessenger:messenger codec:codec]
autorelease];
}
- (instancetype)initWithName:(NSString*)name
@@ -42,31 +45,28 @@
}
- (void)sendMessage:(id)message {
[_messenger sendBinaryMessage:[_codec encode:message] channelName:_name];
[_messenger sendOnChannel:_name message:[_codec encode:message]];
}
- (void)sendMessage:(id)message replyHandler:(FlutterReplyHandler)handler {
FlutterBinaryReplyHandler replyHandler = ^(NSData* reply) {
if (handler)
handler([_codec decode:reply]);
- (void)sendMessage:(id)message reply:(FlutterReply)callback {
FlutterBinaryReply reply = ^(NSData* data) {
if (callback)
callback([_codec decode:data]);
};
[_messenger sendBinaryMessage:[_codec encode:message]
channelName:_name
binaryReplyHandler:replyHandler];
[_messenger sendOnChannel:_name message:[_codec encode:message] binaryReply:reply];
}
- (void)setMessageHandler:(FlutterMessageHandler)handler {
if (!handler) {
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:nil];
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
return;
}
FlutterBinaryMessageHandler messageHandler =
^(NSData* message, FlutterBinaryReplyHandler replyHandler) {
handler([_codec decode:message], ^(id reply) {
replyHandler([_codec encode:reply]);
});
};
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
handler([_codec decode:message], ^(id reply) {
callback([_codec encode:reply]);
});
};
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
}
@end
@@ -188,40 +188,38 @@ NSObject const* FlutterMethodNotImplemented = [NSObject new];
FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:method arguments:arguments];
NSData* message = [_codec encodeMethodCall:methodCall];
[_messenger sendBinaryMessage:message channelName:_name];
[_messenger sendOnChannel:_name message:message];
}
- (void)invokeMethod:(NSString*)method
arguments:(id)arguments
resultReceiver:(FlutterResultReceiver)resultReceiver {
- (void)invokeMethod:(NSString*)method arguments:(id)arguments result:(FlutterResult)callback {
FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:method arguments:arguments];
NSData* message = [_codec encodeMethodCall:methodCall];
FlutterBinaryReplyHandler replyHandler = ^(NSData* reply) {
if (resultReceiver) {
resultReceiver((reply == nil) ? FlutterMethodNotImplemented : [_codec decodeEnvelope:reply]);
FlutterBinaryReply reply = ^(NSData* data) {
if (callback) {
callback((data == nil) ? FlutterMethodNotImplemented : [_codec decodeEnvelope:data]);
}
};
[_messenger sendBinaryMessage:message channelName:_name binaryReplyHandler:replyHandler];
[_messenger sendOnChannel:_name message:message binaryReply:reply];
}
- (void)setMethodCallHandler:(FlutterMethodCallHandler)handler {
if (!handler) {
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:nil];
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
return;
}
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReplyHandler reply) {
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
FlutterMethodCall* call = [_codec decodeMethodCall:message];
handler(call, ^(id result) {
if (result == FlutterMethodNotImplemented)
reply(nil);
callback(nil);
else if ([result isKindOfClass:[FlutterError class]])
reply([_codec encodeErrorEnvelope:(FlutterError*)result]);
callback([_codec encodeErrorEnvelope:(FlutterError*)result]);
else
reply([_codec encodeSuccessEnvelope:result]);
callback([_codec encodeSuccessEnvelope:result]);
});
};
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
}
@end
@@ -260,37 +258,36 @@ NSObject const* FlutterEndOfEventStream = [NSObject new];
- (void)setStreamHandler:(NSObject<FlutterStreamHandler>*)handler {
if (!handler) {
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:nil];
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
return;
}
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReplyHandler reply) {
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
FlutterMethodCall* call = [_codec decodeMethodCall:message];
if ([call.method isEqual:@"listen"]) {
FlutterEventReceiver eventReceiver = ^(id event) {
FlutterEventSink eventSink = ^(id event) {
if (event == FlutterEndOfEventStream)
[_messenger sendBinaryMessage:nil channelName:_name];
[_messenger sendOnChannel:_name message:nil];
else if ([event isKindOfClass:[FlutterError class]])
[_messenger sendBinaryMessage:[_codec encodeErrorEnvelope:(FlutterError*)event]
channelName:_name];
[_messenger sendOnChannel:_name
message:[_codec encodeErrorEnvelope:(FlutterError*)event]];
else
[_messenger sendBinaryMessage:[_codec encodeSuccessEnvelope:event] channelName:_name];
[_messenger sendOnChannel:_name message:[_codec encodeSuccessEnvelope:event]];
};
FlutterError* error =
[handler onListenWithArguments:call.arguments eventReceiver:eventReceiver];
FlutterError* error = [handler onListenWithArguments:call.arguments eventSink:eventSink];
if (error)
reply([_codec encodeErrorEnvelope:error]);
callback([_codec encodeErrorEnvelope:error]);
else
reply([_codec encodeSuccessEnvelope:nil]);
callback([_codec encodeSuccessEnvelope:nil]);
} else if ([call.method isEqual:@"cancel"]) {
FlutterError* error = [handler onCancelWithArguments:call.arguments];
if (error)
reply([_codec encodeErrorEnvelope:error]);
callback([_codec encodeErrorEnvelope:error]);
else
reply([_codec encodeSuccessEnvelope:nil]);
callback([_codec encodeSuccessEnvelope:nil]);
} else {
reply(nil);
callback(nil);
}
};
[_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
[_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:messageHandler];
}
@end

View File

@@ -20,8 +20,7 @@ typedef void (^ValidationResult)(BOOL result, NSString* message);
packages:(NSURL*)packages
flxArchive:(NSURL*)flxArchive NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFLXArchiveWithScriptSnapshot:(NSURL*)flxArchive
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFLXArchiveWithScriptSnapshot:(NSURL*)flxArchive NS_DESIGNATED_INITIALIZER;
- (void)validate:(ValidationResult)result;

View File

@@ -9,7 +9,7 @@
@interface FlutterPlatformPlugin : NSObject
-(void)handleMethodCall:(FlutterMethodCall*)call resultReceiver:(FlutterResultReceiver)resultReceiver;
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
@end

View File

@@ -40,45 +40,44 @@ using namespace shell;
@implementation FlutterPlatformPlugin
- (void)handleMethodCall:(FlutterMethodCall*)call
resultReceiver:(FlutterResultReceiver)resultReceiver {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString* method = call.method;
id args = call.arguments;
if ([method isEqualToString:@"SystemSound.play"]) {
[self playSystemSound:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"HapticFeedback.vibrate"]) {
[self vibrateHapticFeedback];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"UrlLauncher.launch"]) {
[self launchURL:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"SystemChrome.setPreferredOrientations"]) {
[self setSystemChromePreferredOrientations:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"SystemChrome.setApplicationSwitcherDescription"]) {
[self setSystemChromeApplicationSwitcherDescription:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"SystemChrome.setEnabledSystemUIOverlays"]) {
[self setSystemChromeEnabledSystemUIOverlays:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"SystemChrome.setSystemUIOverlayStyle"]) {
[self setSystemChromeSystemUIOverlayStyle:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"SystemNavigator.pop"]) {
[self popSystemNavigator];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"Clipboard.getData"]) {
resultReceiver([self getClipboardData:args]);
result([self getClipboardData:args]);
} else if ([method isEqualToString:@"Clipboard.setData"]) {
[self setClipboardData:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"PathProvider.getTemporaryDirectory"]) {
resultReceiver([self getPathProviderTemporaryDirectory]);
result([self getPathProviderTemporaryDirectory]);
} else if ([method isEqualToString:@"PathProvider.getApplicationDocumentsDirectory"]) {
resultReceiver([self getPathProviderApplicationDocumentsDirectory]);
result([self getPathProviderApplicationDocumentsDirectory]);
} else {
resultReceiver(FlutterMethodNotImplemented);
result(FlutterMethodNotImplemented);
}
}

View File

@@ -25,12 +25,10 @@ typedef NS_ENUM(NSInteger, FlutterStandardField) {
};
namespace shell {
FlutterStandardField FlutterStandardFieldForDataType(
FlutterStandardDataType type) {
FlutterStandardField FlutterStandardFieldForDataType(FlutterStandardDataType type) {
return (FlutterStandardField)(type + FlutterStandardFieldUInt8Data);
}
FlutterStandardDataType FlutterStandardDataTypeForField(
FlutterStandardField field) {
FlutterStandardDataType FlutterStandardDataTypeForField(FlutterStandardField field) {
return (FlutterStandardDataType)(field - FlutterStandardFieldUInt8Data);
}
UInt8 elementSizeForFlutterStandardDataType(FlutterStandardDataType type) {

View File

@@ -11,7 +11,7 @@
@interface FlutterTextInputPlugin : NSObject
@property(nonatomic, assign) id<FlutterTextInputDelegate> textInputDelegate;
-(void)handleMethodCall:(FlutterMethodCall*)call resultReceiver:(FlutterResultReceiver)resultReceiver;
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
@end

View File

@@ -282,7 +282,8 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
return [FlutterTextRange rangeWithNSRange:NSMakeRange(fromIndex, toIndex - fromIndex)];
}
/** Returns the range of the character sequence at the specified index in the text. */
/** Returns the range of the character sequence at the specified index in the
* text. */
- (NSRange)rangeForCharacterAtIndex:(NSUInteger)index {
if (index < self.text.length)
[self.text rangeOfComposedCharacterSequenceAtIndex:index];
@@ -397,8 +398,10 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
#pragma mark - UITextInput cursor, selection rect handling
// The following methods are required to support force-touch cursor positioning and to position the
// candidates view for multi-stage input methods (e.g., Japanese) when using a physical keyboard.
// The following methods are required to support force-touch cursor positioning
// and to position the
// candidates view for multi-stage input methods (e.g., Japanese) when using a
// physical keyboard.
- (CGRect)firstRectForRange:(UITextRange*)range {
// TODO(cbracken) Implement.
@@ -496,27 +499,26 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
[super dealloc];
}
- (void)handleMethodCall:(FlutterMethodCall*)call
resultReceiver:(FlutterResultReceiver)resultReceiver {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString* method = call.method;
id args = call.arguments;
if ([method isEqualToString:@"TextInput.show"]) {
[self showTextInput];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"TextInput.hide"]) {
[self hideTextInput];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"TextInput.setClient"]) {
[self setTextInputClient:[args[0] intValue] withConfiguration:args[1]];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"TextInput.setEditingState"]) {
[self setTextInputEditingState:args];
resultReceiver(nil);
result(nil);
} else if ([method isEqualToString:@"TextInput.clearClient"]) {
[self clearTextInputClient];
resultReceiver(nil);
result(nil);
} else {
resultReceiver(FlutterMethodNotImplemented);
result(FlutterMethodNotImplemented);
}
}

View File

@@ -69,8 +69,8 @@ class PlatformMessageResponseDarwin : public blink::PlatformMessageResponse {
fml::scoped_nsprotocol<FlutterMethodChannel*> _navigationChannel;
fml::scoped_nsprotocol<FlutterMethodChannel*> _platformChannel;
fml::scoped_nsprotocol<FlutterMethodChannel*> _textInputChannel;
fml::scoped_nsprotocol<FlutterMessageChannel*> _lifecycleChannel;
fml::scoped_nsprotocol<FlutterMessageChannel*> _systemChannel;
fml::scoped_nsprotocol<FlutterBasicMessageChannel*> _lifecycleChannel;
fml::scoped_nsprotocol<FlutterBasicMessageChannel*> _systemChannel;
BOOL _initialized;
}
@@ -141,28 +141,26 @@ class PlatformMessageResponseDarwin : public blink::PlatformMessageResponse {
binaryMessenger:self
codec:[FlutterJSONMethodCodec sharedInstance]]);
_lifecycleChannel.reset([[FlutterMessageChannel alloc]
_lifecycleChannel.reset([[FlutterBasicMessageChannel alloc]
initWithName:@"flutter/lifecycle"
binaryMessenger:self
codec:[FlutterStringCodec sharedInstance]]);
_systemChannel.reset([[FlutterMessageChannel alloc]
_systemChannel.reset([[FlutterBasicMessageChannel alloc]
initWithName:@"flutter/system"
binaryMessenger:self
codec:[FlutterJSONMessageCodec sharedInstance]]);
_platformPlugin.reset([[FlutterPlatformPlugin alloc] init]);
[_platformChannel.get()
setMethodCallHandler:^(FlutterMethodCall* call, FlutterResultReceiver resultReceiver) {
[_platformPlugin.get() handleMethodCall:call resultReceiver:resultReceiver];
}];
[_platformChannel.get() setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
[_platformPlugin.get() handleMethodCall:call result:result];
}];
_textInputPlugin.reset([[FlutterTextInputPlugin alloc] init]);
_textInputPlugin.get().textInputDelegate = self;
[_textInputChannel.get()
setMethodCallHandler:^(FlutterMethodCall* call, FlutterResultReceiver resultReceiver) {
[_textInputPlugin.get() handleMethodCall:call resultReceiver:resultReceiver];
}];
[_textInputChannel.get() setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
[_textInputPlugin.get() handleMethodCall:call result:result];
}];
[self setupNotificationCenterObservers];
@@ -561,15 +559,15 @@ constexpr CGFloat kStandardStatusBarHeight = 20.0;
});
}
#pragma mark - Application Messages
#pragma mark - FlutterBinaryMessenger
- (void)sendBinaryMessage:(NSData*)message channelName:(NSString*)channel {
[self sendBinaryMessage:message channelName:channel binaryReplyHandler:nil];
- (void)sendOnChannel:(NSString*)channel message:(NSData*)message {
[self sendOnChannel:channel message:message binaryReply:nil];
}
- (void)sendBinaryMessage:(NSData*)message
channelName:(NSString*)channel
binaryReplyHandler:(FlutterBinaryReplyHandler)callback {
- (void)sendOnChannel:(NSString*)channel
message:(NSData*)message
binaryReply:(FlutterBinaryReply)callback {
NSAssert(channel, @"The channel must not be null");
ftl::RefPtr<PlatformMessageResponseDarwin> response =
(callback == nil) ? nullptr
@@ -583,9 +581,9 @@ constexpr CGFloat kStandardStatusBarHeight = 20.0;
_platformView->DispatchPlatformMessage(platformMessage);
}
- (void)setBinaryMessageHandlerOnChannel:(NSString*)channel
binaryMessageHandler:(FlutterBinaryMessageHandler)handler {
NSAssert(channel, @"The channel name must not be null");
- (void)setMessageHandlerOnChannel:(NSString*)channel
binaryMessageHandler:(FlutterBinaryMessageHandler)handler {
NSAssert(channel, @"The channel must not be null");
_platformView->platform_message_router().SetMessageHandler(channel.UTF8String, handler);
}
@end

View File

@@ -54,8 +54,7 @@ class AccessibilityBridge final {
private:
SemanticsObject* GetOrCreateObject(int32_t id);
void VisitObjectsRecursively(SemanticsObject* object,
std::unordered_set<int>* visited_objects);
void VisitObjectsRecursively(SemanticsObject* object, std::unordered_set<int>* visited_objects);
void ReleaseObjects(const std::unordered_map<int, SemanticsObject*>& objects);
UIView* view_;

View File

@@ -20,12 +20,10 @@ class PlatformMessageRouter {
void HandlePlatformMessage(ftl::RefPtr<blink::PlatformMessage> message);
void SetMessageHandler(const std::string& channel,
FlutterBinaryMessageHandler handler);
void SetMessageHandler(const std::string& channel, FlutterBinaryMessageHandler handler);
private:
std::unordered_map<std::string, FlutterBinaryMessageHandler>
message_handlers_;
std::unordered_map<std::string, FlutterBinaryMessageHandler> message_handlers_;
FTL_DISALLOW_COPY_AND_ASSIGN(PlatformMessageRouter);
};

View File

@@ -5,8 +5,8 @@
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_VSYNC_WAITER_IOS_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_VSYNC_WAITER_IOS_H_
#include "lib/ftl/macros.h"
#include "flutter/shell/common/vsync_waiter.h"
#include "lib/ftl/macros.h"
#if __OBJC__
@class VSyncClient;