Add @UiThread to MethodChannel and related classes/calls (#32642). (flutter/engine#8947)

This commit is contained in:
Matt Carroll
2019-05-13 13:26:31 -07:00
committed by GitHub
parent d405522a62
commit 965d20ae44
9 changed files with 41 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ package io.flutter.embedding.engine.dart;
import android.content.res.AssetManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.Log;
import java.nio.ByteBuffer;
@@ -145,6 +146,7 @@ public class DartExecutor implements BinaryMessenger {
* @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes
*/
@Override
@UiThread
public void send(@NonNull String channel, @Nullable ByteBuffer message) {
messenger.send(channel, message, null);
}
@@ -159,6 +161,7 @@ public class DartExecutor implements BinaryMessenger {
* @param callback a callback invoked when the Dart application responds to the message
*/
@Override
@UiThread
public void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) {
messenger.send(channel, message, callback);
}
@@ -172,6 +175,7 @@ public class DartExecutor implements BinaryMessenger {
* @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null.
*/
@Override
@UiThread
public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) {
messenger.setMessageHandler(channel, handler);
}

View File

@@ -6,6 +6,7 @@ package io.flutter.embedding.engine.dart;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.Log;
import java.nio.ByteBuffer;
@@ -53,6 +54,7 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
}
@Override
@UiThread
public void send(@NonNull String channel, @NonNull ByteBuffer message) {
send(channel, message, null);
}

View File

@@ -4,6 +4,7 @@
package io.flutter.plugin.common;
import android.support.annotation.UiThread;
import android.util.Log;
import java.nio.ByteBuffer;
@@ -73,6 +74,7 @@ public final class BasicMessageChannel<T> {
* @param message the message, possibly null.
* @param callback a {@link Reply} callback, possibly null.
*/
@UiThread
public void send(T message, final Reply<T> callback) {
messenger.send(name, codec.encodeMessage(message),
callback == null ? null : new IncomingReplyHandler(callback));
@@ -89,6 +91,7 @@ public final class BasicMessageChannel<T> {
*
* @param handler a {@link MessageHandler}, or null to deregister.
*/
@UiThread
public void setMessageHandler(final MessageHandler<T> handler) {
messenger.setMessageHandler(name,
handler == null ? null : new IncomingMessageHandler(handler));

View File

@@ -4,6 +4,8 @@
package io.flutter.plugin.common;
import android.support.annotation.UiThread;
import java.nio.ByteBuffer;
/**
@@ -29,6 +31,7 @@ public interface BinaryMessenger {
* @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes
* between position zero and current position, or null.
*/
@UiThread
void send(String channel, ByteBuffer message);
/**
@@ -42,6 +45,7 @@ public interface BinaryMessenger {
* @param callback a {@link BinaryReply} callback invoked when the Flutter application responds to the
* message, possibly null.
*/
@UiThread
void send(String channel, ByteBuffer message, BinaryReply callback);
/**
@@ -57,6 +61,7 @@ public interface BinaryMessenger {
* @param channel the name {@link String} of the channel.
* @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null.
*/
@UiThread
void setMessageHandler(String channel, BinaryMessageHandler handler);
/**
@@ -76,6 +81,7 @@ public interface BinaryMessenger {
* @param message the message {@link ByteBuffer} payload, possibly null.
* @param reply A {@link BinaryReply} used for submitting a reply back to Flutter.
*/
@UiThread
void onMessage(ByteBuffer message, BinaryReply reply);
}
@@ -92,6 +98,7 @@ public interface BinaryMessenger {
* outgoing replies must place the reply bytes between position zero and current position.
* Reply receivers can read from the buffer directly.
*/
@UiThread
void reply(ByteBuffer reply);
}
}

View File

@@ -4,6 +4,7 @@
package io.flutter.plugin.common;
import android.support.annotation.UiThread;
import android.util.Log;
import io.flutter.BuildConfig;
@@ -83,6 +84,7 @@ public final class EventChannel {
*
* @param handler a {@link StreamHandler}, or null to deregister.
*/
@UiThread
public void setStreamHandler(final StreamHandler handler) {
messenger.setMessageHandler(name, handler == null ? null : new IncomingStreamRequestHandler(handler));
}
@@ -216,6 +218,7 @@ public final class EventChannel {
final AtomicBoolean hasEnded = new AtomicBoolean(false);
@Override
@UiThread
public void success(Object event) {
if (hasEnded.get() || activeSink.get() != this) {
return;
@@ -224,6 +227,7 @@ public final class EventChannel {
}
@Override
@UiThread
public void error(String errorCode, String errorMessage, Object errorDetails) {
if (hasEnded.get() || activeSink.get() != this) {
return;
@@ -234,6 +238,7 @@ public final class EventChannel {
}
@Override
@UiThread
public void endOfStream() {
if (hasEnded.getAndSet(true) || activeSink.get() != this) {
return;

View File

@@ -6,6 +6,7 @@ package io.flutter.plugin.common;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.Log;
import io.flutter.BuildConfig;
@@ -78,6 +79,7 @@ public final class MethodChannel {
* @param method the name String of the method.
* @param arguments the arguments for the invocation, possibly null.
*/
@UiThread
public void invokeMethod(@NonNull String method, @Nullable Object arguments) {
invokeMethod(method, arguments, null);
}
@@ -91,6 +93,7 @@ public final class MethodChannel {
* @param arguments the arguments for the invocation, possibly null.
* @param callback a {@link Result} callback for the invocation result, or null.
*/
@UiThread
public void invokeMethod(String method, @Nullable Object arguments, Result callback) {
messenger.send(name, codec.encodeMethodCall(new MethodCall(method, arguments)),
callback == null ? null : new IncomingResultHandler(callback));
@@ -110,6 +113,7 @@ public final class MethodChannel {
*
* @param handler a {@link MethodCallHandler}, or null to deregister.
*/
@UiThread
public void setMethodCallHandler(final @Nullable MethodCallHandler handler) {
messenger.setMessageHandler(name,
handler == null ? null : new IncomingMethodCallHandler(handler));
@@ -137,6 +141,7 @@ public final class MethodChannel {
* @param call A {@link MethodCall}.
* @param result A {@link Result} used for submitting the result of the call.
*/
@UiThread
void onMethodCall(MethodCall call, Result result);
}
@@ -156,6 +161,7 @@ public final class MethodChannel {
*
* @param result The result, possibly null.
*/
@UiThread
void success(@Nullable Object result);
/**
@@ -165,11 +171,13 @@ public final class MethodChannel {
* @param errorMessage A human-readable error message String, possibly null.
* @param errorDetails Error details, possibly null
*/
@UiThread
void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails);
/**
* Handles a call to an unimplemented method.
*/
@UiThread
void notImplemented();
}
@@ -181,6 +189,7 @@ public final class MethodChannel {
}
@Override
@UiThread
public void reply(ByteBuffer reply) {
try {
if (reply == null) {
@@ -206,6 +215,7 @@ public final class MethodChannel {
}
@Override
@UiThread
public void onMessage(ByteBuffer message, final BinaryReply reply) {
final MethodCall call = codec.decodeMethodCall(message);
try {

View File

@@ -10,6 +10,7 @@ import static android.view.MotionEvent.PointerProperties;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.UiThread;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
@@ -92,6 +93,7 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler,
* After calling this the platform views controller will no longer listen to it's previous messenger, and will
* not maintain references to the texture registry, context, and messenger passed to the previous attach call.
*/
@UiThread
public void detach() {
mMessenger.setMessageHandler(CHANNEL_NAME, null);
mMessenger = null;

View File

@@ -7,6 +7,7 @@ package io.flutter.view;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import android.util.Log;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.FlutterJNI;
@@ -138,11 +139,13 @@ public class FlutterNativeView implements BinaryMessenger {
}
@Override
@UiThread
public void send(String channel, ByteBuffer message) {
dartExecutor.send(channel, message);
}
@Override
@UiThread
public void send(String channel, ByteBuffer message, BinaryReply callback) {
if (!isAttached()) {
Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
@@ -153,6 +156,7 @@ public class FlutterNativeView implements BinaryMessenger {
}
@Override
@UiThread
public void setMessageHandler(String channel, BinaryMessageHandler handler) {
dartExecutor.setMessageHandler(channel, handler);
}

View File

@@ -17,6 +17,7 @@ import android.os.Build;
import android.os.Handler;
import android.os.LocaleList;
import android.support.annotation.RequiresApi;
import android.support.annotation.UiThread;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
@@ -725,11 +726,13 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture
}
@Override
@UiThread
public void send(String channel, ByteBuffer message) {
send(channel, message, null);
}
@Override
@UiThread
public void send(String channel, ByteBuffer message, BinaryReply callback) {
if (!isAttached()) {
Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
@@ -739,6 +742,7 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture
}
@Override
@UiThread
public void setMessageHandler(String channel, BinaryMessageHandler handler) {
mNativeView.setMessageHandler(channel, handler);
}