Add navigate to aidl

This commit is contained in:
PavelRatushny 2017-09-01 12:13:47 +03:00
parent f14dfc70c8
commit d6d520dd4d
5 changed files with 262 additions and 0 deletions

View file

@ -41,6 +41,8 @@ import net.osmand.aidl.maplayer.AddMapLayerParams;
import net.osmand.aidl.maplayer.RemoveMapLayerParams;
import net.osmand.aidl.maplayer.UpdateMapLayerParams;
import net.osmand.aidl.navigation.NavigateParams;
import net.osmand.aidl.note.TakePhotoNoteParams;
import net.osmand.aidl.note.StartVideoRecordingParams;
import net.osmand.aidl.note.StartAudioRecordingParams;
@ -92,4 +94,6 @@ interface IOsmAndAidlInterface {
boolean startAudioRecording(in StartAudioRecordingParams params);
boolean stopRecording(in StopRecordingParams params);
boolean navigate(in NavigateParams params);
}

View file

@ -2,11 +2,13 @@ package net.osmand.aidl;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AlertDialog;
import android.view.View;
import net.osmand.IndexConstants;
@ -22,6 +24,7 @@ import net.osmand.aidl.mapwidget.AMapWidget;
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.GPXDatabase.GpxDataItem;
import net.osmand.plus.GPXUtilities;
@ -32,11 +35,13 @@ 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.dialogs.ConfigureMapMenu;
import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.views.AidlMapLayer;
import net.osmand.plus.views.MapInfoLayer;
import net.osmand.plus.views.OsmandMapLayer;
@ -66,6 +71,15 @@ public class OsmandAidlApi {
private static final String AIDL_ZOOM = "aidl_zoom";
private static final String AIDL_ANIMATED = "aidl_animated";
private static final String AIDL_START_NAME = "aidl_start_name";
private static final String AIDL_START_LAT = "aidl_start_lat";
private static final String AIDL_START_LON = "aidl_start_lon";
private static final String AIDL_DEST_NAME = "aidl_dest_name";
private static final String AIDL_DEST_LAT = "aidl_dest_lat";
private static final String AIDL_DEST_LON = "aidl_dest_lon";
private static final String AIDL_PROFILE = "aidl_profile";
private static final String AIDL_FORCE = "aidl_force";
private static final String AIDL_OBJECT_ID = "aidl_object_id";
private static final String AIDL_ADD_MAP_WIDGET = "aidl_add_map_widget";
@ -79,6 +93,16 @@ public class OsmandAidlApi {
private static final String AIDL_START_AUDIO_RECORDING = "aidl_start_audio_recording";
private static final String AIDL_STOP_RECORDING = "aidl_stop_recording";
private static final String AIDL_NAVIGATE = "aidl_navigate";
private static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR;
private static final ApplicationMode[] VALID_PROFILES = new ApplicationMode[]{
ApplicationMode.CAR,
ApplicationMode.BICYCLE,
ApplicationMode.PEDESTRIAN
};
private OsmandApplication app;
private Map<String, AMapWidget> widgets = new ConcurrentHashMap<>();
private Map<String, TextInfoWidget> widgetControls = new ConcurrentHashMap<>();
@ -95,6 +119,7 @@ public class OsmandAidlApi {
private BroadcastReceiver startVideoRecordingReceiver;
private BroadcastReceiver startAudioRecordingReceiver;
private BroadcastReceiver stopRecordingReceiver;
private BroadcastReceiver navigateReceiver;
public OsmandAidlApi(OsmandApplication app) {
this.app = app;
@ -111,6 +136,7 @@ public class OsmandAidlApi {
registerStartVideoRecordingReceiver(mapActivity);
registerStartAudioRecordingReceiver(mapActivity);
registerStopRecordingReceiver(mapActivity);
registerNavigateReceiver(mapActivity);
}
public void onDestroyMapActivity(final MapActivity mapActivity) {
@ -197,6 +223,14 @@ public class OsmandAidlApi {
}
stopRecordingReceiver = null;
}
if (navigateReceiver != null) {
try {
mapActivity.unregisterReceiver(navigateReceiver);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
navigateReceiver = null;
}
}
private void registerRefreshMapReceiver(final MapActivity mapActivity) {
@ -407,6 +441,98 @@ public class OsmandAidlApi {
mapActivity.registerReceiver(stopRecordingReceiver, new IntentFilter(AIDL_STOP_RECORDING));
}
private void registerNavigateReceiver(final MapActivity mapActivity) {
navigateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String profileStr = intent.getStringExtra(AIDL_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) {
String startName = intent.getStringExtra(AIDL_START_NAME);
if (Algorithms.isEmpty(startName)) {
startName = "";
}
String destName = intent.getStringExtra(AIDL_DEST_NAME);
if (Algorithms.isEmpty(destName)) {
destName = "";
}
final LatLon start;
final PointDescription startDesc;
double startLat = intent.getDoubleExtra(AIDL_START_LAT, 0);
double startLon = intent.getDoubleExtra(AIDL_START_LON, 0);
if (startLat != 0 && startLon != 0) {
start = new LatLon(startLat, startLon);
startDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, startName);
} else {
start = null;
startDesc = null;
}
double destLat = intent.getDoubleExtra(AIDL_DEST_LAT, 0);
double destLon = intent.getDoubleExtra(AIDL_DEST_LON, 0);
final LatLon dest = new LatLon(destLat, destLon);
final PointDescription destDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, destName);
final RoutingHelper routingHelper = app.getRoutingHelper();
boolean force = intent.getBooleanExtra(AIDL_FORCE, true);
if (routingHelper.isFollowingMode() && !force) {
AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm();
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (!routingHelper.isFollowingMode()) {
startNavigation(mapActivity, null, start, startDesc, dest, destDesc, profile);
}
}
});
} else {
startNavigation(mapActivity, null, start, startDesc, dest, destDesc, profile);
}
}
}
};
mapActivity.registerReceiver(navigateReceiver, new IntentFilter(AIDL_NAVIGATE));
}
private void startNavigation(MapActivity mapActivity,
GPXFile gpx,
LatLon from, PointDescription fromDesc,
LatLon to, PointDescription toDesc,
ApplicationMode mode) {
OsmandApplication app = mapActivity.getMyApplication();
RoutingHelper routingHelper = app.getRoutingHelper();
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 {
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);
routingHelper.setRoutePlanningMode(false);
mapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode();
app.getRoutingHelper().notifyIfRouteIsCalculated();
routingHelper.setCurrentLocation(app.getLocationProvider().getLastKnownLocation(), false);
}
}
public void registerMapLayers(MapActivity mapActivity) {
for (AMapLayer layer : layers.values()) {
OsmandMapLayer mapLayer = mapLayers.get(layer.getId());
@ -1003,4 +1129,19 @@ public class OsmandAidlApi {
app.sendBroadcast(intent);
return true;
}
boolean navigate(String startName, double startLat, double startLon, String destName, double destLat, double destLon, String profile, boolean force) {
Intent intent = new Intent();
intent.setAction(AIDL_NAVIGATE);
intent.putExtra(AIDL_START_NAME, startName);
intent.putExtra(AIDL_START_LAT, startLat);
intent.putExtra(AIDL_START_LON, startLon);
intent.putExtra(AIDL_DEST_NAME, destName);
intent.putExtra(AIDL_DEST_LAT, destLat);
intent.putExtra(AIDL_DEST_LON, destLon);
intent.putExtra(AIDL_PROFILE, profile);
intent.putExtra(AIDL_FORCE, force);
app.sendBroadcast(intent);
return true;
}
}

View file

@ -31,6 +31,7 @@ import net.osmand.aidl.mapmarker.UpdateMapMarkerParams;
import net.osmand.aidl.mapwidget.AddMapWidgetParams;
import net.osmand.aidl.mapwidget.RemoveMapWidgetParams;
import net.osmand.aidl.mapwidget.UpdateMapWidgetParams;
import net.osmand.aidl.navigation.NavigateParams;
import net.osmand.aidl.note.StartAudioRecordingParams;
import net.osmand.aidl.note.StopRecordingParams;
import net.osmand.aidl.note.TakePhotoNoteParams;
@ -380,5 +381,14 @@ public class OsmandAidlService extends Service {
return false;
}
}
@Override
public boolean navigate(NavigateParams params) throws RemoteException {
try {
return params != null && getApi().navigate(params.getStartName(), params.getStartLat(), params.getStartLon(), params.getDestName(), params.getDestLat(), params.getDestLon(), params.getProfile(), params.getForce());
} catch (Exception e) {
return false;
}
}
};
}

