|
import android.app.Activity; |
|
import android.os.Bundle; |
|
import android.util.Log; |
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import com.facebook.react.ReactRootView; |
|
import com.google.android.gms.ads.AdError; |
|
import com.google.android.gms.ads.AdRequest; |
|
import com.google.android.gms.ads.FullScreenContentCallback; |
|
import com.google.android.gms.ads.LoadAdError; |
|
import com.google.android.gms.ads.interstitial.InterstitialAd; |
|
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback; |
|
import com.lengerrong.coca.BuildConfig; |
|
|
|
public class LaunchAppAdManager { |
|
private static final String LOG_TAG = "LaunchAppAdManager"; |
|
private InterstitialAd mInterstitialAd; |
|
|
|
public void load(Activity activity, ReactRootView reactRootView) { |
|
if (mInterstitialAd != null) |
|
return; |
|
|
|
AdRequest adRequest = new AdRequest.Builder().build(); |
|
updateAdStateToReact(reactRootView, "AdLoading"); |
|
|
|
InterstitialAd.load(activity, BuildConfig.LAUNCH_APP_AD_UNIT_ID, adRequest, |
|
new InterstitialAdLoadCallback() { |
|
@Override |
|
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) { |
|
// The mInterstitialAd reference will be null until |
|
// an ad is loaded. |
|
mInterstitialAd = interstitialAd; |
|
Log.i(LOG_TAG, "onAdLoaded"); |
|
mInterstitialAd.show(activity); |
|
updateAdStateToReact(reactRootView, "AdLoaded"); |
|
|
|
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){ |
|
@Override |
|
public void onAdClicked() { |
|
// Called when a click is recorded for an ad. |
|
Log.d(LOG_TAG, "Ad was clicked."); |
|
updateAdStateToReact(reactRootView, "AdClicked"); |
|
} |
|
|
|
@Override |
|
public void onAdDismissedFullScreenContent() { |
|
// Called when ad is dismissed. |
|
// Set the ad reference to null so you don't show the ad a second time. |
|
Log.d(LOG_TAG, "Ad dismissed fullscreen content."); |
|
mInterstitialAd = null; |
|
updateAdStateToReact(reactRootView, "AdDismissed"); |
|
} |
|
|
|
@Override |
|
public void onAdFailedToShowFullScreenContent(AdError adError) { |
|
// Called when ad fails to show. |
|
Log.e(LOG_TAG, "Ad failed to show fullscreen content."); |
|
mInterstitialAd = null; |
|
updateAdStateToReact(reactRootView, "AdFailedToShow"); |
|
} |
|
|
|
@Override |
|
public void onAdImpression() { |
|
// Called when an impression is recorded for an ad. |
|
Log.d(LOG_TAG, "Ad recorded an impression."); |
|
updateAdStateToReact(reactRootView, "AdImpression"); |
|
} |
|
|
|
@Override |
|
public void onAdShowedFullScreenContent() { |
|
// Called when ad is shown. |
|
Log.d(LOG_TAG, "Ad showed fullscreen content."); |
|
updateAdStateToReact(reactRootView, "AdShowedFullScreenContent"); |
|
} |
|
}); |
|
} |
|
|
|
@Override |
|
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { |
|
// Handle the error |
|
Log.d(LOG_TAG, loadAdError.toString()); |
|
mInterstitialAd = null; |
|
updateAdStateToReact(reactRootView, "AdFailedToLoad"); |
|
} |
|
}); |
|
} |
|
|
|
public void show(Activity activity) { |
|
if (mInterstitialAd != null) { |
|
mInterstitialAd.show(activity); |
|
} else { |
|
Log.d(LOG_TAG, "The interstitial ad wasn't ready yet."); |
|
} |
|
} |
|
|
|
public void updateAdStateToReact(ReactRootView reactRootView, String adStatus) { |
|
Bundle updatedProps = reactRootView.getAppProperties(); |
|
updatedProps.putString("launchAdStatus", adStatus); |
|
reactRootView.setAppProperties(updatedProps); |
|
} |
|
|
|
} |