master
kt 152 lines 4.79 KB
Raw
1 package com.plugin.admob.core
2
3 import android.os.Bundle
4 import com.google.ads.mediation.admob.AdMobAdapter
5 import com.google.android.gms.ads.AdRequest
6 import com.google.android.gms.ads.LoadAdError
7 import com.google.android.gms.ads.MobileAds
8 import com.google.android.gms.ads.RequestConfiguration
9 import com.google.android.gms.ads.rewarded.ServerSideVerificationOptions
10 import com.plugin.admob.InvokeArgs
11 import org.json.JSONObject
12 import java.util.Objects
13
14 interface Context {
15 fun has(name: String): Boolean
16 fun opt(name: String): Any?
17 fun optBoolean(name: String): Boolean?
18 fun optDouble(name: String): Double?
19 fun optDouble(name: String, defaultValue: Double): Double {
20 return optDouble(name) ?: return defaultValue
21 }
22
23 fun getInvokeArgs(): InvokeArgs
24
25 fun optFloat(name: String): Float? {
26 val v = optDouble(name) ?: return null
27 return v.toFloat()
28 }
29
30 fun optInt(name: String): Int?
31 fun optString(name: String): String?
32 fun optStringList(name: String): List<String?>
33 fun optObject(name: String): JSONObject?
34 fun resolve()
35 fun resolve(data: Boolean)
36 fun reject(msg: String?)
37 fun reject() {
38 reject("unknown error")
39 }
40
41 fun reject(loadAdError: LoadAdError) {
42 reject(loadAdError.message)
43 }
44
45 fun optId(): Int? {
46 return optInt("id")
47 }
48
49 fun optAd(): Ad? {
50 return Helper.getAd(optId())
51 }
52
53 fun optAdOrError(): Ad? {
54 val ad = optAd()
55 if (ad == null) {
56 this.reject("Ad not found")
57 }
58 return ad
59 }
60
61 fun optAdUnitID(): String? {
62 return optString("adUnitId")
63 }
64
65 fun optAppMuted(): Boolean? {
66 return optBoolean("appMuted")
67 }
68
69 fun optAppVolume(): Float? {
70 return optFloat("appVolume")
71 }
72
73 fun optPosition(): String? {
74 return optString("position")
75 }
76
77 fun optAdRequest(): AdRequest {
78 val builder = AdRequest.Builder()
79 if (has("contentUrl")) {
80 Objects.requireNonNull(optString("contentUrl"))?.let { builder.setContentUrl(it) }
81 }
82 val extras = Bundle()
83 if (has("npa")) {
84 extras.putString("npa", optString("npa"))
85 }
86 return builder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build()
87 }
88
89 fun optRequestConfiguration(): RequestConfiguration {
90 val builder = RequestConfiguration.Builder()
91 if (has("maxAdContentRating")) {
92 builder.setMaxAdContentRating(optString("maxAdContentRating"))
93 }
94 val tagForChildDirectedTreatment = intFromBool(
95 this, "tagForChildDirectedTreatment",
96 RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_UNSPECIFIED,
97 RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE,
98 RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE
99 )
100 if (tagForChildDirectedTreatment != null) {
101 builder.setTagForChildDirectedTreatment(tagForChildDirectedTreatment)
102 }
103 val tagForUnderAgeOfConsent = intFromBool(
104 this, "tagForUnderAgeOfConsent",
105 RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_UNSPECIFIED,
106 RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE,
107 RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE
108 )
109 if (tagForUnderAgeOfConsent != null) {
110 builder.setTagForUnderAgeOfConsent(tagForUnderAgeOfConsent)
111 }
112 if (has("testDeviceIds")) {
113 builder.setTestDeviceIds(optStringList("testDeviceIds"))
114 }
115 return builder.build()
116 }
117
118 fun optServerSideVerificationOptions(): ServerSideVerificationOptions? {
119 val param = this.getInvokeArgs();
120 val serverSideVerification = param.serverSideVerification ?: return null
121 val builder = ServerSideVerificationOptions.Builder()
122 if (serverSideVerification.customData != null) {
123 builder.setCustomData(serverSideVerification.customData!!)
124 }
125 if (serverSideVerification.userId != null) {
126 builder.setUserId(serverSideVerification.userId!!)
127 }
128 return builder.build()
129 }
130
131 fun configure(helper: Helper) {
132 val appMuted = optAppMuted()
133 if (appMuted != null) {
134 MobileAds.setAppMuted(appMuted)
135 }
136 val appVolume = optAppVolume()
137 if (appVolume != null) {
138 MobileAds.setAppVolume(appVolume)
139 }
140 MobileAds.setRequestConfiguration(optRequestConfiguration())
141 helper.configForTestLab()
142 resolve()
143 }
144
145 companion object {
146 fun intFromBool(ctx: Context, name: String, vNull: Int, vTrue: Int, vFalse: Int): Int? {
147 if (!ctx.has(name)) return null
148 val v = ctx.optBoolean(name) ?: return vNull
149 return if (v) vTrue else vFalse
150 }
151 }
152 }