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