Skip to content

Commit

Permalink
Fixed possible issue with int formatting in generated text files when…
Browse files Browse the repository at this point in the history
… value is >= 1000 m.
  • Loading branch information
zamojski committed Sep 30, 2016
1 parent e2f3e48 commit 3e574c2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ public String formatRow(Measurement m) {
// mcc value only when defined
int mcc = m.getMcc();
if (mcc != Measurement.UNKNOWN_CID)
sb.append(mcc);
sb.append(formatInt(mcc));
sb.append(',');
sb.append(m.getMnc());
sb.append(formatInt(m.getMnc()));
sb.append(',');
sb.append(m.getLac());
sb.append(formatInt(m.getLac()));
sb.append(',');
sb.append(m.getCid());
sb.append(formatInt(m.getCid()));
sb.append(',');
int psc = m.getPsc();
if (psc != Measurement.UNKNOWN_CID)
sb.append(psc);
sb.append(formatInt(psc));
sb.append(',');

sb.append(formatAsuSignal(m.getAsu()));
Expand All @@ -61,7 +61,7 @@ public String formatRow(Measurement m) {
sb.append(',');
int ta = m.getTa();
if (ta != Measurement.UNKNOWN_SIGNAL)
sb.append(ta);
sb.append(formatInt(ta));
sb.append(',');

sb.append(formatCoordinate(m.getLatitude()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public abstract class CsvFormatter implements ICsvFormatter {

protected static final NumberFormat coordsDoubleFormatter;

protected static final NumberFormat intFormatter;

protected static String deviceName;

static {
Expand All @@ -23,10 +25,18 @@ public abstract class CsvFormatter implements ICsvFormatter {
coordsDoubleFormatter.setMinimumFractionDigits(8);
coordsDoubleFormatter.setMaximumFractionDigits(12);

intFormatter = NumberFormat.getNumberInstance(LOCALE);
intFormatter.setParseIntegerOnly(true);
intFormatter.setGroupingUsed(false);

deviceName = ApkUtils.getDeviceName();
}

protected String formatCoordinate(double value) {
return coordsDoubleFormatter.format(value);
}

protected String formatInt(int value) {
return intFormatter.format(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,41 @@ public String formatRow(Measurement m) {
// mnc
sb.append(',');
// sid
sb.append(m.getMnc());
sb.append(formatInt(m.getMnc()));
sb.append(',');
// lac
sb.append(',');
// tac
sb.append(',');
// nid
sb.append(m.getLac());
sb.append(formatInt(m.getLac()));
sb.append(',');
// cellid
sb.append(',');
// bid
sb.append(m.getCid());
sb.append(formatInt(m.getCid()));
sb.append(',');
} else {
// mcc
sb.append(m.getMcc());
sb.append(formatInt(m.getMcc()));
sb.append(',');
// mnc
sb.append(m.getMnc());
sb.append(formatInt(m.getMnc()));
sb.append(',');
// sid
sb.append(',');
// lac
if (m.getNetworkType() != NetworkGroup.Lte)
sb.append(m.getLac());
sb.append(formatInt(m.getLac()));
sb.append(',');
// tac
if (m.getNetworkType() == NetworkGroup.Lte)
sb.append(m.getLac());
sb.append(formatInt(m.getLac()));
sb.append(',');
// nid
sb.append(',');
// cellid
sb.append(m.getCid());
sb.append(formatInt(m.getCid()));
sb.append(',');
// bid
sb.append(',');
Expand All @@ -90,28 +90,28 @@ public String formatRow(Measurement m) {
// psc
int psc = m.getPsc();
if (psc != Measurement.UNKNOWN_CID && m.getNetworkType() == NetworkGroup.Wcdma)
sb.append(psc);
sb.append(formatInt(psc));
sb.append(',');
// pci
if (psc != Measurement.UNKNOWN_CID && m.getNetworkType() == NetworkGroup.Lte)
sb.append(psc);
sb.append(formatInt(psc));
sb.append(',');

sb.append(formatSignal(m.getAsu(), m.getDbm()));
sb.append(',');
int ta = m.getTa();
if (ta != Measurement.UNKNOWN_SIGNAL)
sb.append(ta);
sb.append(formatInt(ta));
sb.append(',');

sb.append(formatDate(m.getTimestamp()));
sb.append(',');

sb.append(convertToInt(m.getGpsAccuracy()));
sb.append(formatInt(convertToInt(m.getGpsAccuracy())));
sb.append(',');
sb.append(convertToInt(m.getGpsSpeed()));
sb.append(formatInt(convertToInt(m.getGpsSpeed())));
sb.append(',');
sb.append(convertToInt(m.getGpsBearing()));
sb.append(formatInt(convertToInt(m.getGpsBearing())));
sb.append(',');

sb.append(cellUtils.getSystemType(m.getNetworkType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class GpxExportFormatter implements IGpxFormatter {
private static final NumberFormat gpsDoubleFormatter;
private static final SimpleDateFormat exportDateFormatter;

protected static final NumberFormat intFormatter;

private static final ICellUtils cellUtils;

static {
Expand All @@ -38,7 +40,11 @@ public class GpxExportFormatter implements IGpxFormatter {
gpsDoubleFormatter.setMinimumFractionDigits(0);
gpsDoubleFormatter.setMaximumFractionDigits(2);

exportDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
intFormatter = NumberFormat.getNumberInstance(LOCALE);
intFormatter.setParseIntegerOnly(true);
intFormatter.setGroupingUsed(false);

exportDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", LOCALE);
exportDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

cellUtils = new GeneralCellUtils();
Expand All @@ -47,25 +53,25 @@ public class GpxExportFormatter implements IGpxFormatter {
@Override
public String formatHeader(HeaderData h) {
String headerString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
headerString += String.format("<gpx version=\"1.1\" creator=\"Tower Collector %s\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v2 http://www.garmin.com/xmlschemas/TrackPointExtensionv2.xsd\">\r\n",
headerString += String.format(LOCALE, "<gpx version=\"1.1\" creator=\"Tower Collector %s\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v2 http://www.garmin.com/xmlschemas/TrackPointExtensionv2.xsd\">\r\n",
h.ApkVersion);
headerString += " <metadata>\r\n";
String trackName = String.format("Sessions taken between %s and %s",
String trackName = String.format(LOCALE, "Sessions taken between %s and %s",
formatDate(h.FirstMeasurementTimestamp),
formatDate(h.LastMeasurementTimestamp));
headerString += String.format(" <name>%s</name>\r\n",
headerString += String.format(LOCALE, " <name>%s</name>\r\n",
trackName);
headerString += String.format(" <desc>File generated by Tower Collector %s on %s</desc>\r\n", h.ApkVersion, ApkUtils.getDeviceName());
headerString += String.format(" <time>%s</time>\r\n",
headerString += String.format(LOCALE, " <desc>File generated by Tower Collector %s on %s</desc>\r\n", h.ApkVersion, ApkUtils.getDeviceName());
headerString += String.format(LOCALE, " <time>%s</time>\r\n",
formatDate(System.currentTimeMillis()));
headerString += String.format(" <bounds minlat=\"%s\" minlon=\"%s\" maxlat=\"%s\" maxlon=\"%s\" />\r\n",
headerString += String.format(LOCALE, " <bounds minlat=\"%s\" minlon=\"%s\" maxlat=\"%s\" maxlon=\"%s\" />\r\n",
formatCoordinate(h.Boundaries.getMinLat()),
formatCoordinate(h.Boundaries.getMinLon()),
formatCoordinate(h.Boundaries.getMaxLat()),
formatCoordinate(h.Boundaries.getMaxLon()));
headerString += " </metadata>\r\n";
headerString += " <trk>\r\n";
headerString += String.format(" <name>%s</name>\r\n",
headerString += String.format(LOCALE, " <name>%s</name>\r\n",
trackName);
headerString += " <trkseg>\r\n";
return headerString;
Expand All @@ -76,20 +82,20 @@ public String formatRow(Measurement m) {
StringBuilder sb = new StringBuilder();

// begin trackpoint
sb.append(String.format(" <trkpt lat=\"%s\" lon=\"%s\">\r\n", formatCoordinate(m.getLatitude()), formatCoordinate(m.getLongitude())));
sb.append(String.format(LOCALE, " <trkpt lat=\"%s\" lon=\"%s\">\r\n", formatCoordinate(m.getLatitude()), formatCoordinate(m.getLongitude())));

//set standard properties
boolean altitudeAvailable = !DoubleUtils.equals(m.getGpsAltitude(), Measurement.GPS_VALUE_NOT_AVAILABLE);
if (altitudeAvailable) {
sb.append(String.format(" <ele>%s</ele>\r\n", formatGpsValue(m.getGpsAltitude())));
sb.append(String.format(LOCALE, " <ele>%s</ele>\r\n", formatGpsValue(m.getGpsAltitude())));
}
sb.append(String.format(" <time>%s</time>\r\n", formatDate(m.getTimestamp())));
sb.append(String.format(LOCALE, " <time>%s</time>\r\n", formatDate(m.getTimestamp())));
int mcc = m.getMcc();
sb.append(String.format(" <name><![CDATA[%sMNC %d, LAC %d, CID %d, Signal strength %s dBm, Network %s, GPS accuracy %s m%s]]></name>\r\n",
(mcc != Measurement.UNKNOWN_CID ? String.format("MCC %d, ", mcc) : ""),
m.getMnc(),
m.getLac(),
m.getCid(),
sb.append(String.format(LOCALE, " <name><![CDATA[%sMNC %s, LAC %s, CID %s, Signal strength %s dBm, Network %s, GPS accuracy %s m%s]]></name>\r\n",
(mcc != Measurement.UNKNOWN_CID ? String.format(LOCALE, "MCC %s, ", formatInt(mcc)) : ""),
formatInt(m.getMnc()),
formatInt(m.getLac()),
formatInt(m.getCid()),
formatDbmSignal(m.getDbm()),
cellUtils.getSystemType(m.getNetworkType()),
formatGpsValue(m.getGpsAccuracy()),
Expand All @@ -102,10 +108,10 @@ public String formatRow(Measurement m) {
sb.append(" <extensions>\r\n"
+ " <gpxtpx:TrackPointExtension>\r\n");
if (speedAvailable)
sb.append(String.format(" <gpxtpx:speed>%s</gpxtpx:speed>\r\n",
sb.append(String.format(LOCALE, " <gpxtpx:speed>%s</gpxtpx:speed>\r\n",
formatGpsValue(m.getGpsSpeed())));
if (bearingAvailable)
sb.append(String.format(" <gpxtpx:bearing>%s</gpxtpx:bearing>\r\n",
sb.append(String.format(LOCALE, " <gpxtpx:bearing>%s</gpxtpx:bearing>\r\n",
formatGpsValue(m.getGpsBearing())));
sb.append(" </gpxtpx:TrackPointExtension>\r\n"
+ " </extensions>\r\n");
Expand Down Expand Up @@ -148,4 +154,8 @@ private String formatDbmSignal(int dbm) {
private String formatDate(long timestamp) {
return exportDateFormatter.format(new Date(timestamp));
}

private String formatInt(int value) {
return intFormatter.format(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@ public class JsonBroadcastFormatter implements IJsonFormatter {
private static final NumberFormat gpsDoubleFormatter;
private static final SimpleDateFormat exportDateFormatter;

protected static final NumberFormat intFormatter;

private static final ICellUtils cellUtils;

static {
coordsDoubleFormatter = NumberFormat.getNumberInstance(LOCALE);
coordsDoubleFormatter.setGroupingUsed(false);
coordsDoubleFormatter.setMinimumFractionDigits(8);
coordsDoubleFormatter.setMaximumFractionDigits(12);

gpsDoubleFormatter = NumberFormat.getNumberInstance(LOCALE);
gpsDoubleFormatter.setGroupingUsed(false);
gpsDoubleFormatter.setMinimumFractionDigits(0);
gpsDoubleFormatter.setMaximumFractionDigits(2);

intFormatter = NumberFormat.getNumberInstance(LOCALE);
intFormatter.setParseIntegerOnly(true);
intFormatter.setGroupingUsed(false);

cellUtils = new GeneralCellUtils();

exportDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", LOCALE);
exportDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
Expand All @@ -63,9 +72,9 @@ public String formatList(List<Measurement> ms) throws JSONException {
for (Measurement m : ms) {
JSONObject cell = new JSONObject();
cell.put("mcc", formatNullable(m.getMcc(), Measurement.UNKNOWN_CID));
cell.put("mnc", m.getMnc());
cell.put("lac", m.getLac());
cell.put("cell_id", m.getCid());
cell.put("mnc", formatInt(m.getMnc()));
cell.put("lac", formatInt(m.getLac()));
cell.put("cell_id", formatInt(m.getCid()));
cell.put("psc", formatNullable(m.getPsc(), Measurement.UNKNOWN_CID));
cell.put("asu", formatNullable(m.getAsu(), Measurement.UNKNOWN_SIGNAL));
cell.put("dbm", formatNullable(m.getDbm(), Measurement.UNKNOWN_SIGNAL));
Expand All @@ -80,7 +89,7 @@ public String formatList(List<Measurement> ms) throws JSONException {

private Object formatNullable(int value, int invalid) {
if (value != invalid)
return value;
return formatInt(value);
return JSONObject.NULL;
}

Expand All @@ -95,4 +104,8 @@ private String formatGpsValue(double value) {
private String formatDate(long timestamp) {
return exportDateFormatter.format(new Date(timestamp));
}

private String formatInt(int value) {
return intFormatter.format(value);
}
}

0 comments on commit 3e574c2

Please sign in to comment.