master
kt 73 lines 2.46 KB
Raw
1 package com.plugin.admob.ads
2
3 import com.plugin.admob.ExecuteContext
4 import com.plugin.admob.Generated
5 import com.plugin.admob.core.Context
6 import com.plugin.admob.core.GenericAd
7 import com.google.android.gms.ads.AdError
8 import com.google.android.gms.ads.FullScreenContentCallback
9 import com.google.android.gms.ads.LoadAdError
10 import com.google.android.gms.ads.interstitial.InterstitialAd
11 import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
12
13 class Interstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
14 private var mAd: InterstitialAd? = null
15 override fun destroy() {
16 clear()
17 super.destroy()
18 }
19
20 override fun load(ctx: Context?) {
21 clear()
22 InterstitialAd.load(
23 activity,
24 adUnitId,
25 ctx!!.optAdRequest(),
26 object : InterstitialAdLoadCallback() {
27 override fun onAdLoaded(interstitialAd: InterstitialAd) {
28 mAd = interstitialAd
29 mAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
30 override fun onAdDismissedFullScreenContent() {
31 clear()
32 emit(Generated.Events.INTERSTITIAL_DISMISS)
33 }
34
35 override fun onAdFailedToShowFullScreenContent(adError: AdError) {
36 emit(Generated.Events.INTERSTITIAL_SHOW_FAIL, adError)
37 }
38
39 override fun onAdShowedFullScreenContent() {
40 emit(Generated.Events.INTERSTITIAL_SHOW)
41 }
42
43 override fun onAdImpression() {
44 emit(Generated.Events.INTERSTITIAL_IMPRESSION)
45 }
46 }
47 emit(Generated.Events.INTERSTITIAL_LOAD)
48 ctx.resolve()
49 }
50
51 override fun onAdFailedToLoad(loadAdError: LoadAdError) {
52 clear()
53 emit(Generated.Events.INTERSTITIAL_LOAD_FAIL, loadAdError)
54 ctx.reject(loadAdError.message)
55 }
56 })
57 }
58
59 override val isLoaded: Boolean
60 get() = mAd != null
61
62 override fun show(ctx: Context?) {
63 mAd!!.show(activity)
64 ctx!!.resolve()
65 }
66
67 private fun clear() {
68 if (mAd != null) {
69 mAd!!.fullScreenContentCallback = null
70 mAd = null
71 }
72 }
73 }