From 0b7d3d03b5aa2272315fae4a1af8ca31bced3b54 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 11 Oct 2017 13:36:15 +0300 Subject: [PATCH 1/3] Add location listener --- .../plus/mapmarkers/PlanRouteFragment.java | 48 +++++++++++++++++-- .../adapters/MapMarkersListAdapter.java | 1 + 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java index c053cfdd67..e5fb0be4e9 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java @@ -38,6 +38,7 @@ import net.osmand.plus.IconsCache; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -62,7 +63,7 @@ import java.util.List; import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT; -public class PlanRouteFragment extends Fragment { +public class PlanRouteFragment extends Fragment implements OsmAndLocationListener { public static final String TAG = "PlanRouteFragment"; @@ -83,6 +84,8 @@ public class PlanRouteFragment extends Fragment { private boolean wasCollapseButtonVisible; private boolean cancelSnapToRoad = true; + private Location location; + private View mainView; private RecyclerView markersRv; @@ -255,7 +258,6 @@ public class PlanRouteFragment extends Fragment { adapter = new MapMarkersListAdapter(mapActivity); adapter.setHasStableIds(true); - adapter.calculateStartAndFinishPos(); adapter.setSnappedToRoadPoints(planRouteContext.getSnappedToRoadPoints()); final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter)); touchHelper.attachToRecyclerView(markersRv); @@ -322,12 +324,52 @@ public class PlanRouteFragment extends Fragment { return view; } + @Override + public void onResume() { + super.onResume(); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getMyApplication().getLocationProvider().addLocationListener(this); + } + } + + @Override + public void onPause() { + super.onPause(); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getMyApplication().getLocationProvider().removeLocationListener(this); + } + } + @Override public void onDestroyView() { super.onDestroyView(); exitPlanRouteMode(); } + @Override + public void updateLocation(Location loc) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + Location location = mapActivity.getMyApplication().getLocationProvider().getLastStaleKnownLocation(); + boolean newLocation = (this.location == null && location != null) || location == null; + boolean locationChanged = this.location != null && location != null + && this.location.getLatitude() != location.getLatitude() + && this.location.getLongitude() != location.getLongitude(); + if (newLocation || locationChanged) { + this.location = location; + adapter.reloadData(); + try { + adapter.notifyDataSetChanged(); + } catch (Exception e) { + // to avoid crash because of: + // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling + } + } + } + } + private MapActivity getMapActivity() { return (MapActivity) getActivity(); } @@ -456,7 +498,6 @@ public class PlanRouteFragment extends Fragment { if (mapActivity != null) { markersHelper.reverseActiveMarkersOrder(); adapter.reloadData(); - adapter.calculateStartAndFinishPos(); adapter.notifyDataSetChanged(); planRouteContext.recreateSnapTrkSegment(); } @@ -816,7 +857,6 @@ public class PlanRouteFragment extends Fragment { mapActivity.getMyApplication().getMapMarkersHelper().addSelectedMarkersToTop(res); adapter.reloadData(); - adapter.calculateStartAndFinishPos(); adapter.notifyDataSetChanged(); planRouteContext.recreateSnapTrkSegment(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java index cdfed89b48..92bfb3d868 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java @@ -268,6 +268,7 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter Date: Wed, 11 Oct 2017 14:10:09 +0300 Subject: [PATCH 2/3] Recalculate only if my location changed by 50 meters --- OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java index e5fb0be4e9..f833b4e9a5 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java @@ -66,6 +66,7 @@ import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT; public class PlanRouteFragment extends Fragment implements OsmAndLocationListener { public static final String TAG = "PlanRouteFragment"; + private static final int MIN_DISTANCE_FOR_RECALCULATE = 50; // in meters private MapMarkersHelper markersHelper; private MarkersPlanRouteContext planRouteContext; @@ -357,7 +358,9 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene boolean locationChanged = this.location != null && location != null && this.location.getLatitude() != location.getLatitude() && this.location.getLongitude() != location.getLongitude(); - if (newLocation || locationChanged) { + boolean farEnough = locationChanged && MapUtils.getDistance(this.location.getLatitude(), this.location.getLongitude(), + location.getLatitude(), location.getLongitude()) >= MIN_DISTANCE_FOR_RECALCULATE; + if (newLocation || farEnough) { this.location = location; adapter.reloadData(); try { From fc3c3f45ef7ebfb5b576e287f39b1b4c0c672a28 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 11 Oct 2017 14:32:54 +0300 Subject: [PATCH 3/3] Use ui thread for adapter updates --- .../plus/mapmarkers/PlanRouteFragment.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java index f833b4e9a5..f71e2c3daa 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java @@ -353,7 +353,7 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene public void updateLocation(Location loc) { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { - Location location = mapActivity.getMyApplication().getLocationProvider().getLastStaleKnownLocation(); + final Location location = mapActivity.getMyApplication().getLocationProvider().getLastStaleKnownLocation(); boolean newLocation = (this.location == null && location != null) || location == null; boolean locationChanged = this.location != null && location != null && this.location.getLatitude() != location.getLatitude() @@ -361,14 +361,19 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene boolean farEnough = locationChanged && MapUtils.getDistance(this.location.getLatitude(), this.location.getLongitude(), location.getLatitude(), location.getLongitude()) >= MIN_DISTANCE_FOR_RECALCULATE; if (newLocation || farEnough) { - this.location = location; - adapter.reloadData(); - try { - adapter.notifyDataSetChanged(); - } catch (Exception e) { - // to avoid crash because of: - // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling - } + mapActivity.getMyApplication().runInUIThread(new Runnable() { + @Override + public void run() { + PlanRouteFragment.this.location = location; + adapter.reloadData(); + try { + adapter.notifyDataSetChanged(); + } catch (Exception e) { + // to avoid crash because of: + // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling + } + } + }); } } }