diff --git a/app/src/main/java/com/noti/restore/overlay/OverlayPanelManager.kt b/app/src/main/java/com/noti/restore/overlay/OverlayPanelManager.kt index 282c108..6e35c6d 100644 --- a/app/src/main/java/com/noti/restore/overlay/OverlayPanelManager.kt +++ b/app/src/main/java/com/noti/restore/overlay/OverlayPanelManager.kt @@ -129,17 +129,38 @@ object OverlayPanelManager { stripContext = context loadSavedBrightness(context) - // Watch for system brightness changes and override with our target + // Watch for system brightness changes and adopt them as our new target, so the + // stock System UI slider (or any other writer of Settings.System.SCREEN_BRIGHTNESS) + // stays in sync with KompaktX's overlay instead of being immediately reverted by it. + // + // applyBrightness() below sets currentBrightnessTarget *before* writing + // Settings.System.SCREEN_BRIGHTNESS, so a change we triggered ourselves always finds + // sys == target here and is a no-op. Only a genuinely external write reaches the + // branch below, which is what lets this safely distinguish "someone else changed it" + // from "we just changed it" without an extra flag. brightnessObserver = object : ContentObserver(handler) { override fun onChange(selfChange: Boolean) { val target = currentBrightnessTarget if (target < 0) return try { + // Don't fight Adaptive Brightness: an external write to SCREEN_BRIGHTNESS + // while the system is in automatic mode (e.g. Settings' own Display screen + // mirroring its live auto-brightness value into this setting) is not KompaktX + // being asked to sync anything — adopting it as a new target and pinning the + // overlay to it here would fight Adaptive Brightness the same way reverting + // did before this branch, just with a different stale-ish value. + if (isAdaptiveBrightnessOn(context.contentResolver)) return val sys = Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS) if (sys != target) { - Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, target) - val screenVal = if (target <= 0) 0.001f else target / 255f + currentBrightnessTarget = sys + val screenVal = if (sys <= 0) 0.001f else sys / 255f + // Mirror applyBrightness()'s side effects so the externally-set value + // actually reaches the physical backlight (the overlay's screenBrightness + // param drives it, not Settings.System by itself), persists across a + // KompaktX process restart, and still applies above the lockscreen. applyScreenBrightnessToView(brightnessOverlay, screenVal) + com.noti.restore.service.RecentsButtonService.applyAccessibilityBrightness(screenVal) + saveBrightness(context) } } catch (_: Exception) {} } @@ -2319,6 +2340,14 @@ object OverlayPanelManager { } catch (_: Exception) {} } + /** True when the system's own Adaptive Brightness is currently on. */ + private fun isAdaptiveBrightnessOn(resolver: android.content.ContentResolver): Boolean { + return try { + Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == + Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC + } catch (_: Exception) { false } + } + /** Re-apply our brightness target after screen wake. Forces both Settings.System and overlay. */ fun reapplyBrightness() { val ctx = stripContext ?: return