Skip to content

Commit 98e0d69

Browse files
committed
bug fixed
1 parent bd0aa86 commit 98e0d69

File tree

13 files changed

+90
-54
lines changed

13 files changed

+90
-54
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 32 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GoogleDirectionApi/src/main/java/com/codebyashish/googledirectionapi/AbstractRouting.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import android.os.AsyncTask;
44

5-
import com.codebyashish.googledirectionapi.modelclass.RouteInfoModel;
6-
import com.codebyashish.googledirectionapi.utilities.ErrorHandling;
7-
import com.codebyashish.googledirectionapi.utilities.RouteListener;
85
import com.google.android.gms.maps.model.LatLng;
96
import com.google.android.gms.maps.model.PolylineOptions;
107

@@ -65,7 +62,7 @@ protected ArrayList<RouteInfoModel> doInBackground(Void... voids) {
6562
return result;
6663
}
6764

68-
protected abstract String constructURL();
65+
protected abstract String constructURL() throws ErrorHandling;
6966

7067
protected void onPreExecute() {
7168
this.dispatchOnStart();
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.codebyashish.googledirectionapi.utilities;
1+
package com.codebyashish.googledirectionapi;
22

33
public class Constants {
44

@@ -7,5 +7,8 @@ public class Constants {
77
public static String START_ADDRESS = "start_address";
88
public static String END_ADDRESS = "end_address";
99
public static String OK = "OK";
10+
public static String getTutorial() {
11+
return " To fix this, please watch tutorial - https://youtu.be/DRaRStNyZ0k";
12+
}
1013

1114
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.codebyashish.googledirectionapi.utilities;
1+
package com.codebyashish.googledirectionapi;
2+
3+
import static com.codebyashish.googledirectionapi.Constants.getTutorial;
24

35
import android.util.Log;
46

@@ -17,9 +19,9 @@ public ErrorHandling(JSONObject json) {
1719
} else {
1820
try {
1921
this.statusCode = json.getString("status");
20-
this.message = json.getString("error_message");
22+
this.message = json.getString("error_message") + getTutorial();
2123
} catch (JSONException e) {
22-
Log.e("route json error" , "JSON parsing error : " + e.getMessage());
24+
Log.e("route json error" , "JSON parsing error : " + e.getMessage() + getTutorial());
2325
}
2426

2527
}

GoogleDirectionApi/src/main/java/com/codebyashish/googledirectionapi/RouteDrawing.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
package com.codebyashish.googledirectionapi;
22

3+
import static com.codebyashish.googledirectionapi.Constants.getTutorial;
4+
35
import android.content.Context;
46
import android.content.pm.ApplicationInfo;
57
import android.content.pm.PackageManager;
68
import android.os.Bundle;
79

8-
import com.codebyashish.googledirectionapi.utilities.Constants;
9-
import com.codebyashish.googledirectionapi.utilities.RouteListener;
1010
import com.google.android.gms.maps.model.LatLng;
1111

1212
import java.util.ArrayList;
1313
import java.util.Collections;
1414
import java.util.List;
1515

1616
public class RouteDrawing extends AbstractRouting {
17-
private final AbstractRouting.TravelMode travelMode;
17+
private final TravelMode travelMode;
1818
private final boolean alternativeRoutes;
1919
private final List<LatLng> waypoints;
2020
private final int avoidKinds;
2121
private final boolean optimize;
2222
private final String language;
23-
private String key;
2423
private final Context context;
2524

2625
private RouteDrawing(Builder builder) {
2726
super(builder.listener);
27+
this.context = builder.context;
2828
this.travelMode = builder.travelMode;
2929
this.waypoints = builder.waypoints;
3030
this.avoidKinds = builder.avoidKinds;
3131
this.optimize = builder.optimize;
3232
this.alternativeRoutes = builder.alternativeRoutes;
3333
this.language = builder.language;
34-
// this.key = builder.key;
35-
this.context = builder.context;
34+
3635
}
3736

38-
protected String constructURL() {
37+
protected String constructURL() throws ErrorHandling {
3938
StringBuilder stringBuilder = new StringBuilder(Constants.DIRECTION_BASE_URL);
4039
LatLng origin = this.waypoints.get(0);
4140
stringBuilder.append("origin=");
@@ -79,22 +78,28 @@ protected String constructURL() {
7978
stringBuilder.append("&language=").append(this.language);
8079
}
8180

82-
if (this.key != null) {
83-
stringBuilder.append("&key=").append(getMapKey(context));
81+
if (isGoogleMapsLinked(context)){
82+
if (getMapKey(context) != null){
83+
stringBuilder.append("&key=").append(getMapKey(context));
84+
} else {
85+
throw new ErrorHandling("This application is not authorized to use this API key" + getTutorial());
86+
}
87+
} else {
88+
throw new ErrorHandling("Your project is not linked with google cloud platform" + getTutorial());
8489
}
8590

91+
8692
return stringBuilder.toString();
8793
}
8894

8995
public static class Builder {
90-
private AbstractRouting.TravelMode travelMode;
96+
private TravelMode travelMode;
9197
private boolean alternativeRoutes;
9298
private List<LatLng> waypoints;
9399
private int avoidKinds;
94100
private RouteListener listener;
95101
private boolean optimize;
96102
private String language;
97-
private String key;
98103
private Context context;
99104

100105
public Builder() {
@@ -105,11 +110,10 @@ public Builder() {
105110
this.listener = null;
106111
this.optimize = false;
107112
this.language = null;
108-
// this.key = null;
109113
this.context = null;
110114
}
111115

112-
public Builder travelMode(AbstractRouting.TravelMode travelMode) {
116+
public Builder travelMode(TravelMode travelMode) {
113117
this.travelMode = travelMode;
114118
return this;
115119
}
@@ -141,11 +145,6 @@ public Builder optimize(boolean optimize) {
141145
return this;
142146
}
143147

144-
/* public Builder key(String key) {
145-
this.key = key;
146-
return this;
147-
}*/
148-
149148
public Builder context(Context context) {
150149
this.context = context;
151150
return this;
@@ -167,6 +166,7 @@ public RouteDrawing build() {
167166
}
168167
}
169168

169+
/** Check for Map API Key*/
170170
private static String getMapKey(Context context) {
171171
PackageManager packageManager = context.getPackageManager();
172172
String apiKey = null;
@@ -184,4 +184,23 @@ private static String getMapKey(Context context) {
184184

185185
return apiKey;
186186
}
187+
188+
/** Check for Map SDK Integration*/
189+
private static boolean isGoogleMapsLinked(Context context) {
190+
PackageManager packageManager = context.getPackageManager();
191+
String apiKey = null;
192+
193+
try {
194+
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
195+
Bundle metaData = applicationInfo.metaData;
196+
197+
if (metaData != null && metaData.containsKey("com.google.android.geo.API_KEY")) {
198+
apiKey = metaData.getString("com.google.android.geo.API_KEY");
199+
}
200+
} catch (PackageManager.NameNotFoundException e) {
201+
e.printStackTrace();
202+
}
203+
204+
return (apiKey != null);
205+
}
187206
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.codebyashish.googledirectionapi.modelclass;
1+
package com.codebyashish.googledirectionapi;
22

33
import com.google.android.gms.maps.model.LatLng;
44
import com.google.android.gms.maps.model.LatLngBounds;

GoogleDirectionApi/src/main/java/com/codebyashish/googledirectionapi/RouteJsonParser.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
import android.util.Log;
44

5-
import com.codebyashish.googledirectionapi.modelclass.RouteInfoModel;
6-
import com.codebyashish.googledirectionapi.modelclass.StepsModel;
7-
import com.codebyashish.googledirectionapi.utilities.Constants;
8-
import com.codebyashish.googledirectionapi.utilities.ErrorHandling;
9-
import com.codebyashish.googledirectionapi.utilities.XMLParser;
105
import com.google.android.gms.maps.model.LatLng;
116

127
import org.json.JSONArray;
@@ -102,14 +97,14 @@ private static String convertStreamToString(InputStream input) {
10297
sBuf.append(line);
10398
}
10499
} catch (IOException e) {
105-
Log.e("RouteDrawing Error", e.getMessage());
100+
Log.e("RouteDrawing Error", e.getMessage() + " please watch tutorial for more - https://youtu.be/DRaRStNyZ0k");
106101
}
107102
} finally {
108103
try {
109104
input.close();
110105
reader.close();
111106
} catch (IOException e) {
112-
Log.e("RouteDrawing Error", e.getMessage());
107+
Log.e("RouteDrawing Error", e.getMessage() + " please watch tutorial for more - https://youtu.be/DRaRStNyZ0k");
113108
}
114109

115110
}

0 commit comments

Comments
 (0)