Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - replace low level json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jan 2, 2018
1 parent 4709bf0 commit c17d722
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mapbox.mapboxsdk.testapp.offline;

import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.OfflineUtils;
import org.junit.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static junit.framework.Assert.assertEquals;

public class OfflineUtilsTest extends BaseActivityTest {

private static final String REGION_NAME = "hello world";

@Override
protected Class getActivityClass() {
return EspressoTestActivity.class;
}

@Test
public void testOfflineUtilsConvertToBytes() {
byte[] expected = REGION_NAME.getBytes(UTF_8);
byte[] actual = OfflineUtils.convertRegionName(REGION_NAME);
assertEquals("Bytes should match", expected, actual);
}

@Test
public void testOfflineUtilsConvertToString() {
String actual = OfflineUtils.convertRegionName(REGION_NAME.getBytes());
assertEquals("Strings should match", REGION_NAME, actual);
}

@Test
public void testOfflineUtilsConvertNoOp() {
String convertNoOp = OfflineUtils.convertRegionName(OfflineUtils.convertRegionName(REGION_NAME));
assertEquals("Strings should match", REGION_NAME, convertNoOp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
Expand All @@ -26,18 +25,16 @@
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
import com.mapbox.mapboxsdk.testapp.utils.IconUtils;

import org.json.JSONException;
import timber.log.Timber;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import timber.log.Timber;

/**
* Test activity showcasing adding a large amount of Markers or MarkerViews.
*/
Expand Down Expand Up @@ -247,32 +244,42 @@ public void onAnimationEnd(Animator animation) {

private static class LoadLocationTask extends AsyncTask<Void, Integer, List<LatLng>> {

private BulkMarkerActivity activity;
private WeakReference<BulkMarkerActivity> activity;
private ProgressDialog progressDialog;
private int amount;

private LoadLocationTask(BulkMarkerActivity activity, int amount) {
this.amount = amount;
this.activity = activity;
this.activity = new WeakReference<>(activity);
progressDialog = ProgressDialog.show(activity, "Loading", "Fetching markers", false);
}

@Override
protected List<LatLng> doInBackground(Void... params) {
try {
String json = GeoParseUtil.loadStringFromAssets(activity.getApplicationContext(), "points.geojson");
return GeoParseUtil.parseGeoJsonCoordinates(json);
} catch (IOException | JSONException exception) {
Timber.e(exception, "Could not add markers");
return null;
BulkMarkerActivity activity = this.activity.get();
if (activity != null) {
String json = null;
try {
json = GeoParseUtil.loadStringFromAssets(activity.getApplicationContext(), "points.geojson");
} catch (IOException exception) {
Timber.e(exception, "Could not add markers");
}

if (json != null) {
return GeoParseUtil.parseGeoJsonCoordinates(json);
}
}
return null;
}

@Override
protected void onPostExecute(List<LatLng> locations) {
super.onPostExecute(locations);
activity.onLatLngListLoaded(locations, amount);
progressDialog.hide();
BulkMarkerActivity activity = this.activity.get();
if (activity != null) {
activity.onLatLngListLoaded(locations, amount);
progressDialog.hide();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import android.text.TextUtils;

import com.mapbox.mapboxsdk.geometry.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Geometry;
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.models.Position;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -29,35 +30,17 @@ public static String loadStringFromAssets(final Context context, final String fi
return readAll(rd);
}

public static List<LatLng> parseGeoJsonCoordinates(String geojsonStr) throws JSONException {
public static List<LatLng> parseGeoJsonCoordinates(String geojsonStr) {
List<LatLng> latLngs = new ArrayList<>();
JSONObject jsonObject = new JSONObject(geojsonStr);
JSONArray features = jsonObject.getJSONArray("features");
int featureLength = features.length();
for (int j = 0; j < featureLength; ++j) {
JSONObject feature = features.getJSONObject(j);
JSONObject geometry = feature.getJSONObject("geometry");
String type = geometry.getString("type");
JSONArray coordinates;
if (type.equals("Polygon")) {
coordinates = geometry.getJSONArray("coordinates").getJSONArray(0);
} else {
coordinates = geometry.getJSONArray("coordinates");
}
int len = coordinates.length();
for (int i = 0; i < len; ++i) {
if (coordinates.get(i) instanceof JSONArray) {
JSONArray coord = coordinates.getJSONArray(i);
double lng = coord.getDouble(0);
double lat = coord.getDouble(1);
latLngs.add(new LatLng(lat, lng));
} else {
double lng = coordinates.getDouble(0);
double lat = coordinates.getDouble(1);
latLngs.add(new LatLng(lat, lng));
break;
}
}
FeatureCollection featureCollection = FeatureCollection.fromJson(geojsonStr);

Geometry<Point> geometry;
Position point;

for (Feature feature : featureCollection.getFeatures()) {
geometry = feature.getGeometry();
point = geometry.getCoordinates().getCoordinates();
latLngs.add(new LatLng(point.getLatitude(), point.getLongitude()));
}
return latLngs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.support.annotation.NonNull;

import org.json.JSONObject;
import com.google.gson.JsonObject;

import timber.log.Timber;

Expand All @@ -13,9 +13,10 @@ public class OfflineUtils {

public static String convertRegionName(@NonNull byte[] metadata) {
try {
JsonObject jsonObject = new JsonObject();
String json = new String(metadata, JSON_CHARSET);
JSONObject jsonObject = new JSONObject(json);
return jsonObject.getString(JSON_FIELD_REGION_NAME);
jsonObject.addProperty(JSON_FIELD_REGION_NAME, json);
return jsonObject.getAsString();
} catch (Exception exception) {
return null;
}
Expand All @@ -24,9 +25,9 @@ public static String convertRegionName(@NonNull byte[] metadata) {
public static byte[] convertRegionName(String regionName) {
byte[] metadata = null;
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(JSON_FIELD_REGION_NAME, regionName);
String json = jsonObject.toString();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(JSON_FIELD_REGION_NAME, regionName);
String json = jsonObject.getAsString();
metadata = json.getBytes(JSON_CHARSET);
} catch (Exception exception) {
Timber.e(exception, "Failed to encode metadata: ");
Expand Down

0 comments on commit c17d722

Please sign in to comment.