Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
0cd1f300eb
4 changed files with 66 additions and 39 deletions
|
@ -0,0 +1,7 @@
|
|||
package net.osmand.plus.osmo;
|
||||
|
||||
public class OsMoConnectionException extends RuntimeException {
|
||||
public OsMoConnectionException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> queueOfMessages = new LinkedList<String>();
|
||||
|
||||
|
||||
private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss", Locale.US);
|
||||
|
||||
|
||||
private ConcurrentLinkedQueue<String> lastCommands = new ConcurrentLinkedQueue<String>();
|
||||
private final static int STACK_CMD = 30;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public OsMoThread(OsMoService service) {
|
||||
this.service = service;
|
||||
|
@ -116,7 +118,7 @@ public class OsMoThread {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private Collection<OsMoReactor> 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<String> 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";
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue