Android Open Source - playnomics-android Placement






From Project

Back to project page playnomics-android.

License

The source code is released under:

Apache License

If you think the Android project playnomics-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.playnomics.android.messaging;
/*w  w w.j  a  va 2  s .  com*/
import android.app.Activity;

import com.playnomics.android.messaging.Target.TargetType;
import com.playnomics.android.messaging.ui.PlayDialog;
import com.playnomics.android.messaging.ui.PlayViewFactory.IImageViewHandler;
import com.playnomics.android.messaging.ui.PlayWebView;
import com.playnomics.android.messaging.ui.RenderTaskFactory;
import com.playnomics.android.sdk.IPlacementDelegate;
import com.playnomics.android.sdk.IPlaynomicsPlacementDelegate;
import com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate;
import com.playnomics.android.session.ICallbackProcessor;
import com.playnomics.android.util.Logger;
import com.playnomics.android.util.Logger.LogLevel;
import com.playnomics.android.util.Util;

public class Placement implements PlayWebView.IPlayWebViewHandler, IImageViewHandler {

  public interface IPlacementStateObserver {
    void onPlacementShown(Activity activity, Placement placement);

    void onPlacementDisposed(Activity activity, Placement placement);
  }

  public enum PlacementState {
    NOT_LOADED, LOAD_STARTED, LOAD_COMPLETE, LOAD_FAILED
  }

  private boolean shouldRender;

  
  private boolean impressionLogged;

  private Util util;
  private IPlacementDelegate delegate;
  private Activity activity;
  private ICallbackProcessor callbackProcessor;
  private Logger logger;
  private IPlacementStateObserver observer;
  private RenderTaskFactory renderTaskFactory;

  private PlayDialog dialog;

  private String placementName;

  public String getPlacementName() {
    return placementName;
  }

  private Object sycnLockState = new Object();
  private PlacementState state;

  public PlacementState getState() {
    synchronized(sycnLockState){
      return state;
    }
  }

  public void setState(PlacementState state) {
    synchronized(sycnLockState){
      this.state = state;
    }
  }

  public void setDialog(PlayDialog dialog) {
    this.dialog = dialog;
  }

  private boolean hasNativeCloseButton() {
    return htmlAd != null
        && htmlAd.getCloseButton() instanceof NativeCloseButton;
  }

  private HtmlAd htmlAd;

  public Placement(String placementName,
      ICallbackProcessor callbackProcessor, Util util, Logger logger,
      IPlacementStateObserver observer,
      RenderTaskFactory renderTaskFactory) {
    this.observer = observer;
    this.placementName = placementName;
    this.state = PlacementState.NOT_LOADED;
    this.callbackProcessor = callbackProcessor;
    this.util = util;
    this.logger = logger;
    this.renderTaskFactory = renderTaskFactory;
  }

  public void updatePlacementData(HtmlAd htmlAd) {
    //do this first
    setState(PlacementState.LOAD_COMPLETE);
    this.htmlAd = htmlAd;
    impressionLogged = false;
    if (shouldRender) {
      loadWebView();
    }
  }

  public void show(final Activity activity,
      IPlacementDelegate delegate) {
    
    logger.log(LogLevel.DEBUG, "Showing on THREAD %d", Thread.currentThread().getId());
    this.delegate = delegate;
    this.activity = activity;
    
    shouldRender = true;
    if (getState() == PlacementState.LOAD_COMPLETE) {
      loadWebView();
    } else if (getState() == PlacementState.LOAD_FAILED) {
      if (delegate != null) {
        delegate.onRenderFailed();
      }
    }
  }

  private void loadWebView() {
    if (!(shouldRender && getState() == PlacementState.LOAD_COMPLETE)) {
      return;
    }

    Runnable renderTask = renderTaskFactory.createLayoutPlacementTask(this,
        htmlAd, activity, this, this, observer);
    // make sure we run this task on the UI thread
    util.runTaskOnActivityUIThread(renderTask, activity);
  }

