master
kt 83 lines 2.97 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.rewarded.RewardItem
11 import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
12 import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback
13
14 class RewardedInterstitial(ctx: ExecuteContext?) : AdBase(ctx), GenericAd {
15 private var mAd: RewardedInterstitialAd? = null
16 override fun destroy() {
17 clear()
18 super.destroy()
19 }
20
21 override fun load(ctx: Context?) {
22 clear()
23 RewardedInterstitialAd.load(
24 activity,
25 adUnitId,
26 ctx!!.optAdRequest(),
27 object : RewardedInterstitialAdLoadCallback() {
28 override fun onAdFailedToLoad(loadAdError: LoadAdError) {
29 clear()
30 emit(Generated.Events.REWARDED_INTERSTITIAL_LOAD_FAIL, loadAdError)
31 ctx.reject(loadAdError)
32 }
33
34 override fun onAdLoaded(rewardedAd: RewardedInterstitialAd) {
35 mAd = rewardedAd
36 val ssv = ctx.optServerSideVerificationOptions()
37 if (ssv != null) {
38 mAd!!.setServerSideVerificationOptions(ssv)
39 }
40 mAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
41 override fun onAdDismissedFullScreenContent() {
42 clear()
43 emit(Generated.Events.REWARDED_INTERSTITIAL_DISMISS)
44 }
45
46 override fun onAdFailedToShowFullScreenContent(adError: AdError) {
47 emit(Generated.Events.REWARDED_INTERSTITIAL_SHOW_FAIL, adError)
48 }
49
50 override fun onAdShowedFullScreenContent() {
51 emit(Generated.Events.REWARDED_INTERSTITIAL_SHOW)
52 }
53
54 override fun onAdImpression() {
55 emit(Generated.Events.REWARDED_INTERSTITIAL_IMPRESSION)
56 }
57 }
58 emit(Generated.Events.REWARDED_INTERSTITIAL_LOAD)
59 ctx.resolve()
60 }
61 })
62 }
63
64 override val isLoaded: Boolean
65 get() = mAd != null
66
67 override fun show(ctx: Context?) {
68 mAd!!.show(activity) { rewardItem: RewardItem? ->
69 emit(
70 Generated.Events.REWARDED_INTERSTITIAL_REWARD,
71 rewardItem!!
72 )
73 }
74 ctx!!.resolve()
75 }
76
77 private fun clear() {
78 if (mAd != null) {
79 mAd!!.fullScreenContentCallback = null
80 mAd = null
81 }
82 }
83 }