-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathFlutterMessengerResponder.java
More file actions
74 lines (67 loc) · 2.47 KB
/
FlutterMessengerResponder.java
File metadata and controls
74 lines (67 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.onesignal.flutter;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import java.util.HashMap;
abstract class FlutterMessengerResponder {
Context context;
protected MethodChannel channel;
BinaryMessenger messenger;
/**
* MethodChannel class is home to success() method used by Result class
* It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
* This will communicate success back to Dart
*/
void replySuccess(final MethodChannel.Result reply, final Object response) {
runOnMainThread(new Runnable() {
@Override
public void run() {
reply.success(response);
}
});
}
/**
* MethodChannel class is home to error() method used by Result class
* It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
* This will communicate error back to Dart
*/
void replyError(final MethodChannel.Result reply, final String tag, final String message, final Object response) {
runOnMainThread(new Runnable() {
@Override
public void run() {
reply.error(tag, message, response);
}
});
}
/**
* MethodChannel class is home to notImplemented() method used by Result class
* It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
* This will communicate not implemented back to Dart
*/
void replyNotImplemented(final MethodChannel.Result reply) {
runOnMainThread(new Runnable() {
@Override
public void run() {
reply.notImplemented();
}
});
}
private void runOnMainThread(final Runnable runnable) {
if (Looper.getMainLooper().getThread() == Thread.currentThread()) runnable.run();
else {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(runnable);
}
}
void invokeMethodOnUiThread(final String methodName, final HashMap map) {
// final MethodChannel channel = this.channel;
runOnMainThread(new Runnable() {
@Override
public void run() {
channel.invokeMethod(methodName, map);
}
});
}
}