@@ -16,10 +16,14 @@ import kotlinx.serialization.Serializable
1616import org.json.JSONObject
1717import java.lang.ref.WeakReference
1818
19- internal class CommunicationClient (context : Context , callback : EthereumEventCallback ? , private val logger : Logger = DefaultLogger ) {
19+ class CommunicationClient (
20+ context : Context ,
21+ callback : EthereumEventCallback ? ,
22+ private val sessionManager : SessionManager ,
23+ private val keyExchange : KeyExchange ,
24+ private val logger : Logger = DefaultLogger ) {
2025
2126 var sessionId: String = " "
22- private val keyExchange: KeyExchange = KeyExchange ()
2327
2428 var dappMetadata: DappMetadata ? = null
2529 var isServiceConnected = false
@@ -35,20 +39,23 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
3539 private var submittedRequests: MutableMap <String , SubmittedRequest > = mutableMapOf ()
3640 private var queuedRequests: MutableMap <String , SubmittedRequest > = mutableMapOf ()
3741
38- private var sessionManager: SessionManager
39-
4042 private var isMetaMaskReady = false
4143 private var sentOriginatorInfo = false
4244 private var requestedBindService = false
4345
46+ var hasSubmittedRequests: Boolean = submittedRequests.isEmpty()
47+ var hasRequestJobs: Boolean = requestJobs.isEmpty()
48+ var hasQueuedRequests: Boolean = queuedRequests.isEmpty()
49+
4450 var enableDebug: Boolean = false
4551 set(value) {
4652 field = value
4753 tracker.enableDebug = value
4854 }
4955
5056 init {
51- sessionManager = SessionManager (KeyStorage (context))
57+ sessionId = sessionManager.sessionId
58+ // in case not yet initialised in SessionManager
5259 sessionManager.onInitialized = {
5360 sessionId = sessionManager.sessionId
5461 }
@@ -142,7 +149,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
142149 sentOriginatorInfo = false
143150 }
144151
145- private fun handleMessage (message : String ) {
152+ fun handleMessage (message : String ) {
146153 val jsonString = keyExchange.decrypt(message)
147154 val json = JSONObject (jsonString)
148155
@@ -187,7 +194,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
187194 }
188195 }
189196
190- private fun resumeRequestJobs () {
197+ fun resumeRequestJobs () {
191198 logger.log(" CommunicationClient:: Resuming jobs" )
192199
193200 while (requestJobs.isNotEmpty()) {
@@ -196,12 +203,12 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
196203 }
197204 }
198205
199- private fun queueRequestJob (job : () -> Unit ) {
206+ fun queueRequestJob (job : () -> Unit ) {
200207 requestJobs.add(job)
201208 logger.log(" CommunicationClient:: Queued job" )
202209 }
203210
204- private fun clearPendingRequests () {
211+ fun clearPendingRequests () {
205212 queuedRequests = mutableMapOf ()
206213 requestJobs = mutableListOf ()
207214 submittedRequests = mutableMapOf ()
@@ -311,7 +318,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
311318 }
312319 }
313320
314- private fun handleError (error : String , id : String ): Boolean {
321+ fun handleError (error : String , id : String ): Boolean {
315322 if (error.isEmpty()) {
316323 return false
317324 }
@@ -329,7 +336,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
329336 return true
330337 }
331338
332- private fun completeRequest (id : String , result : Result ) {
339+ fun completeRequest (id : String , result : Result ) {
333340 if (queuedRequests[id] != null ) {
334341 queuedRequests[id]?.callback?.invoke(result)
335342 queuedRequests.remove(id)
@@ -338,13 +345,13 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
338345 submittedRequests.remove(id)
339346 }
340347
341- private fun handleEvent (event : JSONObject ) {
348+ fun handleEvent (event : JSONObject ) {
342349 when (event.optString(" method" )) {
343350 EthereumMethod .METAMASK_ACCOUNTS_CHANGED .value -> {
344351 val accountsJson = event.optString(" params" )
345352 val accounts: List <String > = Gson ().fromJson(accountsJson, object : TypeToken <List <String >>() {}.type)
346353 accounts.getOrNull(0 )?.let { account ->
347- logger.error (" CommunicationClient:: Event Updated to account $account " )
354+ logger.log (" CommunicationClient:: Event Updated to account $account " )
348355 updateAccount(account)
349356 }
350357 }
@@ -362,17 +369,17 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
362369 }
363370 }
364371
365- private fun updateAccount (account : String ) {
372+ fun updateAccount (account : String ) {
366373 val callback = ethereumEventCallbackRef.get()
367374 callback?.updateAccount(account)
368375 }
369376
370- private fun updateChainId (chainId : String ) {
377+ fun updateChainId (chainId : String ) {
371378 val callback = ethereumEventCallbackRef.get()
372379 callback?.updateChainId(chainId)
373380 }
374381
375- private fun handleKeyExchange (message : String ) {
382+ fun handleKeyExchange (message : String ) {
376383 val json = JSONObject (message)
377384
378385 val keyExchangeStep = json.optString(KeyExchange .TYPE , KeyExchangeMessageType .KEY_HANDSHAKE_SYN .name)
@@ -396,7 +403,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
396403 }
397404 }
398405
399- private fun sendMessage (message : String ) {
406+ fun sendMessage (message : String ) {
400407 val bundle = Bundle ().apply {
401408 putString(MESSAGE , message)
402409 }
@@ -439,7 +446,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
439446 }
440447 }
441448
442- private fun processRequest (request : RpcRequest , callback : (Result ) -> Unit ) {
449+ fun processRequest (request : RpcRequest , callback : (Result ) -> Unit ) {
443450 logger.log(" CommunicationClient:: sending request $request " )
444451 if (queuedRequests[request.id] != null ) {
445452 queuedRequests.remove(request.id)
@@ -455,7 +462,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
455462 sendMessage(messageJson)
456463 }
457464
458- private fun sendOriginatorInfo () {
465+ fun sendOriginatorInfo () {
459466 if (sentOriginatorInfo) { return }
460467 sentOriginatorInfo = true
461468
@@ -479,7 +486,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
479486 sendMessage(messageJson)
480487 }
481488
482- private fun isQA (): Boolean {
489+ fun isQA (): Boolean {
483490 if (Build .VERSION .SDK_INT < 33 ) { // i.e Build.VERSION_CODES.TIRAMISU
484491 return false
485492 }
@@ -494,7 +501,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
494501 }
495502 }
496503
497- private fun bindService () {
504+ fun bindService () {
498505 logger.log(" CommunicationClient:: Binding service" )
499506 requestedBindService = true
500507
@@ -538,7 +545,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
538545 sendKeyExchangeMesage(keyExchange.toString())
539546 }
540547
541- private fun sendKeyExchangeMesage (message : String ) {
548+ fun sendKeyExchangeMesage (message : String ) {
542549 val bundle = Bundle ().apply {
543550 putString(KEY_EXCHANGE , message)
544551 }
0 commit comments