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

Commit

Permalink
Dont require bluetooth permission (#128)
Browse files Browse the repository at this point in the history
* Dont require bluetooth permission

* reduce params in LocationSettingsResultRequest
  • Loading branch information
sarahsnow1 authored and ecgreb committed Oct 14, 2016
1 parent a7f2084 commit aae7775
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,7 @@

public class LocationSettingsResultRequest extends PendingResult<LocationSettingsResult> {

private final Context context;
private final BluetoothAdapter bluetoothAdapter;
private final PackageManager packageManager;
private final LocationManager locationManager;
Expand All @@ -40,11 +43,12 @@ public class LocationSettingsResultRequest extends PendingResult<LocationSetting

Future<LocationSettingsResult> 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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -40,11 +38,7 @@ public SettingsApiImpl(Context context, BluetoothAdapter adapter) {
@Override
public PendingResult<LocationSettingsResult> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocationRequest> requests = new ArrayList<>();
LocationRequest highAccuracy =
LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //gps + wifi
Expand All @@ -48,8 +51,7 @@
.build();

resultRequest =
new LocationSettingsResultRequest(bluetoothAdapter, pm, locationManager, generator,
request);
new LocationSettingsResultRequest(context, bluetoothAdapter, generator, request);
}

@Test public void await_shouldReturnSuccessfulResult() {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand All @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand All @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand All @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand All @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand All @@ -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<LocationSettingsResult>() {
@Override public void onResult(@NonNull LocationSettingsResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@

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 {

Context context;
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);
}

Expand All @@ -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<LocationSettingsResult> result =
settingsApi.checkLocationSettings(apiClient, request);
result.await();
verify(packageManager).checkPermission(Manifest.permission.BLUETOOTH, pkgName);
}
}

0 comments on commit aae7775

Please sign in to comment.