Skip to content

Commit 862c92a

Browse files
committed
feat(scanner): update KeepScreenOn to increase brightness for bills when pulled up
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent b06ed5c commit 862c92a

2 files changed

Lines changed: 51 additions & 37 deletions

File tree

  • apps/flipcash/features/scanner/src/main/kotlin/com/flipcash/app/scanner/internal
  • ui/components/src/main/kotlin/com/getcode/ui/utils

apps/flipcash/features/scanner/src/main/kotlin/com/flipcash/app/scanner/internal/Scanner.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.flipcash.app.scanner.internal
22

33
import android.annotation.SuppressLint
4-
import android.app.Activity
5-
import android.view.WindowManager
64
import androidx.compose.runtime.Composable
75
import androidx.compose.runtime.DisposableEffect
86
import androidx.compose.runtime.LaunchedEffect
@@ -27,14 +25,12 @@ import com.getcode.opencode.model.financial.orZero
2725
import com.getcode.ui.components.OnLifecycleEvent
2826
import com.getcode.ui.scanner.CodeScanner
2927
import com.getcode.ui.scanner.NoCamerasAvailableException
28+
import com.getcode.ui.utils.KeepScreenOn
3029
import com.getcode.util.vibration.LocalVibrator
3130
import com.getcode.utils.ErrorUtils
3231
import com.kik.kikx.kikcodes.implementation.KikCodeResult
3332
import dev.theolm.rinku.DeepLink
34-
import kotlinx.coroutines.delay
3533
import timber.log.Timber
36-
import java.util.Timer
37-
import kotlin.concurrent.schedule
3834

3935
@Composable
4036
internal fun Scanner(deepLink: DeeplinkType?) {
@@ -182,15 +178,10 @@ internal fun Scanner(deepLink: DeeplinkType?) {
182178
if (billState.bill != null) {
183179
navigator.hide()
184180
}
185-
resetScreenTimeout(context as Activity)
186181
}
187-
}
188182

189-
private fun resetScreenTimeout(activity: Activity) {
190-
activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
191-
Timer().schedule(10000) {
192-
activity.runOnUiThread {
193-
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
194-
}
195-
}
183+
KeepScreenOn(
184+
isEnabled = billState.bill != null,
185+
useBrightness = true,
186+
)
196187
}
Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
11
package com.getcode.ui.utils
22

3+
import android.app.Activity
34
import android.view.WindowManager
45
import androidx.compose.runtime.Composable
5-
import androidx.compose.runtime.DisposableEffect
6+
import androidx.compose.runtime.LaunchedEffect
67
import 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

Comments
 (0)