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

Commit

Permalink
[android] - refactor resource reading into util
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Aug 10, 2017
1 parent 5ae8522 commit 4627b6e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.style;

import android.content.res.Resources;
import android.support.annotation.RawRes;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
Expand All @@ -13,6 +12,7 @@
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
Expand All @@ -21,18 +21,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import timber.log.Timber;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.fail;

/**
* Tests for {@link GeoJsonSource}
Expand All @@ -46,19 +41,22 @@ protected Class getActivityClass() {
}

@Test
public void testFeatureCollection() {
public void testFeatureCollection() throws Exception {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new BaseViewAction() {

@Override
public void perform(UiController uiController, View view) {
GeoJsonSource source = new GeoJsonSource("source", FeatureCollection
.fromJson(readRawResource(rule.getActivity().getResources(), R.raw.test_feature_collection)));
GeoJsonSource source = null;
try {
source = new GeoJsonSource("source", FeatureCollection
.fromJson(ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_collection)));
} catch (IOException exception) {
Timber.e(exception);
}
mapboxMap.addSource(source);

mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
}

});
}

Expand All @@ -79,14 +77,19 @@ public void perform(UiController uiController, View view) {
}

@Test
public void testFeatureProperties() {
public void testFeatureProperties() throws IOException {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new BaseViewAction() {

@Override
public void perform(UiController uiController, View view) {
GeoJsonSource source = new GeoJsonSource("source",
readRawResource(rule.getActivity().getResources(), R.raw.test_feature_properties));
GeoJsonSource source = null;
try {
source = new GeoJsonSource("source",
ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_properties));
} catch (IOException exception) {
Timber.e(exception);
}
mapboxMap.addSource(source);

mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
Expand Down Expand Up @@ -141,8 +144,11 @@ public void perform(UiController uiController, View view) {
Layer layer = new CircleLayer("layer", source.getId());
mapboxMap.addLayer(layer);

source.setGeoJson(Feature.fromJson(
readRawResource(rule.getActivity().getResources(), resource)));
try {
source.setGeoJson(Feature.fromJson(ResourceUtils.readRawResource(rule.getActivity(), resource)));
} catch (IOException exception) {
Timber.e(exception);
}

mapboxMap.removeLayer(layer);
mapboxMap.removeSource(source);
Expand All @@ -151,27 +157,6 @@ public void perform(UiController uiController, View view) {
});
}

private String readRawResource(Resources resources, @RawRes int rawResource) {
InputStream is = resources.openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}
} catch (IOException err) {
fail(err.getMessage());
}

return writer.toString();
}

public abstract class BaseViewAction implements ViewAction {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.RawRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
Expand All @@ -14,15 +13,10 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import com.mapbox.services.commons.geojson.Feature;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;

import timber.log.Timber;
Expand Down Expand Up @@ -57,9 +51,10 @@ public void onMapReady(final MapboxMap mapboxMap) {

// Add a symbol layer (also works with annotations)
try {
mapboxMap.addSource(new GeoJsonSource("symbols-source", readRawResource(R.raw.test_points_utrecht)));
mapboxMap.addSource(new GeoJsonSource("symbols-source", ResourceUtils.readRawResource(
QueryRenderedFeaturesBoxSymbolCountActivity.this, R.raw.test_points_utrecht)));
} catch (IOException ioException) {
Timber.e(ioException,"Could not load geojson");
Timber.e(ioException, "Could not load geojson");
return;
}
mapboxMap.addImage(
Expand Down Expand Up @@ -94,23 +89,6 @@ public void onClick(View view) {
});
}

private String readRawResource(@RawRes int rawResource) throws IOException {
InputStream is = getResources().openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}

return writer.toString();
}

public MapboxMap getMapboxMap() {
return mapboxMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.RawRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -18,14 +17,9 @@
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import timber.log.Timber;

Expand Down Expand Up @@ -355,7 +349,7 @@ private void addParksLayer() {
// Add a source
Source source;
try {
source = new GeoJsonSource("amsterdam-parks-source", readRawResource(R.raw.amsterdam));
source = new GeoJsonSource("amsterdam-parks-source", ResourceUtils.readRawResource(this, R.raw.amsterdam));
mapboxMap.addSource(source);
} catch (IOException ioException) {
Toast.makeText(
Expand All @@ -375,21 +369,4 @@ private void addParksLayer() {
)
);
}

private String readRawResource(@RawRes int rawResource) throws IOException {
InputStream is = getResources().openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}

return writer.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RawRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -32,16 +31,11 @@
import com.mapbox.mapboxsdk.style.sources.TileSet;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -285,7 +279,7 @@ private void addParksLayer() {
// Add a source
Source source;
try {
source = new GeoJsonSource("amsterdam-spots", readRawResource(R.raw.amsterdam));
source = new GeoJsonSource("amsterdam-spots", ResourceUtils.readRawResource(this, R.raw.amsterdam));
} catch (IOException ioException) {
Toast.makeText(
RuntimeStyleActivity.this,
Expand Down Expand Up @@ -332,7 +326,7 @@ private void addDynamicParksLayer() {
// Load some data
FeatureCollection parks;
try {
String json = readRawResource(R.raw.amsterdam);
String json = ResourceUtils.readRawResource(this, R.raw.amsterdam);
parks = FeatureCollection.fromJson(json);
} catch (IOException ioException) {
Toast.makeText(
Expand Down Expand Up @@ -487,23 +481,6 @@ private void updateWaterColorOnZoom() {
}
}

private String readRawResource(@RawRes int rawResource) throws IOException {
InputStream is = getResources().openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}

return writer.toString();
}

private void addCustomTileSource() {
// Add a source
Source source = new VectorSource("custom-tile-source", new TileSet("2.1.0", "https://vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt?api_key=vector-tiles-LM25tq4"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RawRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -15,17 +14,12 @@
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.ref.WeakReference;

import timber.log.Timber;
Expand Down Expand Up @@ -87,7 +81,7 @@ private static class LoadStyleFileTask extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... voids) {
String styleJson = "";
try {
styleJson = RawResourceReaderWriter.readRawResource(context.get(), R.raw.sat_style);
styleJson = ResourceUtils.readRawResource(context.get(), R.raw.sat_style);
} catch (Exception exception) {
Timber.e(exception, "Can't load local file style");
}
Expand Down Expand Up @@ -126,7 +120,7 @@ protected Long doInBackground(Void... params) {
Timber.i("Writing style file to: %s", cacheStyleFile.getAbsolutePath());
Context context = this.context.get();
if (context != null) {
writeToFile(cacheStyleFile, RawResourceReaderWriter.readRawResource(context, R.raw.local_style));
writeToFile(cacheStyleFile, ResourceUtils.readRawResource(context, R.raw.local_style));
}
} catch (Exception exception) {
Toast.makeText(context.get(), "Could not create style file in cache dir", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -155,28 +149,6 @@ private void writeToFile(File file, String contents) throws IOException {
}
}

static class RawResourceReaderWriter {
static String readRawResource(Context context, @RawRes int rawResource) throws IOException {
String json = "";
if (context != null) {
InputStream is = context.getResources().openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}
json = writer.toString();
}
return json;
}
}

@Override
protected void onStart() {
super.onStart();
Expand Down
Loading

0 comments on commit 4627b6e

Please sign in to comment.