From 71dabcc563cf507ae20ed22eddccd8465699c4df Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 27 Jan 2012 00:46:15 +0100 Subject: [PATCH] Add live monitoring --- OsmAnd/res/values/strings.xml | 6 +++ OsmAnd/res/xml/settings_pref.xml | 6 +++ .../net/osmand/plus/NavigationService.java | 5 ++ .../src/net/osmand/plus/OsmandSettings.java | 11 +++++ .../plus/activities/LiveMonitoringHelper.java | 48 +++++++++++++++++++ .../osmand/plus/activities/MapActivity.java | 22 ++++++--- .../plus/activities/SettingsActivity.java | 3 ++ 7 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/activities/LiveMonitoringHelper.java diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 1a1334edc6..d692acd5b5 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,11 @@ + Enable sending HTTP requests to the specified web service + Enable Live Tracking + Specify Live Tracking inverval + Live Tracking intervals + Specify Live Tracking web address using following patterns : lat={0}, lon={1}, timestamp={2}, hdop={3}, altitude={5}, speed={6} + Live Tracking web address Please enable \'Log track to GPX\' Tracking settings Show current track Changes in 0.7.0 : diff --git a/OsmAnd/res/xml/settings_pref.xml b/OsmAnd/res/xml/settings_pref.xml index 004739fc66..cf49b18959 100644 --- a/OsmAnd/res/xml/settings_pref.xml +++ b/OsmAnd/res/xml/settings_pref.xml @@ -56,6 +56,12 @@ + + + diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index 209792b21f..9da30d5436 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -2,6 +2,7 @@ package net.osmand.plus; import net.osmand.Version; +import net.osmand.plus.activities.LiveMonitoringHelper; import net.osmand.plus.activities.OsmandApplication; import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.routing.RoutingHelper; @@ -53,6 +54,7 @@ public class NavigationService extends Service implements LocationListener { private static WakeLock lockStatic; private PendingIntent pendingIntent; private BroadcastReceiver broadcastReceiver; + private LiveMonitoringHelper liveMonitoringHelper; @Override public IBinder onBind(Intent intent) { @@ -94,6 +96,7 @@ public class NavigationService extends Service implements LocationListener { serviceOffProvider = settings.SERVICE_OFF_PROVIDER.get(); serviceError = settings.SERVICE_OFF_WAIT_INTERVAL.get(); savingTrackHelper = new SavingTrackHelper(this); + liveMonitoringHelper = new LiveMonitoringHelper(this); routingHelper = ((OsmandApplication)getApplication()).getRoutingHelper(); ((OsmandApplication)getApplication()).setNavigationService(this); @@ -193,6 +196,8 @@ public class NavigationService extends Service implements LocationListener { savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(), location.getAccuracy(), location.getTime(), settings); + liveMonitoringHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), + location.getSpeed(), location.getAccuracy(), location.getTime(), settings); if(routingHelper.isFollowingMode()){ routingHelper.setCurrentLocation(location); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 5513cca82f..e9522bc937 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -479,6 +479,17 @@ public class OsmandSettings { SAVE_TRACK_INTERVAL.setModeDefaultValue(ApplicationMode.BICYCLE, 10); SAVE_TRACK_INTERVAL.setModeDefaultValue(ApplicationMode.PEDESTRIAN, 20); } + + // this value string is synchronized with settings_pref.xml preference name + public final CommonPreference LIVE_MONITORING = new BooleanPreference("live_monitoring", false, false); + + // this value string is synchronized with settings_pref.xml preference name + public final CommonPreference LIVE_MONITORING_INTERVAL = new IntPreference("live_monitoring_interval", 5, false); + + // this value string is synchronized with settings_pref.xml preference name + public final CommonPreference LIVE_MONITORING_URL = new StringPreference("live_monitoring_url", + "http://example.com?lat={0}&lon={1}×tamp={2}&hdop={3}&altitude={5}&speed={6}", false); + // this value string is synchronized with settings_pref.xml preference name public final OsmandPreference USE_OSMAND_ROUTING_SERVICE_ALWAYS = diff --git a/OsmAnd/src/net/osmand/plus/activities/LiveMonitoringHelper.java b/OsmAnd/src/net/osmand/plus/activities/LiveMonitoringHelper.java new file mode 100644 index 0000000000..a7b829dbd6 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/activities/LiveMonitoringHelper.java @@ -0,0 +1,48 @@ +package net.osmand.plus.activities; + +import java.net.URL; +import java.net.URLConnection; +import java.text.MessageFormat; + +import org.apache.commons.logging.Log; + +import net.osmand.LogUtil; +import net.osmand.plus.OsmandSettings; +import android.content.Context; + +public class LiveMonitoringHelper { + + protected Context ctx; + private OsmandSettings settings; + private long lastTimeUpdated; + private final static Log log = LogUtil.getLog(LiveMonitoringHelper.class); + + public LiveMonitoringHelper(Context ctx){ + this.ctx = ctx; + settings = OsmandSettings.getOsmandSettings(ctx); + } + + public boolean isLiveMonitoringEnabled(){ + return settings.LIVE_MONITORING.get(); + } + + public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, OsmandSettings settings){ + if (time - lastTimeUpdated > settings.LIVE_MONITORING_INTERVAL.get() * 1000) { + sendData((float)lat, (float)lon,(float) alt,(float) speed,(float) hdop, time ); + lastTimeUpdated = time; + } + } + + public void sendData(float lat, float lon, float alt, float speed, float hdop, long time) { + String url = MessageFormat.format(settings.LIVE_MONITORING_URL.get(), lat, lon, time, hdop, alt, speed); + try { + URL curl = new URL(url); + URLConnection conn = curl.openConnection(); + conn.setDoInput(false); + conn.setDoOutput(false); + conn.connect(); + } catch (Exception e) { + log.error("Failed connect to " + url, e); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index b362e78762..d8dac73c18 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -106,6 +106,7 @@ public class MapActivity extends TrackedActivity implements IMapLocationListener final private MapActivityLayers mapLayers = new MapActivityLayers(this); private SavingTrackHelper savingTrackHelper; + private LiveMonitoringHelper liveMonitoringHelper; private RoutingHelper routingHelper; private boolean sensorRegistered = false; @@ -190,6 +191,7 @@ public class MapActivity extends TrackedActivity implements IMapLocationListener savingTrackHelper = new SavingTrackHelper(this); + liveMonitoringHelper = new LiveMonitoringHelper(this); LatLon pointToNavigate = settings.getPointToNavigate(); routingHelper = getMyApplication().getRoutingHelper(); @@ -662,15 +664,21 @@ public class MapActivity extends TrackedActivity implements IMapLocationListener if(Log.isLoggable(LogUtil.TAG, Log.DEBUG)){ Log.d(LogUtil.TAG, "Location changed " + location.getProvider()); //$NON-NLS-1$ } - if(location != null && settings.SAVE_TRACK_TO_GPX.get()){ + if(location != null ){ // write only with 50 meters accuracy 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); + if (settings.SAVE_TRACK_TO_GPX.get()) { + 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); + } + } + if(settings.LIVE_MONITORING.get()){ + liveMonitoringHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), + location.getSpeed(), location.getAccuracy(), location.getTime(), settings); } } } diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index 2a045a8d63..162f1b6be2 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -173,6 +173,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference registerBooleanPreference(osmandSettings.AUTO_ZOOM_MAP,screen); registerBooleanPreference(osmandSettings.AUTO_FOLLOW_ROUTE_NAV,screen); registerBooleanPreference(osmandSettings.SAVE_TRACK_TO_GPX,screen); + registerBooleanPreference(osmandSettings.LIVE_MONITORING,screen); registerBooleanPreference(osmandSettings.DEBUG_RENDERING_INFO,screen); registerBooleanPreference(osmandSettings.FAST_ROUTE_MODE,screen); registerBooleanPreference(osmandSettings.USE_OSMAND_ROUTING_SERVICE_ALWAYS,screen); @@ -190,6 +191,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference registerEditTextPreference(osmandSettings.USER_NAME, screen); registerEditTextPreference(osmandSettings.USER_PASSWORD, screen); + registerEditTextPreference(osmandSettings.LIVE_MONITORING_URL, screen); registerSeekBarPreference(osmandSettings.MAP_OVERLAY_TRANSPARENCY, screen); @@ -285,6 +287,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference registerListPreference(osmandSettings.SERVICE_OFF_PROVIDER, screen, entries, entrieValues); registerTimeListPreference(osmandSettings.SAVE_TRACK_INTERVAL, screen, new int[]{1, 2, 3, 5, 10, 15, 20, 30}, new int[]{1, 2, 3, 5}, 1); + registerTimeListPreference(osmandSettings.LIVE_MONITORING_INTERVAL, screen, new int[]{1, 2, 3, 5, 10, 15, 20, 30}, new int[]{1, 2, 3, 5}, 1); registerTimeListPreference(osmandSettings.SERVICE_OFF_INTERVAL, screen, new int[]{0, 30, 45, 60}, new int[]{2, 3, 5, 10, 15, 30, 45, 60, 90}, 1000); registerTimeListPreference(osmandSettings.SERVICE_OFF_WAIT_INTERVAL, screen,