Request user to turn on location for adding contact nearby on API 28+

This commit is contained in:
Torsten Grote
2021-03-29 11:30:17 -03:00
parent 1d44305e34
commit 4f3e4b019a
3 changed files with 42 additions and 3 deletions

View File

@@ -1,6 +1,8 @@
package org.briarproject.briar.android.contact.add.nearby;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.BaseActivity;
@@ -15,6 +17,7 @@ import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Build.VERSION.SDK_INT;
import static android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS;
import static androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale;
import static androidx.core.content.ContextCompat.checkSelfPermission;
import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListener;
@@ -45,6 +48,19 @@ class AddNearbyContactPermissionManager {
locationPermission = Permission.UNKNOWN;
}
/**
* @return true if location is enabled,
* or it isn't required due to this being a SDK < 28 device.
*/
static boolean isLocationEnabled(Context ctx) {
if (SDK_INT >= 28) {
LocationManager lm = ctx.getSystemService(LocationManager.class);
return lm.isLocationEnabled();
} else {
return true;
}
}
static boolean areEssentialPermissionsGranted(Context ctx,
boolean isBluetoothSupported) {
int ok = PERMISSION_GRANTED;
@@ -61,7 +77,8 @@ class AddNearbyContactPermissionManager {
}
boolean checkPermissions() {
if (areEssentialPermissionsGranted()) return true;
boolean locationEnabled = isLocationEnabled(ctx);
if (locationEnabled && areEssentialPermissionsGranted()) return true;
// If an essential permission has been permanently denied, ask the
// user to change the setting
if (cameraPermission == Permission.PERMANENTLY_DENIED) {
@@ -86,8 +103,10 @@ class AddNearbyContactPermissionManager {
} else if (locationPermission == Permission.SHOW_RATIONALE) {
showRationale(R.string.permission_location_title,
R.string.permission_location_request_body);
} else {
} else if (isLocationEnabled(ctx)) {
requestPermissions();
} else {
showLocationDialog(ctx);
}
return false;
}
@@ -113,6 +132,20 @@ class AddNearbyContactPermissionManager {
builder.show();
}
private static void showLocationDialog(Context ctx) {
AlertDialog.Builder builder =
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
builder.setTitle(R.string.permission_location_setting_title);
builder.setMessage(R.string.permission_location_setting_body);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.permission_location_setting_button,
(dialog, which) -> {
Intent i = new Intent(ACTION_LOCATION_SOURCE_SETTINGS);
ctx.startActivity(i);
});
builder.show();
}
private void requestPermissions() {
String[] permissions;
if (isBluetoothSupported) {

View File

@@ -83,6 +83,7 @@ import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager.areEssentialPermissionsGranted;
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager.isLocationEnabled;
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.REFUSED;
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.UNKNOWN;
@@ -365,7 +366,9 @@ class AddNearbyContactViewModel extends AndroidViewModel
void showQrCodeFragmentIfAllowed() {
boolean permissionsGranted = areEssentialPermissionsGranted(
getApplication(), isBluetoothSupported());
if (isActivityResumed && wasContinueClicked && permissionsGranted) {
boolean locationEnabled = isLocationEnabled(getApplication());
if (isActivityResumed && wasContinueClicked && permissionsGranted &&
locationEnabled) {
if (isWifiReady() && isBluetoothReady()) {
LOG.info("Wifi and Bluetooth are ready");
startAddingContact();

View File

@@ -609,6 +609,9 @@
<string name="permission_camera_location_request_body">To scan the QR code, Briar needs access to the camera.\n\nTo discover Bluetooth devices, Briar needs permission to access your location.\n\nBriar does not store your location or share it with anyone.</string>
<string name="permission_camera_denied_body">You have denied access to the camera, but adding contacts requires using the camera.\n\nPlease consider granting access.</string>
<string name="permission_location_denied_body">You have denied access to your location, but Briar needs this permission to discover Bluetooth devices.\n\nPlease consider granting access.</string>
<string name="permission_location_setting_title">Location setting</string>
<string name="permission_location_setting_body">Your device\'s location setting must be turned on to find other devices via Bluetooth. Please enable location to continue. You can disable it again afterwards.</string>
<string name="permission_location_setting_button">Enable location</string>
<string name="qr_code">QR code</string>
<string name="show_qr_code_fullscreen">Show QR code fullscreen</string>