Merge pull request #827 from Bars107/refactor

Refactor. Added waypoints helper.
This commit is contained in:
vshcherb 2014-08-08 16:33:21 +02:00
commit 386a43cfcc
8 changed files with 175 additions and 143 deletions

View file

@ -103,124 +103,8 @@ public class OsmAndLocationProvider implements SensorEventListener {
private OsmandPreference<Boolean> USE_MAGNETIC_FIELD_SENSOR_COMPASS;
private OsmandPreference<Boolean> USE_FILTER_FOR_COMPASS;
private static final int NOT_ANNOUNCED = 0;
private static final int ANNOUNCED_ONCE = 1;
private ConcurrentHashMap<LocationPoint , Integer> locationPointsStates = new ConcurrentHashMap<LocationPoint, Integer>();
private List<LocationPoint> visibleLocationPoints = new CopyOnWriteArrayList<LocationPoint>();
private long locationPointsModified;
public List<LocationPoint> getVisibleLocationPoints() {
return visibleLocationPoints;
}
public void setVisibleLocationPoints(List<LocationPoint> points) {
locationPointsStates.clear();
visibleLocationPoints.clear();
if (points == null) {
return;
}
for (LocationPoint p : points) {
locationPointsStates.put(p, NOT_ANNOUNCED);
visibleLocationPoints.add(p);
}
sortVisibleLocationPoints();
}
public void addVisibleLocationPoint(LocationPoint lp) {
this.locationPointsStates.put(lp, NOT_ANNOUNCED);
this.locationPointsModified = System.currentTimeMillis();
sortVisibleLocationPoints();
}
public void clearAllVisiblePoints() {
this.locationPointsStates.clear();
this.visibleLocationPoints.clear();
this.locationPointsModified = System.currentTimeMillis();
}
public void sortVisibleLocationPoints() {
net.osmand.Location lastLocation = getLastKnownLocation();
if (lastLocation != null) {
Object[] loc = visibleLocationPoints.toArray();
Arrays.sort(loc, getComparator(lastLocation));
visibleLocationPoints.clear();
for (Object aLoc : loc) {
visibleLocationPoints.add((LocationPoint) aLoc);
}
locationPointsModified = System.currentTimeMillis();
}
}
private Comparator<Object> getComparator(final net.osmand.Location lastLocation){
return new Comparator<Object>() {
@Override
public int compare(Object locationPoint, Object locationPoint2) {
double d1 = MapUtils.getDistance(lastLocation.getLatitude(), lastLocation.getLongitude(),
((LocationPoint)locationPoint).getLatitude(), ((LocationPoint)locationPoint).getLongitude());
double d2 = MapUtils.getDistance(lastLocation.getLatitude(), lastLocation.getLongitude(),
((LocationPoint)locationPoint2).getLatitude(), ((LocationPoint)locationPoint2).getLongitude());
return Double.compare(d1, d2);
}
};
}
public long getLocationPointsModified() {
return locationPointsModified;
}
public void removeVisibleLocationPoint(LocationPoint lp) {
this.visibleLocationPoints = removeFromList(visibleLocationPoints, lp);
this.locationPointsStates.remove(lp);
this.locationPointsModified = System.currentTimeMillis();
}
private void announceVisibleLocations() {
final net.osmand.Location lastLocation = getLastKnownLocation();
if (lastLocation != null && app.getRoutingHelper().isFollowingMode()) {
String nameToAnnounce = null;
List<LocationPoint> approachPoints = new ArrayList<LocationPoint>();
List<LocationPoint> announcePoints = new ArrayList<LocationPoint>();
for (LocationPoint point : locationPointsStates.keySet()) {
double d1 = MapUtils.getDistance(lastLocation.getLatitude(), lastLocation.getLongitude(),
point.getLatitude(), point.getLongitude());
int state = locationPointsStates.get(point);
if (state <= ANNOUNCED_ONCE && app.getRoutingHelper().getVoiceRouter().isDistanceLess(lastLocation.getSpeed(), d1, 150)) {
nameToAnnounce = (nameToAnnounce == null ? "" : ", ") + point.getName();
locationPointsStates.remove(point);
this.locationPointsModified = System.currentTimeMillis();
app.getMapActivity().getMapLayers().getMapControlsLayer().getWaypointDialogHelper().updateDialog();
announcePoints.add(point);
} else if (state == NOT_ANNOUNCED && app.getRoutingHelper().getVoiceRouter().isDistanceLess(lastLocation.getSpeed(), d1, 500)) {
locationPointsStates.put(point, state + 1);
this.locationPointsModified = System.currentTimeMillis();
app.getMapActivity().getMapLayers().getMapControlsLayer().getWaypointDialogHelper().updateDialog();
approachPoints.add(point);
}
}
if (!announcePoints.isEmpty()) {
app.getRoutingHelper().getVoiceRouter().announceWaypoint(announcePoints);
}
if (!approachPoints.isEmpty()) {
app.getRoutingHelper().getVoiceRouter().approachWaypoint(lastLocation, approachPoints);
}
}
}
public List<LocationPoint> removeFromList(List<LocationPoint> items, Object item){
List<LocationPoint> newArray = new ArrayList<LocationPoint>();
Object[] oldArray = items.toArray();
for (int i=0; i<oldArray.length; i++){
if (!item.equals(oldArray[i])){
newArray.add((LocationPoint)oldArray[i]);
}
}
items.clear();
return new CopyOnWriteArrayList<LocationPoint>(newArray);
}
public class SimulationProvider {
private int currentRoad;
@ -635,9 +519,8 @@ public class OsmAndLocationProvider implements SensorEventListener {
private void updateLocation(net.osmand.Location loc ) {
if (app.getSettings().ANNOUNCE_NEARBY_FAVORITES.get() && app.getRoutingHelper().isFollowingMode()){
sortVisibleLocationPoints();
app.getMapActivity().getMapLayers().getMapControlsLayer().getWaypointDialogHelper().updateDialog();
announceVisibleLocations();
app.getWaypointHelper().locationChanged(getLastKnownLocation());
}
for(OsmAndLocationListener l : locationListeners){
l.updateLocation(loc);

View file

@ -24,6 +24,7 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.SettingsActivity;
import net.osmand.plus.api.SQLiteAPI;
import net.osmand.plus.api.SQLiteAPIImpl;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.render.NativeOsmandLibrary;
import net.osmand.plus.render.RendererRegistry;
@ -102,6 +103,7 @@ public class OsmandApplication extends Application {
private LiveMonitoringHelper liveMonitoringHelper;
private TargetPointsHelper targetPointsHelper;
private RoutingConfiguration.Builder defaultRoutingConfig;
private WaypointHelper waypointHelper;
private boolean applicationInitializing = false;
private Locale preferredLocale = null;
@ -152,6 +154,7 @@ public class OsmandApplication extends Application {
liveMonitoringHelper = new LiveMonitoringHelper(this);
selectedGpxHelper = new GpxSelectionHelper(this);
favorites = new FavouritesDbHelper(this);
waypointHelper = new WaypointHelper(this);
uiHandler = new Handler();
rendererRegistry = new RendererRegistry();
targetPointsHelper = new TargetPointsHelper(this);
@ -224,6 +227,10 @@ public class OsmandApplication extends Application {
return liveMonitoringHelper;
}
public WaypointHelper getWaypointHelper() {
return waypointHelper;
}
public PoiFiltersHelper getPoiFilters() {
if (poiFilters == null) {
poiFilters = new PoiFiltersHelper(this);

View file

@ -1,4 +1,4 @@
package net.osmand.plus.sherpafy;
package net.osmand.plus.helpers;
import android.app.AlertDialog;
import android.content.Context;
@ -33,7 +33,7 @@ public class WaypointDialogHelper {
private MapActivity mapActivity;
private OsmandApplication app;
private FrameLayout mainLayout;
private OsmAndLocationProvider locationProvider;
private WaypointHelper waypointHelper;
public static boolean OVERLAP_LAYOUT = true;
private long uiModified;
@ -41,14 +41,14 @@ public class WaypointDialogHelper {
public WaypointDialogHelper(MapActivity mapActivity) {
this.app = mapActivity.getMyApplication();
locationProvider = this.app.getLocationProvider();
waypointHelper = this.app.getWaypointHelper();
this.mapActivity = mapActivity;
this.mainLayout = (FrameLayout) ((FrameLayout) mapActivity.getLayout()).getChildAt(0);
}
public void updateDialog() {
List<LocationPoint> vlp = locationProvider.getVisibleLocationPoints();
long locationPointsModified = locationProvider.getLocationPointsModified();
List<LocationPoint> vlp = waypointHelper.getVisibleLocationPoints();
long locationPointsModified = waypointHelper.getLocationPointsModified();
if (locationPointsModified != uiModified) {
uiModified = locationPointsModified;
if (vlp.isEmpty()) {
@ -78,7 +78,7 @@ public class WaypointDialogHelper {
btnN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationProvider.removeVisibleLocationPoint(point);
waypointHelper.removeVisibleLocationPoint(point);
updateDialog();
}
});
@ -186,7 +186,7 @@ public class WaypointDialogHelper {
}
public void showAllDialog(){
final List<LocationPoint> visibleLocationPoints = locationProvider.getVisibleLocationPoints();
final List<LocationPoint> visibleLocationPoints = waypointHelper.getVisibleLocationPoints();
final ArrayAdapter<LocationPoint> listAdapter = new ArrayAdapter<LocationPoint>(mapActivity, R.layout.waypoint_reached, R.id.title,
visibleLocationPoints) {
@Override
@ -217,9 +217,9 @@ public class WaypointDialogHelper {
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LocationPoint point = locationProvider.getVisibleLocationPoints().get(position);
LocationPoint point = waypointHelper.getVisibleLocationPoints().get(position);
remove(point);
locationProvider.removeVisibleLocationPoint(point);
waypointHelper.removeVisibleLocationPoint(point);
notifyDataSetChanged();
}
});
@ -246,7 +246,7 @@ public class WaypointDialogHelper {
builder.setNegativeButton(mapActivity.getString(R.string.hide_all_waypoints), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
locationProvider.clearAllVisiblePoints();
waypointHelper.clearAllVisiblePoints();
updateDialog();
}
});

View file

@ -0,0 +1,153 @@
package net.osmand.plus.helpers;
import net.osmand.Location;
import net.osmand.data.LocationPoint;
import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmandApplication;
import net.osmand.util.MapUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Created by Denis on 08.08.2014.
*/
public class WaypointHelper {
OsmandApplication app;
private List<LocationPoint> visibleLocationPoints = new CopyOnWriteArrayList<LocationPoint>();
private ConcurrentHashMap<LocationPoint, Integer> locationPointsStates = new ConcurrentHashMap<LocationPoint, Integer>();
private long locationPointsModified;
private Location lastKnownLocation;
private static final int NOT_ANNOUNCED = 0;
private static final int ANNOUNCED_ONCE = 1;
public WaypointHelper(OsmandApplication application) {
app = application;
}
public void updateWaypoints() {
}
public List<LocationPoint> getVisibleLocationPoints() {
return visibleLocationPoints;
}
public void locationChanged(Location location) {
lastKnownLocation = location;
sortVisibleWaypoints();
announceVisibleLocations();
}
private void sortVisibleWaypoints() {
if (lastKnownLocation != null) {
Object[] loc = visibleLocationPoints.toArray();
Arrays.sort(loc, getComparator(lastKnownLocation));
visibleLocationPoints.clear();
for (Object aLoc : loc) {
visibleLocationPoints.add((LocationPoint) aLoc);
}
locationPointsModified = System.currentTimeMillis();
}
}
public long getLocationPointsModified() {
return locationPointsModified;
}
public void removeVisibleLocationPoint(LocationPoint lp) {
this.visibleLocationPoints = removeFromList(visibleLocationPoints, lp);
this.locationPointsStates.remove(lp);
this.locationPointsModified = System.currentTimeMillis();
}
public void announceVisibleLocations() {
if (lastKnownLocation != null && app.getRoutingHelper().isFollowingMode()) {
String nameToAnnounce = null;
List<LocationPoint> approachPoints = new ArrayList<LocationPoint>();
List<LocationPoint> announcePoints = new ArrayList<LocationPoint>();
for (LocationPoint point : locationPointsStates.keySet()) {
double d1 = MapUtils.getDistance(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude(),
point.getLatitude(), point.getLongitude());
int state = locationPointsStates.get(point);
if (state <= ANNOUNCED_ONCE && app.getRoutingHelper().getVoiceRouter().isDistanceLess(lastKnownLocation.getSpeed(), d1, 150)) {
nameToAnnounce = (nameToAnnounce == null ? "" : ", ") + point.getName();
locationPointsStates.remove(point);
this.locationPointsModified = System.currentTimeMillis();
app.getMapActivity().getMapLayers().getMapControlsLayer().getWaypointDialogHelper().updateDialog();
announcePoints.add(point);
} else if (state == NOT_ANNOUNCED && app.getRoutingHelper().getVoiceRouter().isDistanceLess(lastKnownLocation.getSpeed(), d1, 500)) {
locationPointsStates.put(point, state + 1);
this.locationPointsModified = System.currentTimeMillis();
app.getMapActivity().getMapLayers().getMapControlsLayer().getWaypointDialogHelper().updateDialog();
approachPoints.add(point);
}
}
if (!announcePoints.isEmpty()) {
app.getRoutingHelper().getVoiceRouter().announceWaypoint(announcePoints);
}
if (!approachPoints.isEmpty()) {
app.getRoutingHelper().getVoiceRouter().approachWaypoint(lastKnownLocation, approachPoints);
}
}
}
public void addVisibleLocationPoint(LocationPoint lp) {
this.locationPointsStates.put(lp, NOT_ANNOUNCED);
this.locationPointsModified = System.currentTimeMillis();
sortVisibleWaypoints();
}
public void clearAllVisiblePoints() {
this.locationPointsStates.clear();
this.visibleLocationPoints.clear();
this.locationPointsModified = System.currentTimeMillis();
}
public void setVisibleLocationPoints(List<LocationPoint> points) {
locationPointsStates.clear();
visibleLocationPoints.clear();
if (points == null) {
return;
}
for (LocationPoint p : points) {
locationPointsStates.put(p, NOT_ANNOUNCED);
visibleLocationPoints.add(p);
}
sortVisibleWaypoints();
}
public List<LocationPoint> removeFromList(List<LocationPoint> items, Object item) {
List<LocationPoint> newArray = new ArrayList<LocationPoint>();
Object[] oldArray = items.toArray();
for (int i = 0; i < oldArray.length; i++) {
if (!item.equals(oldArray[i])) {
newArray.add((LocationPoint) oldArray[i]);
}
}
items.clear();
return new CopyOnWriteArrayList<LocationPoint>(newArray);
}
private Comparator<Object> getComparator(final net.osmand.Location lastLocation) {
return new Comparator<Object>() {
@Override
public int compare(Object locationPoint, Object locationPoint2) {
double d1 = MapUtils.getDistance(lastLocation.getLatitude(), lastLocation.getLongitude(),
((LocationPoint) locationPoint).getLatitude(), ((LocationPoint) locationPoint).getLongitude());
double d2 = MapUtils.getDistance(lastLocation.getLatitude(), lastLocation.getLongitude(),
((LocationPoint) locationPoint2).getLatitude(), ((LocationPoint) locationPoint2).getLongitude());
return Double.compare(d1, d2);
}
};
}
}

View file

@ -16,7 +16,6 @@ import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.Route;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.sherpafy.WaypointDialogHelper;
import net.osmand.plus.views.MapInfoLayer;
import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.OsmandMapTileView;

View file

@ -11,21 +11,14 @@ import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon;
import net.osmand.data.LocationPoint;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.*;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.SearchOnTheRouteHelper;
import net.osmand.plus.OsmandSettings.MetricsConstants;
import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.routing.AlarmInfo.AlarmInfoType;
import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo;
import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder;
import net.osmand.plus.routing.RouteProvider.RouteService;
import net.osmand.plus.sherpafy.SherpafyCustomization;
import net.osmand.plus.voice.CommandPlayer;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.router.RouteSegmentResult;
@ -537,7 +530,7 @@ public class RoutingHelper {
locationPoints.addAll(app.getFavorites().getFavouritePoints());
}
locationPoints.addAll(res.getLocationPoints());
app.getLocationProvider().setVisibleLocationPoints(locationPoints);
app.getWaypointHelper().setVisibleLocationPoints(locationPoints);
final boolean newRoute = !this.route.isCalculated();
route = res;
if (isFollowingMode) {

View file

@ -3,14 +3,13 @@ package net.osmand.plus.views;
import java.util.ArrayList;
import java.util.List;
import android.view.ViewParent;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.sherpafy.WaypointDialogHelper;
import net.osmand.plus.helpers.WaypointDialogHelper;
import net.osmand.plus.views.controls.MapRoutePlanControl;
import net.osmand.plus.views.controls.MapRoutePreferencesControl;
import net.osmand.plus.views.controls.MapCancelControl;

View file

@ -1,9 +1,7 @@
package net.osmand.plus.views.controls;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.sherpafy.WaypointDialogHelper;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import android.content.Context;
import android.graphics.Canvas;