From 73ec4dbc9697021b10722e2f658e05a37c057536 Mon Sep 17 00:00:00 2001 From: Daniel Fernando Rico Date: Tue, 9 Dec 2025 11:49:24 -0500 Subject: [PATCH] fix: update kotlin modules to support latest version of kotlin and RN 0.81 --- .../rn/eidreader/EIdReaderModule.kt | 136 +++++++++--------- 1 file changed, 67 insertions(+), 69 deletions(-) diff --git a/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt b/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt index a14f689..fedf13e 100644 --- a/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt +++ b/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt @@ -10,7 +10,6 @@ import android.content.Intent import android.content.IntentFilter import android.content.pm.PackageManager import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.nfc.NfcAdapter import android.nfc.Tag import android.nfc.tech.IsoDep @@ -36,12 +35,7 @@ import kotlinx.coroutines.launch import org.jmrtd.BACKey import org.jmrtd.BACKeySpec import org.json.JSONObject -import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream -import java.io.InputStream -import jj2000.j2k.decoder.Decoder -import jj2000.j2k.util.ParameterList -import org.jmrtd.lds.AbstractImageInfo import java.io.IOException @@ -99,7 +93,7 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : override fun onHostResume() { try { adapter?.let { - currentActivity?.let { activity -> + reactApplicationContext.currentActivity?.let { activity -> val intent = Intent(activity, activity.javaClass).apply { addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) } @@ -144,59 +138,60 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : override fun onHostDestroy() { try { - adapter?.disableForegroundDispatch(currentActivity) + adapter?.disableForegroundDispatch(reactApplicationContext.currentActivity) } catch (e: Exception) { Log.w("EIdReader", e.message ?: "onHostDestroy Unknown Error") } } - override fun onActivityResult(p0: Activity?, p1: Int, p2: Int, p3: Intent?) { + override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) { } - override fun onNewIntent(p0: Intent?) { - p0?.let { intent -> - if (!isReading) return + override fun onNewIntent(intent: Intent) { + intent.let { intent -> + if (!isReading) return - sendEvent("onTagDiscovered", null) - currentActivity?.runOnUiThread(Runnable { - _dialog?.setMessage("ID Document found.") - }) - - - if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) { - val tag = intent.extras!!.getParcelable(NfcAdapter.EXTRA_TAG) - - if (listOf(*tag!!.techList).contains("android.nfc.tech.IsoDep")) { - CoroutineScope(Dispatchers.IO).launch { - try { - val bacKey: BACKeySpec = BACKey( - mrzInfo!!.documentNo, - mrzInfo!!.birthDate, - mrzInfo!!.expiryDate - ) - - currentActivity?.runOnUiThread(Runnable { - val message = labels?.getString("reading") ?: "Reading. Hold your document..." - _dialog?.setMessage(message) - }) - - val result = nfcPassportReader.readPassport(IsoDep.get(tag), bacKey, includeImages, includeRawData) + sendEvent("onTagDiscovered", null) + reactApplicationContext.currentActivity?.runOnUiThread { + _dialog?.setMessage("ID Document found.") + } - val map = result.serializeToMap() - val reactMap = jsonToReactMap.convertJsonToMap(JSONObject(map)) - stopReading() - _promise?.resolve(reactMap) - } catch (e: Exception) { - reject(e) - } + if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) { + val tag = intent.extras!!.getParcelable(NfcAdapter.EXTRA_TAG) + + if (listOf(*tag!!.techList).contains("android.nfc.tech.IsoDep")) { + CoroutineScope(Dispatchers.IO).launch { + try { + val bacKey: BACKeySpec = BACKey( + mrzInfo!!.documentNo, + mrzInfo!!.birthDate, + mrzInfo!!.expiryDate + ) + + reactApplicationContext.currentActivity?.runOnUiThread { + val message = + labels?.getString("reading") ?: "Reading. Hold your document..." + _dialog?.setMessage(message) + } + + val result = nfcPassportReader.readPassport(IsoDep.get(tag), bacKey, includeImages, includeRawData) + + val map = result.serializeToMap() + val reactMap = jsonToReactMap.convertJsonToMap(JSONObject(map)) + + stopReading() + _promise?.resolve(reactMap) + } catch (e: Exception) { + reject(e) + } + } + } else { + reject(Exception("Tag tech is not IsoDep")) + } } - } else { - reject(Exception("Tag tech is not IsoDep")) - } } - } } private fun sendEvent(eventName: String, params: Any?) { @@ -205,11 +200,12 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : } private fun reject(e: Exception) { - currentActivity?.runOnUiThread(Runnable { - val message = labels?.getString("error") ?: "Sorry, there was a problem reading the passport. Please try again" - _dialog?.setMessage(message) - }) - stopReading(false) + reactApplicationContext.currentActivity?.runOnUiThread { + val message = labels?.getString("error") + ?: "Sorry, there was a problem reading the passport. Please try again" + _dialog?.setMessage(message) + } + stopReading(false) _promise?.reject(e) } @@ -218,8 +214,8 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : readableMap?.let { try { _promise = promise - labels = readableMap?.getMap("labels") - val mrzMap = readableMap?.getMap("mrzInfo") + labels = readableMap.getMap("labels") + val mrzMap = readableMap.getMap("mrzInfo") val mrzExpirationDate = mrzMap?.getString("expirationDate") val mrzBirthDate = mrzMap?.getString("birthDate") val mrzDocumentNumber = mrzMap?.getString("documentNumber") @@ -236,25 +232,27 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : includeRawData = readableMap.hasKey("includeRawData") && readableMap.getBoolean("includeRawData") - val currentActivity = currentActivity + val currentActivity = reactApplicationContext.currentActivity if (currentActivity != null) { - currentActivity?.runOnUiThread(Runnable { - val title = labels?.getString("title") ?: "Ready to Scan" - val message = labels?.getString("requestPresentPassport") ?: "Hold your phone near an NFC enabled passport" - val cancelButton = labels?.getString("cancelButton") ?: "Cancel" - val builder = AlertDialog.Builder(currentActivity) - builder.setTitle(title) - .setMessage(message) - .setCancelable(true) - .setNegativeButton(cancelButton) { dialog, _ -> + currentActivity.runOnUiThread { + val title = labels?.getString("title") ?: "Ready to Scan" + val message = labels?.getString("requestPresentPassport") + ?: "Hold your phone near an NFC enabled passport" + val cancelButton = labels?.getString("cancelButton") ?: "Cancel" + val builder = AlertDialog.Builder(currentActivity) + builder.setTitle(title) + .setMessage(message) + .setCancelable(true) + .setNegativeButton(cancelButton) { dialog, _ -> stopReading() - val reactMap = jsonToReactMap.convertJsonToMap(JSONObject("{status: 'Canceled' }")) + val reactMap = + jsonToReactMap.convertJsonToMap(JSONObject("{status: 'Canceled' }")) _promise?.resolve(reactMap) - } - _dialog = builder.create() - _dialog?.show() - }) + } + _dialog = builder.create() + _dialog?.show() + } } else { promise.reject("ActivityNotFound", "Current activity is null") }