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

Commit

Permalink
[android] - harden query invocations, move hasSurface handling to Nat…
Browse files Browse the repository at this point in the history
…iveMapView
  • Loading branch information
tobrun committed Apr 11, 2019
1 parent ae3b4d2 commit 70b929e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -36,13 +35,12 @@
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.utils.BitmapUtils;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION;
import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE;

Expand Down Expand Up @@ -74,7 +72,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
private MapboxMapOptions mapboxMapOptions;
private MapRenderer mapRenderer;
private boolean destroyed;
private boolean hasSurface;

private CompassView compassView;
private PointF focalPoint;
Expand Down Expand Up @@ -292,6 +289,12 @@ protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
super.onSurfaceCreated(gl, config);
}

@Override
protected void onSurfaceDestroyed() {
MapView.this.onSurfaceDestroyed();
super.onSurfaceDestroyed();
}
};

addView(textureView, 0);
Expand All @@ -304,6 +307,12 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
super.onSurfaceCreated(gl, config);
}

@Override
protected void onSurfaceDestroyed() {
MapView.this.onSurfaceDestroyed();
super.onSurfaceDestroyed();
}
};

addView(glSurfaceView, 0);
Expand All @@ -316,7 +325,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

private void onSurfaceCreated() {
hasSurface = true;
nativeMapView.setHasSurface(true);
post(new Runnable() {
@Override
public void run() {
Expand All @@ -329,6 +338,12 @@ public void run() {
});
}

private void onSurfaceDestroyed() {
if (nativeMapView != null) {
nativeMapView.setHasSurface(false);
}
}

/**
* You must call this method from the parent's Activity#onSaveInstanceState(Bundle)
* or Fragment#onSaveInstanceState(Bundle).
Expand Down Expand Up @@ -427,7 +442,7 @@ public void onDestroy() {
mapboxMap.onDestroy();
}

if (nativeMapView != null && hasSurface) {
if (nativeMapView != null && nativeMapView.hasSurface()) {
// null when destroying an activity programmatically mapbox-navigation-android/issues/503
nativeMapView.destroy();
nativeMapView = null;
Expand All @@ -439,10 +454,10 @@ public void onDestroy() {
}

/**
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (mapRenderer != null) {
Expand Down Expand Up @@ -747,8 +762,7 @@ public void removeOnDidBecomeIdleListener(OnDidBecomeIdleListener listener) {
}

/**
/**
* /**
* Set a callback that's invoked when the style has finished loading.
*
* @param listener The callback that's invoked when the style has finished loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface NativeMap {

boolean isDestroyed();

boolean hasSurface();

void setHasSurface(boolean hasSurface);

//
// Camera API
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ final class NativeMapView implements NativeMap {
// Device density
private final float pixelRatio;

// Flag to indicating destroy was called
// Flag to indicate destroy was called
private boolean destroyed = false;

// Flag to indicate surface was destroyed
private boolean hasSurface = false;

// Holds the pointer to JNI NativeMapView
@Keep
private long nativePtr = 0;
Expand Down Expand Up @@ -890,7 +893,7 @@ public Bitmap getImage(String name) {
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
@Nullable String[] layerIds,
@Nullable Expression filter) {
if (checkState("queryRenderedFeatures")) {
if (checkState("queryRenderedFeatures") || !hasSurface) {
return new ArrayList<>();
}
Feature[] features = nativeQueryRenderedFeaturesForPoint(coordinates.x / pixelRatio,
Expand All @@ -903,7 +906,7 @@ public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
@Nullable String[] layerIds,
@Nullable Expression filter) {
if (checkState("queryRenderedFeatures")) {
if (checkState("queryRenderedFeatures") || !hasSurface) {
return new ArrayList<>();
}
Feature[] features = nativeQueryRenderedFeaturesForBox(
Expand Down Expand Up @@ -1418,6 +1421,16 @@ public boolean isDestroyed() {
return destroyed;
}

@Override
public boolean hasSurface() {
return hasSurface;
}

@Override
public void setHasSurface(boolean hasSurface) {
this.hasSurface = hasSurface;
}

public interface ViewCallback {
int getWidth();

Expand Down

0 comments on commit 70b929e

Please sign in to comment.