package com.mapbox.mapboxsdk.maps; import android.content.Context; import android.graphics.Canvas; 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; import android.support.annotation.Nullable; import android.support.annotation.UiThread; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ZoomButtonsController; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.maps.widgets.CompassView; import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings; import com.mapbox.mapboxsdk.net.ConnectivityReceiver; import com.mapbox.services.android.telemetry.MapboxTelemetry; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** *

* A {@code MapView} provides an embeddable map interface. * You use this class to display map information and to manipulate the map contents from your application. * You can center the map on a given coordinate, specify the size of the area you want to display, * and style the features of the map to fit your application's use case. *

*

* Use of {@code MapView} requires a Mapbox API access token. * Obtain an access token on the Mapbox account page. *

* Warning: 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 { private NativeMapView nativeMapView; private boolean textureMode; private boolean destroyed; private boolean hasSurface; private MapboxMap mapboxMap; private MapCallback mapCallback; private MapGestureDetector mapGestureDetector; private MapKeyListener mapKeyListener; private MapZoomButtonController mapZoomButtonController; @UiThread public MapView(@NonNull Context context) { super(context); initialise(context, MapboxMapOptions.createFromAttributes(context, null)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initialise(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialise(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable MapboxMapOptions options) { super(context); initialise(context, options == null ? MapboxMapOptions.createFromAttributes(context, null) : options); } private void initialise(@NonNull final Context context, @NonNull final MapboxMapOptions options) { if (isInEditMode()) { // in IDE layout editor, just return return; } // determine render surface textureMode = options.getTextureMode(); // 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); // add accessibility support setContentDescription(context.getString(R.string.mapbox_mapActionDescription)); // create native Map object nativeMapView = new NativeMapView(this); // callback for focal point invalidation FocalPointInvalidator focalPoint = new FocalPointInvalidator(compassView); // callback for registering touch listeners RegisterTouchListener registerTouchListener = new RegisterTouchListener(); // callback for zooming in the camera CameraZoomInvalidator zoomInvalidator = new CameraZoomInvalidator(); // callback for camera change events CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher(); // setup components for MapboxMap creation Projection proj = new Projection(nativeMapView); UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, view.findViewById(R.id.logoView)); TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint, zoomInvalidator); MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint); MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer)); AnnotationManager annotations = new AnnotationManager(nativeMapView, this, markerViewManager); Transform transform = new Transform(nativeMapView, annotations.getMarkerViewManager(), trackingSettings, cameraChangeDispatcher); mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, proj, registerTouchListener, annotations, cameraChangeDispatcher); // user input mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings, annotations, cameraChangeDispatcher); mapKeyListener = new MapKeyListener(transform, trackingSettings, uiSettings); MapZoomControllerListener zoomListener = new MapZoomControllerListener(mapGestureDetector, uiSettings, transform); mapZoomButtonController = new MapZoomButtonController(this, uiSettings, zoomListener); // inject widgets with MapboxMap compassView.setMapboxMap(mapboxMap); myLocationView.setMapboxMap(mapboxMap); attrView.setOnClickListener(new AttributionDialogManager(context, mapboxMap)); // Ensure this view is interactable setClickable(true); setLongClickable(true); setFocusable(true); 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); } // // Lifecycle events // /** *

* You must call this method from the parent's Activity#onCreate(Bundle)} or * Fragment#onCreate(Bundle). *

* You must set a valid access token with {@link com.mapbox.mapboxsdk.Mapbox#getInstance(Context, String)} * before you call this method or an exception will be thrown. * * @param savedInstanceState Pass in the parent's savedInstanceState. * @see com.mapbox.mapboxsdk.Mapbox#getInstance(Context, String) */ @UiThread public void onCreate(@Nullable Bundle savedInstanceState) { if (savedInstanceState == null) { MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapLoadEvent()); } else if (savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) { mapboxMap.onRestoreInstanceState(savedInstanceState); } initialiseDrawingSurface(textureMode); addOnMapChangedListener(mapCallback = new MapCallback(mapboxMap)); } private void initialiseDrawingSurface(boolean textureMode) { nativeMapView.initializeDisplay(); nativeMapView.initializeContext(); if (textureMode) { TextureView textureView = new TextureView(getContext()); textureView.setSurfaceTextureListener(new SurfaceTextureListener()); addView(textureView, 0); } else { SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceView.getHolder().addCallback(new SurfaceCallback()); surfaceView.setVisibility(View.VISIBLE); } } /** * You must call this method from the parent's Activity#onSaveInstanceState(Bundle) * or Fragment#onSaveInstanceState(Bundle). * * @param outState Pass in the parent's outState. */ @UiThread public void onSaveInstanceState(@NonNull Bundle outState) { outState.putBoolean(MapboxConstants.STATE_HAS_SAVED_STATE, true); mapboxMap.onSaveInstanceState(outState); } /** * You must call this method from the parent's Activity#onStart() or Fragment#onStart() */ @UiThread public void onStart() { mapboxMap.onStart(); ConnectivityReceiver.instance(getContext()).activate(); } /** * You must call this method from the parent's Activity#onResume() or Fragment#onResume(). */ @UiThread public void onResume() { // replaced by onStart in v5.0.0 } /** * You must call this method from the parent's Activity#onPause() or Fragment#onPause(). */ @UiThread public void onPause() { // replaced by onStop in v5.0.0 } /** * You must call this method from the parent's Activity#onStop() or Fragment#onStop(). */ @UiThread public void onStop() { mapboxMap.onStop(); ConnectivityReceiver.instance(getContext()).deactivate(); } /** * You must call this method from the parent's Activity#onDestroy() or Fragment#onDestroy(). */ @UiThread public void onDestroy() { destroyed = true; nativeMapView.terminateContext(); nativeMapView.terminateDisplay(); nativeMapView.destroySurface(); nativeMapView.destroy(); nativeMapView = null; } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { mapZoomButtonController.setVisible(true); } return mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mapKeyListener.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { return mapKeyListener.onKeyLongPress(keyCode, event) || super.onKeyLongPress(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mapKeyListener.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event); } @Override public boolean onTrackballEvent(MotionEvent event) { return mapKeyListener.onTrackballEvent(event) || super.onTrackballEvent(event); } @Override public boolean onGenericMotionEvent(MotionEvent event) { return mapGestureDetector.onGenericMotionEvent(event) || super.onGenericMotionEvent(event); } @Override public boolean onHoverEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_HOVER_ENTER: case MotionEvent.ACTION_HOVER_MOVE: mapZoomButtonController.setVisible(true); return true; case MotionEvent.ACTION_HOVER_EXIT: mapZoomButtonController.setVisible(false); return true; default: // We are not interested in this event return false; } } /** * You must call this method from the parent's Activity#onLowMemory() or Fragment#onLowMemory(). */ @UiThread public void onLowMemory() { nativeMapView.onLowMemory(); } // Called when debug mode is enabled to update a FPS counter // Called via JNI from NativeMapView // Forward to any listener protected void onFpsChanged(final double fps) { final MapboxMap.OnFpsChangedListener listener = mapboxMap.getOnFpsChangedListener(); if (listener != null) { post(new Runnable() { @Override public void run() { listener.onFpsChanged(fps); } }); } } /** *

* Loads a new map style from the specified URL. *

* {@code url} can take the following forms: *