Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: missing render when coming back from background #364

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.view.TextureView.SurfaceTextureListener;
import android.location.Location;
import android.os.Build;
import android.util.DisplayMetrics;
Expand All @@ -21,6 +23,7 @@
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -221,6 +224,7 @@ private CameraPosition getCameraPosition() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
installInvalidator();
if (mapReadyResult != null) {
mapReadyResult.success(null);
mapReadyResult = null;
Expand Down Expand Up @@ -256,6 +260,71 @@ public boolean onTouch(View v, MotionEvent event) {
setStyleString(styleStringInitial);
}

// Returns the first TextureView found in the view hierarchy.
private static TextureView findTextureView(ViewGroup group) {
final int n = group.getChildCount();
for (int i = 0; i < n; i++) {
View view = group.getChildAt(i);
if (view instanceof TextureView) {
return (TextureView) view;
}
if (view instanceof ViewGroup) {
TextureView r = findTextureView((ViewGroup) view);
if (r != null) {
return r;
}
}
}
return null;
}

private void installInvalidator() {
if (mapView == null) {
// This should only happen in tests.
return;
}
TextureView textureView = findTextureView(mapView);
if (textureView == null) {
Log.i(TAG, "No TextureView found. Likely using the LEGACY renderer.");
return;
}
Log.i(TAG, "Installing custom TextureView driven invalidator.");
SurfaceTextureListener internalListener = textureView.getSurfaceTextureListener();
// Override the Maps internal SurfaceTextureListener with our own. Our listener
// mostly just invokes the internal listener callbacks but in onSurfaceTextureUpdated
// the mapView is invalidated which ensures that all map updates are presented to the
// screen.
final MapView mapView = this.mapView;
textureView.setSurfaceTextureListener(
new TextureView.SurfaceTextureListener() {
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (internalListener != null) {
internalListener.onSurfaceTextureAvailable(surface, width, height);
}
}

public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (internalListener != null) {
return internalListener.onSurfaceTextureDestroyed(surface);
}
return true;
}

public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
if (internalListener != null) {
internalListener.onSurfaceTextureSizeChanged(surface, width, height);
}
}

public void onSurfaceTextureUpdated(SurfaceTexture surface) {
if (internalListener != null) {
internalListener.onSurfaceTextureUpdated(surface);
}
mapView.postInvalidate();
}
});
}

@Override
public void setStyleString(@NonNull String styleString) {
// clear old layer id from the location Component
Expand Down
Loading