From 4f03f5383acc36e2e9f2c552d6eb2ae10ec65f64 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Wed, 16 Mar 2016 16:59:26 +0300 Subject: [PATCH] Osmand API done --- .../src/net/osmand/plus/MapMarkersHelper.java | 8 + .../osmand/plus/activities/MapActivity.java | 3 +- .../plus/helpers/ExternalApiHelper.java | 366 ++++++++++++++---- 3 files changed, 298 insertions(+), 79 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 175b03b5c6..a5a158c2f3 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -197,6 +197,14 @@ public class MapMarkersHelper { return mapMarkers; } + public MapMarker getFirstMapMarker() { + if (mapMarkers.size() > 0) { + return mapMarkers.get(0); + } else { + return null; + } + } + public List getSortedMapMarkers() { return sortedMapMarkers; } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 08e6735afe..c4740dfda7 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -544,6 +544,8 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents, readLocationToShow(); + OsmandPlugin.onMapActivityResume(this); + final Intent intent = getIntent(); if (intent != null) { if (Intent.ACTION_VIEW.equals(intent.getAction())) { @@ -578,7 +580,6 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents, app.getResourceManager().setBusyIndicator(new BusyIndicator(this, progress)); } - OsmandPlugin.onMapActivityResume(this); mapView.refreshMap(true); if (atlasMapRendererView != null) { atlasMapRendererView.handleOnResume(); diff --git a/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java index 8da0aaef8c..cfdae1dda8 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java @@ -5,17 +5,24 @@ import android.content.Intent; import android.net.Uri; import android.support.v7.app.AlertDialog; +import net.osmand.AndroidUtils; +import net.osmand.IndexConstants; +import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.MapMarkersHelper; +import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.audionotes.AudioVideoNotesPlugin; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.routing.RoutingHelper; import net.osmand.util.Algorithms; @@ -27,37 +34,65 @@ public class ExternalApiHelper { private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ExternalApiHelper.class); - private static final String API_CMD_SHOW_GPX = "show_gpx"; - private static final String API_CMD_NAVIGATE_GPX = "navigate_gpx"; + public static final String API_CMD_SHOW_GPX = "show_gpx"; + public static final String API_CMD_NAVIGATE_GPX = "navigate_gpx"; - private static final String API_CMD_CALC_ROUTE = "calc_route"; - private static final String API_CMD_REC_AV_NOTE = "rec_av_note"; - private static final String API_CMD_GET_INFO = "get_info"; + public static final String API_CMD_NAVIGATE = "navigate"; - private static final String API_CMD_ADD_FAVORITE = "add_favorite"; - private static final String API_CMD_ADD_MAP_MARKER = "add_map_marker"; + public static final String API_CMD_RECORD_AUDIO = "record_audio"; + public static final String API_CMD_RECORD_VIDEO = "record_video"; + public static final String API_CMD_RECORD_PHOTO = "record_photo"; + public static final String API_CMD_STOP_AV_REC = "stop_av_rec"; - private static final String API_CMD_START_GPX_REC = "start_gpx_rec"; - private static final String API_CMD_STOP_GPX_REC = "stop_gpx_rec"; + public static final String API_CMD_GET_INFO = "get_info"; - private static final String API_CMD_SUBSCRIBE_VOICE_NOTIFICATIONS = "subscribe_voice_notifications"; + public static final String API_CMD_ADD_FAVORITE = "add_favorite"; + public static final String API_CMD_ADD_MAP_MARKER = "add_map_marker"; - private static final String PARAM_NAME = "name"; - private static final String PARAM_DESC = "desc"; - private static final String PARAM_CATEGORY = "category"; - private static final String PARAM_LAT = "lat"; - private static final String PARAM_LON = "lon"; - private static final String PARAM_COLOR = "color"; - private static final String PARAM_VISIBLE = "visible"; + public static final String API_CMD_START_GPX_REC = "start_gpx_rec"; + public static final String API_CMD_STOP_GPX_REC = "stop_gpx_rec"; - private static final String PARAM_PATH = "path"; - private static final String PARAM_DATA = "data"; + public static final String API_CMD_SUBSCRIBE_VOICE_NOTIFICATIONS = "subscribe_voice_notifications"; + + public static final String PARAM_NAME = "name"; + public static final String PARAM_DESC = "desc"; + public static final String PARAM_CATEGORY = "category"; + public static final String PARAM_LAT = "lat"; + public static final String PARAM_LON = "lon"; + public static final String PARAM_COLOR = "color"; + public static final String PARAM_VISIBLE = "visible"; + + public static final String PARAM_PATH = "path"; + public static final String PARAM_DATA = "data"; + public static final String PARAM_FORCE = "force"; + + public static final String PARAM_START_NAME = "start_name"; + public static final String PARAM_DEST_NAME = "dest_name"; + public static final String PARAM_START_LAT = "start_lat"; + public static final String PARAM_START_LON = "start_lon"; + public static final String PARAM_DEST_LAT = "dest_lat"; + public static final String PARAM_DEST_LON = "dest_lon"; + public static final String PARAM_PROFILE = "profile"; + + public static final String PARAM_ETA = "eta"; + public static final String PARAM_TIME_LEFT = "time_left"; + public static final String PARAM_DISTANCE_LEFT = "time_distance_left"; + + public static final ApplicationMode[] VALID_PROFILES = new ApplicationMode[]{ + ApplicationMode.CAR, + ApplicationMode.BICYCLE, + ApplicationMode.PEDESTRIAN + }; + + public static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR; - private static final int RESULT_CODE_OK = 0; - private static final int RESULT_CODE_ERROR_UNKNOWN = -1; - private static final int RESULT_CODE_ERROR_GPX_PLUGIN_INACTIVE = 10; - private static final int RESULT_CODE_ERROR_GPX_NOT_FOUND = 20; + public static final int RESULT_CODE_OK = 0; + public static final int RESULT_CODE_ERROR_UNKNOWN = -1; + public static final int RESULT_CODE_ERROR_NOT_IMPLEMENTED = -2; + public static final int RESULT_CODE_ERROR_PLUGIN_INACTIVE = 10; + public static final int RESULT_CODE_ERROR_GPX_NOT_FOUND = 20; + public static final int RESULT_CODE_ERROR_INVALID_PROFILE = 30; private MapActivity mapActivity; private int resultCode; @@ -81,49 +116,13 @@ public class ExternalApiHelper { OsmandApplication app = (OsmandApplication) mapActivity.getApplication(); try { - - /* - + 1. Intent to show GPX file / start navigation with GPX - 2. Intent to calculate route between points (passing profile mode) and immediately start navigation - 3. Intent to request audio/video recording - 4. Intent (with result?) Current location, ETA, distance to go, time to go on the route - + 5. Intent to add Favorites / Markers - + 6. Intent to start/stop recording GPX - Service: - 8. Subscribe to voice notifications - - - // test marker - Uri uri = Uri.parse("osmand.api://add_map_marker?lat=45.610677&lon=34.368430&name=Marker"); - - // test favorite - Uri uri = Uri.parse("osmand.api://add_favorite?lat=45.610677&lon=34.368430&name=Favorite&desc=Description&category=test2&color=red&visible=true"); - - // test start gpx recording - Uri uri = Uri.parse("osmand.api://start_gpx_rec"); - - // test stop gpx recording - Uri uri = Uri.parse("osmand.api://stop_gpx_rec"); - - // test show gpx (path) - File gpx = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), "xxx.gpx"); - Uri uri = Uri.parse("osmand.api://show_gpx?path=" + URLEncoder.encode(gpx.getAbsolutePath(), "UTF-8")); - Uri uri = Uri.parse("osmand.api://navigate_gpx?path=" + URLEncoder.encode(gpx.getAbsolutePath(), "UTF-8")); - - // test show gpx (data) - Uri uri = Uri.parse("osmand.api://show_gpx"); - Uri uri = Uri.parse("osmand.api://navigate_gpx"); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - intent.putExtra("data", AndroidUtils.getFileAsString( - new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), "xxx.gpx"))); - - */ - Uri uri = intent.getData(); String cmd = uri.getHost().toLowerCase(); if (API_CMD_SHOW_GPX.equals(cmd) || API_CMD_NAVIGATE_GPX.equals(cmd)) { boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd); String path = uri.getQueryParameter(PARAM_PATH); + boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false); + GPXFile gpx = null; if (path != null) { File f = new File(path); @@ -142,7 +141,7 @@ public class ExternalApiHelper { if (gpx != null) { if (navigate) { final RoutingHelper routingHelper = app.getRoutingHelper(); - if (routingHelper.isFollowingMode()) { + if (routingHelper.isFollowingMode() && !force) { final GPXFile gpxFile = gpx; AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm(); dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { @@ -150,12 +149,12 @@ public class ExternalApiHelper { @Override public void onDismiss(DialogInterface dialog) { if (!routingHelper.isFollowingMode()) { - startNavigation(gpxFile); + startNavigation(gpxFile, null, null, null, null, null); } } }); } else { - startNavigation(gpx); + startNavigation(gpx, null, null, null, null, null); } } else { app.getSelectedGpxHelper().setGpxFileToDisplay(gpx); @@ -165,12 +164,109 @@ public class ExternalApiHelper { resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND; } - } else if (API_CMD_CALC_ROUTE.equals(cmd)) { + } else if (API_CMD_NAVIGATE.equals(cmd)) { + String profileStr = uri.getQueryParameter(PARAM_PROFILE); + final ApplicationMode profile = ApplicationMode.valueOfStringKey(profileStr, DEFAULT_PROFILE); + boolean validProfile = false; + for (ApplicationMode mode : VALID_PROFILES) { + if (mode == profile) { + validProfile = true; + break; + } + } + if (!validProfile) { + resultCode = RESULT_CODE_ERROR_INVALID_PROFILE; + } else { + String startName = uri.getQueryParameter(PARAM_START_NAME); + if (Algorithms.isEmpty(startName)) { + startName = ""; + } + String destName = uri.getQueryParameter(PARAM_DEST_NAME); + if (Algorithms.isEmpty(destName)) { + destName = ""; + } - } else if (API_CMD_REC_AV_NOTE.equals(cmd)) { + + final LatLon start; + final PointDescription startDesc; + String startLatStr = uri.getQueryParameter(PARAM_START_LAT); + String startLonStr = uri.getQueryParameter(PARAM_START_LON); + if (!Algorithms.isEmpty(startLatStr) && !Algorithms.isEmpty(startLonStr)) { + double lat = Double.parseDouble(uri.getQueryParameter(PARAM_START_LAT)); + double lon = Double.parseDouble(uri.getQueryParameter(PARAM_START_LON)); + start = new LatLon(lat, lon); + startDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, startName); + } else { + start = null; + startDesc = null; + } + + double destLat = Double.parseDouble(uri.getQueryParameter(PARAM_DEST_LAT)); + double destLon = Double.parseDouble(uri.getQueryParameter(PARAM_DEST_LON)); + final LatLon dest = new LatLon(destLat, destLon); + final PointDescription destDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, destName); + + boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false); + + final RoutingHelper routingHelper = app.getRoutingHelper(); + if (routingHelper.isFollowingMode() && !force) { + AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm(); + dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { + + @Override + public void onDismiss(DialogInterface dialog) { + if (!routingHelper.isFollowingMode()) { + startNavigation(null, start, startDesc, dest, destDesc, profile); + } + } + }); + } else { + startNavigation(null, start, startDesc, dest, destDesc, profile); + } + } + + } else if (API_CMD_RECORD_AUDIO.equals(cmd) + || API_CMD_RECORD_VIDEO.equals(cmd) + || API_CMD_RECORD_PHOTO.equals(cmd) + || API_CMD_STOP_AV_REC.equals(cmd)) { + AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class); + if (plugin == null) { + resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE; + } else { + if (API_CMD_STOP_AV_REC.equals(cmd)) { + plugin.stopRecording(mapActivity, false); + } else { + double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT)); + double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON)); + if (API_CMD_RECORD_AUDIO.equals(cmd)) { + plugin.recordAudio(lat, lon, mapActivity); + } else if (API_CMD_RECORD_VIDEO.equals(cmd)) { + plugin.recordVideo(lat, lon, mapActivity); + } else if (API_CMD_RECORD_PHOTO.equals(cmd)) { + plugin.takePhoto(lat, lon, mapActivity); + } + } + + resultCode = RESULT_CODE_OK; + } } else if (API_CMD_GET_INFO.equals(cmd)) { + Location location = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation(); + if (location != null) { + result.putExtra(PARAM_LAT, location.getLatitude()); + result.putExtra(PARAM_LON, location.getLongitude()); + } + + final RoutingHelper routingHelper = app.getRoutingHelper(); + if (routingHelper.isFollowingMode()) { + int time = routingHelper.getLeftTime(); + result.putExtra(PARAM_TIME_LEFT, time); + long eta = time + System.currentTimeMillis() / 1000; + result.putExtra(PARAM_ETA, eta); + result.putExtra(PARAM_DISTANCE_LEFT, routingHelper.getLeftDistance()); + } + finish = true; resultCode = RESULT_CODE_OK; @@ -181,7 +277,7 @@ public class ExternalApiHelper { double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT)); double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON)); String colorTag = uri.getQueryParameter(PARAM_COLOR); - String visibleStr = uri.getQueryParameter(PARAM_VISIBLE); + boolean visible = uri.getBooleanQueryParameter(PARAM_VISIBLE, true); if (name == null) { name = ""; @@ -201,11 +297,6 @@ public class ExternalApiHelper { } } - boolean visible = true; - if (!Algorithms.isEmpty(visibleStr)) { - visible = Boolean.parseBoolean(visibleStr); - } - FavouritePoint fav = new FavouritePoint(lat, lon, name, category); fav.setDescription(desc); fav.setColor(color); @@ -214,6 +305,9 @@ public class ExternalApiHelper { FavouritesDbHelper helper = app.getFavorites(); helper.addFavourite(fav); + mapActivity.getContextMenu().show(new LatLon(lat, lon), + mapActivity.getMapLayers().getFavoritesLayer().getObjectName(fav), fav); + resultCode = RESULT_CODE_OK; } else if (API_CMD_ADD_MAP_MARKER.equals(cmd)) { @@ -227,12 +321,18 @@ public class ExternalApiHelper { MapMarkersHelper markersHelper = app.getMapMarkersHelper(); markersHelper.addMapMarker(new LatLon(lat, lon), pd); + MapMarker marker = markersHelper.getFirstMapMarker(); + if (marker != null) { + mapActivity.getContextMenu().show(new LatLon(lat, lon), + mapActivity.getMapLayers().getMapMarkersLayer().getObjectName(marker), marker); + } + resultCode = RESULT_CODE_OK; } else if (API_CMD_START_GPX_REC.equals(cmd)) { OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); if (plugin == null) { - resultCode = RESULT_CODE_ERROR_GPX_PLUGIN_INACTIVE; + resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE; } else { plugin.startGPXMonitoring(null); } @@ -242,7 +342,7 @@ public class ExternalApiHelper { } else if (API_CMD_STOP_GPX_REC.equals(cmd)) { OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); if (plugin == null) { - resultCode = RESULT_CODE_ERROR_GPX_PLUGIN_INACTIVE; + resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE; } else { plugin.stopRecording(); } @@ -250,7 +350,8 @@ public class ExternalApiHelper { resultCode = RESULT_CODE_OK; } else if (API_CMD_SUBSCRIBE_VOICE_NOTIFICATIONS.equals(cmd)) { - + // not implemented yet + resultCode = RESULT_CODE_ERROR_NOT_IMPLEMENTED; } } catch (Exception e) { @@ -261,14 +362,25 @@ public class ExternalApiHelper { return result; } - private void startNavigation(GPXFile gpx) { + private void startNavigation(GPXFile gpx, + LatLon from, PointDescription fromDesc, + LatLon to, PointDescription toDesc, + ApplicationMode mode) { OsmandApplication app = mapActivity.getMyApplication(); RoutingHelper routingHelper = app.getRoutingHelper(); - mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpx, null, null, false, false); + if (gpx == null) { + app.getSettings().APPLICATION_MODE.set(mode); + final TargetPointsHelper targets = mapActivity.getMyApplication().getTargetPointsHelper(); + targets.removeAllWayPoints(false, true); + targets.navigateToPoint(to, true, -1, toDesc); + } + mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpx, from, fromDesc, true, false); if (!app.getTargetPointsHelper().checkPointToNavigateShort()) { mapActivity.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu().show(); } else { - app.getSettings().APPLICATION_MODE.set(routingHelper.getAppMode()); + if (app.getSettings().APPLICATION_MODE.get() != routingHelper.getAppMode()) { + app.getSettings().APPLICATION_MODE.set(routingHelper.getAppMode()); + } mapActivity.getMapViewTrackingUtilities().backToLocationImpl(); app.getSettings().FOLLOW_THE_ROUTE.set(true); routingHelper.setFollowingMode(true); @@ -278,4 +390,102 @@ public class ExternalApiHelper { routingHelper.setCurrentLocation(app.getLocationProvider().getLastKnownLocation(), false); } } + + public void testApi(OsmandApplication app, String command) { + Uri uri = null; + Intent intent = null; + + String lat = "44.98062"; + String lon = "34.09258"; + String destLat = "44.97799"; + String destLon = "34.10286"; + String gpxName = "xxx.gpx"; + + try { + + if (API_CMD_GET_INFO.equals(command)) { + uri = Uri.parse("osmand.api://get_info"); + } + + if (API_CMD_NAVIGATE.equals(command)) { + // test navigate + uri = Uri.parse("osmand.api://navigate" + + "?start_lat=" + lat + "&start_lon=" + lon + "&start_name=Start" + + "&dest_lat=" + destLat + "&dest_lon=" + destLon + "&dest_name=Finish" + + "&profile=bicycle"); + } + + if (API_CMD_RECORD_AUDIO.equals(command)) { + // test record audio + uri = Uri.parse("osmand.api://record_audio?lat=" + lat + "&lon=" + lon); + } + if (API_CMD_RECORD_VIDEO.equals(command)) { + // test record video + uri = Uri.parse("osmand.api://record_video?lat=" + lat + "&lon=" + lon); + } + if (API_CMD_RECORD_PHOTO.equals(command)) { + // test take photo + uri = Uri.parse("osmand.api://record_photo?lat=" + lat + "&lon=" + lon); + } + if (API_CMD_STOP_AV_REC.equals(command)) { + // test record video + uri = Uri.parse("osmand.api://stop_av_rec"); + } + + if (API_CMD_ADD_MAP_MARKER.equals(command)) { + // test marker + uri = Uri.parse("osmand.api://add_map_marker?lat=" + lat + "&lon=" + lon + "&name=Marker"); + } + + if (API_CMD_ADD_FAVORITE.equals(command)) { + // test favorite + uri = Uri.parse("osmand.api://add_favorite?lat=" + lat + "&lon=" + lon + "&name=Favorite&desc=Description&category=test2&color=red&visible=true"); + } + + if (API_CMD_START_GPX_REC.equals(command)) { + // test start gpx recording + uri = Uri.parse("osmand.api://start_gpx_rec"); + } + + if (API_CMD_STOP_GPX_REC.equals(command)) { + // test stop gpx recording + uri = Uri.parse("osmand.api://stop_gpx_rec"); + } + + if (API_CMD_SHOW_GPX.equals(command)) { + // test show gpx (path) + //File gpx = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), gpxName); + //uri = Uri.parse("osmand.api://show_gpx?path=" + URLEncoder.encode(gpx.getAbsolutePath(), "UTF-8")); + + // test show gpx (data) + uri = Uri.parse("osmand.api://show_gpx"); + intent = new Intent(Intent.ACTION_VIEW, uri); + intent.putExtra("data", AndroidUtils.getFileAsString( + new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), gpxName))); + } + + if (API_CMD_NAVIGATE_GPX.equals(command)) { + // test navigate gpx (path) + //File gpx = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), gpxName); + //uri = Uri.parse("osmand.api://navigate_gpx?force=true&path=" + URLEncoder.encode(gpx.getAbsolutePath(), "UTF-8")); + + // test navigate gpx (data) + uri = Uri.parse("osmand.api://navigate_gpx?force=true"); + intent = new Intent(Intent.ACTION_VIEW, uri); + intent.putExtra("data", AndroidUtils.getFileAsString( + new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), gpxName))); + } + + if (intent == null && uri != null) { + intent = new Intent(Intent.ACTION_VIEW, uri); + } + + if (intent != null) { + mapActivity.startActivity(intent); + } + + } catch (Exception e) { + LOG.error("Test failed", e); + } + } }