  public void hide() {
    //do this first for synchronization
    setState(PlacementState.NOT_LOADED);
    removeFromView();
    shouldRender = false;
    observer.onPlacementDisposed(activity, this);
  }
  
  private void removeFromView(){
    if (dialog != null) {
      Runnable hideTask = renderTaskFactory
          .createHidePlacementTask(dialog);
      util.runTaskOnActivityUIThread(hideTask, activity);
      dialog = null;
    }
  }

  public void onLoadFailure(int errorCode) {
    onLoadFailure();
  }

  private void onLoadFailure() {
    setState(PlacementState.LOAD_FAILED);
    if (delegate != null) {
      delegate.onRenderFailed();
    }
  }

  public void onLoadComplete() {
    setState(PlacementState.LOAD_COMPLETE);
    
    Runnable showTask = renderTaskFactory.createShowPlacementTask(dialog);
    util.runTaskOnActivityUIThread(showTask, activity);
    
    if (!impressionLogged) {
      impressionLogged = true;
      callbackProcessor.processUrlCallback(htmlAd.getImpressionUrl());
    
      if (delegate != null) {
        if(delegate instanceof IPlaynomicsPlacementDelegate){
          ((IPlaynomicsPlacementDelegate) delegate).onShow(htmlAd.getTarget().getTargetData());
        } else if(delegate instanceof IPlaynomicsPlacementRawDelegate){
          ((IPlaynomicsPlacementRawDelegate) delegate).onShow(htmlAd.getTarget().getTargetDataJson());
        }
      }
    }
  }

  public void onUrlLoading(String url) {
    hide();
    if (!Util.stringIsNullOrEmpty(url)) {
      if (!hasNativeCloseButton()) {
        HtmlCloseButton htmlClose = (HtmlCloseButton) htmlAd
            .getCloseButton();
        if (url.equals(htmlClose.getCloseLink())) {
          onAdClosed(true);
          return;
        }
      }

      onAdTouched();
      if (!url.equals(htmlAd.getClickLink())) {
        util.startUriIntent(url, activity);
      }
    }
  }

  private void onAdTouched() {
    callbackProcessor.processUrlCallback(htmlAd.getClickUrl());
    Target target = htmlAd.getTarget();

    if (target.getTargetType() == TargetType.URL
        && !Util.stringIsNullOrEmpty(target.getTargetUrl())) {
      util.startUriIntent(target.getTargetUrl(), activity);
    }

    if (delegate != null) {
      if(delegate instanceof IPlaynomicsPlacementDelegate){
        ((IPlaynomicsPlacementDelegate) delegate).onTouch(htmlAd.getTarget().getTargetData());
      } else if(delegate instanceof IPlaynomicsPlacementRawDelegate){
        ((IPlaynomicsPlacementRawDelegate) delegate).onTouch(htmlAd.getTarget().getTargetDataJson());
      }
    }
  }

  private void onAdClosed(boolean closedByUser) {
    if (closedByUser) {
      callbackProcessor.processUrlCallback(htmlAd.getCloseUrl());
    }
    if (delegate != null) {
      if(delegate instanceof IPlaynomicsPlacementDelegate){
        ((IPlaynomicsPlacementDelegate) delegate).onClose(htmlAd.getTarget().getTargetData());
      } else if(delegate instanceof IPlaynomicsPlacementRawDelegate){
        ((IPlaynomicsPlacementRawDelegate) delegate).onClose(htmlAd.getTarget().getTargetDataJson());
      }
    }
  }

  public void attachActivity(Activity activity) {
    this.activity = activity;
    loadWebView();
  }

  public void detachActivity() {
    removeFromView();
  }

  public void onTouch() {
    hide();
    onAdClosed(true);
  }
}




Java Source Code List

