Advertisement
fastholf

VmaxInterstitialAdvert

Feb 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package com.camshare.camfrog.app.ads.interstitial;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5.  
  6. import com.camshare.camfrog.app.ads.AdvertListenerAdapter;
  7. import com.camshare.camfrog.app.ads.IAdvertView;
  8. import com.vmax.android.ads.api.VmaxAdView;
  9. import com.vmax.android.ads.exception.VmaxAdError;
  10.  
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. import io.reactivex.Observable;
  14.  
  15. import static java.lang.System.currentTimeMillis;
  16.  
  17. public
  18. class VmaxInterstitialAdvert
  19.         extends InterstitialAdvert
  20. {
  21.     private static final int ERROR = 1000;
  22.     private final int mLoadAdDelay;
  23.  
  24.     @NonNull
  25.     private final IAdvertView mInterstitial;
  26.     @NonNull
  27.     private final Delegate mDelegate;
  28.  
  29.     private long mLoadTimestamp = 0;
  30.     private boolean mTimerNotExist = true;
  31.  
  32.     private State mState = State.NOT_LOADED;
  33.  
  34.  
  35.     enum State
  36.     {
  37.         NOT_LOADED,
  38.         LOADING,
  39.         LOADED,
  40.         LOAD_FAILED;
  41.     }
  42.  
  43.     public
  44.     VmaxInterstitialAdvert(@NonNull final IAdvertView interstitial,
  45.                            @NonNull final Delegate delegate,
  46.                            final int delayBetweenLoad) {
  47.         mInterstitial = interstitial;
  48.         mDelegate = delegate;
  49.         mLoadAdDelay = delayBetweenLoad;
  50.         mInterstitial.setAdListener(createListener());
  51.     }
  52.  
  53.     @Override
  54.     public
  55.     void showAd(final Context context) {
  56.         mInterstitial.replaceContext(context);
  57.  
  58.         switch (mState) {
  59.             case LOADING:
  60.                 break;
  61.             case LOADED:
  62.                 mInterstitial.showAd();
  63.                 break;
  64.             case NOT_LOADED:
  65.             case LOAD_FAILED:
  66.                 loadAd();
  67.                 break;
  68.         }
  69.     }
  70.  
  71.     protected
  72.     InterstitialAdListener createListener() {
  73.         return new InterstitialAdListener();
  74.     }
  75.  
  76.     @Override
  77.     public
  78.     void replaceContext(@NonNull final Context context) {
  79.         mInterstitial.replaceContext(context);
  80.     }
  81.  
  82.     @Override
  83.     public
  84.     void loadAd() {
  85.         final long delay = Math.abs(currentTimeMillis() - mLoadTimestamp);
  86.         if (needToDelayLoad(delay)) {
  87.             mTimerNotExist = false;
  88.             Observable
  89.                     .timer(mLoadAdDelay - delay + ERROR, TimeUnit.MILLISECONDS)
  90.                     .subscribe(i -> {
  91.                         loadAd();
  92.                         mTimerNotExist = true;
  93.                     });
  94.         } else if (wasNotLoaded()) {
  95.             mState = State.LOADING;
  96.             mInterstitial.cacheAd();
  97.             mLoadTimestamp = currentTimeMillis();
  98.         }
  99.     }
  100.  
  101.     protected
  102.     void onVideoLoaded() {
  103.     }
  104.  
  105.     private
  106.     boolean needToDelayLoad(final long delay) {
  107.         return mState == State.LOAD_FAILED && delay < mLoadAdDelay && mTimerNotExist;
  108.     }
  109.  
  110.     private
  111.     boolean wasNotLoaded() {
  112.         return mState != State.LOADED && mState != State.LOADING;
  113.     }
  114.  
  115.     protected
  116.     class InterstitialAdListener
  117.             extends AdvertListenerAdapter
  118.     {
  119.         @Override
  120.         public
  121.         void onAdReady(final VmaxAdView vmaxAdView) {
  122.             super.onAdReady(vmaxAdView);
  123.             mState = State.LOADED;
  124.             onVideoLoaded();
  125.         }
  126.  
  127.         @Override
  128.         public
  129.         void onAdError(final VmaxAdError vmaxAdError) {
  130.             super.onAdError(vmaxAdError);
  131.             mState = State.LOAD_FAILED;
  132.         }
  133.  
  134.         @Override
  135.         public
  136.         void onAdDismissed(final VmaxAdView vmaxAdView) {
  137.             super.onAdDismissed(vmaxAdView);
  138.             mState = State.NOT_LOADED;
  139.             mDelegate.onAdClose();
  140.             loadAd();
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement