Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository.

## [Unreleased]

### 01-12-2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: The date on this changelog seems reversed when compared to older entries

Also, idk if we want to go ahead with a release right away or not, but in case we do, it's also missing a pom.xml and setting the version in the changelog here too.

- 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Comment thread
OS-pedrogustavobilro marked this conversation as resolved.
Resolved

/**
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand All @@ -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) }
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -860,7 +888,7 @@ class OSBARCScannerActivity : ComponentActivity() {
buttonToCompare = 1,
"$roundedRatio${getZoomButtonSuffix(selectedButton, 1)}",
onClick = {
selectedButton = 1
selectedZoomButton = ZOOM_BUTTON_MIN
camera?.cameraControl?.setZoomRatio(minZoomRatio)
}
)
Expand All @@ -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)
}
)
Expand All @@ -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)
}
)
Expand Down Expand Up @@ -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,
Expand Down
Loading