diff --git a/res/layout/map_bubble_layout.xml b/res/layout/map_bubble_layout.xml new file mode 100644 index 000000000..327b25eda --- /dev/null +++ b/res/layout/map_bubble_layout.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/map/GenerateInfoWindowCallback.java b/src/org/thoughtcrime/securesms/map/GenerateInfoWindowCallback.java new file mode 100644 index 000000000..21d38ee95 --- /dev/null +++ b/src/org/thoughtcrime/securesms/map/GenerateInfoWindowCallback.java @@ -0,0 +1,24 @@ +package org.thoughtcrime.securesms.map; + +import android.content.Context; +import android.graphics.Bitmap; + +import java.util.HashMap; + +/** + * Created by cyberta on 13.03.19. + */ + +public interface GenerateInfoWindowCallback { + Context getContext(); + + /** + * Invoked when the marker resources of contact with contactId should be updated + * @param contactId + */ + void refreshSource(int contactId); + /** + * Invoked when the bitmaps have been generated from a view. + */ + void setInfoWindowResults(HashMap results); +} diff --git a/src/org/thoughtcrime/securesms/map/GenerateInfoWindowTask.java b/src/org/thoughtcrime/securesms/map/GenerateInfoWindowTask.java new file mode 100644 index 000000000..34a650abb --- /dev/null +++ b/src/org/thoughtcrime/securesms/map/GenerateInfoWindowTask.java @@ -0,0 +1,102 @@ +package org.thoughtcrime.securesms.map; + +/** + * Created by cyberta on 13.03.19. + */ + +import android.graphics.Bitmap; +import android.os.AsyncTask; +import android.util.Log; +import android.view.LayoutInflater; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.mapbox.geojson.Feature; + +import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.util.BitmapUtil; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashMap; + +import static org.thoughtcrime.securesms.map.MapDataManager.INFO_WINDOW_ID; +import static org.thoughtcrime.securesms.map.MapDataManager.TIMESTAMP; + +/** + * AsyncTask to generate Bitmap from Views to be used as iconImage in a SymbolLayer. + *

+ * Call be optionally be called to update the underlying data source after execution. + *

+ *

+ * Generating Views on background thread since we are not going to be adding them to the view hierarchy. + *

+ */ +public class GenerateInfoWindowTask extends AsyncTask, HashMap, HashMap> { + + private static final String TAG = GenerateInfoWindowCallback.class.getName(); + private final WeakReference callbackRef; + private final int contactId; + + GenerateInfoWindowTask(GenerateInfoWindowCallback callback, int contactId) { + this.callbackRef = new WeakReference<>(callback); + this.contactId = contactId; + } + + @SuppressWarnings("WrongThread") + @Override + protected HashMap doInBackground(ArrayList... params) { + Log.d(TAG, "GenerateInfoWindowTask start"); + Thread.currentThread().setName(GenerateInfoWindowTask.class.getName()); + HashMap imagesMap = new HashMap<>(); + + try { + LayoutInflater inflater = LayoutInflater.from(callbackRef.get().getContext()); + + ArrayList featureList = params[0]; + for (int i = 0; i < featureList.size(); i++) { + Feature feature = featureList.get(i); + + Log.d(TAG, "GenerateInfoWindowTask: feature " + i+ ": " + feature.id()); + + LinearLayout bubbleLayout = (LinearLayout) + inflater.inflate(R.layout.map_bubble_layout, null); + TextView titleTextView = bubbleLayout.findViewById(R.id.conversation_item_body); + titleTextView.setText("TIMESTAMP: " + feature.getNumberProperty(TIMESTAMP)); + bubbleLayout.setBackgroundResource(R.drawable.message_bubble_background_received_alone); + + Bitmap bitmap = BitmapUtil.generate(bubbleLayout); + + String id = feature.getStringProperty(INFO_WINDOW_ID); + imagesMap.put(id, bitmap); + + if (i % 10 == 0) { + publishProgress(imagesMap); + imagesMap.clear(); + } + } + } catch (NullPointerException npe) { + npe.printStackTrace(); + Log.e(TAG, "Callback was GC'ed before task finished."); + } + + Log.d(TAG, "GenerateInfoWindowTask finished"); + return imagesMap; + } + + @Override + protected void onProgressUpdate(HashMap... imagesMap) { + callbackRef.get().setInfoWindowResults(imagesMap[0]); + callbackRef.get().refreshSource(contactId); + Log.d(TAG, "updating progress for contact: " + contactId); + } + + @Override + protected void onPostExecute(HashMap bitmapHashMap) { + GenerateInfoWindowCallback callback = callbackRef.get(); + if (callback != null && bitmapHashMap != null && bitmapHashMap.size() > 0) { + callback.setInfoWindowResults(bitmapHashMap); + callback.refreshSource(contactId); + } + } +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/map/MapDataManager.java b/src/org/thoughtcrime/securesms/map/MapDataManager.java index 5916b95f4..537d2d135 100644 --- a/src/org/thoughtcrime/securesms/map/MapDataManager.java +++ b/src/org/thoughtcrime/securesms/map/MapDataManager.java @@ -37,22 +37,27 @@ 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.eq; 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.Property.ICON_ANCHOR_BOTTOM_LEFT; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; 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.iconOffset; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; /** * Created by cyberta on 07.03.19. */ -public class MapDataManager implements DcEventCenter.DcEventDelegate { +public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateInfoWindowCallback { public static final String MARKER_SELECTED = "MARKER_SELECTED"; public static final String CONTACT_ID = "CONTACT_ID"; + public static final String INFO_WINDOW_ID = "INFO_WINDOW_ID"; public static final String TIMESTAMP = "TIMESTAMP"; public static final String MESSAGE_ID = "MESSAGE_ID"; public static final String ACCURACY = "ACCURACY"; @@ -74,6 +79,7 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { for (int contactId : contactIds) { addContactMapSource(contactId); updateSource(contactId); + generateInfoWindows(contactId); } } @@ -127,14 +133,33 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { private void updateSources() { for (Integer contactId : contactMapSources.keySet()) { updateSource(contactId); + generateMissingInfoWindows(contactId); } } - private void refreshSource(int contactId) { + @Override + public Context getContext() { + return context; + } + + /** + * Invoked when the bitmaps have been generated from a view. + */ + @Override + public void setInfoWindowResults(HashMap results) { + Log.d(TAG, "setInfoWindowResults start"); + mapboxStyle.addImages(results); + Log.d(TAG, "setInfoWindowResults finished"); + } + + @Override + public void refreshSource(int contactId) { + Log.d(TAG, "refreshSource start"); MapSource source = contactMapSources.get(contactId); ArrayList collection = featureCollections.get(source.getMarkerFeatureCollection()); GeoJsonSource pointSource = (GeoJsonSource) mapboxStyle.getSource(source.getMarkerSource()); pointSource.setGeoJson(FeatureCollection.fromFeatures(collection)); + Log.d(TAG, "refreshSource finished"); } private void updateSource(int contactId) { @@ -146,14 +171,17 @@ 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, new JsonObject(), contactId+"_"+i); + String id = contactId+"_"+i; + Feature pointFeature = Feature.fromGeometry(p, new JsonObject(), id); pointFeature.addBooleanProperty(MARKER_SELECTED, false); pointFeature.addNumberProperty(CONTACT_ID, contactId); + pointFeature.addStringProperty(INFO_WINDOW_ID, id); pointFeature.addNumberProperty(TIMESTAMP, locations.getTimestamp(i)); pointFeature.addNumberProperty(MESSAGE_ID, locations.getMsgId(i)); pointFeature.addNumberProperty(ACCURACY, locations.getAccuracy(i)); @@ -174,6 +202,32 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { featureCollections.put(contactMapMetadata.getMarkerFeatureCollection(), pointFeatureList); } + private void generateMissingInfoWindows(int contactId) { + MapSource contactMapMetadata = contactMapSources.get(contactId); + ArrayList featureList = featureCollections.get(contactMapMetadata.getMarkerFeatureCollection()); + ArrayList missingWindows = new ArrayList<>(); + + for (Feature f : featureList) { + String infoWindowId = f.getStringProperty(INFO_WINDOW_ID); + if (mapboxStyle.getImage(infoWindowId) == null) { + Log.d(TAG, "create new infoWindow for " + infoWindowId); + missingWindows.add(f); + } else { + // the list is ordered and thus, all older features should already have an info window + break; + } + } + + new GenerateInfoWindowTask(this, contactId).execute(missingWindows); + } + + + private void generateInfoWindows(int contactId) { + MapSource contactMapMetadata = contactMapSources.get(contactId); + ArrayList featureList = featureCollections.get(contactMapMetadata.getMarkerFeatureCollection()); + new GenerateInfoWindowTask(this, contactId).execute(featureList); + } + private void initGeoJsonSources(MapSource source) { GeoJsonSource lastPositionSource = new GeoJsonSource(source.getLastPositionSource()); GeoJsonSource markerPositionSource = new GeoJsonSource(source.getMarkerSource()); @@ -196,7 +250,6 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { mapboxStyle.addImage(source.getMarkerIcon(), 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( @@ -209,6 +262,17 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { iconAnchor(ICON_ANCHOR_BOTTOM), iconSize(markerSizeExpression)) ); + Expression filterInfoWindowExpression = eq((get(MARKER_SELECTED)), literal(true)); + mapboxStyle.addLayer(new SymbolLayer(source.getInfoWindowLayer(), source.getMarkerSource()). + withProperties( + iconImage("{"+INFO_WINDOW_ID+"}"), + iconAnchor(ICON_ANCHOR_BOTTOM_LEFT), + /* all info window and marker image to appear at the same time*/ + iconAllowOverlap(true), + /* offset the info window to be above the marker */ + iconOffset(new Float[] {-2f, -15f}) + ).withFilter(filterInfoWindowExpression)); + mapboxStyle.addLayer(new LineLayer(source.getLineLayer(), source.getLineSource()) .withProperties(PropertyFactory.lineCap(Property.LINE_CAP_ROUND), PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND), @@ -221,6 +285,7 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate { int contactId = (Integer) data1; if (contactMapSources.containsKey(contactId)) { updateSource(contactId); + generateMissingInfoWindows(contactId); } } diff --git a/src/org/thoughtcrime/securesms/map/MapSource.java b/src/org/thoughtcrime/securesms/map/MapSource.java index b1955d51c..1c5e34ad6 100644 --- a/src/org/thoughtcrime/securesms/map/MapSource.java +++ b/src/org/thoughtcrime/securesms/map/MapSource.java @@ -9,6 +9,7 @@ import android.graphics.Color; public class MapSource { public static final String LINE_LAYER = "line_layer"; public static final String MARKER_LAYER = "symbol_layer"; + public static final String INFO_WINDOW_LAYER = "info_window_layer"; public static final String LAST_POSITON_LAYER = "last_position_layer"; public static final String LINE_SOURCE = "line_source"; public static final String MARKER_POSITION_SOURCE = "marker_position"; @@ -16,6 +17,7 @@ public class MapSource { 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"; + public static final String MARKER_INFO_WINDOW = "marker_info_window"; private final String markerSource; private final String lineSource; @@ -24,11 +26,14 @@ public class MapSource { private final String markerLayer; private final String lineLayer; private final String lastPositionLayer; + private final String infoWindowLayer; private final String markerIcon; private final String markerLastPositon; + private final String markerInfoWindow; private final String markerFeatureCollection; + private int color; private int colorArgb; @@ -39,9 +44,11 @@ public class MapSource { markerLayer = MARKER_LAYER + "_" + chatId; lineLayer = LINE_LAYER + "_" + chatId; lastPositionLayer = LAST_POSITON_LAYER + "_" + chatId; + infoWindowLayer = INFO_WINDOW_LAYER + "_" + chatId; markerIcon = MARKER_ICON + "_" + chatId; markerLastPositon = MARKER_LAST_POSITON + "_" + chatId; markerFeatureCollection = MARKER_FEATURE_LIST + "_" + chatId; + markerInfoWindow = MARKER_INFO_WINDOW + "_" + chatId; } public void setColor(int color) { @@ -81,6 +88,8 @@ public class MapSource { return lastPositionLayer; } + public String getInfoWindowLayer() { return infoWindowLayer; } + public String getMarkerIcon() { return markerIcon; } @@ -94,4 +103,6 @@ public class MapSource { } public String getMarkerFeatureCollection() { return markerFeatureCollection; } + + public String getMarkerInfoWindow() { return markerInfoWindow; } } diff --git a/src/org/thoughtcrime/securesms/util/BitmapUtil.java b/src/org/thoughtcrime/securesms/util/BitmapUtil.java index ebdfc3ba5..112216d81 100644 --- a/src/org/thoughtcrime/securesms/util/BitmapUtil.java +++ b/src/org/thoughtcrime/securesms/util/BitmapUtil.java @@ -5,6 +5,7 @@ import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.ImageFormat; import android.graphics.Matrix; import android.graphics.Rect; @@ -16,6 +17,7 @@ import android.support.annotation.WorkerThread; import android.support.media.ExifInterface; import android.util.Log; import android.util.Pair; +import android.view.View; import com.b44t.messenger.DcMsg; import com.bumptech.glide.load.engine.DiskCacheStrategy; @@ -453,4 +455,25 @@ public class BitmapUtil { } } + + /** + * Generate a Bitmap from an Android SDK View. + * + * @param view the View to be drawn to a Bitmap + * @return the generated bitmap + */ + public static Bitmap generate(@NonNull View view) { + int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + view.measure(measureSpec, measureSpec); + + int measuredWidth = view.getMeasuredWidth(); + int measuredHeight = view.getMeasuredHeight(); + + view.layout(0, 0, measuredWidth, measuredHeight); + Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888); + bitmap.eraseColor(Color.TRANSPARENT); + Canvas canvas = new Canvas(bitmap); + view.draw(canvas); + return bitmap; + } }