-
Notifications
You must be signed in to change notification settings - Fork 4
/
PluginScaffoldPlugin.kt
327 lines (278 loc) · 9.49 KB
/
PluginScaffoldPlugin.kt
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.pycampers.plugin_scaffold
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import android.os.AsyncTask
import android.os.Handler
import android.util.Log
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel.EventSink
import java.io.PrintWriter
import java.io.StringWriter
import java.lang.reflect.Method
const val TAG = "PluginScaffold"
const val ON_LISTEN = "OnListen"
const val ON_CANCEL = "OnCancel"
const val ON_SUCCESS = "onSuccess"
const val ON_ERROR = "onError"
const val END_OF_STREAM = "endOfStream"
private val methodSignature = listOf(MethodCall::class.java, Result::class.java)
private val onListenSignature =
listOf(Int::class.java, Object::class.java, EventSink::class.java)
typealias OnError = (errorCode: String, errorMessage: String?, errorDetails: Any?) -> Unit
typealias OnSuccess = (result: Any?) -> Unit
typealias AnyFn = () -> Any?
typealias UnitFn = () -> Unit
typealias MethodMap = MutableMap<String, Method>
/**
* Create a plugin with the provided [channelName].
*
* The methods of [pluginObj] having parameters - ([MethodCall], [Result]),
* are automatically exposed through the returned [MethodChannel]:
*
* The [messenger] can be passed in various ways.
* - If you're inside a subclass of [FlutterActivity], pass [FlutterActivity.getFlutterView] (generally regular flutter apps)
* - If you have access to a [Registrar] object, pass [Registrar.messenger] (generally flutter plugin projects)
*
* If [runOnMainThread] is set to `true`,
* the methods in [pluginObj] will be invoked from the main thread (using [Handler.post]).
* Otherwise, they will be invoked from an [AsyncTask].
*/
fun createPluginScaffold(
messenger: BinaryMessenger,
channelName: String,
pluginObj: Any = Any(),
runOnMainThread: Boolean = false
): MethodChannel {
val methods = buildMethodMap(pluginObj)
val (onListenMethods, onCancelMethods) = buildStreamMethodMap(pluginObj)
val channel = MethodChannel(messenger, channelName)
val wrapper = createMethodWrapper(runOnMainThread)
channel.setMethodCallHandler { call, result ->
val name = call.method
val args = call.arguments
val mainResult = MainThreadResult(result)
//
// Try to find the method in [methods], [onListenMethods] and [onCancelMethods],
// and invoke it using [wrapFunCall].
//
// If not found, invoke [Result.notImplemented]
//
methods[name]?.let {
Log.d(TAG, "invoke { channel: $channelName, method: $name(), args: $args }")
wrapper(mainResult) {
it.invoke(pluginObj, call, mainResult)
}
return@setMethodCallHandler
}
onListenMethods[name]?.let {
val streamName = getStreamName(name)!!
val (hashCode: Any?, streamArgs: Any?) = args as List<*>
val prefix = "$streamName/$hashCode"
val sink = MainThreadEventSink(channel, prefix)
Log.d(
TAG,
"activate stream { channel: $channelName, stream: $streamName, hashCode: $hashCode, args: $streamArgs }"
)
wrapper(mainResult) {
it.invoke(pluginObj, hashCode, streamArgs, sink)
}
return@setMethodCallHandler
}
onCancelMethods[name]?.let {
val streamName = getStreamName(name)!!
val (hashCode: Any?, streamArgs: Any?) = args as List<*>
Log.d(
TAG,
"de-activate stream { channel: $channelName, stream: $streamName, hashCode: $hashCode, args: $streamArgs }"
)
wrapper(mainResult) {
it.invoke(pluginObj, hashCode, streamArgs)
}
return@setMethodCallHandler
}
mainResult.notImplemented()
}
return channel
}
fun createMethodWrapper(runOnMainThread: Boolean): (Result, UnitFn) -> Unit {
if (runOnMainThread) {
return { result, fn ->
handler.post {
catchErrors(result, fn)
}
}
} else {
return { result, fn ->
DoAsync {
catchErrors(result, fn)
}
}
}
}
/**
* Try to send the value returned by [fn] using [onSuccess].
*
* It is advisable to wrap any native code inside [fn],
* because this will automatically catch and send to dart exceptions using
* using [sendThrowable] and [onError] if required.
*/
fun trySend(onSuccess: OnSuccess, onError: OnError, fn: AnyFn? = null) {
val value: Any?
try {
value = fn?.invoke()
onSuccess(if (value is Unit) null else value)
} catch (e: Throwable) {
sendThrowable(onError, e)
}
}
fun trySend(result: Result, fn: AnyFn? = null) {
trySend(result::success, result::error, fn)
}
fun trySend(events: EventSink, fn: AnyFn? = null) {
trySend(events::success, events::error, fn)
}
/**
* Run [fn].
* Automatically send exceptions using error using [sendThrowable] if required.
*
* This differs from [trySend],
* in that it won't invoke [Result.success] using the return value of [fn].
*/
fun catchErrors(onError: OnError, fn: UnitFn) {
try {
fn()
} catch (e: Throwable) {
sendThrowable(onError, e)
}
}
fun catchErrors(result: Result, fn: UnitFn) {
catchErrors(result::error, fn)
}
fun catchErrors(events: EventSink, fn: UnitFn) {
catchErrors(events::error, fn)
}
/**
* Serialize the [throwable] and send it using [onError].
*/
fun sendThrowable(onError: OnError, throwable: Throwable) {
val e = throwable.cause ?: throwable
onError(
e.javaClass.canonicalName ?: "null",
e.message,
serializeStackTrace(e)
)
}
fun sendThrowable(result: Result, throwable: Throwable) {
sendThrowable(result::error, throwable)
}
fun sendThrowable(events: EventSink, throwable: Throwable) {
sendThrowable(events::error, throwable)
}
/**
* Serialize the stacktrace contained in [throwable] to a [String].
*/
fun serializeStackTrace(throwable: Throwable): String {
val sw = StringWriter()
val pw = PrintWriter(sw)
throwable.printStackTrace(pw)
return sw.toString()
}
fun buildMethodMap(pluginObj: Any): MethodMap {
val map: MethodMap = mutableMapOf()
for (method in pluginObj::class.java.methods) {
val paramList = method.parameterTypes.toList()
if (paramList != methodSignature) continue
map[method.name] = method
}
return map
}
fun buildStreamMethodMap(pluginObj: Any): Pair<MethodMap, MethodMap> {
val onListenMethods: MethodMap = mutableMapOf()
val onCancelMethods: MethodMap = mutableMapOf()
val cls = pluginObj::class.java
for (listenMethod in cls.methods) {
val paramList = listenMethod.parameterTypes.toList()
if (paramList != onListenSignature) continue
val onListenName = listenMethod.name
val streamName = getStreamName(onListenName) ?: continue
val onCancelName = streamName + ON_CANCEL
val cancelMethod: Method
try {
cancelMethod = cls.getMethod(onCancelName, Int::class.java, Object::class.java)
} catch (e: NoSuchMethodException) {
Log.w(
TAG,
"Found \"$onListenName()\" in \"$cls\", but accompanying method \"$onCancelName()\" was not found!"
)
continue
}
onListenMethods[onListenName] = listenMethod
onCancelMethods[onCancelName] = cancelMethod
}
return Pair(onListenMethods, onCancelMethods)
}
fun getStreamName(methodName: String): String? {
val name =
when {
methodName.endsWith(ON_LISTEN) -> methodName.substring(
0,
methodName.length - ON_LISTEN.length
)
methodName.endsWith(ON_CANCEL) -> methodName.substring(
0,
methodName.length - ON_CANCEL.length
)
else -> null
}
if (name != null && name.isNotEmpty()) {
return name
}
return null
}
class DoAsync(val fn: () -> Unit) : AsyncTask<Void, Void, Void>() {
init {
execute()
}
override fun doInBackground(vararg params: Void?): Void? {
fn()
return null
}
}
/** PluginScaffoldPlugin */
public class PluginScaffoldPlugin: FlutterPlugin, MethodCallHandler {
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "plugin_scaffold")
channel.setMethodCallHandler(PluginScaffoldPlugin());
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "plugin_scaffold")
channel.setMethodCallHandler(PluginScaffoldPlugin())
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}
}