From c0163b8b41c99c4e104bde95c6ba22184a29536b Mon Sep 17 00:00:00 2001 From: Dmitry Yunitsky Date: Fri, 5 Aug 2016 19:28:32 +0300 Subject: [PATCH] [android] Retrieve parsed routing params and build route. --- android/jni/com/mapswithme/maps/Framework.cpp | 31 +++++++++++++++ .../src/com/mapswithme/maps/Framework.java | 5 +++ .../src/com/mapswithme/maps/MwmActivity.java | 25 +++++++++++- .../maps/api/ParsedRoutingData.java | 18 +++++++++ .../maps/api/ParsedUrlMwmRequest.java | 38 +++++++++++++++++++ .../com/mapswithme/maps/api/RoutePoint.java | 18 +++++++++ .../maps/routing/RoutingController.java | 13 +++++-- 7 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 android/src/com/mapswithme/maps/api/ParsedRoutingData.java create mode 100644 android/src/com/mapswithme/maps/api/ParsedUrlMwmRequest.java create mode 100644 android/src/com/mapswithme/maps/api/RoutePoint.java diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 9efab79d49..295a259809 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -540,6 +540,37 @@ Java_com_mapswithme_maps_Framework_nativeClearApiPoints(JNIEnv * env, jclass cla guard.m_controller.Clear(); } +JNIEXPORT jint JNICALL +Java_com_mapswithme_maps_Framework_nativeParseAndSetApiUrl(JNIEnv * env, jclass clazz, jstring url) +{ + return static_cast(frm()->ParseAndSetApiURL(jni::ToNativeString(env, url))); +} + +JNIEXPORT jobject JNICALL +Java_com_mapswithme_maps_Framework_nativeGetParsedRoutingData(JNIEnv * env, jclass clazz) +{ + using namespace url_scheme; + static jclass const pointClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/api/RoutePoint"); + // Java signature : RoutePoint(double lat, double lon, String name) + static jmethodID const pointConstructor = jni::GetConstructorID(env, pointClazz, "(DDLjava/lang/String;)V"); + + static jclass const routeDataClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/api/ParsedRoutingData"); + // Java signature : ParsedRoutingData(RoutePoint[] points, int routerType) { + static jmethodID const routeDataConstructor = jni::GetConstructorID(env, routeDataClazz, "([Lcom/mapswithme/maps/api/RoutePoint;I)V"); + + auto const & routingData = frm()->GetParsedRoutingData(); + jobjectArray points = jni::ToJavaArray(env, pointClazz, routingData.m_points, + [](JNIEnv * env, RoutePoint const & point) + { + jni::TScopedLocalRef const name(env, jni::ToJavaString(env, point.m_name)); + return env->NewObject(pointClazz, pointConstructor, + MercatorBounds::YToLat(point.m_org.y), + MercatorBounds::XToLon(point.m_org.x), name.get()); + }); + + return env->NewObject(routeDataClazz, routeDataConstructor, points, routingData.m_type); +} + JNIEXPORT void JNICALL Java_com_mapswithme_maps_Framework_nativeSetMapObjectListener(JNIEnv * env, jclass clazz, jobject jListener) { diff --git a/android/src/com/mapswithme/maps/Framework.java b/android/src/com/mapswithme/maps/Framework.java index 36b26a9156..2929374217 100644 --- a/android/src/com/mapswithme/maps/Framework.java +++ b/android/src/com/mapswithme/maps/Framework.java @@ -8,6 +8,8 @@ import android.support.annotation.UiThread; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import com.mapswithme.maps.api.ParsedRoutingData; +import com.mapswithme.maps.api.ParsedUrlMwmRequest; import com.mapswithme.maps.bookmarks.data.DistanceAndAzimut; import com.mapswithme.maps.bookmarks.data.MapObject; import com.mapswithme.maps.routing.RoutingInfo; @@ -103,6 +105,9 @@ public class Framework public static native long nativeGetDataVersion(); public static native void nativeClearApiPoints(); + @ParsedUrlMwmRequest.ParsingResult + public static native int nativeParseAndSetApiUrl(String url); + public static native ParsedRoutingData nativeGetParsedRoutingData(); public static native void nativeDeactivatePopup(); diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index a96994b9ce..b880483150 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -26,6 +26,9 @@ import com.mapswithme.maps.Framework.MapObjectListener; import com.mapswithme.maps.activity.CustomNavigateUpListener; import com.mapswithme.maps.ads.LikesManager; import com.mapswithme.maps.api.ParsedMwmRequest; +import com.mapswithme.maps.api.ParsedRoutingData; +import com.mapswithme.maps.api.ParsedUrlMwmRequest; +import com.mapswithme.maps.api.RoutePoint; import com.mapswithme.maps.base.BaseMwmFragmentActivity; import com.mapswithme.maps.base.OnBackPressListener; import com.mapswithme.maps.bookmarks.BookmarkCategoriesActivity; @@ -1137,7 +1140,27 @@ public class MwmActivity extends BaseMwmFragmentActivity @Override public boolean run(MwmActivity target) { - return MapFragment.nativeShowMapForUrl(mUrl); + final @ParsedUrlMwmRequest.ParsingResult int result = Framework.nativeParseAndSetApiUrl(mUrl); + switch (result) + { + case ParsedUrlMwmRequest.RESULT_INCORRECT: + // TODO handle error + break; + case ParsedUrlMwmRequest.RESULT_MAP: + return MapFragment.nativeShowMapForUrl(mUrl); + case ParsedUrlMwmRequest.RESULT_ROUTE: + final ParsedRoutingData data = Framework.nativeGetParsedRoutingData(); + RoutingController.get().setRouterType(data.mRouterType); + final RoutePoint from = data.mPoints[0]; + final RoutePoint to = data.mPoints[1]; + RoutingController.get().prepare(new MapObject(MapObject.API_POINT, from.mName, "", "", + from.mLat, from.mLon, ""), + new MapObject(MapObject.API_POINT, to.mName, "", "", + to.mLat, to.mLon, "")); + return true; + } + + return false; } } diff --git a/android/src/com/mapswithme/maps/api/ParsedRoutingData.java b/android/src/com/mapswithme/maps/api/ParsedRoutingData.java new file mode 100644 index 0000000000..206b46786e --- /dev/null +++ b/android/src/com/mapswithme/maps/api/ParsedRoutingData.java @@ -0,0 +1,18 @@ +package com.mapswithme.maps.api; + +import com.mapswithme.maps.Framework; + +/** + * Represents Framework::ParsedRoutingData from core. + */ +public class ParsedRoutingData +{ + public final RoutePoint[] mPoints; + @Framework.RouterType + public final int mRouterType; + + public ParsedRoutingData(RoutePoint[] points, int routerType) { + this.mPoints = points; + this.mRouterType = routerType; + } +} diff --git a/android/src/com/mapswithme/maps/api/ParsedUrlMwmRequest.java b/android/src/com/mapswithme/maps/api/ParsedUrlMwmRequest.java new file mode 100644 index 0000000000..84f35f4ec2 --- /dev/null +++ b/android/src/com/mapswithme/maps/api/ParsedUrlMwmRequest.java @@ -0,0 +1,38 @@ +package com.mapswithme.maps.api; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Represents url_scheme::ParsedMapApi::ParsingResult from core. + */ +public class ParsedUrlMwmRequest +{ + @Retention(RetentionPolicy.SOURCE) + @IntDef({RESULT_INCORRECT, RESULT_MAP, RESULT_ROUTE}) + public @interface ParsingResult {} + + public static final int RESULT_INCORRECT = 0; + public static final int RESULT_MAP = 1; + public static final int RESULT_ROUTE = 2; + + public final RoutePoint[] mRoutePoints; + public final String mGlobalUrl; + public final String mAppTitle; + public final int mVersion; + public final double mZoomLevel; + public final boolean mGoBackOnBalloonClick; + public final boolean mIsValid; + + public ParsedUrlMwmRequest(RoutePoint[] routePoints, String globalUrl, String appTitle, int version, double zoomLevel, boolean goBackOnBalloonClick, boolean isValid) { + this.mRoutePoints = routePoints; + this.mGlobalUrl = globalUrl; + this.mAppTitle = appTitle; + this.mVersion = version; + this.mZoomLevel = zoomLevel; + this.mGoBackOnBalloonClick = goBackOnBalloonClick; + this.mIsValid = isValid; + } +} diff --git a/android/src/com/mapswithme/maps/api/RoutePoint.java b/android/src/com/mapswithme/maps/api/RoutePoint.java new file mode 100644 index 0000000000..cad6c0ce68 --- /dev/null +++ b/android/src/com/mapswithme/maps/api/RoutePoint.java @@ -0,0 +1,18 @@ +package com.mapswithme.maps.api; + +/** + * Represents url_scheme::RoutePoint from core. + */ +public class RoutePoint +{ + public final double mLat; + public final double mLon; + public final String mName; + + public RoutePoint(double lat, double lon, String name) + { + mLat = lat; + mLon = lon; + mName = name; + } +} diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index 2d2a94d60b..fe8aa5cb82 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -254,7 +254,7 @@ public class RoutingController Framework.nativeBuildRoute(mStartPoint.getLat(), mStartPoint.getLon(), mEndPoint.getLat(), mEndPoint.getLon()); } - private void showDisclaimer(final MapObject endPoint) + private void showDisclaimer(final MapObject startPoint, final MapObject endPoint) { StringBuilder builder = new StringBuilder(); for (int resId : new int[] { R.string.dialog_routing_disclaimer_priority, R.string.dialog_routing_disclaimer_precision, @@ -273,23 +273,28 @@ public class RoutingController public void onClick(DialogInterface dlg, int which) { Config.acceptRoutingDisclaimer(); - prepare(endPoint); + prepare(startPoint, endPoint); } }).show(); } public void prepare(@Nullable MapObject endPoint) + { + prepare(LocationHelper.INSTANCE.getMyPosition(), endPoint); + } + + public void prepare(@Nullable MapObject startPoint, @Nullable MapObject endPoint) { mLogger.d("prepare (" + (endPoint == null ? "route)" : "p2p)")); if (!Config.isRoutingDisclaimerAccepted()) { - showDisclaimer(endPoint); + showDisclaimer(startPoint, endPoint); return; } cancel(); - mStartPoint = LocationHelper.INSTANCE.getMyPosition(); + mStartPoint = startPoint; mEndPoint = endPoint; setState(State.PREPARE);