Skip to content

Commit 19450d5

Browse files
author
Cameron Mace
authored
make service and turf RuntimeExceptions (mapbox#373)
* make service and turf RuntimeExceptions * fixed checkstyle * fixed error in test json
1 parent bb848c7 commit 19450d5

21 files changed

Lines changed: 208 additions & 322 deletions

File tree

mapbox/app/src/main/java/com/mapbox/services/android/testapp/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected void onCreate(Bundle savedInstanceState) {
176176

177177
// Check for location permission
178178
permissionsManager = new PermissionsManager(this);
179-
if (!permissionsManager.areLocationPermissionsGranted(this)) {
179+
if (!PermissionsManager.areLocationPermissionsGranted(this)) {
180180
recyclerView.setEnabled(false);
181181
permissionsManager.requestLocationPermissions(this);
182182
}

mapbox/app/src/main/java/com/mapbox/services/android/testapp/directions/DirectionsV5Activity.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.mapbox.services.Constants;
1919
import com.mapbox.services.android.testapp.R;
2020
import com.mapbox.services.android.testapp.Utils;
21-
import com.mapbox.services.api.ServicesException;
2221
import com.mapbox.services.api.directions.v5.DirectionsCriteria;
2322
import com.mapbox.services.api.directions.v5.MapboxDirections;
2423
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
@@ -91,16 +90,13 @@ public void onMapReady(MapboxMap mapboxMapReady) {
9190
.snippet("San Jose"));
9291

9392
// Get route from API
94-
try {
95-
getRoute(origin, destination);
96-
} catch (ServicesException servicesException) {
97-
servicesException.printStackTrace();
98-
}
93+
getRoute(origin, destination);
94+
9995
}
10096
});
10197
}
10298

103-
private void getRoute(Position origin, Position destination) throws ServicesException {
99+
private void getRoute(Position origin, Position destination) {
104100
ArrayList<Position> positions = new ArrayList<>();
105101
positions.add(origin);
106102
positions.add(destination);

mapbox/app/src/main/java/com/mapbox/services/android/testapp/directions/RouteUtilsV5Activity.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.mapbox.services.api.directions.v5.models.LegStep;
3131
import com.mapbox.services.api.directions.v5.models.RouteLeg;
3232
import com.mapbox.services.api.navigation.v5.RouteUtils;
33-
import com.mapbox.services.api.utils.turf.TurfException;
3433
import com.mapbox.services.commons.models.Position;
3534
import com.mapbox.services.commons.utils.PolylineUtils;
3635

@@ -94,15 +93,7 @@ public void onMapClick(@NonNull LatLng point) {
9493
} else if (to == null) {
9594
setTo(point);
9695
} else {
97-
try {
98-
doUtils(point);
99-
} catch (ServicesException servicesException) {
100-
Log.e(LOG_TAG, "Services exception: " + servicesException.getMessage());
101-
servicesException.printStackTrace();
102-
} catch (TurfException turfException) {
103-
Log.e(LOG_TAG, "Turf exception: " + turfException.getMessage());
104-
turfException.printStackTrace();
105-
}
96+
doUtils(point);
10697
}
10798
}
10899
});
@@ -121,16 +112,11 @@ private void setTo(LatLng point) {
121112
.position(point)
122113
.title("To"));
123114

124-
try {
125-
getRoute(Position.fromCoordinates(from.getLongitude(), from.getLatitude()),
126-
Position.fromCoordinates(to.getLongitude(), to.getLatitude()));
127-
} catch (ServicesException servicesException) {
128-
showMessage(servicesException.getMessage());
129-
servicesException.printStackTrace();
130-
}
115+
getRoute(Position.fromCoordinates(from.getLongitude(), from.getLatitude()),
116+
Position.fromCoordinates(to.getLongitude(), to.getLatitude()));
131117
}
132118

133-
private void doUtils(LatLng point) throws ServicesException, TurfException {
119+
private void doUtils(LatLng point) {
134120
// Remove previous
135121
if (userTap != null) {
136122
mapboxMap.removeMarker(userTap);
@@ -159,7 +145,7 @@ private void doUtils(LatLng point) throws ServicesException, TurfException {
159145
// Draw snap to route lines
160146
snapLines = new ArrayList<>();
161147
for (int stepIndex = 0; stepIndex < route.getSteps().size(); stepIndex++) {
162-
Position snapPoint = routeUtils.getSnapToRoute(position, route, stepIndex);
148+
Position snapPoint = RouteUtils.getSnapToRoute(position, route, stepIndex);
163149
LatLng[] points = new LatLng[] {
164150
point,
165151
new LatLng(snapPoint.getLatitude(), snapPoint.getLongitude())};

mapbox/app/src/main/java/com/mapbox/services/android/testapp/distance/DistanceActivity.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.mapbox.mapboxsdk.maps.MapboxMap;
1414
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
1515
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
16-
import com.mapbox.mapboxsdk.style.layers.NoSuchLayerException;
1716
import com.mapbox.mapboxsdk.style.layers.Property;
1817
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
1918
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
@@ -209,12 +208,11 @@ private void updateLayers() {
209208

210209
for (int i = 0; i < restaurants.size(); i++) {
211210

212-
try {
211+
if (mapboxMap.getLayer(restaurants.get(i).getStringProperty("name" + "-layer")) != null) {
213212
mapboxMap.removeLayer(restaurants.get(i).getStringProperty("name") + "-layer");
214-
} catch (NoSuchLayerException exception) {
215-
// Layer hasn't been added yet. No worries :)
216213
}
217214

215+
218216
SymbolLayer marker = new SymbolLayer(
219217
restaurants.get(i).getStringProperty("name") + "-layer",
220218
restaurants.get(i).getStringProperty("name") + "-source"

mapbox/app/src/main/java/com/mapbox/services/android/testapp/geocoding/GeocodingReverseActivity.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
1414
import com.mapbox.services.android.testapp.R;
1515
import com.mapbox.services.android.testapp.Utils;
16-
import com.mapbox.services.api.ServicesException;
1716
import com.mapbox.services.api.geocoding.v5.GeocodingCriteria;
1817
import com.mapbox.services.api.geocoding.v5.MapboxGeocoding;
1918
import com.mapbox.services.api.geocoding.v5.models.CarmenFeature;
@@ -56,11 +55,7 @@ public void onMapClick(@NonNull LatLng point) {
5655
mapboxMap.addMarker(new MarkerOptions()
5756
.position(point)
5857
.title("Your finger is here"));
59-
try {
60-
geocode(point);
61-
} catch (ServicesException servicesException) {
62-
setMessage("Geocoding failed: " + servicesException.getMessage());
63-
}
58+
geocode(point);
6459
}
6560
});
6661
}
@@ -113,7 +108,7 @@ public void onLowMemory() {
113108
* Forward geocoding
114109
*/
115110

116-
private void geocode(LatLng point) throws ServicesException {
111+
private void geocode(LatLng point) {
117112
Position position = Position.fromCoordinates(point.getLongitude(), point.getLatitude());
118113
MapboxGeocoding client = new MapboxGeocoding.Builder()
119114
.setAccessToken(Utils.getMapboxAccessToken(this))

mapbox/app/src/main/java/com/mapbox/services/android/testapp/nav/OffRouteDetectionActivity.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
3434
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
3535
import com.mapbox.services.api.navigation.v5.RouteUtils;
36-
import com.mapbox.services.api.utils.turf.TurfException;
3736
import com.mapbox.services.api.utils.turf.TurfMeasurement;
3837
import com.mapbox.services.commons.geojson.LineString;
3938
import com.mapbox.services.commons.models.Position;
@@ -102,19 +101,12 @@ public void onMapClick(@NonNull LatLng point) {
102101

103102
destination = Position.fromCoordinates(point.getLongitude(), point.getLatitude());
104103

105-
try {
106-
getRoute(
107-
Position.fromCoordinates(car.getPosition().getLongitude(), car.getPosition().getLatitude()),
108-
Position.fromCoordinates(point.getLongitude(), point.getLatitude())
109-
);
110-
} catch (ServicesException servicesException) {
111-
servicesException.printStackTrace();
112-
Log.e(TAG, "onMapReady: " + servicesException.getMessage());
113-
}
114-
104+
getRoute(
105+
Position.fromCoordinates(car.getPosition().getLongitude(), car.getPosition().getLatitude()),
106+
Position.fromCoordinates(point.getLongitude(), point.getLatitude())
107+
);
115108
}
116109
});
117-
118110
} // End onMapReady
119111
});
120112
} // End onCreate
@@ -281,7 +273,7 @@ private void drawRoute(DirectionsRoute route) {
281273
}
282274
}
283275

284-
private void checkIfOffRoute() throws ServicesException, TurfException {
276+
private void checkIfOffRoute() {
285277

286278
Position carCurrentPosition = Position.fromCoordinates(
287279
car.getPosition().getLongitude(),
@@ -344,12 +336,7 @@ public void run() {
344336
// Check that the vehicles off route or not. If you aren't simulating the car,
345337
// and want to use this example in the real world, the checkingIfOffRoute method
346338
// should go in a locationListener.
347-
try {
348-
checkIfOffRoute();
349-
} catch (ServicesException | TurfException exception) {
350-
exception.printStackTrace();
351-
Log.e(TAG, "check if off route error: " + exception.getMessage());
352-
}
339+
checkIfOffRoute();
353340

354341
// Keeping the current point count we are on.
355342
count++;

mapbox/app/src/main/java/com/mapbox/services/android/testapp/nav/SnapToRouteActivity.java

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
2121
import com.mapbox.services.Constants;
2222
import com.mapbox.services.android.testapp.R;
23-
import com.mapbox.services.api.ServicesException;
2423
import com.mapbox.services.api.directions.v5.DirectionsCriteria;
2524
import com.mapbox.services.api.directions.v5.MapboxDirections;
2625
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
2726
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
2827
import com.mapbox.services.api.navigation.v5.RouteUtils;
29-
import com.mapbox.services.api.utils.turf.TurfException;
3028
import com.mapbox.services.api.utils.turf.TurfMisc;
3129
import com.mapbox.services.commons.geojson.LineString;
3230
import com.mapbox.services.commons.geojson.Point;
@@ -120,19 +118,11 @@ public void onMapClick(@NonNull LatLng point) {
120118
.position(point)
121119
);
122120

123-
RouteUtils routeUtils = new RouteUtils();
124-
Position snappedPosition = null;
125-
126-
try {
127-
snappedPosition = routeUtils.getSnapToRoute(
128-
Position.fromCoordinates(point.getLongitude(), point.getLatitude()),
129-
currentRoute.getLegs().get(0),
130-
stepCount
131-
);
132-
} catch (ServicesException | TurfException exception) {
133-
exception.printStackTrace();
134-
Log.e(TAG, "snap to route util: " + exception.getMessage());
135-
}
121+
Position snappedPosition = RouteUtils.getSnapToRoute(
122+
Position.fromCoordinates(point.getLongitude(), point.getLatitude()),
123+
currentRoute.getLegs().get(0),
124+
stepCount
125+
);
136126

137127
if (snappedPosition == null) {
138128
Log.i(TAG, "snapPosition is null");
@@ -155,39 +145,31 @@ public void onMapClick(@NonNull LatLng point) {
155145
mapboxMap.removePolyline(distancePolyline);
156146
}
157147

158-
try {
159-
LineString slicedLine = TurfMisc.lineSlice(
160-
Point.fromCoordinates(snappedPosition),
161-
Point.fromCoordinates(coords.get(coords.size() - 1)),
162-
LineString.fromCoordinates(coords)
163-
);
164-
165-
List<Position> linePositions = slicedLine.getCoordinates();
166-
List<LatLng> lineLatLng = new ArrayList<>();
167-
for (Position pos : linePositions) {
168-
lineLatLng.add(new LatLng(pos.getLatitude(), pos.getLongitude()));
169-
}
148+
LineString slicedLine = TurfMisc.lineSlice(
149+
Point.fromCoordinates(snappedPosition),
150+
Point.fromCoordinates(coords.get(coords.size() - 1)),
151+
LineString.fromCoordinates(coords)
152+
);
170153

171-
distancePolyline = mapboxMap.addPolyline(new PolylineOptions()
172-
.addAll(lineLatLng)
173-
.color(Color.parseColor("#f1f075"))
174-
.width(5f)
175-
);
176-
} catch (TurfException turfException) {
177-
turfException.printStackTrace();
154+
List<Position> linePositions = slicedLine.getCoordinates();
155+
List<LatLng> lineLatLng = new ArrayList<>();
156+
for (Position pos : linePositions) {
157+
lineLatLng.add(new LatLng(pos.getLatitude(), pos.getLongitude()));
178158
}
159+
160+
distancePolyline = mapboxMap.addPolyline(new PolylineOptions()
161+
.addAll(lineLatLng)
162+
.color(Color.parseColor("#f1f075"))
163+
.width(5f)
164+
);
179165
}
180166

181167
@Override
182168
public void onMapReady(MapboxMap mapboxMap) {
183169
SnapToRouteActivity.this.mapboxMap = mapboxMap;
184170
mapboxMap.setOnMapClickListener(this);
185171

186-
try {
187-
getRoute(origin, destination);
188-
} catch (ServicesException servicesException) {
189-
servicesException.printStackTrace();
190-
}
172+
getRoute(origin, destination);
191173
}
192174

193175
private void drawDistanceRoutePolyline(Position snappedPosition) {
@@ -199,30 +181,26 @@ private void drawDistanceRoutePolyline(Position snappedPosition) {
199181
mapboxMap.removePolyline(distanceRoutePolyline);
200182
}
201183

202-
try {
203-
LineString slicedRouteLine = TurfMisc.lineSlice(
204-
Point.fromCoordinates(snappedPosition),
205-
Point.fromCoordinates(routeCoords.get(routeCoords.size() - 1)),
206-
LineString.fromCoordinates(routeCoords)
207-
);
208-
209-
List<Position> linePositions = slicedRouteLine.getCoordinates();
210-
List<LatLng> lineLatLng = new ArrayList<>();
211-
for (Position pos : linePositions) {
212-
lineLatLng.add(new LatLng(pos.getLatitude(), pos.getLongitude()));
213-
}
184+
LineString slicedRouteLine = TurfMisc.lineSlice(
185+
Point.fromCoordinates(snappedPosition),
186+
Point.fromCoordinates(routeCoords.get(routeCoords.size() - 1)),
187+
LineString.fromCoordinates(routeCoords)
188+
);
214189

215-
distanceRoutePolyline = mapboxMap.addPolyline(new PolylineOptions()
216-
.addAll(lineLatLng)
217-
.color(Color.parseColor("#3887be"))
218-
.width(5f)
219-
);
220-
} catch (TurfException turfException) {
221-
turfException.printStackTrace();
190+
List<Position> linePositions = slicedRouteLine.getCoordinates();
191+
List<LatLng> lineLatLng = new ArrayList<>();
192+
for (Position pos : linePositions) {
193+
lineLatLng.add(new LatLng(pos.getLatitude(), pos.getLongitude()));
222194
}
195+
196+
distanceRoutePolyline = mapboxMap.addPolyline(new PolylineOptions()
197+
.addAll(lineLatLng)
198+
.color(Color.parseColor("#3887be"))
199+
.width(5f)
200+
);
223201
}
224202

225-
private void getRoute(Position origin, Position destination) throws ServicesException {
203+
private void getRoute(Position origin, Position destination) {
226204

227205
MapboxDirections client = new MapboxDirections.Builder()
228206
.setOrigin(origin)

mapbox/app/src/main/java/com/mapbox/services/android/testapp/staticimage/StaticImageActivity.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.support.v7.widget.GridLayoutManager;
77
import android.support.v7.widget.RecyclerView;
8-
import android.util.Log;
98

109
import com.mapbox.services.Constants;
1110
import com.mapbox.services.android.testapp.R;
1211
import com.mapbox.services.android.testapp.Utils;
13-
import com.mapbox.services.api.ServicesException;
1412
import com.mapbox.services.api.staticimage.v1.MapboxStaticImage;
1513

1614
public class StaticImageActivity extends AppCompatActivity {
1715

18-
private static final String LOG_TAG = "StaticImageActivity";
19-
2016
// All default Mapbox styles
2117
public static final String[] MAPBOX_STYLES = {
2218
Constants.MAPBOX_STYLE_STREETS, Constants.MAPBOX_STYLE_LIGHT,
@@ -66,22 +62,16 @@ private String[] buildDataset() {
6662
int i = 0;
6763
for (double[] place : PLACES) {
6864
for (String style : MAPBOX_STYLES) {
69-
String imageUrl = null;
70-
try {
71-
imageUrl = getImageUrl(style, place, isRetina);
72-
dataset[i] = imageUrl;
73-
i++;
74-
} catch (ServicesException servicesException) {
75-
Log.e(LOG_TAG, "Error: " + servicesException.getMessage());
76-
servicesException.printStackTrace();
77-
}
65+
String imageUrl = getImageUrl(style, place, isRetina);
66+
dataset[i] = imageUrl;
67+
i++;
7868
}
7969
}
8070

8171
return dataset;
8272
}
8373

84-
private String getImageUrl(String style, double[] place, boolean isRetina) throws ServicesException {
74+
private String getImageUrl(String style, double[] place, boolean isRetina) {
8575
MapboxStaticImage staticImage = new MapboxStaticImage.Builder()
8676
.setAccessToken(Utils.getMapboxAccessToken(this))
8777
.setUsername(Constants.MAPBOX_USER)

0 commit comments

Comments
 (0)