Skip to content

Commit

Permalink
Report ignoreBatteryOptimizations permission correctly (#1200)
Browse files Browse the repository at this point in the history
* Report ignoreBatteryOptimizations permission correctly

* Report restricted status pre-marshmallow devices
  • Loading branch information
mvanbeusekom authored Oct 23, 2023
1 parent 7a0244d commit 992674f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 4 additions & 0 deletions permission_handler_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 12.0.1

* Fixes a bug where the `ignoreBatteryOptimizations` permission didn't report the correct status when the permission is requested and granted.

## 12.0.0

* **BREAKING CHANGES:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@

final class PermissionManager implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener {

@NonNull
private final Context context;
@Nullable
private RequestPermissionsSuccessCallback successCallback;

@Nullable
private Activity activity;

@NonNull
private final Context context;

/**
* The number of pending permission requests.
* <p>
Expand Down Expand Up @@ -77,14 +74,20 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
return false;
}

int status = resultCode == Activity.RESULT_OK
? PermissionConstants.PERMISSION_STATUS_GRANTED
: PermissionConstants.PERMISSION_STATUS_DENIED;

int permission;
int status, permission;

if (requestCode == PermissionConstants.PERMISSION_CODE_IGNORE_BATTERY_OPTIMIZATIONS) {
permission = PermissionConstants.PERMISSION_GROUP_IGNORE_BATTERY_OPTIMIZATIONS;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
status = (pm != null && pm.isIgnoringBatteryOptimizations(packageName))
? PermissionConstants.PERMISSION_STATUS_GRANTED
: PermissionConstants.PERMISSION_STATUS_DENIED;
} else {
status = PermissionConstants.PERMISSION_STATUS_RESTRICTED;
}
} else if (requestCode == PermissionConstants.PERMISSION_CODE_MANAGE_EXTERNAL_STORAGE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
status = Environment.isExternalStorageManager()
Expand Down Expand Up @@ -264,21 +267,6 @@ public boolean onRequestPermissionsResult(
return true;
}

@FunctionalInterface
interface RequestPermissionsSuccessCallback {
void onSuccess(Map<Integer, Integer> results);
}

@FunctionalInterface
interface CheckPermissionsSuccessCallback {
void onSuccess(@PermissionConstants.PermissionStatus int permissionStatus);
}

@FunctionalInterface
interface ShouldShowRequestPermissionRationaleSuccessCallback {
void onSuccess(boolean shouldShowRequestPermissionRationale);
}

/**
* Determines the permission status of the provided permission.
* <p>
Expand Down Expand Up @@ -666,4 +654,19 @@ private boolean isValidManifestForCalendarFullAccess() {
}
return true;
}

@FunctionalInterface
interface RequestPermissionsSuccessCallback {
void onSuccess(Map<Integer, Integer> results);
}

@FunctionalInterface
interface CheckPermissionsSuccessCallback {
void onSuccess(@PermissionConstants.PermissionStatus int permissionStatus);
}

@FunctionalInterface
interface ShouldShowRequestPermissionRationaleSuccessCallback {
void onSuccess(boolean shouldShowRequestPermissionRationale);
}
}
2 changes: 1 addition & 1 deletion permission_handler_android/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: permission_handler_android
description: Permission plugin for Flutter. This plugin provides the Android API to request and check permissions.
homepage: https://github.com/baseflow/flutter-permission-handler
version: 12.0.0
version: 12.0.1

environment:
sdk: ">=2.15.0 <4.0.0"
Expand Down

0 comments on commit 992674f

Please sign in to comment.