Advertisement
fastholf

VmaxBannerAdvert

Feb 27th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package com.camshare.camfrog.app.ads.banner;
  2.  
  3. import android.content.Context;
  4. import android.content.MutableContextWrapper;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.util.Log;
  8. import android.view.View;
  9.  
  10. import com.camshare.camfrog.app.ads.AdvertListenerAdapter;
  11. import com.vmax.android.ads.api.AdContainer;
  12. import com.vmax.android.ads.api.VmaxAdView;
  13. import com.vmax.android.ads.exception.VmaxAdError;
  14.  
  15. import java.util.concurrent.TimeUnit;
  16.  
  17. import io.reactivex.Observable;
  18. import io.reactivex.android.schedulers.AndroidSchedulers;
  19. import io.reactivex.disposables.Disposable;
  20. import io.reactivex.functions.Consumer;
  21. import io.reactivex.subjects.PublishSubject;
  22.  
  23. public
  24. class VmaxBannerAdvert
  25.         extends BannerAdvert
  26. {
  27.     private static final String TAG = VmaxBannerAdvert.class.getSimpleName();
  28.  
  29.     @NonNull
  30.     private final AdContainer mAdContainer;
  31.     private final String mVmaxBannerId;
  32.     private Disposable mLoadSubscription;
  33.  
  34.     private boolean mCacheWasLoaded = false;
  35.     private boolean mInScreen = false;
  36.     private int mInScreenLock = 0;
  37.  
  38.     @Nullable
  39.     private Disposable mTimerSubscription;
  40.  
  41.     private final PublishSubject<Boolean> mLoadSubject = PublishSubject.create();
  42.     private boolean mBackPressed;
  43.  
  44.     public
  45.     VmaxBannerAdvert(@NonNull final Context context, final String vmaxBannerId) {
  46.         mVmaxBannerId = vmaxBannerId;
  47.         final VmaxAdView bannerAdView = VmaxAdView.getMutableInstance(context, vmaxBannerId, VmaxAdView.UX_BANNER);
  48.         bannerAdView.setAdListener(new VmaxBannerAdListener());
  49.         bannerAdView.setRefresh(false);
  50.  
  51.         /** For fix #11119 (crash when displaying ads on xiaomi devices) */
  52.         bannerAdView.cacheAd();
  53.  
  54.         mAdContainer = AdContainer.getInstance();
  55.     }
  56.  
  57.     @Override
  58.     public
  59.     void replaceContext(@NonNull final Context context) {
  60.         ((MutableContextWrapper) getBanner().getContext()).setBaseContext(context);
  61.     }
  62.  
  63.     @Override
  64.     protected
  65.     void onProStatusChanged(final boolean free) {
  66.         Log.d(TAG, "onProStatusChanged: free = [" + free + "]");
  67.         if (free) {
  68.             initTimer();
  69.         } else {
  70.             stopTimer();
  71.         }
  72.     }
  73.  
  74.     private
  75.     void initTimer() {
  76.         stopTimer();
  77.  
  78.         Log.d(TAG, "startTimer");
  79.         mLoadSubscription = mLoadSubject.hide()
  80.                 .filter(inScreen -> inScreen)
  81.                 .throttleFirst(30, TimeUnit.SECONDS)
  82.                 .doOnNext(mStartTimerAction)
  83.                 .observeOn(AndroidSchedulers.mainThread())
  84.                 .subscribe(inScreen -> {
  85.                     Log.d(TAG, "======= timer tick =======");
  86.                     setVisible(false);
  87.                     mCacheWasLoaded = false;
  88.                     getBanner().cacheAd();
  89.                 });
  90.  
  91.         load();
  92.     }
  93.  
  94.     private final Consumer<Boolean> mStartTimerAction = inScreen -> {
  95.         if (mTimerSubscription != null) {
  96.             mTimerSubscription.dispose();
  97.         }
  98.         /** 31 second because timer in Android has a not high accuracy  */
  99.         mTimerSubscription = Observable.interval(31, TimeUnit.SECONDS)
  100.                 .subscribe(l -> load());
  101.     };
  102.  
  103.     private
  104.     void stopTimer() {
  105.         Log.d(TAG, "stopTimer");
  106.         if (mTimerSubscription != null) {
  107.             mTimerSubscription.dispose();
  108.         }
  109.         if (mLoadSubscription != null) {
  110.             mLoadSubscription.dispose();
  111.         }
  112.     }
  113.  
  114.     private
  115.     void load() {
  116.         mLoadSubject.onNext(mInScreen);
  117.     }
  118.  
  119.     @NonNull
  120.     @Override
  121.     public
  122.     VmaxAdView getView(@NonNull final Context context) {
  123.         replaceContext(context);
  124.         return getBanner();
  125.     }
  126.  
  127.     private
  128.     VmaxAdView getBanner() {
  129.         return mAdContainer.getAdView(mVmaxBannerId);
  130.     }
  131.  
  132.     @Override
  133.     public
  134.     void viewShown() {
  135.         mInScreenLock++;
  136.         mInScreen = true;
  137.         showAd();
  138.     }
  139.  
  140.     @Override
  141.     public
  142.     void viewHidden() {
  143.         mInScreenLock--;
  144.         if (mInScreenLock == 0) {
  145.             mInScreen = false;
  146.             setVisible(false);
  147.         }
  148.     }
  149.  
  150.     @Override
  151.     public
  152.     void showAd() {
  153.         Log.d(TAG, "showAd cacheWasLoaded = " + readyToShow());
  154.  
  155.         if (readyToShow()) {
  156.             getBanner().showAd();
  157.             setVisible(true);
  158.         } else {
  159.             load();
  160.         }
  161.     }
  162.  
  163.     private
  164.     boolean readyToShow() {
  165.         return mCacheWasLoaded;
  166.     }
  167.  
  168.  
  169.     @Override
  170.     public
  171.     void hideAd() {
  172.         setVisible(false);
  173.     }
  174.  
  175.     private
  176.     void setVisible(final boolean visible) {
  177.         Log.d(TAG, "setVisible() called with: visible = [" + visible + "]");
  178.         if (visible && isFree()) {
  179.             getBanner().setVisibility(View.VISIBLE);
  180.         } else {
  181.             getBanner().setVisibility(View.GONE);
  182.         }
  183.     }
  184.  
  185.     @Override
  186.     public
  187.     void onResume() {
  188.         Log.d(TAG, "onResume");
  189.         getBanner().onResume();
  190.         load();
  191.     }
  192.  
  193.     @Override
  194.     public
  195.     void onPause() {
  196.         Log.d(TAG, "onPause");
  197.         getBanner().onPause();
  198.     }
  199.  
  200.     @Override
  201.     public
  202.     void onBackPressed() {
  203.         if (mInScreen) {
  204.             Log.d(TAG, "onBackPressed");
  205.             mBackPressed = true;
  206.             getBanner().onBackPressed();
  207.         }
  208.     }
  209.  
  210.     @Override
  211.     public
  212.     void finish() {
  213.         if (!mInScreen) {
  214.             Log.d(TAG, "finish");
  215.             getBanner().finish();
  216.         }
  217.     }
  218.  
  219.     @Override
  220.     public
  221.     void onDestroy() {
  222.         if (!mInScreen && !mBackPressed) {
  223.             Log.d(TAG, "onDestroy");
  224.             mCacheWasLoaded = false;
  225.         }
  226.         mBackPressed = false;
  227.     }
  228.  
  229.     private
  230.     class VmaxBannerAdListener
  231.             extends AdvertListenerAdapter
  232.     {
  233.         @Override
  234.         public
  235.         void onAdReady(final VmaxAdView vmaxAdView) {
  236.             Log.d(TAG, "onAdReady: inScreen = " + mInScreen);
  237.             mCacheWasLoaded = true;
  238.  
  239.             if (mInScreen) {
  240.                 getBanner().showAd();
  241.             }
  242.         }
  243.  
  244.         @Override
  245.         public
  246.         void onAdError(final VmaxAdError vmaxAdError) {
  247.             mCacheWasLoaded = false;
  248.             setVisible(false);
  249.         }
  250.  
  251.         @Override
  252.         public
  253.         void onAdDismissed(final VmaxAdView vmaxAdView) {
  254.             super.onAdDismissed(vmaxAdView);
  255.         }
  256.  
  257.         @Override
  258.         public
  259.         void onAdStarted(final VmaxAdView vmaxAdView) { // TODO nsv: проверить что работает
  260.             Log.d(TAG, "willPresentAd");
  261.             if (isFree()) {
  262.                 vmaxAdView.setVisibility(View.VISIBLE);
  263.             }
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement