-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
ExecuteContext.kt
68 lines (54 loc) · 1.56 KB
/
ExecuteContext.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
package admob.plus.cordova
import admob.plus.cordova.ads.AdBase
import android.app.Activity
import org.apache.cordova.CallbackContext
import org.apache.cordova.PluginResult
import org.json.JSONArray
import org.json.JSONObject
import java.util.Collections.synchronizedMap
val ads = synchronizedMap(mutableMapOf<String, AdBase>())
data class ExecuteContext(
val action: String,
val args: JSONArray,
val callbackContext: CallbackContext,
val plugin: AdMob,
) {
val opts: JSONObject by lazy { args.optJSONObject(0) }
val activity: Activity get() = plugin.cordova.activity
fun optBoolean(name: String): Boolean? {
return if (opts.has(name)) opts.optBoolean(name) else null
}
fun optString(name: String): String? {
return if (opts.has(name)) opts.optString(name) else null
}
fun resolve() {
callbackContext.success()
}
fun resolve(data: Boolean) {
val result = PluginResult(PluginResult.Status.OK, data)
sendResult(result)
}
fun resolve(data: Map<String, Any>) {
callbackContext.success(JSONObject(data))
}
fun reject(msg: String?) {
callbackContext.error(msg)
}
fun sendResult(result: PluginResult?) {
callbackContext.sendPluginResult(result)
}
fun optId(): String? {
return optString("id")
}
private fun optAd(): AdBase? {
return optId()?.let {
ads[it]
}
}
fun optAdOrReject(): AdBase? {
return optAd() ?: let {
reject("Ad not found")
null
}
}
}