package com.mapbox.mapboxsdk.maps; import android.content.Context; import android.graphics.Bitmap; import android.graphics.PointF; import android.graphics.drawable.ColorDrawable; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.UiThread; import android.support.annotation.Nullable; import android.support.annotation.CallSuper; import android.support.v4.util.LongSparseArray; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.FrameLayout; import android.widget.ImageView; import com.mapbox.android.gestures.AndroidGesturesManager; import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.location.LocationComponent; import com.mapbox.mapboxsdk.maps.renderer.MapRenderer; import com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer; import com.mapbox.mapboxsdk.maps.renderer.textureview.TextureViewMapRenderer; import com.mapbox.mapboxsdk.maps.widgets.CompassView; import com.mapbox.mapboxsdk.net.ConnectivityReceiver; import com.mapbox.mapboxsdk.offline.OfflineRegionDefinition; 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 static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION; import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE; /** *

* 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 implements NativeMapView.ViewCallback { private final MapChangeReceiver mapChangeReceiver = new MapChangeReceiver(); private final MapCallback mapCallback = new MapCallback(); private final InitialRenderCallback initialRenderCallback = new InitialRenderCallback(); private NativeMapView nativeMapView; private MapboxMap mapboxMap; private MapboxMapOptions mapboxMapOptions; private MapRenderer mapRenderer; private boolean destroyed; private boolean hasSurface; private CompassView compassView; private PointF focalPoint; private ImageView attrView; private ImageView logoView; private MapGestureDetector mapGestureDetector; private MapKeyListener mapKeyListener; private Bundle savedInstanceState; @UiThread public MapView(@NonNull Context context) { super(context); initialize(context, MapboxMapOptions.createFromAttributes(context, null)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable MapboxMapOptions options) { super(context); initialize(context, options == null ? MapboxMapOptions.createFromAttributes(context, null) : options); } @CallSuper @UiThread protected void initialize(@NonNull final Context context, @NonNull final MapboxMapOptions options) { if (isInEditMode()) { // in IDE layout editor, just return return; } // hide surface until map is fully loaded #10990 setForeground(new ColorDrawable(options.getForegroundLoadColor())); mapboxMapOptions = options; // inflate view View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this); compassView = view.findViewById(R.id.compassView); attrView = view.findViewById(R.id.attributionView); logoView = view.findViewById(R.id.logoView); // add accessibility support setContentDescription(context.getString(R.string.mapbox_mapActionDescription)); setWillNotDraw(false); initialiseDrawingSurface(options); } private void initialiseMap() { Context context = getContext(); // callback for focal point invalidation final FocalPointInvalidator focalInvalidator = new FocalPointInvalidator(); focalInvalidator.addListener(createFocalPointChangeListener()); // callback for registering touch listeners GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener(); // callback for camera change events final CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher(); // setup components for MapboxMap creation Projection proj = new Projection(nativeMapView); UiSettings uiSettings = new UiSettings(proj, focalInvalidator, compassView, attrView, logoView, getPixelRatio()); LongSparseArray annotationsArray = new LongSparseArray<>(); MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer)); IconManager iconManager = new IconManager(nativeMapView); Annotations annotations = new AnnotationContainer(nativeMapView, annotationsArray); Markers markers = new MarkerContainer(nativeMapView, this, annotationsArray, iconManager, markerViewManager); Polygons polygons = new PolygonContainer(nativeMapView, annotationsArray); Polylines polylines = new PolylineContainer(nativeMapView, annotationsArray); ShapeAnnotations shapeAnnotations = new ShapeAnnotationContainer(nativeMapView, annotationsArray); AnnotationManager annotationManager = new AnnotationManager(this, annotationsArray, markerViewManager, iconManager, annotations, markers, polygons, polylines, shapeAnnotations); Transform transform = new Transform(this, nativeMapView, annotationManager.getMarkerViewManager(), cameraChangeDispatcher); // MapboxMap mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener, annotationManager, cameraChangeDispatcher, mapChangeReceiver); // user input mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, annotationManager, cameraChangeDispatcher); mapKeyListener = new MapKeyListener(transform, uiSettings, mapGestureDetector); // compass compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraChangeDispatcher)); compassView.setOnClickListener(createCompassClickListener(cameraChangeDispatcher)); // LocationComponent mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap)); // inject widgets with MapboxMap attrView.setOnClickListener(new AttributionClickListener(context, mapboxMap)); // Ensure this view is interactable setClickable(true); setLongClickable(true); setFocusable(true); setFocusableInTouchMode(true); requestDisallowInterceptTouchEvent(true); // notify Map object about current connectivity state nativeMapView.setReachability(ConnectivityReceiver.instance(context).isConnected(context)); // initialise MapboxMap if (savedInstanceState == null) { mapboxMap.initialise(context, mapboxMapOptions); } else { mapboxMap.onRestoreInstanceState(savedInstanceState); } mapCallback.initialised(); } private FocalPointChangeListener createFocalPointChangeListener() { return new FocalPointChangeListener() { @Override public void onFocalPointChanged(PointF pointF) { focalPoint = pointF; } }; } private MapboxMap.OnCompassAnimationListener createCompassAnimationListener(final CameraChangeDispatcher cameraChangeDispatcher) { return new MapboxMap.OnCompassAnimationListener() { @Override public void onCompassAnimation() { cameraChangeDispatcher.onCameraMove(); } @Override public void onCompassAnimationFinished() { compassView.isAnimating(false); cameraChangeDispatcher.onCameraIdle(); } }; } private OnClickListener createCompassClickListener(final CameraChangeDispatcher cameraChangeDispatcher) { return new OnClickListener() { @Override public void onClick(View v) { if (mapboxMap != null && compassView != null) { if (focalPoint != null) { mapboxMap.setFocalBearing(0, focalPoint.x, focalPoint.y, TIME_MAP_NORTH_ANIMATION); } else { mapboxMap.setFocalBearing(0, mapboxMap.getWidth() / 2, mapboxMap.getHeight() / 2, TIME_MAP_NORTH_ANIMATION); } cameraChangeDispatcher.onCameraMoveStarted(MapboxMap.OnCameraMoveStartedListener.REASON_API_ANIMATION); compassView.isAnimating(true); compassView.postDelayed(compassView, TIME_WAIT_IDLE + TIME_MAP_NORTH_ANIMATION); } } }; } // // Lifecycle events // /** *

* You must call this method from the parent's Activity#onCreate(Bundle)} or * Fragment#onViewCreated(View, 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) { TelemetryDefinition telemetry = Mapbox.getTelemetry(); if (telemetry != null) { telemetry.onAppUserTurnstileEvent(); } } else if (savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) { this.savedInstanceState = savedInstanceState; } } private void initialiseDrawingSurface(MapboxMapOptions options) { String localFontFamily = options.getLocalIdeographFontFamily(); if (options.getTextureMode()) { TextureView textureView = new TextureView(getContext()); boolean translucentSurface = options.getTranslucentTextureSurface(); mapRenderer = new TextureViewMapRenderer(getContext(), textureView, localFontFamily, translucentSurface) { @Override protected void onSurfaceCreated(GL10 gl, EGLConfig config) { MapView.this.onSurfaceCreated(); super.onSurfaceCreated(gl, config); } }; addView(textureView, 0); } else { GLSurfaceView glSurfaceView = new GLSurfaceView(getContext()); glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop()); mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView, localFontFamily) { @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { MapView.this.onSurfaceCreated(); super.onSurfaceCreated(gl, config); } }; addView(glSurfaceView, 0); } boolean crossSourceCollisions = mapboxMapOptions.getCrossSourceCollisions(); nativeMapView = new NativeMapView( getContext(), getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer ); } private void onSurfaceCreated() { hasSurface = true; post(new Runnable() { @Override public void run() { // Initialise only when not destroyed and only once if (!destroyed && mapboxMap == null) { MapView.this.initialiseMap(); mapboxMap.onStart(); } } }); } /** * 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) { if (mapboxMap != null) { 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() { ConnectivityReceiver.instance(getContext()).activate(); FileSource.getInstance(getContext()).activate(); if (mapboxMap != null) { mapboxMap.onStart(); } if (mapRenderer != null) { mapRenderer.onStart(); } } /** * You must call this method from the parent's Activity#onResume() or Fragment#onResume(). */ @UiThread public void onResume() { if (mapRenderer != null) { mapRenderer.onResume(); } } /** * You must call this method from the parent's Activity#onPause() or Fragment#onPause(). */ @UiThread public void onPause() { if (mapRenderer != null) { mapRenderer.onPause(); } } /** * You must call this method from the parent's Activity#onStop() or Fragment#onStop(). */ @UiThread public void onStop() { if (mapboxMap != null) { // map was destroyed before it was started mapGestureDetector.cancelAnimators(); mapboxMap.onStop(); } if (mapRenderer != null) { mapRenderer.onStop(); } ConnectivityReceiver.instance(getContext()).deactivate(); FileSource.getInstance(getContext()).deactivate(); } /** * You must call this method from the parent's Activity#onDestroy() or Fragment#onDestroyView(). */ @UiThread public void onDestroy() { destroyed = true; mapChangeReceiver.clear(); mapCallback.onDestroy(); initialRenderCallback.onDestroy(); if (mapboxMap != null) { mapboxMap.onDestroy(); } if (nativeMapView != null && hasSurface) { // null when destroying an activity programmatically mapbox-navigation-android/issues/503 nativeMapView.destroy(); nativeMapView = null; } if (mapRenderer != null) { mapRenderer.onDestroy(); } } /** * Returns if the map has been destroyed. *

* This method can be used to determine if the result of an asynchronous operation should be set. *

* * @return true, if the map has been destroyed */ @UiThread public boolean isDestroyed() { return destroyed; } @Override public boolean onTouchEvent(MotionEvent event) { if (!isGestureDetectorInitialized()) { return super.onTouchEvent(event); } 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) { if (!isGestureDetectorInitialized()) { return super.onGenericMotionEvent(event); } return mapGestureDetector.onGenericMotionEvent(event) || super.onGenericMotionEvent(event); } /** * You must call this method from the parent's Activity#onLowMemory() or Fragment#onLowMemory(). */ @UiThread public void onLowMemory() { if (nativeMapView != null) { nativeMapView.onLowMemory(); } } /** *

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

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