diff options
author | Michael Keppler <michael.keppler@gmx.de> | 2014-04-21 18:33:07 +0200 |
---|---|---|
committer | Michael Keppler <michael.keppler@gmx.de> | 2014-04-21 18:33:07 +0200 |
commit | 7955c23b643ba127d002b07fac57790ca430ad88 (patch) | |
tree | 4f61c7f0c81bf8d85cb44034f097ff9b7c63e44b /mapswithme-api/src/com | |
parent | bcced1c4bfafcd0984f7c94abf53bc966359799e (diff) | |
download | cgeo-7955c23b643ba127d002b07fac57790ca430ad88.zip cgeo-7955c23b643ba127d002b07fac57790ca430ad88.tar.gz cgeo-7955c23b643ba127d002b07fac57790ca430ad88.tar.bz2 |
new: integrate mapswithme
Diffstat (limited to 'mapswithme-api/src/com')
6 files changed, 636 insertions, 0 deletions
diff --git a/mapswithme-api/src/com/mapswithme/maps/api/Const.java b/mapswithme-api/src/com/mapswithme/maps/api/Const.java new file mode 100644 index 0000000..8fbd196 --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/Const.java @@ -0,0 +1,55 @@ +/****************************************************************************** + Copyright (c) 2013, MapsWithMe GmbH All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. Redistributions in binary form must + reproduce the above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided with the + distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + 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; + +public class Const +{ + + /* Request extras */ + static final String AUTHORITY = "com.mapswithme.maps.api"; + public static final String EXTRA_URL = AUTHORITY + ".url"; + public static final String EXTRA_TITLE = AUTHORITY + ".title"; + public static final String EXTRA_API_VERSION = AUTHORITY + ".version"; + 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"; + public static final String EXTRA_PICK_POINT = AUTHORITY + ".pick_point"; + public static final String EXTRA_CUSTOM_BUTTON_NAME = AUTHORITY + ".custom_button_name"; + + + /* Response extras */ + /* Point part-by-part*/ + public static final String EXTRA_MWM_RESPONSE_POINT_NAME = AUTHORITY + ".point_name"; + public static final String EXTRA_MWM_RESPONSE_POINT_LAT = AUTHORITY + ".point_lat"; + public static final String EXTRA_MWM_RESPONSE_POINT_LON = AUTHORITY + ".point_lon"; + public static final String EXTRA_MWM_RESPONSE_POINT_ID = AUTHORITY + ".point_id"; + public static final String EXTRA_MWM_RESPONSE_ZOOM = AUTHORITY + ".zoom_level"; + + + public static final String ACTION_MWM_REQUEST = AUTHORITY + ".request"; + static final int API_VERSION = 2; + static final String CALLBACK_PREFIX = "mapswithme.client."; + + private Const() {} +} diff --git a/mapswithme-api/src/com/mapswithme/maps/api/DownloadMapsWithMeDialog.java b/mapswithme-api/src/com/mapswithme/maps/api/DownloadMapsWithMeDialog.java new file mode 100644 index 0000000..5670a5f --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/DownloadMapsWithMeDialog.java @@ -0,0 +1,65 @@ +/****************************************************************************** + Copyright (c) 2013, MapsWithMe GmbH All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. Redistributions in binary form must + reproduce the above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided with the + distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + 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 android.app.Activity; +import android.app.Dialog; +import android.content.Intent; +import android.net.Uri; +import android.view.View; +import android.view.Window; + +import com.mapwithme.maps.api.R; + +public class DownloadMapsWithMeDialog extends Dialog implements android.view.View.OnClickListener +{ + + public DownloadMapsWithMeDialog(Activity activity) + { + super(activity); + + requestWindowFeature(Window.FEATURE_NO_TITLE); + setContentView(R.layout.dlg_install_mwm); + + findViewById(R.id.btn_lite).setOnClickListener(this); + findViewById(R.id.btn_pro).setOnClickListener(this); + + setOwnerActivity(activity); + } + + + public void onDownloadButtonClicked(String url) + { + Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + getContext().startActivity(i); + dismiss(); + } + + + @Override + public void onClick(View v) + { + String url = getContext().getString(v.getId() == R.id.btn_lite ? R.string.url_lite : R.string.url_pro); + onDownloadButtonClicked(url); + } +} diff --git a/mapswithme-api/src/com/mapswithme/maps/api/MWMPoint.java b/mapswithme-api/src/com/mapswithme/maps/api/MWMPoint.java new file mode 100644 index 0000000..86fa39a --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/MWMPoint.java @@ -0,0 +1,107 @@ +/****************************************************************************** + Copyright (c) 2013, MapsWithMe GmbH All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. Redistributions in binary form must + reproduce the above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided with the + distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + 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.io.Serializable; + +/** + * POI wrapper object. + * Has it's <code>equals()</code> and <code>hashCode()</code> methods overloaded + * so could be used in Hash(Map/Set/etc) classes. + */ +public final class MWMPoint implements Serializable +{ + private static final long serialVersionUID = 1L; + + final private double mLat; + final private double mLon; + final private String mName; + private String mId; + + public MWMPoint(double lat, double lon, String name) + { + this(lat, lon, name, null); + } + + public MWMPoint(double lat, double lon, String name, String id) + { + this.mLat = lat; + this.mLon = lon; + this.mName = name; + this.mId = id; + } + + public double getLat() { return mLat; } + public double getLon() { return mLon; } + public String getName() { return mName; } + public String getId() { return mId; } + + /** + * Sets string ID for this point. Internally it is not used to distinguish point, + * it's purpose to help clients code to associate point with domain objects of their application. + * @param id + */ + public void setId(String id) { mId = id; } + + @Override + public String toString() + { + return "MWMPoint [lat=" + mLat + ", lon=" + mLon + ", name=" + mName + ", id=" + mId + "]"; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(mLat); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(mLon); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + ((mName == null) ? 0 : mName.hashCode()); + return result; + } + + /** + * Two point are considered + * equal if they have they lat, lon, and name attributes equal. + */ + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final MWMPoint other = (MWMPoint) obj; + if (Double.doubleToLongBits(mLat) != Double.doubleToLongBits(other.mLat)) + return false; + if (Double.doubleToLongBits(mLon) != Double.doubleToLongBits(other.mLon)) + return false; + + return mName == null ? other.mName == null : mName.equals(other.mName); + } +} diff --git a/mapswithme-api/src/com/mapswithme/maps/api/MWMResponse.java b/mapswithme-api/src/com/mapswithme/maps/api/MWMResponse.java new file mode 100644 index 0000000..e2d865e --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/MWMResponse.java @@ -0,0 +1,77 @@ +/****************************************************************************** + Copyright (c) 2013, MapsWithMe GmbH All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. Redistributions in binary form must + reproduce the above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided with the + distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + 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 android.content.Context; +import android.content.Intent; + +public class MWMResponse +{ + private MWMPoint mPoint; + private double mZoomLevel; + + /** + * + * @return point, for which user requested more information in MapsWithMe application. + */ + public MWMPoint getPoint() { return mPoint; } + public boolean hasPoint() { return mPoint != null; } + public double getZoomLevel() { return mZoomLevel; } + + @Override + public String toString() + { + return "MWMResponse [SelectedPoint=" + mPoint + "]"; + } + + /** + * Factory method to extract response data from intent. + * + * @param context + * @param intent + * @return + */ + public static MWMResponse extractFromIntent(Context context, Intent intent) + { + final MWMResponse response = new MWMResponse(); + // parse point + final double lat = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LAT, INVALID_LL); + final double lon = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LON, INVALID_LL); + final String name = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_NAME); + final String id = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_ID); + + // parse additional info + response.mZoomLevel = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_ZOOM, 9); + + if (lat != INVALID_LL && lon != INVALID_LL) + response.mPoint = new MWMPoint(lat, lon, name, id); + else + response.mPoint = null; + + return response; + } + + private final static double INVALID_LL = Double.MIN_VALUE; + + private MWMResponse() {} +} diff --git a/mapswithme-api/src/com/mapswithme/maps/api/MapsWithMeApi.java b/mapswithme-api/src/com/mapswithme/maps/api/MapsWithMeApi.java new file mode 100644 index 0000000..e7fd9cd --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/MapsWithMeApi.java @@ -0,0 +1,159 @@ +/****************************************************************************** + Copyright (c) 2013, MapsWithMe GmbH All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. Redistributions in binary form must + reproduce the above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided with the + distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + 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 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 +{ + + /** + * Most detailed level, buildings and trees are seen. + */ + public static final double ZOOM_MAX = 19; + /** + * Least detailed level, continents are seen. + */ + 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); + + if (isMapsWithMeInstalled(caller)) + { + // Match activity for intent + final ActivityInfo aInfo = caller.getPackageManager().resolveActivity(mwmIntent, 0).activityInfo; + mwmIntent.setClassName(aInfo.packageName, aInfo.name); + caller.startActivity(mwmIntent); + } + else + (new DownloadMapsWithMeDialog(caller)).show(); + } + + /** + * 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) + { + 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); + } + + private static void showPointsOnMap(Activity caller, String title, double zoomLevel, PendingIntent pendingIntent, + MWMPoint... points) + { + final MwmRequest request = new MwmRequest() + .setTitle(title) + .setZoomLevel(zoomLevel) + .setPendingIntent(pendingIntent) + .setPoints(points); + sendRequest(caller, request); + } + + public static void pickPoint(Activity caller, String title, PendingIntent pi) + { + final MwmRequest request = new MwmRequest() + .setTitle(title) + .setPickPointMode(true) + .setPendingIntent(pi); + 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/mapswithme-api/src/com/mapswithme/maps/api/MwmRequest.java b/mapswithme-api/src/com/mapswithme/maps/api/MwmRequest.java new file mode 100644 index 0000000..7a2335d --- /dev/null +++ b/mapswithme-api/src/com/mapswithme/maps/api/MwmRequest.java @@ -0,0 +1,173 @@ +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 +{ + + // ** + private List<MWMPoint> mPoints = new ArrayList<MWMPoint>(); + private PendingIntent mPendingIntent; + private String mTitle; + private double mZoomLevel = 1; + private boolean mReturnOnBalloonClick; + private boolean mPickPoint = false; + private String mCustomButtonName = ""; + // ** + + public MwmRequest setCustomButtonName(String buttonName) + { + mCustomButtonName = buttonName != null ? buttonName : ""; + return this; + } + + public MwmRequest setTitle(String title) + { + mTitle = title; + return this; + } + + public MwmRequest setPickPointMode(boolean pickPoint) + { + mPickPoint = pickPoint; + 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<MWMPoint> points) + { + mPoints = new ArrayList<MWMPoint>(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); + // pick point + mwmIntent.putExtra(Const.EXTRA_PICK_POINT, mPickPoint); + // custom button name + mwmIntent.putExtra(Const.EXTRA_CUSTOM_BUTTON_NAME, mCustomButtonName); + + 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<MWMPoint> 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; + } + +} |