From c51753c8a00be229629fa0dd1a3e931186f7dba7 Mon Sep 17 00:00:00 2001 From: Alexandre Jacinto Date: Tue, 1 Dec 2026 16:22:44 -0400 Subject: [PATCH 1/2] fix: keep torch and zoom state on screen rotation References: https://outsystemsrd.atlassian.net/browse/RMET-4398 Torch and zoom state lived in `remember` inside composables, so it was lost when the activity was recreated (rotation, theme, language, etc.). The state is now held at activity level, saved in `onSaveInstanceState`, and re-applied to the camera once it is bound. Not yet tested on a device. --- CHANGELOG.md | 3 + .../barcode/view/OSBARCScannerActivity.kt | 62 +++++++++++++++++-- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e29c79c..311db26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository. ## [Unreleased] +### 01-12-2026 +- Fix: torch and zoom state are kept when the screen rotates (https://outsystemsrd.atlassian.net/browse/RMET-4398). + ## [2.1.2] ### 2026-10-30 diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt index 7ad45f5..5b890fe 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt @@ -141,6 +141,9 @@ class OSBARCScannerActivity : ComponentActivity() { private var showDialog by mutableStateOf(false) private var permissionGiven by mutableStateOf(false) private var isScanning = false + // UI state that must survive configuration changes (e.g. screen rotation) + private var isTorchOn by mutableStateOf(false) + private var selectedZoomButton by mutableStateOf(DEFAULT_ZOOM_BUTTON) private lateinit var cameraExecutor: ExecutorService private lateinit var barcodeAnalyzer: OSBARCBarcodeAnalyzer @@ -158,6 +161,18 @@ class OSBARCScannerActivity : ComponentActivity() { private const val CAM_DIRECTION_FRONT = 2 private const val ORIENTATION_PORTRAIT = 1 private const val ORIENTATION_LANDSCAPE = 2 + private const val STATE_TORCH_ON = "STATE_TORCH_ON" + private const val STATE_SELECTED_ZOOM_BUTTON = "STATE_SELECTED_ZOOM_BUTTON" + private const val ZOOM_BUTTON_MIN = 1 + private const val ZOOM_BUTTON_ONE_X = 2 + private const val ZOOM_BUTTON_TWO_X = 3 + private const val DEFAULT_ZOOM_BUTTON = ZOOM_BUTTON_ONE_X + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putBoolean(STATE_TORCH_ON, isTorchOn) + outState.putInt(STATE_SELECTED_ZOOM_BUTTON, selectedZoomButton) } /** @@ -167,6 +182,11 @@ class OSBARCScannerActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + savedInstanceState?.let { + isTorchOn = it.getBoolean(STATE_TORCH_ON, false) + selectedZoomButton = it.getInt(STATE_SELECTED_ZOOM_BUTTON, DEFAULT_ZOOM_BUTTON) + } + cameraExecutor = Executors.newSingleThreadExecutor() val parameters = IntentCompat.getSerializableExtra(intent, SCAN_PARAMETERS, OSBARCScanParameters::class.java) @@ -316,6 +336,7 @@ class OSBARCScannerActivity : ComponentActivity() { minZoomRatio = it.cameraInfo.zoomState.value?.minZoomRatio ?: 1f, maxZoomRatio = it.cameraInfo.zoomState.value?.maxZoomRatio ?: 1f ) + restoreCameraState(it, uiState) } } catch (e: Exception) { e.message?.let { Log.e(LOG_TAG, it) } @@ -754,7 +775,7 @@ class OSBARCScannerActivity : ComponentActivity() { onAccessibilityLabel: String? = null, offAccessibilityLabel: String? = null ) { - var isFlashlightOn by remember { mutableStateOf(false) } + val isFlashlightOn = isTorchOn val onIcon = painterResource(id = R.drawable.flash_on) val offIcon = painterResource(id = R.drawable.flash_off) val icon = if (isFlashlightOn) onIcon else offIcon @@ -771,7 +792,7 @@ class OSBARCScannerActivity : ComponentActivity() { .clickable { try { camera?.cameraControl?.enableTorch(!isFlashlightOn) - isFlashlightOn = !isFlashlightOn + isTorchOn = !isFlashlightOn } catch (e: Exception) { e.message?.let { Log.e(LOG_TAG, it) } } @@ -842,7 +863,7 @@ class OSBARCScannerActivity : ComponentActivity() { val minZoomRatio = uiState.minZoomRatio val roundedRatio = (minZoomRatio * 10).roundToInt() / 10f val maxZoomRatio = uiState.maxZoomRatio - var selectedButton by remember { mutableStateOf(2) } + val selectedButton = selectedZoomButton Row( modifier = Modifier @@ -860,7 +881,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 1, "$roundedRatio${getZoomButtonSuffix(selectedButton, 1)}", onClick = { - selectedButton = 1 + selectedZoomButton = ZOOM_BUTTON_MIN camera?.cameraControl?.setZoomRatio(minZoomRatio) } ) @@ -879,7 +900,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 2, "1${getZoomButtonSuffix(selectedButton, 2)}", onClick = { - selectedButton = 2 + selectedZoomButton = ZOOM_BUTTON_ONE_X camera?.cameraControl?.setZoomRatio(1f) } ) @@ -894,7 +915,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 3, "2${getZoomButtonSuffix(selectedButton, 3)}", onClick = { - selectedButton = 3 + selectedZoomButton = ZOOM_BUTTON_TWO_X camera?.cameraControl?.setZoomRatio(2f) } ) @@ -944,6 +965,35 @@ class OSBARCScannerActivity : ComponentActivity() { return if (selectedButton == buttonToCompare) "x" else "" } + /** + * Re-applies the torch and zoom state to a freshly bound camera, so that + * they survive configuration changes (the activity, and thus the camera, is recreated). + */ + private fun restoreCameraState(boundCamera: Camera, state: OSBARCScannerUiState) { + try { + if (isTorchOn) { + if (state.hasFlashUnit) { + boundCamera.cameraControl.enableTorch(true) + } else { + isTorchOn = false + } + } + val zoomRatio = when (selectedZoomButton) { + ZOOM_BUTTON_MIN -> state.minZoomRatio.takeIf { state.minZoomRatio < 1f } + ZOOM_BUTTON_TWO_X -> 2f.takeIf { state.maxZoomRatio >= 2f } + else -> 1f + } + if (zoomRatio == null) { + selectedZoomButton = DEFAULT_ZOOM_BUTTON + boundCamera.cameraControl.setZoomRatio(1f) + } else if (zoomRatio != 1f) { + boundCamera.cameraControl.setZoomRatio(zoomRatio) + } + } catch (e: Exception) { + e.message?.let { Log.e(LOG_TAG, it) } + } + } + private fun hasCameraPermission(context: Context): Boolean { return ContextCompat.checkSelfPermission( context, From 2c44d3862658a437370d0ec150060e1bc75022fe Mon Sep 17 00:00:00 2001 From: Alexandre Jacinto Date: Fri, 4 Dec 2026 09:29:47 -0400 Subject: [PATCH 2/2] fix: keep scanning state and permission request flag on screen rotation Addresses review feedback: isScanning was reset on every recreation, and permissionRequestCount made the camera permission be requested again after rotation. Only request the permission when it is not already granted. --- CHANGELOG.md | 2 +- .../barcode/view/OSBARCScannerActivity.kt | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 311db26..51b55f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ The changes documented here do not include those from the original repository. ## [Unreleased] ### 01-12-2026 -- Fix: torch and zoom state are kept when the screen rotates (https://outsystemsrd.atlassian.net/browse/RMET-4398). +- Fix: torch, zoom and scanning state are kept when the screen rotates, and the camera permission is no longer requested again on rotation (https://outsystemsrd.atlassian.net/browse/RMET-4398). ## [2.1.2] diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt index 5b890fe..29f3ddf 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt @@ -140,10 +140,10 @@ class OSBARCScannerActivity : ComponentActivity() { private var permissionRequestCount = 0 private var showDialog by mutableStateOf(false) private var permissionGiven by mutableStateOf(false) - private var isScanning = false // UI state that must survive configuration changes (e.g. screen rotation) private var isTorchOn by mutableStateOf(false) private var selectedZoomButton by mutableStateOf(DEFAULT_ZOOM_BUTTON) + private var isScanning by mutableStateOf(false) private lateinit var cameraExecutor: ExecutorService private lateinit var barcodeAnalyzer: OSBARCBarcodeAnalyzer @@ -163,6 +163,8 @@ class OSBARCScannerActivity : ComponentActivity() { private const val ORIENTATION_LANDSCAPE = 2 private const val STATE_TORCH_ON = "STATE_TORCH_ON" private const val STATE_SELECTED_ZOOM_BUTTON = "STATE_SELECTED_ZOOM_BUTTON" + private const val STATE_IS_SCANNING = "STATE_IS_SCANNING" + private const val STATE_PERMISSION_REQUEST_COUNT = "STATE_PERMISSION_REQUEST_COUNT" private const val ZOOM_BUTTON_MIN = 1 private const val ZOOM_BUTTON_ONE_X = 2 private const val ZOOM_BUTTON_TWO_X = 3 @@ -173,6 +175,8 @@ class OSBARCScannerActivity : ComponentActivity() { super.onSaveInstanceState(outState) outState.putBoolean(STATE_TORCH_ON, isTorchOn) outState.putInt(STATE_SELECTED_ZOOM_BUTTON, selectedZoomButton) + outState.putBoolean(STATE_IS_SCANNING, isScanning) + outState.putInt(STATE_PERMISSION_REQUEST_COUNT, permissionRequestCount) } /** @@ -185,6 +189,7 @@ class OSBARCScannerActivity : ComponentActivity() { savedInstanceState?.let { isTorchOn = it.getBoolean(STATE_TORCH_ON, false) selectedZoomButton = it.getInt(STATE_SELECTED_ZOOM_BUTTON, DEFAULT_ZOOM_BUTTON) + permissionRequestCount = it.getInt(STATE_PERMISSION_REQUEST_COUNT, 0) } cameraExecutor = Executors.newSingleThreadExecutor() @@ -206,7 +211,9 @@ class OSBARCScannerActivity : ComponentActivity() { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE } - isScanning = !parameters.scanButton + // with a scan button, scanning only starts when the user taps it, so keep it across recreation + isScanning = savedInstanceState?.getBoolean(STATE_IS_SCANNING, !parameters.scanButton) + ?: !parameters.scanButton selector = CameraSelector.Builder() .requireLensFacing(if (parameters.cameraDirection == CAM_DIRECTION_FRONT) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK) .build() @@ -269,7 +276,8 @@ class OSBARCScannerActivity : ComponentActivity() { updatePermissionState(isGranted) } SideEffect { - if (permissionRequestCount == 0) { + // only ask when needed: not on every recreation, and not if already granted + if (permissionRequestCount == 0 && !hasCameraPermission(context)) { permissionRequestCount++ requestPermissionLauncher.launch(Manifest.permission.CAMERA) } @@ -807,14 +815,13 @@ class OSBARCScannerActivity : ComponentActivity() { */ @Composable fun ScanButton(modifier: Modifier, scanButtonText: String) { - var scanning by remember { mutableStateOf(false) } + val scanning = isScanning val backgroundColor = if (scanning) ButtonsBackgroundWhite else ButtonsBackgroundGray val textColor = if (scanning) ButtonsTextGray else ButtonsTextWhite Button( onClick = { isScanning = !isScanning - scanning = !scanning }, colors = ButtonDefaults.buttonColors( containerColor = backgroundColor