generated from capacitor-community/.github
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reward interstitial support (#330)
- Loading branch information
1 parent
93d19e5
commit fbb5b9b
Showing
22 changed files
with
833 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...a/com/getcapacitor/community/admob/rewardedinterstitial/AdRewardInterstitialExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import androidx.core.util.Supplier; | ||
import com.getcapacitor.JSObject; | ||
import com.getcapacitor.PluginCall; | ||
import com.getcapacitor.PluginMethod; | ||
import com.getcapacitor.community.admob.helpers.AdViewIdHelper; | ||
import com.getcapacitor.community.admob.helpers.RequestHelper; | ||
import com.getcapacitor.community.admob.models.AdMobPluginError; | ||
import com.getcapacitor.community.admob.models.AdOptions; | ||
import com.getcapacitor.community.admob.models.Executor; | ||
import com.google.android.gms.ads.AdRequest; | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd; | ||
import com.google.android.gms.common.util.BiConsumer; | ||
|
||
public class AdRewardInterstitialExecutor extends Executor { | ||
|
||
public static RewardedInterstitialAd mRewardedInterstitialAd; | ||
|
||
public AdRewardInterstitialExecutor( | ||
Supplier<Context> contextSupplier, | ||
Supplier<Activity> activitySupplier, | ||
BiConsumer<String, JSObject> notifyListenersFunction, | ||
String pluginLogTag | ||
) { | ||
super(contextSupplier, activitySupplier, notifyListenersFunction, pluginLogTag, "AdRewardExecutor"); | ||
} | ||
|
||
@PluginMethod | ||
public void prepareRewardInterstitialAd(final PluginCall call, BiConsumer<String, JSObject> notifyListenersFunction) { | ||
final AdOptions adOptions = AdOptions.getFactory().createRewardInterstitialOptions(call); | ||
|
||
activitySupplier | ||
.get() | ||
.runOnUiThread( | ||
() -> { | ||
try { | ||
final AdRequest adRequest = RequestHelper.createRequest(adOptions); | ||
final String id = AdViewIdHelper.getFinalAdId(adOptions, adRequest, logTag, contextSupplier.get()); | ||
RewardedInterstitialAd.load( | ||
contextSupplier.get(), | ||
id, | ||
adRequest, | ||
RewardedInterstitialAdCallbackAndListeners.INSTANCE.getRewardedAdLoadCallback( | ||
call, | ||
notifyListenersFunction, | ||
adOptions | ||
) | ||
); | ||
} catch (Exception ex) { | ||
call.reject(ex.getLocalizedMessage(), ex); | ||
} | ||
} | ||
); | ||
} | ||
|
||
@PluginMethod | ||
public void showRewardInterstitialAd(final PluginCall call, BiConsumer<String, JSObject> notifyListenersFunction) { | ||
if (mRewardedInterstitialAd == null) { | ||
String errorMessage = "No Reward Video Ad can be show. It was not prepared or maybe it failed to be prepared."; | ||
call.reject(errorMessage); | ||
AdMobPluginError errorObject = new AdMobPluginError(-1, errorMessage); | ||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.FailedToLoad, errorObject); | ||
return; | ||
} | ||
|
||
try { | ||
activitySupplier | ||
.get() | ||
.runOnUiThread( | ||
() -> { | ||
mRewardedInterstitialAd.show( | ||
activitySupplier.get(), | ||
RewardedInterstitialAdCallbackAndListeners.INSTANCE.getOnUserEarnedRewardListener(call, notifyListenersFunction) | ||
); | ||
} | ||
); | ||
} catch (Exception ex) { | ||
call.reject(ex.getLocalizedMessage(), ex); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...com/getcapacitor/community/admob/rewardedinterstitial/RewardInterstitialAdPluginEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial | ||
|
||
import com.getcapacitor.community.admob.models.LoadPluginEventNames | ||
|
||
object RewardInterstitialAdPluginEvents: LoadPluginEventNames { | ||
const val Loaded = "onRewardedInterstitialAdLoaded" | ||
const val FailedToLoad = "onRewardedInterstitialAdFailedToLoad" | ||
const val Rewarded = "onRewardedInterstitialAdReward" | ||
override val Showed = "onRewardedInterstitialAdShowed" | ||
override val FailedToShow = "onRewardedInterstitialAdFailedToShow" | ||
override val Dismissed = "onRewardedInterstitialAdDismissed" | ||
} |
50 changes: 50 additions & 0 deletions
50
...acitor/community/admob/rewardedinterstitial/RewardedInterstitialAdCallbackAndListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial | ||
|
||
import com.getcapacitor.JSObject | ||
import com.getcapacitor.PluginCall | ||
import com.getcapacitor.community.admob.helpers.FullscreenPluginCallback | ||
import com.getcapacitor.community.admob.models.AdMobPluginError | ||
import com.getcapacitor.community.admob.models.AdOptions | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.OnUserEarnedRewardListener | ||
import com.google.android.gms.ads.rewarded.RewardItem | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback | ||
import com.google.android.gms.common.util.BiConsumer | ||
|
||
object RewardedInterstitialAdCallbackAndListeners { | ||
|
||
fun getOnUserEarnedRewardListener(call: PluginCall, notifyListenersFunction: BiConsumer<String, JSObject>): OnUserEarnedRewardListener { | ||
return OnUserEarnedRewardListener { item: RewardItem -> | ||
val response = JSObject() | ||
response.put("type", item.type) | ||
.put("amount", item.amount) | ||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.Rewarded, response) | ||
call.resolve(response) | ||
} | ||
} | ||
|
||
fun getRewardedAdLoadCallback(call: PluginCall, notifyListenersFunction: BiConsumer<String, JSObject>, adOptions: AdOptions): RewardedInterstitialAdLoadCallback { | ||
return object : RewardedInterstitialAdLoadCallback() { | ||
override fun onAdLoaded(ad: RewardedInterstitialAd) { | ||
AdRewardInterstitialExecutor.mRewardedInterstitialAd = ad | ||
AdRewardInterstitialExecutor.mRewardedInterstitialAd.fullScreenContentCallback = FullscreenPluginCallback( | ||
RewardInterstitialAdPluginEvents, notifyListenersFunction) | ||
|
||
val adInfo = JSObject() | ||
adInfo.put("adUnitId", ad.adUnitId) | ||
call.resolve(adInfo) | ||
|
||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.Loaded, adInfo) | ||
} | ||
|
||
override fun onAdFailedToLoad(adError: LoadAdError) { | ||
val adMobError = AdMobPluginError(adError) | ||
|
||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.FailedToLoad, adMobError) | ||
call.reject(adError.message) | ||
} | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...oid/src/main/java/com/getcapacitor/community/admob/rewardedinterstitial/models/SsvInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial.models | ||
|
||
import com.getcapacitor.PluginCall | ||
|
||
class SsvInfo( | ||
val customData: String? = null, | ||
val userId: String? = null) { | ||
|
||
constructor(pluginCall: PluginCall?) : this( | ||
pluginCall?.getObject("ssv")?.getString("customData"), | ||
pluginCall?.getObject("ssv")?.getString("userId") | ||
) | ||
|
||
constructor() : this(null, null) | ||
|
||
val hasInfo | ||
get(): Boolean { | ||
return customData != null || userId != null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.