From 3c2ff2d859033da9a1cebd2879b41df32cffa4ac Mon Sep 17 00:00:00 2001 From: jvde-github Date: Wed, 31 Jul 2024 20:37:34 +0200 Subject: [PATCH] additional wakelock checks --- app/build.gradle | 4 +- .../com/jvdegithub/aiscatcher/AisService.java | 43 +++++++++++-------- app/src/main/jni/AIS-catcher | 2 +- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5f23609..2ac908a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.jvdegithub.aiscatcher" minSdk 23 targetSdk 34 - versionCode 104 - versionName '1.04' + versionCode 105 + versionName '1.05' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { diff --git a/app/src/main/java/com/jvdegithub/aiscatcher/AisService.java b/app/src/main/java/com/jvdegithub/aiscatcher/AisService.java index 1357e0b..8b4a703 100644 --- a/app/src/main/java/com/jvdegithub/aiscatcher/AisService.java +++ b/app/src/main/java/com/jvdegithub/aiscatcher/AisService.java @@ -88,19 +88,23 @@ private Notification buildNotification(String msg) { } - public void acquireLocks() - { + public void acquireLocks() { PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); - wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, - "AIS-catcher:WakeLock"); - wakeLock.acquire(); + if (powerManager != null) { + wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AIS-catcher:WakeLock"); + if (wakeLock != null && !wakeLock.isHeld()) { + wakeLock.acquire(); + } + } } - public void releaseLocks() - { - wakeLock.release(); + public void releaseLocks() { + if (wakeLock != null && wakeLock.isHeld()) { + wakeLock.release(); + } } + @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -118,19 +122,20 @@ public int onStartCommand(Intent intent, int flags, int startId) { String msg = "Receiver running - " + DeviceManager.getDeviceTypeDescription() + " @ " + AisCatcherJava.getRateDescription(); startForeground(1001, buildNotification(msg)); - new Thread( - () -> { - acquireLocks(); - - AisCatcherJava.Run(); - AisCatcherJava.Close(); + new Thread(() -> { + try { + acquireLocks(); + AisCatcherJava.Run(); + AisCatcherJava.Close(); + sendBroadcast(); - stopForeground(true); - stopSelf(); - sendBroadcast(); + } finally { + releaseLocks(); - releaseLocks(); - }).start(); + stopForeground(true); + stopSelf(); + } + }).start(); } else { String msg = "Receiver creation failed"; startForeground(1001, buildNotification(msg)); diff --git a/app/src/main/jni/AIS-catcher b/app/src/main/jni/AIS-catcher index 3637d9a..398bb7b 160000 --- a/app/src/main/jni/AIS-catcher +++ b/app/src/main/jni/AIS-catcher @@ -1 +1 @@ -Subproject commit 3637d9a835380780dc8551149d6f26f547ab752a +Subproject commit 398bb7b28676a39a5d2ea5c50c1ed56590fddd72