View file

@ -0,0 +1,3 @@
package net.osmand.aidl.navigation;
parcelable NavigateParams;

View file

@ -0,0 +1,104 @@
package net.osmand.aidl.navigation;
import android.os.Parcel;
import android.os.Parcelable;
public class NavigateParams implements Parcelable {
private String startName;
private double startLat;
private double startLon;
private String destName;
private double destLat;
private double destLon;
private String profile;
private boolean force;
public NavigateParams(String startName, double startLat, double startLon, String destName, double destLat, double destLon, String profile, boolean force) {
this.startName = startName;
this.startLat = startLat;
this.startLon = startLon;
this.destName = destName;
this.destLat = destLat;
this.destLon = destLon;
this.profile = profile;
this.force = force;
}
public NavigateParams(Parcel in) {
readFromParcel(in);
}
public static final Creator<NavigateParams> CREATOR = new Creator<NavigateParams>() {
@Override
public NavigateParams createFromParcel(Parcel in) {
return new NavigateParams(in);
}
@Override
public NavigateParams[] newArray(int size) {
return new NavigateParams[size];
}
};
public String getStartName() {
return startName;
}
public double getStartLat() {
return startLat;
}
public double getStartLon() {
return startLon;
}
public String getDestName() {
return destName;
}
public double getDestLat() {
return destLat;
}
public double getDestLon() {
return destLon;
}
public String getProfile() {
return profile;
}
public boolean getForce() {
return force;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(startName);
out.writeDouble(startLat);
out.writeDouble(startLon);
out.writeString(destName);
out.writeDouble(destLat);
out.writeDouble(destLon);
out.writeString(profile);
out.writeByte((byte) (force ? 1 : 0));
}
private void readFromParcel(Parcel in) {
startName = in.readString();
startLat = in.readDouble();
startLon = in.readDouble();
destName = in.readString();
destLat = in.readDouble();
destLon = in.readDouble();
profile = in.readString();
force = in.readByte() != 0;
}
@Override
public int describeContents() {
return 0;
}
}