From b388904ebbaebd7a0ed80da778de9e842996070e Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Wed, 31 Aug 2022 12:50:43 +0200 Subject: [PATCH] fix: Fix wakelock being acquired but not released Based on https://github.com/ekasetiawans/flutter_background_service/commit/e427f3b70138ec26f9671c2617f9061f25eade6f and https://github.com/ekasetiawans/flutter_background_service/commit/6c353dd4fc65818ffde3fdcd8364604ceaff569b. Also bump version. --- src/moxplatform/pubspec.yaml | 6 +++--- .../BackgroundService.java | 21 +++++++++++++++---- .../moxplatform_android/BootReceiver.java | 11 +++++++++- .../moxplatform_android/Constants.java | 6 ++++++ .../lib/isolate_android.dart | 7 ++++--- src/moxplatform_android/pubspec.yaml | 6 +++--- src/moxplatform_generic/pubspec.yaml | 6 +++--- src/moxplatform_linux/pubspec.yaml | 4 ++-- .../pubspec.yaml | 4 ++-- 9 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.java diff --git a/src/moxplatform/pubspec.yaml b/src/moxplatform/pubspec.yaml index 489594f..5a2038e 100644 --- a/src/moxplatform/pubspec.yaml +++ b/src/moxplatform/pubspec.yaml @@ -1,6 +1,6 @@ name: moxplatform description: Moxxy platform-specific code -version: 0.1.10 +version: 0.1.11 publish_to: https://git.polynom.me/api/packages/Moxxy/pub environment: @@ -21,10 +21,10 @@ dependencies: moxplatform_android: hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.10 + version: 0.1.11 moxplatform_platform_interface: hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.10 + version: 0.1.11 moxlib: hosted: https://git.polynom.me/api/packages/Moxxy/pub diff --git a/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java index aaec5d0..78e4d0f 100644 --- a/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java +++ b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java @@ -35,7 +35,7 @@ import io.flutter.view.FlutterCallbackInformation; import io.flutter.view.FlutterMain; public class BackgroundService extends Service implements MethodChannel.MethodCallHandler { - private static String TAG = "BackgroundService"; + private static final String TAG = "BackgroundService"; private static final String manuallyStoppedKey = "manually_stopped"; private static final String backgroundMethodChannelKey = MoxplatformAndroidPlugin.methodChannelKey + "_bg"; /// The [FlutterEngine] executing the background service @@ -48,12 +48,12 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa private AtomicBoolean isRunning = new AtomicBoolean(false); private static final String WAKE_LOCK_NAME = BackgroundService.class.getName() + ".Lock"; - private static volatile PowerManager.WakeLock wakeLock = null; + public static volatile PowerManager.WakeLock wakeLock = null; /// Notification data private String notificationBody = "Preparing..."; private static final String notificationTitle = "Moxxy"; - synchronized private static PowerManager.WakeLock getLock(Context context) { + synchronized public static PowerManager.WakeLock getLock(Context context) { if (wakeLock == null) { PowerManager mgr = (PowerManager) context .getSystemService(Context.POWER_SERVICE); @@ -137,6 +137,13 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa try { if (isRunning.get() || (engine != null && !engine.getDartExecutor().isExecutingDart())) return; + if (wakeLock == null) { + + Log.d(TAG, "Wakelock is null. Acquiring it..."); + getLock(getApplicationContext()).acquire(MoxplatformConstants.WAKE_LOCK_DURATION); + Log.d(TAG, "Wakelock acquired..."); + } + updateNotificationInfo(); // Initialize Flutter if it's not already @@ -176,6 +183,13 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa @Override public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { + if (wakeLock != null) { + Log.d(TAG, "Wakelock is not null. Releasing it..."); + wakeLock.release(); + wakeLock = null; + Log.d(TAG, "Wakelock released..."); + } + switch (call.method) { case "getHandler": result.success(MoxplatformAndroidPlugin.getHandle(this)); @@ -255,7 +269,6 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa setManuallyStopped(this,false); enqueue(this); runService(); - getLock(getApplicationContext()).acquire(); return START_STICKY; } diff --git a/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BootReceiver.java b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BootReceiver.java index 2e40784..372d141 100644 --- a/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BootReceiver.java +++ b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BootReceiver.java @@ -4,16 +4,25 @@ package me.polynom.moxplatform_android; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.util.Log; import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat; public class BootReceiver extends BroadcastReceiver { + private final String TAG = "BootReceiver"; + @Override public void onReceive(Context context, Intent intent) { if (MoxplatformAndroidPlugin.getStartAtBoot(context)) { + if (BackgroundService.wakeLock == null) { + Log.d(TAG, "Wakelock is null. Acquiring it..."); + BackgroundService.getLock(context).acquire(MoxplatformConstants.WAKE_LOCK_DURATION); + Log.d(TAG, "Wakelock acquired..."); + } + ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class)); } } -} \ No newline at end of file +} diff --git a/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.java b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.java new file mode 100644 index 0000000..5414a8e --- /dev/null +++ b/src/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.java @@ -0,0 +1,6 @@ +package me.polynom.moxplatform_android; + +class MoxplatformConstants { + // https://github.com/ekasetiawans/flutter_background_service/blob/e427f3b70138ec26f9671c2617f9061f25eade6f/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BootReceiver.java#L20 + public static final long WAKE_LOCK_DURATION = 10*60*1000L; +} diff --git a/src/moxplatform_android/lib/isolate_android.dart b/src/moxplatform_android/lib/isolate_android.dart index eab4e9b..fafe632 100644 --- a/src/moxplatform_android/lib/isolate_android.dart +++ b/src/moxplatform_android/lib/isolate_android.dart @@ -23,6 +23,7 @@ class BackgroundServiceDataSender extends AwaitableDataSender androidEntrypoint() async { print("androidEntrypoint: Called on new FlutterEngine"); WidgetsFlutterBinding.ensureInitialized(); @@ -76,10 +77,10 @@ class AndroidIsolateHandler extends IsolateHandler { _log.finest("Called start"); WidgetsFlutterBinding.ensureInitialized(); - final androidEntryHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle(); - _log.finest('AndroidEntryHandle: $androidEntryHandle'); + final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle(); + _log.finest('androidEntrypointHandle: $androidEntrypointHandle'); await _channel.invokeMethod("configure", [ - androidEntryHandle, + androidEntrypointHandle, jsonEncode({ "genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(), "eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle() diff --git a/src/moxplatform_android/pubspec.yaml b/src/moxplatform_android/pubspec.yaml index a85e860..78524e0 100644 --- a/src/moxplatform_android/pubspec.yaml +++ b/src/moxplatform_android/pubspec.yaml @@ -1,6 +1,6 @@ name: moxplatform_android description: Android implementation of moxplatform -version: 0.1.10 +version: 0.1.11 homepage: https://codeberg.org/moxxy/moxplatform publish_to: https://git.polynom.me/api/packages/Moxxy/pub @@ -22,10 +22,10 @@ dependencies: sdk: flutter moxplatform_platform_interface: hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.10 + version: 0.1.11 moxplatform: hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.10 + version: 0.1.11 moxlib: hosted: https://git.polynom.me/api/packages/Moxxy/pub diff --git a/src/moxplatform_generic/pubspec.yaml b/src/moxplatform_generic/pubspec.yaml index 1f74c20..6bc7d3e 100644 --- a/src/moxplatform_generic/pubspec.yaml +++ b/src/moxplatform_generic/pubspec.yaml @@ -1,6 +1,6 @@ name: moxplatform_generic description: Generic implementations of moxplatform -version: 0.1.10 +version: 0.1.11 homepage: https://git.polynom.me/api/packages/Moxxy/pub environment: @@ -14,11 +14,11 @@ dependencies: moxplatform: path: ../moxplatform #hosted: https://git.polynom.me/api/packages/Moxxy/pub - #version: 0.1.10 + #version: 0.1.11 moxplatform_platform_interface: path: ../moxplatform_platform_interface #hosted: https://git.polynom.me/api/packages/Moxxy/pub - #version: 0.1.10 + #version: 0.1.11 moxlib: hosted: https://git.polynom.me/api/packages/Moxxy/pub diff --git a/src/moxplatform_linux/pubspec.yaml b/src/moxplatform_linux/pubspec.yaml index 47dbb53..e480b3b 100644 --- a/src/moxplatform_linux/pubspec.yaml +++ b/src/moxplatform_linux/pubspec.yaml @@ -1,6 +1,6 @@ name: moxplatform_linux description: Linux-specific implementation of moxplatform -version: 0.1.10 +version: 0.1.11 homepage: https://codeberg.org/moxxy/moxplatform environment: @@ -14,7 +14,7 @@ dependencies: moxplatform: path: ../moxplatform #hosted: https://git.polynom.me/api/packages/Moxxy/pub - #version: 0.1.10 + #version: 0.1.11 moxplatform_generic: path: ../moxplatform_generic diff --git a/src/moxplatform_platform_interface/pubspec.yaml b/src/moxplatform_platform_interface/pubspec.yaml index 8bd3ea2..84f8905 100644 --- a/src/moxplatform_platform_interface/pubspec.yaml +++ b/src/moxplatform_platform_interface/pubspec.yaml @@ -1,6 +1,6 @@ name: moxplatform_platform_interface description: A common platform interface for the my_plugin plugin. -version: 0.1.10 +version: 0.1.11 homepage: https://codeberg.org/moxxy/moxplatform publish_to: https://git.polynom.me/api/packages/Moxxy/pub @@ -14,7 +14,7 @@ dependencies: moxplatform: hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.10 + version: 0.1.11 moxlib: hosted: https://git.polynom.me/api/packages/Moxxy/pub