Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 67 additions & 69 deletions android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<Tag>(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<Tag>(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?) {
Expand All @@ -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)
}

Expand All @@ -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")
Expand All @@ -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")
}
Expand Down
Loading