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 @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; boolean processed = false;
String operation = command; String operation = command + ":" + gid;
OsMoGroup group = null; OsMoGroup group = null;
if(command.startsWith("GROUP_CHANGE:")) { if(command.equalsIgnoreCase("GROUP_CHANGE")) {
String gid = command.substring(command.indexOf(':') + 1);
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group != null) { if(group != null) {
List<OsMoDevice> delta = mergeGroup(group, obj, false); List<OsMoDevice> delta = mergeGroup(group, obj, false);
for(OsMoDevice d :delta) { for(OsMoDevice d : delta) {
if(d.getDeletedTimestamp() != 0 && d.isEnabled()) { if(d.getDeletedTimestamp() != 0 && d.isEnabled()) {
disconnectImpl(d); disconnectImpl(d);
} else if(d.isEnabled() && !d.isActive()) { } else if(d.isEnabled() && !d.isActive()) {
@ -152,15 +151,13 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
storage.save(); storage.save();
} }
processed = true; processed = true;
} else if(command.startsWith("GROUP_DISCONNECT:")) { } else if(command.equalsIgnoreCase("GROUP_DISCONNECT")) {
String gid = command.substring(command.indexOf(':') + 1);
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group != null) { if(group != null) {
disconnectAllGroupUsers(group); disconnectAllGroupUsers(group);
} }
processed = true; processed = true;
} else if(command.startsWith("GROUP_CONNECT:")) { } else if(command.equalsIgnoreCase("GROUP_CONNECT")) {
String gid = command.substring(command.indexOf(':') + 1);
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group != null) { if(group != null) {
mergeGroup(group, obj, true); mergeGroup(group, obj, true);
@ -172,9 +169,9 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
storage.save(); storage.save();
} }
processed = true; processed = true;
} else if(command.startsWith("AGROUP_CREATE") || command.startsWith("GROUP_JOIN:") ) { } else if(command.equalsIgnoreCase("AGROUP_CREATE") || command.equalsIgnoreCase("GROUP_JOIN") ) {
String gid ; if(command.equalsIgnoreCase("AGROUP_CREATE")) {
if(command.startsWith("AGROUP_CREATE")) { operation = command;
try { try {
gid = obj.getString(GROUP_ID); gid = obj.getString(GROUP_ID);
} catch (JSONException e) { } catch (JSONException e) {
@ -182,8 +179,6 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
service.showErrorMessage(e.getMessage()); service.showErrorMessage(e.getMessage());
return true; return true;
} }
} else {
gid = command.substring(command.indexOf(':') + 1);
} }
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group == null) { if(group == null) {
@ -191,12 +186,10 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
group.groupId = gid; group.groupId = gid;
storage.addGroup(group); storage.addGroup(group);
} }
parseGroup(obj, group);
connectGroupImpl(group); connectGroupImpl(group);
storage.save(); storage.save();
processed = true; processed = true;
} else if(command.startsWith("GROUP_LEAVE:")) { } else if(command.equals("GROUP_LEAVE")) {
String gid = command.substring(command.indexOf(':') + 1);
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group != null) { if(group != null) {
storage.deleteGroup(group); storage.deleteGroup(group);
@ -242,7 +235,7 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
private List<OsMoDevice> mergeGroup(OsMoGroup gr, JSONObject obj, boolean deleteUsers) { private List<OsMoDevice> mergeGroup(OsMoGroup gr, JSONObject obj, boolean deleteUsers) {
List<OsMoDevice> delta = new ArrayList<OsMoDevice>(); List<OsMoDevice> delta = new ArrayList<OsMoDevice>();
try { try {
parseGroup(obj, gr); parseGroup(obj.getJSONObject("group"), gr);
JSONArray arr = obj.getJSONArray(USERS); JSONArray arr = obj.getJSONArray(USERS);
Map<String, OsMoDevice> toDelete = new HashMap<String, OsMoDevice>(gr.users); Map<String, OsMoDevice> toDelete = new HashMap<String, OsMoDevice>(gr.users);
for (int i = 0; i < arr.length(); i++) { 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.MapActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.OsmandExpandableListActivity; import net.osmand.plus.activities.OsmandExpandableListActivity;
import net.osmand.plus.activities.SettingsNavigationActivity;
import net.osmand.plus.activities.actions.ShareDialog; import net.osmand.plus.activities.actions.ShareDialog;
import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.osmo.OsMoGroups.OsMoGroupsUIListener; import net.osmand.plus.osmo.OsMoGroups.OsMoGroupsUIListener;
@ -41,6 +42,7 @@ import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
@ -438,24 +440,30 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
quitSelectionMode(); 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) { protected void showGroupInfo(OsMoGroup group) {
Builder bld = new AlertDialog.Builder(this); Builder bld = new AlertDialog.Builder(this);
bld.setTitle(R.string.osmo_group); bld.setTitle(R.string.osmo_group);
StringBuilder sb = new StringBuilder(); 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) { 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) { 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) { 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.setMessage(sb.toString());
bld.show(); bld.show();
@ -501,6 +509,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
if (item.getItemId() == CONNECT_TO) { if (item.getItemId() == CONNECT_TO) {
connectToDevice(); connectToDevice();
return true; return true;
} else if (item.getItemId() == SETTINGS_ID) {
startActivity(new Intent(this, SettingsOsMoActivity.class));
return true;
} else if (item.getItemId() == SHARE_SESSION) { } else if (item.getItemId() == SHARE_SESSION) {
shareSession(); shareSession();
return true; return true;
@ -663,6 +674,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
createMenuItem(menu, CREATE_GROUP, R.string.osmo_create_group, createMenuItem(menu, CREATE_GROUP, R.string.osmo_create_group,
0, 0,/*R.drawable.ic_action_marker_light,*/ 0, 0,/*R.drawable.ic_action_marker_light,*/
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 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); return super.onCreateOptionsMenu(menu);
} }
@ -694,7 +708,9 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
if(operation == top || (operation != null && operation.equals(top))) { if(operation == top || (operation != null && operation.equals(top))) {
hideProgressBar(); hideProgressBar();
} }
adapter.update(group); if(group != null) {
adapter.update(group);
}
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
updateStatus(); updateStatus();
} }
@ -1056,9 +1072,8 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements
@Override @Override
public void draw(Canvas canvas) { 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) / 6, white);
canvas.drawCircle(width/2, height/2, (width + height) / 7, paintRouteDirection);
} }
@Override @Override

View file

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

View file

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

View file

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

View file

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

View file

@ -1,12 +1,18 @@
package net.osmand.plus.osmo; 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.access.AccessibleToast;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.activities.actions.ShareDialog; import net.osmand.plus.activities.actions.ShareDialog;
import net.osmand.plus.osmo.OsMoService.SessionInfo; import net.osmand.plus.osmo.OsMoService.SessionInfo;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.Preference; import android.preference.Preference;
@ -83,11 +89,21 @@ public class SettingsOsMoActivity extends SettingsBaseActivity {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
final OsMoPlugin plugin = OsMoPlugin.getEnabledPlugin(OsMoPlugin.class);
if (preference == debugPref) { if (preference == debugPref) {
updateDebugPref(); 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; return true;
} else if(preference == trackerId) { } else if(preference == trackerId) {
final OsMoPlugin plugin = OsMoPlugin.getEnabledPlugin(OsMoPlugin.class);
OsMoService service = plugin.getService(); OsMoService service = plugin.getService();
SessionInfo ci = service.getCurrentSessionInfo(); SessionInfo ci = service.getCurrentSessionInfo();
if(ci == null || ci.trackerId == null) { if(ci == null || ci.trackerId == null) {