Check credentials in assistant before exiting
This commit is contained in:
parent
20c0b611d0
commit
2979ca0934
7 changed files with 132 additions and 17 deletions
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
package org.linphone.activities.assistant.fragments
|
||||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
@ -31,7 +32,9 @@ import org.linphone.R
|
|||
import org.linphone.activities.assistant.viewmodels.AccountLoginViewModel
|
||||
import org.linphone.activities.assistant.viewmodels.AccountLoginViewModelFactory
|
||||
import org.linphone.activities.assistant.viewmodels.SharedAssistantViewModel
|
||||
import org.linphone.activities.main.viewmodels.DialogViewModel
|
||||
import org.linphone.databinding.AssistantAccountLoginFragmentBinding
|
||||
import org.linphone.utils.DialogUtils
|
||||
|
||||
class AccountLoginFragment : AbstractPhoneFragment() {
|
||||
private lateinit var binding: AssistantAccountLoginFragmentBinding
|
||||
|
@ -94,6 +97,19 @@ class AccountLoginFragment : AbstractPhoneFragment() {
|
|||
}
|
||||
})
|
||||
|
||||
viewModel.invalidCredentialsEvent.observe(viewLifecycleOwner, Observer {
|
||||
it.consume {
|
||||
val dialogViewModel = DialogViewModel(getString(R.string.assistant_error_invalid_credentials))
|
||||
val dialog: Dialog = DialogUtils.getDialog(requireContext(), dialogViewModel)
|
||||
|
||||
dialogViewModel.showDeleteButton({
|
||||
dialog.dismiss()
|
||||
}, getString(R.string.dialog_ok))
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
})
|
||||
|
||||
checkPermission()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
package org.linphone.activities.assistant.fragments
|
||||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
@ -32,7 +33,9 @@ import org.linphone.R
|
|||
import org.linphone.activities.assistant.viewmodels.GenericLoginViewModel
|
||||
import org.linphone.activities.assistant.viewmodels.GenericLoginViewModelFactory
|
||||
import org.linphone.activities.assistant.viewmodels.SharedAssistantViewModel
|
||||
import org.linphone.activities.main.viewmodels.DialogViewModel
|
||||
import org.linphone.databinding.AssistantGenericAccountLoginFragmentBinding
|
||||
import org.linphone.utils.DialogUtils
|
||||
|
||||
class GenericAccountLoginFragment : Fragment() {
|
||||
private lateinit var binding: AssistantGenericAccountLoginFragmentBinding
|
||||
|
@ -71,5 +74,18 @@ class GenericAccountLoginFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
viewModel.invalidCredentialsEvent.observe(viewLifecycleOwner, Observer {
|
||||
it.consume {
|
||||
val dialogViewModel = DialogViewModel(getString(R.string.assistant_error_invalid_credentials))
|
||||
val dialog: Dialog = DialogUtils.getDialog(requireContext(), dialogViewModel)
|
||||
|
||||
dialogViewModel.showDeleteButton({
|
||||
dialog.dismiss()
|
||||
}, getString(R.string.dialog_ok))
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,8 @@
|
|||
package org.linphone.activities.assistant.viewmodels
|
||||
|
||||
import androidx.lifecycle.*
|
||||
import org.linphone.core.AccountCreator
|
||||
import org.linphone.core.AccountCreatorListenerStub
|
||||
import org.linphone.core.ProxyConfig
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.core.*
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.utils.Event
|
||||
|
||||
|
@ -50,6 +49,10 @@ class AccountLoginViewModel(accountCreator: AccountCreator) : AbstractPhoneViewM
|
|||
MutableLiveData<Event<Boolean>>()
|
||||
}
|
||||
|
||||
val invalidCredentialsEvent: MutableLiveData<Event<Boolean>> by lazy {
|
||||
MutableLiveData<Event<Boolean>>()
|
||||
}
|
||||
|
||||
val goToSmsValidationEvent: MutableLiveData<Event<Boolean>> by lazy {
|
||||
MutableLiveData<Event<Boolean>>()
|
||||
}
|
||||
|
@ -71,6 +74,33 @@ class AccountLoginViewModel(accountCreator: AccountCreator) : AbstractPhoneViewM
|
|||
}
|
||||
}
|
||||
|
||||
private var proxyConfigToCheck: ProxyConfig? = null
|
||||
|
||||
private val coreListener = object : CoreListenerStub() {
|
||||
override fun onRegistrationStateChanged(
|
||||
core: Core,
|
||||
cfg: ProxyConfig,
|
||||
state: RegistrationState,
|
||||
message: String?
|
||||
) {
|
||||
if (cfg == proxyConfigToCheck) {
|
||||
Log.i("[Assistant] [Account Login] Registration state is $state: $message")
|
||||
waitForServerAnswer.value = false
|
||||
if (state == RegistrationState.Ok) {
|
||||
leaveAssistantEvent.value = Event(true)
|
||||
core.removeListener(this)
|
||||
} else if (state == RegistrationState.Failed) {
|
||||
invalidCredentialsEvent.value = Event(true)
|
||||
val authInfo = cfg.findAuthInfo()
|
||||
if (authInfo != null) core.removeAuthInfo(authInfo)
|
||||
core.removeProxyConfig(cfg)
|
||||
proxyConfigToCheck = null
|
||||
core.removeListener(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
accountCreator.addListener(listener)
|
||||
|
||||
|
@ -109,10 +139,10 @@ class AccountLoginViewModel(accountCreator: AccountCreator) : AbstractPhoneViewM
|
|||
Log.i("[Assistant] [Account Login] Username is ${accountCreator.username}")
|
||||
|
||||
waitForServerAnswer.value = true
|
||||
if (createProxyConfig()) {
|
||||
leaveAssistantEvent.value = Event(true)
|
||||
} else {
|
||||
coreContext.core.addListener(coreListener)
|
||||
if (!createProxyConfig()) {
|
||||
waitForServerAnswer.value = false
|
||||
coreContext.core.removeListener(coreListener)
|
||||
// TODO: show error
|
||||
}
|
||||
} else {
|
||||
|
@ -140,6 +170,7 @@ class AccountLoginViewModel(accountCreator: AccountCreator) : AbstractPhoneViewM
|
|||
|
||||
private fun createProxyConfig(): Boolean {
|
||||
val proxyConfig: ProxyConfig? = accountCreator.createProxyConfig()
|
||||
proxyConfigToCheck = proxyConfig
|
||||
|
||||
if (proxyConfig == null) {
|
||||
Log.e("[Assistant] [Account Login] Account creator couldn't create proxy config")
|
||||
|
|
|
@ -23,9 +23,8 @@ import androidx.lifecycle.MediatorLiveData
|
|||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.linphone.core.AccountCreator
|
||||
import org.linphone.core.ProxyConfig
|
||||
import org.linphone.core.TransportType
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.core.*
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.utils.Event
|
||||
|
||||
|
@ -51,8 +50,41 @@ class GenericLoginViewModel(private val accountCreator: AccountCreator) : ViewMo
|
|||
|
||||
val loginEnabled: MediatorLiveData<Boolean> = MediatorLiveData()
|
||||
|
||||
val waitForServerAnswer = MutableLiveData<Boolean>()
|
||||
|
||||
val leaveAssistantEvent = MutableLiveData<Event<Boolean>>()
|
||||
|
||||
val invalidCredentialsEvent: MutableLiveData<Event<Boolean>> by lazy {
|
||||
MutableLiveData<Event<Boolean>>()
|
||||
}
|
||||
|
||||
private var proxyConfigToCheck: ProxyConfig? = null
|
||||
|
||||
private val coreListener = object : CoreListenerStub() {
|
||||
override fun onRegistrationStateChanged(
|
||||
core: Core,
|
||||
cfg: ProxyConfig,
|
||||
state: RegistrationState,
|
||||
message: String?
|
||||
) {
|
||||
if (cfg == proxyConfigToCheck) {
|
||||
Log.i("[Assistant] [Generic Login] Registration state is $state: $message")
|
||||
waitForServerAnswer.value = false
|
||||
if (state == RegistrationState.Ok) {
|
||||
leaveAssistantEvent.value = Event(true)
|
||||
core.removeListener(this)
|
||||
} else if (state == RegistrationState.Failed) {
|
||||
invalidCredentialsEvent.value = Event(true)
|
||||
val authInfo = cfg.findAuthInfo()
|
||||
if (authInfo != null) core.removeAuthInfo(authInfo)
|
||||
core.removeProxyConfig(cfg)
|
||||
proxyConfigToCheck = null
|
||||
core.removeListener(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
transport.value = TransportType.Tls
|
||||
|
||||
|
@ -73,6 +105,9 @@ class GenericLoginViewModel(private val accountCreator: AccountCreator) : ViewMo
|
|||
}
|
||||
|
||||
fun createProxyConfig() {
|
||||
waitForServerAnswer.value = true
|
||||
coreContext.core.addListener(coreListener)
|
||||
|
||||
accountCreator.username = username.value
|
||||
accountCreator.password = password.value
|
||||
accountCreator.domain = domain.value
|
||||
|
@ -80,15 +115,16 @@ class GenericLoginViewModel(private val accountCreator: AccountCreator) : ViewMo
|
|||
accountCreator.transport = transport.value
|
||||
|
||||
val proxyConfig: ProxyConfig? = accountCreator.createProxyConfig()
|
||||
proxyConfigToCheck = proxyConfig
|
||||
|
||||
if (proxyConfig == null) {
|
||||
Log.e("[Assistant] [Generic Login] Account creator couldn't create proxy config")
|
||||
coreContext.core.removeListener(coreListener)
|
||||
// TODO: show error
|
||||
return
|
||||
}
|
||||
|
||||
Log.i("[Assistant] [Generic Login] Proxy config created")
|
||||
leaveAssistantEvent.value = Event(true)
|
||||
}
|
||||
|
||||
private fun isLoginButtonEnabled(): Boolean {
|
||||
|
|
|
@ -75,12 +75,8 @@ class AccountSettingsViewModel(val proxyConfig: ProxyConfig) : GenericSettingsVi
|
|||
) {
|
||||
if (state == RegistrationState.Cleared && cfg == proxyConfigToDelete) {
|
||||
Log.i("[Account Settings] Proxy config to remove registration is now cleared")
|
||||
val authInfo = cfg.findAuthInfo()
|
||||
core.removeProxyConfig(cfg)
|
||||
core.removeAuthInfo(authInfo)
|
||||
|
||||
waitForUnregister.value = false
|
||||
proxyConfigRemovedEvent.value = Event(true)
|
||||
deleteProxyConfig(cfg)
|
||||
} else {
|
||||
update()
|
||||
}
|
||||
|
@ -140,10 +136,19 @@ class AccountSettingsViewModel(val proxyConfig: ProxyConfig) : GenericSettingsVi
|
|||
}
|
||||
// isDefault mutable is above
|
||||
|
||||
private fun deleteProxyConfig(cfg: ProxyConfig) {
|
||||
val authInfo = cfg.findAuthInfo()
|
||||
core.removeProxyConfig(cfg)
|
||||
core.removeAuthInfo(authInfo)
|
||||
proxyConfigRemovedEvent.value = Event(true)
|
||||
}
|
||||
|
||||
val deleteListener = object : SettingListenerStub() {
|
||||
override fun onClicked() {
|
||||
proxyConfigToDelete = proxyConfig
|
||||
waitForUnregister.value = true
|
||||
|
||||
val registered = proxyConfig.state == RegistrationState.Ok
|
||||
waitForUnregister.value = registered
|
||||
|
||||
if (core.defaultProxyConfig == proxyConfig) {
|
||||
Log.i("[Account Settings] Proxy config was default, let's look for a replacement")
|
||||
|
@ -159,6 +164,11 @@ class AccountSettingsViewModel(val proxyConfig: ProxyConfig) : GenericSettingsVi
|
|||
proxyConfig.edit()
|
||||
proxyConfig.enableRegister(false)
|
||||
proxyConfig.done()
|
||||
|
||||
if (!registered) {
|
||||
Log.w("[Account Settings] Proxy config isn't registered, don't unregister before removing it")
|
||||
deleteProxyConfig(proxyConfig)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:bind="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<data>
|
||||
<import type="android.view.View"/>
|
||||
|
@ -235,6 +236,10 @@
|
|||
|
||||
</ScrollView>
|
||||
|
||||
<include
|
||||
layout="@layout/wait_layout"
|
||||
bind:visibility="@{viewModel.waitForServerAnswer}"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</layout>
|
||||
|
|
|
@ -234,6 +234,7 @@
|
|||
<string name="assistant_error_invalid_email_address">Email address isn\t valid</string>
|
||||
<string name="assistant_error_username_too_long">Username has too many characters</string>
|
||||
<string name="assistant_error_account_not_activated">Your account has not been validated yet</string>
|
||||
<string name="assistant_error_invalid_credentials">Account does not exist or password does not match</string>
|
||||
|
||||
<!-- Assistant login -->
|
||||
<string name="assistant_linphone_account">Use your &appName; account</string>
|
||||
|
|
Loading…
Reference in a new issue