Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.camshare.camfrog.app.ads.banner;
- import android.content.Context;
- import android.content.MutableContextWrapper;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import android.view.View;
- import com.camshare.camfrog.app.ads.AdvertListenerAdapter;
- import com.vmax.android.ads.api.AdContainer;
- import com.vmax.android.ads.api.VmaxAdView;
- import com.vmax.android.ads.exception.VmaxAdError;
- import java.util.concurrent.TimeUnit;
- import io.reactivex.Observable;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.disposables.Disposable;
- import io.reactivex.functions.Consumer;
- import io.reactivex.subjects.PublishSubject;
- public
- class VmaxBannerAdvert
- extends BannerAdvert
- {
- private static final String TAG = VmaxBannerAdvert.class.getSimpleName();
- @NonNull
- private final AdContainer mAdContainer;
- private final String mVmaxBannerId;
- private Disposable mLoadSubscription;
- private boolean mCacheWasLoaded = false;
- private boolean mInScreen = false;
- private int mInScreenLock = 0;
- @Nullable
- private Disposable mTimerSubscription;
- private final PublishSubject<Boolean> mLoadSubject = PublishSubject.create();
- private boolean mBackPressed;
- public
- VmaxBannerAdvert(@NonNull final Context context, final String vmaxBannerId) {
- mVmaxBannerId = vmaxBannerId;
- final VmaxAdView bannerAdView = VmaxAdView.getMutableInstance(context, vmaxBannerId, VmaxAdView.UX_BANNER);
- bannerAdView.setAdListener(new VmaxBannerAdListener());
- bannerAdView.setRefresh(false);
- /** For fix #11119 (crash when displaying ads on xiaomi devices) */
- bannerAdView.cacheAd();
- mAdContainer = AdContainer.getInstance();
- }
- @Override
- public
- void replaceContext(@NonNull final Context context) {
- ((MutableContextWrapper) getBanner().getContext()).setBaseContext(context);
- }
- @Override
- protected
- void onProStatusChanged(final boolean free) {
- Log.d(TAG, "onProStatusChanged: free = [" + free + "]");
- if (free) {
- initTimer();
- } else {
- stopTimer();
- }
- }
- private
- void initTimer() {
- stopTimer();
- Log.d(TAG, "startTimer");
- mLoadSubscription = mLoadSubject.hide()
- .filter(inScreen -> inScreen)
- .throttleFirst(30, TimeUnit.SECONDS)
- .doOnNext(mStartTimerAction)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(inScreen -> {
- Log.d(TAG, "======= timer tick =======");
- setVisible(false);
- mCacheWasLoaded = false;
- getBanner().cacheAd();
- });
- load();
- }
- private final Consumer<Boolean> mStartTimerAction = inScreen -> {
- if (mTimerSubscription != null) {
- mTimerSubscription.dispose();
- }
- /** 31 second because timer in Android has a not high accuracy */
- mTimerSubscription = Observable.interval(31, TimeUnit.SECONDS)
- .subscribe(l -> load());
- };
- private
- void stopTimer() {
- Log.d(TAG, "stopTimer");
- if (mTimerSubscription != null) {
- mTimerSubscription.dispose();
- }
- if (mLoadSubscription != null) {
- mLoadSubscription.dispose();
- }
- }
- private
- void load() {
- mLoadSubject.onNext(mInScreen);
- }
- @NonNull
- @Override
- public
- VmaxAdView getView(@NonNull final Context context) {
- replaceContext(context);
- return getBanner();
- }
- private
- VmaxAdView getBanner() {
- return mAdContainer.getAdView(mVmaxBannerId);
- }
- @Override
- public
- void viewShown() {
- mInScreenLock++;
- mInScreen = true;
- showAd();
- }
- @Override
- public
- void viewHidden() {
- mInScreenLock--;
- if (mInScreenLock == 0) {
- mInScreen = false;
- setVisible(false);
- }
- }
- @Override
- public
- void showAd() {
- Log.d(TAG, "showAd cacheWasLoaded = " + readyToShow());
- if (readyToShow()) {
- getBanner().showAd();
- setVisible(true);
- } else {
- load();
- }
- }
- private
- boolean readyToShow() {
- return mCacheWasLoaded;
- }
- @Override
- public
- void hideAd() {
- setVisible(false);
- }
- private
- void setVisible(final boolean visible) {
- Log.d(TAG, "setVisible() called with: visible = [" + visible + "]");
- if (visible && isFree()) {
- getBanner().setVisibility(View.VISIBLE);
- } else {
- getBanner().setVisibility(View.GONE);
- }
- }
- @Override
- public
- void onResume() {
- Log.d(TAG, "onResume");
- getBanner().onResume();
- load();
- }
- @Override
- public
- void onPause() {
- Log.d(TAG, "onPause");
- getBanner().onPause();
- }
- @Override
- public
- void onBackPressed() {
- if (mInScreen) {
- Log.d(TAG, "onBackPressed");
- mBackPressed = true;
- getBanner().onBackPressed();
- }
- }
- @Override
- public
- void finish() {
- if (!mInScreen) {
- Log.d(TAG, "finish");
- getBanner().finish();
- }
- }
- @Override
- public
- void onDestroy() {
- if (!mInScreen && !mBackPressed) {
- Log.d(TAG, "onDestroy");
- mCacheWasLoaded = false;
- }
- mBackPressed = false;
- }
- private
- class VmaxBannerAdListener
- extends AdvertListenerAdapter
- {
- @Override
- public
- void onAdReady(final VmaxAdView vmaxAdView) {
- Log.d(TAG, "onAdReady: inScreen = " + mInScreen);
- mCacheWasLoaded = true;
- if (mInScreen) {
- getBanner().showAd();
- }
- }
- @Override
- public
- void onAdError(final VmaxAdError vmaxAdError) {
- mCacheWasLoaded = false;
- setVisible(false);
- }
- @Override
- public
- void onAdDismissed(final VmaxAdView vmaxAdView) {
- super.onAdDismissed(vmaxAdView);
- }
- @Override
- public
- void onAdStarted(final VmaxAdView vmaxAdView) { // TODO nsv: проверить что работает
- Log.d(TAG, "willPresentAd");
- if (isFree()) {
- vmaxAdView.setVisibility(View.VISIBLE);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement