From 9df2319b3020c49f6a661eb16e40ef87d74e9bd6 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 20 Sep 2017 16:19:42 +0300 Subject: [PATCH 1/5] Save current location for 15 minutes --- .../src/net/osmand/plus/views/MapMarkersLayer.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java index ec93c52755..526f398de8 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java @@ -35,6 +35,8 @@ import gnu.trove.list.array.TIntArrayList; public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvider, IContextMenuProviderSelection, ContextMenuLayer.IMoveObjectProvider { + + private static final long STALE_LOCATION_TIMEOUT = 1000 * 60 * 15; // 15 minutes protected static final int DIST_TO_SHOW = 80; private final MapActivity map; @@ -42,6 +44,8 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi private MapMarkersWidgetsFactory widgetsFactory; + private Location myLoc; + private Paint bitmapPaint; private Bitmap markerBitmapBlue; private Bitmap markerBitmapGreen; @@ -189,7 +193,14 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi @Override public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings nightMode) { - Location myLoc = map.getMyApplication().getLocationProvider().getLastKnownLocation(); + Location newloc = map.getMyApplication().getLocationProvider().getLastKnownLocation(); + if (newloc == null) { + if (myLoc != null && System.currentTimeMillis() - myLoc.getTime() > STALE_LOCATION_TIMEOUT) { + myLoc = null; + } + } else { + myLoc = newloc; + } widgetsFactory.updateInfo(myLoc == null ? tileBox.getCenterLatLon() : new LatLon(myLoc.getLatitude(), myLoc.getLongitude()), tileBox.getZoom()); OsmandSettings settings = map.getMyApplication().getSettings(); From 1c9388a2c1b6739af04fd456bf89eed3e609279d Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 20 Sep 2017 18:12:07 +0300 Subject: [PATCH 2/5] Change logic for getting location --- .../osmand/plus/OsmAndLocationProvider.java | 28 +++++++++++++++++-- .../osmand/plus/views/MapControlsLayer.java | 2 +- .../osmand/plus/views/MapMarkersLayer.java | 12 +------- .../net/osmand/plus/views/OsmandMapLayer.java | 20 ------------- .../osmand/plus/views/PointLocationLayer.java | 7 ++--- 5 files changed, 31 insertions(+), 38 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index d07a2b40d4..ad96b84af6 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -38,6 +38,7 @@ import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.provider.Settings; +import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.util.Log; @@ -69,11 +70,14 @@ public class OsmAndLocationProvider implements SensorEventListener { private static final int GPS_DIST_REQUEST = 0; private static final int NOT_SWITCH_TO_NETWORK_WHEN_GPS_LOST_MS = 12000; - private static final long STALE_LOCATION_TIMEOUT = 1000 * 60 * 60; // 60 minutes - public static final long STALE_LOCATION_TIMEOUT_FOR_ICON = 1000 * 60 * 5; // 5 minutes + private static final long STALE_LOCATION_TIMEOUT = 1000 * 60 * 5; // 5 minutes + private static final long STALE_LOCATION_TIMEOUT_FOR_UI = 1000 * 60 * 15; // 15 minutes private static final int UPDATES_BEFORE_CHECK_LOCATION = 100; private int updatesCounter; + private int updatesCounterUi; + + private net.osmand.Location cachedLocation; private long lastTimeGPSLocationFixed = 0; private boolean gpsSignalLost; @@ -877,6 +881,26 @@ public class OsmAndLocationProvider implements SensorEventListener { return location; } + @Nullable + public net.osmand.Location getLastStaleKnownLocation() { + if (updatesCounterUi == 0) { + net.osmand.Location newLoc = getLastKnownLocation(); + if (newLoc == null) { + if (cachedLocation != null && System.currentTimeMillis() - cachedLocation.getTime() > STALE_LOCATION_TIMEOUT_FOR_UI) { + cachedLocation = null; + } + } else { + cachedLocation = newLoc; + } + } + if (updatesCounterUi == UPDATES_BEFORE_CHECK_LOCATION) { + updatesCounterUi = 0; + } else { + updatesCounterUi++; + } + return cachedLocation; + } + public void showNavigationInfo(TargetPoint pointToNavigate, Context uiActivity) { getNavigationInfo().show(pointToNavigate, getHeading(), uiActivity); diff --git a/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java b/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java index 50bb769fca..88dd4bf182 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java @@ -797,7 +797,7 @@ public class MapControlsLayer extends OsmandMapLayer { private void updateMyLocation(RoutingHelper rh, boolean dialogOpened) { Location lastKnownLocation = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation(); - boolean enabled = lastKnownLocation != null && !isLocationOutdated(lastKnownLocation); + boolean enabled = lastKnownLocation != null; boolean tracked = mapActivity.getMapViewTrackingUtilities().isMapLinkedToLocation(); if (!enabled) { diff --git a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java index 526f398de8..b20504ac30 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java @@ -36,7 +36,6 @@ import gnu.trove.list.array.TIntArrayList; public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvider, IContextMenuProviderSelection, ContextMenuLayer.IMoveObjectProvider { - private static final long STALE_LOCATION_TIMEOUT = 1000 * 60 * 15; // 15 minutes protected static final int DIST_TO_SHOW = 80; private final MapActivity map; @@ -44,8 +43,6 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi private MapMarkersWidgetsFactory widgetsFactory; - private Location myLoc; - private Paint bitmapPaint; private Bitmap markerBitmapBlue; private Bitmap markerBitmapGreen; @@ -193,14 +190,7 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi @Override public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings nightMode) { - Location newloc = map.getMyApplication().getLocationProvider().getLastKnownLocation(); - if (newloc == null) { - if (myLoc != null && System.currentTimeMillis() - myLoc.getTime() > STALE_LOCATION_TIMEOUT) { - myLoc = null; - } - } else { - myLoc = newloc; - } + Location myLoc = map.getMyApplication().getLocationProvider().getLastStaleKnownLocation(); widgetsFactory.updateInfo(myLoc == null ? tileBox.getCenterLatLon() : new LatLon(myLoc.getLatitude(), myLoc.getLongitude()), tileBox.getZoom()); OsmandSettings settings = map.getMyApplication().getSettings(); diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index dc09eca433..0c2592cd07 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -16,13 +16,11 @@ import android.os.Build; import android.support.annotation.NonNull; import android.view.MotionEvent; -import net.osmand.Location; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; import net.osmand.data.RotatedTileBox; import net.osmand.plus.ContextMenuAdapter; -import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.render.OsmandRenderer; @@ -40,8 +38,6 @@ import gnu.trove.list.array.TIntArrayList; public abstract class OsmandMapLayer { - protected static final int UPDATES_BEFORE_CHECK_LOCATION = 40; - protected List fullObjectsLatLon; protected List smallObjectsLatLon; @@ -51,22 +47,6 @@ public abstract class OsmandMapLayer { TWO_POINTERS_ZOOM_OUT } - private int updatesCounter; - private boolean locationOutdated; - - boolean isLocationOutdated(Location location) { - if (location != null && updatesCounter == 0) { - locationOutdated = System.currentTimeMillis() - location.getTime() > - OsmAndLocationProvider.STALE_LOCATION_TIMEOUT_FOR_ICON; - } - if (updatesCounter == UPDATES_BEFORE_CHECK_LOCATION) { - updatesCounter = 0; - } else { - updatesCounter++; - } - return locationOutdated; - } - public boolean isMapGestureAllowed(MapGestureType type) { return true; } diff --git a/OsmAnd/src/net/osmand/plus/views/PointLocationLayer.java b/OsmAnd/src/net/osmand/plus/views/PointLocationLayer.java index f66c05713f..860b0cc58f 100644 --- a/OsmAnd/src/net/osmand/plus/views/PointLocationLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/PointLocationLayer.java @@ -64,8 +64,7 @@ public class PointLocationLayer extends OsmandMapLayer implements ContextMenuLay aroundArea.setAntiAlias(true); locationProvider = view.getApplication().getLocationProvider(); - updateIcons(view.getSettings().getApplicationMode(), false, - isLocationOutdated(locationProvider.getLastKnownLocation())); + updateIcons(view.getSettings().getApplicationMode(), false, locationProvider.getLastKnownLocation() == null); } @Override @@ -86,9 +85,9 @@ public class PointLocationLayer extends OsmandMapLayer implements ContextMenuLay } // draw boolean nm = nightMode != null && nightMode.isNightMode(); - Location lastKnownLocation = locationProvider.getLastKnownLocation(); + Location lastKnownLocation = locationProvider.getLastStaleKnownLocation(); updateIcons(view.getSettings().getApplicationMode(), nm, - isLocationOutdated(lastKnownLocation)); + view.getApplication().getLocationProvider().getLastKnownLocation() == null); if(lastKnownLocation == null || view == null){ return; } From cf6265201c6e0dc6e103b5daad0952249296a217 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 20 Sep 2017 18:27:14 +0300 Subject: [PATCH 3/5] Do not close the menu after changing the switch state --- .../ShowDirectionBottomSheetDialogFragment.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/ShowDirectionBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/ShowDirectionBottomSheetDialogFragment.java index 7c84e30f85..a7d10456db 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/ShowDirectionBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/ShowDirectionBottomSheetDialogFragment.java @@ -90,29 +90,31 @@ public class ShowDirectionBottomSheetDialogFragment extends BottomSheetDialogFra ((TextView) mainView.findViewById(R.id.show_direction_title)).setTextColor(getResources().getColor(R.color.ctx_menu_info_text_dark)); } - CompoundButton showArrowsToggle = (CompoundButton) mainView.findViewById(R.id.show_arrows_switch); + final CompoundButton showArrowsToggle = (CompoundButton) mainView.findViewById(R.id.show_arrows_switch); showArrowsToggle.setChecked(settings.SHOW_ARROWS_TO_FIRST_MARKERS.get()); mainView.findViewById(R.id.show_arrows_row).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - settings.SHOW_ARROWS_TO_FIRST_MARKERS.set(!settings.SHOW_ARROWS_TO_FIRST_MARKERS.get()); + boolean newState = !settings.SHOW_ARROWS_TO_FIRST_MARKERS.get(); + settings.SHOW_ARROWS_TO_FIRST_MARKERS.set(newState); + showArrowsToggle.setChecked(newState); if (getMapActivity() != null) { getMapActivity().refreshMap(); } - dismiss(); } }); - CompoundButton showLinesToggle = (CompoundButton) mainView.findViewById(R.id.show_guide_line_switch); + final CompoundButton showLinesToggle = (CompoundButton) mainView.findViewById(R.id.show_guide_line_switch); showLinesToggle.setChecked(settings.SHOW_LINES_TO_FIRST_MARKERS.get()); mainView.findViewById(R.id.show_guide_line_row).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - settings.SHOW_LINES_TO_FIRST_MARKERS.set(!settings.SHOW_LINES_TO_FIRST_MARKERS.get()); + boolean newState = !settings.SHOW_LINES_TO_FIRST_MARKERS.get(); + settings.SHOW_LINES_TO_FIRST_MARKERS.set(newState); + showLinesToggle.setChecked(newState); if (getMapActivity() != null) { getMapActivity().refreshMap(); } - dismiss(); } }); From 53c64d553b3ecce379a3b191e47ab1763887b736 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 20 Sep 2017 18:48:28 +0300 Subject: [PATCH 4/5] Draw a line to marker with marker color --- .../osmand/plus/views/MapMarkersLayer.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java index b20504ac30..556e90c20f 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java @@ -228,27 +228,26 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi List activeMapMarkers = markersHelper.getMapMarkers(); if (settings.SHOW_LINES_TO_FIRST_MARKERS.get() && myLoc != null) { - linePath.reset(); - tx.clear(); - ty.clear(); int locX = tileBox.getPixXFromLonNoRot(myLoc.getLongitude()); int locY = tileBox.getPixYFromLatNoRot(myLoc.getLatitude()); + int[] colors = MapMarker.getColors(map); for (int i = 0; i < activeMapMarkers.size() && i < 2; i++) { - int markerX = tileBox.getPixXFromLonNoRot(activeMapMarkers.get(i).getLongitude()); - int markerY = tileBox.getPixYFromLatNoRot(activeMapMarkers.get(i).getLatitude()); + MapMarker marker = activeMapMarkers.get(i); + int markerX = tileBox.getPixXFromLonNoRot(marker.getLongitude()); + int markerY = tileBox.getPixYFromLatNoRot(marker.getLatitude()); + linePath.reset(); + tx.clear(); + ty.clear(); + linePath.moveTo(locX, locY); + linePath.lineTo(markerX, markerY); + tx.add(locX); + ty.add(locY); tx.add(markerX); ty.add(markerY); - if (i == 0) { - linePath.moveTo(markerX, markerY); - linePath.lineTo(locX, locY); - tx.add(locX); - ty.add(locY); - } else { - linePath.lineTo(markerX, markerY); - } + calculatePath(tileBox, tx, ty, linePath); + lineAttrs.paint.setColor(colors[marker.colorIndex]); + canvas.drawPath(linePath, lineAttrs.paint); } - calculatePath(tileBox, tx, ty, linePath); - canvas.drawPath(linePath, lineAttrs.paint); } for (MapMarker marker : activeMapMarkers) { From e29953acdeb1b1d5fc03657820bff00d9607e7fc Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 20 Sep 2017 18:58:21 +0300 Subject: [PATCH 5/5] Always give random color to markers --- .../src/net/osmand/plus/MapMarkersHelper.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index a2d866ceec..78fd2d5df8 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -15,7 +15,6 @@ import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; -import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -411,13 +410,10 @@ public class MapMarkersHelper { for (MapMarker marker : markers) { if (marker.id.equals(group.getId() + name)) { exists = true; - int colorIndex = MapMarker.getColorIndex(ctx, ColorDialogs.getNearestColor(group.getColor(), MapMarker.getColors(ctx))); - boolean updateColor = group.getColor() != -1 && marker.colorIndex != colorIndex; - if (!marker.history && (!marker.point.equals(latLon) || updateColor)) { + if (!marker.history && (!marker.point.equals(latLon))) { for (MapMarker m : mapMarkers) { if (m.id.equals(marker.id)) { m.point = latLon; - m.colorIndex = colorIndex; updateMapMarker(m, true); break; } @@ -634,8 +630,7 @@ public class MapMarkersHelper { private void addMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group) { if (points.size() > 0) { - boolean randomColor = group == null || group.getColor() == -1; - int colorIndex = randomColor ? -1 : MapMarker.getColorIndex(ctx, ColorDialogs.getNearestColor(group.getColor(), MapMarker.getColors(ctx))); + int colorIndex = -1; for (int i = 0; i < points.size(); i++) { LatLon point = points.get(i); PointDescription historyName = historyNames.get(i); @@ -648,16 +643,14 @@ public class MapMarkersHelper { if (pointDescription.isLocation() && Algorithms.isEmpty(pointDescription.getName())) { pointDescription.setName(PointDescription.getSearchAddressStr(ctx)); } - if (randomColor) { - if (colorIndex == -1) { - if (mapMarkers.size() > 0) { - colorIndex = (mapMarkers.get(mapMarkers.size() - 1).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; - } else { - colorIndex = 0; - } + if (colorIndex == -1) { + if (mapMarkers.size() > 0) { + colorIndex = (mapMarkers.get(mapMarkers.size() - 1).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; } else { - colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; + colorIndex = 0; } + } else { + colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; } MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);