From aae777522ff0c275a060d530c2932737799f39e3 Mon Sep 17 00:00:00 2001 From: Sarah Lensing Date: Fri, 14 Oct 2016 09:57:02 -0600 Subject: [PATCH] Dont require bluetooth permission (#128) * Dont require bluetooth permission * reduce params in LocationSettingsResultRequest --- .../LocationSettingsResultRequest.java | 20 ++++++--- .../lost/internal/SettingsApiImpl.java | 8 +--- .../LocationSettingsResultRequestTest.java | 42 +++++++------------ .../lost/internal/SettingsApiImplTest.java | 27 ++++++++++-- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/LocationSettingsResultRequest.java b/lost/src/main/java/com/mapzen/android/lost/internal/LocationSettingsResultRequest.java index c8f085b..f1f8ac6 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/LocationSettingsResultRequest.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/LocationSettingsResultRequest.java @@ -8,8 +8,10 @@ import com.mapzen.android.lost.api.ResultCallback; import com.mapzen.android.lost.api.Status; +import android.Manifest; import android.app.PendingIntent; import android.bluetooth.BluetoothAdapter; +import android.content.Context; import android.content.pm.PackageManager; import android.location.LocationManager; import android.support.annotation.NonNull; @@ -31,6 +33,7 @@ public class LocationSettingsResultRequest extends PendingResult { + private final Context context; private final BluetoothAdapter bluetoothAdapter; private final PackageManager packageManager; private final LocationManager locationManager; @@ -40,11 +43,12 @@ public class LocationSettingsResultRequest extends PendingResult future; - public LocationSettingsResultRequest(BluetoothAdapter btAdapter, PackageManager pm, - LocationManager lm, PendingIntentGenerator generator, LocationSettingsRequest request) { + public LocationSettingsResultRequest(Context context, BluetoothAdapter btAdapter, + PendingIntentGenerator generator, LocationSettingsRequest request) { + this.context = context; + packageManager = context.getPackageManager(); + locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); bluetoothAdapter = btAdapter; - packageManager = pm; - locationManager = lm; pendingIntentGenerator = generator; settingsRequest = request; } @@ -109,7 +113,13 @@ private LocationSettingsResult generateLocationSettingsResult() { boolean networkUsable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean networkPresent = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK); - boolean bleUsable = bluetoothAdapter != null && bluetoothAdapter.isEnabled(); + + + String pkgName = context.getPackageName(); + boolean hasBtPermission = packageManager.checkPermission( + Manifest.permission.BLUETOOTH, pkgName) == PackageManager.PERMISSION_GRANTED; + boolean bleUsable = hasBtPermission && bluetoothAdapter != null + && bluetoothAdapter.isEnabled(); boolean blePresent = packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE); boolean hasGpsResolution = needGps && gpsPresent && !gpsUsable; diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/SettingsApiImpl.java b/lost/src/main/java/com/mapzen/android/lost/internal/SettingsApiImpl.java index 0b266bc..5b5e95c 100644 --- a/lost/src/main/java/com/mapzen/android/lost/internal/SettingsApiImpl.java +++ b/lost/src/main/java/com/mapzen/android/lost/internal/SettingsApiImpl.java @@ -8,8 +8,6 @@ import android.bluetooth.BluetoothAdapter; import android.content.Context; -import android.content.pm.PackageManager; -import android.location.LocationManager; public class SettingsApiImpl implements SettingsApi { @@ -40,11 +38,7 @@ public SettingsApiImpl(Context context, BluetoothAdapter adapter) { @Override public PendingResult checkLocationSettings(LostApiClient client, LocationSettingsRequest request) { - PackageManager pm = context.getPackageManager(); - LocationManager locationManager = - (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); PendingIntentGenerator generator = new PendingIntentGenerator(context); - return new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - request); + return new LocationSettingsResultRequest(context, bluetoothAdapter, generator, request); } } diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java index 4bf333a..742ef3f 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/LocationSettingsResultRequestTest.java @@ -39,6 +39,9 @@ locationManager = Mockito.mock(LocationManager.class); generator = new TestPendingIntentGenerator(context); + when(context.getPackageManager()).thenReturn(pm); + when(context.getSystemService(Context.LOCATION_SERVICE)).thenReturn(locationManager); + ArrayList requests = new ArrayList<>(); LocationRequest highAccuracy = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //gps + wifi @@ -48,8 +51,7 @@ .build(); resultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - request); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, request); } @Test public void await_shouldReturnSuccessfulResult() { @@ -177,8 +179,7 @@ .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); LocationSettingsResult result = settingsResultRequest.await(); assertThat(result.getStatus().getStatusCode()).isEqualTo(Status.RESOLUTION_REQUIRED); @@ -201,8 +202,7 @@ .setNeedBle(true) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); LocationSettingsResult result = settingsResultRequest.await(); assertThat(result.getStatus().getStatusCode()).isEqualTo(Status.RESOLUTION_REQUIRED); @@ -226,8 +226,7 @@ .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); LocationSettingsResult result = settingsResultRequest.await(); assertThat(result.getStatus().getStatusCode()).isEqualTo(Status.SUCCESS); @@ -247,8 +246,7 @@ .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); LocationSettingsResult result = settingsResultRequest.await(); assertThat(result.getStatus().getStatusCode()).isEqualTo(Status.SUCCESS); @@ -268,8 +266,7 @@ .setNeedBle(true) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); LocationSettingsResult result = settingsResultRequest.await(); assertThat(result.getStatus().getStatusCode()).isEqualTo(Status.RESOLUTION_REQUIRED); @@ -282,8 +279,7 @@ DelayTestPendingIntentGenerator delayedGenerator = new DelayTestPendingIntentGenerator(context); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, delayedGenerator, - request); + new LocationSettingsResultRequest(context, bluetoothAdapter, delayedGenerator, request); LocationSettingsResult resultRequest = settingsResultRequest.await(1000, TimeUnit.MILLISECONDS); assertThat(resultRequest.getStatus().getStatusCode()).isEqualTo(Status.TIMEOUT); @@ -433,8 +429,7 @@ public void setResultCallback_balancedLowPower_noBle_shouldReturnStatusResolutio .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { @@ -461,8 +456,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut .setNeedBle(true) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { @@ -489,8 +483,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { @@ -513,8 +506,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut .setNeedBle(false) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { @@ -537,8 +529,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut .setNeedBle(true) .build(); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator, - settingsRequest); + new LocationSettingsResultRequest(context, bluetoothAdapter, generator, settingsRequest); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { @@ -554,8 +545,7 @@ public void setResultCallback_balancedLowPower_needBle_shouldReturnStatusResolut DelayTestPendingIntentGenerator delayedGenerator = new DelayTestPendingIntentGenerator(context); LocationSettingsResultRequest settingsResultRequest = - new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, delayedGenerator, - request); + new LocationSettingsResultRequest(context, bluetoothAdapter, delayedGenerator, request); settingsResultRequest.setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull LocationSettingsResult result) { diff --git a/lost/src/test/java/com/mapzen/android/lost/internal/SettingsApiImplTest.java b/lost/src/test/java/com/mapzen/android/lost/internal/SettingsApiImplTest.java index 0f0fc2c..a81d0f0 100644 --- a/lost/src/test/java/com/mapzen/android/lost/internal/SettingsApiImplTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/internal/SettingsApiImplTest.java @@ -7,12 +7,17 @@ import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.content.Context; +import android.content.pm.PackageManager; +import android.location.LocationManager; import static org.fest.assertions.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class SettingsApiImplTest { @@ -20,8 +25,8 @@ public class SettingsApiImplTest { SettingsApiImpl settingsApi; @Before public void setup() { - context = Mockito.mock(Context.class); - BluetoothAdapter adapter = Mockito.mock(BluetoothAdapter.class); + context = mock(Context.class); + BluetoothAdapter adapter = mock(BluetoothAdapter.class); settingsApi = new SettingsApiImpl(context, adapter); } @@ -33,4 +38,20 @@ public class SettingsApiImplTest { settingsApi.checkLocationSettings(apiClient, request); assertThat(result).isInstanceOf(LocationSettingsResultRequest.class); } + + @Test public void checkLocationSettings_shouldNotRequireBluetoothPermission() { + PackageManager packageManager = mock(PackageManager.class); + String pkgName = "com.test.pkg"; + LocationManager locationManager = mock(LocationManager.class); + when(context.getPackageName()).thenReturn(pkgName); + when(context.getPackageManager()).thenReturn(packageManager); + when(context.getSystemService(Context.LOCATION_SERVICE)).thenReturn(locationManager); + + LocationSettingsRequest request = new LocationSettingsRequest.Builder().build(); + LostApiClient apiClient = new LostApiClient.Builder(context).build(); + PendingResult result = + settingsApi.checkLocationSettings(apiClient, request); + result.await(); + verify(packageManager).checkPermission(Manifest.permission.BLUETOOTH, pkgName); + } }