package io.scanbot.barcodesdk.capacitor import android.content.Intent import com.getcapacitor.JSObject import com.getcapacitor.Plugin import com.getcapacitor.PluginCall import com.getcapacitor.PluginMethod import com.getcapacitor.annotation.CapacitorPlugin import io.scanbot.sdk_wrapper.barcode.operations.detection.SBBDetectBarcodes import io.scanbot.sdk_wrapper.barcode.operations.image_operations.SBBImageOperations import io.scanbot.sdk_wrapper.barcode.operations.license.SBBLicenseOperations import io.scanbot.sdk_wrapper.barcode.operations.pdf_extraction.SBBExtractFromPdf import io.scanbot.sdk_wrapper.barcode.operations.setup.SBBSetupOperations import io.scanbot.sdk_wrapper.barcode.rtuui.SBBRtuUi import io.scanbot.sdk_wrapper.barcode.rtuui.SBRtuUiRequestCodes import io.scanbot.sdk_wrapper.extensions.getStringOrDefault import org.json.JSONObject @CapacitorPlugin( name = "ScanbotBarcodeSDK", requestCodes = [ SBRtuUiRequestCodes.LEGACY_BARCODE_SCANNER, SBRtuUiRequestCodes.LEGACY_BATCH_BARCODE_SCANNER, SBRtuUiRequestCodes.BARCODE_SCANNER ] ) class ScanbotBarcodeSDKPlugin : Plugin() { private var barcodeItemMapperPluginCall: PluginCall? = null @PluginMethod fun initializeSdk(pluginCall: PluginCall) { SBBSetupOperations.initializeSdk( activity.application, pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun getLicenseInfo(pluginCall: PluginCall) { SBBLicenseOperations.getLicenseInfo(ScanbotBarcodeSDKPluginResultDelegate(pluginCall)) } @PluginMethod fun cleanup(pluginCall: PluginCall) { SBBSetupOperations.cleanup(ScanbotBarcodeSDKPluginResultDelegate(pluginCall)) } @PluginMethod fun startBarcodeScanner(pluginCall: PluginCall) { SBBRtuUi.startLegacyBarcodeScanner( activity, pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun closeBarcodeScanner(pluginCall: PluginCall) { SBBRtuUi.closeLegacyBarcodeScanner( activity, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun startBatchBarcodeScanner(pluginCall: PluginCall) { SBBRtuUi.startLegacyBatchBarcodeScanner( activity, pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun closeBatchBarcodeScanner(pluginCall: PluginCall) { SBBRtuUi.closeLegacyBatchBarcodeScanner( activity, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun startBarcodeScannerV2(pluginCall: PluginCall) { if (barcodeItemMapperPluginCall != null) { SBBRtuUi.startBarcodeScanner( activity, pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) { barcodeItem -> barcodeItemMapperPluginCall?.resolve(JSObject.fromJSONObject(barcodeItem)) ?: run { SBBRtuUi.onBarcodeItemMapper(barcodeItemUuid(barcodeItem)) } } } else { SBBRtuUi.startBarcodeScanner( activity, pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } } @PluginMethod fun closeBarcodeScannerV2(pluginCall: PluginCall) { pluginCall.unimplemented("Not implemented on Android") } @PluginMethod fun detectBarcodesOnImage(pluginCall: PluginCall) { SBBDetectBarcodes.detectOnImage( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun extractImagesFromPDF(pluginCall: PluginCall) { SBBExtractFromPdf.extractImages( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun getImageData(pluginCall: PluginCall) { SBBImageOperations.getImageData( pluginCall.getString("imageFileUri", "")!!, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } //region Helper methods that are used only by us from the plugin middle layer (not exposed to the users) @PluginMethod(returnType = PluginMethod.RETURN_NONE) fun registerBarcodeItemMapperCallback(pluginCall: PluginCall) { barcodeItemMapperPluginCall = pluginCall.apply { setKeepAlive(true) } } @PluginMethod(returnType = PluginMethod.RETURN_NONE) fun onBarcodeItemMapper(pluginCall: PluginCall) { val barcodeMappedData = pluginCall.getObject("barcodeMappedData") SBBRtuUi.onBarcodeItemMapper( pluginCall.getString("barcodeItemUuid")!!, barcodeMappedData ) } //endregion Helper methods private fun barcodeItemUuid(barcodeItem: JSONObject): String { val textWithExt = barcodeItem.getString("textWithExtension") val type = barcodeItem.getStringOrDefault("type", "")!! return "${textWithExt}_$type" } @Suppress("OVERRIDE_DEPRECATION", "DEPRECATION") override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (SBBRtuUi.onActivityResult(requestCode, resultCode, data)) { if (requestCode == SBRtuUiRequestCodes.BARCODE_SCANNER) { barcodeItemMapperPluginCall?.release(bridge) barcodeItemMapperPluginCall = null } } else { super.handleOnActivityResult(requestCode, resultCode, data) } } }