Skip to content

Commit

Permalink
Merge pull request google#457 from cco3/json
Browse files Browse the repository at this point in the history
[android] Make PwoMetadata serializable to json
  • Loading branch information
cco3 committed Jul 8, 2015
2 parents 4ab5b0f + a68d691 commit 33d126f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private void updateDebugView(PwoMetadata pwoMetadata, View view) {
}

// Metadata debug line
float scanTime = pwoMetadata.scanMillis / 1000.0f;
double scanTime = pwoMetadata.scanMillis / 1000.0;
String scanTimeString = getString(R.string.metadata_debug_scan_time_prefix)
+ new DecimalFormat("##.##s").format(scanTime);
TextView scanTimeView = (TextView) view.findViewById(R.id.metadata_debug_scan_time);
Expand All @@ -486,12 +486,12 @@ private void updateDebugView(PwoMetadata pwoMetadata, View view) {
TextView pwsTripTimeView = (TextView) view.findViewById(R.id.metadata_debug_pws_trip_time);
if (pwoMetadata.hasUrlMetadata()) {
UrlMetadata urlMetadata = pwoMetadata.urlMetadata;
float rank = urlMetadata.rank;
double rank = urlMetadata.rank;
String rankString = getString(R.string.metadata_debug_rank_prefix)
+ new DecimalFormat("##.##").format(rank);
rankView.setText(rankString);

float pwsTripTime = pwoMetadata.pwsTripMillis / 1000.0f;
double pwsTripTime = pwoMetadata.pwsTripMillis / 1000.0;
String pwsTripTimeString = "" + getString(R.string.metadata_debug_pws_trip_time_prefix)
+ new DecimalFormat("##.##s").format(pwsTripTime);
pwsTripTimeView.setText(pwsTripTimeString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Base64;
import android.webkit.URLUtil;

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

import java.io.ByteArrayOutputStream;

/**
* This class holds data about a Physical Web Object and its url.
*
* A physical web object is any source of a broadcasted url.
*/
class PwoMetadata implements Comparable<PwoMetadata>{
class PwoMetadata implements Comparable<PwoMetadata> {
private static final String URL_KEY = "deviceAddress";
private static final String IS_PUBLIC_KEY = "isPublic";
private static final String SCAN_MILLIS_KEY = "scanMillis";
private static final String PWS_TRIP_MILLIS_KEY = "pwsTripMillis";
private static final String BLE_METADATA_KEY = "bleMetadata";
private static final String URL_METADATA_KEY = "urlMetadata";
String url;
boolean isPublic;
long scanMillis;
Expand All @@ -40,6 +53,9 @@ class PwoMetadata implements Comparable<PwoMetadata>{
* A container class for ble-specific metadata.
*/
public static class BleMetadata {
private static final String DEVICE_ADDRESS_KEY = "deviceAddress";
private static final String RSSI_KEY = "rssi";
private static final String TX_POWER_KEY = "txPower";
String deviceAddress;
int rssi;
int txPower;
Expand All @@ -49,6 +65,30 @@ public BleMetadata(String deviceAddress, int rssi, int txPower) {
this.rssi = rssi;
this.txPower = txPower;
}

public JSONObject toJsonObj() throws JSONException {
JSONObject jsonObj = new JSONObject();
jsonObj.put(DEVICE_ADDRESS_KEY, deviceAddress);
jsonObj.put(RSSI_KEY, rssi);
jsonObj.put(TX_POWER_KEY, txPower);
return jsonObj;
}

public String toJsonStr() throws JSONException {
return toJsonObj().toString();
}

public static BleMetadata fromJsonObj(JSONObject jsonObj) throws JSONException {
BleMetadata bleMetadata = new BleMetadata(
jsonObj.getString(DEVICE_ADDRESS_KEY),
jsonObj.getInt(RSSI_KEY),
jsonObj.getInt(TX_POWER_KEY));
return bleMetadata;
}

public static BleMetadata fromJsonStr(String jsonStr) throws JSONException {
return fromJsonObj(new JSONObject(jsonStr));
}
}

/**
Expand All @@ -59,27 +99,77 @@ public BleMetadata(String deviceAddress, int rssi, int txPower) {
* and returns a json blob.
*/
public static class UrlMetadata implements Comparable<UrlMetadata>{
private static final String ID_KEY = "id";
private static final String SITE_URL_KEY = "siteUrl";
private static final String DISPLAY_URL_KEY = "displayUrl";
private static final String TITLE_KEY = "title";
private static final String DESCRIPTION_KEY = "description";
private static final String ICON_URL_KEY = "iconUrl";
private static final String ICON_KEY = "icon";
private static final String RANK_KEY = "rank";
public String id;
public String siteUrl;
public String displayUrl;
public String title;
public String description;
public String iconUrl;
public Bitmap icon;
public float rank;
public double rank;

public UrlMetadata() {
}

public int compareTo(UrlMetadata other) {
int rankCompare = ((Float) rank).compareTo(other.rank);
int rankCompare = ((Double) rank).compareTo(other.rank);
if (rankCompare != 0) {
return rankCompare;
}

// If ranks are equal, compare based on title
return title.compareTo(other.title);
}

public JSONObject toJsonObj() throws JSONException {
JSONObject jsonObj = new JSONObject();
jsonObj.put(ID_KEY, id);
jsonObj.put(SITE_URL_KEY, siteUrl);
jsonObj.put(DISPLAY_URL_KEY, displayUrl);
jsonObj.put(TITLE_KEY, title);
jsonObj.put(DESCRIPTION_KEY, description);
jsonObj.put(ICON_URL_KEY, iconUrl);
if (icon != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapData = stream.toByteArray();
jsonObj.put(ICON_KEY, Base64.encodeToString(bitmapData, Base64.DEFAULT));
}
jsonObj.put(RANK_KEY, rank);
return jsonObj;
}

public String toJsonStr() throws JSONException {
return toJsonObj().toString();
}

public static UrlMetadata fromJsonObj(JSONObject jsonObj) throws JSONException {
UrlMetadata urlMetadata = new UrlMetadata();
urlMetadata.id = jsonObj.getString(ID_KEY);
urlMetadata.siteUrl = jsonObj.getString(SITE_URL_KEY);
urlMetadata.displayUrl = jsonObj.getString(DISPLAY_URL_KEY);
urlMetadata.title = jsonObj.getString(TITLE_KEY);
urlMetadata.description = jsonObj.getString(DESCRIPTION_KEY);
urlMetadata.iconUrl = jsonObj.getString(ICON_URL_KEY);
if (jsonObj.has(ICON_KEY)) {
byte[] bitmapData = Base64.decode(jsonObj.getString(ICON_KEY), Base64.DEFAULT);
urlMetadata.icon = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
}
urlMetadata.rank = jsonObj.getDouble(RANK_KEY);
return urlMetadata;
}

public static UrlMetadata fromJsonStr(String jsonStr) throws JSONException {
return fromJsonObj(new JSONObject(jsonStr));
}
}

public PwoMetadata(String url, long scanMillis) {
Expand Down Expand Up @@ -145,4 +235,39 @@ public int compareTo(PwoMetadata other) {
}
return urlMetadata.compareTo(other.urlMetadata);
}

public JSONObject toJsonObj() throws JSONException {
JSONObject jsonObj = new JSONObject();
jsonObj.put(URL_KEY, url);
jsonObj.put(SCAN_MILLIS_KEY, scanMillis);
jsonObj.put(PWS_TRIP_MILLIS_KEY, pwsTripMillis);
if (hasBleMetadata()) {
jsonObj.put(BLE_METADATA_KEY, bleMetadata.toJsonObj());
}
if (hasUrlMetadata()) {
jsonObj.put(URL_METADATA_KEY, urlMetadata.toJsonObj());
}
return jsonObj;
}

public String toJsonStr() throws JSONException {
return toJsonObj().toString();
}

public static PwoMetadata fromJsonObj(JSONObject jsonObj) throws JSONException {
PwoMetadata pwoMetadata = new PwoMetadata(jsonObj.getString(URL_KEY),
jsonObj.getLong(SCAN_MILLIS_KEY));
pwoMetadata.pwsTripMillis = jsonObj.getLong(PWS_TRIP_MILLIS_KEY);
if (jsonObj.has(BLE_METADATA_KEY)) {
pwoMetadata.bleMetadata = BleMetadata.fromJsonObj(jsonObj.getJSONObject(BLE_METADATA_KEY));
}
if (jsonObj.has(URL_METADATA_KEY)) {
pwoMetadata.urlMetadata = UrlMetadata.fromJsonObj(jsonObj.getJSONObject(URL_METADATA_KEY));
}
return pwoMetadata;
}

public static PwoMetadata fromJsonStr(String jsonStr) throws JSONException {
return fromJsonObj(new JSONObject(jsonStr));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void onResponse(JSONObject jsonResponse) {
urlMetadata.title = jsonUrlMetadata.optString("title");
urlMetadata.description = jsonUrlMetadata.optString("description");
urlMetadata.iconUrl = jsonUrlMetadata.optString("icon");
urlMetadata.rank = (float) jsonUrlMetadata.getDouble("rank");
urlMetadata.rank = jsonUrlMetadata.getDouble("rank");
} catch (JSONException e) {
Log.i(TAG, "Pws gave bad JSON: " + e);
continue;
Expand Down

0 comments on commit 33d126f

Please sign in to comment.