create UI to add POI markers

This commit is contained in:
cyBerta
2019-04-25 18:49:32 +02:00
parent ea0d3bed37
commit e55a230220
11 changed files with 669 additions and 97 deletions
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/touch_highlight" />
</shape>
</item>
<item android:state_enabled="true">
<shape android:shape="oval">
<solid android:color="@color/gray70" />
</shape>
</item>
</selector>
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="35"
android:pivotX="0%"
android:pivotY="0%"
android:toDegrees="-10">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<solid android:color="#FFFFFFFF" />
</shape>
</rotate>
</item>
<item android:top="8dp"
>
<shape android:shape="rectangle">
<solid android:color="#FFFFFFFF" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
+55
View File
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="70dp"
android:orientation="vertical"
android:background="@drawable/map_bubble_add_poi"
android:paddingTop="8dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_marginRight="4dp"
android:layout_marginEnd="4dp"
android:text="@string/add_poi"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/message_view"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:maxLines="1"
android:maxLength="16"
android:textAlignment="center"
android:layout_gravity="center"
/>
<ImageButton
android:id="@+id/sendView"
android:layout_gravity="center_vertical"
android:layout_width="35dp"
android:layout_height="35dp"
android:padding="4dp"
android:paddingLeft="8dp"
android:paddingStart="8dp"
android:src="@drawable/ic_send_sms_white_24dp"
android:background="@drawable/circle_touch_highlight_background_map"
android:contentDescription="@string/menu_send"
android:scaleType="fitCenter"
/>
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>
+1
View File
@@ -222,6 +222,7 @@
<!-- map -->
<string name="filter_map_on_time">Show locations in time frame</string>
<string name="show_location_traces">Show traces</string>
<string name="add_poi">Add point of interest</string>
<!-- search -->
@@ -0,0 +1,55 @@
package org.thoughtcrime.securesms.map;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.b44t.messenger.DcContext;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
import static org.thoughtcrime.securesms.util.BitmapUtil.generateColoredBitmap;
/**
* Created by cyberta on 24.04.19.
*/
public class AddPoiView extends LinearLayoutCompat {
private ImageButton sendView;
private LatLng latLng;
public AddPoiView(Context context) {
this(context, null);
}
public AddPoiView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AddPoiView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflate(context, R.layout.add_poi_view, this);
sendView = this.findViewById(R.id.sendView);
sendView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//TODO: submit change
// DcHelper.getContext(AddPoiView.this.getContext()).
}
});
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
}
@@ -0,0 +1,221 @@
package org.thoughtcrime.securesms.map;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.maps.MapFragment;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import com.mapbox.mapboxsdk.utils.MapFragmentUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Support Fragment wrapper around a map view.
* <p>
* A Map component in an app. This fragment is the simplest way to place a map in an application.
* It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.
* Being a fragment, this component can be added to an activity's layout or can dynamically be added
* using a FragmentManager.
* </p>
* <p>
* To get a reference to the MapView, use {@link #getMapAsync(OnMapReadyCallback)}}
* </p>
*
* @see #getMapAsync(OnMapReadyCallback)
*/
public class DCMapFragment extends Fragment implements OnMapReadyCallback {
private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>();
private MapFragment.OnMapViewReadyCallback mapViewReadyCallback;
private MapboxMap mapboxMap;
private MapView map;
/**
* Creates a default MapFragment instance
*
* @return MapFragment created
*/
public static DCMapFragment newInstance() {
return new DCMapFragment();
}
/**
* Creates a MapFragment instance
*
* @param mapboxMapOptions The configuration options to be used.
* @return MapFragment created.
*/
@NonNull
public static DCMapFragment newInstance(@Nullable MapboxMapOptions mapboxMapOptions) {
DCMapFragment mapFragment = new DCMapFragment();
mapFragment.setArguments(MapFragmentUtils.createFragmentArgs(mapboxMapOptions));
return mapFragment;
}
/**
* Called when the context attaches to this fragment.
*
* @param context the context attaching
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MapFragment.OnMapViewReadyCallback) {
mapViewReadyCallback = (MapFragment.OnMapViewReadyCallback) context;
}
}
/**
* Called when this fragment is inflated, parses XML tag attributes.
*
* @param context The context inflating this fragment.
* @param attrs The XML tag attributes.
* @param savedInstanceState The saved instance state for the map fragment.
*/
@Override
public void onInflate(@NonNull Context context, AttributeSet attrs, Bundle savedInstanceState) {
super.onInflate(context, attrs, savedInstanceState);
setArguments(MapFragmentUtils.createFragmentArgs(MapboxMapOptions.createFromAttributes(context, attrs)));
}
/**
* Creates the fragment view hierarchy.
*
* @param inflater Inflater used to inflate content.
* @param container The parent layout for the map fragment.
* @param savedInstanceState The saved instance state for the map fragment.
* @return The view created
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Context context = inflater.getContext();
map = new MapView(context, MapFragmentUtils.resolveArgs(context, getArguments()));
return map;
}
/**
* Called when the fragment view hierarchy is created.
*
* @param view The content view of the fragment
* @param savedInstanceState THe saved instance state of the framgnt
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
map.onCreate(savedInstanceState);
map.getMapAsync(this);
// notify listeners about MapView creation
if (mapViewReadyCallback != null) {
mapViewReadyCallback.onMapViewReady(map);
}
}
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
for (OnMapReadyCallback onMapReadyCallback : mapReadyCallbackList) {
onMapReadyCallback.onMapReady(mapboxMap);
}
}
/**
* Called when the fragment is visible for the users.
*/
@Override
public void onStart() {
super.onStart();
map.onStart();
}
/**
* Called when the fragment is ready to be interacted with.
*/
@Override
public void onResume() {
super.onResume();
map.onResume();
}
/**
* Called when the fragment is pausing.
*/
@Override
public void onPause() {
super.onPause();
map.onPause();
}
/**
* Called when the fragment state needs to be saved.
*
* @param outState The saved state
*/
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (map != null && !map.isDestroyed()) {
map.onSaveInstanceState(outState);
}
}
/**
* Called when the fragment is no longer visible for the user.
*/
@Override
public void onStop() {
super.onStop();
map.onStop();
}
/**
* Called when the fragment receives onLowMemory call from the hosting Activity.
*/
@Override
public void onLowMemory() {
super.onLowMemory();
if (map != null && !map.isDestroyed()) {
map.onLowMemory();
}
}
/**
* Called when the fragment is view hiearchy is being destroyed.
*/
@Override
public void onDestroyView() {
super.onDestroyView();
map.onDestroy();
mapReadyCallbackList.clear();
}
/**
* Sets a callback object which will be triggered when the MapboxMap instance is ready to be used.
*
* @param onMapReadyCallback The callback to be invoked.
*/
public void getMapAsync(@NonNull final OnMapReadyCallback onMapReadyCallback) {
if (mapboxMap == null) {
mapReadyCallbackList.add(onMapReadyCallback);
} else {
onMapReadyCallback.onMapReady(mapboxMap);
}
}
public MapView getMapView() {
return map;
}
}
@@ -5,7 +5,6 @@ import android.graphics.PointF;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
@@ -18,11 +17,9 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.BaseActivity;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.ConversationActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.rangeslider.TimeRangeSlider;
@@ -53,7 +50,8 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
private DcLocation dcLocation;
private MapDataManager mapDataManager;
private MapboxMap mapboxMap;
SupportMapFragment mapFragment;
DCMapFragment mapFragment;
MarkerViewManager markerViewManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -71,16 +69,19 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
if (savedInstanceState == null) {
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
mapFragment = SupportMapFragment.newInstance();
mapFragment = DCMapFragment.newInstance();
transaction.add(R.id.container, mapFragment, MAP_TAG);
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag(MAP_TAG);
mapFragment = (DCMapFragment) getSupportFragmentManager().findFragmentByTag(MAP_TAG);
}
mapFragment.getMapAsync(mapboxMap -> mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
this.mapboxMap = mapboxMap;
this.markerViewManager = new MarkerViewManager(mapFragment.getMapView(), mapboxMap);
final LatLng lastMapCenter = Prefs.getMapCenter(this.getApplicationContext(), chatId);
if (lastMapCenter != null) {
double lastZoom = Prefs.getMapZoom(this.getApplicationContext(), chatId);
@@ -115,60 +116,12 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 1000);
}
mapboxMap.addOnMapClickListener(point -> {
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
Log.d(TAG, "on item clicked.");
mapboxMap.addOnMapClickListener(point ->
handleMarkerClick(point) ||
handleInfoWindowClick(point) ||
handleAddPoiClick(point));
List<Feature> features = mapboxMap.queryRenderedFeatures(pixel, mapDataManager.getMarkerLayers());
for (Feature feature : features) {
Log.d(TAG, "found feature: " + feature.toJson());
//show first feature that has meta data infos
if (feature.hasProperty(MARKER_SELECTED)) {
mapDataManager.setMarkerSelected(feature.id());
return true;
}
}
mapDataManager.unselectMarker();
return false;
});
mapboxMap.addOnMapClickListener(point -> {
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> features = mapboxMap.queryRenderedFeatures(pixel, INFO_WINDOW_LAYER);
Log.d(TAG, "on info window clicked." + features.size());
for (Feature feature : features) {
Log.d(TAG, "found feature: " + feature.toJson());
int messageId = feature.getNumberProperty(MESSAGE_ID).intValue();
DcMsg dcMsg = ApplicationContext.getInstance(this).dcContext.getMsg(messageId);
int dcMsgChatId = dcMsg.getChatId();
if (dcMsgChatId == DC_CHAT_NO_CHAT) {
continue;
}
int msgs[] = DcHelper.getContext(MapActivity.this).getChatMsgs(dcMsgChatId, 0, 0);
int startingPosition = -1;
for(int i=0; i< msgs.length; i++ ) {
if(msgs[i] == messageId) {
startingPosition = msgs.length-1-i;
break;
}
}
Intent intent = new Intent(MapActivity.this, ConversationActivity.class);
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, dcMsgChatId);
intent.putExtra(ConversationActivity.LAST_SEEN_EXTRA, 0);
intent.putExtra(ConversationActivity.STARTING_POSITION_EXTRA, startingPosition);
startActivity(intent);
return true;
}
return false;
});
if (BuildConfig.DEBUG) {
/*if (BuildConfig.DEBUG) {
mapboxMap.addOnMapLongClickListener(point -> {
new AlertDialog.Builder(MapActivity.this)
.setMessage(getString(R.string.menu_delete_locations))
@@ -179,7 +132,7 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
.show();
return true;
});
}
}*/
SwitchCompat switchCompat = this.findViewById(R.id.locationTraceSwitch);
switchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
@@ -195,7 +148,6 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
View bottomSheet = this.findViewById(R.id.bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
RelativeLayout bottomSheetSlider = this.findViewById(R.id.bottomSheetSlider);
bottomSheetSlider.setOnClickListener(v -> {
switch (behavior.getState()) {
@@ -207,7 +159,6 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
break;
}
});
}
@Override
@@ -266,11 +217,77 @@ public class MapActivity extends BaseActivity implements Observer, TimeRangeSlid
mapDataManager.filterLastPositions(startTimestamp);
}
double getRandomLongitude() {
private double getRandomLongitude() {
double start = -180;
double end = 180;
double random = new Random().nextDouble();
return start + (random * (end - start));
}
private boolean handleMarkerClick(LatLng point) {
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
Log.d(TAG, "on item clicked.");
List<Feature> features = mapboxMap.queryRenderedFeatures(pixel, mapDataManager.getMarkerLayers());
for (Feature feature : features) {
Log.d(TAG, "found feature: " + feature.toJson());
//show first feature that has meta data infos
if (feature.hasProperty(MARKER_SELECTED)) {
mapDataManager.setMarkerSelected(feature.id());
if (markerViewManager.hasMarkers()) {
markerViewManager.removeMarkers();
}
return true;
}
}
return mapDataManager.unselectMarker();
}
private boolean handleInfoWindowClick(LatLng point) {
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> features = mapboxMap.queryRenderedFeatures(pixel, INFO_WINDOW_LAYER);
Log.d(TAG, "on info window clicked." + features.size());
for (Feature feature : features) {
Log.d(TAG, "found feature: " + feature.toJson());
int messageId = feature.getNumberProperty(MESSAGE_ID).intValue();
DcMsg dcMsg = ApplicationContext.getInstance(this).dcContext.getMsg(messageId);
int dcMsgChatId = dcMsg.getChatId();
if (dcMsgChatId == DC_CHAT_NO_CHAT) {
continue;
}
int msgs[] = DcHelper.getContext(MapActivity.this).getChatMsgs(dcMsgChatId, 0, 0);
int startingPosition = -1;
for(int i=0; i< msgs.length; i++ ) {
if(msgs[i] == messageId) {
startingPosition = msgs.length-1-i;
break;
}
}
Intent intent = new Intent(MapActivity.this, ConversationActivity.class);
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, dcMsgChatId);
intent.putExtra(ConversationActivity.LAST_SEEN_EXTRA, 0);
intent.putExtra(ConversationActivity.STARTING_POSITION_EXTRA, startingPosition);
startActivity(intent);
return true;
}
return false;
}
private boolean handleAddPoiClick(LatLng point) {
if (markerViewManager.hasMarkers()) {
markerViewManager.removeMarkers();
} else {
AddPoiView addPoiView = new AddPoiView(this);
addPoiView.setLatLng(point);
markerViewManager.addMarker(new MarkerView(point, addPoiView));
}
return true;
}
}
@@ -2,15 +2,8 @@ package org.thoughtcrime.securesms.map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.b44t.messenger.DcEventCenter;
@@ -33,7 +26,6 @@ import org.thoughtcrime.securesms.map.DataCollectionTask.DataCollectionCallback;
import org.thoughtcrime.securesms.map.GenerateInfoWindowTask.GenerateInfoWindowCallback;
import org.thoughtcrime.securesms.map.model.FilterProvider;
import org.thoughtcrime.securesms.map.model.MapSource;
import org.thoughtcrime.securesms.util.Prefs;
import java.util.ArrayList;
import java.util.Arrays;
@@ -72,6 +64,7 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacem
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
import static org.thoughtcrime.securesms.map.model.MapSource.INFO_WINDOW_LAYER;
import static org.thoughtcrime.securesms.map.model.MapSource.LINE_FEATURE_LIST;
import static org.thoughtcrime.securesms.util.BitmapUtil.generateColoredBitmap;
/**
@@ -211,14 +204,16 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn
return markerLayers;
}
public void unselectMarker() {
public boolean unselectMarker() {
if (selectedFeature != null) {
selectedFeature.addBooleanProperty(MARKER_SELECTED, false);
refreshSource(selectedFeature.getNumberProperty(CONTACT_ID).intValue());
selectedFeature = null;
GeoJsonSource source = (GeoJsonSource) mapboxStyle.getSource(INFO_WINDOW_SRC);
source.setGeoJson(FeatureCollection.fromFeatures(new ArrayList<>()));
return true;
}
return false;
}
public void setMarkerSelected(String featureId) {
@@ -252,7 +247,6 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn
applyLineFilter(source);
}
if (boundingBuilder != null && callback != null) {
LatLngBounds bound = null;
try {
@@ -286,7 +280,7 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn
public int getChatId() {
return chatId;
}
private void showLineLayer(MapSource source) {
LineLayer lineLayer = (LineLayer) mapboxStyle.getLayer(source.getLineLayer());
lineLayer.setProperties(visibility(showTraces ? VISIBLE : NONE));
@@ -429,31 +423,11 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn
}
private Bitmap generateColoredLastPositionIcon(int colorFilter) {
return generateColoredBitmap(colorFilter, R.drawable.ic_location_on_white_48dp);
return generateColoredBitmap(context, colorFilter, R.drawable.ic_location_on_white_48dp);
}
private Bitmap generateColoredLocationIcon(int colorFilter) {
return generateColoredBitmap(colorFilter, R.drawable.ic_location_dot);
}
private Bitmap generateColoredBitmap(int colorFilter, @DrawableRes int res) {
Bitmap icon = getBitmap(res);
Paint paint = new Paint();
ColorFilter filter = new PorterDuffColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(icon);
canvas.drawBitmap(icon, 0, 0, paint);
return icon;
}
private Bitmap getBitmap(@DrawableRes int res) {
Drawable drawable = ContextCompat.getDrawable(context, res);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
return generateColoredBitmap(context, colorFilter, R.drawable.ic_location_dot);
}
private void updateSources() {
@@ -0,0 +1,84 @@
package org.thoughtcrime.securesms.map;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.view.View;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.Projection;
/**
* MarkerView class wraps a latitude-longitude pair with a Android SDK View.
* <p>
* It can be used in conjunction with {@link com.mapbox.mapboxsdk.plugins.markerview.MarkerViewManager} to synchronise the Android SDK View on top
* of map at the latitude-longitude pair.
* </p>
*/
public class MarkerView {
private final View view;
private LatLng latLng;
private Projection projection;
private OnPositionUpdateListener onPositionUpdateListener;
/**
* Create a MarkerView
*
* @param latLng latitude-longitude pair
* @param view an Android SDK View
*/
public MarkerView(@NonNull LatLng latLng, @NonNull View view) {
this.latLng = latLng;
this.view = view;
}
/**
* Update the location of the MarkerView on the map.
* <p>
* Provided as a latitude-longitude pair.
* </p>
*
* @param latLng latitude-longitude pair
*/
public void setLatLng(@NonNull LatLng latLng) {
this.latLng = latLng;
update();
}
/**
* Set a callback to be invoked when position placement is calculated.
* <p>
* Can be used to offset a MarkerView on screen.
* </p>
*
* @param onPositionUpdateListener callback to be invoked when position placement is calculated
*/
public void setOnPositionUpdateListener(OnPositionUpdateListener onPositionUpdateListener) {
this.onPositionUpdateListener = onPositionUpdateListener;
}
/**
* Callback definition that is invoked when position placement of a MarkerView is calculated.
*/
public interface OnPositionUpdateListener {
PointF onUpdate(PointF pointF);
}
void setProjection(Projection projection) {
this.projection = projection;
}
View getView() {
return view;
}
void update() {
PointF point = projection.toScreenLocation(latLng);
if (onPositionUpdateListener != null) {
point = onPositionUpdateListener.onUpdate(point);
}
view.setX(point.x);
view.setY(point.y);
}
}
@@ -0,0 +1,104 @@
package org.thoughtcrime.securesms.map;
import android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import java.util.ArrayList;
import java.util.List;
/**
* Class responsible for synchronising views at a LatLng on top of a Map.
*/
public class MarkerViewManager implements MapView.OnDidFinishRenderingFrameListener {
private final MapView mapView;
private final MapboxMap mapboxMap;
private final List<MarkerView> markers = new ArrayList<>();
private boolean initialised;
/**
* Create a MarkerViewManager.
*
* @param mapView the MapView used to synchronise views on
* @param mapboxMap the MapboxMap to synchronise views with
*/
public MarkerViewManager(MapView mapView, MapboxMap mapboxMap) {
this.mapView = mapView;
this.mapboxMap = mapboxMap;
}
/**
* Destroys the MarkerViewManager.
* <p>
* Should be called before MapView#onDestroy
* </p>
*/
@UiThread
public void onDestroy() {
markers.clear();
mapView.removeOnDidFinishRenderingFrameListener(this);
initialised = false;
}
/**
* Add a MarkerView to the map using MarkerView and LatLng.
*
* @param markerView the markerView to synchronise on the map
*/
@UiThread
public void addMarker(@NonNull MarkerView markerView) {
if (mapView.isDestroyed() || markers.contains(markerView)) {
return;
}
if (!initialised) {
initialised = true;
mapView.addOnDidFinishRenderingFrameListener(this);
}
markerView.setProjection(mapboxMap.getProjection());
mapView.addView(markerView.getView());
markers.add(markerView);
}
/**
* Remove an existing markerView from the map.
*
* @param markerView the markerView to be removed from the map
*/
@UiThread
public void removeMarker(@NonNull MarkerView markerView) {
if (mapView.isDestroyed() || !markers.contains(markerView)) {
return;
}
mapView.removeView(markerView.getView());
markers.remove(markerView);
}
public boolean hasMarkers() {
return markers.size() > 0;
}
public void removeMarkers() {
for (MarkerView markerView : markers) {
mapView.removeView(markerView.getView());
}
markers.clear();
}
@Override
public void onDidFinishRenderingFrame(boolean fully) {
if (fully) {
update();
}
}
private void update() {
for (MarkerView marker : markers) {
marker.update();
}
}
}
@@ -6,8 +6,12 @@ import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.graphics.drawable.BitmapDrawable;
@@ -15,6 +19,7 @@ import android.graphics.drawable.Drawable;
import android.support.annotation.*;
import android.support.annotation.WorkerThread;
import android.support.media.ExifInterface;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.util.Pair;
import android.view.View;
@@ -476,4 +481,24 @@ public class BitmapUtil {
view.draw(canvas);
return bitmap;
}
public static Bitmap generateColoredBitmap(Context context, int colorFilter, @DrawableRes int res) {
Bitmap icon = getBitmap(context, res);
Paint paint = new Paint();
ColorFilter filter = new PorterDuffColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(icon);
canvas.drawBitmap(icon, 0, 0, paint);
return icon;
}
private static Bitmap getBitmap(Context context, @DrawableRes int res) {
Drawable drawable = ContextCompat.getDrawable(context, res);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
}