diff --git a/mapbox/build.gradle b/mapbox/build.gradle index f74e54b7d..3c89a224c 100644 --- a/mapbox/build.gradle +++ b/mapbox/build.gradle @@ -1,34 +1,35 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - repositories { - jcenter() - mavenCentral() - maven { url "http://oss.sonatype.org/content/repositories/snapshots/" } - } + repositories { + jcenter() + mavenCentral() + maven { url "http://oss.sonatype.org/content/repositories/snapshots/" } + maven { url "https://plugins.gradle.org/m2/" } + } - dependencies { - classpath 'com.android.tools.build:gradle:2.3.2' - classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2' + dependencies { + classpath 'com.android.tools.build:gradle:2.3.3' + classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2' - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } } allprojects { - repositories { - jcenter() - mavenCentral() - maven { url "http://oss.sonatype.org/content/repositories/snapshots/" } - } + repositories { + jcenter() + mavenCentral() + maven { url "http://oss.sonatype.org/content/repositories/snapshots/" } + } - group = GROUP - version = VERSION_NAME + group = GROUP + version = VERSION_NAME } task clean(type: Delete) { - delete rootProject.buildDir + delete rootProject.buildDir } -apply from: rootProject.file('dependencies.gradle') +apply from: rootProject.file('dependencies.gradle') \ No newline at end of file diff --git a/mapbox/dependencies.gradle b/mapbox/dependencies.gradle index 24f398e86..da533b63d 100644 --- a/mapbox/dependencies.gradle +++ b/mapbox/dependencies.gradle @@ -20,7 +20,7 @@ ext { mapbox : 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar', // gson - gson : 'com.google.code.gson:gson:2.8.0', + gson : 'com.google.code.gson:gson:2.8.1', // timber timber : 'com.jakewharton.timber:timber:4.5.1', @@ -30,6 +30,10 @@ ext { retrofit2Gson : 'com.squareup.retrofit2:converter-gson:2.2.0', retrofit2Rx : 'com.squareup.retrofit2:adapter-rxjava2:2.2.0', + // auto value + autoValues : 'com.google.auto.value:auto-value:1.4.1', + autoValuesGson : 'com.ryanharter.auto.value:auto-value-gson:0.4.6', + // okhttp okhttp3 : 'com.squareup.okhttp3:okhttp:3.6.0', okhttp3Logging : 'com.squareup.okhttp3:logging-interceptor:3.6.0', diff --git a/mapbox/libjava-services/build.gradle b/mapbox/libjava-services/build.gradle index 77cfbb8d4..7fa232878 100644 --- a/mapbox/libjava-services/build.gradle +++ b/mapbox/libjava-services/build.gradle @@ -1,21 +1,29 @@ +plugins { id "net.ltgt.apt" version "0.10" } apply plugin: 'java' +apply plugin: 'idea' sourceCompatibility = "1.7" targetCompatibility = "1.7" dependencies { - // Mapbox Java Services requires GeoJSON support - compile project(':libjava-geojson') + // Mapbox Java Services requires GeoJSON support + compile project(':libjava-geojson') - // Retrofit - compile rootProject.ext.dep.retrofit2Main - compile rootProject.ext.dep.retrofit2Gson - compile rootProject.ext.dep.okhttp3Logging + // Retrofit + compile rootProject.ext.dep.retrofit2Main + compile rootProject.ext.dep.retrofit2Gson + compile rootProject.ext.dep.okhttp3Logging - // Testing - testCompile rootProject.ext.dep.okhttp3Mockwebserver - testCompile rootProject.ext.dep.junit - testCompile rootProject.ext.dep.hamcrestJunit + // AutoValues + compileOnly rootProject.ext.dep.autoValuesGson + apt rootProject.ext.dep.autoValuesGson +// compileOnly rootProject.ext.dep.autoValues +// apt rootProject.ext.dep.autoValues + + // Testing + testCompile rootProject.ext.dep.okhttp3Mockwebserver + testCompile rootProject.ext.dep.junit + testCompile rootProject.ext.dep.hamcrestJunit } apply from: 'gradle-javadoc.gradle' diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/DirectionsTypeAdapterFactory.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/DirectionsTypeAdapterFactory.java new file mode 100644 index 000000000..ad0087f9d --- /dev/null +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/DirectionsTypeAdapterFactory.java @@ -0,0 +1,27 @@ +package com.mapbox.services.api.directions; + +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.mapbox.services.api.directions.v5.models.DirectionsResponse; +import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory; + +@GsonTypeAdapterFactory +public abstract class DirectionsTypeAdapterFactory implements TypeAdapterFactory { + + public static DirectionsTypeAdapterFactory create() { + return new AutoValueGson_DirectionsTypeAdapterFactory(); + } + + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + Class rawType = type.getRawType(); + if (rawType.equals(DirectionsResponse.class)) { + return (TypeAdapter) DirectionsResponse.typeAdapter(gson); + } + return null; + } +} + diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java index dddec9f28..b453d4cd2 100644 --- a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java @@ -1,8 +1,11 @@ package com.mapbox.services.api.directions.v5; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.mapbox.services.api.MapboxBuilder; import com.mapbox.services.api.MapboxService; import com.mapbox.services.api.ServicesException; +import com.mapbox.services.api.directions.DirectionsTypeAdapterFactory; import com.mapbox.services.api.directions.v5.models.DirectionsResponse; import com.mapbox.services.commons.models.Position; import com.mapbox.services.commons.utils.TextUtils; @@ -30,11 +33,22 @@ public class MapboxDirections extends MapboxService { protected Builder builder = null; private DirectionsService service = null; private Call call = null; + private Gson gson; protected MapboxDirections(Builder builder) { this.builder = builder; } + protected Gson getGson() { + // Gson instance with type adapters + if (gson == null) { + gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsTypeAdapterFactory.create()) + .create(); + } + return gson; + } + private DirectionsService getService() { // No need to recreate it if (service != null) { @@ -44,7 +58,7 @@ private DirectionsService getService() { // Retrofit instance Retrofit.Builder retrofitBuilder = new Retrofit.Builder() .baseUrl(builder.getBaseUrl()) - .addConverterFactory(GsonConverterFactory.create()); + .addConverterFactory(GsonConverterFactory.create(getGson())); if (getCallFactory() != null) { retrofitBuilder.callFactory(getCallFactory()); } else { diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsResponse.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsResponse.java index ec6629991..0d94efcbe 100644 --- a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsResponse.java +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsResponse.java @@ -1,5 +1,9 @@ package com.mapbox.services.api.directions.v5.models; +import com.google.auto.value.AutoValue; +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; + import java.util.List; /** @@ -7,76 +11,35 @@ * * @since 1.0.0 */ -public class DirectionsResponse { - - private String code; - private List routes; - private List waypoints; - - /** - * Empty constructor - * - * @since 2.1.0 - */ - public DirectionsResponse() { - } +@AutoValue +public abstract class DirectionsResponse { /** * Constructor taking in both a list of {@link DirectionsRoute} and a list of {@link DirectionsWaypoint}s. * - * @param routes list of routes you can pass in while building this object. + * @param code {@code String} that indicates the status of the response + * @param routes list of routes you can pass in while building this object * @param waypoints list of waypoints you can pass in while building this object. Ideally these should match what was - * used to crate the route. + * used to crate the route * @since 2.0.0 */ - public DirectionsResponse(List routes, List waypoints) { - this.routes = routes; - this.waypoints = waypoints; + public static DirectionsResponse create(String code, List routes, + List waypoints) { + return new AutoValue_DirectionsResponse(code, routes, waypoints); } - /** - * String indicating the state of the response. This is a separate code than the HTTP - * status code. - * - * @return "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput". - * @since 1.0.0 - */ - public String getCode() { - return code; + public static TypeAdapter typeAdapter(Gson gson) { + return new AutoValue_DirectionsResponse.GsonTypeAdapter(gson); } /** * String indicating the state of the response. This is a separate code than the HTTP * status code. * - * @param code "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput". - * @since 2.1.0 - */ - public void setCode(String code) { - this.code = code; - } - - /** - * List with Waypoints of locations snapped to the road and path network and appear in the List - * in the order of the input coordinates. - * - * @return List of {@link DirectionsWaypoint} objects. + * @return "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput". * @since 1.0.0 */ - public List getWaypoints() { - return waypoints; - } - - /** - * List with Waypoints of locations snapped to the road and path network and should appear in the List - * in the order of the input coordinates. - * - * @param waypoints List of {@link DirectionsWaypoint} objects. - * @since 2.1.0 - */ - public void setWaypoints(List waypoints) { - this.waypoints = waypoints; - } + public abstract String getCode(); /** * List containing all the different route options. It's ordered by descending recommendation @@ -87,20 +50,14 @@ public void setWaypoints(List waypoints) { * @return List of {@link DirectionsRoute} objects. * @since 1.0.0 */ - public List getRoutes() { - return routes; - } + public abstract List getRoutes(); /** - * List containing all the different route options. It should be ordered by descending recommendation - * rank. In other words, object 0 in the List is the highest recommended route. if you don't - * setAlternatives to true (default is false) in your builder this should always be a List of - * size 1. + * List with waypoints of locations snapped to the road and path network and appear in the List + * in the order of the input coordinates. * - * @param routes List of {@link DirectionsRoute} objects. - * @since 2.1.0 + * @return List of {@link DirectionsWaypoint} objects. + * @since 1.0.0 */ - public void setRoutes(List routes) { - this.routes = routes; - } + public abstract List getWaypoints(); } diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsRoute.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsRoute.java index 2a8db9051..5323b6713 100644 --- a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsRoute.java +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/models/DirectionsRoute.java @@ -1,5 +1,8 @@ package com.mapbox.services.api.directions.v5.models; +import com.google.auto.value.AutoValue; +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; import com.google.gson.annotations.SerializedName; import java.util.List; @@ -9,17 +12,16 @@ * * @since 1.0.0 */ -public class DirectionsRoute { +@AutoValue +public abstract class DirectionsRoute { - private double distance; - private double duration; - private String geometry; - private double weight; - @SerializedName("weight_name") - private String weightName; - private List legs; + public static DirectionsRoute create(double distance, String weightName, double weight, double duration, + String geometry, List getLegs) { + return new AutoValue_DirectionsRoute(distance, weightName, weight, duration, geometry, getLegs); + } - public DirectionsRoute() { + public static TypeAdapter typeAdapter(Gson gson) { + return new AutoValue_DirectionsRoute.GsonTypeAdapter(gson); } /** @@ -28,19 +30,7 @@ public DirectionsRoute() { * @return a double number with unit meters. * @since 1.0.0 */ - public double getDistance() { - return distance; - } - - /** - * The distance traveled from origin to destination. - * - * @param distance a double number with unit meters. - * @since 2.1.0 - */ - public void setDistance(double distance) { - this.distance = distance; - } + public abstract double getDistance(); /** * The name of the weight profile used while calculating during extraction phase. In many cases, this will return @@ -51,22 +41,8 @@ public void setDistance(double distance) { * @return a String representing the weight profile used while calculating the route. * @since 2.1.0 */ - public String getWeightName() { - return weightName; - } - - /** - * The name of the weight profile used while calculating during extraction phase. In many cases, this would be - * {@link com.mapbox.services.api.directions.v5.DirectionsCriteria#WEIGHT_NAME_ROUTABILITY}, - * {@link com.mapbox.services.api.directions.v5.DirectionsCriteria#WEIGHT_NAME_DURATION}, or - * {@link com.mapbox.services.api.directions.v5.DirectionsCriteria#WEIGHT_NAME_DISTANCE}. - * - * @param weightName a String representing the weight profile used while calculating the route. - * @since 2.1.0 - */ - public void setWeightName(String weightName) { - this.weightName = weightName; - } + @SerializedName("weight_name") + public abstract String getWeightName(); /** * The calculated weight of the route. @@ -74,19 +50,7 @@ public void setWeightName(String weightName) { * @return the weight value provided from the API as a {@code double} value. * @since 2.1.0 */ - public double getWeight() { - return weight; - } - - /** - * The calculated weight of the route. - * - * @param weight the weight value as a {@code double} value. - * @since 2.1.0 - */ - public void setWeight(double weight) { - this.weight = weight; - } + public abstract double getWeight(); /** * The estimated travel time from origin to destination. @@ -94,19 +58,7 @@ public void setWeight(double weight) { * @return a double number with unit seconds. * @since 1.0.0 */ - public double getDuration() { - return duration; - } - - /** - * The estimated travel time from origin to destination. - * - * @param duration a double number with unit seconds. - * @since 2.1.0 - */ - public void setDuration(double duration) { - this.duration = duration; - } + public abstract double getDuration(); /** * Gives the geometry of the route. Commonly used to draw the route on the map view. @@ -114,19 +66,7 @@ public void setDuration(double duration) { * @return An encoded polyline string. * @since 1.0.0 */ - public String getGeometry() { - return geometry; - } - - /** - * Sets the geometry of the route. Commonly used to draw the route on the map view. - * - * @param geometry an encoded polyline string. - * @since 2.1.0 - */ - public void setGeometry(String geometry) { - this.geometry = geometry; - } + public abstract String getGeometry(); /** * A Leg is a route between only two waypoints @@ -134,17 +74,5 @@ public void setGeometry(String geometry) { * @return List of {@link RouteLeg} objects. * @since 1.0.0 */ - public List getLegs() { - return legs; - } - - /** - * A Leg is a route between only two waypoints - * - * @param legs list of {@link RouteLeg} objects. - * @since 2.1.0 - */ - public void setLegs(List legs) { - this.legs = legs; - } + public abstract List getLegs(); } diff --git a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/models/MapMatchingMatching.java b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/models/MapMatchingMatching.java index 98ad92c4c..47e6fb0d0 100644 --- a/mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/models/MapMatchingMatching.java +++ b/mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/models/MapMatchingMatching.java @@ -5,7 +5,7 @@ /** * A match object is a {@link DirectionsRoute} object with an additional confidence field. */ -public class MapMatchingMatching extends DirectionsRoute { +public abstract class MapMatchingMatching extends DirectionsRoute { private double confidence;