Skip to content

Commit

Permalink
feat: Adding config to disable location permission alert
Browse files Browse the repository at this point in the history
  • Loading branch information
pandrade-ciandt committed Dec 4, 2020
1 parent 1336c1c commit 5f507b2
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/android/LocationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public class LocationManager extends CordovaPlugin implements BeaconConsumer {
private static final boolean DEFAULT_ENABLE_ARMA_FILTER = false;
private static final String REQUEST_BT_PERMISSION_NAME = "com.unarin.cordova.beacon.android.altbeacon.RequestBtPermission";
private static final boolean DEFAULT_REQUEST_BT_PERMISSION = true;
private static final String ENABLE_LOCATION_PERMISSION_ALERT = "com.unarin.cordova.beacon.android.altbeacon.EnableLocationPermissionAlert";
private static final boolean DEFAULT_ENABLE_LOCATION_PERMISSION_ALERT = true;
private static final int DEFAULT_FOREGROUND_SCAN_PERIOD = 1100;
private static int CDV_LOCATION_MANAGER_DOM_DELEGATE_TIMEOUT = 30;
private static final int BUILD_VERSION_CODES_M = 23;
Expand Down Expand Up @@ -315,32 +317,37 @@ private void tryToRequestMarshmallowLocationPermission() {
"supported version of Android.");
return;
}

final boolean enableLocationPermissionAlert = this.preferences.getBoolean(
ENABLE_LOCATION_PERMISSION_ALERT, DEFAULT_ENABLE_LOCATION_PERMISSION_ALERT);

if (enableLocationPermissionAlert) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@SuppressLint("NewApi")
@Override
public void onDismiss(final DialogInterface dialog) {

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@SuppressLint("NewApi")
@Override
public void onDismiss(final DialogInterface dialog) {

try {
requestPermissionsMethod.invoke(activity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION
);
} catch (IllegalAccessException e) {
Log.e(TAG, "IllegalAccessException while requesting permission for " +
"ACCESS_COARSE_LOCATION:", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "InvocationTargetException while requesting permission for " +
"ACCESS_COARSE_LOCATION:", e);
try {
requestPermissionsMethod.invoke(activity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION
);
} catch (IllegalAccessException e) {
Log.e(TAG, "IllegalAccessException while requesting permission for " +
"ACCESS_COARSE_LOCATION:", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "InvocationTargetException while requesting permission for " +
"ACCESS_COARSE_LOCATION:", e);
}
}
}
});
});

builder.show();
builder.show();
}

} catch (final IllegalAccessException e) {
Log.w(TAG, "IllegalAccessException while checking for ACCESS_COARSE_LOCATION:", e);
Expand Down

0 comments on commit 5f507b2

Please sign in to comment.