private void prepareMidRoll() {
    final ArrayList<ISlot> mid_slots =
        adContext.getSlotsByTimePositionClass(adConstants.TIME_POSITION_CLASS_MIDROLL());

    Iterator<ISlot> iter = mid_slots.iterator();
    while (iter.hasNext()) {
      ISlot slot = iter.next();
      double tp = slot.getTimePosition();
      Log.i(CLASSTAG, "at " + tp + " has midroll");
      this.midrolls.put(tp, slot.getCustomId());
      this.midrollkeys.add(tp);
    }

    // get all overlay slots
    final ArrayList<ISlot> overlay_slots =
        adContext.getSlotsByTimePositionClass(adConstants.TIME_POSITION_CLASS_OVERLAY());

    // TODO: what if the overlay has the same time position with a mid-roll?
    iter = overlay_slots.iterator();
    while (iter.hasNext()) {
      ISlot slot = iter.next();
      double tp = slot.getTimePosition();
      Log.i(CLASSTAG, "at " + tp + " has overlay");
      this.midrolls.put(tp, slot.getCustomId());
      this.midrollkeys.add(tp);
    }

    Collections.sort(this.midrollkeys);
  }
 protected void onFreeWheelEvent(IEvent event) {
   Log.i(CLASSTAG, event.getType());
   if (event.getType().equals(adConstants.EVENT_REQUEST_COMPLETE())) {
     Log.d(CLASSTAG, "ad request complete");
     try {
       Thread.sleep(5000);
     } catch (InterruptedException e) {
     }
     this.prerollSlots =
         adContext.getSlotsByTimePositionClass(adConstants.TIME_POSITION_CLASS_PREROLL());
     this.postrollSlots =
         adContext.getSlotsByTimePositionClass(adConstants.TIME_POSITION_CLASS_POSTROLL());
     this.showDisplayAd();
     this.prepareMidRoll();
     this.playAdSlot(adConstants.TIME_POSITION_CLASS_PREROLL());
   } else if (event.getType().equals(adConstants.EVENT_SLOT_ENDED())) {
     String customId = (String) event.getData().get(adConstants.INFO_KEY_CUSTOM_ID());
     ISlot endedSlot = adContext.getSlotByCustomId(customId);
     if (endedSlot.getTimePositionClass() == adConstants.TIME_POSITION_CLASS_PREROLL()
         || endedSlot.getTimePositionClass() == adConstants.TIME_POSITION_CLASS_POSTROLL()) {
       this.playAdSlot(endedSlot.getTimePositionClass());
     }
   } else if (event.getType().equals(adConstants.EVENT_REQUEST_CONTENT_VIDEO_RESUME())) {
     this.resumeMainVideo();
   } else if (event.getType().equals(adConstants.EVENT_REQUEST_CONTENT_VIDEO_PAUSE())) {
     this.pauseMainVideo();
   }
 }
  public void sendAdRequestToFreeWheel() {
    IEventListener eventListener =
        new IEventListener() {
          public void run(final IEvent event) {
            onFreeWheelEvent(event);
          }
        };

    adContext = AdPreviewer.ADMANAGER.newContext();
    adContext.setActivity(this);
    adConstants = adContext.getConstants();
    adContext.registerVideoDisplay(this.adHolder);

    adContext.addEventListener(adConstants.EVENT_REQUEST_COMPLETE(), eventListener);
    adContext.addEventListener(adConstants.EVENT_SLOT_ENDED(), eventListener);
    adContext.addEventListener(adConstants.EVENT_REQUEST_CONTENT_VIDEO_PAUSE(), eventListener);
    adContext.addEventListener(adConstants.EVENT_REQUEST_CONTENT_VIDEO_RESUME(), eventListener);

    if (this.previewJson) {
      adContext.addRenderer(
          this.HTMLTranslatorClass,
          null,
          null,
          "image/png,image/gif,image/jpeg,text/html,text/js_ref,text/javascript",
          "preroll,midroll,postroll,overlay,display",
          null);
    }
    adContext.submitRequest(10.0);
  }
  @Override
  public void onCompletion(MediaPlayer mp) {
    if (this.videoPlayer != null) {
      this.videoPlayer.setVisibility(View.GONE);
      this.videoBase.removeView(this.videoPlayer);
      this.videoPlayer = null;
    }

    if (this.adContext != null) {
      this.adContext.setVideoState(adConstants.VIDEO_STATE_COMPLETED());
      Log.d(CLASSTAG, "will play postroll");
      this.playAdSlot(adConstants.TIME_POSITION_CLASS_POSTROLL());
    } else {
      finish();
    }
  }
 public void playAdSlot(int tpc) {
   if (tpc == adConstants.TIME_POSITION_CLASS_PREROLL()) {
     if (!this.prerollSlots.isEmpty()) {
       ISlot slot = this.prerollSlots.remove(0);
       Log.d(CLASSTAG, "playing slot: " + slot.toString());
       slot.play();
     } else {
       this.playMainVideo();
     }
   } else if (tpc == adConstants.TIME_POSITION_CLASS_POSTROLL()) {
     if (!this.postrollSlots.isEmpty()) {
       ISlot slot = this.postrollSlots.remove(0);
       Log.d(CLASSTAG, "playing slot: " + slot.toString());
       slot.play();
     } else {
       this.finish();
     }
   }
 }
 private void pauseMainVideo() {
   this.currentPlayheadTime = this.videoPlayer.getCurrentPosition();
   this.checkTimer.cancel();
   this.adContext.setVideoState(adConstants.VIDEO_STATE_PAUSED());
   this.runOnUiThread(
       new Runnable() {
         @Override
         public void run() {
           videoPlayer.pause();
           videoPlayer.setVisibility(View.GONE);
         }
       });
 }
  @Override
  public void onPause() {
    Log.d(CLASSTAG, "onPause");
    super.onPause();
    if (this.videoPlayer != null) {
      this.checkTimer.cancel();
      this.checkTimer = new Timer();
      if (this.adContext != null) {
        this.adContext.setVideoState(adConstants.VIDEO_STATE_PAUSED());
      }
      if (this.videoPlayer.isPlaying()) {
        this.currentPlayheadTime = this.videoPlayer.getCurrentPosition();
        this.wasPlaying = true;
      } else {
        this.wasPlaying = false;
      }
    }

    if (this.adContext != null) {
      this.adContext.setActivityState(this.adContext.getConstants().ACTIVITY_STATE_PAUSE());
    }
  }