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

View File

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

View File

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

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 {
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()

View File

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

View File

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

View File

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

View File

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