From 5fb7ca418025a12cdfdab2ef1053fdd5bceff708 Mon Sep 17 00:00:00 2001 From: Patrick Schneider Date: Sun, 3 Aug 2025 14:04:32 +0200 Subject: [PATCH 1/2] [update] Removes the foreground service requirement for the timer service. --- app/src/main/AndroidManifest.xml | 5 +- .../activities/ExerciseActivity.java | 62 ++++++++++++++++++- .../receivers/TimerSchedulerReceiver.java | 11 ++-- .../aktivpause/service/TimerService.java | 49 +++++++++++++-- 4 files changed, 110 insertions(+), 17 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 88e8f33..3c5e177 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,8 +10,6 @@ - - + android:exported="false" /> loader, ExerciseSet set) { if (set != null) { diff --git a/app/src/main/java/org/secuso/aktivpause/receivers/TimerSchedulerReceiver.java b/app/src/main/java/org/secuso/aktivpause/receivers/TimerSchedulerReceiver.java index 8138054..d9318ef 100644 --- a/app/src/main/java/org/secuso/aktivpause/receivers/TimerSchedulerReceiver.java +++ b/app/src/main/java/org/secuso/aktivpause/receivers/TimerSchedulerReceiver.java @@ -11,6 +11,7 @@ import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; import androidx.annotation.NonNull; +import androidx.core.app.AlarmManagerCompat; import androidx.legacy.content.WakefulBroadcastReceiver; import org.secuso.aktivpause.service.TimerService; @@ -79,7 +80,7 @@ public class TimerSchedulerReceiver extends WakefulBroadcastReceiver { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent automaticTimerIntent = new Intent(context, TimerSchedulerReceiver.class); - PendingIntent automaticTimerPending = PendingIntent.getBroadcast(context, 0, automaticTimerIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent automaticTimerPending = PendingIntent.getBroadcast(context, 0, automaticTimerIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); @@ -143,12 +144,8 @@ public class TimerSchedulerReceiver extends WakefulBroadcastReceiver { } } - if(done || !scheduleExerciseDaysEnabled) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), automaticTimerPending); - } else { - alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), automaticTimerPending); - } + if((done || !scheduleExerciseDaysEnabled)) { + AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), automaticTimerPending); } } diff --git a/app/src/main/java/org/secuso/aktivpause/service/TimerService.java b/app/src/main/java/org/secuso/aktivpause/service/TimerService.java index 8b74948..99bb99d 100644 --- a/app/src/main/java/org/secuso/aktivpause/service/TimerService.java +++ b/app/src/main/java/org/secuso/aktivpause/service/TimerService.java @@ -12,6 +12,7 @@ import android.content.SharedPreferences; import android.content.pm.ServiceInfo; import android.os.Binder; import android.os.CountDownTimer; +import android.os.Handler; import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.Settings; @@ -56,6 +57,9 @@ public class TimerService extends Service { private static final int UPDATE_INTERVAL = 125; public static final int NOTIFICATION_ID = 31337; + private NotificationCompat.Builder notiBuilder = null; + private NotificationManager notiManager = null; + private boolean isAppInBackground = false; private TimerServiceBinder mBinder = new TimerServiceBinder(); private CountDownTimer mTimer; @@ -366,13 +370,50 @@ public class TimerService extends Service { } private void updateNotification() { - if(isRunning() || isPaused()) { - ServiceCompat.startForeground(this, NOTIFICATION_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE); - } else { - stopForeground(true); + if(isAppInBackground) { + Notification notification = buildNotification(); + notiManager.notify(NOTIFICATION_ID, notification); + } + else if(notiManager != null) { + notiManager.cancel(NOTIFICATION_ID); } } + /** + * Check if the app is in the background. + * If so, start a notification showing the current timer. + * + * @param isInBackground Sets global flag to determine whether the app is in the background + */ + public void setIsAppInBackground(boolean isInBackground){ + this.isAppInBackground = isInBackground; + + //Execute after short delay to prevent short notification popup if workoutActivity is closed + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + updateNotification(); + } + }, 700); + } + + /** + * Cancel the notification when workout activity is destroyed + */ + public void workoutClosed(){ + this.isAppInBackground = false; + notiManager.cancel(NOTIFICATION_ID); + } + +// private void updateNotification() { +// if(isRunning() || isPaused()) { +// ServiceCompat.startForeground(this, NOTIFICATION_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE); +// } else { +// stopForeground(true); +// } +// } + @Override public IBinder onBind(Intent intent) { return mBinder; From ca48a56dde15c8f1e8edf7c43f3c8d4583fc02aa Mon Sep 17 00:00:00 2001 From: Patrick Schneider Date: Sun, 3 Aug 2025 14:53:58 +0200 Subject: [PATCH 2/2] Change versionCode to differentiate from last Google Play version --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 167eccd..6a4248a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.secuso.aktivpause" minSdkVersion 21 targetSdkVersion 34 - versionCode 11 - versionName "1.2.2" + versionCode 100 + versionName "1.2.3" vectorDrawables.useSupportLibrary = true } buildTypes {