Merge pull request #4572 from osmandapp/sasha_pasha_branch
Add location listener
This commit is contained in:
commit
7c7083d4e0
2 changed files with 53 additions and 4 deletions
|
@ -38,6 +38,7 @@ import net.osmand.plus.IconsCache;
|
||||||
import net.osmand.plus.MapMarkersHelper;
|
import net.osmand.plus.MapMarkersHelper;
|
||||||
import net.osmand.plus.MapMarkersHelper.MapMarker;
|
import net.osmand.plus.MapMarkersHelper.MapMarker;
|
||||||
import net.osmand.plus.OsmAndFormatter;
|
import net.osmand.plus.OsmAndFormatter;
|
||||||
|
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.OsmandSettings;
|
import net.osmand.plus.OsmandSettings;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
|
@ -62,9 +63,10 @@ import java.util.List;
|
||||||
|
|
||||||
import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT;
|
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";
|
public static final String TAG = "PlanRouteFragment";
|
||||||
|
private static final int MIN_DISTANCE_FOR_RECALCULATE = 50; // in meters
|
||||||
|
|
||||||
private MapMarkersHelper markersHelper;
|
private MapMarkersHelper markersHelper;
|
||||||
private MarkersPlanRouteContext planRouteContext;
|
private MarkersPlanRouteContext planRouteContext;
|
||||||
|
@ -83,6 +85,8 @@ public class PlanRouteFragment extends Fragment {
|
||||||
private boolean wasCollapseButtonVisible;
|
private boolean wasCollapseButtonVisible;
|
||||||
private boolean cancelSnapToRoad = true;
|
private boolean cancelSnapToRoad = true;
|
||||||
|
|
||||||
|
private Location location;
|
||||||
|
|
||||||
private View mainView;
|
private View mainView;
|
||||||
private RecyclerView markersRv;
|
private RecyclerView markersRv;
|
||||||
|
|
||||||
|
@ -255,7 +259,6 @@ public class PlanRouteFragment extends Fragment {
|
||||||
|
|
||||||
adapter = new MapMarkersListAdapter(mapActivity);
|
adapter = new MapMarkersListAdapter(mapActivity);
|
||||||
adapter.setHasStableIds(true);
|
adapter.setHasStableIds(true);
|
||||||
adapter.calculateStartAndFinishPos();
|
|
||||||
adapter.setSnappedToRoadPoints(planRouteContext.getSnappedToRoadPoints());
|
adapter.setSnappedToRoadPoints(planRouteContext.getSnappedToRoadPoints());
|
||||||
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter));
|
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter));
|
||||||
touchHelper.attachToRecyclerView(markersRv);
|
touchHelper.attachToRecyclerView(markersRv);
|
||||||
|
@ -322,12 +325,59 @@ public class PlanRouteFragment extends Fragment {
|
||||||
return view;
|
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
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
exitPlanRouteMode();
|
exitPlanRouteMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateLocation(Location loc) {
|
||||||
|
MapActivity mapActivity = getMapActivity();
|
||||||
|
if (mapActivity != null) {
|
||||||
|
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()
|
||||||
|
&& this.location.getLongitude() != location.getLongitude();
|
||||||
|
boolean farEnough = locationChanged && MapUtils.getDistance(this.location.getLatitude(), this.location.getLongitude(),
|
||||||
|
location.getLatitude(), location.getLongitude()) >= MIN_DISTANCE_FOR_RECALCULATE;
|
||||||
|
if (newLocation || farEnough) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private MapActivity getMapActivity() {
|
private MapActivity getMapActivity() {
|
||||||
return (MapActivity) getActivity();
|
return (MapActivity) getActivity();
|
||||||
}
|
}
|
||||||
|
@ -456,7 +506,6 @@ public class PlanRouteFragment extends Fragment {
|
||||||
if (mapActivity != null) {
|
if (mapActivity != null) {
|
||||||
markersHelper.reverseActiveMarkersOrder();
|
markersHelper.reverseActiveMarkersOrder();
|
||||||
adapter.reloadData();
|
adapter.reloadData();
|
||||||
adapter.calculateStartAndFinishPos();
|
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
planRouteContext.recreateSnapTrkSegment();
|
planRouteContext.recreateSnapTrkSegment();
|
||||||
}
|
}
|
||||||
|
@ -816,7 +865,6 @@ public class PlanRouteFragment extends Fragment {
|
||||||
|
|
||||||
mapActivity.getMyApplication().getMapMarkersHelper().addSelectedMarkersToTop(res);
|
mapActivity.getMyApplication().getMapMarkersHelper().addSelectedMarkersToTop(res);
|
||||||
adapter.reloadData();
|
adapter.reloadData();
|
||||||
adapter.calculateStartAndFinishPos();
|
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
planRouteContext.recreateSnapTrkSegment();
|
planRouteContext.recreateSnapTrkSegment();
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,6 +268,7 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
|
||||||
items.add(myLoc);
|
items.add(myLoc);
|
||||||
}
|
}
|
||||||
items.addAll(mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkers());
|
items.addAll(mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkers());
|
||||||
|
calculateStartAndFinishPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void lookupLocationAddress(OsmandApplication app) {
|
private void lookupLocationAddress(OsmandApplication app) {
|
||||||
|
|
Loading…
Reference in a new issue