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

Commit

Permalink
[android] - rewrite map object intialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jul 10, 2017
1 parent 7d59e3f commit 4b2720c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.graphics.PointF;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.CallSuper;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -42,6 +41,9 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import timber.log.Timber;

/**
* <p>
Expand All @@ -59,17 +61,24 @@
*/
public class MapView extends FrameLayout {

private final MapCallback mapCallback = new MapCallback();
private MapboxMap mapboxMap;

private NativeMapView nativeMapView;
private boolean textureMode;
private MapboxMapOptions mapboxMapOptions;
private boolean destroyed;
private boolean hasSurface;

private MapboxMap mapboxMap;
private MapCallback mapCallback;
private MyLocationView myLocationView;
private CompassView compassView;
private ImageView attrView;
private ImageView logoView;

private MapGestureDetector mapGestureDetector;
private MapKeyListener mapKeyListener;
private MapZoomButtonController mapZoomButtonController;
private Bundle savedInstanceState;
private final CopyOnWriteArrayList<OnMapChangedListener> onMapChangedListeners = new CopyOnWriteArrayList<>();

@UiThread
public MapView(@NonNull Context context) {
Expand Down Expand Up @@ -101,21 +110,25 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap
LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_preview, this);
return;
}
mapboxMapOptions = options;

// determine render surface
textureMode = options.getTextureMode();
Timber.e("MapView inflation");

// inflate view
View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this);
CompassView compassView = (CompassView) view.findViewById(R.id.compassView);
MyLocationView myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView);
ImageView attrView = (ImageView) view.findViewById(R.id.attributionView);
compassView = (CompassView) view.findViewById(R.id.compassView);
myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView);
attrView = (ImageView) view.findViewById(R.id.attributionView);
logoView = (ImageView) view.findViewById(R.id.logoView);

// add accessibility support
setContentDescription(context.getString(R.string.mapbox_mapActionDescription));
setWillNotDraw(false);
}

// create native Map object
nativeMapView = new NativeMapView(this);
private void initialiseMap() {
Context context = getContext();
addOnMapChangedListener(mapCallback);

// callback for focal point invalidation
FocalPointInvalidator focalPoint = new FocalPointInvalidator(compassView);
Expand All @@ -131,7 +144,7 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap

// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, view.findViewById(R.id.logoView));
UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, logoView);
TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint, zoomInvalidator);
MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
Expand All @@ -147,6 +160,7 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap
cameraChangeDispatcher);
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, proj,
registerTouchListener, annotationManager, cameraChangeDispatcher);
mapCallback.attachMapboxMap(mapboxMap);

// user input
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings,
Expand All @@ -168,14 +182,15 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap
setFocusableInTouchMode(true);
requestDisallowInterceptTouchEvent(true);

// allow onDraw invocation
setWillNotDraw(false);

// notify Map object about current connectivity state
nativeMapView.setReachability(ConnectivityReceiver.instance(context).isConnected(context));

// initialise MapboxMap
mapboxMap.initialise(context, options);
if (savedInstanceState == null) {
mapboxMap.initialise(context, mapboxMapOptions);
} else {
mapboxMap.onRestoreInstanceState(savedInstanceState);
}
}

//
Expand All @@ -195,19 +210,17 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap
*/
@UiThread
public void onCreate(@Nullable Bundle savedInstanceState) {
Timber.e("MapView onCreate");
if (savedInstanceState == null) {
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapLoadEvent());
} else if (savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) {
mapboxMap.onRestoreInstanceState(savedInstanceState);
this.savedInstanceState = savedInstanceState;
}

initialiseDrawingSurface(textureMode);
addOnMapChangedListener(mapCallback = new MapCallback(mapboxMap));
}

