Skip to content

Commit

Permalink
Adds AndroidQ support for locationAlways
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Oct 10, 2019
1 parent 36ba8ee commit de399a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ private static int parseManifestName(String permission) {
case Manifest.permission.WRITE_CONTACTS:
case Manifest.permission.GET_ACCOUNTS:
return PERMISSION_GROUP_CONTACTS;
case Manifest.permission.ACCESS_BACKGROUND_LOCATION:
return PERMISSION_GROUP_LOCATION_ALWAYS;
case Manifest.permission.ACCESS_COARSE_LOCATION:
case Manifest.permission.ACCESS_FINE_LOCATION:
return PERMISSION_GROUP_LOCATION;
Expand Down Expand Up @@ -450,17 +452,20 @@ private void handlePermissionsRequest(String[] permissions, int[] grantResults)
if (!mRequestResults.containsKey(PERMISSION_GROUP_SPEECH)) {
mRequestResults.put(PERMISSION_GROUP_SPEECH, toPermissionStatus(grantResults[i]));
}
} else if (permission == PERMISSION_GROUP_LOCATION) {
final Context context = mRegistrar.activity() == null ? mRegistrar.activeContext() : mRegistrar.activity();
final boolean isLocationServiceEnabled = context != null && isLocationServiceEnabled(context);
@PermissionStatus int permissionStatus = toPermissionStatus(grantResults[i]);
if (permissionStatus == PERMISSION_STATUS_GRANTED && !isLocationServiceEnabled) {
permissionStatus = PERMISSION_STATUS_DISABLED;
}
} else if (permission == PERMISSION_GROUP_LOCATION_ALWAYS) {
@PermissionStatus int permissionStatus = determineActualLocationStatus(grantResults[i]);

if (!mRequestResults.containsKey(PERMISSION_GROUP_LOCATION_ALWAYS)) {
mRequestResults.put(PERMISSION_GROUP_LOCATION_ALWAYS, permissionStatus);
}
} else if (permission == PERMISSION_GROUP_LOCATION) {
@PermissionStatus int permissionStatus = determineActualLocationStatus(grantResults[i]);

if (VERSION.SDK_INT < VERSION_CODES.Q) {
if (!mRequestResults.containsKey(PERMISSION_GROUP_LOCATION_ALWAYS)) {
mRequestResults.put(PERMISSION_GROUP_LOCATION_ALWAYS, permissionStatus);
}
}

if (!mRequestResults.containsKey(PERMISSION_GROUP_LOCATION_WHEN_IN_USE)) {
mRequestResults.put(PERMISSION_GROUP_LOCATION_WHEN_IN_USE, permissionStatus);
Expand All @@ -475,6 +480,23 @@ private void handlePermissionsRequest(String[] permissions, int[] grantResults)
processResult();
}

/**
* Crosschecks a permission grant result with the location service availability.
*
* @param grantResult Grant Result as received from the Android system.
*/
@PermissionStatus
private int determineActualLocationStatus(int grantResult) {
final Context context =
mRegistrar.activity() == null ? mRegistrar.activeContext() : mRegistrar.activity();
final boolean isLocationServiceEnabled = context != null && isLocationServiceEnabled(context);
@PermissionStatus int permissionStatus = toPermissionStatus(grantResult);
if (permissionStatus == PERMISSION_STATUS_GRANTED && !isLocationServiceEnabled) {
permissionStatus = PERMISSION_STATUS_DISABLED;
}
return permissionStatus;
}

private void handleIgnoreBatteryOptimizationsRequest(boolean granted) {
if (mResult == null) {
return;
Expand Down Expand Up @@ -551,6 +573,9 @@ private List<String> getManifestNames(@PermissionGroup int permission) {
break;

case PERMISSION_GROUP_LOCATION_ALWAYS:
if (hasPermissionInManifest(Manifest.permission.ACCESS_BACKGROUND_LOCATION))
permissionNames.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);

case PERMISSION_GROUP_LOCATION_WHEN_IN_USE:
case PERMISSION_GROUP_LOCATION:
if (hasPermissionInManifest(Manifest.permission.ACCESS_COARSE_LOCATION))
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<!-- Permissions options for the `location` group -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<!-- Permissions options for the `microphone` or `speech` group -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Expand Down

0 comments on commit de399a7

Please sign in to comment.