Add settings and add history of commands

This commit is contained in:
vshcherb 2014-05-28 01:12:11 +02:00
parent ed930d3eb1
commit 02f9525728
8 changed files with 96 additions and 36 deletions

View file

@ -133,16 +133,15 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
@Override
public boolean acceptCommand(String command, String data, JSONObject obj, OsMoThread thread) {
public boolean acceptCommand(String command, String gid, String data, JSONObject obj, OsMoThread thread) {
boolean processed = false;
String operation = command;
String operation = command + ":" + gid;
OsMoGroup group = null;
if(command.startsWith("GROUP_CHANGE:")) {
String gid = command.substring(command.indexOf(':') + 1);
if(command.equalsIgnoreCase("GROUP_CHANGE")) {
group = storage.getGroup(gid);
if(group != null) {
List<OsMoDevice> delta = mergeGroup(group, obj, false);
for(OsMoDevice d :delta) {
for(OsMoDevice d : delta) {
if(d.getDeletedTimestamp() != 0 && d.isEnabled()) {
disconnectImpl(d);
} else if(d.isEnabled() && !d.isActive()) {
@ -152,15 +151,13 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
storage.save();
}
processed = true;
} else if(command.startsWith("GROUP_DISCONNECT:")) {
String gid = command.substring(command.indexOf(':') + 1);
} else if(command.equalsIgnoreCase("GROUP_DISCONNECT")) {
group = storage.getGroup(gid);
if(group != null) {
disconnectAllGroupUsers(group);
}
processed = true;
} else if(command.startsWith("GROUP_CONNECT:")) {
String gid = command.substring(command.indexOf(':') + 1);
} else if(command.equalsIgnoreCase("GROUP_CONNECT")) {
group = storage.getGroup(gid);
if(group != null) {
mergeGroup(group, obj, true);
@ -172,9 +169,9 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
storage.save();
}
processed = true;
} else if(command.startsWith("AGROUP_CREATE") || command.startsWith("GROUP_JOIN:") ) {
String gid ;
if(command.startsWith("AGROUP_CREATE")) {
} else if(command.equalsIgnoreCase("AGROUP_CREATE") || command.equalsIgnoreCase("GROUP_JOIN") ) {
if(command.equalsIgnoreCase("AGROUP_CREATE")) {
operation = command;
try {
gid = obj.getString(GROUP_ID);
} catch (JSONException e) {
@ -182,8 +179,6 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
service.showErrorMessage(e.getMessage());
return true;
}
} else {
gid = command.substring(command.indexOf(':') + 1);
}
group = storage.getGroup(gid);
if(group == null) {
@ -191,12 +186,10 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
group.groupId = gid;
storage.addGroup(group);
}
parseGroup(obj, group);
connectGroupImpl(group);
storage.save();
processed = true;
} else if(command.startsWith("GROUP_LEAVE:")) {
String gid = command.substring(command.indexOf(':') + 1);
} else if(command.equals("GROUP_LEAVE")) {
group = storage.getGroup(gid);
if(group != null) {
storage.deleteGroup(group);
@ -242,7 +235,7 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
private List<OsMoDevice> mergeGroup(OsMoGroup gr, JSONObject obj, boolean deleteUsers) {
List<OsMoDevice> delta = new ArrayList<OsMoDevice>();
try {
parseGroup(obj, gr);
parseGroup(obj.getJSONObject("group"), gr);
JSONArray arr = obj.getJSONArray(USERS);
Map<String, OsMoDevice> toDelete = new HashMap<String, OsMoDevice>(gr.users);
for (int i = 0; i < arr.length(); i++) {

View file

@ -30,6 +30,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.OsmandExpandableListActivity;
import net.osmand.plus.activities.SettingsNavigationActivity;
import net.osmand.plus.activities.actions.ShareDialog;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.osmo.OsMoGroups.OsMoGroupsUIListener;
@ -41,6 +42,7 @@ import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
@ -438,24 +440,30 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
quitSelectionMode();
}
});
}
private StringBuilder setFields(StringBuilder bld, int field, String value) {
bld.append(getString(field)).append(": ").append(value).append("\n");
return bld;
}
protected void showGroupInfo(OsMoGroup group) {
Builder bld = new AlertDialog.Builder(this);
bld.setTitle(R.string.osmo_group);
StringBuilder sb = new StringBuilder();
sb.append(getString(R.string.osmo_group_name)).append(" ").append(group.name);
setFields(sb, R.string.osmo_group_name, group.name);
if(group.description != null) {
sb.append(getString(R.string.osmo_group_description)).append(" ").append(group.description);
setFields(sb, R.string.osmo_group_description, group.description);
}
if(group.expireTime != 0) {
sb.append(getString(R.string.osmo_expire_group)).append(" ").append(new Date(group.expireTime).toString());
setFields(sb, R.string.osmo_expire_group, new Date(group.expireTime).toString());
}
if(group.policy != null) {
sb.append(getString(R.string.osmo_group_policy)).append(" ").append(group.policy);
setFields(sb, R.string.osmo_group_policy, group.policy);
}
sb.append(getString(R.string.osmo_connect_to_group_id)).append(" ").append(group.groupId);
setFields(sb, R.string.osmo_connect_to_group_id, group.groupId);
bld.setMessage(sb.toString());
bld.show();
@ -501,6 +509,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
if (item.getItemId() == CONNECT_TO) {
connectToDevice();
return true;
} else if (item.getItemId() == SETTINGS_ID) {
startActivity(new Intent(this, SettingsOsMoActivity.class));
return true;
} else if (item.getItemId() == SHARE_SESSION) {
shareSession();
return true;
@ -663,6 +674,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
createMenuItem(menu, CREATE_GROUP, R.string.osmo_create_group,
0, 0,/*R.drawable.ic_action_marker_light,*/
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, SETTINGS_ID, R.string.settings,
R.drawable.ic_action_settings_light, R.drawable.ic_action_settings_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
@ -694,7 +708,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
if(operation == top || (operation != null && operation.equals(top))) {
hideProgressBar();
}
if(group != null) {
adapter.update(group);
}
adapter.notifyDataSetChanged();
updateStatus();
}
@ -1056,9 +1072,8 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
@Override
public void draw(Canvas canvas) {
canvas.drawCircle(width/2, height/2, (width + height) / 7, paintRouteDirection);
canvas.drawCircle(width/2, height/2, (width + height) / 6, white);
canvas.drawCircle(width/2, height/2, (width + height) / 7, paintRouteDirection);
}
@Override

View file

@ -366,6 +366,9 @@ public class OsMoGroupsStorage {
if(userName != null && userName.length() > 0) {
return userName;
}
if(serverName == null || serverName.length() == 0) {
return trackerId;
}
return serverName;
}
}

View file

@ -4,6 +4,6 @@ import org.json.JSONObject;
public interface OsMoReactor {
public boolean acceptCommand(String command, String data, JSONObject obj, OsMoThread tread);
public boolean acceptCommand(String command, String id, String data, JSONObject obj, OsMoThread tread);
}

View file

@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -61,6 +62,14 @@ public class OsMoService implements OsMoSender, OsMoReactor {
return 0;
}
public List<String> getHistoryOfCommands() {
if(thread == null) {
return Collections.emptyList();
}
return new ArrayList<String>(thread.getLast100Commands());
}
public long getConnectionTime() {
return thread == null || !thread.isConnected() ? System.currentTimeMillis() : thread.getConnectionTime();
}
@ -243,7 +252,7 @@ public class OsMoService implements OsMoSender, OsMoReactor {
}
@Override
public boolean acceptCommand(String command, String data, JSONObject obj, OsMoThread tread) {
public boolean acceptCommand(String command, String id, String data, JSONObject obj, OsMoThread tread) {
if(command.equals("MOTD")) {
SessionInfo si = getCurrentSessionInfo();
if(si != null) {

View file

@ -7,13 +7,17 @@ import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Set;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.osmo.OsMoService.SessionInfo;
@ -25,6 +29,7 @@ import org.json.JSONObject;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.TimeUtils;
public class OsMoThread {
// private static String TRACKER_SERVER = "srv.osmo.mobi";
@ -62,6 +67,9 @@ public class OsMoThread {
private ByteBuffer pendingReadCommand = ByteBuffer.allocate(2048);
private LinkedList<String> queueOfMessages = new LinkedList<String>();
private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
private ConcurrentLinkedQueue<String> last100Commands = new ConcurrentLinkedQueue<String>();
@ -246,11 +254,17 @@ public class OsMoThread {
log.info("OSMO get:"+cmd);
int k = cmd.indexOf('|');
String header = cmd;
String id = "";
String data = "";
if(k >= 0) {
header = cmd.substring(0, k);
data = cmd.substring(k + 1);
}
int ks = header.indexOf(':');
if (ks >= 0) {
id = header.substring(ks + 1);
header = header.substring(0, ks);
}
JSONObject obj = null;
if(data.startsWith("{")) {
try {
@ -285,7 +299,7 @@ public class OsMoThread {
}
boolean processed = false;
for (OsMoReactor o : listReactors) {
if (o.acceptCommand(header, data, obj, this)) {
if (o.acceptCommand(header, id, data, obj, this)) {
processed = true;
break;
}
@ -349,6 +363,7 @@ public class OsMoThread {
if(authorized == 0) {
String auth = "TOKEN|"+ sessionInfo.token;
log.info("OSMO send:" + auth);
cmd(auth, true);
authorized = 1;
return ByteBuffer.wrap(prepareCommand(auth).toString().getBytes("UTF-8"));
}
@ -366,6 +381,8 @@ public class OsMoThread {
if(System.currentTimeMillis() - lastSendCommand > TIMEOUT_TO_PING) {
if(pingSent == 0) {
pingSent = System.currentTimeMillis();
cmd(PING_CMD, true);
log.info("OSMO send " + PING_CMD);
return ByteBuffer.wrap(prepareCommand(PING_CMD).toString().getBytes("UTF-8"));
}
@ -373,6 +390,14 @@ public class OsMoThread {
return null;
}
public ConcurrentLinkedQueue<String> getLast100Commands() {
return last100Commands;
}
private void cmd(String cmd, boolean b) {
last100Commands.add((b ? "> " : ">> ") + df.format(new Date()) + cmd);
}
public SessionInfo getSessionInfo() {
return sessionInfo;
}

View file

@ -65,11 +65,11 @@ public class OsMoTracker implements OsMoSender, OsMoReactor {
}
public void startTrackingId(String id) {
service.pushCommand("LISTEN|"+id);
service.pushCommand("LISTEN:"+id);
}
public void stopTrackingId(String id) {
service.pushCommand("UNLISTEN|"+id);
service.pushCommand("UNLISTEN:"+id);
otherLocations.remove(id);
}
@ -139,7 +139,7 @@ public class OsMoTracker implements OsMoSender, OsMoReactor {
@Override
public boolean acceptCommand(String command, String data, JSONObject obj, OsMoThread thread) {
public boolean acceptCommand(String command, String tid, String data, JSONObject obj, OsMoThread thread) {
if(command.equals("LISTEN")) {
return true;
} else if(command.equals("UNLISTEN")) {
@ -154,8 +154,7 @@ public class OsMoTracker implements OsMoSender, OsMoReactor {
e.printStackTrace();
}
return true;
} else if(command.startsWith("LT:")) {
String tid = command.substring(command.indexOf(':') + 1);
} else if(command.equals("LT")) {
float lat = 0;
float lon = 0;
float speed = 0;

View file

@ -1,12 +1,18 @@
package net.osmand.plus.osmo;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import net.osmand.access.AccessibleToast;
import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.activities.actions.ShareDialog;
import net.osmand.plus.osmo.OsMoService.SessionInfo;
import net.osmand.util.Algorithms;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
@ -83,11 +89,21 @@ public class SettingsOsMoActivity extends SettingsBaseActivity {
@Override
public boolean onPreferenceClick(Preference preference) {
final OsMoPlugin plugin = OsMoPlugin.getEnabledPlugin(OsMoPlugin.class);
if (preference == debugPref) {
updateDebugPref();
Builder bld = new AlertDialog.Builder(this);
StringBuilder bs = new StringBuilder();
List<String> hs = plugin.getService().getHistoryOfCommands();
if(hs != null) {
for(int i = hs.size() - 1 ; i >= 0; i--) {
bs.append(hs.get(i)).append("\n");
}
}
bld.setMessage(bs.toString());
bld.show();
return true;
} else if(preference == trackerId) {
final OsMoPlugin plugin = OsMoPlugin.getEnabledPlugin(OsMoPlugin.class);
OsMoService service = plugin.getService();
SessionInfo ci = service.getCurrentSessionInfo();
if(ci == null || ci.trackerId == null) {