From b037a1db8ce3a76df083291469688533d5904745 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 17 Apr 2018 11:26:25 +0200 Subject: [android] - integrate view callback abstraction --- .../java/com/mapbox/mapboxsdk/maps/MapView.java | 34 +++++----- .../com/mapbox/mapboxsdk/maps/NativeMapView.java | 73 +++++++++++++--------- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 22d5dd8f19..4ccbc88375 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -1,6 +1,7 @@ package com.mapbox.mapboxsdk.maps; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.PointF; import android.opengl.GLSurfaceView; import android.os.Build; @@ -40,6 +41,7 @@ import com.mapbox.mapboxsdk.maps.renderer.textureview.TextureViewMapRenderer; import com.mapbox.mapboxsdk.maps.widgets.CompassView; import com.mapbox.mapboxsdk.net.ConnectivityReceiver; import com.mapbox.mapboxsdk.storage.FileSource; +import com.mapbox.mapboxsdk.utils.BitmapUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -47,13 +49,10 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; -import timber.log.Timber; - import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION; import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE; @@ -71,13 +70,14 @@ import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE; * Warning: Please note that you are responsible for getting permission to use the map data, * and for ensuring your use adheres to the relevant terms of use. */ -public class MapView extends FrameLayout { +public class MapView extends FrameLayout implements NativeMapView.ViewCallback { private final MapCallback mapCallback = new MapCallback(); private MapboxMap mapboxMap; private NativeMapView nativeMapView; private MapboxMapOptions mapboxMapOptions; + private MapRenderer mapRenderer; private boolean destroyed; private boolean hasSurface; @@ -90,9 +90,6 @@ public class MapView extends FrameLayout { private MapKeyListener mapKeyListener; private MapZoomButtonController mapZoomButtonController; private Bundle savedInstanceState; - private final CopyOnWriteArrayList onMapChangedListeners = new CopyOnWriteArrayList<>(); - - private MapRenderer mapRenderer; @UiThread public MapView(@NonNull Context context) { @@ -307,7 +304,7 @@ public class MapView extends FrameLayout { addView(glSurfaceView, 0); } - nativeMapView = new NativeMapView(this, mapRenderer); + nativeMapView = new NativeMapView(getContext(), this, mapRenderer); nativeMapView.resizeView(getMeasuredWidth(), getMeasuredHeight()); } @@ -566,19 +563,18 @@ public class MapView extends FrameLayout { } // - // Map events + // ViewCallback // - void onMapChange(int rawChange) { - for (MapView.OnMapChangedListener onMapChangedListener : onMapChangedListeners) { - try { - onMapChangedListener.onMapChanged(rawChange); - } catch (RuntimeException err) { - Timber.e(err, "Exception in MapView.OnMapChangedListener"); - } - } + @Override + public Bitmap getViewContent() { + return BitmapUtils.createBitmapFromView(this); } + // + // Map events + // + /** *

* Add a callback that's invoked when the displayed map view changes. @@ -590,7 +586,7 @@ public class MapView extends FrameLayout { */ public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) { if (listener != null) { - onMapChangedListeners.add(listener); + nativeMapView.addOnMapChangedListener(listener); } } @@ -602,7 +598,7 @@ public class MapView extends FrameLayout { */ public void removeOnMapChangedListener(@Nullable OnMapChangedListener listener) { if (listener != null) { - onMapChangedListeners.remove(listener); + nativeMapView.removeOnMapChangedListener(listener); } } 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 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(); + } } -- cgit v1.2.1