5package io.flutter.plugin.common;
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.UiThread;
10import io.flutter.BuildConfig;
12import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
13import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
14import java.nio.ByteBuffer;
15import java.util.Arrays;
32 private static final String TAG =
"BasicMessageChannel#";
36 @NonNull
private final String name;
38 @Nullable
private final BinaryMessenger.TaskQueue taskQueue;
50 this(messenger, name, codec,
null);
70 if (messenger ==
null) {
71 Log.
e(
TAG,
"Parameter messenger must not be null.");
74 Log.
e(
TAG,
"Parameter name must not be null.");
77 Log.
e(
TAG,
"Parameter codec must not be null.");
80 this.messenger = messenger;
83 this.taskQueue = taskQueue;
127 if (taskQueue !=
null) {
128 messenger.setMessageHandler(
129 name, handler ==
null ?
null :
new IncomingMessageHandler(handler), taskQueue);
131 messenger.setMessageHandler(
132 name, handler ==
null ?
null :
new IncomingMessageHandler(handler));
154 private static ByteBuffer packetFromEncodedMessage(ByteBuffer
message) {
157 final byte[] bytes =
new byte[
message.remaining()];
162 ByteBuffer packet = ByteBuffer.allocateDirect(bytes.length);
174 @NonNull
BinaryMessenger messenger, @NonNull String channel,
int newSize) {
176 Object[] arguments = {channel, newSize};
179 ByteBuffer packet = packetFromEncodedMessage(
message);
189 @NonNull
BinaryMessenger messenger, @NonNull String channel,
boolean warns) {
191 Object[] arguments = {channel, !warns};
194 ByteBuffer packet = packetFromEncodedMessage(
message);
234 private final class IncomingReplyHandler
implements BinaryReply {
242 public void reply(@Nullable ByteBuffer reply) {
244 callback.reply(codec.decodeMessage(reply));
245 }
catch (RuntimeException
e) {
246 Log.
e(TAG + name,
"Failed to handle message reply",
e);
252 private final MessageHandler<T> handler;
254 private IncomingMessageHandler(@NonNull MessageHandler<T> handler) {
255 this.handler = handler;
265 public void reply(T reply) {
266 callback.reply(codec.encodeMessage(reply));
269 }
catch (RuntimeException
e) {
270 Log.e(TAG + name,
"Failed to handle message",
e);
static final boolean DEBUG
static void e(@NonNull String tag, @NonNull String message)
static final String CHANNEL_BUFFERS_CHANNEL
void setMessageHandler(@Nullable final MessageHandler< T > handler)
void send(@Nullable T message, @Nullable final Reply< T > callback)
void send(@Nullable T message)
static void setWarnsOnChannelOverflow( @NonNull BinaryMessenger messenger, @NonNull String channel, boolean warns)
void resizeChannelBuffer(int newSize)
BasicMessageChannel( @NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MessageCodec< T > codec)
void setWarnsOnChannelOverflow(boolean warns)
static void resizeChannelBuffer( @NonNull BinaryMessenger messenger, @NonNull String channel, int newSize)
BasicMessageChannel( @NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MessageCodec< T > codec, BinaryMessenger.TaskQueue taskQueue)
ByteBuffer encodeMethodCall(@NonNull MethodCall methodCall)
static final StandardMethodCodec INSTANCE
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
void onMessage(@Nullable T message, @NonNull Reply< T > reply)
void reply(@Nullable T reply)
void Log(const char *format,...) SK_PRINTF_LIKE(1
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply