diff --git a/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java index 5fded7c..1f0259c 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java @@ -45,6 +45,10 @@ public class RouteDetailFragment extends Fragment { return view; } + /** + * Button onclick method that starts the route that is being viewed. + * @param view the button + */ public void startRoute(View view) { ApiHandler.INSTANCE.getDirections(route); RouteHandler.INSTANCE.followRoute(route); diff --git a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java index 46d3830..61f3176 100644 --- a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java +++ b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java @@ -20,43 +20,72 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +/** + * Singleton that handles all API calls + */ public enum ApiHandler { INSTANCE; private static String TAG = ApiHandler.class.getCanonicalName(); + public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private final String BASE_URL = "https://api.openrouteservice.org/v2/directions/"; private final String API_KEY = "5b3ce3597851110001cf6248d4eee2099f724255918adc71cc502b2a"; private final String DIRECTIONS_MODE = "foot-walking"; + private List listeners = new ArrayList<>(); private OkHttpClient client = new OkHttpClient(); + /** + * gets directions from the start location to the end location + * @param startLocation the start location + * @param endLocation the end location + */ public void getDirections(Location startLocation, Location endLocation) { getDirections(startLocation.getCoordinates(),endLocation.getCoordinates()); } + /** + * gets directions from the start location latitude and longitude and the end location latitude and longitude + * @param startLat the start latitude + * @param startLong the start longitude + * @param endLat the end latitude + * @param endLong the end longitude + */ public void getDirections(double startLat, double startLong, double endLat, double endLong) { getDirections(startLong + "," + startLat, endLong + "," + endLat); } + /** + * Gets the directions from the start location to the end location. An example location would be "3.543543,5.7675765" + * @param startLocation the start location represented as startlat,startlong + * @param endLocation the end location represented as endlat,endlong + */ public void getDirections(String startLocation, String endLocation) { + // build the url String requestUrl = BASE_URL + DIRECTIONS_MODE + "?api_key=" + API_KEY + "&start=" +startLocation + "&end=" + endLocation; + + // start a new thread to do the request, because we don't want to be networking on our main thread Thread t = new Thread(() -> { + // build a request from the url Request request = new Request.Builder().url(requestUrl).build(); + // try to get the response try (Response response = client.newCall(request).execute()) { if (response.body() != null) { String responseString = Objects.requireNonNull(response.body()).string(); Log.d(TAG, "getDirections: got response: " + responseString); + // convert the response into a result object DirectionsResult result = new DirectionsResult(); result.parse(responseString); Log.d(TAG, "getDirections: " + result.getSteps().size()); + // notify the listeners for (DirectionsListener listener : listeners) { listener.onDirectionsAvailable(result); } @@ -77,29 +106,35 @@ public enum ApiHandler { } + /** + * adds a listener for when a result of an api call is available + * @param listener the new listener + */ public void addListener(DirectionsListener listener) { this.listeners.add(listener); } + /** + * gets directions for the given {@link Route} + * @param route the route to get directions for + */ public void getDirections(Route route) { -// for (int i = 0; i < route.getLocations().size()-1; i+= 2) { -// Location start = route.getLocations().get(i); -// Location end = route.getLocations().get(i+1); -// getDirections(start,end); -// } + // add the coordinates to the list ArrayList allCoords = new ArrayList<>(); for (Location location : route.getLocations()) { allCoords.add(location.getCoordinatesAsDoubles()); } + // convert it to a json string String body = "{\"coordinates\":" + new Gson().toJson(allCoords) + "}"; - String requestUrl = BASE_URL + DIRECTIONS_MODE + "?api_key=" + API_KEY; + // new thread, because we don't want to be networking on our main thread Thread t = new Thread(() -> { + // add the body to the request RequestBody requestBody = RequestBody.create(body,JSON); Request request = new Request.Builder() .url(requestUrl) @@ -110,15 +145,19 @@ public enum ApiHandler { if (response.body() != null) { String responseString = Objects.requireNonNull(response.body()).string(); Log.d(TAG, "getDirections: got response: " + responseString); + + // kinda lazy, but we had time pressure if (responseString.startsWith("{\"error")) { Log.e(TAG, "getDirections: ERROR IN REQUEST!"); return; } + // convert the result into a DirectionsResult DirectionsResult result = new DirectionsResult(); result.parseRoute(responseString); Log.d(TAG, "getDirections: " + result.getSteps().size()); + //notify the listeners for (DirectionsListener listener : listeners) { listener.onDirectionsAvailable(result); } @@ -132,11 +171,12 @@ public enum ApiHandler { t.start(); - try { - t.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } +// +// try { +// t.join(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } }