From 63366eecf38e36d107eaa8b9a2fb62e8cb9e35f4 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 12 Mar 2019 16:22:42 +0100 Subject: [PATCH] add logic to select markers, prerequisite for showing an info overlay --- res/drawable/ic_location_dot.xml | 6 +- .../securesms/map/MapActivity.java | 27 ++++ .../securesms/map/MapDataManager.java | 124 ++++++++++++++++-- .../thoughtcrime/securesms/map/MapSource.java | 5 + 4 files changed, 151 insertions(+), 11 deletions(-) diff --git a/res/drawable/ic_location_dot.xml b/res/drawable/ic_location_dot.xml index 15c532a57..7426bb67b 100644 --- a/res/drawable/ic_location_dot.xml +++ b/res/drawable/ic_location_dot.xml @@ -1,5 +1,9 @@ - + + + + + \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/map/MapActivity.java b/src/org/thoughtcrime/securesms/map/MapActivity.java index f85fe17c7..1ae8dccdd 100644 --- a/src/org/thoughtcrime/securesms/map/MapActivity.java +++ b/src/org/thoughtcrime/securesms/map/MapActivity.java @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.map; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.graphics.PointF; import android.os.Bundle; import android.support.annotation.NonNull; import android.util.Log; @@ -26,9 +27,12 @@ import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import org.thoughtcrime.securesms.BaseActivity; import org.thoughtcrime.securesms.R; +import java.util.List; import java.util.Observable; import java.util.Observer; +import static org.thoughtcrime.securesms.map.MapDataManager.MARKER_SELECTED; + public class MapActivity extends BaseActivity implements Observer { public static final String TAG = MapActivity.class.getSimpleName(); @@ -66,6 +70,29 @@ public class MapActivity extends BaseActivity implements Observer { } mapDataManager = new MapDataManager(this, mapBoxStyle, chatId); + mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() { + @Override + public boolean onMapClick(@NonNull LatLng point) { + final PointF pixel = mapboxMap.getProjection().toScreenLocation(point); + Log.d(TAG, "on item clicked."); + + List features = mapboxMap.queryRenderedFeatures(pixel, mapDataManager.getMarkerLayers()); + for (Feature feature : features) { + Log.d(TAG, "found feature: " + feature.toJson()); + if (feature.hasProperty(MapDataManager.TIMESTAMP)){ + //show first feature that has meta data infos + if (feature.hasProperty(MARKER_SELECTED)) { + mapDataManager.setMarkerSelected(feature.id()); + } + Log.d(TAG, "on item clicked. timestamp : " + feature.getNumberProperty(MapDataManager.TIMESTAMP)); + + return true; + } + } + mapDataManager.unselectMarker(); + return false; + } + }); })); dcLocation = DcLocation.getInstance(); diff --git a/src/org/thoughtcrime/securesms/map/MapDataManager.java b/src/org/thoughtcrime/securesms/map/MapDataManager.java index 940ef552f..5916b95f4 100644 --- a/src/org/thoughtcrime/securesms/map/MapDataManager.java +++ b/src/org/thoughtcrime/securesms/map/MapDataManager.java @@ -16,11 +16,13 @@ import android.util.Log; import com.b44t.messenger.DcArray; import com.b44t.messenger.DcContact; import com.b44t.messenger.DcEventCenter; +import com.google.gson.JsonObject; import com.mapbox.geojson.Feature; import com.mapbox.geojson.FeatureCollection; import com.mapbox.geojson.LineString; import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.maps.Style; +import com.mapbox.mapboxsdk.style.expressions.Expression; import com.mapbox.mapboxsdk.style.layers.LineLayer; import com.mapbox.mapboxsdk.style.layers.Property; import com.mapbox.mapboxsdk.style.layers.PropertyFactory; @@ -32,21 +34,33 @@ import org.thoughtcrime.securesms.R; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; import static com.b44t.messenger.DcContext.DC_EVENT_LOCATION_CHANGED; +import static com.mapbox.mapboxsdk.style.expressions.Expression.get; +import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; +import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase; +import static com.mapbox.mapboxsdk.style.expressions.Expression.toBool; import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_BOTTOM; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; /** * Created by cyberta on 07.03.19. */ public class MapDataManager implements DcEventCenter.DcEventDelegate { - private static final String TIMESTAMP = "TIMESTAMP"; - private static final String MESSAGE_ID = "MESSAGE_ID"; - private static final String ACCURACY = "ACCURACY"; + public static final String MARKER_SELECTED = "MARKER_SELECTED"; + public static final String CONTACT_ID = "CONTACT_ID"; + public static final String TIMESTAMP = "TIMESTAMP"; + public static final String MESSAGE_ID = "MESSAGE_ID"; + public static final String ACCURACY = "ACCURACY"; private static final String TAG = MapDataManager.class.getSimpleName(); private Style mapboxStyle; private HashMap contactMapSources; + private HashMap> featureCollections; + private Feature selectedFeature; private int chatId; private Context context; @@ -55,6 +69,7 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { this.context = context; this.chatId = chatId; contactMapSources = new HashMap<>(); + featureCollections = new HashMap<>(); int[] contactIds = ApplicationContext.getInstance(context).dcContext.getChatContacts(chatId); for (int contactId : contactIds) { addContactMapSource(contactId); @@ -115,6 +130,13 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { } } + private void refreshSource(int contactId) { + MapSource source = contactMapSources.get(contactId); + ArrayList collection = featureCollections.get(source.getMarkerFeatureCollection()); + GeoJsonSource pointSource = (GeoJsonSource) mapboxStyle.getSource(source.getMarkerSource()); + pointSource.setGeoJson(FeatureCollection.fromFeatures(collection)); + } + private void updateSource(int contactId) { DcArray locations = ApplicationContext.getInstance(context).dcContext.getLocations(chatId, contactId); int count = locations.getCnt(); @@ -124,15 +146,18 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { ArrayList coordinateList = new ArrayList<>(); ArrayList pointFeatureList = new ArrayList<>(); + MapSource contactMapMetadata = contactMapSources.get(contactId); for (int i = 0; i < count; i++) { Point p = Point.fromLngLat(locations.getLongitude(i), locations.getLatitude(i)); coordinateList.add(p); - Feature pointFeature = Feature.fromGeometry(p); + Feature pointFeature = Feature.fromGeometry(p, new JsonObject(), contactId+"_"+i); + pointFeature.addBooleanProperty(MARKER_SELECTED, false); + pointFeature.addNumberProperty(CONTACT_ID, contactId); pointFeature.addNumberProperty(TIMESTAMP, locations.getTimestamp(i)); pointFeature.addNumberProperty(MESSAGE_ID, locations.getMsgId(i)); pointFeature.addNumberProperty(ACCURACY, locations.getAccuracy(i)); - pointFeatureList.add(Feature.fromGeometry(p)); + pointFeatureList.add(pointFeature); } FeatureCollection pointFeatureCollection = FeatureCollection.fromFeatures(pointFeatureList); @@ -140,13 +165,13 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { LineString.fromLngLats(coordinateList) )}); - MapSource contactMapMetadata = contactMapSources.get(contactId); GeoJsonSource lineSource = (GeoJsonSource) mapboxStyle.getSource(contactMapMetadata.getLineSource()); lineSource.setGeoJson(lineFeatureCollection); GeoJsonSource pointSource = (GeoJsonSource) mapboxStyle.getSource(contactMapMetadata.getMarkerSource()); pointSource.setGeoJson(pointFeatureCollection); GeoJsonSource lastPostionSource = (GeoJsonSource) mapboxStyle.getSource(contactMapMetadata.getLastPositionSource()); lastPostionSource.setGeoJson(pointFeatureList.get(0)); + featureCollections.put(contactMapMetadata.getMarkerFeatureCollection(), pointFeatureList); } private void initGeoJsonSources(MapSource source) { @@ -172,11 +197,18 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { generateColoredLocationIcon(source.getColorArgb())); + Expression markerSizeExpression = switchCase(toBool(get(MARKER_SELECTED)), literal(1.5f), literal(1.0f)); mapboxStyle.addLayer(new SymbolLayer(source.getMarkerLayer(), source.getMarkerSource()) - .withProperties(PropertyFactory.iconImage(source.getMarkerIcon()))); + .withProperties( + iconImage(source.getMarkerIcon()), + iconSize(markerSizeExpression)) + ); mapboxStyle.addLayer(new SymbolLayer(source.getLastPositionLayer(), source.getLastPositionSource()) - .withProperties(PropertyFactory.iconImage(source.getMarkerLastPositon()), - PropertyFactory.iconAnchor(ICON_ANCHOR_BOTTOM))); + .withProperties( + iconImage(source.getMarkerLastPositon()), + iconAnchor(ICON_ANCHOR_BOTTOM), + iconSize(markerSizeExpression)) + ); mapboxStyle.addLayer(new LineLayer(source.getLineLayer(), source.getLineSource()) .withProperties(PropertyFactory.lineCap(Property.LINE_CAP_ROUND), PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND), @@ -184,7 +216,6 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { PropertyFactory.lineColor(source.getColorArgb()))); } - @Override public void handleEvent(int eventId, Object data1, Object data2) { int contactId = (Integer) data1; @@ -198,4 +229,77 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { return false; } + + public String[] getMarkerLayers() { + String markerLayers[] = new String[contactMapSources.size()*2]; + int i = 0; + for (Map.Entry entry : contactMapSources.entrySet()) { + markerLayers[i] = entry.getValue().getMarkerLayer(); + markerLayers[i+1] = entry.getValue().getLastPositionLayer(); + i += 2; + } + + return markerLayers; + } + + public void unselectMarker() { + if (selectedFeature != null) { + selectedFeature.addBooleanProperty(MARKER_SELECTED, false); + refreshSource(selectedFeature.getNumberProperty(CONTACT_ID).intValue()); + selectedFeature = null; + } + } + + public void setMarkerSelected(String featureId) { + if (selectedFeature == null) { + setNewMarkerSelected(featureId); + } else if (selectedFeature.id().equals(featureId)) { + updateSelectedMarker(); + } else { + replaceSelectedMarker(featureId); + } + } + + private void replaceSelectedMarker(String featureId) { + Feature feature = getFeatureWithId(featureId); + feature.addBooleanProperty(MARKER_SELECTED, true); + selectedFeature.addBooleanProperty(MARKER_SELECTED, false); + + int lastContactId = selectedFeature.getNumberProperty(CONTACT_ID).intValue(); + int currentContactId = feature.getNumberProperty(CONTACT_ID).intValue(); + + selectedFeature = feature; + refreshSource(currentContactId); + if (lastContactId != currentContactId) { + refreshSource(lastContactId); + } + + } + + private void updateSelectedMarker() { + boolean isSelected = selectedFeature.getBooleanProperty(MARKER_SELECTED); + selectedFeature.addBooleanProperty(MARKER_SELECTED, !isSelected); + refreshSource(selectedFeature.getNumberProperty(CONTACT_ID).intValue()); + } + + private void setNewMarkerSelected(String featureId) { + Feature feature = getFeatureWithId(featureId); + feature.addBooleanProperty(MARKER_SELECTED, true); + selectedFeature = feature; + refreshSource(selectedFeature.getNumberProperty(CONTACT_ID).intValue()); + } + + + private Feature getFeatureWithId(String id) { + for (String key : featureCollections.keySet()) { + ArrayList featureCollection = featureCollections.get(key); + for (Feature f : featureCollection) { + if (f.id().equals(id)) { + return f; + } + } + } + return null; + } + } diff --git a/src/org/thoughtcrime/securesms/map/MapSource.java b/src/org/thoughtcrime/securesms/map/MapSource.java index e4cb40219..b1955d51c 100644 --- a/src/org/thoughtcrime/securesms/map/MapSource.java +++ b/src/org/thoughtcrime/securesms/map/MapSource.java @@ -15,6 +15,7 @@ public class MapSource { public static final String LAST_POSITION_SOURCE = "marker_last_position"; public static final String MARKER_ICON = "marker_icon_id"; public static final String MARKER_LAST_POSITON = "marker_last_position"; + public static final String MARKER_FEATURE_LIST = "marker_feature_list"; private final String markerSource; private final String lineSource; @@ -26,6 +27,7 @@ public class MapSource { private final String markerIcon; private final String markerLastPositon; + private final String markerFeatureCollection; private int color; private int colorArgb; @@ -39,6 +41,7 @@ public class MapSource { lastPositionLayer = LAST_POSITON_LAYER + "_" + chatId; markerIcon = MARKER_ICON + "_" + chatId; markerLastPositon = MARKER_LAST_POSITON + "_" + chatId; + markerFeatureCollection = MARKER_FEATURE_LIST + "_" + chatId; } public void setColor(int color) { @@ -89,4 +92,6 @@ public class MapSource { public String getLastPositionSource() { return lastPositionSource; } + + public String getMarkerFeatureCollection() { return markerFeatureCollection; } }