Skip to content

Commit 5ad5434

Browse files
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 changed
Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
11
package dev.fluttercommunity.plus.wakelock
22

3-
import IsEnabledMessage
4-
import ToggleMessage
53
import android.app.Activity
64
import android.view.WindowManager
5+
import IsEnabledMessage
6+
import ToggleMessage
77

8+
/**
9+
* Manages the wakelock state to keep the screen on or off for an Android Activity.
10+
*/
811
internal class Wakelock {
9-
var activity: Activity? = null
10-
11-
private val enabled
12-
get() = activity!!.window.attributes.flags and
13-
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON != 0
14-
15-
fun toggle(message: ToggleMessage) {
16-
if (activity == null) {
17-
throw NoActivityException()
12+
var activity: Activity? = null
13+
set(value) {
14+
require(value != null) { "Activity cannot be null" }
15+
field = value
16+
}
17+
18+
private val isScreenKeptOn: Boolean
19+
get() = activity?.window?.attributes?.flags
20+
?.and(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0
21+
22+
/**
23+
* Toggles the wakelock state based on the provided [ToggleMessage].
24+
*
25+
* @param message The toggle message containing the enable state.
26+
* @throws NoActivityException If no activity is set.
27+
*/
28+
fun toggle(message: ToggleMessage) {
29+
val currentActivity = activity ?: throw NoActivityException()
30+
val enable = message.enable ?: return // Gracefully handle null enable
31+
32+
when {
33+
enable && !isScreenKeptOn -> {
34+
currentActivity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
35+
}
36+
!enable && isScreenKeptOn -> {
37+
currentActivity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
38+
}
39+
}
1840
}
1941

20-
val activity = this.activity!!
21-
val enabled = this.enabled
22-
23-
if (message.enable!!) {
24-
if (!enabled) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
25-
} else if (enabled) {
26-
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
42+
/**
43+
* Checks if the wakelock is currently enabled.
44+
*
45+
* @return [IsEnabledMessage] indicating whether the screen is kept on.
46+
* @throws NoActivityException If no activity is set.
47+
*/
48+
fun isEnabled(): IsEnabledMessage {
49+
return IsEnabledMessage(enabled = isScreenKeptOn)
2750
}
28-
}
29-
30-
fun isEnabled(): IsEnabledMessage {
31-
if (activity == null) {
32-
throw NoActivityException()
33-
}
34-
35-
return IsEnabledMessage(enabled = enabled)
36-
}
3751
}
3852

39-
class NoActivityException : Exception("wakelock requires a foreground activity")
53+
/**
54+
* Exception thrown when an operation requires a foreground activity but none is available.
55+
*/
56+
class NoActivityException : Exception("Wakelock requires a foreground activity")

0 commit comments

Comments
 (0)