From 882c45bdd63c1965a7b3b3e5c37efbe6b73ef363 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Wed, 21 May 2014 20:37:10 +0200 Subject: [PATCH] Fix osmo settings --- OsmAnd/res/values/strings.xml | 7 +++- .../src/net/osmand/plus/OsmandSettings.java | 2 + .../src/net/osmand/plus/osmo/OsMoPlugin.java | 22 +++++++---- .../src/net/osmand/plus/osmo/OsMoThread.java | 12 +++--- .../src/net/osmand/plus/osmo/OsMoTracker.java | 24 ++++++------ .../plus/osmo/SettingsOsMoActivity.java | 38 ++++++++++++++++--- 6 files changed, 71 insertions(+), 34 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index be68b808cc..e2753608c9 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,10 +9,12 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Automatically start tracker session and send locations after application startup + Automatically start tracker session Personal tracker id Click to view or share tracker id. Using tracker id connected devices will be able to monitor all movements of this device! To disconnect select Regenerate option. - Session token %1$s + Session token : %1$s Waiting for authorization... Locations sent %1$d (in buffer %2$d) Connection established : %1$s @@ -23,10 +25,11 @@ * Changed layout of countries for downloads (support local names search) Calculate route between points + Restart OsMo session Stop OsMo session Start OsMo session Debug information - + Configure monitoring settings and setup personal monitoring channel OsMo (OpenStreetMap-Monitoring) OpenStreetMap-Monitoring - Advanced Live Monitoring with lots of features for remote control http://osmo.mobi OsMo (Advanced Live Monitoring) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 6f88065da9..0ee65645d4 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -828,6 +828,8 @@ public class OsmandSettings { public final OsmandPreference OSMO_DEVICE_KEY = new StringPreference("osmo_device_token", "").makeGlobal(); + public final OsmandPreference OSMO_AUTO_SEND_LOCATIONS = new BooleanPreference("osmo_automatically_send_locations", false).makeGlobal(); + public final OsmandPreference OSMO_GROUPS = new StringPreference("osmo_groups", "{}").makeGlobal(); // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java index afd11d1d2a..8e31f2eb5e 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java @@ -28,7 +28,9 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer public OsMoPlugin(final OsmandApplication app) { service = new OsMoService(app); tracker = new OsMoTracker(service); - tracker.enableTracker(); + if(app.getSettings().OSMO_AUTO_SEND_LOCATIONS.get()) { + tracker.enableTracker(); + } groups = new OsMoGroups(service, tracker, app.getSettings()); this.app = app; } @@ -74,17 +76,23 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer @Override public void addMonitorActions(ContextMenuAdapter qa, MonitoringInfoControl li, final OsmandMapTileView view) { - final boolean off = !service.isConnected(); - qa.item(off ? R.string.osmo_mode_off : R.string.osmo_mode_on) - .icon(off ? R.drawable.monitoring_rec_inactive : R.drawable.monitoring_rec_big) + //final boolean off = !service.isConnected(); + final boolean autosend = app.getSettings().OSMO_AUTO_SEND_LOCATIONS.get(); + final boolean offTracker = tracker.isEnabledTracker(); + qa.item(autosend ? R.string.osmo_mode_restart : + ( offTracker ? R.string.osmo_mode_off : R.string.osmo_mode_on)) + .icon(offTracker ? R.drawable.monitoring_rec_inactive : R.drawable.monitoring_rec_big) .listen(new OnContextMenuClick() { @Override public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) { - if (off) { - service.connect(true); + if(autosend) { + tracker.disableTracker(); + tracker.enableTracker(); + } else if (offTracker) { + tracker.enableTracker(); } else { - service.disconnect(); + tracker.disableTracker(); } } }).reg(); diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java index 8f30319853..cb79345c64 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java @@ -128,14 +128,12 @@ public class OsMoThread { protected void checkAsyncSocket() { long delay = HEARTBEAT_DELAY; try { - if (selector == null) { - stopThread = true; +// if (selector == null) { +// stopThread = true; + if(activeChannel == null || reconnect) { + initConnection(); } else { - if(activeChannel == null || reconnect) { - initConnection(); - } else { - checkSelectedKeys(); - } + checkSelectedKeys(); } } catch (Exception e) { log.info("Exception selecting socket", e); diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java index f2204791d3..4d7005d299 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java @@ -12,7 +12,6 @@ import org.json.JSONObject; public class OsMoTracker implements OsMoSender, OsMoReactor { private ConcurrentLinkedQueue bufferOfLocations = new ConcurrentLinkedQueue(); private Map otherLocations = new ConcurrentHashMap(); - private boolean trackerStarted; private boolean startSendingLocations; private OsmandMapTileView view; private OsMoService service; @@ -24,12 +23,22 @@ public class OsMoTracker implements OsMoSender, OsMoReactor { service.registerReactor(this); } + public boolean isEnabledTracker() { + return startSendingLocations; + } + public void enableTracker() { - startSendingLocations = true; + if(!startSendingLocations) { + startSendingLocations = true; + service.pushCommand("TRACKER_SESSION_OPEN"); + } } public void disableTracker() { - startSendingLocations = false; + if(startSendingLocations) { + startSendingLocations = false; + service.pushCommand("TRACKER_SESSION_CLOSE"); + } } public void startTrackingId(String id) { @@ -44,15 +53,6 @@ public class OsMoTracker implements OsMoSender, OsMoReactor { @Override public String nextSendCommand(OsMoThread thread) { - if (trackerStarted != startSendingLocations) { - if (!trackerStarted) { - trackerStarted = true; - return "TRACKER_SESSION_OPEN"; - } else { - trackerStarted = false; - return "TRACKER_SESSION_CLOSE"; - } - } if(!bufferOfLocations.isEmpty()){ Location loc = bufferOfLocations.poll(); StringBuilder cmd = new StringBuilder("T|"); diff --git a/OsmAnd/src/net/osmand/plus/osmo/SettingsOsMoActivity.java b/OsmAnd/src/net/osmand/plus/osmo/SettingsOsMoActivity.java index 31b0d75254..718eb7dc4f 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/SettingsOsMoActivity.java +++ b/OsmAnd/src/net/osmand/plus/osmo/SettingsOsMoActivity.java @@ -12,6 +12,7 @@ import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.PreferenceScreen; import android.text.format.DateFormat; @@ -25,6 +26,7 @@ public class SettingsOsMoActivity extends SettingsBaseActivity { public static final String DEFINE_EDIT = "DEFINE_EDIT"; private Preference debugPref; private Preference trackerId; + private CheckBoxPreference sendLocationsref; @Override @@ -34,13 +36,21 @@ public class SettingsOsMoActivity extends SettingsBaseActivity { PreferenceScreen grp = getPreferenceScreen(); + trackerId = new Preference(this); trackerId.setTitle(R.string.osmo_tracker_id); trackerId.setSummary(R.string.osmo_tracker_id_descr); + trackerId.setOnPreferenceClickListener(this); + grp.addPreference(trackerId); + + sendLocationsref = createCheckBoxPreference(settings.OSMO_AUTO_SEND_LOCATIONS); + sendLocationsref.setTitle(R.string.osmo_auto_send_locations); + sendLocationsref.setSummary(R.string.osmo_auto_send_locations_descr); + grp.addPreference(sendLocationsref); debugPref = new Preference(this); debugPref.setTitle(R.string.osmo_settings_debug); - + debugPref.setOnPreferenceClickListener(this); updateDebugPref(); grp.addPreference(debugPref); } @@ -50,8 +60,6 @@ public class SettingsOsMoActivity extends SettingsBaseActivity { OsMoService service = plugin.getService(); OsMoTracker tracker = plugin.getTracker(); StringBuilder s = new StringBuilder(); - s.append(getString(R.string.osmo_settings_uuid)).append(" : ") - .append(getMyApplication().getSettings().OSMO_DEVICE_KEY.get().toUpperCase()).append("\n"); if(service.isConnected()) { int seconds = (int) ((System.currentTimeMillis() - service.getConnectionTime()) / 1000); s.append(getString(R.string.osmo_conn_successfull, Algorithms.formatDuration(seconds))).append("\n"); @@ -62,13 +70,18 @@ public class SettingsOsMoActivity extends SettingsBaseActivity { s.append(getString(R.string.osmo_session_token, si.token)).append("\n"); } } else { - s.append(getString(R.string.osmo_io_error) + service.getLastRegistrationError()).append("\n"); + String err = service.getLastRegistrationError(); + if(err == null) { + err = "..."; + } + s.append(getString(R.string.osmo_io_error) + err).append("\n"); } s.append(getString(R.string.osmo_locations_sent, tracker.getLocationsSent(), - tracker.getBufferLocationsSize())); + tracker.getBufferLocationsSize())).append("\n"); + s.append(getString(R.string.osmo_settings_uuid)).append(" : ") + .append(getMyApplication().getSettings().OSMO_DEVICE_KEY.get().toUpperCase()).append("\n"); debugPref.setSummary(s.toString().trim()); - debugPref.setOnPreferenceClickListener(this); } @Override @@ -91,6 +104,19 @@ public class SettingsOsMoActivity extends SettingsBaseActivity { } return super.onPreferenceClick(preference); } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + boolean p = super.onPreferenceChange(preference, newValue); + String id = preference.getKey(); + if (id.equals(settings.OSMO_AUTO_SEND_LOCATIONS.getId())) { + if ((Boolean) newValue) { + final OsMoPlugin plugin = OsMoPlugin.getEnabledPlugin(OsMoPlugin.class); + plugin.getTracker().enableTracker(); + } + } + return p; + } public void updateAllSettings() {