Update osmo service
This commit is contained in:
parent
4868fb7eb5
commit
3753d8a7a9
3 changed files with 133 additions and 45 deletions
|
@ -9,6 +9,9 @@
|
||||||
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
|
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
|
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
|
||||||
-->
|
-->
|
||||||
|
<string name="osmo_io_error">OSMo connection problem : </string>
|
||||||
|
<string name="osmo_mode_on">Stop OSMo</string>
|
||||||
|
<string name="osmo_mode_off">Start OSMo</string>
|
||||||
<string name="osmo_settings_uuid">Unique device id</string>
|
<string name="osmo_settings_uuid">Unique device id</string>
|
||||||
<string name="osmo_settings_descr">View unique device registration key and other monitoring specific settings </string>
|
<string name="osmo_settings_descr">View unique device registration key and other monitoring specific settings </string>
|
||||||
<string name="osmo_settings">OSMo (OpenStreetMap-Monitoring)</string>
|
<string name="osmo_settings">OSMo (OpenStreetMap-Monitoring)</string>
|
||||||
|
|
|
@ -20,6 +20,7 @@ import org.apache.commons.logging.Log;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.Preference.OnPreferenceClickListener;
|
import android.preference.Preference.OnPreferenceClickListener;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
|
@ -31,33 +32,33 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer
|
||||||
public static final String ID = "osmand.osmo";
|
public static final String ID = "osmand.osmo";
|
||||||
private static final Log log = PlatformUtil.getLog(OsMoPlugin.class);
|
private static final Log log = PlatformUtil.getLog(OsMoPlugin.class);
|
||||||
private OsMoService service;
|
private OsMoService service;
|
||||||
|
|
||||||
public OsMoPlugin(final OsmandApplication app){
|
public OsMoPlugin(final OsmandApplication app) {
|
||||||
service = new OsMoService();
|
service = new OsMoService();
|
||||||
this.app = app;
|
this.app = app;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean init(final OsmandApplication app) {
|
public boolean init(final OsmandApplication app) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateLocation(Location location) {
|
public void updateLocation(Location location) {
|
||||||
if(service.isActive()) {
|
if (service.isActive()) {
|
||||||
try {
|
try {
|
||||||
service.sendCoordinate(location.getLatitude(), location.getLongitude(), location.getAccuracy(),
|
service.sendCoordinate(location.getLatitude(), location.getLongitude(), location.getAccuracy(),
|
||||||
(float) location.getAltitude(), location.getSpeed(), location.getBearing());
|
(float) location.getAltitude(), location.getSpeed(), location.getBearing());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUUID(Context ctx) {
|
public static String getUUID(Context ctx) {
|
||||||
return Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
|
return Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return app.getString(R.string.osmo_plugin_description);
|
return app.getString(R.string.osmo_plugin_description);
|
||||||
|
@ -65,65 +66,108 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return app.getString(R.string.osmo_plugin_name) ;
|
return app.getString(R.string.osmo_plugin_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateLayers(OsmandMapTileView mapView, MapActivity activity) {
|
public void updateLayers(OsmandMapTileView mapView, MapActivity activity) {
|
||||||
// registerLayers(activity);
|
// registerLayers(activity);
|
||||||
super.updateLayers(mapView, activity);
|
super.updateLayers(mapView, activity);
|
||||||
MonitoringInfoControl lock = activity.getMapLayers().getMapInfoLayer().getMonitoringInfoControl();
|
MonitoringInfoControl lock = activity.getMapLayers().getMapInfoLayer().getMonitoringInfoControl();
|
||||||
if(lock != null && !lock.getMonitorActions().contains(this)) {
|
if (lock != null && !lock.getMonitorActions().contains(this)) {
|
||||||
lock.addMonitorActions(this);
|
lock.addMonitorActions(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void connect(final boolean enable) {
|
||||||
|
new AsyncTask<Void, Void, String>() {
|
||||||
|
private Exception e;
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(Void... params) {
|
||||||
|
try {
|
||||||
|
String response;
|
||||||
|
if (enable) {
|
||||||
|
response = service.activate(getUUID(app));
|
||||||
|
} else {
|
||||||
|
response = service.deactivate();
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.e = e;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected void onPostExecute(String result) {
|
||||||
|
if(e != null) {
|
||||||
|
app.showToastMessage(app.getString(R.string.osmo_io_error) + ": " + e.getMessage());
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
} else {
|
||||||
|
app.showToastMessage(result + "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCoordinate(final double lat, final double lon) {
|
||||||
|
new AsyncTask<Void, Void, String>() {
|
||||||
|
|
||||||
|
private Exception e;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(Void... params) {
|
||||||
|
try {
|
||||||
|
return service.sendCoordinate(lat, lon, 0, 0, 0,
|
||||||
|
0);
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.e = e;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected void onPostExecute(String result) {
|
||||||
|
if(e != null) {
|
||||||
|
app.showToastMessage(app.getString(R.string.osmo_io_error) + ": " + e.getMessage());
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
} else {
|
||||||
|
app.showToastMessage(result + "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addMonitorActions(ContextMenuAdapter qa, MonitoringInfoControl li, final OsmandMapTileView view) {
|
public void addMonitorActions(ContextMenuAdapter qa, MonitoringInfoControl li, final OsmandMapTileView view) {
|
||||||
final boolean off = !service.isActive();
|
final boolean off = !service.isActive();
|
||||||
qa.item(off ? R.string.osmodroid_mode_off : R.string.osmodroid_mode_on)
|
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)
|
.icon(off ? R.drawable.monitoring_rec_inactive : R.drawable.monitoring_rec_big)
|
||||||
.listen(new OnContextMenuClick() {
|
.listen(new OnContextMenuClick() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
|
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
|
||||||
try {
|
connect(off);
|
||||||
String response;
|
|
||||||
if (off) {
|
|
||||||
response = service.activate(getUUID(app));
|
|
||||||
} else {
|
|
||||||
response = service.deactivate();
|
|
||||||
}
|
|
||||||
app.showToastMessage(response);
|
|
||||||
} catch (Exception e) {
|
|
||||||
app.showToastMessage(app.getString(R.string.error_io_error) + ": " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}).reg();
|
}).reg();
|
||||||
qa.item("Test (send)").
|
qa.item("Test (send)").icons(R.drawable.ic_action_grefresh_dark, R.drawable.ic_action_grefresh_light)
|
||||||
icons(R.drawable.ic_action_grefresh_dark, R.drawable.ic_action_grefresh_light). listen(new OnContextMenuClick() {
|
.listen(new OnContextMenuClick() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
|
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
|
||||||
try {
|
final double lat = view.getLatitude();
|
||||||
String response = service.sendCoordinate(view.getLatitude(), view.getLongitude(), 0, 0, 0, 0);
|
final double lon = view.getLongitude();
|
||||||
app.showToastMessage(response);
|
sendCoordinate(lat, lon);
|
||||||
} catch (Exception e) {
|
}
|
||||||
app.showToastMessage(app.getString(R.string.error_io_error) + ": " + e.getMessage());
|
}).reg();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}).reg();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void settingsActivityCreate(final SettingsActivity activity, PreferenceScreen screen) {
|
public void settingsActivityCreate(final SettingsActivity activity, PreferenceScreen screen) {
|
||||||
Preference grp = new Preference(activity);
|
Preference grp = new Preference(activity);
|
||||||
grp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
grp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
activity.startActivity(new Intent(activity, SettingsOsMoActivity.class));
|
activity.startActivity(new Intent(activity, SettingsOsMoActivity.class));
|
||||||
|
|
|
@ -5,38 +5,79 @@ import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
import net.osmand.PlatformUtil;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
|
||||||
public class OsMoService {
|
public class OsMoService {
|
||||||
private static String TRACKER_SERVER = "srv.osmo.mobi";
|
private static String TRACKER_SERVER = "srv.osmo.mobi";
|
||||||
private static int TRACKER_PORT = 4242;
|
private static int TRACKER_PORT = 4242;
|
||||||
|
public static int NUMBER_OF_TRIES_TO_RECONNECT = 20;
|
||||||
|
private Log log = PlatformUtil.getLog(OsMoService.class);
|
||||||
private OutputStreamWriter out;
|
private OutputStreamWriter out;
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
private Queue<String> buffer = new LinkedList<String>();
|
||||||
|
private int numberOfFailures = 0;
|
||||||
|
private String loginHash;
|
||||||
|
|
||||||
public boolean isActive() {
|
public boolean isActive() {
|
||||||
return socket != null;
|
return socket != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String activate(String hash) throws IOException {
|
public String activate(String loginHash) throws IOException {
|
||||||
|
this.loginHash = loginHash;
|
||||||
socket = new Socket(TRACKER_SERVER, TRACKER_PORT);
|
socket = new Socket(TRACKER_SERVER, TRACKER_PORT);
|
||||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
||||||
out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
|
out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
|
||||||
|
|
||||||
String t = sendCommand("auth|"+hash);
|
String t = sendCommand("auth|"+loginHash);
|
||||||
t += sendCommand("session_open");
|
t += sendCommand("session_open");
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String sendCommand(String s) throws IOException {
|
private String sendCommand(String s) throws IOException {
|
||||||
if(s.endsWith("\n")) {
|
s = prepareCommand(s);
|
||||||
s += "\n";
|
log.info("OSMo command : " + s);
|
||||||
}
|
|
||||||
out.write(s);
|
out.write(s);
|
||||||
return in.readLine();
|
return in.readLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String prepareCommand(String s) {
|
||||||
|
if(s.endsWith("\n")) {
|
||||||
|
s += "\n";
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
public String sendCoordinate(double lat, double lon, float hdop, float alt, float speed, float bearing) throws IOException {
|
public String sendCoordinate(double lat, double lon, float hdop, float alt, float speed, float bearing) throws IOException {
|
||||||
return sendCommand("p|"+lat+":"+lon+":"+hdop+":"+alt+":"+speed+":"+bearing);
|
return sendCommandWithBuffer("p|"+lat+":"+lon+":"+hdop+":"+alt+":"+speed+":"+bearing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized String sendCommandWithBuffer(String command) throws IOException {
|
||||||
|
buffer.add(command);
|
||||||
|
String lastResponse = null;
|
||||||
|
while (!buffer.isEmpty()) {
|
||||||
|
reconnectIfNeeded();
|
||||||
|
try {
|
||||||
|
lastResponse = sendCommand(command);
|
||||||
|
} catch (IOException e) {
|
||||||
|
numberOfFailures++;
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lastResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reconnectIfNeeded() throws IOException {
|
||||||
|
if(numberOfFailures >= NUMBER_OF_TRIES_TO_RECONNECT) {
|
||||||
|
deactivate();
|
||||||
|
activate(this.loginHash);
|
||||||
|
numberOfFailures = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String deactivate() throws IOException {
|
public String deactivate() throws IOException {
|
||||||
|
|
Loading…
Reference in a new issue