Skip to content

Commit

Permalink
minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dangiashish committed Feb 17, 2023
1 parent c519b0e commit 901c053
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 66 deletions.
23 changes: 19 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

public abstract class AbstractRouting extends AsyncTask<Void, Void, ArrayList<RouteInfoModel>> {
protected ArrayList<RouteListener> listenerArrayList = new ArrayList<>();
protected static final String DIRECTIONS_API_URL = "https://maps.googleapis.com/maps/api/directions/json?";
private Exceptions exceptions = null;
private ErrorHandling errorHandling = null;

protected AbstractRouting(RouteListener listener) {
this.registerListener(listener);
Expand All @@ -30,7 +29,7 @@ protected void dispatchOnStart() {

}

protected void dispatchOnFailure(Exceptions exception) {
protected void dispatchOnFailure(ErrorHandling exception) {
for (RouteListener mListener : this.listenerArrayList) {
mListener.onRouteFailure(exception);
}
Expand All @@ -55,9 +54,9 @@ protected ArrayList<RouteInfoModel> doInBackground(Void... voids) {
ArrayList<RouteInfoModel> result = new ArrayList<>();

try {
result = (new GoogleParser(this.constructURL())).parse();
} catch (Exceptions var4) {
this.exceptions = var4;
result = (new RouteJsonParser(this.constructURL())).parse();
} catch (ErrorHandling errorHandling) {
this.errorHandling = errorHandling;
}

return result;
Expand All @@ -75,23 +74,23 @@ protected void onPostExecute(ArrayList<RouteInfoModel> result) {
int minDistance = Integer.MAX_VALUE;

for(int i = 0; i < result.size(); ++i) {
PolylineOptions mOptions = new PolylineOptions();
PolylineOptions polylineOptions = new PolylineOptions();
RouteInfoModel routeInfoModel = result.get(i);
if (routeInfoModel.getLength() < minDistance) {
shortestRouteIndex = i;
minDistance = routeInfoModel.getLength();
}

for (LatLng point : routeInfoModel.getPoints()) {
mOptions.add(point);
polylineOptions.add(point);
}

result.get(i).setPolyOptions(mOptions);
result.get(i).setPolyOptions(polylineOptions);
}

this.dispatchOnSuccess(result, shortestRouteIndex);
} else {
this.dispatchOnFailure(this.exceptions);
this.dispatchOnFailure(this.errorHandling);
}

}
Expand All @@ -105,25 +104,25 @@ public enum AvoidKind {
HIGHWAYS(2, "highways"),
FERRIES(4, "ferries");

private final String _sRequestParam;
private final int _sBitValue;
private final String param;
private final int bit;

AvoidKind(int bit, String param) {
this._sBitValue = bit;
this._sRequestParam = param;
this.bit = bit;
this.param = param;
}

int getBitValue() {
return this._sBitValue;
return this.bit;
}

static String getRequestParam(int bit) {
String ret = "";
AvoidKind[] arr$ = values();

for (AvoidKind kind : arr$) {
if ((bit & kind._sBitValue) == kind._sBitValue) {
ret = ret + kind._sRequestParam;
if ((bit & kind.bit) == kind.bit) {
ret = ret + kind.param;
ret = ret + "|";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import org.json.JSONException;
import org.json.JSONObject;

public class Exceptions extends Exception {
public class ErrorHandling extends Exception {
private String statusCode;
private String message;
private int status;

public Exceptions(JSONObject json) {
public ErrorHandling(JSONObject json) {
if (json == null) {
this.statusCode = "";
this.message = "Cannot parse";
Expand All @@ -18,13 +19,17 @@ public Exceptions(JSONObject json) {
this.statusCode = json.getString("status");
this.message = json.getString("error_message");
} catch (JSONException e) {
Log.e("RouteInfoModel json error" , "JSON parsing error : " + e.getMessage());
Log.e("route json error" , "JSON parsing error : " + e.getMessage());
}

}
}

public Exceptions(String msg) {
public ErrorHandling(int status){
this.status = status;
}

public ErrorHandling(String msg) {
this.message = msg;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codebyashish.googledirectionapi;

public class KeyConstants {

public static String DIRECTION_BASE_URL = "https://maps.googleapis.com/maps/api/directions/json?";
public static String START_LOCATION = "start_location";
public static String START_ADDRESS = "start_address";
public static String END_ADDRESS = "end_address";
public static String OK = "OK";

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private RouteDrawing(Builder builder) {
}

protected String constructURL() {
StringBuilder stringBuilder = new StringBuilder("https://maps.googleapis.com/maps/api/directions/json?");
StringBuilder stringBuilder = new StringBuilder(KeyConstants.DIRECTION_BASE_URL);
LatLng origin = this.waypoints.get(0);
stringBuilder.append("origin=");
stringBuilder.append(origin.latitude);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class RouteInfoModel {
private String name;
private final List<LatLng> points = new ArrayList<>();
private List<Segment> segments = new ArrayList<>();
private List<StepsModel> stepsModels = new ArrayList<>();
private String copyright;
private String warning;
private String country;
Expand Down Expand Up @@ -72,8 +72,8 @@ public void setDistanceValue(int distanceValue) {
this.distanceValue = distanceValue;
}

public void setSegments(List<Segment> segments) {
this.segments = segments;
public void setSegments(List<StepsModel> stepsModels) {
this.stepsModels = stepsModels;
}

public RouteInfoModel() {
Expand All @@ -91,12 +91,12 @@ public List<LatLng> getPoints() {
return this.points;
}

public void addSegment(Segment s) {
this.segments.add(s);
public void addSegment(StepsModel s) {
this.stepsModels.add(s);
}

public List<Segment> getSegments() {
return this.segments;
public List<StepsModel> getSegments() {
return this.stepsModels;
}

public void setName(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.codebyashish.googledirectionapi;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import android.util.Log;

import com.google.android.gms.maps.model.LatLng;
Expand All @@ -21,38 +15,38 @@
import java.util.ArrayList;
import java.util.List;

public class GoogleParser extends XMLParser {
public class RouteJsonParser extends XMLParser {
private int distance;
private String OK = "OK";

public GoogleParser(String feedUrl) {

public RouteJsonParser(String feedUrl) {
super(feedUrl);
}

public ArrayList<RouteInfoModel> parse() throws Exceptions {
public ArrayList<RouteInfoModel> parse() throws ErrorHandling {
ArrayList<RouteInfoModel> routeInfoModels = new ArrayList<>();
String result = convertStreamToString(this.getInputStream());
if (result == null) {
throw new Exceptions("Result is null");
throw new ErrorHandling("No result found");
} else {
try {
JSONObject json = new JSONObject(result);
if (!json.getString("status").equals(this.OK)) {
throw new Exceptions(json);
if (!json.getString("status").equals(KeyConstants.OK)) {
throw new ErrorHandling(json);
} else {
JSONArray jsonRoutes = json.getJSONArray("routes");
for(int i = 0; i < jsonRoutes.length(); ++i) {
RouteInfoModel routeInfoModel = new RouteInfoModel();
Segment segment = new Segment();
StepsModel stepsModel = new StepsModel();
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
JSONObject jsonBounds = jsonRoute.getJSONObject("bounds");
JSONObject jsonNortheast = jsonBounds.getJSONObject("northeast");
JSONObject jsonSouthwest = jsonBounds.getJSONObject("southwest");
routeInfoModel.setLatLgnBounds(new LatLng(jsonNortheast.getDouble("lat"), jsonNortheast.getDouble("lng")), new LatLng(jsonSouthwest.getDouble("lat"), jsonSouthwest.getDouble("lng")));
JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");
int numSteps = steps.length();
routeInfoModel.setName(leg.getString("start_address") + " to " + leg.getString("end_address"));

routeInfoModel.setName(leg.getString(KeyConstants.START_ADDRESS) + " to " + leg.getString(KeyConstants.END_ADDRESS));
routeInfoModel.setCopyright(jsonRoute.getString("copyrights"));
routeInfoModel.setDurationText(leg.getJSONObject("duration").getString("text"));
routeInfoModel.setDurationValue(leg.getJSONObject("duration").getInt("value"));
Expand All @@ -64,18 +58,18 @@ public ArrayList<RouteInfoModel> parse() throws Exceptions {
routeInfoModel.setWarning(jsonRoute.getJSONArray("warnings").getString(0));
}

for(int y = 0; y < numSteps; ++y) {
for(int y = 0; y < steps.length(); ++y) {
JSONObject step = steps.getJSONObject(y);
JSONObject start = step.getJSONObject("start_location");
JSONObject start = step.getJSONObject(KeyConstants.START_LOCATION);
LatLng position = new LatLng(start.getDouble("lat"), start.getDouble("lng"));
segment.setPoint(position);
stepsModel.setPoint(position);
int length = step.getJSONObject("distance").getInt("value");
this.distance += length;
segment.setLength(length);
segment.setDistance((double)this.distance / 1000.0);
segment.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", ""));
stepsModel.setLength(length);
stepsModel.setDistance((double)this.distance / 1000.0);
stepsModel.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", ""));
routeInfoModel.addPoints(this.decodePolyLine(step.getJSONObject("polyline").getString("points")));
routeInfoModel.addSegment(segment.copy());
routeInfoModel.addSegment(stepsModel.copy());
}

routeInfoModels.add(routeInfoModel);
Expand All @@ -84,7 +78,7 @@ public ArrayList<RouteInfoModel> parse() throws Exceptions {
return routeInfoModels;
}
} catch (JSONException var20) {
throw new Exceptions("JSONException. Msg: " + var20.getMessage());
throw new ErrorHandling("JSONException. Msg: " + var20.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;

public interface RouteListener {
void onRouteFailure(Exceptions e);
void onRouteFailure(ErrorHandling e);

void onRouteStart();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import com.google.android.gms.maps.model.LatLng;

public class Segment {
public class StepsModel {
private LatLng start;
private String instruction;
private int length;
private double distance;

public Segment() {
public StepsModel() {
}

public void setInstruction(String turn) {
Expand All @@ -28,8 +28,8 @@ public LatLng startPoint() {
return this.start;
}

public Segment copy() {
Segment copy = new Segment();
public StepsModel copy() {
StepsModel copy = new StepsModel();
copy.start = this.start;
copy.instruction = this.instruction;
copy.length = this.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class XMLParser {
protected XMLParser(String feedUrl) {
try {
this.feedUrl = new URL(feedUrl);
} catch (MalformedURLException var3) {
Log.e("RouteDrawing Error", var3.getMessage());
} catch (MalformedURLException e) {
Log.e("RouteDrawing Error", e.getMessage());
}

}

protected InputStream getInputStream() {
try {
return this.feedUrl.openConnection().getInputStream();
} catch (IOException var2) {
Log.e("RouteDrawing Error", var2.getMessage());
} catch (IOException e) {
Log.e("RouteDrawing Error", e.getMessage());
return null;
}
}
Expand Down

0 comments on commit 901c053

Please sign in to comment.