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 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. *

@@ -666,4 +654,19 @@ private boolean isValidManifestForCalendarFullAccess() { } return true; } + + @FunctionalInterface + interface RequestPermissionsSuccessCallback { + void onSuccess(Map results); + } + + @FunctionalInterface + interface CheckPermissionsSuccessCallback { + void onSuccess(@PermissionConstants.PermissionStatus int permissionStatus); + } + + @FunctionalInterface + interface ShouldShowRequestPermissionRationaleSuccessCallback { + void onSuccess(boolean shouldShowRequestPermissionRationale); + } } diff --git a/permission_handler_android/pubspec.yaml b/permission_handler_android/pubspec.yaml index 84580dbe2..701944a58 100644 --- a/permission_handler_android/pubspec.yaml +++ b/permission_handler_android/pubspec.yaml @@ -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"