From bf794c41dad90cdb12b76f36a704f73b4c55afec Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 6 Feb 2026 15:45:29 -0500 Subject: [PATCH] Maximize screen brightness during selfie flash in new camera. --- .../signal/camera/CameraScreenViewModel.kt | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/feature/camera/src/main/java/org/signal/camera/CameraScreenViewModel.kt b/feature/camera/src/main/java/org/signal/camera/CameraScreenViewModel.kt index 6f8b3c1c82..e270ef9782 100644 --- a/feature/camera/src/main/java/org/signal/camera/CameraScreenViewModel.kt +++ b/feature/camera/src/main/java/org/signal/camera/CameraScreenViewModel.kt @@ -1,11 +1,15 @@ package org.signal.camera +import android.app.Activity import android.content.Context +import android.content.ContextWrapper import android.graphics.Bitmap import android.graphics.Matrix import android.os.Build import android.os.VibrationEffect import android.os.Vibrator +import android.view.Window +import android.view.WindowManager import androidx.camera.core.AspectRatio import androidx.camera.core.Camera import androidx.camera.core.CameraSelector @@ -46,6 +50,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.signal.core.util.logging.Log import org.signal.core.util.throttleLatest +import java.lang.ref.WeakReference import java.util.EnumMap import java.util.concurrent.Executors import kotlin.time.Duration.Companion.seconds @@ -67,6 +72,8 @@ class CameraScreenViewModel : ViewModel() { private var imageCapture: ImageCapture? = null private var videoCapture: VideoCapture? = null private var recording: Recording? = null + private var brightnessBeforeFlash: Float = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE + private var brightnessWindow: WeakReference? = null private val _qrCodeDetected = MutableSharedFlow(extraBufferCapacity = 1) @@ -145,8 +152,9 @@ class CameraScreenViewModel : ViewModel() { state: CameraScreenState, onPhotoCaptured: (Bitmap) -> Unit ) { - // Show selfie flash + // Show selfie flash and maximize screen brightness _state.value = state.copy(showSelfieFlash = true) + setMaxScreenBrightness(context) // Wait for screen to brighten, then capture viewModelScope.launch { @@ -199,6 +207,7 @@ class CameraScreenViewModel : ViewModel() { private fun hideSelfieFlash() { if (_state.value.showSelfieFlash) { _state.value = _state.value.copy(showSelfieFlash = false) + restoreScreenBrightness() } } @@ -586,6 +595,33 @@ class CameraScreenViewModel : ViewModel() { imageProxy.close() } + private fun setMaxScreenBrightness(context: Context) { + val window = context.findActivity()?.window ?: return + + brightnessBeforeFlash = window.attributes.screenBrightness + brightnessWindow = WeakReference(window) + window.attributes = window.attributes.apply { + screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL + } + } + + private fun restoreScreenBrightness() { + val window = brightnessWindow?.get() ?: return + window.attributes = window.attributes.apply { + screenBrightness = brightnessBeforeFlash + } + brightnessWindow = null + } + + private fun Context.findActivity(): Activity? { + var context = this + while (context is ContextWrapper) { + if (context is Activity) return context + context = context.baseContext + } + return null + } + private fun vibrate(context: Context) { val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator vibrator?.let {