Fixed open osmo bugs.

This commit is contained in:
GaidamakUA 2016-02-09 16:50:32 +02:00
parent 03b8e7011a
commit 5d0b25ef2b
4 changed files with 66 additions and 39 deletions

View file

@ -0,0 +1,7 @@
package net.osmand.plus.osmo;
public class OsMoConnectionException extends RuntimeException {
public OsMoConnectionException(String detailMessage) {
super(detailMessage);
}
}

View file

@ -134,7 +134,9 @@ 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) {
@ -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,9 +1539,9 @@ 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);
}
@ -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();
}

View file

@ -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;
}
}

View file

@ -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;
@ -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) {