Partial implementation of QR login logic

This commit is contained in:
Hugh Nimmo-Smith 2022-10-11 23:34:05 +01:00
parent 1235db7895
commit 4b14ee4695
5 changed files with 73 additions and 53 deletions

View File

@ -3344,6 +3344,7 @@
<string name="qr_code_login_header_failed_device_is_not_supported_description">Linking with this device is not supported.</string>
<string name="qr_code_login_header_failed_timeout_description">The linking wasnt completed in the required time.</string>
<string name="qr_code_login_header_failed_denied_description">The request was denied on the other device.</string>
<string name="qr_code_login_header_failed_other_description">The request failed.</string>
<string name="qr_code_login_new_device_instruction_1">Open ${app_name} on your other device</string>
<string name="qr_code_login_new_device_instruction_2">Go to Settings -> Security &amp; Privacy -> Show All Sessions</string>
<string name="qr_code_login_new_device_instruction_3">Select \'Show QR code\'</string>

View File

@ -16,9 +16,11 @@
package im.vector.app.features.login.qr
import org.matrix.android.sdk.internal.rendezvous.RendezvousFailureReason
sealed class QrCodeLoginConnectionStatus {
object ConnectingToDevice : QrCodeLoginConnectionStatus()
data class Connected(val securityCode: String, val canConfirmSecurityCode: Boolean) : QrCodeLoginConnectionStatus()
object SigningIn : QrCodeLoginConnectionStatus()
data class Failed(val errorType: QrCodeLoginErrorType, val canTryAgain: Boolean) : QrCodeLoginConnectionStatus()
data class Failed(val errorType: RendezvousFailureReason, val canTryAgain: Boolean) : QrCodeLoginConnectionStatus()
}

View File

@ -1,23 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.login.qr
enum class QrCodeLoginErrorType {
DEVICE_IS_NOT_SUPPORTED,
TIMEOUT,
REQUEST_WAS_DENIED,
}

View File

@ -27,6 +27,7 @@ import im.vector.app.R
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentQrCodeLoginStatusBinding
import im.vector.app.features.themes.ThemeUtils
import org.matrix.android.sdk.internal.rendezvous.RendezvousFailureReason
@AndroidEntryPoint
class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBinding>() {
@ -77,11 +78,12 @@ class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBi
)
}
private fun getErrorCode(errorType: QrCodeLoginErrorType): String {
return when (errorType) {
QrCodeLoginErrorType.DEVICE_IS_NOT_SUPPORTED -> getString(R.string.qr_code_login_header_failed_device_is_not_supported_description)
QrCodeLoginErrorType.TIMEOUT -> getString(R.string.qr_code_login_header_failed_timeout_description)
QrCodeLoginErrorType.REQUEST_WAS_DENIED -> getString(R.string.qr_code_login_header_failed_denied_description)
private fun getErrorCode(reason: RendezvousFailureReason): String {
return when (reason) {
RendezvousFailureReason.UnsupportedAlgorithm, RendezvousFailureReason.UnsupportedTransport -> getString(R.string.qr_code_login_header_failed_device_is_not_supported_description)
RendezvousFailureReason.Expired -> getString(R.string.qr_code_login_header_failed_timeout_description)
RendezvousFailureReason.UserDeclined -> getString(R.string.qr_code_login_header_failed_denied_description)
else -> getString(R.string.qr_code_login_header_failed_other_description)
}
}

View File

@ -23,13 +23,19 @@ import dagger.assisted.AssistedInject
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.internal.rendezvous.Rendezvous
import org.matrix.android.sdk.internal.rendezvous.RendezvousFailureReason
import timber.log.Timber
class QrCodeLoginViewModel @AssistedInject constructor(
@Assisted private val initialState: QrCodeLoginViewState,
@Assisted private val initialState: QrCodeLoginViewState
) : VectorViewModel<QrCodeLoginViewState, QrCodeLoginAction, QrCodeLoginViewEvents>(initialState) {
val TAG: String = QrCodeLoginViewModel::class.java.simpleName
@AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<QrCodeLoginViewModel, QrCodeLoginViewState> {
override fun create(initialState: QrCodeLoginViewState): QrCodeLoginViewModel
@ -59,32 +65,64 @@ class QrCodeLoginViewModel @AssistedInject constructor(
}
private fun handleOnQrCodeScanned(action: QrCodeLoginAction.OnQrCodeScanned) {
if (isValidQrCode(action.qrCode)) {
Timber.tag(TAG).d("Scanned code: ${action.qrCode}")
val rendezvous = Rendezvous.buildChannelFromCode(action.qrCode) { reason ->
Timber.tag(TAG).d("Rendezvous cancelled: $reason")
onFailed(reason)
}
setState {
copy(
connectionStatus = QrCodeLoginConnectionStatus.ConnectingToDevice
)
}
_viewEvents.post(QrCodeLoginViewEvents.NavigateToStatusScreen)
viewModelScope.launch(Dispatchers.IO) {
val confirmationCode = rendezvous.startAfterScanningCode()
Timber.tag(TAG).i("Established secure channel with checksum: $confirmationCode")
confirmationCode ?.let {
onConnectionEstablished(it)
}
rendezvous.completeOnNewDevice()
}
// if (isValidQrCode(action.qrCode)) {
// setState {
// copy(
// connectionStatus = QrCodeLoginConnectionStatus.ConnectingToDevice
// )
// }
// _viewEvents.post(QrCodeLoginViewEvents.NavigateToStatusScreen)
// }
//
// // TODO. UI test purpose. Fixme remove!
// viewModelScope.launch {
// delay(3000)
// onFailed(QrCodeLoginErrorType.TIMEOUT, true)
// delay(3000)
// onConnectionEstablished("1234-ABCD-5678-EFGH")
// delay(3000)
// onSigningIn()
// delay(3000)
// onFailed(QrCodeLoginErrorType.DEVICE_IS_NOT_SUPPORTED, false)
// }
// // TODO. UI test purpose. Fixme remove!
// viewModelScope.launch {
// delay(3000)
// onConnectionEstablished("1234-ABCD-5678-EFGH")
// delay(3000)
// onSigningIn()
// }
}
// TODO. UI test purpose. Fixme remove!
viewModelScope.launch {
delay(3000)
onFailed(QrCodeLoginErrorType.TIMEOUT, true)
delay(3000)
onConnectionEstablished("1234-ABCD-5678-EFGH")
delay(3000)
onSigningIn()
delay(3000)
onFailed(QrCodeLoginErrorType.DEVICE_IS_NOT_SUPPORTED, false)
}
}
private fun onFailed(errorType: QrCodeLoginErrorType, canTryAgain: Boolean) {
private fun onFailed(reason: RendezvousFailureReason) {
setState {
copy(
connectionStatus = QrCodeLoginConnectionStatus.Failed(errorType, canTryAgain)
connectionStatus = QrCodeLoginConnectionStatus.Failed(reason, reason.canRetry)
)
}
}