diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2018-04-17 11:26:25 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2018-05-14 11:38:21 +0200 |
commit | 7854dfa3a02a7c5425b1c5066012fb81028df967 (patch) | |
tree | d316e44b14dfa162ce0e47251738d73df01f1f72 | |
parent | 36d30f13fcc6f4356e0d58318fc168c77d7dbecf (diff) | |
download | qtlocation-mapboxgl-7854dfa3a02a7c5425b1c5066012fb81028df967.tar.gz |
[android] - integrate view callback abstraction
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; * <strong>Warning:</strong> 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<OnMapChangedListener> 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 + // + /** * <p> * 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 305dd906dd..7cd1177523 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; } @@ -862,8 +863,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"); + } } } @@ -872,7 +877,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)); } @@ -1070,14 +1075,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(); } // @@ -1085,13 +1090,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); + } } // @@ -1107,15 +1112,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() { @@ -1124,14 +1129,12 @@ final class NativeMapView { }); } - }); } }); } - // // Image conversion // @@ -1181,4 +1184,12 @@ final class NativeMapView { } } } + + public interface ViewCallback { + int getWidth(); + + int getHeight(); + + Bitmap getViewContent(); + } } |