fix: Fix wakelock being acquired but not released

Based on e427f3b701
and
6c353dd4fc.

Also bump version.
This commit is contained in:
PapaTutuWawa 2022-08-31 12:50:43 +02:00
parent 90d11e3191
commit b388904ebb
9 changed files with 50 additions and 21 deletions

View File

@ -1,6 +1,6 @@
name: moxplatform name: moxplatform
description: Moxxy platform-specific code description: Moxxy platform-specific code
version: 0.1.10 version: 0.1.11
publish_to: https://git.polynom.me/api/packages/Moxxy/pub publish_to: https://git.polynom.me/api/packages/Moxxy/pub
environment: environment:
@ -21,10 +21,10 @@ dependencies:
moxplatform_android: moxplatform_android:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.10 version: 0.1.11
moxplatform_platform_interface: moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.10 version: 0.1.11
moxlib: moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub

View File

@ -35,7 +35,7 @@ import io.flutter.view.FlutterCallbackInformation;
import io.flutter.view.FlutterMain; import io.flutter.view.FlutterMain;
public class BackgroundService extends Service implements MethodChannel.MethodCallHandler { 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 manuallyStoppedKey = "manually_stopped";
private static final String backgroundMethodChannelKey = MoxplatformAndroidPlugin.methodChannelKey + "_bg"; private static final String backgroundMethodChannelKey = MoxplatformAndroidPlugin.methodChannelKey + "_bg";
/// The [FlutterEngine] executing the background service /// 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 AtomicBoolean isRunning = new AtomicBoolean(false);
private static final String WAKE_LOCK_NAME = BackgroundService.class.getName() + ".Lock"; 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 /// Notification data
private String notificationBody = "Preparing..."; private String notificationBody = "Preparing...";
private static final String notificationTitle = "Moxxy"; 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) { if (wakeLock == null) {
PowerManager mgr = (PowerManager) context PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE); .getSystemService(Context.POWER_SERVICE);
@ -137,6 +137,13 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
try { try {
if (isRunning.get() || (engine != null && !engine.getDartExecutor().isExecutingDart())) return; 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(); updateNotificationInfo();
// Initialize Flutter if it's not already // Initialize Flutter if it's not already
@ -176,6 +183,13 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
@Override @Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { 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) { switch (call.method) {
case "getHandler": case "getHandler":
result.success(MoxplatformAndroidPlugin.getHandle(this)); result.success(MoxplatformAndroidPlugin.getHandle(this));
@ -255,7 +269,6 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
setManuallyStopped(this,false); setManuallyStopped(this,false);
enqueue(this); enqueue(this);
runService(); runService();
getLock(getApplicationContext()).acquire();
return START_STICKY; return START_STICKY;
} }

View File

@ -4,16 +4,25 @@ package me.polynom.moxplatform_android;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
private final String TAG = "BootReceiver";
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (MoxplatformAndroidPlugin.getStartAtBoot(context)) { 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)); ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
} }
} }
} }

View File

@ -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;
}

View File

@ -23,6 +23,7 @@ class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand,
} }
} }
@pragma('vm:entry-point')
Future<void> androidEntrypoint() async { Future<void> androidEntrypoint() async {
print("androidEntrypoint: Called on new FlutterEngine"); print("androidEntrypoint: Called on new FlutterEngine");
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@ -76,10 +77,10 @@ class AndroidIsolateHandler extends IsolateHandler {
_log.finest("Called start"); _log.finest("Called start");
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
final androidEntryHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle(); final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
_log.finest('AndroidEntryHandle: $androidEntryHandle'); _log.finest('androidEntrypointHandle: $androidEntrypointHandle');
await _channel.invokeMethod("configure", [ await _channel.invokeMethod("configure", [
androidEntryHandle, androidEntrypointHandle,
jsonEncode({ jsonEncode({
"genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(), "genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
"eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle() "eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()

View File

@ -1,6 +1,6 @@
name: moxplatform_android name: moxplatform_android
description: Android implementation of moxplatform description: Android implementation of moxplatform
version: 0.1.10 version: 0.1.11
homepage: https://codeberg.org/moxxy/moxplatform homepage: https://codeberg.org/moxxy/moxplatform
publish_to: https://git.polynom.me/api/packages/Moxxy/pub publish_to: https://git.polynom.me/api/packages/Moxxy/pub
@ -22,10 +22,10 @@ dependencies:
sdk: flutter sdk: flutter
moxplatform_platform_interface: moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.10 version: 0.1.11
moxplatform: moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.10 version: 0.1.11
moxlib: moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub

View File

@ -1,6 +1,6 @@
name: moxplatform_generic name: moxplatform_generic
description: Generic implementations of moxplatform description: Generic implementations of moxplatform
version: 0.1.10 version: 0.1.11
homepage: https://git.polynom.me/api/packages/Moxxy/pub homepage: https://git.polynom.me/api/packages/Moxxy/pub
environment: environment:
@ -14,11 +14,11 @@ dependencies:
moxplatform: moxplatform:
path: ../moxplatform path: ../moxplatform
#hosted: https://git.polynom.me/api/packages/Moxxy/pub #hosted: https://git.polynom.me/api/packages/Moxxy/pub
#version: 0.1.10 #version: 0.1.11
moxplatform_platform_interface: moxplatform_platform_interface:
path: ../moxplatform_platform_interface path: ../moxplatform_platform_interface
#hosted: https://git.polynom.me/api/packages/Moxxy/pub #hosted: https://git.polynom.me/api/packages/Moxxy/pub
#version: 0.1.10 #version: 0.1.11
moxlib: moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub

View File

@ -1,6 +1,6 @@
name: moxplatform_linux name: moxplatform_linux
description: Linux-specific implementation of moxplatform description: Linux-specific implementation of moxplatform
version: 0.1.10 version: 0.1.11
homepage: https://codeberg.org/moxxy/moxplatform homepage: https://codeberg.org/moxxy/moxplatform
environment: environment:
@ -14,7 +14,7 @@ dependencies:
moxplatform: moxplatform:
path: ../moxplatform path: ../moxplatform
#hosted: https://git.polynom.me/api/packages/Moxxy/pub #hosted: https://git.polynom.me/api/packages/Moxxy/pub
#version: 0.1.10 #version: 0.1.11
moxplatform_generic: moxplatform_generic:
path: ../moxplatform_generic path: ../moxplatform_generic

View File

@ -1,6 +1,6 @@
name: moxplatform_platform_interface name: moxplatform_platform_interface
description: A common platform interface for the my_plugin plugin. description: A common platform interface for the my_plugin plugin.
version: 0.1.10 version: 0.1.11
homepage: https://codeberg.org/moxxy/moxplatform homepage: https://codeberg.org/moxxy/moxplatform
publish_to: https://git.polynom.me/api/packages/Moxxy/pub publish_to: https://git.polynom.me/api/packages/Moxxy/pub
@ -14,7 +14,7 @@ dependencies:
moxplatform: moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.10 version: 0.1.11
moxlib: moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub hosted: https://git.polynom.me/api/packages/Moxxy/pub