diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 19ff772..84c8ae7 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -7,6 +7,7 @@
+
= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager
@@ -89,6 +95,7 @@ class TimerService : Service() {
saveTimeToDb()
notificationManager.cancel(1)
alarm?.release()
+ wakeLock?.release()
}
super.onDestroy()
}
@@ -117,6 +124,7 @@ class TimerService : Service() {
return super.onStartCommand(intent, flags, startId)
}
+ @Suppress("DEPRECATION")
private fun toggleTimer() {
updateProgressSegments()
@@ -157,6 +165,20 @@ class TimerService : Service() {
if (iterations == 0) showTimerNotification(time.toInt())
if (time < 0) {
+ val powerManager = this@TimerService.getSystemService(POWER_SERVICE) as PowerManager
+ wakeLock = powerManager.newWakeLock(
+ PowerManager.FULL_WAKE_LOCK or
+ PowerManager.ACQUIRE_CAUSES_WAKEUP or
+ PowerManager.ON_AFTER_RELEASE,
+ "PomodoroApp:AlarmWakeLock"
+ )
+ wakeLock?.acquire(2 * 60 * 1000L)
+ val intent = Intent(this@TimerService, MainActivity::class.java).apply {
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+ }
+ startActivity(intent)
+
+
skipTimer()
_timerState.update { currentState ->
currentState.copy(timerRunning = false)
@@ -176,7 +198,7 @@ class TimerService : Service() {
}
}
- @SuppressLint("MissingPermission") // We check for the permission when pressing the Play button in the UI
+ @SuppressLint("MissingPermission", "StringFormatInvalid") // We check for the permission when pressing the Play button in the UI
fun showTimerNotification(
remainingTime: Int, paused: Boolean = false, complete: Boolean = false
) {
@@ -372,6 +394,9 @@ class TimerService : Service() {
vibrator.cancel()
}
+ wakeLock?.release()
+ wakeLock = null
+
_timerState.update { currentState ->
currentState.copy(alarmRinging = false)
}
diff --git a/app/src/main/java/org/nsh07/pomodoro/ui/timerScreen/AlarmDialog.kt b/app/src/main/java/org/nsh07/pomodoro/ui/timerScreen/AlarmDialog.kt
index 9b27779..d41c27e 100644
--- a/app/src/main/java/org/nsh07/pomodoro/ui/timerScreen/AlarmDialog.kt
+++ b/app/src/main/java/org/nsh07/pomodoro/ui/timerScreen/AlarmDialog.kt
@@ -7,6 +7,11 @@
package org.nsh07.pomodoro.ui.timerScreen
+import android.app.Activity
+import android.content.Context
+import android.content.ContextWrapper
+import android.os.Build
+import android.view.WindowManager
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
@@ -24,8 +29,10 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@@ -37,6 +44,38 @@ fun AlarmDialog(
modifier: Modifier = Modifier,
stopAlarm: () -> Unit
) {
+ val context = LocalContext.current
+ val activity = context.findActivity()
+
+ // Set lockscreen flags when dialog appears, remove when it disappears
+ DisposableEffect(Unit) {
+ // Show over lockscreen
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
+ activity?.setShowWhenLocked(true)
+ activity?.setTurnScreenOn(true)
+ } else {
+ @Suppress("DEPRECATION")
+ activity?.window?.addFlags(
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+ )
+ }
+
+ onDispose {
+ // Remove lockscreen flags when dialog is dismissed
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
+ activity?.setShowWhenLocked(false)
+ activity?.setTurnScreenOn(false)
+ } else {
+ @Suppress("DEPRECATION")
+ activity?.window?.clearFlags(
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+ )
+ }
+ }
+ }
+
BasicAlertDialog(
onDismissRequest = stopAlarm,
modifier = modifier
@@ -75,4 +114,14 @@ fun AlarmDialog(
}
}
}
+}
+
+// Add this helper function at the file level (outside the composable)
+fun Context.findActivity(): Activity? {
+ var context = this
+ while (context is ContextWrapper) {
+ if (context is Activity) return context
+ context = context.baseContext
+ }
+ return null
}
\ No newline at end of file