private void initialiseDrawingSurface(boolean textureMode) {
nativeMapView.initializeDisplay();
nativeMapView.initializeContext();
Timber.e("initalisDisplay and context");

if (textureMode) {
TextureView textureView = new TextureView(getContext());
textureView.setSurfaceTextureListener(new SurfaceTextureListener());
Expand All @@ -227,6 +240,7 @@ private void initialiseDrawingSurface(boolean textureMode) {
*/
@UiThread
public void onSaveInstanceState(@NonNull Bundle outState) {
Timber.e("onsaveinstancestate");
outState.putBoolean(MapboxConstants.STATE_HAS_SAVED_STATE, true);
mapboxMap.onSaveInstanceState(outState);
}
Expand All @@ -236,8 +250,11 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
*/
@UiThread
public void onStart() {
mapboxMap.onStart();
Timber.e("onstart");
ConnectivityReceiver.instance(getContext()).activate();
if (mapboxMap != null) {
mapboxMap.onStart();
}
}

/**
Expand All @@ -261,6 +278,7 @@ public void onPause() {
*/
@UiThread
public void onStop() {
Timber.e("onStop");
mapboxMap.onStop();
ConnectivityReceiver.instance(getContext()).deactivate();
}
Expand All @@ -270,7 +288,9 @@ public void onStop() {
*/
@UiThread
public void onDestroy() {
Timber.e("onDestroy");
destroyed = true;
Timber.e("terminate context, display and surface and map");
nativeMapView.terminateContext();
nativeMapView.terminateDisplay();
nativeMapView.destroySurface();
Expand Down Expand Up @@ -384,7 +404,10 @@ public void setStyleUrl(@NonNull String url) {
if (destroyed) {
return;
}

if (nativeMapView == null) {
mapboxMapOptions.styleUrl(url);
return;
}
nativeMapView.setStyleUrl(url);
}

Expand All @@ -406,13 +429,15 @@ public void onDraw(Canvas canvas) {
}

if (destroyed) {
Timber.e("not render destroyed");
return;
}

if (!hasSurface) {
Timber.e("not render no surface");
return;
}

Timber.e("render");
nativeMapView.render();
}

Expand All @@ -422,7 +447,7 @@ protected void onSizeChanged(int width, int height, int oldw, int oldh) {
return;
}

if (!isInEditMode()) {
if (!isInEditMode() && nativeMapView != null) {
nativeMapView.resizeView(width, height);
}
}
Expand All @@ -433,7 +458,19 @@ private class SurfaceCallback implements SurfaceHolder.Callback {

@Override
public void surfaceCreated(SurfaceHolder holder) {
nativeMapView.createSurface(surface = holder.getSurface());
Timber.e("onSurfaceCreated");
if (nativeMapView == null) {
nativeMapView = new NativeMapView(MapView.this);
nativeMapView.initializeDisplay();
nativeMapView.initializeContext();
nativeMapView.createSurface(surface = holder.getSurface());
nativeMapView.resizeView(getWidth(), getHeight());
initialiseMap();
mapboxMap.onStart();
} else {
nativeMapView.createSurface(surface = holder.getSurface());
}

hasSurface = true;
}

Expand All @@ -442,14 +479,17 @@ public void surfaceChanged(SurfaceHolder holder, int format, int width, int heig
if (destroyed) {
return;
}
Timber.e("surfaceChanged");
nativeMapView.resizeFramebuffer(width, height);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Timber.e("surfaceDestroyed");
hasSurface = false;

if (nativeMapView != null) {
Timber.e("NATIVEMAPVIEW IS NOT NULL, DESTROY SURFACE");
nativeMapView.destroySurface();
}
surface.release();
Expand All @@ -465,8 +505,19 @@ private class SurfaceTextureListener implements TextureView.SurfaceTextureListen
// Must do all EGL/GL ES initialization here
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
nativeMapView.createSurface(this.surface = new Surface(surface));
nativeMapView.resizeFramebuffer(width, height);
if (nativeMapView == null) {
nativeMapView = new NativeMapView(MapView.this);
nativeMapView.initializeDisplay();
nativeMapView.initializeContext();
nativeMapView.createSurface(this.surface = new Surface(surface));
nativeMapView.resizeFramebuffer(width, height);
nativeMapView.resizeView(width, height);
initialiseMap();
mapboxMap.onStart();
} else {
nativeMapView.createSurface(this.surface = new Surface(surface));
}

hasSurface = true;
}

Expand Down Expand Up @@ -522,16 +573,33 @@ protected void onDetachedFromWindow() {
// Called when view is hidden and shown
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
if (isInEditMode() || mapZoomButtonController == null) {
Timber.e("OnVisibilityChanged, %s, %s", changedView, visibility);
if (isInEditMode()) {
return;
}
mapZoomButtonController.setVisible(visibility == View.VISIBLE);
if (visibility == View.VISIBLE && nativeMapView == null) {
initialiseDrawingSurface(mapboxMapOptions.getTextureMode());
}

if (mapZoomButtonController != null && nativeMapView != null) {
mapZoomButtonController.setVisible(visibility == View.VISIBLE);
}
}

//
// Map events
//

void onMapChange(int rawChange) {
for (MapView.OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
try {
onMapChangedListener.onMapChanged(rawChange);
} catch (RuntimeException err) {
Timber.e("Exception (%s) in MapView.OnMapChangedListener: %s", err.getClass(), err.getMessage());
}
}
}

/**
* <p>
* Add a callback that's invoked when the displayed map view changes.
Expand All @@ -543,7 +611,7 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
*/
public void addOnMapChangedListener(@Nullable OnMapChangedListener listener) {
if (listener != null) {
nativeMapView.addOnMapChangedListener(listener);
onMapChangedListeners.add(listener);
}
}

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

Expand All @@ -566,13 +634,7 @@ public void removeOnMapChangedListener(@Nullable OnMapChangedListener listener)
*/
@UiThread
public void getMapAsync(final OnMapReadyCallback callback) {
if (!mapCallback.isInitialLoad() && callback != null) {
callback.onMapReady(mapboxMap);
} else {
if (callback != null) {
mapCallback.addOnMapReadyCallback(callback);
}
}
mapCallback.addOnMapReadyCallback(callback);
}

MapboxMap getMapboxMap() {
Expand Down Expand Up @@ -919,26 +981,21 @@ private void setZoom(double zoomLevel, @Nullable PointF focalPoint, @NonNull Tra

private static class MapCallback implements OnMapChangedListener {

private final MapboxMap mapboxMap;
private MapboxMap mapboxMap;
private final List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>();
private boolean initialLoad = true;

MapCallback(MapboxMap mapboxMap) {
void attachMapboxMap(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
}

@Override
public void onMapChanged(@MapChange int change) {
if (change == DID_FINISH_LOADING_STYLE && initialLoad) {
initialLoad = false;
new Handler().post(new Runnable() {
@Override
public void run() {
mapboxMap.onPreMapReady();
onMapReady();
mapboxMap.onPostMapReady();
}
});
mapboxMap.onPreMapReady();
onMapReady();
mapboxMap.onPostMapReady();
} else if (change == DID_FINISH_RENDERING_FRAME || change == DID_FINISH_RENDERING_FRAME_FULLY_RENDERED) {
mapboxMap.onUpdateFullyRendered();
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) {
Expand Down
Loading

0 comments on commit 4b2720c

Please sign in to comment.