summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java')
-rwxr-xr-xplatform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java73
1 files changed, 42 insertions, 31 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java
index 976277dcac..6ba87b4238 100755
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java
@@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.AsyncTask;
+import android.os.Handler;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -38,33 +39,36 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
import timber.log.Timber;
// Class that wraps the native methods for convenience
final class NativeMapView {
- // Flag to indicating destroy was called
- private boolean destroyed = false;
-
- // Holds the pointer to JNI NativeMapView
- private long nativePtr = 0;
-
- // Used for callbacks
- private MapView mapView;
-
//Hold a reference to prevent it from being GC'd as long as it's used on the native side
private final FileSource fileSource;
// Used to schedule work on the MapRenderer Thread
- private MapRenderer mapRenderer;
+ private final MapRenderer mapRenderer;
+
+ // Used for callbacks
+ private ViewCallback viewCallback;
// Device density
private final float pixelRatio;
+ // Flag to indicating destroy was called
+ private boolean destroyed = false;
+
+ // Holds the pointer to JNI NativeMapView
+ private long nativePtr = 0;
+
// Listener invoked to return a bitmap of the map
private MapboxMap.SnapshotReadyCallback snapshotReadyCallback;
+ private final CopyOnWriteArrayList<MapView.OnMapChangedListener> onMapChangedListeners = new CopyOnWriteArrayList<>();
+
static {
LibraryLoader.load();
}
@@ -73,14 +77,11 @@ final class NativeMapView {
// Constructors
//
- public NativeMapView(final MapView mapView, MapRenderer mapRenderer) {
+ public NativeMapView(final Context context, final ViewCallback viewCallback, final MapRenderer mapRenderer) {
this.mapRenderer = mapRenderer;
- this.mapView = mapView;
-
- Context context = mapView.getContext();
- fileSource = FileSource.getInstance(context);
- pixelRatio = context.getResources().getDisplayMetrics().density;
-
+ this.viewCallback = viewCallback;
+ this.fileSource = FileSource.getInstance(context);
+ this.pixelRatio = context.getResources().getDisplayMetrics().density;
nativeInitialize(this, fileSource, mapRenderer, pixelRatio);
}
@@ -100,7 +101,7 @@ final class NativeMapView {
public void destroy() {
nativeDestroy();
- mapView = null;
+ viewCallback = null;
destroyed = true;
}
@@ -857,8 +858,12 @@ final class NativeMapView {
//
protected void onMapChanged(int rawChange) {
- if (mapView != null) {
- mapView.onMapChange(rawChange);
+ for (MapView.OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
+ try {
+ onMapChangedListener.onMapChanged(rawChange);
+ } catch (RuntimeException err) {
+ Timber.e(err, "Exception in MapView.OnMapChangedListener");
+ }
}
}
@@ -867,7 +872,7 @@ final class NativeMapView {
return;
}
- Bitmap viewContent = BitmapUtils.createBitmapFromView(mapView);
+ Bitmap viewContent = viewCallback.getViewContent();
if (snapshotReadyCallback != null && mapContent != null && viewContent != null) {
snapshotReadyCallback.onSnapshotReady(BitmapUtils.mergeBitmap(mapContent, viewContent));
}
@@ -1065,14 +1070,14 @@ final class NativeMapView {
if (isDestroyedOn("")) {
return 0;
}
- return mapView.getWidth();
+ return viewCallback.getWidth();
}
int getHeight() {
if (isDestroyedOn("")) {
return 0;
}
- return mapView.getHeight();
+ return viewCallback.getHeight();
}
//
@@ -1080,13 +1085,13 @@ final class NativeMapView {
//
void addOnMapChangedListener(@NonNull MapView.OnMapChangedListener listener) {
- if (mapView != null) {
- mapView.addOnMapChangedListener(listener);
- }
+ onMapChangedListeners.add(listener);
}
void removeOnMapChangedListener(@NonNull MapView.OnMapChangedListener listener) {
- mapView.removeOnMapChangedListener(listener);
+ if (onMapChangedListeners.contains(listener)) {
+ onMapChangedListeners.remove(listener);
+ }
}
//
@@ -1102,15 +1107,15 @@ final class NativeMapView {
}
public void setOnFpsChangedListener(final MapboxMap.OnFpsChangedListener listener) {
+ final Handler handler = new Handler();
mapRenderer.queueEvent(new Runnable() {
@Override
public void run() {
mapRenderer.setOnFpsChangedListener(new MapboxMap.OnFpsChangedListener() {
-
@Override
public void onFpsChanged(final double fps) {
- mapView.post(new Runnable() {
+ handler.post(new Runnable() {
@Override
public void run() {
@@ -1119,14 +1124,12 @@ final class NativeMapView {
});
}
-
});
}
});
}
-
//
// Image conversion
//
@@ -1176,4 +1179,12 @@ final class NativeMapView {
}
}
}
+
+ public interface ViewCallback {
+ int getWidth();
+
+ int getHeight();
+
+ Bitmap getViewContent();
+ }
}