Navaron Bracke

handle errors from start() in callback

@@ -165,58 +165,60 @@ class MobileScannerHandler( @@ -165,58 +165,60 @@ class MobileScannerHandler(
165 165
166 val detectionSpeed: DetectionSpeed = DetectionSpeed.values().first { it.intValue == speed} 166 val detectionSpeed: DetectionSpeed = DetectionSpeed.values().first { it.intValue == speed}
167 167
168 - try {  
169 - mobileScanner!!.start(  
170 - barcodeScannerOptions,  
171 - returnImage,  
172 - position,  
173 - torch,  
174 - detectionSpeed,  
175 - torchStateCallback,  
176 - zoomScaleStateCallback,  
177 - mobileScannerStartedCallback = {  
178 - Handler(Looper.getMainLooper()).post {  
179 - result.success(mapOf(  
180 - "textureId" to it.id,  
181 - "size" to mapOf("width" to it.width, "height" to it.height),  
182 - "torchable" to it.hasFlashUnit  
183 - )) 168 + mobileScanner!!.start(
  169 + barcodeScannerOptions,
  170 + returnImage,
  171 + position,
  172 + torch,
  173 + detectionSpeed,
  174 + torchStateCallback,
  175 + zoomScaleStateCallback,
  176 + mobileScannerStartedCallback = {
  177 + Handler(Looper.getMainLooper()).post {
  178 + result.success(mapOf(
  179 + "textureId" to it.id,
  180 + "size" to mapOf("width" to it.width, "height" to it.height),
  181 + "torchable" to it.hasFlashUnit
  182 + ))
  183 + }
  184 + },
  185 + mobileScannerErrorCallback = {
  186 + Handler(Looper.getMainLooper()).post {
  187 + when (it) {
  188 + is AlreadyStarted -> {
  189 + result.error(
  190 + "MobileScanner",
  191 + "Called start() while already started",
  192 + null
  193 + )
  194 + }
  195 + is CameraError -> {
  196 + result.error(
  197 + "MobileScanner",
  198 + "Error occurred when setting up camera!",
  199 + null
  200 + )
  201 + }
  202 + is NoCamera -> {
  203 + result.error(
  204 + "MobileScanner",
  205 + "No camera found or failed to open camera!",
  206 + null
  207 + )
  208 + }
  209 + else -> {
  210 + result.error(
  211 + "MobileScanner",
  212 + "Unknown error occurred.",
  213 + null
  214 + )
  215 + }
184 } 216 }
185 - },  
186 - timeout.toLong(),  
187 - cameraResolution,  
188 - )  
189 - } catch (e: AlreadyStarted) {  
190 - result.error(  
191 - "MobileScanner",  
192 - "Called start() while already started",  
193 - null  
194 - )  
195 - } catch (e: NoCamera) {  
196 - result.error(  
197 - "MobileScanner",  
198 - "No camera found or failed to open camera!",  
199 - null  
200 - )  
201 - } catch (e: TorchError) {  
202 - result.error(  
203 - "MobileScanner",  
204 - "Error occurred when setting torch!",  
205 - null  
206 - )  
207 - } catch (e: CameraError) {  
208 - result.error(  
209 - "MobileScanner",  
210 - "Error occurred when setting up camera!",  
211 - null  
212 - )  
213 - } catch (e: Exception) {  
214 - result.error(  
215 - "MobileScanner",  
216 - "Unknown error occurred..",  
217 - null  
218 - )  
219 - } 217 + }
  218 + },
  219 + timeout.toLong(),
  220 + cameraResolution,
  221 + )
220 } 222 }
221 223
222 private fun stop(result: MethodChannel.Result) { 224 private fun stop(result: MethodChannel.Result) {