com.playnomics.PlaynomicsMoreTestActivity.java
com.playnomics.PlaynomicsTestAppActivity.java
com.playnomics.RichDataFrameDelegate.java
com.playnomics.android.client.AssetClient.java
com.playnomics.android.client.EventQueue.java
com.playnomics.android.client.EventWorker.java
com.playnomics.android.client.HttpConnectionFactory.java
com.playnomics.android.client.IEventQueue.java
com.playnomics.android.client.IEventWorker.java
com.playnomics.android.client.IHttpConnectionFactory.java
com.playnomics.android.client.PlacementDataClient.java
com.playnomics.android.client.SegmentationClient.java
com.playnomics.android.events.AppPageEvent.java
com.playnomics.android.events.AppPauseEvent.java
com.playnomics.android.events.AppResumeEvent.java
com.playnomics.android.events.AppRunningEvent.java
com.playnomics.android.events.AppStartEvent.java
com.playnomics.android.events.CustomEvent.java
com.playnomics.android.events.ExplicitEvent.java
com.playnomics.android.events.ImplicitEvent.java
com.playnomics.android.events.PlaynomicsEvent.java
com.playnomics.android.events.TransactionEvent.java
com.playnomics.android.events.UserInfoEvent.java
com.playnomics.android.messaging.CloseButton.java
com.playnomics.android.messaging.HtmlAdFactory.java
com.playnomics.android.messaging.HtmlAd.java
com.playnomics.android.messaging.HtmlCloseButton.java
com.playnomics.android.messaging.MessagingManager.java
com.playnomics.android.messaging.NativeCloseButton.java
com.playnomics.android.messaging.Placement.java
com.playnomics.android.messaging.Position.java
com.playnomics.android.messaging.Target.java
com.playnomics.android.messaging.ui.IPlayViewFactory.java
com.playnomics.android.messaging.ui.PlayDialog.java
com.playnomics.android.messaging.ui.PlayViewFactory.java
com.playnomics.android.messaging.ui.PlayWebView.java
com.playnomics.android.messaging.ui.RenderTaskFactory.java
com.playnomics.android.push.GcmBroadcastReceiver.java
com.playnomics.android.push.GcmIntentService.java
com.playnomics.android.push.GcmManager.java
com.playnomics.android.sdk.IGoogleCloudMessageConfig.java
com.playnomics.android.sdk.IPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate.java
com.playnomics.android.sdk.IPlaynomicsSegmentationDelegate.java
com.playnomics.android.sdk.IPushConfig.java
com.playnomics.android.sdk.IPushNotificationDelegate.java
com.playnomics.android.sdk.Playnomics.java
com.playnomics.android.segments.UserSegmentIds.java
com.playnomics.android.session.ActivityObserver.java
com.playnomics.android.session.GameSessionInfo.java
com.playnomics.android.session.HeartBeatHandler.java
com.playnomics.android.session.HeartBeatProducer.java
com.playnomics.android.session.IActivityObserver.java
com.playnomics.android.session.ICallbackProcessor.java
com.playnomics.android.session.IHeartBeatProducer.java
com.playnomics.android.session.SessionStateMachine.java
com.playnomics.android.session.Session.java
com.playnomics.android.session.TouchEventHandler.java
com.playnomics.android.session.WindowCallbackProxy.java
com.playnomics.android.util.AndroidLogger.java
com.playnomics.android.util.AsyncTaskRunner.java
com.playnomics.android.util.CacheFile.java
com.playnomics.android.util.Config.java
com.playnomics.android.util.ContextWrapper.java
com.playnomics.android.util.EventTime.java
com.playnomics.android.util.IAsyncCall.java
com.playnomics.android.util.IConfig.java
com.playnomics.android.util.IRandomGenerator.java
com.playnomics.android.util.LargeGeneratedId.java
com.playnomics.android.util.LogWriter.java
com.playnomics.android.util.Logger.java
com.playnomics.android.util.Util.java