diff options
4 files changed, 86 insertions, 58 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/OfflineUtilsTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/OfflineUtilsTest.java new file mode 100644 index 0000000000..84f84fdb90 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/OfflineUtilsTest.java @@ -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); + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java index 7ef2d5b6d4..afeb3b8979 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java @@ -16,7 +16,6 @@ import android.widget.ArrayAdapter; 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; @@ -26,18 +25,16 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; 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. */ @@ -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) { @@ -80,6 +78,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView 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); @@ -87,6 +86,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView } private void onLatLngListLoaded(List<LatLng> latLngs, int amount) { + progressDialog.hide(); locations = latLngs; showMarkers(amount); } @@ -247,32 +247,39 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView 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); + } } } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.java index 0d21fd2c71..97f70e33bb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.java @@ -2,12 +2,11 @@ package com.mapbox.mapboxsdk.testapp.utils; 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; @@ -29,34 +28,13 @@ public class GeoParseUtil { 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; diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/OfflineUtils.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/OfflineUtils.java index 4be0d6ea84..d4ec95ce5c 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/OfflineUtils.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/OfflineUtils.java @@ -2,7 +2,8 @@ package com.mapbox.mapboxsdk.testapp.utils; import android.support.annotation.NonNull; -import org.json.JSONObject; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import timber.log.Timber; @@ -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; } } |