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

Replace low level json parsing #10815

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 java.io.UnsupportedEncodingException;
import java.util.Arrays;

import static com.mapbox.mapboxsdk.testapp.activity.offline.OfflineActivity.JSON_CHARSET;
import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertTrue;

public class OfflineUtilsTest extends BaseActivityTest {

private static final String REGION_NAME = "hello world";
private static final String CONVERTED_REGION_NAME = "{\"FIELD_REGION_NAME\":\"hello world\"}";

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

@Test
public void testOfflineUtilsConvertToBytes() throws UnsupportedEncodingException {
byte[] expected = CONVERTED_REGION_NAME.getBytes(JSON_CHARSET);
byte[] actual = OfflineUtils.convertRegionName(REGION_NAME);
assertTrue("Bytes arrays should match", Arrays.equals(expected, actual));
}

@Test
public void testOfflineUtilsConvertToString() throws UnsupportedEncodingException {
String actual = OfflineUtils.convertRegionName(CONVERTED_REGION_NAME.getBytes(JSON_CHARSET));
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 All @@ -47,6 +44,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
private MapView mapView;
private boolean customMarkerView;
private List<LatLng> locations;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -80,13 +78,15 @@ public boolean onCreateOptionsMenu(Menu menu) {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int amount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[position]);
if (locations == null) {
progressDialog = ProgressDialog.show(this, "Loading", "Fetching markers", false);
new LoadLocationTask(this, amount).execute();
} else {
showMarkers(amount);
}
}

private void onLatLngListLoaded(List<LatLng> latLngs, int amount) {
progressDialog.hide();
locations = latLngs;
showMarkers(amount);
}
Expand Down Expand Up @@ -247,32 +247,39 @@ public void onAnimationEnd(Animator animation) {

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

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

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

@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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import android.content.Context;
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.Point;
import com.mapbox.services.commons.models.Position;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -29,34 +28,13 @@ 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);
for (Feature feature : featureCollection.getFeatures()) {
if (feature.getGeometry() instanceof Point) {
Position point = ((Point) feature.getGeometry()).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,8 @@

import android.support.annotation.NonNull;

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

import timber.log.Timber;

Expand All @@ -14,24 +15,22 @@ public class OfflineUtils {
public static String convertRegionName(@NonNull byte[] metadata) {
try {
String json = new String(metadata, JSON_CHARSET);
JSONObject jsonObject = new JSONObject(json);
return jsonObject.getString(JSON_FIELD_REGION_NAME);
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
return jsonObject.get(JSON_FIELD_REGION_NAME).getAsString();
} catch (Exception exception) {
return null;
}
}

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();
metadata = json.getBytes(JSON_CHARSET);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(JSON_FIELD_REGION_NAME, regionName);
return jsonObject.toString().getBytes(JSON_CHARSET);
} catch (Exception exception) {
Timber.e(exception, "Failed to encode metadata: ");
}
return metadata;
return null;
}

}