Commit 5ad5434
authored
Update Wakelock.kt
package dev.fluttercommunity.plus.wakelock
import android.app.Activity
import android.view.WindowManager
import IsEnabledMessage
import ToggleMessage
/**
* Manages the wakelock state to keep the screen on or off for an Android Activity.
*/
internal class Wakelock {
var activity: Activity? = null
set(value) {
require(value != null) { "Activity cannot be null" }
field = value
}
private val isScreenKeptOn: Boolean
get() = activity?.window?.attributes?.flags
?.and(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0
/**
* Toggles the wakelock state based on the provided [ToggleMessage].
*
* @param message The toggle message containing the enable state.
* @throws NoActivityException If no activity is set.
*/
fun toggle(message: ToggleMessage) {
val currentActivity = activity ?: throw NoActivityException()
val enable = message.enable ?: return // Gracefully handle null enable
when {
enable && !isScreenKeptOn -> {
currentActivity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
!enable && isScreenKeptOn -> {
currentActivity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
}
/**
* Checks if the wakelock is currently enabled.
*
* @return [IsEnabledMessage] indicating whether the screen is kept on.
* @throws NoActivityException If no activity is set.
*/
fun isEnabled(): IsEnabledMessage {
return IsEnabledMessage(enabled = isScreenKeptOn)
}
}
/**
* Exception thrown when an operation requires a foreground activity but none is available.
*/
class NoActivityException : Exception("Wakelock requires a foreground activity")1 parent 0c74e5b commit 5ad5434
File tree
1 file changed
+45
-28
lines changed- wakelock_plus/android/src/main/kotlin/dev/fluttercommunity/plus/wakelock
1 file changed
+45
-28
lines changedLines changed: 45 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
18 | 40 | | |
19 | 41 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
27 | 50 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | 51 | | |
38 | 52 | | |
39 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
0 commit comments