Android Open Source - playnomics-android Html Ad Factory






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  av  a 2 s  .c o m*/
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.playnomics.android.messaging.Position.PositionType;
import com.playnomics.android.messaging.Target.TargetType;
import com.playnomics.android.util.Logger;
import com.playnomics.android.util.Logger.LogLevel;
import com.playnomics.android.util.Util;

public class HtmlAdFactory {
  
  private Logger logger;
  
  public HtmlAdFactory(Logger logger){
    this.logger = logger;
  }
  
  public HtmlAd createDataFromBytes(byte[] data, String placementName)
      throws UnsupportedEncodingException, JSONException {
    if (data == null || data.length == 0) {
      return null;
    }

    String jsonData = new String(data, Util.UT8_ENCODING);
    JSONObject json = new JSONObject(jsonData);
  
    logger.log(LogLevel.VERBOSE, "Received json for placement: %s\n%s", placementName, jsonData);
    
    JSONArray adArray = json.getJSONArray("ads");

    if (adArray == null || adArray.length() == 0) {
      return null;
    }

    JSONObject firstAdData = adArray.getJSONObject(0);

    Position position = getPositionFromResponse(firstAdData);
    Target target = getTargetFromResponse(firstAdData);
    CloseButton closeButton = getCloseButtonFromResponse(firstAdData);
    String clickUrl = cleanJSONString(firstAdData, "clickUrl", false);
    String clickLink = cleanJSONString(firstAdData, "clickLink", true);
    String impressionUrl = cleanJSONString(firstAdData, "impressionUrl",
        false);
    String closeUrl = cleanJSONString(firstAdData, "closeUrl", false);
    String htmlContent = cleanJSONString(firstAdData, "htmlContent", false);
    return new HtmlAd(position, closeButton, target, clickLink, clickUrl,
        impressionUrl, closeUrl, htmlContent);
  }

  private Position getPositionFromResponse(JSONObject response)
      throws JSONException {
    JSONObject positionData = response.getJSONObject("position");
    String positionTypeString = cleanJSONString(positionData,
        "positionType", false);
    if (positionTypeString.equals("fullscreen")) {
      return new Position(PositionType.FULLSCREEN);
    }
    throw new JSONException(String.format("Unsupported positionType: %s",
        positionTypeString));
  }

  private Target getTargetFromResponse(JSONObject response)
      throws JSONException {
    JSONObject targetJSONData = response.getJSONObject("target");
    String targetTypeString = cleanJSONString(targetJSONData, "targetType",
        false);
    TargetType targetType = toTargetType(targetTypeString);

    if (targetType == TargetType.URL) {
      String targetUrl = cleanJSONString(targetJSONData, "targetUrl",
          true);
      return new Target(TargetType.URL, targetUrl);
    }

    if (targetType == TargetType.DATA) {
      String targetDataJSON = targetJSONData
          .optString("targetData", null);
      Map<String, Object> targetData = null;
      if (targetDataJSON != null) {
        JSONObject targetDataJson = new JSONObject(targetDataJSON);
        targetData = Util.toMap(targetDataJson);
      }
      return new Target(TargetType.DATA, targetData, targetDataJSON);
    }

    if (targetType == TargetType.EXTERNAL) {
      return new Target(TargetType.EXTERNAL);
    }
    throw new JSONException("Could not deserialize the target information");
  }

  private CloseButton getCloseButtonFromResponse(JSONObject response)
      throws JSONException {

    JSONObject closeButtonJSONData = response.getJSONObject("closeButton");

    String closeButtonType = closeButtonJSONData
        .getString("closeButtonType");

    if (closeButtonType.equals("native")) {
      String closeImageUrl = closeButtonJSONData
          .getString("closeImageUrl");
      Integer height = closeButtonJSONData.getInt("height");
      Integer width = closeButtonJSONData.getInt("width");
      return new NativeCloseButton(height, width, closeImageUrl);
    } else if (closeButtonType.equals("html")) {
      String closeLink = closeButtonJSONData.getString("closeLink");
      return new HtmlCloseButton(closeLink);
    }
    throw new JSONException(String.format(
        "Unsupported closeButtonType: %s", closeButtonType));
  }

  private TargetType toTargetType(String targetTypeString)
      throws JSONException {
    if (targetTypeString == null) {
      throw new JSONException("targetTypeString is null");
    }

    targetTypeString = targetTypeString.toLowerCase();

    if (targetTypeString.equals("url")) {
      return TargetType.URL;
    } else if (targetTypeString.equals("external")) {
      return TargetType.EXTERNAL;
    } else if (targetTypeString.equals("data")) {
      return TargetType.DATA;
    }
    throw new JSONException(String.format("Unsupported targetType: %s",
        targetTypeString));
  }

  private String cleanJSONString(JSONObject json, String key, boolean optional)
      throws JSONException {
    if (!json.has(key) || json.get(key) == JSONObject.NULL) {
      return null;
    }

    if (optional) {
      return json.optString(key, null);
    }
    return json.getString(key);
  }
}




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