Navaron Bracke

reimplement toggle torch on Android

@@ -19,6 +19,7 @@ import androidx.camera.core.ExperimentalGetImage @@ -19,6 +19,7 @@ import androidx.camera.core.ExperimentalGetImage
19 import androidx.camera.core.ImageAnalysis 19 import androidx.camera.core.ImageAnalysis
20 import androidx.camera.core.ImageProxy 20 import androidx.camera.core.ImageProxy
21 import androidx.camera.core.Preview 21 import androidx.camera.core.Preview
  22 +import androidx.camera.core.TorchState
22 import androidx.camera.core.resolutionselector.AspectRatioStrategy 23 import androidx.camera.core.resolutionselector.AspectRatioStrategy
23 import androidx.camera.core.resolutionselector.ResolutionSelector 24 import androidx.camera.core.resolutionselector.ResolutionSelector
24 import androidx.camera.core.resolutionselector.ResolutionStrategy 25 import androidx.camera.core.resolutionselector.ResolutionStrategy
@@ -422,13 +423,16 @@ class MobileScanner( @@ -422,13 +423,16 @@ class MobileScanner(
422 /** 423 /**
423 * Toggles the flash light on or off. 424 * Toggles the flash light on or off.
424 */ 425 */
425 - fun toggleTorch(enableTorch: Boolean) {  
426 - if (camera == null) {  
427 - return  
428 - } 426 + fun toggleTorch() {
  427 + camera?.let {
  428 + if (!it.cameraInfo.hasFlashUnit()) {
  429 + return@let
  430 + }
429 431
430 - if (camera?.cameraInfo?.hasFlashUnit() == true) {  
431 - camera?.cameraControl?.enableTorch(enableTorch) 432 + when(it.cameraInfo.torchState.value) {
  433 + TorchState.OFF -> it.cameraControl.enableTorch(true)
  434 + TorchState.ON -> it.cameraControl.enableTorch(false)
  435 + }
432 } 436 }
433 } 437 }
434 438
@@ -122,8 +122,8 @@ class MobileScannerHandler( @@ -122,8 +122,8 @@ class MobileScannerHandler(
122 } 122 }
123 }) 123 })
124 "start" -> start(call, result) 124 "start" -> start(call, result)
125 - "torch" -> toggleTorch(call, result)  
126 "stop" -> stop(result) 125 "stop" -> stop(result)
  126 + "toggleTorch" -> toggleTorch(result)
127 "analyzeImage" -> analyzeImage(call, result) 127 "analyzeImage" -> analyzeImage(call, result)
128 "setScale" -> setScale(call, result) 128 "setScale" -> setScale(call, result)
129 "resetScale" -> resetScale(result) 129 "resetScale" -> resetScale(result)
@@ -168,7 +168,7 @@ class MobileScannerHandler( @@ -168,7 +168,7 @@ class MobileScannerHandler(
168 val position = 168 val position =
169 if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA 169 if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA
170 170
171 - val detectionSpeed: DetectionSpeed = DetectionSpeed.values().first { it.intValue == speed} 171 + val detectionSpeed: DetectionSpeed = DetectionSpeed.entries.first { it.intValue == speed}
172 172
173 mobileScanner!!.start( 173 mobileScanner!!.start(
174 barcodeScannerOptions, 174 barcodeScannerOptions,
@@ -244,8 +244,8 @@ class MobileScannerHandler( @@ -244,8 +244,8 @@ class MobileScannerHandler(
244 mobileScanner!!.analyzeImage(uri, analyzeImageSuccessCallback, analyzeImageErrorCallback) 244 mobileScanner!!.analyzeImage(uri, analyzeImageSuccessCallback, analyzeImageErrorCallback)
245 } 245 }
246 246
247 - private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) {  
248 - mobileScanner!!.toggleTorch(call.arguments == 1) 247 + private fun toggleTorch(result: MethodChannel.Result) {
  248 + mobileScanner?.toggleTorch()
249 result.success(null) 249 result.success(null)
250 } 250 }
251 251