diff --git a/permission_handler_android/CHANGELOG.md b/permission_handler_android/CHANGELOG.md index 2a1134b83..88765c28d 100644 --- a/permission_handler_android/CHANGELOG.md +++ b/permission_handler_android/CHANGELOG.md @@ -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:** diff --git a/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java b/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java index 985b27d04..504ef5789 100644 --- a/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java +++ b/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java @@ -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. *
@@ -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) {
+ return false;
+ }
+
+ 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 if (requestCode == PermissionConstants.PERMISSION_CODE_MANAGE_EXTERNAL_STORAGE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
status = Environment.isExternalStorageManager()
@@ -264,21 +267,6 @@ public boolean onRequestPermissionsResult(
return true;
}
- @FunctionalInterface
- interface RequestPermissionsSuccessCallback {
- void onSuccess(Map
@@ -666,4 +654,19 @@ private boolean isValidManifestForCalendarFullAccess() {
}
return true;
}
+
+ @FunctionalInterface
+ interface RequestPermissionsSuccessCallback {
+ void onSuccess(Map