11package com.getcode.ui.utils
22
3+ import android.app.Activity
34import android.view.WindowManager
45import androidx.compose.runtime.Composable
5- import androidx.compose.runtime.DisposableEffect
6+ import androidx.compose.runtime.LaunchedEffect
67import androidx.compose.ui.platform.LocalContext
7- import androidx.compose.ui.platform.LocalLifecycleOwner
8- import androidx.lifecycle.Lifecycle
9- import androidx.lifecycle.LifecycleEventObserver
8+ import kotlinx.coroutines.delay
109
10+ /* *
11+ * A composable function that keeps the screen on for a specified duration when enabled.
12+ * It can also optionally increase the screen brightness to a target level if it's below a minimum threshold.
13+ *
14+ * This is useful for scenarios like displaying a QR code where the user needs the screen to
15+ * stay on and be bright enough to be scanned.
16+ *
17+ * The effect is launched whenever the `isEnabled` parameter changes to `true`. After the specified
18+ * `timeoutMs`, the screen-on flag and any brightness changes are reverted.
19+ *
20+ * @param isEnabled A boolean flag to enable or disable the keep-screen-on functionality.
21+ * @param timeoutMs The duration in milliseconds for which to keep the screen on. Defaults to 10 seconds.
22+ * @param useBrightness A boolean flag to control whether the screen brightness should be adjusted.
23+ * @param minBrightness The minimum brightness threshold. If the current brightness is below this value
24+ * (or is set to automatic), the brightness will be adjusted. Brightness is a value from 0.0 to 1.0.
25+ * @param targetBrightness The brightness level to set the screen to if adjustment is needed. Brightness is a value from 0.0 to 1.0.
26+ */
1127@Composable
12- fun KeepScreenOn (isEnabled : Boolean ) {
28+ fun KeepScreenOn (
29+ isEnabled : Boolean ,
30+ timeoutMs : Long = 10_000L,
31+ useBrightness : Boolean = false,
32+ minBrightness : Float = 0.4f,
33+ targetBrightness : Float = 0.6f
34+ ) {
1335 val context = LocalContext .current
14- val lifecycleOwner = LocalLifecycleOwner .current
15- val window = (context as ? androidx.activity.ComponentActivity )?.window
1636
17- DisposableEffect (lifecycleOwner, isEnabled) {
18- val observer = LifecycleEventObserver { _, event ->
19- if (event == Lifecycle .Event .ON_RESUME ) {
20- if (isEnabled) {
21- // Keep screen on
22- window?.addFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
23- } else {
24- // Allow screen to turn off
25- window?.clearFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
26- }
27- }
28- }
37+ LaunchedEffect (isEnabled) {
38+ if (! isEnabled) return @LaunchedEffect
2939
30- lifecycleOwner.lifecycle.addObserver(observer)
40+ val window = (context as Activity ).window
41+ val layoutParams = window.attributes
42+ val originalBrightness = layoutParams.screenBrightness
3143
32- onDispose {
33- lifecycleOwner.lifecycle.removeObserver(observer)
34- window?.clearFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
44+ if (useBrightness && (originalBrightness < minBrightness || originalBrightness == WindowManager .LayoutParams .BRIGHTNESS_OVERRIDE_NONE )) {
45+ layoutParams.screenBrightness = targetBrightness
46+ window.attributes = layoutParams
47+ }
48+
49+ try {
50+ window.addFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
51+ delay(timeoutMs)
52+ } finally {
53+ window.clearFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
54+ if (useBrightness) {
55+ layoutParams.screenBrightness = originalBrightness
56+ window.attributes = layoutParams
57+ }
3558 }
3659 }
3760}
0 commit comments