initial info window implementation

This commit is contained in:
cyBerta
2019-03-13 23:39:40 +01:00
parent 55973ef857
commit a553b33783
6 changed files with 347 additions and 4 deletions
+118
View File
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/body_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="@dimen/message_bubble_edge_margin"
android:layout_marginStart="@dimen/message_bubble_edge_margin"
android:clipToPadding="false"
android:clipChildren="false"
android:background="@color/white"
tools:backgroundTint="@color/core_light_10">
<LinearLayout
android:id="@+id/group_sender_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_bubble_top_padding"
android:layout_marginBottom="2dp"
android:layout_marginLeft="@dimen/message_bubble_horizontal_padding"
android:layout_marginRight="@dimen/message_bubble_horizontal_padding"
android:orientation="horizontal"
android:visibility="gone"
tools:visibility="visible">
<TextView
android:id="@+id/group_message_sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4sp"
android:layout_marginEnd="4sp"
style="@style/Signal.Text.Preview"
android:fontFamily="sans-serif-medium"
android:maxLines="1"
android:ellipsize="end"
tools:visibility="visible"
tools:text="Bob"/>
</LinearLayout>
<org.thoughtcrime.securesms.components.QuoteView
android:id="@+id/quote_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_bubble_top_padding"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:visibility="gone"
app:message_type="outgoing"
app:quote_colorPrimary="?attr/conversation_item_quote_text_color"
app:quote_colorSecondary="?attr/conversation_item_quote_text_color"
tools:visibility="visible"/>
<ViewStub
android:id="@+id/image_view_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/conversation_item_sent_thumbnail" />
<ViewStub
android:id="@+id/audio_view_stub"
android:layout="@layout/conversation_item_sent_audio"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_bubble_top_padding"
android:layout_marginLeft="@dimen/message_bubble_horizontal_padding"
android:layout_marginRight="@dimen/message_bubble_horizontal_padding" />
<ViewStub
android:id="@+id/document_view_stub"
android:layout="@layout/conversation_item_sent_document"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/message_bubble_top_padding"
android:layout_marginBottom="@dimen/message_bubble_collapsed_footer_padding"
android:layout_marginLeft="@dimen/message_bubble_horizontal_padding"
android:layout_marginRight="@dimen/message_bubble_horizontal_padding" />
<org.thoughtcrime.securesms.components.emoji.EmojiTextView
android:id="@+id/conversation_item_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/message_bubble_horizontal_padding"
android:layout_marginRight="@dimen/message_bubble_horizontal_padding"
android:layout_marginBottom="@dimen/message_bubble_collapsed_footer_padding"
style="@style/Signal.Text.Body"
android:paddingTop="@dimen/message_bubble_top_padding"
android:textColor="?conversation_item_outgoing_text_primary_color"
android:textColorLink="?conversation_item_outgoing_text_primary_color"
app:scaleEmojis="true"
tools:text="Mango pickle lorem ipsum"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<org.thoughtcrime.securesms.components.ConversationItemFooter
android:id="@+id/conversation_item_footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-4dp"
android:layout_marginLeft="@dimen/message_bubble_horizontal_padding"
android:layout_marginRight="@dimen/message_bubble_horizontal_padding"
android:layout_marginBottom="@dimen/message_bubble_bottom_padding"
android:layout_gravity="end"
android:gravity="end"
android:clipChildren="false"
android:clipToPadding="false"
app:footer_text_color="?attr/conversation_item_outgoing_text_secondary_color"/>
</LinearLayout>
@@ -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<String, Bitmap> results);
}
@@ -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.
* <p>
* Call be optionally be called to update the underlying data source after execution.
* </p>
* <p>
* Generating Views on background thread since we are not going to be adding them to the view hierarchy.
* </p>
*/
public class GenerateInfoWindowTask extends AsyncTask<ArrayList<Feature>, HashMap<String, Bitmap>, HashMap<String, Bitmap>> {
private static final String TAG = GenerateInfoWindowCallback.class.getName();
private final WeakReference<GenerateInfoWindowCallback> callbackRef;
private final int contactId;
GenerateInfoWindowTask(GenerateInfoWindowCallback callback, int contactId) {
this.callbackRef = new WeakReference<>(callback);
this.contactId = contactId;
}
@SuppressWarnings("WrongThread")
@Override
protected HashMap<String, Bitmap> doInBackground(ArrayList<Feature>... params) {
Log.d(TAG, "GenerateInfoWindowTask start");
Thread.currentThread().setName(GenerateInfoWindowTask.class.getName());
HashMap<String, Bitmap> imagesMap = new HashMap<>();
try {
LayoutInflater inflater = LayoutInflater.from(callbackRef.get().getContext());
ArrayList<Feature> 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<String, Bitmap>... imagesMap) {
callbackRef.get().setInfoWindowResults(imagesMap[0]);
callbackRef.get().refreshSource(contactId);
Log.d(TAG, "updating progress for contact: " + contactId);
}
@Override
protected void onPostExecute(HashMap<String, Bitmap> bitmapHashMap) {
GenerateInfoWindowCallback callback = callbackRef.get();
if (callback != null && bitmapHashMap != null && bitmapHashMap.size() > 0) {
callback.setInfoWindowResults(bitmapHashMap);
callback.refreshSource(contactId);
}
}
}
@@ -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<String, Bitmap> 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<Feature> 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<Point> coordinateList = new ArrayList<>();
ArrayList<Feature> 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<Feature> featureList = featureCollections.get(contactMapMetadata.getMarkerFeatureCollection());
ArrayList<Feature> 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<Feature> 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);
}
}
@@ -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; }
}
@@ -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;
}
}