From a1e3b2fc31d2621857a58a2ff4f8c486742a0548 Mon Sep 17 00:00:00 2001 From: d-kunin Date: Fri, 16 Aug 2013 18:16:53 +0300 Subject: [PATCH 1/4] Added MwmRequest + ReturnOnBallonClicked param. --- lib/src/com/mapswithme/maps/api/Const.java | 1 + .../mapswithme/maps/api/MapsWithMeApi.java | 203 ++++++------------ .../com/mapswithme/maps/api/MwmRequest.java | 155 +++++++++++++ 3 files changed, 224 insertions(+), 135 deletions(-) create mode 100644 lib/src/com/mapswithme/maps/api/MwmRequest.java diff --git a/lib/src/com/mapswithme/maps/api/Const.java b/lib/src/com/mapswithme/maps/api/Const.java index 4f6664c..96a25de 100644 --- a/lib/src/com/mapswithme/maps/api/Const.java +++ b/lib/src/com/mapswithme/maps/api/Const.java @@ -33,6 +33,7 @@ public class Const public static final String EXTRA_CALLER_APP_INFO = AUTHORITY + ".caller_app_info"; public static final String EXTRA_HAS_PENDING_INTENT = AUTHORITY + ".has_pen_intent"; public static final String EXTRA_CALLER_PENDING_INTENT = AUTHORITY + ".pending_intent"; + public static final String EXTRA_RETURN_ON_BALLOON_CLICK = AUTHORITY + ".return_on_balloon_click"; /* Response extras */ diff --git a/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java b/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java index 615fbbb..4b224fc 100644 --- a/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java +++ b/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java @@ -19,18 +19,16 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -******************************************************************************/ + ******************************************************************************/ package com.mapswithme.maps.api; -import java.util.Locale; - -import android.annotation.SuppressLint; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; -import android.net.Uri; + + public final class MapsWithMeApi { @@ -43,90 +41,9 @@ public final class MapsWithMeApi */ public static final double ZOOM_MIN = 1; - - /** - * Shows single point on the map. - * - * @param caller - * @param lat - * @param lon - * @param name - */ - public static void showPointOnMap(Activity caller, double lat, double lon, String name) + public static void sendRequest(Activity caller, MwmRequest request) { - showPointsOnMap(caller, (String)null, (PendingIntent)null, new MWMPoint(lat, lon, name)); - } - - - /** - * Shows single point on the map using specified - * zoom level in range from {@link MapsWithMeApi#ZOOM_MIN} to {@link MapsWithMeApi#ZOOM_MAX}. - * - * @param caller - * @param lat - * @param lon - * @param name - * @param zoomLevel - */ - public static void showPointOnMap(Activity caller, double lat, double lon, String name, double zoomLevel) - { - showPointsOnMap(caller, (String)null, zoomLevel, (PendingIntent)null, new MWMPoint(lat, lon, name)); - } - - /** - * Shows set of points on the map. - * - * @param caller - * @param title - * @param points - */ - public static void showPointsOnMap(Activity caller, String title, MWMPoint... points) - { - showPointsOnMap(caller, title, null, points); - } - - /** - * Shows set of points on the maps - * and allows MapsWithMeApplication to send {@link PendingIntent} provided by client application. - * - * @param caller - * @param title - * @param pendingIntent - * @param points - */ - public static void showPointsOnMap(Activity caller, String title, PendingIntent pendingIntent, MWMPoint ... points) - { - showPointsOnMap(caller, title, -1, pendingIntent, points); - } - - /** - * Detects if any version (Lite, Pro) of MapsWithMe, which supports - * API calls are installed on the device. - * - * @param context - * @return - */ - public static boolean isMapsWithMeInstalled(Context context) - { - final Intent intent = new Intent(Const.ACTION_MWM_REQUEST); - return context.getPackageManager().resolveActivity(intent, 0) != null; - } - - // Internal only code - - private static void showPointsOnMap(Activity caller, String title, double zoomLevel, PendingIntent pendingIntent, MWMPoint... points) - { - final Intent mwmIntent = new Intent(Const.ACTION_MWM_REQUEST); - - mwmIntent.putExtra(Const.EXTRA_URL, createMwmUrl(caller, title, zoomLevel, points).toString()); - mwmIntent.putExtra(Const.EXTRA_TITLE, title); - - final boolean hasIntent = pendingIntent != null; - mwmIntent.putExtra(Const.EXTRA_HAS_PENDING_INTENT, hasIntent); - if (hasIntent) - mwmIntent.putExtra(Const.EXTRA_CALLER_PENDING_INTENT, pendingIntent); - - addCommonExtras(caller, mwmIntent); + final Intent mwmIntent = request.toIntent(caller); if (isMapsWithMeInstalled(caller)) { @@ -139,65 +56,81 @@ public final class MapsWithMeApi (new DownloadMapsWithMeDialog(caller)).show(); } - static StringBuilder createMwmUrl(Context context, String title, double zoomLevel, MWMPoint ... points) + /** + * Shows single point on the map. + * + * @param caller + * @param lat + * @param lon + * @param name + */ + public static void showPointOnMap(Activity caller, double lat, double lon, String name) { - StringBuilder urlBuilder = new StringBuilder("mapswithme://map?"); - // version - urlBuilder.append("v=") - .append(Const.API_VERSION) - .append("&"); - // back url, always not null - urlBuilder.append("backurl=") - .append(getCallbackAction(context)) - .append("&"); - // title - appendIfNotNull(urlBuilder, "appname", title); - // zoom - appendIfNotNull(urlBuilder, "z", isValidZoomLevel(zoomLevel) ? String.valueOf(zoomLevel) : null); - - // points - for (MWMPoint point : points) - { - if (point != null) - { - urlBuilder.append("ll=") - .append(String.format(Locale.US, "%f,%f&", point.getLat(), point.getLon())); - - appendIfNotNull(urlBuilder, "n", point.getName()); - appendIfNotNull(urlBuilder, "id", point.getId()); - } - } - - return urlBuilder; + showPointsOnMap(caller, (String) null, (PendingIntent) null, new MWMPoint(lat, lon, name)); } - static String getCallbackAction(Context context) + /** + * Shows single point on the map using specified zoom level in range from + * {@link MapsWithMeApi#ZOOM_MIN} to {@link MapsWithMeApi#ZOOM_MAX}. + * + * @param caller + * @param lat + * @param lon + * @param name + * @param zoomLevel + */ + public static void showPointOnMap(Activity caller, double lat, double lon, String name, double zoomLevel) { - return Const.CALLBACK_PREFIX + context.getPackageName(); + showPointsOnMap(caller, (String) null, zoomLevel, (PendingIntent) null, new MWMPoint(lat, lon, name)); } - @SuppressLint("NewApi") - private static Intent addCommonExtras(Context context, Intent intent) + /** + * Shows set of points on the map. + * + * @param caller + * @param title + * @param points + */ + public static void showPointsOnMap(Activity caller, String title, MWMPoint... points) { - intent.putExtra(Const.EXTRA_CALLER_APP_INFO, context.getApplicationInfo()); - intent.putExtra(Const.EXTRA_API_VERSION, Const.API_VERSION); - - return intent; + showPointsOnMap(caller, title, null, points); } - private static StringBuilder appendIfNotNull(StringBuilder builder, String key, String value) + /** + * Shows set of points on the maps and allows MapsWithMeApplication to send + * {@link PendingIntent} provided by client application. + * + * @param caller + * @param title + * @param pendingIntent + * @param points + */ + public static void showPointsOnMap(Activity caller, String title, PendingIntent pendingIntent, MWMPoint... points) { - if (value != null) - builder.append(key) - .append("=") - .append(Uri.encode(value)) - .append("&"); - - return builder; + showPointsOnMap(caller, title, -1, pendingIntent, points); } - private static boolean isValidZoomLevel(double zoom) + private static void showPointsOnMap(Activity caller, String title, double zoomLevel, PendingIntent pendingIntent, + MWMPoint... points) { - return zoom >= ZOOM_MIN && zoom <= ZOOM_MAX; + final MwmRequest request = new MwmRequest() + .setTitle(title) + .setZoomLevel(zoomLevel) + .setPendingIntent(pendingIntent) + .setPoints(points); + sendRequest(caller, request); + } + + /** + * Detects if any version (Lite, Pro) of MapsWithMe, which supports API calls + * are installed on the device. + * + * @param context + * @return + */ + public static boolean isMapsWithMeInstalled(Context context) + { + final Intent intent = new Intent(Const.ACTION_MWM_REQUEST); + return context.getPackageManager().resolveActivity(intent, 0) != null; } } diff --git a/lib/src/com/mapswithme/maps/api/MwmRequest.java b/lib/src/com/mapswithme/maps/api/MwmRequest.java new file mode 100644 index 0000000..1c2ce28 --- /dev/null +++ b/lib/src/com/mapswithme/maps/api/MwmRequest.java @@ -0,0 +1,155 @@ +package com.mapswithme.maps.api; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Locale; + +import android.annotation.SuppressLint; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; + +public class MwmRequest +{ + + // ** + List mPoints = new ArrayList(); + PendingIntent mPendingIntent; + String mTitle; + double mZoomLevel = 1; + boolean mReturnOnBalloonClick; + // ** + + public MwmRequest setTitle(String title) + { + mTitle = title; + return this; + } + + public MwmRequest addPoint(MWMPoint point) + { + mPoints.add(point); + return this; + } + + public MwmRequest addPoint(double lat, double lon, String name, String id) + { + return addPoint(new MWMPoint(lat, lon, name, id)); + } + + public MwmRequest setPoints(Collection points) + { + mPoints = new ArrayList(points); + return this; + } + + public MwmRequest setReturnOnBalloonClick(boolean doReturn) + { + mReturnOnBalloonClick = doReturn; + return this; + } + + public MwmRequest setZoomLevel(double zoomLevel) + { + mZoomLevel = zoomLevel; + return this; + } + + public MwmRequest setPendingIntent(PendingIntent pi) + { + mPendingIntent = pi; + return this; + } + + public Intent toIntent(Context context) + { + final Intent mwmIntent = new Intent(Const.ACTION_MWM_REQUEST); + + // url + final String mwmUrl = createMwmUrl(context, mTitle, mZoomLevel, mPoints).toString(); + mwmIntent.putExtra(Const.EXTRA_URL, mwmUrl); + // title + mwmIntent.putExtra(Const.EXTRA_TITLE, mTitle); + // more + mwmIntent.putExtra(Const.EXTRA_RETURN_ON_BALLOON_CLICK, mReturnOnBalloonClick); + + final boolean hasIntent = mPendingIntent != null; + mwmIntent.putExtra(Const.EXTRA_HAS_PENDING_INTENT, hasIntent); + if (hasIntent) + mwmIntent.putExtra(Const.EXTRA_CALLER_PENDING_INTENT, mPendingIntent); + + addCommonExtras(context, mwmIntent); + + return mwmIntent; + } + + /** + * @Hidden + * This method is internal only. + * Used for compatibility. + */ + MwmRequest setPoints(MWMPoint[] points) + { + return setPoints(Arrays.asList(points)); + } + + // Below are utilities from MapsWithMeApi because we are not "Feature Envy" + + private static StringBuilder createMwmUrl(Context context, String title, double zoomLevel, List points) + { + final StringBuilder urlBuilder = new StringBuilder("mapswithme://map?"); + // version + urlBuilder.append("v=").append(Const.API_VERSION).append("&"); + // back url, always not null + urlBuilder.append("backurl=").append(getCallbackAction(context)).append("&"); + // title + appendIfNotNull(urlBuilder, "appname", title); + // zoom + appendIfNotNull(urlBuilder, "z", isValidZoomLevel(zoomLevel) ? String.valueOf(zoomLevel) : null); + + // points + for (final MWMPoint point : points) + { + if (point != null) + { + urlBuilder.append("ll=").append(String.format(Locale.US, "%f,%f&", point.getLat(), point.getLon())); + + appendIfNotNull(urlBuilder, "n", point.getName()); + appendIfNotNull(urlBuilder, "id", point.getId()); + } + } + + return urlBuilder; + } + + private static String getCallbackAction(Context context) + { + return Const.CALLBACK_PREFIX + context.getPackageName(); + } + + @SuppressLint("NewApi") + private static Intent addCommonExtras(Context context, Intent intent) + { + intent.putExtra(Const.EXTRA_CALLER_APP_INFO, context.getApplicationInfo()); + intent.putExtra(Const.EXTRA_API_VERSION, Const.API_VERSION); + + return intent; + } + + private static StringBuilder appendIfNotNull(StringBuilder builder, String key, String value) + { + if (value != null) + builder.append(key).append("=").append(Uri.encode(value)).append("&"); + + return builder; + } + + private static boolean isValidZoomLevel(double zoom) + { + return zoom >= MapsWithMeApi.ZOOM_MIN && zoom <= MapsWithMeApi.ZOOM_MAX; + } + +} From 68033ae0a5712d20fc9b5b676145d08f5032c53b Mon Sep 17 00:00:00 2001 From: d-kunin Date: Sun, 18 Aug 2013 21:57:31 +0300 Subject: [PATCH 2/4] [and] Url parsing from GWM. --- lib/src/com/mapswithme/maps/api/MapsWithMeApi.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java b/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java index 4b224fc..0600628 100644 --- a/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java +++ b/lib/src/com/mapswithme/maps/api/MapsWithMeApi.java @@ -27,6 +27,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.net.Uri; public final class MapsWithMeApi @@ -41,6 +42,19 @@ public final class MapsWithMeApi */ public static final double ZOOM_MIN = 1; + + public static void showMapsWithMeUrl(Activity caller, PendingIntent pendingIntent, double zoomLevel, String url) + { + final Uri uri = Uri.parse(url); + final String latlon[] = uri.getQueryParameter("ll").split(","); + final double lat = Double.parseDouble(latlon[0]); + final double lon = Double.parseDouble(latlon[1]); + final String name = uri.getQueryParameter("n"); + final String id = uri.getQueryParameter("id"); + + showPointsOnMap(caller, name, zoomLevel, pendingIntent, new MWMPoint(lat, lon, name, id)); + } + public static void sendRequest(Activity caller, MwmRequest request) { final Intent mwmIntent = request.toIntent(caller); From d9bffebe0d84dabb02e0b17c193fe4f557470233 Mon Sep 17 00:00:00 2001 From: d-kunin Date: Sun, 18 Aug 2013 21:57:41 +0300 Subject: [PATCH 3/4] [and] API version up. --- lib/src/com/mapswithme/maps/api/Const.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/com/mapswithme/maps/api/Const.java b/lib/src/com/mapswithme/maps/api/Const.java index 96a25de..3393e47 100644 --- a/lib/src/com/mapswithme/maps/api/Const.java +++ b/lib/src/com/mapswithme/maps/api/Const.java @@ -45,7 +45,7 @@ public class Const public static final String ACTION_MWM_REQUEST = AUTHORITY + ".request"; - static final int API_VERSION = 1; + static final int API_VERSION = 2; static final String CALLBACK_PREFIX = "mapswithme.client."; private Const() {} From 5af09de9791fbec3ef49a68190a89a311a9375a0 Mon Sep 17 00:00:00 2001 From: d-kunin Date: Mon, 19 Aug 2013 13:51:38 +0300 Subject: [PATCH 4/4] [and] package private -> private --- lib/src/com/mapswithme/maps/api/MwmRequest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/com/mapswithme/maps/api/MwmRequest.java b/lib/src/com/mapswithme/maps/api/MwmRequest.java index 1c2ce28..61e53b6 100644 --- a/lib/src/com/mapswithme/maps/api/MwmRequest.java +++ b/lib/src/com/mapswithme/maps/api/MwmRequest.java @@ -16,11 +16,11 @@ public class MwmRequest { // ** - List mPoints = new ArrayList(); - PendingIntent mPendingIntent; - String mTitle; - double mZoomLevel = 1; - boolean mReturnOnBalloonClick; + private List mPoints = new ArrayList(); + private PendingIntent mPendingIntent; + private String mTitle; + private double mZoomLevel = 1; + private boolean mReturnOnBalloonClick; // ** public MwmRequest setTitle(String title)