This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
/
HelloKotlin.kt
59 lines (47 loc) · 1.44 KB
/
HelloKotlin.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
package cordova.plugin
import org.apache.cordova.*
import org.json.JSONArray
import org.json.JSONException
import android.util.Log
class HelloKotlin : CordovaPlugin() {
lateinit var context: CallbackContext
@Throws(JSONException::class)
override fun execute(action: String, data: JSONArray, callbackContext: CallbackContext): Boolean {
context = callbackContext
var result = true
try {
if (action == "hello") {
val input = data.getString(0)
val output = "Kotlin says \"$input\""
callbackContext.success(output)
} else {
handleError("Invalid action")
result = false
}
} catch (e: Exception) {
handleException(e)
result = false
}
return result
}
/**
* Handles an error while executing a plugin API method.
* Calls the registered Javascript plugin error handler callback.
*
* @param errorMsg Error message to pass to the JS error handler
*/
private fun handleError(errorMsg: String) {
try {
Log.e(TAG, errorMsg)
context.error(errorMsg)
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
}
private fun handleException(exception: Exception) {
handleError(exception.toString())
}
companion object {
protected val TAG = "HelloKotlin"
}
}