summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortobrun <tobrun.van.nuland@gmail.com>2018-02-06 14:57:45 +0100
committertobrun <tobrun.van.nuland@gmail.com>2018-02-06 14:57:45 +0100
commitb28fe02d253e5bfae77aa4b8798a19ef34061d39 (patch)
tree07351ba8d69ce52549d00d896f0b2b293f4ab8da
parent332dad2a4e002b1856ea072d6af22c9e501a55fd (diff)
downloadqtlocation-mapboxgl-upstream/tvn-prevent-context-leak.tar.gz
[android] - introduce mapview weak reference in global layout listenerupstream/tvn-prevent-context-leak
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java46
1 files changed, 29 insertions, 17 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 c5b7159e8c..4deea90cb5 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
@@ -22,7 +22,6 @@ import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ZoomButtonsController;
-
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
@@ -37,19 +36,18 @@ import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.services.android.telemetry.MapboxTelemetry;
+import timber.log.Timber;
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+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;
@@ -133,17 +131,7 @@ public class MapView extends FrameLayout {
setContentDescription(context.getString(R.string.mapbox_mapActionDescription));
setWillNotDraw(false);
- getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- getViewTreeObserver().removeOnGlobalLayoutListener(this);
- } else {
- getViewTreeObserver().removeGlobalOnLayoutListener(this);
- }
- initialiseDrawingSurface(options);
- }
- });
+ getViewTreeObserver().addOnGlobalLayoutListener(new MapViewLayoutListener(this, options));
}
private void initialiseMap() {
@@ -882,6 +870,30 @@ public class MapView extends FrameLayout {
void onMapChanged(@MapChange int change);
}
+ private static class MapViewLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
+
+ private WeakReference<MapView> mapViewWeakReference;
+ private MapboxMapOptions options;
+
+ MapViewLayoutListener(MapView mapView, MapboxMapOptions options) {
+ this.mapViewWeakReference = new WeakReference<>(mapView);
+ this.options = options;
+ }
+
+ @Override
+ public void onGlobalLayout() {
+ MapView mapView = mapViewWeakReference.get();
+ if (mapView != null) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+ } else {
+ mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
+ }
+ mapView.initialiseDrawingSurface(options);
+ }
+ }
+ }
+
private class FocalPointInvalidator implements FocalPointChangeListener {
private final List<FocalPointChangeListener> focalPointChangeListeners = new ArrayList<>();