@@ -7,6 +7,7 @@ import android.media.AudioAttributes
77import android.media.MediaPlayer
88import android.os.Build
99import android.os.IBinder
10+ import android.os.PowerManager
1011import android.os.SystemClock
1112import android.os.VibrationEffect
1213import android.os.Vibrator
@@ -22,6 +23,7 @@ import kotlinx.coroutines.flow.asStateFlow
2223import kotlinx.coroutines.flow.update
2324import kotlinx.coroutines.launch
2425import kotlinx.coroutines.runBlocking
26+ import org.nsh07.pomodoro.MainActivity
2527import org.nsh07.pomodoro.R
2628import org.nsh07.pomodoro.TomatoApplication
2729import org.nsh07.pomodoro.ui.timerScreen.viewModel.TimerMode
@@ -59,6 +61,9 @@ class TimerService : Service() {
5961
6062 private var alarm: MediaPlayer ? = null
6163
64+
65+ private var wakeLock: PowerManager .WakeLock ? = null
66+
6267 private val vibrator by lazy {
6368 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
6469 val vibratorManager = getSystemService(VIBRATOR_MANAGER_SERVICE ) as VibratorManager
@@ -89,6 +94,7 @@ class TimerService : Service() {
8994 saveTimeToDb()
9095 notificationManager.cancel(1 )
9196 alarm?.release()
97+ wakeLock?.release()
9298 }
9399 super .onDestroy()
94100 }
@@ -117,6 +123,7 @@ class TimerService : Service() {
117123 return super .onStartCommand(intent, flags, startId)
118124 }
119125
126+
120127 private fun toggleTimer () {
121128 updateProgressSegments()
122129
@@ -157,6 +164,20 @@ class TimerService : Service() {
157164 if (iterations == 0 ) showTimerNotification(time.toInt())
158165
159166 if (time < 0 ) {
167+ val powerManager = this @TimerService.getSystemService(POWER_SERVICE ) as PowerManager
168+ wakeLock = powerManager.newWakeLock(
169+ PowerManager .SCREEN_BRIGHT_WAKE_LOCK or
170+ PowerManager .ACQUIRE_CAUSES_WAKEUP or
171+ PowerManager .ON_AFTER_RELEASE ,
172+ " PomodoroApp:AlarmWakeLock"
173+ )
174+ wakeLock?.acquire(2 * 60 * 1000L )
175+ val intent = Intent (this @TimerService, MainActivity ::class .java).apply {
176+ addFlags(Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_SINGLE_TOP )
177+ }
178+ startActivity(intent)
179+
180+
160181 skipTimer()
161182 _timerState .update { currentState ->
162183 currentState.copy(timerRunning = false )
@@ -176,7 +197,7 @@ class TimerService : Service() {
176197 }
177198 }
178199
179- @SuppressLint(" MissingPermission" ) // We check for the permission when pressing the Play button in the UI
200+ @SuppressLint(" MissingPermission" , " StringFormatInvalid " ) // We check for the permission when pressing the Play button in the UI
180201 fun showTimerNotification (
181202 remainingTime : Int , paused : Boolean = false, complete : Boolean = false
182203 ) {
@@ -372,6 +393,9 @@ class TimerService : Service() {
372393 vibrator.cancel()
373394 }
374395
396+ wakeLock?.release()
397+ wakeLock = null
398+
375399 _timerState .update { currentState ->
376400 currentState.copy(alarmRinging = false )
377401 }
0 commit comments