/** * Docutain SDK Barcode Cordova Plugin * Copyright (c) 2024 INFOSOFT Informations- und Dokumentationssysteme GmbH. All rights reserved. * * Docutain SDK Cordova Plugin is a commercial product and requires a license. * Details found in the LICENSE file in the root directory of this source tree. */ package com.docutain.sdk.barcode.plugin.cordova import android.content.Intent import android.util.Log import android.os.Build import androidx.appcompat.app.AppCompatActivity import de.docutain.sdk.DocutainSDK import de.docutain.sdk.Logger import de.docutain.sdk.barcode.data.Barcode import de.docutain.sdk.barcode.configuration.BarcodeScannerConfiguration import de.docutain.sdk.barcode.ui.BarcodeScanner import de.docutain.sdk.barcode.data.BarcodeFormat import org.apache.cordova.CallbackContext import org.apache.cordova.CordovaPlugin import org.json.JSONArray import org.json.JSONException import org.json.JSONObject class DocutainSdkBarcodePlugin : CordovaPlugin() { private val logTag = "DocutainSdkBarcode" private var AktCallbackContext: CallbackContext? = null private val SCAN_BARCODE_RESULT_CODE = 1 private val KEY_DETECTION_SCANNED_LIST = "DETECTION_SCANNED_LIST" private var InitSDKOk:Boolean = false private fun barcodeReturn(barcode:Barcode?): JSONObject { val ret = JSONObject() if(barcode==null) return ret; ret.put("state", "OK"); ret.put("format", barcode.format); ret.put("type", barcode.type); ret.put("value", barcode.displayValue); ret.put("timestamp", barcode.timestamp); return ret; } override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) { Log.v(logTag, "onActivityResult:" + requestCode) if (intent != null && requestCode == SCAN_BARCODE_RESULT_CODE) { if(resultCode == AppCompatActivity.RESULT_OK || resultCode == AppCompatActivity.RESULT_CANCELED) { var ret = JSONObject() if(resultCode == AppCompatActivity.RESULT_OK){ var nativeBarcode: Barcode? = null if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { nativeBarcode = intent?.getParcelableExtra(KEY_DETECTION_SCANNED_LIST, Barcode::class.java) } else { nativeBarcode = intent?.getParcelableExtra(KEY_DETECTION_SCANNED_LIST) as? Barcode } ret = barcodeReturn(nativeBarcode) } else { ret.put("statee", "CANCELED"); } AktCallbackContext?.success(ret) } else { AktCallbackContext?.error("resultCode: "+resultCode) } } } @Throws(JSONException::class) override fun execute(action: String, args: JSONArray, callbackContext: CallbackContext): Boolean { Log.v(logTag, "Plugin execute:$action") when (action) { "initSDK"-> { val licenseKey = args.getString(0) initSDK(licenseKey, callbackContext) } "deleteTempFiles"-> { val deleteTraceFileContent = args.getBoolean(0) deleteTempFiles(deleteTraceFileContent, callbackContext) } "scanBarcode"-> scanBarcode(args.getJSONObject(0), callbackContext) "setLogLevel" -> { val logLevel = args.getString(0) setLogLevel(logLevel, callbackContext) } "getTraceFile"-> getTraceFile(callbackContext) else -> { Log.e(logTag, "Plugin execute:$action unknown") return false } } return true } private fun initSDK(licenseKey: String, callbackContext: CallbackContext) { if(InitSDKOk) { callbackContext.success() return } Log.v(logTag, "InitSDK Start") val context = this.cordova.getActivity().getApplicationContext() if(!DocutainSDK.initSDK(context as android.app.Application, licenseKey)){ //init of Docutain SDK failed, get the last error message InitSDKOk = false Log.e(logTag, "Initialization of the Docutain SDK failed: "+ DocutainSDK.getLastError()) callbackContext.error(DocutainSDK.getLastError()) } else { InitSDKOk = true Log.d(logTag, "initSDK Ok: \$licenseKey") callbackContext.success() } } private fun deleteTempFiles(deleteTraceFileContent: Boolean ,callbackContext: CallbackContext) { try { Log.v(logTag, "deleteTempFiles" + deleteTraceFileContent) if(DocutainSDK.deleteTempFiles(deleteTraceFileContent)) callbackContext.success() else callbackContext.error(DocutainSDK.getLastError()) } catch(ex:Exception ) { Log.e(logTag, "deleteTempFiles Exception"+ ex) callbackContext.error("deleteTempFiles Exception"+ ex) } } private fun setLogLevel(logLevel: String, callbackContext: CallbackContext) { Log.v(logTag, "setLogLevel:" + logLevel) if(logLevel.isNullOrEmpty()) { callbackContext.error("LogLevel is null") } else { try { val level: Logger.Level = getLogLevelFromString(logLevel) Log.d(logTag, "setLogLevel " + level) Logger.setLogLevel(level) callbackContext.success() } catch (ex: Exception) { Log.e(logTag, "setLogLevel Exception" + ex) callbackContext.error("setLogLevel Exception"+ ex) } } } private fun getTraceFile(callbackContext: CallbackContext) { try { val text = Logger.getTraceFile().getPath() callbackContext.success(text) } catch(ex:Exception ) { Log.e(logTag, "getTraceFile Exception"+ ex) callbackContext.error("getTraceFile Exception"+ ex) } } private fun scanBarcode(scannerConfiguration: JSONObject, callbackContext: CallbackContext) { Log.v(logTag, "scanDocument Start") try { AktCallbackContext = callbackContext val nativeScanConfig = BarcodeScannerConfiguration() if(!mapBarcodeScannerConfiguration(scannerConfiguration, nativeScanConfig)) { callbackContext.error("scanBarcode config not valid") } else { val currentActivity = this.cordova.getActivity() val intent = BarcodeScanner.newScanIntent(currentActivity.getApplicationContext(), nativeScanConfig) cordova.setActivityResultCallback(this) cordova.startActivityForResult(this as CordovaPlugin, intent, SCAN_BARCODE_RESULT_CODE) Log.v(logTag, "scanDocument start OK") } } catch(ex:Exception ) { Log.e(logTag, "scanDocument Exception"+ ex) callbackContext.error("scanDocument Exception"+ ex) } } private fun getLogLevelFromString(loglevel: String) : Logger.Level { when (loglevel.uppercase()) { "DISABLE" -> return Logger.Level.DISABLE "ASSERT" -> return Logger.Level.ASSERT "ERROR" -> return Logger.Level.ERROR "WARNING" -> return Logger.Level.WARNING "INFO" -> return Logger.Level.INFO "DEBUG" -> return Logger.Level.DEBUG "VERBOSE" -> return Logger.Level.VERBOSE else -> return Logger.Level.ERROR } } private fun mapBarcodeScannerConfiguration(options: JSONObject, scanConfig: BarcodeScannerConfiguration): Boolean { try { if(options == null){ return false } if(options.has("beepOnSuccess")) scanConfig.beepOnSuccess = options.getBoolean("beepOnSuccess") if(options.has("vibrateOnSuccess")) scanConfig.vibrateOnSuccess = options.getBoolean("vibrateOnSuccess") if(options.has("generateBarcodeImage")) scanConfig.generateBarcodeImage = options.getBoolean("generateBarcodeImage") if(options.has("detectionHintTitle")) scanConfig.textConfig.setDetectionHintTitle(options.getString("detectionHintTitle")!!) if(options.has("bottomImage")){ val imgSource = options.getString("bottomImage")!! val imgIdentifier = DocutainSDK.context.resources.getIdentifier(imgSource, "drawable", DocutainSDK.context.packageName) scanConfig.bottomImage = imgIdentifier } if(options.has("codeFormats")){ val items = options.get("codeFormats") as JSONArray if(items != null) { val codeFormats = ArrayList() for (i in 0 ..items.length()-1) { codeFormats.add(BarcodeFormat.valueOf(items.getString(i))) } scanConfig.codeFormats = codeFormats } } } catch(ex:Exception ) { Log.e("DocutainSdk", "mapBarcodeScannerConfiguration Exception" + ex) return false } return true } }