Hooked up dialog ui

This commit is contained in:
Mikunj 2019-10-01 15:35:52 +10:00
parent 258ad57c12
commit 80e9b8223a
2 changed files with 63 additions and 32 deletions

View File

@ -1047,8 +1047,6 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
boolean valid = isAuthorisationValid(authorisation); boolean valid = isAuthorisationValid(authorisation);
LokiDeviceLinkingSession linkingSession = LokiDeviceLinkingSession.Companion.getShared(); LokiDeviceLinkingSession linkingSession = LokiDeviceLinkingSession.Companion.getShared();
if (valid && linkingSession.isListeningForLinkingRequest()) { if (valid && linkingSession.isListeningForLinkingRequest()) {
// Save to the database and trigger the event
DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(authorisation);
linkingSession.receivedLinkingRequest(authorisation); linkingSession.receivedLinkingRequest(authorisation);
} else { } else {
// Remove pre key bundle from the user // Remove pre key bundle from the user

View File

@ -11,7 +11,13 @@ import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_device_linking.view.* import kotlinx.android.synthetic.main.view_device_linking.view.*
import network.loki.messenger.R import network.loki.messenger.R
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.w3c.dom.Text
import org.whispersystems.signalservice.loki.api.LokiDeviceLinkingSession
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
import org.whispersystems.signalservice.loki.crypto.MnemonicCodec import org.whispersystems.signalservice.loki.crypto.MnemonicCodec
import org.whispersystems.signalservice.loki.utilities.removing05PrefixIfNeeded import org.whispersystems.signalservice.loki.utilities.removing05PrefixIfNeeded
import java.io.File import java.io.File
@ -30,6 +36,7 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
private var delegate: DeviceLinkingDialogDelegate? = null private var delegate: DeviceLinkingDialogDelegate? = null
private lateinit var languageFileDirectory: File private lateinit var languageFileDirectory: File
var dismiss: (() -> Unit)? = null var dismiss: (() -> Unit)? = null
private var pairingAuthorisation: LokiPairingAuthorisation? = null
// region Types // region Types
enum class Mode { Master, Slave } enum class Mode { Master, Slave }
@ -46,10 +53,7 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
} }
setUpLanguageFileDirectory() setUpLanguageFileDirectory()
setUpViewHierarchy() setUpViewHierarchy()
when (mode) { LokiDeviceLinkingSession.shared.startListeningForLinkingRequests()
Mode.Master -> Log.d("Loki", "TODO: DeviceLinkingSession.startListeningForLinkingRequests(this)")
Mode.Slave -> Log.d("Loki", "TODO: DeviceLinkingSession.startListeningForAuthorization(this)")
}
} }
private fun setUpLanguageFileDirectory() { private fun setUpLanguageFileDirectory() {
@ -92,14 +96,29 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ") mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ")
} }
authorizeButton.visibility = View.GONE authorizeButton.visibility = View.GONE
authorizeButton.setOnClickListener { authorizeDeviceLink() }
cancelButton.setOnClickListener { cancel() } cancelButton.setOnClickListener { cancel() }
} }
// endregion // endregion
// region Device Linking // region Device Linking
private fun requestUserAuthorization() { // TODO: deviceLink parameter private fun requestUserAuthorization(authorisation: LokiPairingAuthorisation) {
// To be called by DeviceLinkingSession when a linking request has been received // To be called by DeviceLinkingSession when a linking request has been received
// TODO: this.deviceLink = deviceLink if (this.pairingAuthorisation != null) {
Log.e("Loki", "Received request for another pairing authorisation when one was active")
return
}
if (!authorisation.verify()) {
Log.w("Loki", "Received authorisation but it was not valid.")
return
}
this.pairingAuthorisation = authorisation
// Stop listening to any more requests
LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests()
spinner.visibility = View.GONE spinner.visibility = View.GONE
val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams
titleTextViewLayoutParams.topMargin = toPx(16, resources) titleTextViewLayoutParams.topMargin = toPx(16, resources)
@ -107,30 +126,38 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
titleTextView.text = resources.getString(R.string.view_device_linking_title_3) titleTextView.text = resources.getString(R.string.view_device_linking_title_3)
explanationTextView.text = resources.getString(R.string.view_device_linking_explanation_2) explanationTextView.text = resources.getString(R.string.view_device_linking_explanation_2)
mnemonicTextView.visibility = View.VISIBLE mnemonicTextView.visibility = View.VISIBLE
val hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context).removing05PrefixIfNeeded() // TODO: deviceLink.slave.hexEncodedPublicKey.removing05PrefixIfNeeded() val hexEncodedPublicKey = authorisation.secondaryDevicePubKey.removing05PrefixIfNeeded()
mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ") mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ")
authorizeButton.visibility = View.VISIBLE authorizeButton.visibility = View.VISIBLE
} }
private fun authorizeDeviceLink() { private fun authorizeDeviceLink() {
// TODO: val deviceLink = this.deviceLink!! if (pairingAuthorisation == null) { return; }
// TODO: val linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(deviceLink)
// TODO: Send the linking authorization message val authorisation = pairingAuthorisation!!
// TODO: val session = DeviceLinkingSession.current!! val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
// TODO: session.stopListeningForLinkingRequests() val signedAuthorisation = authorisation.sign(LokiPairingAuthorisation.Type.GRANT, userPrivateKey)
// TODO: session.markLinkingRequestAsProcessed() if (signedAuthorisation == null) {
dismiss?.invoke() Log.e("Loki", "Failed to sign grant authorisation")
// TODO: val master = DeviceLink.Device(deviceLink.master.hexEncodedPublicKey, linkingAuthorizationMessage.masterSignature) return
// TODO: val signedDeviceLink = DeviceLink(master, deviceLink.slave) }
// TODO: LokiStorageAPI.addDeviceLink(signedDeviceLink).fail { error ->
// TODO: Log.d("Loki", "Failed to add device link due to error: $error.") // TODO: Send authorisation message
// TODO: }
// Add the auth to the database
DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(signedAuthorisation)
// Update the api
LokiStorageAPI.shared?.updateOurDeviceMappings()
dismiss()
} }
private fun handleDeviceLinkAuthorized() { // TODO: deviceLink parameter private fun handleDeviceLinkAuthorized() { // TODO: deviceLink parameter
// To be called by DeviceLinkingSession when a device link has been authorized // To be called by DeviceLinkingSession when a device link has been authorized
// TODO: val session = DeviceLinkingSession.current!! // Pairings get automatically added to the database when we receive them
// TODO: session.stopListeningForLinkingAuthorization() LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests()
spinner.visibility = View.GONE spinner.visibility = View.GONE
val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams
titleTextViewLayoutParams.topMargin = toPx(8, resources) titleTextViewLayoutParams.topMargin = toPx(8, resources)
@ -143,23 +170,29 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
titleTextView.text = resources.getString(R.string.view_device_linking_title_4) titleTextView.text = resources.getString(R.string.view_device_linking_title_4)
mnemonicTextView.visibility = View.GONE mnemonicTextView.visibility = View.GONE
buttonContainer.visibility = View.GONE buttonContainer.visibility = View.GONE
// TODO: LokiStorageAPI.addDeviceLink(signedDeviceLink).fail { error ->
// TODO: Log.d("Loki", "Failed to add device link due to error: $error.")
// TODO: }
Handler().postDelayed({ Handler().postDelayed({
delegate?.handleDeviceLinkAuthorized() delegate?.handleDeviceLinkAuthorized()
dismiss?.invoke() dismiss()
}, 4000) }, 4000)
} }
// endregion // endregion
// region Interaction // region Interaction
private fun cancel() { private fun dismiss() {
// TODO: val session = DeviceLinkingSession.current!! LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests()
// TODO: session.stopListeningForLinkingRequests()
// TODO: session.markLinkingRequestAsProcessed() // Only relevant in master mode
delegate?.handleDeviceLinkingDialogDismissed() // Only relevant in slave mode
dismiss?.invoke() dismiss?.invoke()
} }
private fun cancel() {
if (mode == Mode.Master && pairingAuthorisation != null) {
val authorisation = pairingAuthorisation!!
// Remove pre key bundle from the requesting device
DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(authorisation.secondaryDevicePubKey)
}
delegate?.handleDeviceLinkingDialogDismissed() // Only relevant in slave mode
dismiss()
}
// endregion // endregion
} }