From 376c267348787ccb6b7057031861061340124398 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Wed, 28 May 2014 21:48:20 +0200 Subject: [PATCH] Fix small issues and add lock device to position --- OsmAnd/res/values-ru/strings.xml | 2 +- .../src/net/osmand/plus/OsmAndConstants.java | 10 +-- .../src/net/osmand/plus/osmo/OsMoGroups.java | 9 +- .../osmand/plus/osmo/OsMoGroupsActivity.java | 21 +++-- .../osmand/plus/osmo/OsMoGroupsStorage.java | 3 +- .../src/net/osmand/plus/osmo/OsMoPlugin.java | 32 +++---- .../osmand/plus/osmo/OsMoPositionLayer.java | 85 ++++++++++++++++++- .../src/net/osmand/plus/osmo/OsMoTracker.java | 18 ++-- .../osmand/plus/views/ContextMenuLayer.java | 10 ++- 9 files changed, 138 insertions(+), 52 deletions(-) diff --git a/OsmAnd/res/values-ru/strings.xml b/OsmAnd/res/values-ru/strings.xml index 2b8f9a964a..defcb02438 100644 --- a/OsmAnd/res/values-ru/strings.xml +++ b/OsmAnd/res/values-ru/strings.xml @@ -1297,7 +1297,7 @@ Доступно %1$d файлов для скачивания осталось %1$d файлов - Пожалуйста, подождите, пока завершится текущий трек + Пожалуйста, подождите, пока завершится текущая операция Открыть существующий GPX Начать новый подтрек Имя файла GPX diff --git a/OsmAnd/src/net/osmand/plus/OsmAndConstants.java b/OsmAnd/src/net/osmand/plus/OsmAndConstants.java index 21ca887c6f..4e808ac947 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndConstants.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndConstants.java @@ -3,14 +3,14 @@ package net.osmand.plus; public interface OsmAndConstants { - public int UI_HANDLER_MAP_VIEW = 300; + public int UI_HANDLER_MAP_VIEW = 3000; - public int UI_HANDLER_MAP_CONTROLS = 400; + public int UI_HANDLER_MAP_CONTROLS = 4000; - public int UI_HANDLER_LOCATION_SERVICE = 500; + public int UI_HANDLER_LOCATION_SERVICE = 5000; - public int UI_HANDLER_PROGRESS = 600; + public int UI_HANDLER_PROGRESS = 6000; - public int UI_HANDLER_SEARCH = 700; + public int UI_HANDLER_SEARCH = 7000; } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java index 8292165e63..cb13b308b7 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java @@ -38,6 +38,8 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { public interface OsMoGroupsUIListener { public void groupsListChange(String operation, OsMoGroup group); + + public void deviceLocationChanged(OsMoDevice device); } public OsMoGroups(OsMoService service, OsMoTracker tracker, OsmandSettings settings) { @@ -132,8 +134,11 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { @Override public void locationChange(String trackerId, Location location) { - for(OsMoGroup g: getGroups()) { - g.updateLastLocation(trackerId, location); + for (OsMoGroup g : getGroups()) { + OsMoDevice d = g.updateLastLocation(trackerId, location); + if (d != null && uiListener != null) { + uiListener.deviceLocationChanged(d); + } } } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java index 4fe840365f..cdc41e9913 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsActivity.java @@ -36,7 +36,6 @@ import net.osmand.plus.osmo.OsMoGroups.OsMoGroupsUIListener; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoGroup; import net.osmand.plus.osmo.OsMoService.SessionInfo; -import net.osmand.plus.osmo.OsMoTracker.OsmoTrackerListener; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; @@ -89,7 +88,7 @@ import com.actionbarsherlock.view.Window; * */ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements OsmAndCompassListener, - OsmAndLocationListener, OsmoTrackerListener, OsMoGroupsUIListener { + OsmAndLocationListener, OsMoGroupsUIListener { public static final int CONNECT_TO = 1; protected static final int DELETE_ACTION_ID = 2; @@ -303,7 +302,6 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements app.getLocationProvider().registerOrUnregisterCompassListener(true); app.getLocationProvider().addLocationListener(this); app.getLocationProvider().resumeAllUpdates(); - osMoPlugin.getTracker().setUITrackerListener(this); osMoPlugin.getGroups().setUiListener(this); adapter.synchronizeGroups(); } @@ -314,7 +312,6 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements app.getLocationProvider().pauseAllUpdates(); app.getLocationProvider().removeCompassListener(this); app.getLocationProvider().removeLocationListener(this); - osMoPlugin.getTracker().setUITrackerListener(null); osMoPlugin.getGroups().setUiListener(null); } @@ -339,9 +336,6 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements if(device != null && device.getLastLocation() != null) { createMenuItem(menu, SHOW_ON_MAP_ID, R.string.show_poi_on_map, R.drawable.ic_action_marker_light, R.drawable.ic_action_marker_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); - } else if(group != null) { - createMenuItem(menu, SHOW_ON_MAP_ID, R.string.show_poi_on_map, R.drawable.ic_action_marker_light, R.drawable.ic_action_marker_dark, - MenuItem.SHOW_AS_ACTION_IF_ROOM); } createMenuItem(menu, SHARE_ID, R.string.share_fav, R.drawable.ic_action_gshare_light, R.drawable.ic_action_gshare_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); @@ -354,8 +348,11 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements createMenuItem(menu, GROUP_INFO, R.string.osmo_group_info, R.drawable.ic_action_info_light, R.drawable.ic_action_info_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); } - createMenuItem(menu, DELETE_ACTION_ID, R.string.default_buttons_delete, R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark, - MenuItem.SHOW_AS_ACTION_IF_ROOM); + if (device == null || device.getGroup().isMainGroup()) { + createMenuItem(menu, DELETE_ACTION_ID, R.string.default_buttons_delete, + R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark, + MenuItem.SHOW_AS_ACTION_IF_ROOM); + } if (mi != null) { @@ -417,7 +414,8 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements if(device != null) { Location location = device.getLastLocation(); app.getSettings().setMapLocationToShow(location.getLatitude(), location.getLongitude(), app.getSettings().getMapZoomToShow(), - null, device.getVisibleName(), device); + null, device.getVisibleName(), device); + osMoPlugin.setMapFollowTrackerId(device); MapActivity.launchMapActivityMoveToTop(OsMoGroupsActivity.this); } } else if(item.getItemId() == ON_OFF_ACTION_ID) { @@ -1003,7 +1001,7 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements } @Override - public void locationChange(String trackerId, Location loc) { + public void deviceLocationChanged(OsMoDevice device) { refreshList(); } public static String colorToString(int color) { @@ -1169,5 +1167,6 @@ public class OsMoGroupsActivity extends OsmandExpandableListActivity implements } + } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java index af76b7de08..a66f688023 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java @@ -270,7 +270,7 @@ public class OsMoGroupsStorage { return name; } - public void updateLastLocation(String trackerId, Location location) { + public OsMoDevice updateLastLocation(String trackerId, Location location) { OsMoDevice d = users.get(trackerId); if(d != null) { d.setLastLocation(location); @@ -278,6 +278,7 @@ public class OsMoGroupsStorage { d.setLastOnline(location.getTime()); } } + return d; } } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java index 752e41766e..c10f03392a 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java @@ -11,8 +11,8 @@ import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.SettingsActivity; +import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; import net.osmand.plus.osmo.OsMoService.SessionInfo; -import net.osmand.plus.osmo.OsMoTracker.OsmoTrackerListener; import net.osmand.plus.views.MapInfoLayer; import net.osmand.plus.views.MonitoringInfoControl; import net.osmand.plus.views.MonitoringInfoControl.MonitoringInfoControlServices; @@ -38,10 +38,10 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer private OsMoGroups groups; private BaseMapWidget osmoControl; private OsMoPositionLayer olayer; + private Object followTrackerId; // 2014-05-27 23:11:40 public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - private OsmandMapTileView mv; public OsMoPlugin(final OsmandApplication app) { service = new OsMoService(app); @@ -109,23 +109,14 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer @Override public void mapActivityPause(MapActivity activity) { - mv = null; - tracker.setUITrackerListener(new OsmoTrackerListener() { - - @Override - public void locationChange(String trackerId, Location location) { - if(mv != null) { - mv.refreshMap(); - } - } - }); + groups.setUiListener(null); } @Override public void mapActivityResume(MapActivity activity) { - mv = activity.getMapView(); - tracker.setUITrackerListener(null); - + if(olayer != null) { + groups.setUiListener(olayer); + } } /** @@ -249,6 +240,15 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer }).reg(); } + public Object getFollowTrackerId() { + return followTrackerId; + } + + public void setMapFollowTrackerId(OsMoDevice d) { + if(olayer != null) { + olayer.setFollowTrackerId(d); + } + } @Override public String getId() { @@ -267,4 +267,6 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer return service; } + + } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java index a9ba676e11..d3f60e1bbe 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java @@ -8,18 +8,23 @@ import net.osmand.Location; import net.osmand.access.AccessibleToast; import net.osmand.data.LatLon; import net.osmand.data.RotatedTileBox; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.osmo.OsMoGroups.OsMoGroupsUIListener; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; +import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoGroup; import net.osmand.plus.views.ContextMenuLayer; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.util.Algorithms; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.PointF; +import android.os.Handler; import android.util.DisplayMetrics; import android.view.WindowManager; import android.widget.Toast; @@ -28,7 +33,7 @@ import android.widget.Toast; * Class represents a layer for osmo positions * */ -public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider { +public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider, OsMoGroupsUIListener { private DisplayMetrics dm; private final MapActivity map; @@ -37,6 +42,7 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye private Paint point; private OsMoPlugin plugin; private final static float startZoom = 7; + private Handler uiHandler; public OsMoPositionLayer(MapActivity map, OsMoPlugin plugin) { this.map = map; @@ -46,6 +52,7 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye @Override public void initLayer(OsmandMapTileView view) { this.view = view; + uiHandler = new Handler(); dm = new DisplayMetrics(); WindowManager wmgr = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE); wmgr.getDefaultDisplay().getMetrics(dm); @@ -140,14 +147,22 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye @Override public String getObjectDescription(Object o) { if (o instanceof OsMoDevice) { - return map.getString(R.string.osmo_user_name) + " " + ((OsMoDevice) o).getVisibleName(); + String d = map.getString(R.string.osmo_user_name) + " " + ((OsMoDevice) o).getVisibleName(); + final Location l = ((OsMoDevice) o).getLastLocation(); + if(l != null && l.hasSpeed()) { + d += "\n"+ OsmAndFormatter.getFormattedSpeed(l.getSpeed(), map.getMyApplication()); + } + return d; } return null; } @Override public String getObjectName(Object o) { - return ((OsMoDevice) o).getVisibleName(); + if(o instanceof OsMoDevice) { + return map.getString(R.string.osmo_user_name) + " " + ((OsMoDevice) o).getVisibleName(); + } + return null; } public void refresh() { @@ -180,6 +195,70 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye } } } + + @Override + public void groupsListChange(String operation, OsMoGroup group) { + } + + private volatile boolean schedule = false; + // store between rotations + private static String followTrackerId; + private static LatLon followMapLocation; + + public void setFollowTrackerId(OsMoDevice d) { + if(d != null) { + followTrackerId = d.trackerId; + Location l = d.getLastLocation(); + if(l != null) { + followMapLocation = new LatLon(l.getLatitude(), l.getLongitude()); + } + } else { + followTrackerId = null; + } + + } + + @Override + public void deviceLocationChanged(final OsMoDevice device) { + boolean sameId = Algorithms.objectEquals(followTrackerId, device.trackerId); + Location l = device.getLastLocation(); + if(sameId && !schedule && l != null) { + schedule = true; + ContextMenuLayer cl = map.getMapLayers().getContextMenuLayer(); + final boolean sameObject = Algorithms.objectEquals(device, cl.getFirstSelectedObject()); + LatLon mapLoc = new LatLon(map.getMapView().getLatitude(), map.getMapView().getLongitude()); + final boolean centered = Algorithms.objectEquals(followMapLocation, mapLoc); + if(sameObject || centered) { + if(centered ) { + followMapLocation = new LatLon(l.getLatitude(), l.getLongitude()); + } else if(!map.getMapView().getAnimatedDraggingThread().isAnimating()) { + // disable tracking + followMapLocation = null; + } + uiHandler.postDelayed(new Runnable() { + + @Override + public void run() { + schedule = false; + if (sameObject) { + ContextMenuLayer cl = map.getMapLayers().getContextMenuLayer(); + Location l = device.getLastLocation(); + cl.setLocation(new LatLon(l.getLatitude(), l.getLongitude()), getObjectDescription(device)); + cl.setSelectedObject(device); + } + if (centered) { + map.getMapView().setLatLon(followMapLocation.getLatitude(), + followMapLocation.getLongitude()); + } + map.getMapView().refreshMap(); + } + + }, 150); + } else { + followTrackerId = null; + } + } + } } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java index 9040a54c29..be16437712 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java @@ -16,7 +16,6 @@ public class OsMoTracker implements OsMoReactor { private boolean startSendingLocations; private OsMoService service; private int locationsSent = 0; - private OsmoTrackerListener uiTrackerListener = null; private OsmoTrackerListener trackerListener = null; private Location lastSendLocation; private Location lastBufferLocation; @@ -29,6 +28,7 @@ public class OsMoTracker implements OsMoReactor { public void locationChange(String trackerId, Location location); } + public OsMoTracker(OsMoService service, OsmandPreference interval, OsmandPreference autoStart) { @@ -112,8 +112,10 @@ public class OsMoTracker implements OsMoReactor { if (location.getTime() - ltime > pref.get()) { if(lastBufferLocation != null && (!lastBufferLocation.hasSpeed() || lastBufferLocation.getSpeed() < 1) && lastBufferLocation.distanceTo(location) < 20){ - // ignores - return; + if(lastBufferLocation != null && location.getTime() - ltime < 60000) { + // ignores + return; + } } lastBufferLocation = location; bufferOfLocations.add(location); @@ -187,9 +189,6 @@ public class OsMoTracker implements OsMoReactor { if(trackerListener != null) { trackerListener.locationChange(tid, loc); } - if(uiTrackerListener != null){ - uiTrackerListener.locationChange(tid, loc); - } } return true; } @@ -204,13 +203,6 @@ public class OsMoTracker implements OsMoReactor { return trackerListener; } - public OsmoTrackerListener getUITrackerListener() { - return uiTrackerListener; - } - - public void setUITrackerListener(OsmoTrackerListener trackerListener) { - this.uiTrackerListener = trackerListener; - } public Collection getTrackingDevices() { return trackingDevices.values(); diff --git a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java index 7ba9f792e1..553a0e6157 100644 --- a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java @@ -6,6 +6,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; import net.osmand.data.LatLon; import net.osmand.data.RotatedTileBox; @@ -46,7 +47,7 @@ public class ContextMenuLayer extends OsmandMapLayer { private static final String KEY_SELECTED_OBJECTS = "context_menu_selected_objects"; private LatLon latLon; private String description; - private Map selectedObjects = new LinkedHashMap(); + private Map selectedObjects = new ConcurrentHashMap(); private TextView textView; private ImageView closeButton; @@ -74,6 +75,13 @@ public class ContextMenuLayer extends OsmandMapLayer { @Override public void destroyLayer() { } + + public Object getFirstSelectedObject() { + if(!selectedObjects.isEmpty()) { + return selectedObjects.keySet().iterator().next(); + } + return null; + } @Override public void initLayer(OsmandMapTileView view) {