Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[android] - Refactor dependencies, introduce focused components #7189

Merged
merged 2 commits into from
Dec 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
@@ -31,27 +32,31 @@
*/
public class MarkerViewManager {

private Map<MarkerView, View> markerViewMap;
private final ViewGroup markerViewContainer;
private final Map<MarkerView, View> markerViewMap = new HashMap<>();
private final List<MapboxMap.MarkerViewAdapter> markerViewAdapters = new ArrayList<>();

// TODO refactor MapboxMap out for Projection and Transform
// Requires removing MapboxMap from Annotations by using Peer model from #6912
private MapboxMap mapboxMap;
private MapView mapView;
private List<MapboxMap.MarkerViewAdapter> markerViewAdapters;

private long viewMarkerBoundsUpdateTime;
private MapboxMap.OnMarkerViewClickListener onMarkerViewClickListener;
private ImageMarkerViewAdapter defaultMarkerViewAdapter;

/**
* Creates an instance of MarkerViewManager.
*
* @param mapboxMap the MapboxMap associated with the MarkerViewManager
* @param mapView the MapView associated with the MarkerViewManager
* @param container the ViewGroup associated with the MarkerViewManager
*/
public MarkerViewManager(@NonNull MapboxMap mapboxMap, @NonNull MapView mapView) {
public MarkerViewManager(@NonNull ViewGroup container) {
this.markerViewContainer = container;
this.markerViewAdapters.add(new ImageMarkerViewAdapter(container.getContext()));
}

// TODO refactor MapboxMap out for Projection and Transform
// Requires removing MapboxMap from Annotations by using Peer model from #6912
public void bind(MapboxMap mapboxMap){
this.mapboxMap = mapboxMap;
this.markerViewAdapters = new ArrayList<>();
this.mapView = mapView;
this.markerViewMap = new HashMap<>();
this.defaultMarkerViewAdapter = new ImageMarkerViewAdapter(mapView.getContext());
this.markerViewAdapters.add(defaultMarkerViewAdapter);
}

/**
@@ -415,7 +420,7 @@ public void scheduleViewMarkerInvalidation() {
* </p>
*/
public void invalidateViewMarkersInVisibleRegion() {
RectF mapViewRect = new RectF(0, 0, mapView.getWidth(), mapView.getHeight());
RectF mapViewRect = new RectF(0, 0, markerViewContainer.getWidth(), markerViewContainer.getHeight());
List<MarkerView> markers = mapboxMap.getMarkerViewsInRect(mapViewRect);
View convertView;

@@ -445,7 +450,7 @@ public void invalidateViewMarkersInVisibleRegion() {

// Inflate View
convertView = (View) adapter.getViewReusePool().acquire();
final View adaptedView = adapter.getView(marker, convertView, mapView);
final View adaptedView = adapter.getView(marker, convertView, markerViewContainer);
if (adaptedView != null) {
adaptedView.setRotationX(marker.getTilt());
adaptedView.setRotation(marker.getRotation());
@@ -464,7 +469,7 @@ public void invalidateViewMarkersInVisibleRegion() {
markerViewMap.put(marker, adaptedView);
if (convertView == null) {
adaptedView.setVisibility(View.GONE);
mapView.getMarkerViewContainer().addView(adaptedView);
markerViewContainer.addView(adaptedView);
}
}
}
@@ -515,15 +520,15 @@ public void ensureInfoWindowOffset(MarkerView marker) {
for (final MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) {
if (adapter.getMarkerClass().equals(marker.getClass())) {
View convertView = (View) adapter.getViewReusePool().acquire();
view = adapter.getView(marker, convertView, mapView);
view = adapter.getView(marker, convertView, markerViewContainer);
break;
}
}
}

if (view != null) {
if (marker.getWidth() == 0) {
if(view.getMeasuredWidth()==0) {
if (view.getMeasuredWidth() == 0) {
//Ensure the marker's view is measured first
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
}
@@ -546,6 +551,10 @@ public void ensureInfoWindowOffset(MarkerView marker) {
}
}

public ViewGroup getMarkerViewContainer() {
return markerViewContainer;
}

/**
* Default MarkerViewAdapter used for base class of {@link MarkerView} to adapt a MarkerView to
* an ImageView.
Original file line number Diff line number Diff line change
@@ -257,7 +257,7 @@ public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
float scaleX = (uiSettings.getWidth() - padding.left - padding.right) / width;
float scaleY = (uiSettings.getHeight() - padding.top - padding.bottom) / height;
minScale = scaleX < scaleY ? scaleX : scaleY;
zoom = projection.calculateZoom(minScale);
zoom = calculateZoom(mapboxMap, minScale);
zoom = MathUtils.clamp(zoom, mapboxMap.getMinZoom(), mapboxMap.getMaxZoom());
}

@@ -275,6 +275,16 @@ public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
.bearing(0)
.build();
}

/**
* Calculates a zoom level based on minimum scale and current scale from MapView
*
* @param minScale The minimum scale to calculate the zoom level.
* @return zoom level that fits the MapView.
*/
public double calculateZoom(MapboxMap mapboxMap, float minScale) {
return Math.log(mapboxMap.getCameraPosition().zoom * minScale) / Math.log(2);
}
}

static final class CameraMoveUpdate implements CameraUpdate {
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.mapbox.mapboxsdk.maps;

import android.graphics.PointF;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LongSparseArray;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
@@ -18,6 +20,7 @@
import com.mapbox.mapboxsdk.annotations.PolylineOptions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
@@ -32,30 +35,38 @@
*/
class AnnotationManager implements MapView.OnMapChangedListener {

private NativeMapView nativeMapView;
private IconManager iconManager;
private InfoWindowManager infoWindowManager;
private MarkerViewManager markerViewManager;
private final NativeMapView nativeMapView;
private final MapView mapView;
private final IconManager iconManager;
private final InfoWindowManager infoWindowManager = new InfoWindowManager();
private final MarkerViewManager markerViewManager;
private final LongSparseArray<Annotation> annotations = new LongSparseArray<>();
private final List<Marker> selectedMarkers = new ArrayList<>();

private LongSparseArray<Annotation> annotations;
private List<Marker> selectedMarkers;
private MapboxMap mapboxMap;

private MapboxMap.OnMarkerClickListener onMarkerClickListener;
private boolean isWaitingForRenderInvoke;

AnnotationManager(NativeMapView view, IconManager iconManager, InfoWindowManager manager) {
AnnotationManager(NativeMapView view, MapView mapView, MarkerViewManager markerViewManager) {
this.nativeMapView = view;
this.iconManager = iconManager;
this.infoWindowManager = manager;
this.selectedMarkers = new ArrayList<>();
this.annotations = new LongSparseArray<>();

this.mapView = mapView;
this.iconManager = new IconManager(nativeMapView);
this.markerViewManager = markerViewManager;
if (view != null) {
// null checking needed for unit tests
view.addOnMapChangedListener(this);
}
}

// TODO refactor MapboxMap out for Projection and Transform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's note these things on #6912 as well. I'm planning to pick that up soon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Requires removing MapboxMap from Annotations by using Peer model from #6912
AnnotationManager bind(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
this.markerViewManager.bind(mapboxMap);
return this;
}

@Override
public void onMapChanged(@MapView.MapChange int change) {
if (isWaitingForRenderInvoke && change == MapView.DID_FINISH_RENDERING_FRAME_FULLY_RENDERED) {
@@ -280,7 +291,7 @@ void setOnMarkerClickListener(@Nullable MapboxMap.OnMarkerClickListener listener
onMarkerClickListener = listener;
}

void selectMarker(@NonNull Marker marker, @NonNull MapboxMap mapboxMap) {
void selectMarker(@NonNull Marker marker) {
if (selectedMarkers.contains(marker)) {
return;
}
@@ -303,7 +314,7 @@ void selectMarker(@NonNull Marker marker, @NonNull MapboxMap mapboxMap) {
}

if (infoWindowManager.isInfoWindowValidForMarker(marker) || infoWindowManager.getInfoWindowAdapter() != null) {
infoWindowManager.add(marker.showInfoWindow(mapboxMap, mapboxMap.getMapView()));
infoWindowManager.add(marker.showInfoWindow(mapboxMap, mapView));
}
}

@@ -564,14 +575,11 @@ List<Polyline> getPolylines() {
return polylines;
}

//
// MarkerViewManager
//
InfoWindowManager getInfoWindowManager() {
return infoWindowManager;
}

MarkerViewManager getMarkerViewManager(MapboxMap mapboxMap) {
if (markerViewManager == null) {
this.markerViewManager = new MarkerViewManager(mapboxMap, mapboxMap.getMapView());
}
MarkerViewManager getMarkerViewManager() {
return markerViewManager;
}

@@ -589,12 +597,13 @@ void adjustTopOffsetPixels(MapboxMap mapboxMap) {
for (Marker marker : selectedMarkers) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
marker.showInfoWindow(mapboxMap, mapboxMap.getMapView());
marker.showInfoWindow(mapboxMap, mapView);
}
}
}

void reloadMarkers() {
iconManager.reloadIcons();
int count = annotations.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotations.get(i);
@@ -606,4 +615,58 @@ void reloadMarkers() {
}
}
}

//
// Click event
//

boolean onTap(PointF tapPoint, float screenDensity) {
float toleranceSides = 4 * screenDensity;
float toleranceTopBottom = 10 * screenDensity;

RectF tapRect = new RectF(tapPoint.x - iconManager.getAverageIconWidth() / 2 - toleranceSides,
tapPoint.y - iconManager.getAverageIconHeight() / 2 - toleranceTopBottom,
tapPoint.x + iconManager.getAverageIconWidth() / 2 + toleranceSides,
tapPoint.y + iconManager.getAverageIconHeight() / 2 + toleranceTopBottom);

List<Marker> nearbyMarkers = getMarkersInRect(tapRect);
long newSelectedMarkerId = -1;

if (nearbyMarkers != null && nearbyMarkers.size() > 0) {
Collections.sort(nearbyMarkers);
for (Marker nearbyMarker : nearbyMarkers) {
boolean found = false;
for (Marker selectedMarker : selectedMarkers) {
if (selectedMarker.equals(nearbyMarker)) {
found = true;
}
}
if (!found) {
newSelectedMarkerId = nearbyMarker.getId();
break;
}
}
}

if (newSelectedMarkerId >= 0) {
List<Annotation> annotations = getAnnotations();
int count = annotations.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotations.get(i);
if (annotation instanceof Marker) {
if (annotation.getId() == newSelectedMarkerId) {
if (selectedMarkers.isEmpty() || !selectedMarkers.contains(annotation)) {
if (!(annotation instanceof MarkerView)) {
selectMarker((Marker) annotation);
} else {
markerViewManager.onClickMarkerView((MarkerView) annotation);
}
}
return true;
}
}
}
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mapbox.mapboxsdk.maps;

import android.graphics.PointF;

public interface FocalPointChangeListener {

void onFocalPointChanged(PointF pointF);
}
Loading