Skip to content

Commit 3d97b4b

Browse files
committed
fix: prevent KeepScreenOn from maxing brightness with adaptive brightness enabled
BRIGHTNESS_OVERRIDE_NONE (-1.0f) always tripped the `originalBrightness < minBrightness` check because -1.0 < 0.4, causing an unconditional override to 0.6f. On modern Pixel panels with non-linear brightness curves, 0.6 on the WindowManager scale maps to near-max physical brightness. Query the actual system brightness via Settings.System.SCREEN_BRIGHTNESS when the window reports BRIGHTNESS_OVERRIDE_NONE so the boost only fires when the screen is genuinely dim. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 3337813 commit 3d97b4b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

ui/components/src/main/kotlin/com/getcode/ui/utils/KeepScreenOn.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getcode.ui.utils
22

33
import android.app.Activity
4+
import android.provider.Settings
45
import android.view.WindowManager
56
import androidx.compose.runtime.Composable
67
import androidx.compose.runtime.LaunchedEffect
@@ -41,7 +42,22 @@ fun KeepScreenOn(
4142
val layoutParams = window.attributes
4243
val originalBrightness = layoutParams.screenBrightness
4344

44-
if (useBrightness && (originalBrightness < minBrightness || originalBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)) {
45+
// When brightness is system-managed (adaptive brightness), the window
46+
// reports BRIGHTNESS_OVERRIDE_NONE (-1.0). We must query the actual
47+
// system brightness rather than treating -1.0 as "below threshold" —
48+
// otherwise we unconditionally override to targetBrightness which on
49+
// modern Pixel panels with non-linear curves appears as near-max.
50+
val effectiveBrightness = if (originalBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
51+
Settings.System.getInt(
52+
context.contentResolver,
53+
Settings.System.SCREEN_BRIGHTNESS,
54+
128 // ~50% fallback if unreadable
55+
) / 255f
56+
} else {
57+
originalBrightness
58+
}
59+
60+
if (useBrightness && effectiveBrightness < minBrightness) {
4561
layoutParams.screenBrightness = targetBrightness
4662
window.attributes = layoutParams
4763
}

0 commit comments

Comments
 (0)