diff --git a/CHANGELOG.md b/CHANGELOG.md index e29c79c..51b55f4 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, 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] ### 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..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,7 +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 @@ -158,6 +161,22 @@ 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 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 + 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) + outState.putBoolean(STATE_IS_SCANNING, isScanning) + outState.putInt(STATE_PERMISSION_REQUEST_COUNT, permissionRequestCount) } /** @@ -167,6 +186,12 @@ 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) + permissionRequestCount = it.getInt(STATE_PERMISSION_REQUEST_COUNT, 0) + } + cameraExecutor = Executors.newSingleThreadExecutor() val parameters = IntentCompat.getSerializableExtra(intent, SCAN_PARAMETERS, OSBARCScanParameters::class.java) @@ -186,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() @@ -249,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) } @@ -316,6 +344,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 +783,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 +800,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) } } @@ -786,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 @@ -842,7 +870,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 +888,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 1, "$roundedRatio${getZoomButtonSuffix(selectedButton, 1)}", onClick = { - selectedButton = 1 + selectedZoomButton = ZOOM_BUTTON_MIN camera?.cameraControl?.setZoomRatio(minZoomRatio) } ) @@ -879,7 +907,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 2, "1${getZoomButtonSuffix(selectedButton, 2)}", onClick = { - selectedButton = 2 + selectedZoomButton = ZOOM_BUTTON_ONE_X camera?.cameraControl?.setZoomRatio(1f) } ) @@ -894,7 +922,7 @@ class OSBARCScannerActivity : ComponentActivity() { buttonToCompare = 3, "2${getZoomButtonSuffix(selectedButton, 3)}", onClick = { - selectedButton = 3 + selectedZoomButton = ZOOM_BUTTON_TWO_X camera?.cameraControl?.setZoomRatio(2f) } ) @@ -944,6 +972,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,