Browse Source

Serial connections

connman
adityapk00 5 years ago
parent
commit
20af404598
  1. 11
      app/src/main/java/com/adityapk/zcash/zqwandroid/DataModel.kt
  2. 15
      app/src/main/java/com/adityapk/zcash/zqwandroid/MainActivity.kt

11
app/src/main/java/com/adityapk/zcash/zqwandroid/DataModel.kt

@ -86,6 +86,9 @@ object DataModel {
return when (json.string("command")) {
"getInfo" -> {
mainResponseData = Klaxon().parse<MainResponse>(response)
// Call the next API call
ws?.send(encrypt(json { obj("command" to "getTransactions") }.toJsonString()))
return ParseResponse(false, null)
}
"getTransactions" -> {
@ -133,8 +136,9 @@ object DataModel {
// Connected, but we don't have a secret, so we can't actually connect.
ws?.close(1000, "No shared secret, can't connect")
} else {
// We make only the first API call here. The subsequent ones are made in processMessage(), when this
// call returns a reply
ws?.send(encrypt(json { obj("command" to "getInfo") }.toJsonString()))
ws?.send(encrypt(json { obj("command" to "getTransactions") }.toJsonString()))
}
}
@ -154,7 +158,7 @@ object DataModel {
// Decrypt the hex string into a regular string and return
// First make sure the remote nonce is valid
checkAndUpdateRemoteNonce(nonceHex);
checkAndUpdateRemoteNonce(nonceHex)
val encsize = encHex.length / 2
val encbin = encHex.hexStringToByteArray(encHex.length / 2)
@ -163,11 +167,8 @@ object DataModel {
val noncebin = nonceHex.hexStringToByteArray(Sodium.crypto_secretbox_noncebytes())
Sodium.crypto_secretbox_open_easy(decrypted, encbin, encsize, noncebin, getSecret())
val s = String(decrypted)
println("Decrypted to: ${String(decrypted)}")
return String(decrypted)
}

15
app/src/main/java/com/adityapk/zcash/zqwandroid/MainActivity.kt

@ -307,7 +307,7 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
when(requestCode) {
QrReaderActivity.REQUEST_CONNDATA -> {
if (resultCode == Activity.RESULT_OK) {
Log.i(TAG, "Main Activity got result for QrCode")
Log.i(TAG, "Main Activity got result for QrCode: ${data?.dataString}")
// Check to make sure that the result is an actual address
if (!(data?.dataString ?: "").startsWith("ws")) {
@ -338,6 +338,9 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
private fun disconnected() {
Log.i(TAG, "Disconnected")
connStatus = ConnectionStatus.DISCONNECTED
DataModel.clear()
updateUI(true)
}
@ -345,8 +348,12 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
private fun clearConnection() {
Log.i(TAG, "Clearing connection")
connStatus = ConnectionStatus.DISCONNECTED
DataModel.ws?.close(1000, "Forcibly closing connection")
// If the server returned an error, we need to clear out the connection,
// forcing a reconnection
DataModel.setConnString(null, applicationContext)
disconnected()
}
@ -358,7 +365,7 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
}
override fun onMessage(webSocket: WebSocket?, text: String?) {
Log.i(TAG, "Recieving $text")
Log.i(TAG, "Receiving $text")
val r = DataModel.parseResponse(text!!)
if (r.error == null) {
@ -388,7 +395,7 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
Snackbar.make(layoutConnect, t.localizedMessage, Snackbar.LENGTH_SHORT).show()
}
clearConnection()
disconnected();
}
}

Loading…
Cancel
Save