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

Commit

Permalink
[android] - integrate view callback abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Apr 24, 2018
1 parent 991264a commit f495ad8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,20 +41,18 @@
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;
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;

Expand All @@ -71,13 +70,14 @@
* <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;

Expand All @@ -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) {
Expand Down Expand Up @@ -307,7 +304,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
addView(glSurfaceView, 0);
}

nativeMapView = new NativeMapView(this, mapRenderer);
nativeMapView = new NativeMapView(getContext(), this, mapRenderer);
nativeMapView.resizeView(getMeasuredWidth(), getMeasuredHeight());
}

Expand Down Expand Up @@ -566,19 +563,18 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
}

//
// 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.
Expand All @@ -590,7 +586,7 @@ void onMapChange(int rawChange) {
*/
public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) {
if (listener != null) {
onMapChangedListeners.add(listener);
nativeMapView.addOnMapChangedListener(listener);
}
}

Expand All @@ -602,7 +598,7 @@ public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) {
*/
public void removeOnMapChangedListener(@Nullable OnMapChangedListener listener) {
if (listener != null) {
onMapChangedListeners.remove(listener);
nativeMapView.removeOnMapChangedListener(listener);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -38,33 +39,36 @@
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();
}
Expand All @@ -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);
}

Expand All @@ -100,7 +101,7 @@ private boolean isDestroyedOn(String callingMethod) {

public void destroy() {
nativeDestroy();
mapView = null;
viewCallback = null;
destroyed = true;
}

Expand Down Expand Up @@ -857,8 +858,12 @@ RectF getDensityDependantRectangle(final RectF rectangle) {
//

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");
}
}
}

Expand All @@ -867,7 +872,7 @@ protected void onSnapshotReady(Bitmap mapContent) {
return;
}

Bitmap viewContent = BitmapUtils.createBitmapFromView(mapView);
Bitmap viewContent = viewCallback.getViewContent();
if (snapshotReadyCallback != null && mapContent != null && viewContent != null) {
snapshotReadyCallback.onSnapshotReady(BitmapUtils.mergeBitmap(mapContent, viewContent));
}
Expand Down Expand Up @@ -1065,28 +1070,28 @@ int getWidth() {
if (isDestroyedOn("")) {
return 0;
}
return mapView.getWidth();
return viewCallback.getWidth();
}

int getHeight() {
if (isDestroyedOn("")) {
return 0;
}
return mapView.getHeight();
return viewCallback.getHeight();
}

//
// MapChangeEvents
//

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);
}
}

//
Expand All @@ -1102,15 +1107,15 @@ void addSnapshotCallback(@NonNull MapboxMap.SnapshotReadyCallback callback) {
}

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() {
Expand All @@ -1119,14 +1124,12 @@ public void run() {

});
}

});
}

});
}


//
// Image conversion
//
Expand Down Expand Up @@ -1176,4 +1179,12 @@ protected void onPostExecute(List<Image> images) {
}
}
}

public interface ViewCallback {
int getWidth();

int getHeight();

Bitmap getViewContent();
}
}

0 comments on commit f495ad8

Please sign in to comment.