/**
  * Dismisses an interstitial ad This method dismisses an interstitial ad and only if it is
  * currently presented.
  *
  * <p>After it interstitial ad requires "loading process" to be ready for displaying
  *
  * <p>As a result you'll receive onLoopMeInterstitialHide() notification
  */
 public void dismiss() {
   if (mAdState == AdState.SHOWING) {
     Logging.out(LOG_TAG, "Dismiss ad");
     broadcastDestroyIntent();
     stopExpirationTimer();
     if (mHandler != null) {
       mHandler.removeCallbacksAndMessages(null);
     }
   } else {
     Logging.out(LOG_TAG, "Can't dismiss ad, it's not displaying");
   }
 }
  private void handleVideoMute(boolean mute) {
    Logging.out(LOG_TAG, "JS command: video mute " + mute, LogLevel.DEBUG);

    if (mVideoController != null) {
      mVideoController.muteVideo(mute);
    }
  }
  void switchToNormalMode() {
    if (mDisplayMode == DisplayMode.NORMAL) {
      return;
    }
    if (mDisplayMode == DisplayMode.FULLSCREEN) {
      handleFullscreenMode(false);
    }
    Logging.out(LOG_TAG, "switchToNormalMode", LogLevel.DEBUG);
    mDisplayMode = DisplayMode.NORMAL;

    LoopMeBannerView initialView = ((LoopMeBanner) mAd).getBannerView();
    initialView.setVisibility(View.VISIBLE);

    if (mMinimizedView != null && mMinimizedView.getParent() != null) {
      ((ViewGroup) mMinimizedView.getParent()).removeView(mMinimizedView);
      rebuildView(initialView);
      mMinimizedView.removeAllViews();
    }

    mAdView.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            return (event.getAction() == MotionEvent.ACTION_MOVE);
          }
        });
  }
 /**
  * Shows insterstitial. Interstitial should be loaded and ready to be shown.
  *
  * <p>As a result you'll receive onLoopMeInterstitialShow() callback
  */
 public void show() {
   Logging.out(LOG_TAG, "Interstitial will present fullscreen ad. App key: " + getAppKey());
   if (isReady()) {
     if (mAdState != AdState.SHOWING) {
       mAdState = AdState.SHOWING;
       stopExpirationTimer();
       startAdActivity();
     } else {
       Logging.out(LOG_TAG, "Interstitial is already presented on the screen");
     }
   } else {
     mShowWhenAdNotReadyCounter++;
     Logging.out(
         LOG_TAG, "Interstitial is not ready (" + mShowWhenAdNotReadyCounter + " time(s))");
   }
 }
  private void handleVideoLoad(String videoUrl) {
    Logging.out(LOG_TAG, "JS command: load video " + videoUrl, LogLevel.DEBUG);

    mIsVideoPresented = true;
    if (mVideoController != null) {
      mVideoController.loadVideoFile(videoUrl, mAd.getContext());
    }
  }
 /**
  * Getting already initialized ad object or create new one with specified appKey Note: Returns
  * null if Android version under 4.0
  *
  * @param appKey - your app key
  * @param context - application context
  */
 public static LoopMeInterstitial getInstance(String appKey, Context context) {
   if (Build.VERSION.SDK_INT >= 14) {
     return LoopMeAdHolder.getInterstitial(appKey, context);
   } else {
     Logging.out(LOG_TAG, "Not supported Android version. Expected Android 4.0+");
     return null;
   }
 }
 void preloadHtml(String html) {
   if (mAdView != null) {
     Logging.out(LOG_TAG, "loadDataWithBaseURL", LogLevel.DEBUG);
     mAdView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
   } else {
     mAd.onAdLoadFail(new LoopMeError("Html loading error"));
   }
 }
  private void switchToFullScreenMode() {
    if (mDisplayMode != DisplayMode.FULLSCREEN) {
      Logging.out(LOG_TAG, "switch to fullscreen mode", LogLevel.DEBUG);
      mDisplayMode = DisplayMode.FULLSCREEN;

      startAdActivity();
    }
  }
  /**
   * Creates new `LoopMeInterstitial` object with the given appKey
   *
   * @param context - application context
   * @param appKey - your app key
   * @throws IllegalArgumentException if any of parameters is null
   */
  LoopMeInterstitial(Context context, String appKey) {
    super(context, appKey);
    Logging.out(LOG_TAG, "Start creating interstitial with app key: " + appKey);

    mViewController = new ViewController(this);

    Utils.init(context);
    DebugController.init(context);
  }
 /**
  * Triggered when interstitial ad failed to load ad content
  *
  * @param interstitial - interstitial object - the sender of message
  * @param error - error of unsuccesful ad loading attempt
  */
 void onLoopMeInterstitialLoadFail(LoopMeInterstitial interstitial, final LoopMeError error) {
   Logging.out(LOG_TAG, "Ad fails to load: " + error.getMessage());
   mIsReady = false;
   mAdState = AdState.NONE;
   stopFetcherTimer();
   if (mAdListener != null) {
     mAdListener.onLoopMeInterstitialLoadFail(LoopMeInterstitial.this, error);
   }
 }
 /**
  * Triggered when the interstitial ad disappears on the screen
  *
  * @param interstitial - interstitial object the sender of message
  */
 void onLoopMeInterstitialHide(LoopMeInterstitial interstitial) {
   Logging.out(LOG_TAG, "Ad disappeared from screen");
   mIsReady = false;
   mAdState = AdState.NONE;
   releaseViewController(false);
   if (mAdListener != null) {
     mAdListener.onLoopMeInterstitialHide(this);
   }
 }
 private void handleVideoStretch(boolean b) {
   Logging.out(LOG_TAG, "JS command: stretch video ", LogLevel.DEBUG);
   if (mVideoController != null) {
     if (b) {
       mVideoController.setStreachVideoParameter(VideoController.StretchOption.STRECH);
     } else {
       mVideoController.setStreachVideoParameter(VideoController.StretchOption.NO_STRETCH);
     }
   }
 }
 /**
  * Triggered when the interstitial's loaded ad content is expired. Expiration happens when loaded
  * ad content wasn't displayed during some period of time, approximately one hour. Once the
  * interstitial is presented on the screen, the expiration is no longer tracked and interstitial
  * won't receive this message
  *
  * @param interstitial - interstitial object the sender of message
  */
 void onLoopMeInterstitialExpired(LoopMeInterstitial interstitial) {
   Logging.out(LOG_TAG, "Ads content expired");
   mExpirationTimer = null;
   mIsReady = false;
   mAdState = AdState.NONE;
   releaseViewController(false);
   if (mAdListener != null) {
     mAdListener.onLoopMeInterstitialExpired(this);
   }
 }
  private void handleNonLoopMe(String url) {
    Logging.out(LOG_TAG, "Non Js command", LogLevel.DEBUG);
    Context context = mAd.getContext();
    if (Utils.isOnline(context)) {
      Intent intent = new Intent(context, AdBrowserActivity.class);
      intent.putExtra(EXTRA_URL, url);
      intent.putExtra(StaticParams.APPKEY_TAG, mAd.getAppKey());
      intent.putExtra(StaticParams.FORMAT_TAG, mAd.getAdFormat());
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      mAd.onAdClicked();
      setWebViewState(WebviewState.HIDDEN);
      broadcastAdClickedIntent();

      context.startActivity(intent);
    } else {
      Logging.out(LOG_TAG, "No internet connection", LogLevel.DEBUG);
    }
  }
  private void startAdActivity() {
    Logging.out(LOG_TAG, "Starting Ad Activity");
    LoopMeAdHolder.putAd(this);

    Intent intent = new Intent(getContext(), AdActivity.class);
    intent.putExtra(StaticParams.APPKEY_TAG, getAppKey());
    intent.putExtra(StaticParams.FORMAT_TAG, getAdFormat());
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getContext().startActivity(intent);
  }
  private void handleVideoPlay(final int time) {
    Logging.out(LOG_TAG, "JS command: play video " + time, LogLevel.DEBUG);

    if (mVideoController != null) {
      mVideoController.playVideo(time);
    }

    if (mDisplayMode == DisplayMode.MINIMIZED) {
      Utils.animateAppear(mMinimizedView);
    }
  }
  /**
   * Triggered when the interstitial has successfully loaded the ad content
   *
   * @param interstitial - interstitial object the sender of message
   */
  public void onLoopMeInterstitialLoadSuccess(LoopMeInterstitial interstitial) {
    long currentTime = System.currentTimeMillis();
    long loadingTime = currentTime - mAdLoadingTimer;

    Logging.out(LOG_TAG, "Ad successfully loaded (" + loadingTime + "ms)");
    mIsReady = true;
    mAdState = AdState.NONE;
    stopFetcherTimer();
    if (mAdListener != null) {
      mAdListener.onLoopMeInterstitialLoadSuccess(LoopMeInterstitial.this);
    }
  }
 private void initAppVersion(Context context) {
   if (context == null) {
     mAppVersion = "0.0";
     return;
   }
   try {
     mAppVersion =
         context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
   } catch (NameNotFoundException e) {
     Logging.out(LOG_TAG, "Can't get app version. Exception: " + e.getMessage(), LogLevel.ERROR);
     mAppVersion = "0.0";
   }
 }
  @Override
  public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

    Logging.out(LOG_TAG, "onSurfaceTextureAvailable", LogLevel.DEBUG);

    int viewWidth = 0;
    int viewHeight = 0;

    switch (mDisplayMode) {
      case DisplayMode.MINIMIZED:
        if (mMinimizedMode != null) {
          viewWidth = mMinimizedMode.getWidth();
          viewHeight = mMinimizedMode.getHeight();
        } else {
          Logging.out(LOG_TAG, "WARNING: MinimizedMode is null", LogLevel.ERROR);
        }
        break;

      case DisplayMode.NORMAL:
        viewWidth = mAd.detectWidth();
        viewHeight = mAd.detectHeight();
        break;

      case DisplayMode.FULLSCREEN:
        viewWidth = Utils.getScreenWidth();
        viewHeight = Utils.getScreenHeight();
        break;

      default:
        Logging.out(LOG_TAG, "Unknown display mode", LogLevel.ERROR);
        break;
    }

    if (mVideoController != null) {
      mVideoController.setSurface(mTextureView);
      mVideoController.resizeVideo(mTextureView, viewWidth, viewHeight);
    }
  }
 void destroy(boolean interruptFile) {
   mBridgeListener = null;
   if (mVideoController != null) {
     mVideoController.destroy(interruptFile);
     mVideoController = null;
   }
   if (mAdView != null) {
     mAdView.stopLoading();
     mAdView.clearCache(true);
     mAdView = null;
     Logging.out(LOG_TAG, "AdView destroyed", LogLevel.DEBUG);
   }
   mMinimizedMode = null;
 }
 public String getViewerToken() {
   String advId = mAdvertisingId;
   if (TextUtils.isEmpty(advId)) {
     mDntPresent = true;
     if (mLoopMeId == null) {
       String loopmeId = Long.toHexString(Double.doubleToLongBits(Math.random()));
       Logging.out(LOG_TAG, "LoopMe Id = " + loopmeId, LogLevel.DEBUG);
       mLoopMeId = loopmeId;
     }
     return mLoopMeId;
   } else {
     return advId;
   }
 }
  void switchToMinimizedMode() {
    if (mDisplayMode == DisplayMode.MINIMIZED) {
      if (getCurrentVideoState() == VideoState.PAUSED) {
        setWebViewState(WebviewState.VISIBLE);
      }
      return;
    }
    Logging.out(LOG_TAG, "switchToMinimizedMode", LogLevel.DEBUG);
    mDisplayMode = DisplayMode.MINIMIZED;

    int width = mMinimizedMode.getWidth();
    int height = mMinimizedMode.getHeight();
    mMinimizedView = new LoopMeBannerView(mAdView.getContext(), width, height);

    rebuildView(mMinimizedView);
    addBordersToView(mMinimizedView);

    if (mAdView.getCurrentWebViewState() == WebviewState.HIDDEN) {
      mMinimizedView.setAlpha(0);
    }

    mMinimizedMode.getRootView().addView(mMinimizedView);
    configMinimizedViewLayoutParams(mMinimizedView);

    setWebViewState(WebviewState.VISIBLE);

    mAdView.setOnTouchListener(
        new SwipeListener(
            width,
            new SwipeListener.Listener() {
              @Override
              public void onSwipe(boolean toRight) {
                mAdView.setWebViewState(WebviewState.HIDDEN);

                Animation anim = AnimationUtils.makeOutAnimation(mAd.getContext(), toRight);
                anim.setDuration(200);
                mMinimizedView.startAnimation(anim);

                switchToNormalMode();
                mMinimizedMode = null;
              }
            }));
  }
 private void handleLoadSuccess() {
   Logging.out(LOG_TAG, "JS command: load success", LogLevel.DEBUG);
   mAd.startExpirationTimer();
   mAd.onAdLoadSuccess();
 }
 @Override
 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
   Logging.out(LOG_TAG, "onSurfaceTextureDestroyed", LogLevel.DEBUG);
   return false;
 }
 /**
  * Triggered when your application is about to go to the background, initiated by the SDK. This
  * may happen in various ways, f.e if user wants open the SDK's browser web page in native browser
  * or clicks on `mailto:` links...
  *
  * @param interstitial - interstitial object the sender of message
  */
 void onLoopMeInterstitialLeaveApp(LoopMeInterstitial interstitial) {
   Logging.out(LOG_TAG, "Leaving application");
   if (mAdListener != null) {
     mAdListener.onLoopMeInterstitialLeaveApp(this);
   }
 }
 /**
  * Triggered only when interstitial's video was played until the end. It won't be sent if the
  * video was skipped or the interstitial was dissmissed during the displaying process
  *
  * @param interstitial - interstitial object - the sender of message
  */
 void onLoopMeInterstitialVideoDidReachEnd(LoopMeInterstitial interstitial) {
   Logging.out(LOG_TAG, "Video reach end");
   if (mAdListener != null) {
     mAdListener.onLoopMeInterstitialVideoDidReachEnd(this);
   }
 }
 private void handleClose() {
   Logging.out(LOG_TAG, "JS command: close", LogLevel.DEBUG);
   mAd.dismiss();
 }
 private void handleVideoPause(int time) {
   Logging.out(LOG_TAG, "JS command: pause video " + time, LogLevel.DEBUG);
   if (mVideoController != null) {
     mVideoController.pauseVideo(time);
   }
 }
 void setGoogleAdvertisingId(String advId) {
   Logging.out(LOG_TAG, "Advertising Id = " + advId, LogLevel.DEBUG);
   mAdvertisingId = advId;
 }
 private void handleLoadFail(String mess) {
   Logging.out(LOG_TAG, "JS command: load fail", LogLevel.DEBUG);
   loadFail(mAd, new LoopMeError("Failed to process ad"));
 }