From fd1b3c0abfe290dfc8cfb998dc21a60564c2efaa Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 25 Jan 2012 23:09:13 +0100 Subject: [PATCH] Add support for showing current track --- OsmAnd/res/layout-land/menu.xml | 2 +- OsmAnd/res/layout-large-land/menu.xml | 2 +- OsmAnd/res/layout-large/menu.xml | 2 +- OsmAnd/res/layout/menu.xml | 2 +- OsmAnd/res/values/strings.xml | 2 + OsmAnd/src/net/osmand/GPXUtilities.java | 12 +++ .../net/osmand/plus/FavouritesDbHelper.java | 9 ++ .../src/net/osmand/plus/OsmandSettings.java | 4 + .../plus/activities/LocalIndexesActivity.java | 2 +- .../osmand/plus/activities/MapActivity.java | 8 ++ .../plus/activities/MapActivityActions.java | 5 +- .../plus/activities/MapActivityLayers.java | 86 ++++++++++++------- .../plus/activities/OsmandApplication.java | 3 +- .../plus/activities/SavingTrackHelper.java | 21 +++-- .../src/net/osmand/plus/views/GPXLayer.java | 24 +++++- 15 files changed, 137 insertions(+), 47 deletions(-) diff --git a/OsmAnd/res/layout-land/menu.xml b/OsmAnd/res/layout-land/menu.xml index 552b0aa401..43d9f03e5b 100644 --- a/OsmAnd/res/layout-land/menu.xml +++ b/OsmAnd/res/layout-land/menu.xml @@ -12,7 +12,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:b android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:text="@string/app_name" android:textColor="#000000" android:typeface="serif" android:textSize="20sp"/> + Please enable GPX monitoring in the settings + Show current track Changes in 0.7.0 : \n\t- Offline wikipedia data with articles \n\t- Updated maps diff --git a/OsmAnd/src/net/osmand/GPXUtilities.java b/OsmAnd/src/net/osmand/GPXUtilities.java index 87bb1be6f6..7f62f7aa5d 100644 --- a/OsmAnd/src/net/osmand/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/GPXUtilities.java @@ -68,6 +68,18 @@ public class GPXUtilities { public double ele = Double.NaN; public double speed = 0; public double hdop = Double.NaN; + + public WptPt(){} + + public WptPt(double lat, double lon, long time, double ele, double speed, double hdop) { + this.lat = lat; + this.lon = lon; + this.time = time; + this.ele = ele; + this.speed = speed; + this.hdop = hdop; + } + } diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 2733a784f1..2b9cf2f38a 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -115,6 +115,15 @@ public class FavouritesDbHelper extends SQLiteOpenHelper { recalculateCachedFavPoints(); } + public void addFavoritePointToGPXFile(FavouritePoint fp) { + fp.setCategory(GPX_GROUP); + fp.setStored(false); + if (!favoriteGroups.containsKey(GPX_GROUP)) { + favoriteGroups.put(GPX_GROUP, new ArrayList()); + } + favoriteGroups.get(GPX_GROUP).add(fp); + } + public List getFavouritePoints() { checkFavoritePoints(); return cachedFavoritePoints; diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index c108fed1f8..5513cca82f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -174,6 +174,7 @@ public class OsmandSettings { return internetConnectionAvailable; } + /////////////// PREFERENCES classes //////////////// public abstract class CommonPreference implements OsmandPreference { @@ -1046,6 +1047,9 @@ public class OsmandSettings { public final OsmandPreference SERVICE_OFF_INTERVAL = new IntPreference("service_off_interval", 5 * 60 * 1000, true); + public final CommonPreference SHOW_CURRENT_GPX_TRACK = + new BooleanPreference("show_current_gpx_track", false, true, true); + // this value string is synchronized with settings_pref.xml preference name public final OsmandPreference SERVICE_OFF_WAIT_INTERVAL = new IntPreference("service_off_wait_interval", 90 * 1000, true); diff --git a/OsmAnd/src/net/osmand/plus/activities/LocalIndexesActivity.java b/OsmAnd/src/net/osmand/plus/activities/LocalIndexesActivity.java index 066a7e728c..011b3745d5 100644 --- a/OsmAnd/src/net/osmand/plus/activities/LocalIndexesActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/LocalIndexesActivity.java @@ -163,7 +163,7 @@ public class LocalIndexesActivity extends ExpandableListActivity { OsmandSettings settings = OsmandSettings.getOsmandSettings(LocalIndexesActivity.this); settings.setMapLocationToShow(loc.lat, loc.lon, settings.getLastKnownMapZoom()); } - ((OsmandApplication) getApplication()).setGpxFileToDisplay(info.getGpxFile()); + ((OsmandApplication) getApplication()).setGpxFileToDisplay(info.getGpxFile(), false); MapActivity.launchMapActivityMoveToTop(LocalIndexesActivity.this); } } else if (resId == R.string.local_index_mi_rename) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 586510c179..5afe3bf12b 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -9,6 +9,7 @@ import com.google.android.apps.analytics.easytracking.TrackedActivity; import net.osmand.Algoritms; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.WptPt; import net.osmand.LogUtil; import net.osmand.Version; import net.osmand.data.MapTileDownloader.DownloadRequest; @@ -665,8 +666,15 @@ public class MapActivity extends TrackedActivity implements IMapLocationListener if (!location.hasAccuracy() || location.getAccuracy() < ACCURACY_FOR_GPX_AND_ROUTING) { savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(), location.getAccuracy(), location.getTime(), settings); + if(settings.SHOW_CURRENT_GPX_TRACK.get()) { + WptPt pt = new GPXUtilities.WptPt(location.getLatitude(), location.getLongitude(), location.getTime(), location.getAltitude(), location.getSpeed(), + location.getAccuracy()); + mapLayers.getGpxLayer().addTrackPoint(pt); + } } } + + registerUnregisterSensor(location); updateSpeedBearing(location); mapLayers.getLocationLayer().setLastKnownLocation(location); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 830c62a27f..f7d2114b77 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -209,6 +209,9 @@ public class MapActivityActions implements DialogProvider { double longitude = args.getDouble(KEY_LONGITUDE); String name = editText.getText().toString(); mapActivity.getSavingTrackHelper().insertPointData(latitude, longitude, System.currentTimeMillis(), name); + if(OsmandSettings.getOsmandSettings(mapActivity).SHOW_CURRENT_GPX_TRACK.get()) { + getMyApplication().favorites.addFavoritePointToGPXFile(new FavouritePoint(latitude, longitude, name, "")); + } Toast.makeText(mapActivity, MessageFormat.format(getString(R.string.add_waypoint_dialog_added), name), Toast.LENGTH_SHORT) .show(); dialog.dismiss(); @@ -622,7 +625,7 @@ public class MapActivityActions implements DialogProvider { builder.show(); return true; } - }, false); + }, false, false); } private ApplicationMode getAppMode(ToggleButton[] buttons, OsmandSettings settings){ diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 601f07b31a..992621b34f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -255,7 +255,7 @@ public class MapActivityLayers { selectedList.add(underlayLayer.getMap() != null ? 1 : 0); layers.add(R.string.layer_gpx_layer); - selectedList.add(gpxLayer.isVisible() ? 1 : 0); + selectedList.add(getApplication().getGpxFileToDisplay() != null ? 1 : 0); if(routeInfoLayer.couldBeVisible()){ layers.add(R.string.layer_route); selectedList.add(routeInfoLayer.isUserDefinedVisible() ? 1 : 0); @@ -317,8 +317,8 @@ public class MapActivityLayers { mapTileLayer, mapVectorLayer); } } else if(layers.get(item) == R.string.layer_gpx_layer){ - if(gpxLayer.isVisible()){ - getApplication().setGpxFileToDisplay(null); + if(getApplication().getGpxFileToDisplay() != null){ + getApplication().setGpxFileToDisplay(null, false); gpxLayer.clearCurrentGPX(); } else { dialog.dismiss(); @@ -345,15 +345,27 @@ public class MapActivityLayers { selectGPXFileLayer(new CallbackWithObject() { @Override public boolean processResult(GPXFile result) { - settings.SHOW_FAVORITES.set(true); - if (result != null) { - getApplication().setGpxFileToDisplay(result); - updateGPXLayer(); - mapView.refreshMap(); + GPXFile toShow = result; + if (toShow == null) { + if(!settings.SAVE_TRACK_TO_GPX.get()){ + Toast.makeText(activity, R.string.gpx_monitoring_disabled_warn, Toast.LENGTH_SHORT).show(); + return true; + } + Map data = activity.getSavingTrackHelper().collectRecordedData(); + if(data.isEmpty()){ + toShow = new GPXFile(); + } else { + toShow = data.values().iterator().next(); + } } + + settings.SHOW_FAVORITES.set(true); + getApplication().setGpxFileToDisplay(toShow, result == null); + updateGPXLayer(); + mapView.refreshMap(); return true; } - }, true); + }, true, true); } private void updateGPXLayer(){ @@ -365,7 +377,8 @@ public class MapActivityLayers { } } - public void selectGPXFileLayer(final CallbackWithObject callbackWithObject, final boolean convertCloudmade) { + public void selectGPXFileLayer(final CallbackWithObject callbackWithObject, final boolean convertCloudmade, + final boolean showCurrentGpx) { final List list = new ArrayList(); final OsmandSettings settings = getApplication().getSettings(); final File dir = settings.extendOsmandPath(ResourceManager.GPX_PATH); @@ -395,35 +408,42 @@ public class MapActivityLayers { if(list.isEmpty()){ Toast.makeText(activity, R.string.gpx_files_not_found, Toast.LENGTH_LONG).show(); - } else { + } + if(!list.isEmpty() || showCurrentGpx){ Builder builder = new AlertDialog.Builder(activity); + if(showCurrentGpx){ + list.add(0, getString(R.string.show_current_gpx_title)); + } builder.setItems(list.toArray(new String[list.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - final ProgressDialog dlg = ProgressDialog.show(activity, getString(R.string.loading), - getString(R.string.loading_data)); - final File f = new File(dir, list.get(which)); - new Thread(new Runnable() { - @Override - public void run() { - final GPXFile res = GPXUtilities.loadGPXFile(activity, f, convertCloudmade); - dlg.dismiss(); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if(res.warning != null){ - Toast.makeText(activity, res.warning, Toast.LENGTH_LONG).show(); - } else { - callbackWithObject.processResult(res); + if(showCurrentGpx && which == 0){ + callbackWithObject.processResult(null); + } else { + final ProgressDialog dlg = ProgressDialog.show(activity, getString(R.string.loading), + getString(R.string.loading_data)); + final File f = new File(dir, list.get(which)); + new Thread(new Runnable() { + @Override + public void run() { + final GPXFile res = GPXUtilities.loadGPXFile(activity, f, convertCloudmade); + dlg.dismiss(); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (res.warning != null) { + Toast.makeText(activity, res.warning, Toast.LENGTH_LONG).show(); + } else { + callbackWithObject.processResult(res); + } } - } - }); - } + }); + } - - }, "Loading gpx").start(); //$NON-NLS-1$ + }, "Loading gpx").start(); //$NON-NLS-1$ + } } }); @@ -636,6 +656,10 @@ public class MapActivityLayers { return navigationLayer; } + public GPXLayer getGpxLayer() { + return gpxLayer; + } + public ContextMenuLayer getContextMenuLayer() { return contextMenuLayer; } diff --git a/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java index 1ebb5edb0b..f572a3a4b3 100644 --- a/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java @@ -110,8 +110,9 @@ public class OsmandApplication extends Application { return poiFilters; } - public void setGpxFileToDisplay(GPXFile gpxFileToDisplay) { + public void setGpxFileToDisplay(GPXFile gpxFileToDisplay, boolean showCurrentGpxFile) { this.gpxFileToDisplay = gpxFileToDisplay; + osmandSettings.SHOW_CURRENT_GPX_TRACK.set(showCurrentGpxFile); if(gpxFileToDisplay == null){ getFavorites().setFavoritePointsFromGPXFile(null); } else { diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index 2c5d1ad126..91d718218e 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -123,17 +123,14 @@ public class SavingTrackHelper extends SQLiteOpenHelper { * @return warnings */ public List saveDataToGpx() { - SQLiteDatabase db = getReadableDatabase(); List warnings = new ArrayList(); File dir = OsmandSettings.getOsmandSettings(ctx).getExternalStorageDirectory(); - if (db != null && dir.canWrite()) { + if (dir.canWrite()) { dir = new File(dir, ResourceManager.GPX_PATH); dir.mkdirs(); if (dir.exists()) { - Map data = new LinkedHashMap(); - collectDBPoints(db, data); - collectDBTracks(db, data); + Map data = collectRecordedData(); // save file for (final String f : data.keySet()) { @@ -156,7 +153,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } } - db = getWritableDatabase(); + SQLiteDatabase db = getWritableDatabase(); if (db != null && warnings.isEmpty()) { // remove all from db db.execSQL("DELETE FROM " + TRACK_NAME + " WHERE " + TRACK_COL_DATE + " <= ?", new Object[] { System.currentTimeMillis() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ @@ -165,6 +162,16 @@ public class SavingTrackHelper extends SQLiteOpenHelper { return warnings; } + public Map collectRecordedData() { + Map data = new LinkedHashMap(); + SQLiteDatabase db = getReadableDatabase(); + if(db != null) { + collectDBPoints(db, data); + collectDBTracks(db, data); + } + return data; + } + private void collectDBPoints(SQLiteDatabase db, Map dataTracks) { Cursor query = db.rawQuery("SELECT " + POINT_COL_LAT + "," + POINT_COL_LON + "," + POINT_COL_DATE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + POINT_COL_DESCRIPTION + " FROM " + POINT_NAME+" ORDER BY " + TRACK_COL_DATE +" ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ @@ -243,7 +250,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, OsmandSettings settings){ - if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get()*1000) { + if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() * 1000) { execWithClose(updateScript, new Object[] { lat, lon, alt, speed, hdop, time }); lastTimeUpdated = time; } diff --git a/OsmAnd/src/net/osmand/plus/views/GPXLayer.java b/OsmAnd/src/net/osmand/plus/views/GPXLayer.java index f1246527b7..460beb7ca1 100644 --- a/OsmAnd/src/net/osmand/plus/views/GPXLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/GPXLayer.java @@ -6,6 +6,7 @@ import java.util.List; import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; +import net.osmand.plus.OsmandSettings; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -24,6 +25,9 @@ public class GPXLayer extends OsmandMapLayer { private Paint paint; private Path path; + + private OsmandSettings settings; + private void initUI() { paint = new Paint(); @@ -41,6 +45,7 @@ public class GPXLayer extends OsmandMapLayer { @Override public void initLayer(OsmandMapTileView view) { this.view = view; + settings = OsmandSettings.getOsmandSettings(view.getContext()); initUI(); } @@ -96,8 +101,8 @@ public class GPXLayer extends OsmandMapLayer { } - public boolean isVisible(){ - return !points.isEmpty(); + public boolean isShowingCurrentTrack(){ + return settings.SHOW_CURRENT_GPX_TRACK.get(); } @@ -120,6 +125,21 @@ public class GPXLayer extends OsmandMapLayer { } } + 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() {