Skip to content

Commit c959ae1

Browse files
authored
Merge pull request #85 from Fandroid745/fix/screenwakeup
feat:Fix issue where screen does not wake up when the timer ends
2 parents 9a41d83 + 920d911 commit c959ae1

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
88
<uses-permission android:name="android.permission.POST_PROMOTED_NOTIFICATIONS" />
99
<uses-permission android:name="android.permission.VIBRATE" />
10+
<uses-permission android:name="android.permission.WAKE_LOCK" />
1011

1112
<application
1213
android:name=".TomatoApplication"

app/src/main/java/org/nsh07/pomodoro/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.nsh07.pomodoro.utils.toColor
2020

2121
class MainActivity : ComponentActivity() {
2222

23-
private val timerViewModel: TimerViewModel by viewModels(factoryProducer = { TimerViewModel.Factory })
23+
private val timerViewModel: TimerViewModel by viewModels(factoryProducer = { TimerViewModel.Factory })
2424
private val settingsViewModel: SettingsViewModel by viewModels(factoryProducer = { SettingsViewModel.Factory })
2525

2626
private val appContainer by lazy {
@@ -59,6 +59,7 @@ class MainActivity : ComponentActivity() {
5959
}
6060
}
6161

62+
6263
override fun onStop() {
6364
super.onStop()
6465
// Reduce the timer loop frequency when not visible to save battery power

app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.media.AudioAttributes
77
import android.media.MediaPlayer
88
import android.os.Build
99
import android.os.IBinder
10+
import android.os.PowerManager
1011
import android.os.SystemClock
1112
import android.os.VibrationEffect
1213
import android.os.Vibrator
@@ -22,6 +23,7 @@ import kotlinx.coroutines.flow.asStateFlow
2223
import kotlinx.coroutines.flow.update
2324
import kotlinx.coroutines.launch
2425
import kotlinx.coroutines.runBlocking
26+
import org.nsh07.pomodoro.MainActivity
2527
import org.nsh07.pomodoro.R
2628
import org.nsh07.pomodoro.TomatoApplication
2729
import 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
}

app/src/main/java/org/nsh07/pomodoro/ui/timerScreen/AlarmDialog.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.nsh07.pomodoro.ui.timerScreen
99

10+
import androidx.activity.compose.LocalActivity
1011
import androidx.compose.foundation.clickable
1112
import androidx.compose.foundation.layout.Column
1213
import androidx.compose.foundation.layout.Spacer
@@ -24,6 +25,7 @@ import androidx.compose.material3.Surface
2425
import androidx.compose.material3.Text
2526
import androidx.compose.material3.TextButton
2627
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.DisposableEffect
2729
import androidx.compose.ui.Alignment
2830
import androidx.compose.ui.Modifier
2931
import androidx.compose.ui.res.painterResource
@@ -37,6 +39,21 @@ fun AlarmDialog(
3739
modifier: Modifier = Modifier,
3840
stopAlarm: () -> Unit
3941
) {
42+
val activity = LocalActivity.current
43+
44+
// Set lockscreen flags when dialog appears, remove when it disappears
45+
DisposableEffect(Unit) {
46+
// Show over lockscreen
47+
activity?.setShowWhenLocked(true)
48+
activity?.setTurnScreenOn(true)
49+
50+
onDispose {
51+
// Remove lockscreen flags when dialog is dismissed
52+
activity?.setShowWhenLocked(false)
53+
activity?.setTurnScreenOn(false)
54+
}
55+
}
56+
4057
BasicAlertDialog(
4158
onDismissRequest = stopAlarm,
4259
modifier = modifier
@@ -75,4 +92,5 @@ fun AlarmDialog(
7592
}
7693
}
7794
}
78-
}
95+
}
96+

0 commit comments

Comments
 (0)