Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.camshare.camfrog.app.ads.interstitial;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import com.camshare.camfrog.app.ads.AdvertListenerAdapter;
- import com.camshare.camfrog.app.ads.IAdvertView;
- import com.vmax.android.ads.api.VmaxAdView;
- import com.vmax.android.ads.exception.VmaxAdError;
- import java.util.concurrent.TimeUnit;
- import io.reactivex.Observable;
- import static java.lang.System.currentTimeMillis;
- public
- class VmaxInterstitialAdvert
- extends InterstitialAdvert
- {
- private static final int ERROR = 1000;
- private final int mLoadAdDelay;
- @NonNull
- private final IAdvertView mInterstitial;
- @NonNull
- private final Delegate mDelegate;
- private long mLoadTimestamp = 0;
- private boolean mTimerNotExist = true;
- private State mState = State.NOT_LOADED;
- enum State
- {
- NOT_LOADED,
- LOADING,
- LOADED,
- LOAD_FAILED;
- }
- public
- VmaxInterstitialAdvert(@NonNull final IAdvertView interstitial,
- @NonNull final Delegate delegate,
- final int delayBetweenLoad) {
- mInterstitial = interstitial;
- mDelegate = delegate;
- mLoadAdDelay = delayBetweenLoad;
- mInterstitial.setAdListener(createListener());
- }
- @Override
- public
- void showAd(final Context context) {
- mInterstitial.replaceContext(context);
- switch (mState) {
- case LOADING:
- break;
- case LOADED:
- mInterstitial.showAd();
- break;
- case NOT_LOADED:
- case LOAD_FAILED:
- loadAd();
- break;
- }
- }
- protected
- InterstitialAdListener createListener() {
- return new InterstitialAdListener();
- }
- @Override
- public
- void replaceContext(@NonNull final Context context) {
- mInterstitial.replaceContext(context);
- }
- @Override
- public
- void loadAd() {
- final long delay = Math.abs(currentTimeMillis() - mLoadTimestamp);
- if (needToDelayLoad(delay)) {
- mTimerNotExist = false;
- Observable
- .timer(mLoadAdDelay - delay + ERROR, TimeUnit.MILLISECONDS)
- .subscribe(i -> {
- loadAd();
- mTimerNotExist = true;
- });
- } else if (wasNotLoaded()) {
- mState = State.LOADING;
- mInterstitial.cacheAd();
- mLoadTimestamp = currentTimeMillis();
- }
- }
- protected
- void onVideoLoaded() {
- }
- private
- boolean needToDelayLoad(final long delay) {
- return mState == State.LOAD_FAILED && delay < mLoadAdDelay && mTimerNotExist;
- }
- private
- boolean wasNotLoaded() {
- return mState != State.LOADED && mState != State.LOADING;
- }
- protected
- class InterstitialAdListener
- extends AdvertListenerAdapter
- {
- @Override
- public
- void onAdReady(final VmaxAdView vmaxAdView) {
- super.onAdReady(vmaxAdView);
- mState = State.LOADED;
- onVideoLoaded();
- }
- @Override
- public
- void onAdError(final VmaxAdError vmaxAdError) {
- super.onAdError(vmaxAdError);
- mState = State.LOAD_FAILED;
- }
- @Override
- public
- void onAdDismissed(final VmaxAdView vmaxAdView) {
- super.onAdDismissed(vmaxAdView);
- mState = State.NOT_LOADED;
- mDelegate.onAdClose();
- loadAd();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement