Last active
September 1, 2023 09:03
-
-
Save pSapien/c8e0a46c774166027e0caf51bf4f3cf9 to your computer and use it in GitHub Desktop.
A bridged module for React Native and uses the Yodo1Mas android SDK to handle ads
This file contains hidden or 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
package com.bhoos.sdk; | |
import android.app.Activity; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import com.facebook.react.bridge.Arguments; | |
import com.facebook.react.bridge.Promise; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import com.facebook.react.bridge.WritableMap; | |
import com.facebook.react.modules.core.DeviceEventManagerModule; | |
import com.yodo1.mas.Yodo1Mas; | |
import com.yodo1.mas.error.Yodo1MasError; | |
import com.yodo1.mas.helper.model.Yodo1MasAdBuildConfig; | |
import com.yodo1.mas.interstitial.Yodo1MasInterstitialAd; | |
import com.yodo1.mas.interstitial.Yodo1MasInterstitialAdListener; | |
import com.yodo1.mas.reward.Yodo1MasRewardAd; | |
import com.yodo1.mas.reward.Yodo1MasRewardAdListener; | |
public class Yodo1MASAds extends ReactContextBaseJavaModule { | |
ReactApplicationContext context = null; | |
boolean initialized = false; | |
Yodo1MASAds(ReactApplicationContext context) { | |
super(context); | |
this.context = context; | |
} | |
Activity getActivity() { | |
Activity _activity = getCurrentActivity(); | |
if (_activity == null && this.context != null && this.context.getCurrentActivity() != null) { | |
_activity = this.context.getCurrentActivity(); | |
} | |
return _activity; | |
} | |
@ReactMethod | |
public void initSdk(String appKey) { | |
Yodo1MasAdBuildConfig config = new Yodo1MasAdBuildConfig.Builder().build(); | |
Yodo1Mas.getInstance().setAdBuildConfig(config); | |
Yodo1Mas.getInstance().setCOPPA(false); | |
Yodo1Mas.getInstance().setGDPR(false); | |
Yodo1Mas.getInstance().setCCPA(false); | |
Activity _activity = getActivity(); | |
if (_activity == null) return; | |
Yodo1Mas | |
.getInstance() | |
.initMas( | |
_activity, | |
appKey, | |
new Yodo1Mas.InitListener() { | |
@Override | |
public void onMasInitSuccessful() { | |
setInitialized(true); | |
initInterstitialAds(); | |
initRewardedAds(); | |
sendEvent(Constants.INIT_SUCCESS, null); | |
} | |
@Override | |
public void onMasInitFailed(@NonNull Yodo1MasError error) { | |
sendEvent(Constants.INIT_FAILURE, error); | |
} | |
} | |
); | |
} | |
public void initInterstitialAds() { | |
Yodo1MasInterstitialAd.getInstance().autoDelayIfLoadFail = false; | |
Yodo1MasInterstitialAd | |
.getInstance() | |
.setAdListener( | |
new Yodo1MasInterstitialAdListener() { | |
@Override | |
public void onInterstitialAdLoaded(Yodo1MasInterstitialAd ad) { | |
sendEvent(Constants.INTERSTITIAL_LOAD_SUCCESS, null); | |
} | |
@Override | |
public void onInterstitialAdFailedToLoad( | |
Yodo1MasInterstitialAd ad, | |
@NonNull Yodo1MasError error | |
) { | |
sendEvent(Constants.INTERSTITIAL_LOAD_FAILURE, error); | |
} | |
@Override | |
public void onInterstitialAdOpened(Yodo1MasInterstitialAd ad) { | |
sendEvent(Constants.INTERSTITIAL_OPENED_SUCCESS, null); | |
} | |
@Override | |
public void onInterstitialAdFailedToOpen( | |
Yodo1MasInterstitialAd ad, | |
@NonNull Yodo1MasError error | |
) { | |
sendEvent(Constants.INTERSTITIAL_OPENED_FAILURE, error); | |
} | |
@Override | |
public void onInterstitialAdClosed(Yodo1MasInterstitialAd ad) { | |
sendEvent(Constants.INTERSTITIAL_CLOSED, null); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void loadInterstitialAds() { | |
Activity _activity = getActivity(); | |
if (_activity == null) return; | |
_activity | |
.runOnUiThread( | |
new Runnable() { | |
@Override | |
public void run() { | |
Yodo1MasInterstitialAd | |
.getInstance() | |
.loadAd(_activity); | |
} | |
} | |
); | |
} | |
public void initRewardedAds() { | |
Yodo1MasRewardAd.getInstance().autoDelayIfLoadFail = false; | |
Yodo1MasRewardAd | |
.getInstance() | |
.setAdListener( | |
new Yodo1MasRewardAdListener() { | |
@Override | |
public void onRewardAdLoaded(Yodo1MasRewardAd ad) { | |
sendEvent(Constants.REWARD_LOAD_SUCCESS, null); | |
} | |
@Override | |
public void onRewardAdFailedToLoad( | |
Yodo1MasRewardAd ad, | |
@NonNull Yodo1MasError error | |
) { | |
sendEvent(Constants.REWARD_LOAD_FAILURE, error); | |
} | |
@Override | |
public void onRewardAdOpened(Yodo1MasRewardAd ad) { | |
sendEvent(Constants.REWARD_OPENED_SUCCESS, null); | |
} | |
@Override | |
public void onRewardAdFailedToOpen( | |
Yodo1MasRewardAd ad, | |
@NonNull Yodo1MasError error | |
) { | |
sendEvent(Constants.REWARD_OPENED_FAILURE, error); | |
} | |
@Override | |
public void onRewardAdClosed(Yodo1MasRewardAd ad) { | |
sendEvent(Constants.REWARD_CLOSED, null); | |
} | |
@Override | |
public void onRewardAdEarned(Yodo1MasRewardAd ad) { | |
sendEvent(Constants.REWARD_EARNED, null); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void loadRewardedAds() { | |
Activity _activity = getActivity(); | |
if (_activity == null) return; | |
_activity | |
.runOnUiThread( | |
new Runnable() { | |
@Override | |
public void run() { | |
Yodo1MasRewardAd | |
.getInstance() | |
.loadAd(_activity); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void showInterstitialAds() { | |
Activity _activity = getActivity(); | |
if (_activity == null) return; | |
_activity | |
.runOnUiThread( | |
new Runnable() { | |
@Override | |
public void run() { | |
Yodo1Mas | |
.getInstance() | |
.showInterstitialAd(_activity); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void showDebugger() { | |
Yodo1Mas.getInstance().showDebugger(getActivity()); | |
} | |
@ReactMethod | |
public void showRewardedAds() { | |
Activity _activity = getActivity(); | |
if (_activity == null) return; | |
getActivity() | |
.runOnUiThread( | |
new Runnable() { | |
@Override | |
public void run() { | |
Yodo1Mas | |
.getInstance() | |
.showRewardedAd(_activity); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void isInitialized(final Promise promise) { | |
promise.resolve(this.initialized); | |
} | |
@ReactMethod | |
public void setInitialized(final boolean initialized) { | |
this.initialized = initialized; | |
} | |
@Override | |
public String getName() { | |
return "Yodo1MASAds"; | |
} | |
private void sendEvent(String type, @Nullable Yodo1MasError error) { | |
WritableMap params = Arguments.createMap(); | |
params.putString("type", type); | |
if (error != null) { | |
params.putString("error", error.toString()); | |
} else { | |
params.putNull("error"); | |
} | |
this.context.getJSModule( | |
DeviceEventManagerModule.RCTDeviceEventEmitter.class | |
) | |
.emit(Constants.EVENT_NAME, params); | |
} | |
} | |
class Constants { | |
public static final String EVENT_NAME = "yodo1ad"; | |
public static final String INIT_SUCCESS = "init-success"; | |
public static final String INIT_FAILURE = "init-failure"; | |
public static final String REWARD_LOAD_SUCCESS = "reward-load-success"; | |
public static final String REWARD_LOAD_FAILURE = "reward-load-failure"; | |
public static final String REWARD_OPENED_SUCCESS = "reward-open-success"; | |
public static final String REWARD_OPENED_FAILURE = "reward-open-failure"; | |
public static final String REWARD_CLOSED = "reward-closed"; | |
public static final String REWARD_EARNED = "reward-earned"; | |
public static final String INTERSTITIAL_LOAD_SUCCESS = | |
"interstitial-load-success"; | |
public static final String INTERSTITIAL_LOAD_FAILURE = | |
"interstitial-load-failure"; | |
public static final String INTERSTITIAL_OPENED_SUCCESS = | |
"interstitial-open-success"; | |
public static final String INTERSTITIAL_OPENED_FAILURE = | |
"interstitial-open-failure"; | |
public static final String INTERSTITIAL_CLOSED = "interstitial-closed"; | |
public static final String INTERSTITIAL_EARNED = "interstitial-earned"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment