| 1 | package com.plugin.admob |
| 2 | |
| 3 | import android.app.Activity |
| 4 | import android.content.ContentValues.TAG |
| 5 | import android.util.Log |
| 6 | import android.webkit.WebView |
| 7 | import app.tauri.annotation.Command |
| 8 | import app.tauri.annotation.InvokeArg |
| 9 | import app.tauri.annotation.TauriPlugin |
| 10 | import app.tauri.plugin.JSObject |
| 11 | import app.tauri.plugin.Plugin |
| 12 | import app.tauri.plugin.Invoke |
| 13 | import com.google.android.gms.ads.MobileAds |
| 14 | import com.google.android.gms.ads.initialization.InitializationStatus |
| 15 | import com.google.android.ump.ConsentInformation |
| 16 | import com.google.android.ump.ConsentRequestParameters |
| 17 | import com.google.android.ump.UserMessagingPlatform |
| 18 | import com.plugin.admob.ads.Banner |
| 19 | import com.plugin.admob.ads.Interstitial |
| 20 | import com.plugin.admob.ads.Rewarded |
| 21 | import com.plugin.admob.ads.RewardedInterstitial |
| 22 | import com.plugin.admob.core.GenericAd |
| 23 | import com.plugin.admob.core.Helper |
| 24 | import org.json.JSONException |
| 25 | import org.json.JSONObject |
| 26 | |
| 27 | @InvokeArg |
| 28 | class InvokeArgs { |
| 29 | var cls: String? = null |
| 30 | val id: Int? = null |
| 31 | var adUnitId: String? = null |
| 32 | var appMuted: Boolean? = null |
| 33 | var appVolume: Float? = null |
| 34 | var position: String? = null |
| 35 | var contentUrl: String? = null |
| 36 | var npa: String? = null |
| 37 | var maxAdContentRating: String? = null |
| 38 | val tagForChildDirectedTreatment: Boolean? = null |
| 39 | val tagForUnderAgeOfConsent: Boolean? = null |
| 40 | val testDeviceIds: List<String?>? = null |
| 41 | val serverSideVerification: ServerSideVerification? = null |
| 42 | val customData: String? = null |
| 43 | val userId: String? = null |
| 44 | } |
| 45 | |
| 46 | @InvokeArg |
| 47 | class ServerSideVerification { |
| 48 | var userId: String? = null |
| 49 | var customData: String? = null |
| 50 | } |
| 51 | |
| 52 | @TauriPlugin |
| 53 | class AdmobPlugin(var activity: Activity) : Plugin(activity) { |
| 54 | private lateinit var consentInformation: ConsentInformation |
| 55 | val isPrivacyOptionsRequired: Boolean |
| 56 | get() = |
| 57 | consentInformation.privacyOptionsRequirementStatus == |
| 58 | ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED |
| 59 | private var helper: Helper? = null |
| 60 | var webView: WebView? = null |
| 61 | |
| 62 | override fun load(webView: WebView) { |
| 63 | super.load(webView) |
| 64 | this.webView = webView |
| 65 | helper = Helper(this) |
| 66 | ExecuteContext.Companion.plugin = this |
| 67 | |
| 68 | MobileAds.initialize(activity.baseContext) { status: InitializationStatus? -> |
| 69 | helper!!.configForTestLab() |
| 70 | } |
| 71 | |
| 72 | val params = ConsentRequestParameters |
| 73 | .Builder() |
| 74 | .build() |
| 75 | |
| 76 | consentInformation = UserMessagingPlatform.getConsentInformation(this.activity) |
| 77 | consentInformation.requestConsentInfoUpdate( |
| 78 | this.activity, |
| 79 | params, |
| 80 | { |
| 81 | UserMessagingPlatform.loadAndShowConsentFormIfRequired( |
| 82 | this.activity |
| 83 | ) { loadAndShowError -> |
| 84 | if (loadAndShowError != null) { |
| 85 | Log.w(TAG, "${loadAndShowError.errorCode}: ${loadAndShowError.message}") |
| 86 | return@loadAndShowConsentFormIfRequired |
| 87 | } |
| 88 | } |
| 89 | }, |
| 90 | { |
| 91 | requestConsentError -> |
| 92 | Log.w(TAG, "${requestConsentError.errorCode}: ${requestConsentError.message}") |
| 93 | } |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | @Command |
| 98 | fun isPrivacyOptionsRequired(call: Invoke) { |
| 99 | try { |
| 100 | call.resolve(JSObject("{\"isPrivacyOptionsRequired\": ${isPrivacyOptionsRequired}}")) |
| 101 | } catch (ex: JSONException) { |
| 102 | call.reject(ex.toString()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Command |
| 107 | fun showPrivacyOptionsForm(invoke: Invoke) { |
| 108 | UserMessagingPlatform.showPrivacyOptionsForm(this.activity) { formError -> |
| 109 | if (formError != null) { |
| 110 | Log.w(TAG, "${formError.errorCode}: ${formError.message}") |
| 111 | invoke.reject(formError.message) |
| 112 | return@showPrivacyOptionsForm |
| 113 | } |
| 114 | invoke.resolve() |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Command |
| 119 | fun trackingAuthorizationStatus(call: Invoke) { |
| 120 | try { |
| 121 | call.resolve(JSObject("{\"status\": false}")) |
| 122 | } catch (ex: JSONException) { |
| 123 | call.reject(ex.toString()) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Command |
| 128 | fun requestTrackingAuthorization(call: Invoke) { |
| 129 | try { |
| 130 | call.resolve(JSObject("{\"status\": false}")) |
| 131 | } catch (ex: JSONException) { |
| 132 | call.reject(ex.toString()) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Command |
| 137 | fun configure(call: Invoke) { |
| 138 | val ctx = ExecuteContext(call) |
| 139 | ctx.configure(helper!!) |
| 140 | } |
| 141 | |
| 142 | @Command |
| 143 | fun configRequest(call: Invoke) { |
| 144 | val ctx = ExecuteContext(call) |
| 145 | MobileAds.setRequestConfiguration(ctx.optRequestConfiguration()) |
| 146 | helper!!.configForTestLab() |
| 147 | ctx.resolve() |
| 148 | } |
| 149 | |
| 150 | @Command |
| 151 | fun adCreate(invoke: Invoke) { |
| 152 | val ctx = ExecuteContext(invoke) |
| 153 | android.os.Handler(activity.mainLooper).post { |
| 154 | val adClass = ctx.optString("cls") |
| 155 | if (adClass == null) { |
| 156 | ctx.reject("ad cls is missing") |
| 157 | } else { |
| 158 | when (adClass) { |
| 159 | "BannerAd" -> Banner(ctx) |
| 160 | "InterstitialAd" -> Interstitial(ctx) |
| 161 | "RewardedAd" -> Rewarded(ctx) |
| 162 | "RewardedInterstitialAd" -> RewardedInterstitial(ctx) |
| 163 | else -> ctx.reject("ad cls is not supported: $adClass") |
| 164 | } |
| 165 | ctx.resolve() |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Command |
| 171 | fun adIsLoaded(call: Invoke) { |
| 172 | val ctx = ExecuteContext(call) |
| 173 | android.os.Handler(activity.mainLooper).post { |
| 174 | val ad = ctx.optAdOrError() as GenericAd? |
| 175 | if (ad != null) { |
| 176 | ctx.resolve(ad.isLoaded) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Command |
| 182 | fun adLoad(call: Invoke) { |
| 183 | val ctx = ExecuteContext(call) |
| 184 | android.os.Handler(activity.mainLooper).post { |
| 185 | val ad = ctx.optAdOrError() as GenericAd? |
| 186 | ad?.load(ctx) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | @Command |
| 191 | fun adShow(call: Invoke) { |
| 192 | val ctx = ExecuteContext(call) |
| 193 | |
| 194 | android.os.Handler(activity.mainLooper).post { |
| 195 | val ad = ctx.optAdOrError() as GenericAd? |
| 196 | if (ad != null) { |
| 197 | if (ad.isLoaded) { |
| 198 | ad.show(ctx) |
| 199 | } else { |
| 200 | ctx.reject("ad is not loaded") |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Command |
| 207 | fun adHide(call: Invoke) { |
| 208 | val ctx = ExecuteContext(call) |
| 209 | android.os.Handler(activity.mainLooper).post { |
| 210 | val ad = ctx.optAdOrError() as GenericAd? |
| 211 | ad?.hide(ctx) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | fun emit(eventName: String?, data: Map<String?, Any?>?) { |
| 216 | try { |
| 217 | if (eventName != null) { |
| 218 | trigger(eventName, JSObject.fromJSONObject(JSONObject(data))) |
| 219 | } |
| 220 | } catch (e: JSONException) { |
| 221 | e.printStackTrace() |
| 222 | } |
| 223 | } |
| 224 | } |