package io.scanbot.barcodesdk.capacitor import android.app.Application 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.mock.SBBMock import io.scanbot.sdk_wrapper.barcode.operations.parsing.SBBBarcodeDocumentParser 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 org.json.JSONObject @CapacitorPlugin( name = "ScanbotBarcodeSDK", requestCodes = [SBRtuUiRequestCodes.BARCODE_SCANNER] ) class ScanbotBarcodeSDKPlugin : Plugin() { private var barcodeItemMapperPluginCall: PluginCall? = null @PluginMethod fun initialize(pluginCall: PluginCall) { SBBSetupOperations.initializeSdk( (context.applicationContext as Application), pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun getLicenseInfo(pluginCall: PluginCall) { SBBLicenseOperations.getLicenseInfo(ScanbotBarcodeSDKPluginResultDelegate(pluginCall)) } @PluginMethod fun cleanupStorage(pluginCall: PluginCall) { SBBSetupOperations.cleanup(ScanbotBarcodeSDKPluginResultDelegate(pluginCall)) } @PluginMethod fun mockCamera(pluginCall: PluginCall) { SBBMock.camera( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun startBarcodeScanner(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 forceCloseBarcodeScanner(pluginCall: PluginCall) { pluginCall.unimplemented() } @PluginMethod fun scanBarcodesFromImage(pluginCall: PluginCall) { SBBDetectBarcodes.detectOnImage( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun scanBarcodesFromPdf(pluginCall: PluginCall){ SBBDetectBarcodes.detectOnPdf( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun parseBarcodeDocument(pluginCall: PluginCall){ SBBBarcodeDocumentParser.parseDocument( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun readImageData(pluginCall: PluginCall) { SBBImageOperations.getImageData( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } //region ImageRef @PluginMethod fun imageRefSerialize(pluginCall: PluginCall) { SBBImageOperations.imageRefSerialize( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefDeserialize(pluginCall: PluginCall) { SBBImageOperations.imageRefDeserialize( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefFromImageFileUri(pluginCall: PluginCall) { SBBImageOperations.imageRefFromImageFileUri( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefFromEncodedBuffer(pluginCall: PluginCall) { SBBImageOperations.imageRefFromEncodedBuffer( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefHibernate(pluginCall: PluginCall) { SBBImageOperations.imageRefHibernate( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefClear(pluginCall: PluginCall) { SBBImageOperations.imageRefClear( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefInfo(pluginCall: PluginCall) { SBBImageOperations.imageRefInfo( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefSaveImage(pluginCall: PluginCall) { SBBImageOperations.imageRefSaveImage( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefEncodeImage(pluginCall: PluginCall) { SBBImageOperations.imageRefEncodeImage( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefRelease(pluginCall: PluginCall) { SBBImageOperations.imageRefRelease( pluginCall.data, ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun makeSnapshot(pluginCall: PluginCall) { SBBImageOperations.makeSnapshot( ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } @PluginMethod fun imageRefReleaseAll(pluginCall: PluginCall) { SBBImageOperations.releaseAllImageRefs( ScanbotBarcodeSDKPluginResultDelegate(pluginCall) ) } //endregion ImageRef //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 text = barcodeItem.getString("text") val ext = barcodeItem.getString("upcEanExtension") val format = barcodeItem.getString("format") return "${text}${ext}_$format" } @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) } } }