From f1f63455f535421f27f0ec79c694791a78594500 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 29 Oct 2024 14:56:16 -0700 Subject: [PATCH] (Android) Add background battery usage restriction --- CHANGELOG.md | 2 ++ .../co/airbitz/AbcCoreJsUi/AbcCoreJsUiModule.java | 12 +++++++++--- src/util/notificationPermissionReminder.tsx | 8 +++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e26c7989..51ed51f00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- added: (Android) Detect if user restricted background battery usage + ## 3.23.0 (2024-11-01) - changed: Changed semantics for invalid password login error message: "Invalid username or password" diff --git a/android/src/main/java/co/airbitz/AbcCoreJsUi/AbcCoreJsUiModule.java b/android/src/main/java/co/airbitz/AbcCoreJsUi/AbcCoreJsUiModule.java index 467159492..9e11724a7 100644 --- a/android/src/main/java/co/airbitz/AbcCoreJsUi/AbcCoreJsUiModule.java +++ b/android/src/main/java/co/airbitz/AbcCoreJsUi/AbcCoreJsUiModule.java @@ -2,6 +2,7 @@ import android.Manifest; import android.app.Activity; +import android.app.ActivityManager; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.fingerprint.FingerprintManager; @@ -392,10 +393,15 @@ public void getSupportedBiometryType(Promise promise) { } } - // Function should only be used in iOS and not available on android. - // Resolve with a unavailable if ever, meaning not available on this device @ReactMethod public void backgroundAppRefreshStatus(Promise promise) { - promise.resolve("unavailable"); + if (android.os.Build.VERSION.SDK_INT >= 28) { + ActivityManager activityManager = + (ActivityManager) getReactApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); + String backgroundStatus = activityManager.isBackgroundRestricted() ? "blocked" : "granted"; + promise.resolve(backgroundStatus); + } else { + promise.resolve("unavailable"); + } } } diff --git a/src/util/notificationPermissionReminder.tsx b/src/util/notificationPermissionReminder.tsx index 1f0662032..88a9cbbe0 100644 --- a/src/util/notificationPermissionReminder.tsx +++ b/src/util/notificationPermissionReminder.tsx @@ -79,11 +79,9 @@ export const showNotificationPermissionReminder = async ( ? 1 : 0 - const statusAppRefresh = isIos - ? await AbcCoreJsUi.backgroundAppRefreshStatus().catch((error: undefined) => - console.log(error) - ) - : undefined + const statusAppRefresh = await AbcCoreJsUi.backgroundAppRefreshStatus().catch( + (error: unknown) => console.log(error) + ) const refreshEnabled = statusAppRefresh !== RESULTS.BLOCKED ? 1 : 0 const notificationPermissionsInfoJson = await disklet