From 5d0b25ef2b53979773992c693a0b56496f68e873 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 9 Feb 2016 16:50:32 +0200 Subject: [PATCH] Fixed open osmo bugs. --- .../plus/osmo/OsMoConnectionException.java | 7 +++ .../osmand/plus/osmo/OsMoGroupsActivity.java | 25 ++++++--- .../src/net/osmand/plus/osmo/OsMoService.java | 17 +++--- .../src/net/osmand/plus/osmo/OsMoThread.java | 56 +++++++++++-------- 4 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/osmo/OsMoConnectionException.java diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoConnectionException.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoConnectionException.java new file mode 100644 index 0000000000..8966e3c9ad --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoConnectionException.java @@ -0,0 +1,7 @@ +package net.osmand.plus.osmo; + +public class OsMoConnectionException extends RuntimeException { + public OsMoConnectionException(String detailMessage) { + super(detailMessage); + } +} diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java index 800eb21e12..423754752c 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java @@ -134,8 +134,10 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements private Paint white; private View header; private View footer; - - + private CompoundButton srvc; + + private int connections = 0; + @Override public void onCreate(Bundle icicle) { // This has to be called before setContentView and you must use the @@ -213,7 +215,7 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements }); updateTrackerButton(); - CompoundButton srvc = (CompoundButton) header.findViewById(R.id.enable_service); + srvc = (CompoundButton) header.findViewById(R.id.enable_service); srvc.setChecked(osMoPlugin.getService().isEnabled()); srvc.setText(R.string.osmo_start_service); srvc.setOnCheckedChangeListener(new OnCheckedChangeListener() { @@ -1537,11 +1539,11 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements public void handleConnect() { app.runInUIThread(new Runnable() { - @Override public void run() { + connections++; if (getExpandableListView().getFooterViewsCount() > 0) { - getExpandableListView().removeFooterView(footer); + getExpandableListView().removeFooterView(footer); } updateStatus(); } @@ -1553,13 +1555,18 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements @Override public void run() { - if (!TextUtils.isEmpty(msg)) { + if (!TextUtils.isEmpty(msg) && connections > 0) { CompoundButton srvc = (CompoundButton) header.findViewById(R.id.enable_service); if (srvc.isChecked()) { - if (getExpandableListView().getFooterViewsCount() == 0) { - getExpandableListView().addFooterView(footer); + if (connections == 1) { + if (getExpandableListView().getFooterViewsCount() == 0) { + getExpandableListView().addFooterView(footer); + } + adapter.clear(); + connections--; + } else { + connections = 1; } - adapter.clear(); } updateStatus(); } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java index 255676f07f..2bda34ad49 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java @@ -30,19 +30,16 @@ import org.json.JSONObject; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; -import java.net.URLEncoder; public class OsMoService implements OsMoReactor { private static final String HTTP_API_PREPARE = "http://api.osmo.mobi/prepare"; @@ -237,7 +234,7 @@ public class OsMoService implements OsMoReactor { final JSONObject obj = new JSONObject(r); if(obj.has("error")) { lastRegistrationError = obj.getString("error"); - throw new RuntimeException(obj.getString("error")); + throw new OsMoConnectionException(obj.getString("error")); } app.getSettings().OSMO_DEVICE_KEY.set(obj.getString("key")); return obj.getString("key"); @@ -403,7 +400,7 @@ public class OsMoService implements OsMoReactor { public void showErrorMessage(String string) { - app.showToastMessage(app.getString(R.string.osmo_io_error) + string); + app.showToastMessage(app.getString(R.string.osmo_io_error) + string); } @@ -475,4 +472,8 @@ public class OsMoService implements OsMoReactor { String userName = app.getSettings().OSMO_USER_NAME.get(); return ((!TextUtils.isEmpty(psswd) && !TextUtils.isEmpty(userName))); } + + public OsmandApplication getMyApplication() { + return app; + } } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java index cebdfbf10f..81d6251415 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java @@ -2,7 +2,9 @@ package net.osmand.plus.osmo; import android.os.Handler; import android.os.HandlerThread; +import android.os.Looper; import android.os.Message; +import android.widget.Toast; import net.osmand.PlatformUtil; import net.osmand.plus.osmo.OsMoService.SessionInfo; @@ -33,7 +35,7 @@ public class OsMoThread { // private static String TRACKER_SERVER = "srv.osmo.mobi"; // private static int TRACKER_PORT = 3245; - + private static final String PING_CMD = "P"; protected final static Log log = PlatformUtil.getLog(OsMoThread.class); private static final long HEARTBEAT_DELAY = 100; @@ -63,13 +65,13 @@ public class OsMoThread { private String readCommand = ""; private ByteBuffer pendingReadCommand = ByteBuffer.allocate(2048); private LinkedList queueOfMessages = new LinkedList(); - + private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss", Locale.US); - + private ConcurrentLinkedQueue lastCommands = new ConcurrentLinkedQueue(); private final static int STACK_CMD = 30; - - + + public OsMoThread(OsMoService service) { this.service = service; @@ -116,7 +118,7 @@ public class OsMoThread { } } - + private Collection getReactors() { return service.getListReactors(); } @@ -150,9 +152,9 @@ public class OsMoThread { public boolean isConnected() { return activeChannel != null; } - + public boolean isActive() { - return activeChannel != null && pingSent == 0 && authorized == 2; + return activeChannel != null && pingSent == 0 && authorized == 2; } protected void checkAsyncSocket() { @@ -175,15 +177,25 @@ public class OsMoThread { if (activeChannel != null && !activeChannel.isConnected()) { activeChannel = null; } - String msg = e.getMessage(); + final String msg = e.getMessage(); for(OsMoReactor sender : getReactors()) { sender.onDisconnected(msg); } delay = HEARTBEAT_FAILED_DELAY; - if(lastSendCommand != 0 && System.currentTimeMillis() - lastSendCommand > TIMEOUT_TO_RECONNECT ) { - reconnect = true; - } else if (failures++ > LIMIT_OF_FAILURES_RECONNECT) { - reconnect = true; + if (e instanceof OsMoConnectionException) { + stopThread = true; + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + Toast.makeText(service.getMyApplication(), msg, Toast.LENGTH_LONG).show(); + } + }); + } else { + if (lastSendCommand != 0 && System.currentTimeMillis() - lastSendCommand > TIMEOUT_TO_RECONNECT) { + reconnect = true; + } else if (failures++ > LIMIT_OF_FAILURES_RECONNECT) { + reconnect = true; + } } } if (stopThread) { @@ -300,7 +312,7 @@ public class OsMoThread { } catch (JSONException e) { e.printStackTrace(); } - } + } boolean error = false; if(obj != null && obj.has("error")) { error = true; @@ -376,7 +388,7 @@ public class OsMoThread { } } } - + public long getConnectionTime() { return connectionTime; } @@ -387,7 +399,7 @@ public class OsMoThread { cmd(auth, true); authorized = 1; pendingSendCommand = ByteBuffer.wrap(prepareCommand(auth).toString().getBytes("UTF-8")); - } + } if (pendingSendCommand == null) { pendingSendCommand = getNewPendingSendCommand(); } @@ -401,8 +413,8 @@ public class OsMoThread { } } } - - + + private ByteBuffer getNewPendingSendCommand() throws UnsupportedEncodingException { if(authorized == 1) { @@ -419,7 +431,7 @@ public class OsMoThread { cmd(l, true); return ByteBuffer.wrap(prepareCommand(l).toString().getBytes("UTF-8")); } - + } final long interval = System.currentTimeMillis() - lastSendCommand; if(interval > TIMEOUT_TO_PING) { @@ -434,11 +446,11 @@ public class OsMoThread { } return null; } - + public ConcurrentLinkedQueue getLastCommands() { return lastCommands; } - + private void cmd(String cmd, boolean send) { log.info("OsMO" + (send ? "> " : ">> ") + cmd); lastCommands.add((send ? "> " : ">> ") + df.format(new Date()) + " " + cmd); @@ -460,7 +472,7 @@ public class OsMoThread { } res.append(c); } - + String finalCmd = res.toString().trim(); return finalCmd + "=\n"; }