Skip to content

Commit

Permalink
18013-5 Reader in Kotlin (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Juliano Cezar Chagas Tavares <[email protected]>
  • Loading branch information
rschulman and Juliano1612 authored Nov 5, 2024
1 parent 39b52da commit 2946f9b
Show file tree
Hide file tree
Showing 22 changed files with 1,008 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.spruceid.mobile.sdk

import android.app.Application
import android.bluetooth.BluetoothManager
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import com.spruceid.mobile.sdk.rs.ItemsRequest
import com.spruceid.mobile.sdk.rs.MdlPresentationSession
Expand All @@ -15,7 +17,7 @@ import java.security.KeyStore
import java.security.Signature
import java.util.UUID

class CredentialsViewModel : ViewModel() {
class CredentialsViewModel(application: Application) : AndroidViewModel(application) {

private val _credentials = MutableStateFlow<ArrayList<ParsedCredential>>(arrayListOf())
val credentials = _credentials.asStateFlow()
Expand Down Expand Up @@ -44,7 +46,7 @@ class CredentialsViewModel : ViewModel() {
_credentials.value.add(credential)
}

private fun firstMdoc(): Mdoc {
private fun firstMdoc(): Mdoc {
val mdoc = _credentials.value
.map { credential -> credential.asMsoMdoc() }
.firstOrNull()
Expand Down Expand Up @@ -103,6 +105,7 @@ class CredentialsViewModel : ViewModel() {
"Central",
_session.value!!.getBleIdent(),
::updateRequestData,
getApplication<Application>().applicationContext,
null
)
}
Expand All @@ -116,7 +119,7 @@ class CredentialsViewModel : ViewModel() {

fun submitNamespaces(allowedNamespaces: Map<String, Map<String, List<String>>>) {
val mdoc = this.firstMdoc()
if(allowedNamespaces.isEmpty()) {
if (allowedNamespaces.isEmpty()) {
val e = Error("Select at least one namespace")
Log.e("CredentialsViewModel.submitNamespaces", e.toString())
_currState.value = PresentmentState.ERROR
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spruceid.mobile.sdk

import android.bluetooth.BluetoothManager
import android.content.Context
import android.util.Log
import com.spruceid.mobile.sdk.rs.ItemsRequest
import com.spruceid.mobile.sdk.rs.MdlPresentationSession
Expand All @@ -20,7 +21,8 @@ class IsoMdlPresentation(
val mdoc: Mdoc,
val keyAlias: String,
val bluetoothManager: BluetoothManager,
val callback: BLESessionStateDelegate
val callback: BLESessionStateDelegate,
val context: Context
) {
val uuid: UUID = UUID.randomUUID()
var session: MdlPresentationSession? = null
Expand All @@ -39,6 +41,7 @@ class IsoMdlPresentation(
"Central",
session!!.getBleIdent(),
::updateRequestData,
context,
callback
)
this.callback.update(mapOf(Pair("engagingQRCode", session!!.getQrCodeUri())))
Expand Down
54 changes: 54 additions & 0 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/IsoMdlReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.spruceid.mobile.sdk

import android.bluetooth.BluetoothManager
import android.content.Context
import android.util.Log
import com.spruceid.mobile.sdk.rs.MDocItem
import com.spruceid.mobile.sdk.rs.MdlReaderResponseException
import com.spruceid.mobile.sdk.rs.MdlSessionManager
import com.spruceid.mobile.sdk.rs.establishSession
import java.util.UUID

class IsoMdlReader(
val callback: BLESessionStateDelegate,
uri: String,
requestedItems: Map<String, Map<String, Boolean>>,
trustAnchorRegistry: List<String>?,
platformBluetooth: BluetoothManager,
context: Context
) {
private lateinit var session: MdlSessionManager
private lateinit var bleManager: Transport

init {
try {
val sessionData = establishSession(uri, requestedItems, trustAnchorRegistry)

session = sessionData.state
bleManager = Transport(platformBluetooth)
bleManager.initialize(
"Reader",
UUID.fromString(sessionData.uuid),
"BLE",
"Peripheral",
sessionData.bleIdent,
null,
context,
callback,
sessionData.request
)

} catch (e: Error) {
Log.e("BleSessionManager.constructor", e.toString())
}
}

fun handleResponse(response: ByteArray): Map<String, Map<String, MDocItem>> {
try {
val responseData = com.spruceid.mobile.sdk.rs.handleResponse(session, response)
return responseData.verifiedResponse
} catch (e: MdlReaderResponseException) {
throw e
}
}
}
11 changes: 8 additions & 3 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/Transport.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spruceid.mobile.sdk

import android.bluetooth.BluetoothManager
import android.content.Context
import android.util.Log
import java.util.*

Expand All @@ -21,8 +22,10 @@ class Transport(private var bluetoothManager: BluetoothManager) {
deviceRetrieval: String,
deviceRetrievalOption: String,
ident: ByteArray,
updateRequestData: (data: ByteArray) -> Unit,
callback: BLESessionStateDelegate?
updateRequestData: ((data: ByteArray) -> Unit)? = null,
context: Context,
callback: BLESessionStateDelegate?,
encodedEDeviceKeyBytes: ByteArray = ByteArray(0)
) {

/**
Expand All @@ -42,7 +45,9 @@ class Transport(private var bluetoothManager: BluetoothManager) {
deviceRetrievalOption,
ident,
updateRequestData,
callback
context,
callback,
encodedEDeviceKeyBytes
)
}

Expand Down
60 changes: 48 additions & 12 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/TransportBle.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spruceid.mobile.sdk

import android.bluetooth.BluetoothManager
import android.content.Context
import android.util.Log
import java.util.*

Expand All @@ -11,6 +12,7 @@ class TransportBle(private var bluetoothManager: BluetoothManager) {

private lateinit var transportBleCentralClientHolder: TransportBleCentralClientHolder
private lateinit var transportBlePeripheralServerHolder: TransportBlePeripheralServerHolder
private lateinit var transportBlePeripheralServerReader: TransportBlePeripheralServerReader

/**
* Reserved for later matching with available cbor options.
Expand All @@ -29,24 +31,28 @@ class TransportBle(private var bluetoothManager: BluetoothManager) {
fun initialize(
application: String, serviceUUID: UUID,
deviceRetrievalOption: String, ident: ByteArray,
updateRequestData: (data: ByteArray) -> Unit,
callback: BLESessionStateDelegate?
updateRequestData: ((data: ByteArray) -> Unit)? = null,
context: Context,
callback: BLESessionStateDelegate?,
encodedEDeviceKeyBytes: ByteArray
) {

/**
* Transport Central Client Holder
*/
if (deviceRetrievalOption == "Central" && application == "Holder") {
Log.d("TransportBle.initialize", "-- Selecting Transport Central Client Holder --")

transportBleCentralClientHolder = TransportBleCentralClientHolder(
application,
bluetoothManager,
serviceUUID,
updateRequestData,
callback,
)
transportBleCentralClientHolder.connect(ident)
if (updateRequestData != null) {
transportBleCentralClientHolder = TransportBleCentralClientHolder(
application,
bluetoothManager,
serviceUUID,
updateRequestData,
context,
callback,
)
transportBleCentralClientHolder.connect(ident)
}
}

/**
Expand All @@ -56,9 +62,31 @@ class TransportBle(private var bluetoothManager: BluetoothManager) {
Log.d("TransportBle.initialize", "-- Selecting Peripheral Server Holder --")

transportBlePeripheralServerHolder =
TransportBlePeripheralServerHolder(application, bluetoothManager, serviceUUID)
TransportBlePeripheralServerHolder(
application,
bluetoothManager,
serviceUUID,
context
)
transportBlePeripheralServerHolder.start()
}

/**
* Transport Peripheral Server Reader
*/
if (deviceRetrievalOption == "Peripheral" && application == "Reader") {
Log.d("TransportBle.initialize", "-- Selecting Peripheral Server Reader --")

transportBlePeripheralServerReader =
TransportBlePeripheralServerReader(
callback,
application,
bluetoothManager,
serviceUUID,
context
)
transportBlePeripheralServerReader.start(ident, encodedEDeviceKeyBytes)
}
}

/**
Expand All @@ -85,6 +113,10 @@ class TransportBle(private var bluetoothManager: BluetoothManager) {
if (this::transportBlePeripheralServerHolder.isInitialized) {
transportBlePeripheralServerHolder.stop()
}

if (this::transportBlePeripheralServerReader.isInitialized) {
transportBlePeripheralServerReader.stop()
}
}

/**
Expand All @@ -98,5 +130,9 @@ class TransportBle(private var bluetoothManager: BluetoothManager) {
if (this::transportBlePeripheralServerHolder.isInitialized) {
transportBlePeripheralServerHolder.hardReset()
}

if (this::transportBlePeripheralServerReader.isInitialized) {
transportBlePeripheralServerReader.hardReset()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ class TransportBleCentralClientHolder(
private var bluetoothManager: BluetoothManager,
private var serviceUUID: UUID,
private var updateRequestData: (data: ByteArray) -> Unit,
private var context: Context,
private var callback: BLESessionStateDelegate?,
) : Activity() {

private var context: Context = this

) {
private var bluetoothAdapter: BluetoothAdapter? = null

private lateinit var previousAdapterName: String
Expand Down Expand Up @@ -98,10 +96,17 @@ class TransportBleCentralClientHolder(
"progress: $progress max: $max"
)

if(progress == max) {
if (progress == max) {
callback?.update(mapOf(Pair("success", "")))
} else {
callback?.update(mapOf(Pair("uploadProgress", mapOf(Pair("curr", progress), Pair("max", max)))))
callback?.update(
mapOf(
Pair(
"uploadProgress",
mapOf(Pair("curr", progress), Pair("max", max))
)
)
)
}
}

Expand Down
Loading

0 comments on commit 2946f9b

Please sign in to comment.