what-the-bank/sources/io/flutter/plugin/common/EventChannel.java

150 lines
6.4 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package io.flutter.plugin.common;
import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/* loaded from: classes6.dex */
public final class EventChannel {
private static final String TAG = "EventChannel#";
private final MethodCodec codec;
private final BinaryMessenger messenger;
private final String name;
private final BinaryMessenger.TaskQueue taskQueue;
/* loaded from: classes6.dex */
public interface EventSink {
void endOfStream();
void error(String str, String str2, Object obj);
void success(Object obj);
}
/* loaded from: classes6.dex */
public interface StreamHandler {
void onCancel(Object obj);
void onListen(Object obj, EventSink eventSink);
}
public EventChannel(BinaryMessenger binaryMessenger, String str) {
this(binaryMessenger, str, StandardMethodCodec.INSTANCE);
}
public EventChannel(BinaryMessenger binaryMessenger, String str, MethodCodec methodCodec) {
this(binaryMessenger, str, methodCodec, null);
}
public EventChannel(BinaryMessenger binaryMessenger, String str, MethodCodec methodCodec, BinaryMessenger.TaskQueue taskQueue) {
this.messenger = binaryMessenger;
this.name = str;
this.codec = methodCodec;
this.taskQueue = taskQueue;
}
public final void setStreamHandler(StreamHandler streamHandler) {
if (this.taskQueue != null) {
this.messenger.setMessageHandler(this.name, streamHandler != null ? new IncomingStreamRequestHandler(this, streamHandler) : null, this.taskQueue);
} else {
this.messenger.setMessageHandler(this.name, streamHandler != null ? new IncomingStreamRequestHandler(this, streamHandler) : null);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public final class IncomingStreamRequestHandler implements BinaryMessenger.BinaryMessageHandler {
private final AtomicReference<EventSink> activeSink = new AtomicReference<>(null);
private final StreamHandler handler;
final EventChannel this$0;
IncomingStreamRequestHandler(EventChannel eventChannel, StreamHandler streamHandler) {
this.this$0 = eventChannel;
this.handler = streamHandler;
}
@Override // io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler
public final void onMessage(ByteBuffer byteBuffer, BinaryMessenger.BinaryReply binaryReply) {
MethodCall decodeMethodCall = this.this$0.codec.decodeMethodCall(byteBuffer);
if (decodeMethodCall.method.equals("listen")) {
onListen(decodeMethodCall.arguments, binaryReply);
} else if (decodeMethodCall.method.equals("cancel")) {
onCancel(decodeMethodCall.arguments, binaryReply);
} else {
binaryReply.reply(null);
}
}
private void onListen(Object obj, BinaryMessenger.BinaryReply binaryReply) {
EventSinkImplementation eventSinkImplementation = new EventSinkImplementation();
if (this.activeSink.getAndSet(eventSinkImplementation) != null) {
try {
this.handler.onCancel(null);
} catch (RuntimeException e) {
Log.e(EventChannel.TAG + this.this$0.name, "Failed to close existing event stream", e);
}
}
try {
this.handler.onListen(obj, eventSinkImplementation);
binaryReply.reply(this.this$0.codec.encodeSuccessEnvelope(null));
} catch (RuntimeException e2) {
this.activeSink.set(null);
Log.e(EventChannel.TAG + this.this$0.name, "Failed to open event stream", e2);
binaryReply.reply(this.this$0.codec.encodeErrorEnvelope("error", e2.getMessage(), null));
}
}
private void onCancel(Object obj, BinaryMessenger.BinaryReply binaryReply) {
if (this.activeSink.getAndSet(null) == null) {
binaryReply.reply(this.this$0.codec.encodeErrorEnvelope("error", "No active stream to cancel", null));
return;
}
try {
this.handler.onCancel(obj);
binaryReply.reply(this.this$0.codec.encodeSuccessEnvelope(null));
} catch (RuntimeException e) {
Log.e(EventChannel.TAG + this.this$0.name, "Failed to close event stream", e);
binaryReply.reply(this.this$0.codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public final class EventSinkImplementation implements EventSink {
final AtomicBoolean hasEnded;
final IncomingStreamRequestHandler this$1;
private EventSinkImplementation(IncomingStreamRequestHandler incomingStreamRequestHandler) {
this.this$1 = incomingStreamRequestHandler;
this.hasEnded = new AtomicBoolean(false);
}
@Override // io.flutter.plugin.common.EventChannel.EventSink
public final void success(Object obj) {
if (this.hasEnded.get() || this.this$1.activeSink.get() != this) {
return;
}
this.this$1.this$0.messenger.send(this.this$1.this$0.name, this.this$1.this$0.codec.encodeSuccessEnvelope(obj));
}
@Override // io.flutter.plugin.common.EventChannel.EventSink
public final void error(String str, String str2, Object obj) {
if (this.hasEnded.get() || this.this$1.activeSink.get() != this) {
return;
}
this.this$1.this$0.messenger.send(this.this$1.this$0.name, this.this$1.this$0.codec.encodeErrorEnvelope(str, str2, obj));
}
@Override // io.flutter.plugin.common.EventChannel.EventSink
public final void endOfStream() {
if (this.hasEnded.getAndSet(true) || this.this$1.activeSink.get() != this) {
return;
}
this.this$1.this$0.messenger.send(this.this$1.this$0.name, null);
}
}
}
}