diff options
Diffstat (limited to 'platform')
3 files changed, 23 insertions, 23 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 09056ee123..772b62e990 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -2313,6 +2313,11 @@ public class MapView extends FrameLayout { // User location // + boolean isPermissionsAccepted() { + return (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) || + ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; + } + /** * <p> * Enables or disables the my-location layer. @@ -2328,12 +2333,6 @@ public class MapView extends FrameLayout { */ @UiThread void setMyLocationEnabled(boolean enabled) { - if (!(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) && - !(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) { - Log.e(TAG, "Could not activate user location tracking: " + - "user did not accept the permission or permissions were not requested."); - return; - } mUserLocationView.setEnabled(enabled); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index a402866cea..e22499c56b 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -3,14 +3,15 @@ package com.mapbox.mapboxsdk.maps; import android.Manifest; import android.content.Context; +import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.os.SystemClock; import android.support.annotation.FloatRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.RequiresPermission; import android.support.annotation.UiThread; +import android.support.v4.content.ContextCompat; import android.support.v4.util.LongSparseArray; import android.text.TextUtils; import android.util.Log; @@ -19,7 +20,6 @@ import android.view.View; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; import com.mapbox.mapboxsdk.annotations.Icon; -import com.mapbox.mapboxsdk.annotations.IconFactory; import com.mapbox.mapboxsdk.annotations.InfoWindow; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; @@ -32,13 +32,11 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.constants.Style; -import com.mapbox.mapboxsdk.exceptions.IconBitmapChangedException; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.layers.CustomLayer; import com.mapbox.mapboxsdk.utils.ApiAccess; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; @@ -1260,6 +1258,11 @@ public class MapboxMap { */ @UiThread public void setMyLocationEnabled(boolean enabled) { + if (!mMapView.isPermissionsAccepted()) { + Log.e(MapboxConstants.TAG, "Could not activate user location tracking: " + + "user did not accept the permission or permissions were not requested."); + return; + } mMyLocationEnabled = enabled; mMapView.setMyLocationEnabled(enabled); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java index 5a6a2b5f8b..3781a98ffe 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java @@ -1,7 +1,10 @@ package com.mapbox.mapboxsdk.maps; +import android.Manifest; +import android.content.pm.PackageManager; import android.graphics.Color; import android.graphics.Point; +import android.support.v4.content.ContextCompat; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; @@ -61,7 +64,7 @@ public class MapboxMapTest { } @Test - public void testGetMapView(){ + public void testGetMapView() { assertNotNull("MapView should be non null", mMapboxMap.getMapView()); } @@ -106,22 +109,17 @@ public class MapboxMapTest { @Test public void testMyLocationEnabled() { - try { - mMapboxMap.setMyLocationEnabled(true); - assertTrue("MyLocationEnabled should be true", mMapboxMap.isMyLocationEnabled()); - } catch (SecurityException e) { - assertTrue(false); - } + when(mMapView.isPermissionsAccepted()).thenReturn(true); + mMapboxMap.setMyLocationEnabled(true); + assertTrue("MyLocationEnabled should be true", mMapboxMap.isMyLocationEnabled()); + } @Test public void testMyLocationDisabled() { - try { - mMapboxMap.setMyLocationEnabled(false); - assertFalse("MyLocationEnabled should be false", mMapboxMap.isMyLocationEnabled()); - } catch (SecurityException e) { - assertTrue(false); - } + when(mMapView.isPermissionsAccepted()).thenReturn(true); + mMapboxMap.setMyLocationEnabled(false); + assertFalse("MyLocationEnabled should be false", mMapboxMap.isMyLocationEnabled()); } // |