From 78d624b008cd588f37254911060a532b26c93fd8 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 31 May 2012 22:24:58 +0200 Subject: [PATCH] Fix issue 900. Update latest GPX track while display --- OsmAnd/src/net/osmand/GPXUtilities.java | 12 +++++ .../net/osmand/plus/NavigationService.java | 4 +- .../net/osmand/plus/OsmandApplication.java | 22 ++++++-- .../osmand/plus/activities/MapActivity.java | 9 +--- .../plus/activities/MapActivityLayers.java | 12 ----- .../plus/activities/SavingTrackHelper.java | 32 +++++++++--- .../monitoring/OsmandMonitoringPlugin.java | 7 ++- .../src/net/osmand/plus/views/GPXLayer.java | 52 ++++--------------- .../src/net/osmand/plus/views/RouteLayer.java | 13 +++-- 9 files changed, 78 insertions(+), 85 deletions(-) diff --git a/OsmAnd/src/net/osmand/GPXUtilities.java b/OsmAnd/src/net/osmand/GPXUtilities.java index f854b266e6..ac775211e6 100644 --- a/OsmAnd/src/net/osmand/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/GPXUtilities.java @@ -246,10 +246,22 @@ public class GPXUtilities { public String warning = null; public String path = ""; + public List> processedPointsToDisplay = new ArrayList>(); + public boolean isCloudmadeRouteFile(){ return "cloudmade".equalsIgnoreCase(author); } + public void proccessPoints(){ + List> tpoints = new ArrayList>(); + for (Track t : tracks) { + for (TrkSegment ts : t.segments) { + tpoints.add(ts.points); + } + } + processedPointsToDisplay = tpoints; + } + public WptPt findPointToShow(){ for(Track t : tracks){ for(TrkSegment s : t.segments){ diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index cda56a2b5a..684350ac85 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -95,8 +95,8 @@ public class NavigationService extends Service implements LocationListener { serviceOffInterval = settings.SERVICE_OFF_INTERVAL.get(); serviceOffProvider = settings.SERVICE_OFF_PROVIDER.get(); serviceError = settings.SERVICE_OFF_WAIT_INTERVAL.get(); - savingTrackHelper = new SavingTrackHelper(this); - liveMonitoringHelper = new LiveMonitoringHelper(this); + savingTrackHelper = ((OsmandApplication) getApplication()).getSavingTrackHelper(); + liveMonitoringHelper = ((OsmandApplication) getApplication()).getLiveMonitoringHelper(); routingHelper = ((OsmandApplication)getApplication()).getRoutingHelper(); ((OsmandApplication)getApplication()).setNavigationService(this); diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 0e0d359336..644384fa9b 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -24,6 +24,7 @@ import net.osmand.LogUtil; import net.osmand.access.AccessibilityMode; import net.osmand.access.AccessibleToast; import net.osmand.plus.activities.DayNightHelper; +import net.osmand.plus.activities.LiveMonitoringHelper; import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.SettingsActivity; import net.osmand.plus.render.NativeOsmandLibrary; @@ -71,9 +72,12 @@ public class OsmandApplication extends Application { private List startingWarnings; private Handler uiHandler; private GPXFile gpxFileToDisplay; + private SavingTrackHelper savingTrackHelper; + private LiveMonitoringHelper liveMonitoringHelper; private boolean applicationInitializing = false; private Locale prefferedLocale = null; + @Override public void onCreate() { @@ -85,6 +89,8 @@ public class OsmandApplication extends Application { manager = new ResourceManager(this); daynightHelper = new DayNightHelper(this); bidforfix = new BidForFixHelper("osmand.net", getString(R.string.default_buttons_support), getString(R.string.default_buttons_cancel)); + savingTrackHelper = new SavingTrackHelper(this); + liveMonitoringHelper = new LiveMonitoringHelper(this); uiHandler = new Handler(); rendererRegistry = new RendererRegistry(); checkPrefferedLocale(); @@ -134,6 +140,14 @@ public class OsmandApplication extends Application { return osmandSettings; } + public SavingTrackHelper getSavingTrackHelper() { + return savingTrackHelper; + } + + public LiveMonitoringHelper getLiveMonitoringHelper() { + return liveMonitoringHelper; + } + public PoiFiltersHelper getPoiFilters() { if (poiFilters == null) { @@ -159,6 +173,7 @@ public class OsmandApplication extends Application { pt.setName(p.name); pts.add(pt); } + gpxFileToDisplay.proccessPoints(); getFavorites().setFavoritePointsFromGPXFile(pts); } } @@ -387,12 +402,11 @@ public class OsmandApplication extends Application { } warnings.addAll(manager.reloadIndexes(startDialog)); player = null; - SavingTrackHelper helper = new SavingTrackHelper(OsmandApplication.this); - if (helper.hasDataToSave()) { + if (savingTrackHelper.hasDataToSave()) { startDialog.startTask(getString(R.string.saving_gpx_tracks), -1); - warnings.addAll(helper.saveDataToGpx()); + warnings.addAll(savingTrackHelper.saveDataToGpx()); } - helper.close(); + savingTrackHelper.close(); // restore backuped favorites to normal file final File appDir = getSettings().extendOsmandPath(ResourceManager.APP_DIR); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 0be570eafd..715134bc2d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -198,8 +198,8 @@ public class MapActivity extends AccessibleActivity implements IMapLocationListe }); - savingTrackHelper = new SavingTrackHelper(this); - liveMonitoringHelper = new LiveMonitoringHelper(this); + savingTrackHelper = getMyApplication().getSavingTrackHelper(); + liveMonitoringHelper = getMyApplication().getLiveMonitoringHelper(); LatLon pointToNavigate = settings.getPointToNavigate(); routingHelper = getMyApplication().getRoutingHelper(); @@ -750,11 +750,6 @@ public class MapActivity extends AccessibleActivity implements IMapLocationListe if (settings.SAVE_TRACK_TO_GPX.get()) { savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(), location.getAccuracy(), locationTime, settings); - if (settings.SHOW_CURRENT_GPX_TRACK.get()) { - WptPt pt = new GPXUtilities.WptPt(location.getLatitude(), location.getLongitude(), locationTime, - location.getAltitude(), location.getSpeed(), location.getAccuracy()); - mapLayers.getGpxLayer().addTrackPoint(pt); - } } } if(settings.LIVE_MONITORING.get()){ diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 7cb2d4ce9e..78a0492b5b 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -186,7 +186,6 @@ public class MapActivityLayers { } } OsmandPlugin.refreshLayers(mapView, activity); - updateGPXLayer(); } public void updateMapSource(OsmandMapTileView mapView, CommonPreference settingsToWarnAboutMap){ @@ -262,7 +261,6 @@ public class MapActivityLayers { } else if(itemId == R.string.layer_gpx_layer){ if(getApplication().getGpxFileToDisplay() != null){ getApplication().setGpxFileToDisplay(null, false); - gpxLayer.clearCurrentGPX(); } else { dialog.dismiss(); showGPXFileLayer(mapView); @@ -365,22 +363,12 @@ public class MapActivityLayers { mapView.getAnimatedDraggingThread().startMoving(loc.lat, loc.lon, mapView.getZoom(), true); } - updateGPXLayer(); mapView.refreshMap(); return true; } }, true, true); } - private void updateGPXLayer(){ - GPXFile gpxFileToDisplay = getApplication().getGpxFileToDisplay(); - if(gpxFileToDisplay == null){ - gpxLayer.setTracks(null); - } else { - gpxLayer.setTracks(gpxFileToDisplay.tracks); - } - } - public void selectGPXFileLayer(final CallbackWithObject callbackWithObject, final boolean convertCloudmade, final boolean showCurrentGpx) { final List list = new ArrayList(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index d11859e409..1a679631c8 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -20,7 +20,6 @@ import net.osmand.plus.ResourceManager; import org.apache.commons.logging.Log; -import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; @@ -46,17 +45,14 @@ public class SavingTrackHelper extends SQLiteOpenHelper { public final static String POINT_COL_DESCRIPTION = "description"; //$NON-NLS-1$ public final static Log log = LogUtil.getLog(SavingTrackHelper.class); - - - private String updateScript; private String updatePointsScript; private long lastTimeUpdated = 0; - private final Context ctx; + private final OsmandApplication ctx; - public SavingTrackHelper(Context ctx){ + public SavingTrackHelper(OsmandApplication ctx){ super(ctx, DATABASE_NAME, null, DATABASE_VERSION); this.ctx = ctx; updateScript = "INSERT INTO " + TRACK_NAME + @@ -257,6 +253,30 @@ public class SavingTrackHelper extends SQLiteOpenHelper { if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() * 1000) { execWithClose(updateScript, new Object[] { lat, lon, alt, speed, hdop, time }); lastTimeUpdated = time; + if (settings.SHOW_CURRENT_GPX_TRACK.get()) { + WptPt pt = new GPXUtilities.WptPt(lat, lon, time, + alt, speed, hdop); + addTrackPoint(pt); + } + } + } + + private void addTrackPoint(WptPt pt) { + GPXFile file = ctx.getGpxFileToDisplay(); + if (file != null && ctx.getSettings().SHOW_CURRENT_GPX_TRACK.get()) { + List> points = file.processedPointsToDisplay; + if (points.size() == 0) { + points.add(new ArrayList()); + } + List last = points.get(points.size() - 1); + if (last.size() == 0 || last.get(last.size() - 1).time - pt.time < 6 * 60 * 1000) { + // 6 minutes same segment + last.add(pt); + } else { + ArrayList l = new ArrayList(); + l.add(pt); + points.add(l); + } } } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java index ee998fe237..540eeebc50 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java @@ -102,9 +102,9 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { @Override public boolean onPreferenceClick(Preference preference) { - SavingTrackHelper helper = new SavingTrackHelper(activity); + SavingTrackHelper helper = app.getSavingTrackHelper(); if (helper.hasDataToSave()) { - saveCurrentTracks(activity); + saveCurrentTracks(helper, activity); } else { helper.close(); } @@ -127,14 +127,13 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } - private void saveCurrentTracks(final SettingsActivity activity) { + private void saveCurrentTracks(final SavingTrackHelper helper, final SettingsActivity activity) { activity.progressDlg = ProgressDialog.show(activity, activity.getString(R.string.saving_gpx_tracks), activity.getString(R.string.saving_gpx_tracks), true); final ProgressDialogImplementation impl = new ProgressDialogImplementation(activity.progressDlg); impl.setRunnable("SavingGPX", new Runnable() { //$NON-NLS-1$ @Override public void run() { try { - SavingTrackHelper helper = new SavingTrackHelper(activity); helper.saveDataToGpx(); helper.close(); } finally { diff --git a/OsmAnd/src/net/osmand/plus/views/GPXLayer.java b/OsmAnd/src/net/osmand/plus/views/GPXLayer.java index 09ac7f6527..b3c62b28d0 100644 --- a/OsmAnd/src/net/osmand/plus/views/GPXLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/GPXLayer.java @@ -1,12 +1,9 @@ package net.osmand.plus.views; -import java.util.ArrayList; import java.util.List; -import net.osmand.GPXUtilities.Track; -import net.osmand.GPXUtilities.TrkSegment; +import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import android.graphics.Canvas; @@ -22,16 +19,17 @@ public class GPXLayer extends OsmandMapLayer { private OsmandMapTileView view; - private List> points = new ArrayList>(); private Paint paint; private Path path; private OsmandSettings settings; + private boolean fluorescent; private void initUI() { paint = new Paint(); + fluorescent = view.getSettings().FLUORESCENT_OVERLAYS.get(); if (view.getSettings().FLUORESCENT_OVERLAYS.get()) { paint.setColor(view.getResources().getColor(R.color.gpx_track_fluorescent)); } else { @@ -57,11 +55,14 @@ public class GPXLayer extends OsmandMapLayer { @Override public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, DrawSettings nightMode) { - initUI(); //to change color immediately when needed - List> points = this.points; - if(points.isEmpty()){ + GPXFile gpxFile = view.getApplication().getGpxFileToDisplay(); + if(gpxFile == null){ return; } + List> points = gpxFile.processedPointsToDisplay; + if(view.getSettings().FLUORESCENT_OVERLAYS.get() != fluorescent) { + initUI(); //to change color immediately when needed + } for (List l : points) { path.rewind(); @@ -111,41 +112,6 @@ public class GPXLayer extends OsmandMapLayer { } - public void clearCurrentGPX(){ - points.clear(); - } - - public void setTracks(List tracks){ - if(tracks == null){ - clearCurrentGPX(); - } else { - List> tpoints = new ArrayList>(); - for (Track t : tracks) { - for (TrkSegment ts : t.segments) { - tpoints.add(ts.points); - } - } - points = tpoints; - - } - } - - public void addTrackPoint(WptPt pt){ - if(points.size() == 0){ - points.add(new ArrayList()); - } - List last = points.get(points.size() - 1); - if(last.size() == 0 || last.get(last.size() - 1).time - pt.time < 6 * 60 * 1000) { - // 6 minutes same segment - last.add(pt); - } else { - ArrayList l = new ArrayList(); - l.add(pt); - points.add(l); - } - } - - @Override public void destroyLayer() { diff --git a/OsmAnd/src/net/osmand/plus/views/RouteLayer.java b/OsmAnd/src/net/osmand/plus/views/RouteLayer.java index f2de5a8b53..d9b5c45017 100644 --- a/OsmAnd/src/net/osmand/plus/views/RouteLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/RouteLayer.java @@ -3,7 +3,6 @@ package net.osmand.plus.views; import java.util.ArrayList; import java.util.List; -import net.osmand.LogUtil; import net.osmand.osm.MapUtils; import net.osmand.plus.R; import net.osmand.plus.routing.RoutingHelper; @@ -17,7 +16,6 @@ import android.graphics.PointF; import android.graphics.Rect; import android.graphics.RectF; import android.location.Location; -import android.util.Log; public class RouteLayer extends OsmandMapLayer { @@ -30,6 +28,8 @@ public class RouteLayer extends OsmandMapLayer { private Paint paint; private Path path; + + private Boolean fluorescent; public RouteLayer(RoutingHelper helper){ this.helper = helper; @@ -40,6 +40,7 @@ public class RouteLayer extends OsmandMapLayer { boundsRect = new Rect(0, 0, view.getWidth(), view.getHeight()); tileRect = new RectF(); paint = new Paint(); + fluorescent = view.getSettings().FLUORESCENT_OVERLAYS.get(); if (view.getSettings().FLUORESCENT_OVERLAYS.get()) { paint.setColor(view.getResources().getColor(R.color.nav_track_fluorescent)); } else { @@ -63,10 +64,11 @@ public class RouteLayer extends OsmandMapLayer { @Override public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, DrawSettings nightMode) { - initUI(); //to change color immediately when needed path.reset(); if (helper.hasPointsToShow()) { - long time = System.currentTimeMillis(); + if(view.getSettings().FLUORESCENT_OVERLAYS.get() != fluorescent) { + initUI(); //to change color immediately when needed + } int w = view.getWidth(); int h = view.getHeight(); if(helper.getCurrentLocation() != null && @@ -84,9 +86,6 @@ public class RouteLayer extends OsmandMapLayer { double lat = topLatitude - bottomLatitude + 0.1; double lon = rightLongitude - leftLongitude + 0.1; helper.fillLocationsToShow(topLatitude + lat, leftLongitude - lon, bottomLatitude - lat, rightLongitude + lon, points); - if((System.currentTimeMillis() - time) > 80){ - Log.e(LogUtil.TAG, "Calculate route layer " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ - } if (points.size() > 0) { int px = view.getMapXForPoint(points.get(0).getLongitude());