Skip to content

Commit

Permalink
Reimplement resume of paused navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 25, 2023
1 parent 12acaeb commit 485928a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 40 deletions.
30 changes: 19 additions & 11 deletions app/src/main/java/mobi/maptrek/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,13 @@ public static void setActionPanelState(boolean panelState) {
*/
@Nullable
public static MapObject getNavigationPoint() {
MapObject waypoint = null;
String navWpt = loadString(PREF_NAVIGATION_WAYPOINT, null);
if (navWpt != null) {
waypoint = new MapObject(mSharedPreferences.getFloat(PREF_NAVIGATION_LATITUDE, 0), mSharedPreferences.getFloat(PREF_NAVIGATION_LONGITUDE, 0));
waypoint.name = navWpt;
waypoint.proximity = loadInt(PREF_NAVIGATION_PROXIMITY, 0);
saveString(PREF_NAVIGATION_WAYPOINT, null);
}
float lat = mSharedPreferences.getFloat(PREF_NAVIGATION_LATITUDE, Float.NaN);
float lon = mSharedPreferences.getFloat(PREF_NAVIGATION_LONGITUDE, Float.NaN);
if (Float.isNaN(lat) || Float.isNaN(lon))
return null;
MapObject waypoint = new MapObject(lat, lon);
waypoint.name = loadString(PREF_NAVIGATION_WAYPOINT, null);
waypoint.proximity = loadInt(PREF_NAVIGATION_PROXIMITY, 0);
return waypoint;
}

Expand All @@ -195,7 +194,8 @@ public static void setNavigationPoint(@Nullable MapObject mapObject) {
saveFloat(PREF_NAVIGATION_LONGITUDE, (float) mapObject.coordinates.getLongitude());
saveInt(PREF_NAVIGATION_PROXIMITY, mapObject.proximity);
} else {
saveString(PREF_NAVIGATION_WAYPOINT, null);
remove(PREF_NAVIGATION_LATITUDE);
remove(PREF_NAVIGATION_LONGITUDE);
}
}

Expand Down Expand Up @@ -301,7 +301,7 @@ public static String[] getBitmapMaps() {

public static void setBitmapMaps(@NonNull Collection<MapFile> mapFiles) {
if (mapFiles.isEmpty()) {
saveString(PREF_BITMAP_MAP, null);
remove(PREF_BITMAP_MAP);
} else {
String[] filenames = new String[mapFiles.size()];
int i = 0;
Expand Down Expand Up @@ -329,7 +329,7 @@ public static void clearAdviceState(long advice) {
}

public static void resetAdviceState() {
saveLong(PREF_ADVICE_STATES, 0L);
remove(PREF_ADVICE_STATES);
mAdviceMask = 0L;
}

Expand Down Expand Up @@ -622,6 +622,14 @@ private static void saveString(String key, String value) {
EventBus.getDefault().post(new ChangedEvent(key));
}

private static void remove(String key) {
assert mSharedPreferences != null : "Configuration not initialized";
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.remove(key);
editor.apply();
EventBus.getDefault().post(new ChangedEvent(key));
}

/** @noinspection UnusedReturnValue*/
public static boolean commit() {
assert mSharedPreferences != null : "Configuration not initialized";
Expand Down
26 changes: 16 additions & 10 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,6 @@ public void onChildViewRemoved(View parent, View child) {
boolean visible = Configuration.getZoomButtonsVisible();
mViews.coordinatorLayout.findViewById(R.id.mapZoomHolder).setVisibility(visible ? View.VISIBLE : View.GONE);

// Resume navigation
//MapObject mapObject = Configuration.getNavigationPoint();
//if (mapObject != null)
// startNavigation(mapObject, Configuration.getNavigationViaRoute());

// Get back to full screen mode after edge swipe
/*
decorView.setOnSystemUiVisibilityChangeListener(
Expand Down Expand Up @@ -906,6 +901,10 @@ protected void onStart() {
resultReceiver.setCallback(this);
mResultReceiver = new WeakReference<>(resultReceiver);

// Resume navigation
if (Configuration.getNavigationPoint() != null)
resumeNavigation();

if (Configuration.getConfirmExitEnabled())
getOnBackPressedDispatcher().addCallback(this, mBackPressedCallback);
mBackPressedCallback.setEnabled(mFragmentManager.getBackStackEntryCount() == 0);
Expand Down Expand Up @@ -1436,7 +1435,7 @@ public void onDismissed(Snackbar transientBottomBar, int event) {
removeMarker();
MapObject mapObject = new MapObject(mSelectedPoint.getLatitude(), mSelectedPoint.getLongitude());
mapObject.name = getString(R.string.selectedLocation);
startNavigation(mapObject, false);
startNavigation(mapObject);
return true;
} else if (action == R.id.actionFindRouteHere) {
removeMarker();
Expand Down Expand Up @@ -2002,14 +2001,13 @@ public void onServiceDisconnected(ComponentName className) {
}
};

private void startNavigation(MapObject mapObject, boolean viaRoute) {
private void startNavigation(MapObject mapObject) {
enableNavigation();
Intent i = new Intent(this, NavigationService.class).setAction(NavigationService.NAVIGATE_TO_POINT);
i.putExtra(NavigationService.EXTRA_NAME, mapObject.name);
i.putExtra(NavigationService.EXTRA_LATITUDE, mapObject.coordinates.getLatitude());
i.putExtra(NavigationService.EXTRA_LONGITUDE, mapObject.coordinates.getLongitude());
i.putExtra(NavigationService.EXTRA_PROXIMITY, mapObject.proximity);
i.putExtra(NavigationService.EXTRA_ROUTE, viaRoute);
startService(i);
if (mLocationState == LocationState.DISABLED)
askForPermission(PERMISSIONS_REQUEST_FINE_LOCATION);
Expand All @@ -2035,6 +2033,14 @@ private void startNavigation(Route route) {
askForPermission(PERMISSIONS_REQUEST_FINE_LOCATION);
}

private void resumeNavigation() {
enableNavigation();
Intent i = new Intent(this, NavigationService.class).setAction(NavigationService.RESUME_NAVIGATION);
startService(i);
if (mLocationState == LocationState.DISABLED)
askForPermission(PERMISSIONS_REQUEST_FINE_LOCATION);
}

@Override
public void stopNavigation() {
startService(new Intent(this, NavigationService.class).setAction(NavigationService.STOP_NAVIGATION));
Expand Down Expand Up @@ -2085,7 +2091,7 @@ public void disableTracking() {

@Override
public void navigateTo(@NonNull GeoPoint coordinates, @Nullable String name) {
startNavigation(new MapObject(name, coordinates), false);
startNavigation(new MapObject(name, coordinates));
}

@Override
Expand Down Expand Up @@ -2629,7 +2635,7 @@ public void onWaypointDetails(Waypoint waypoint, boolean fromList) {

@Override
public void onWaypointNavigate(Waypoint waypoint) {
startNavigation(waypoint, false);
startNavigation(waypoint);
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/mobi/maptrek/io/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static Manager getDataManager(String file) {
if (file.toLowerCase().endsWith(TrackManager.EXTENSION)) {
return new TrackManager();
}
if (file.toLowerCase().endsWith(RouteManager.EXTENSION)) {
return new RouteManager();
}
if (file.toLowerCase().endsWith(GPXManager.EXTENSION)) {
return new GPXManager();
}
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/mobi/maptrek/io/RouteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ public void saveData(OutputStream outputStream, FileDataSource source, @Nullable
output.writeRawVarint32(getSerializedInstructionSize(instruction));
output.writeInt32(FIELD_INSTRUCTION_LATITUDE, instruction.latitudeE6);
output.writeInt32(FIELD_INSTRUCTION_LONGITUDE, instruction.longitudeE6);
output.writeString(FIELD_INSTRUCTION_TEXT, instruction.text);
output.writeInt32(FIELD_INSTRUCTION_SIGN, instruction.sign);
if (instruction.text != null)
output.writeString(FIELD_INSTRUCTION_TEXT, instruction.text);
if (instruction.sign != Route.Instruction.UNDEFINED)
output.writeInt32(FIELD_INSTRUCTION_SIGN, instruction.sign);
progress++;
if (progressListener != null)
progressListener.onProgressChanged(progress);
Expand Down Expand Up @@ -206,7 +208,7 @@ private void readInstruction(Route route, CodedInputStream input) throws IOExcep
int latitudeE6 = 0;
int longitudeE6 = 0;
String text = null;
int sign = Route.Instruction.IGNORE;
int sign = Route.Instruction.UNDEFINED;

boolean done = false;
while (!done) {
Expand Down Expand Up @@ -302,8 +304,10 @@ public int getSerializedInstructionSize(Route.Instruction instruction) {
int size = 0;
size += CodedOutputStream.computeInt32Size(FIELD_INSTRUCTION_LATITUDE, instruction.latitudeE6);
size += CodedOutputStream.computeInt32Size(FIELD_INSTRUCTION_LONGITUDE, instruction.longitudeE6);
size += CodedOutputStream.computeStringSize(FIELD_INSTRUCTION_TEXT, instruction.text);
size += CodedOutputStream.computeInt32Size(FIELD_INSTRUCTION_SIGN, instruction.sign);
if (instruction.text != null)
size += CodedOutputStream.computeStringSize(FIELD_INSTRUCTION_TEXT, instruction.text);
if (instruction.sign != Route.Instruction.UNDEFINED)
size += CodedOutputStream.computeInt32Size(FIELD_INSTRUCTION_SIGN, instruction.sign);
return size;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public abstract class BaseNavigationService extends Service
* alone.
*/
public static final String PAUSE_NAVIGATION = "mobi.maptrek.location.pauseNavigation";
/**
* Service command to resume paused navigation.
* alone.
*/
public static final String RESUME_NAVIGATION = "mobi.maptrek.location.resumeNavigation";
/**
* Service command to stop navigation.
*/
Expand Down
74 changes: 60 additions & 14 deletions app/src/main/java/mobi/maptrek/location/NavigationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;

import mobi.maptrek.Configuration;
import mobi.maptrek.MainActivity;
Expand Down Expand Up @@ -139,14 +140,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (action.equals(NAVIGATE_TO_POINT)) {
if (extras == null)
return START_NOT_STICKY;
boolean viaRoute = extras.getBoolean(EXTRA_ROUTE);
if (viaRoute) {
} else {
MapObject mo = new MapObject(extras.getDouble(EXTRA_LATITUDE), extras.getDouble(EXTRA_LONGITUDE));
mo.name = extras.getString(EXTRA_NAME);
mo.proximity = extras.getInt(EXTRA_PROXIMITY);
navigateTo(mo);
}
MapObject mo = new MapObject(extras.getDouble(EXTRA_LATITUDE), extras.getDouble(EXTRA_LONGITUDE));
mo.name = extras.getString(EXTRA_NAME);
mo.proximity = extras.getInt(EXTRA_PROXIMITY);
navigateTo(mo);
}
if (action.equals(NAVIGATE_TO_OBJECT)) {
if (extras == null)
Expand All @@ -167,6 +164,24 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (start != -1)
setRouteWaypoint(start);
}
if (action.equals(RESUME_NAVIGATION)) {
navCurrentRoutePoint = Configuration.getNavigationRoutePoint();
navDirection = Configuration.getNavigationRouteDirection();
navWaypoint = Configuration.getNavigationPoint();
if (navWaypoint == null)
return START_NOT_STICKY;
if (Configuration.getNavigationViaRoute()) {
loadRoute();
if (navRoute != null) {
resumeRoute();
} else {
logger.error("No route to resume");
stopNavigation();
}
} else {
resumeWaypoint();
}
}
if (action.equals(STOP_NAVIGATION) || action.equals(PAUSE_NAVIGATION)) {
mForeground = false;
stopForeground(true);
Expand Down Expand Up @@ -340,7 +355,8 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}

private void connect() {
EventBus.getDefault().register(this);
if (!EventBus.getDefault().isRegistered(this))
EventBus.getDefault().register(this);
bindService(new Intent(this, LocationService.class), locationConnection, BIND_AUTO_CREATE);
}

Expand Down Expand Up @@ -454,15 +470,21 @@ private void clearNavigation() {
avgVMG[0] = 0.0;
avgVMG[1] = 0.0;
avgVMG[2] = 0.0;

Configuration.setNavigationPoint(null);
}

private void navigateTo(final MapObject waypoint) {
if (navWaypoint != null)
stopNavigation();
navWaypoint = waypoint;

resumeWaypoint();
}

private void resumeWaypoint() {
connect();

navWaypoint = waypoint;
navProximity = navWaypoint.proximity > 0 ? navWaypoint.proximity : DEFAULT_WAYPOINT_PROXIMITY;
updateNavigationState(STATE_STARTED);
if (mLastKnownLocation != null)
Expand All @@ -472,14 +494,17 @@ private void navigateTo(final MapObject waypoint) {
private void navigateTo(final Route route, final int direction) {
if (navWaypoint != null)
stopNavigation();

saveRoute();
connect();

navRoute = route;
navDirection = direction;
navCurrentRoutePoint = navDirection == 1 ? 1 : navRoute.length() - 2;

saveRoute();
resumeRoute();
}

private void resumeRoute() {
connect();

navWaypoint = new MapObject(navRoute.get(navCurrentRoutePoint).getCoordinates());
prevWaypoint = new MapObject(navRoute.get(navCurrentRoutePoint - navDirection).getCoordinates());
navProximity = DEFAULT_ROUTE_PROXIMITY;
Expand Down Expand Up @@ -740,6 +765,7 @@ private void updateNavigationStatus() {
logger.trace("Status dispatched");
}

/** @noinspection unused*/
@Subscribe
public void onMapObjectUpdated(MapObject.UpdatedEvent event) {
logger.error("onMapObjectUpdated({})", (event.mapObject.equals(navWaypoint)));
Expand Down Expand Up @@ -771,6 +797,26 @@ public void onError(FileDataSource source, Exception e) {
});
}

private void loadRoute() {
File dataDir = getExternalFilesDir("data");
if (dataDir == null) {
logger.error("Can not load route: application data folder missing");
return; // TODO: what to do?
}
File file = new File(dataDir, FileUtils.sanitizeFilename("CurrentRoute") + RouteManager.EXTENSION);
Manager manager = Manager.getDataManager(file.getName());
if (manager != null) {
try {
FileDataSource source = manager.loadData(new FileInputStream(file), file.getAbsolutePath());
source.path = file.getAbsolutePath();
source.setLoaded();
navRoute = source.routes.get(0);
} catch (Exception e) {
logger.error("Saved route not found");
}
}
}

private final ServiceConnection locationConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mLocationService = (ILocationService) service;
Expand Down

0 comments on commit 485928a

Please sign in to comment.