diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/account/AccountWizardPresenter.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/account/AccountWizardPresenter.kt index f1f21f4cc0c9fdc700a5086dcb1b3384394a9217..69a78fd9adbd0952e5d2417c1a01a1662144de7d 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/account/AccountWizardPresenter.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/account/AccountWizardPresenter.kt @@ -131,10 +131,7 @@ class AccountWizardPresenter @Inject constructor( if (accountCreationModel.isLink) { view!!.displayProgress(true) mCompositeDisposable.add(newAccount - .filter { a: Account -> - val newState = a.registrationState - !(newState.isEmpty() || newState.contentEquals(AccountConfig.STATE_INITIALIZING)) - } + .filter { a: Account -> a.registrationState != AccountConfig.RegistrationState.INITIALIZING } .firstOrError() .observeOn(mUiScheduler) .subscribe({ acc: Account -> @@ -143,7 +140,7 @@ class AccountWizardPresenter @Inject constructor( if (view != null) { view.displayProgress(false) val newState = acc.registrationState - if (newState.contentEquals(AccountConfig.STATE_ERROR_GENERIC)) { + if (newState == AccountConfig.RegistrationState.ERROR_GENERIC) { mCreatingAccount = false if (accountCreationModel.archive == null) view.displayCannotBeFoundError() else view.displayGenericError() } else { @@ -189,10 +186,7 @@ class AccountWizardPresenter @Inject constructor( //mCreationError = false; val account = BehaviorSubject.create<Account>() account.onErrorComplete() - .filter { a: Account -> - val newState = a.registrationState - !(newState.isEmpty() || newState.contentEquals(AccountConfig.STATE_INITIALIZING)) - } + .filter { a: Account -> a.registrationState != AccountConfig.RegistrationState.INITIALIZING } .firstElement() .subscribe { a: Account -> if (!model.isLink && a.isJami && model.username.isNotEmpty()) @@ -212,10 +206,8 @@ class AccountWizardPresenter @Inject constructor( fun profileCreated(model: AccountCreationModel, saveProfile: Boolean) { view!!.blockOrientation() view!!.displayProgress(true) - var newAccount = model.accountObservable!!.filter { a: Account -> - val newState = a.registrationState - !(newState.isEmpty() || newState.contentEquals(AccountConfig.STATE_INITIALIZING)) - } + var newAccount = model.accountObservable!!.filter { a: Account -> a.registrationState != AccountConfig.RegistrationState.INITIALIZING + } .firstOrError() if (saveProfile) { newAccount = newAccount.flatMap { a: Account -> @@ -229,12 +221,10 @@ class AccountWizardPresenter @Inject constructor( val view = view if (view != null) { view.displayProgress(false) - val newState = account.registrationState - Log.w(TAG, "newState $newState") - when (newState) { - AccountConfig.STATE_ERROR_GENERIC -> view.displayGenericError() - AccountConfig.STATE_UNREGISTERED -> { } - AccountConfig.STATE_ERROR_NETWORK -> view.displayNetworkError() + when (account.registrationState) { + AccountConfig.RegistrationState.ERROR_GENERIC -> view.displayGenericError() + AccountConfig.RegistrationState.UNREGISTERED -> { } + AccountConfig.RegistrationState.ERROR_NETWORK -> view.displayNetworkError() else -> {} } view.displaySuccessDialog() diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/account/SIPCreationPresenter.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/account/SIPCreationPresenter.kt index 1b3e5b5e5322683d88a7f87e6f7be88b8de16250..b60770c6c48f2c11e5dc8eef1450aa4e3145ccb9 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/account/SIPCreationPresenter.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/account/SIPCreationPresenter.kt @@ -107,16 +107,16 @@ class SIPCreationPresenter @Inject constructor( override fun onNext(account: Account) { mAccount = account when (account.registrationState) { - AccountConfig.STATE_REGISTERED, AccountConfig.STATE_SUCCESS, AccountConfig.STATE_READY -> { + AccountConfig.RegistrationState.REGISTERED -> { saveProfile(account.accountId) view?.showRegistrationSuccess() dispose() } - AccountConfig.STATE_ERROR_NETWORK -> { + AccountConfig.RegistrationState.ERROR_NETWORK -> { view?.showRegistrationNetworkError() dispose() } - AccountConfig.STATE_TRYING, AccountConfig.STATE_UNREGISTERED -> return + AccountConfig.RegistrationState.TRYING, AccountConfig.RegistrationState.UNREGISTERED -> return else -> { view?.showRegistrationError() dispose() diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt index daa8b7dbb25cd0ec8a96a703de21624a17eb80ad..bedb83075bc15af8c35e5ea99604c741beddcec0 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt @@ -22,14 +22,12 @@ package net.jami.model import io.reactivex.rxjava3.core.* import io.reactivex.rxjava3.core.Observable -import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.subjects.BehaviorSubject import io.reactivex.rxjava3.subjects.PublishSubject import io.reactivex.rxjava3.subjects.Subject import net.jami.model.Interaction.InteractionStatus import net.jami.services.AccountService import net.jami.utils.Log -import java.lang.IllegalStateException import java.util.* import kotlin.collections.ArrayList @@ -71,6 +69,7 @@ class Account( private val contactLocations: MutableMap<Contact, Observable<ContactLocation>> = HashMap() private val mLocationSubject: Subject<Map<Contact, Observable<ContactLocation>>> = BehaviorSubject.createDefault(contactLocations) private val mLocationStartedSubject: Subject<ContactLocationEntry> = PublishSubject.create() + private val registrationStateSubject = BehaviorSubject.createDefault(AccountConfig.RegistrationState.UNLOADED) var historyLoader: Single<Account>? = null var loadedProfile: Single<Profile>? = null @@ -116,7 +115,7 @@ class Account( val removed = cache.remove(key) conversations.remove(key) //Conversation contactConversation = getByUri(contact.getPrimaryUri()); - Log.w(TAG, "conversationStarted " + conversation.accountId + " contact " + key + " " + removed) + // Log.w(TAG, "conversationStarted " + conversation.accountId + " contact " + key + " " + removed) /*if (contactConversation != null) { conversations.remove(contactConversation.getUri().getUri()); }*/ @@ -453,12 +452,15 @@ class Account( config.put(ConfigKey.PROXY_SERVER, proxy) } - val registrationState: String - get() = mVolatileDetails[ConfigKey.ACCOUNT_REGISTRATION_STATUS] + val registrationState: AccountConfig.RegistrationState + get() = registrationStateSubject.blockingFirst() + val registrationStateObservable: Observable<AccountConfig.RegistrationState> + get() = registrationStateSubject - fun setRegistrationState(registeredState: String, code: Int) { - mVolatileDetails.put(ConfigKey.ACCOUNT_REGISTRATION_STATUS, registeredState) - mVolatileDetails.put(ConfigKey.ACCOUNT_REGISTRATION_STATE_CODE, code.toString()) + fun setRegistrationState(registeredState: AccountConfig.RegistrationState, code: Int) { + //mVolatileDetails.put(ConfigKey.ACCOUNT_REGISTRATION_STATUS, registeredState) + registrationStateSubject.onNext(registeredState) + //mVolatileDetails.put(ConfigKey.ACCOUNT_REGISTRATION_STATE_CODE, code.toString()) } fun setVolatileDetails(volatileDetails: Map<String, String>) { @@ -504,24 +506,17 @@ class Account( val details: HashMap<String, String> get() = config.all val isTrying: Boolean - get() = registrationState.contentEquals(AccountConfig.STATE_TRYING) + get() = registrationState == AccountConfig.RegistrationState.TRYING val isRegistered: Boolean - get() = registrationState.contentEquals(AccountConfig.STATE_READY) || registrationState.contentEquals( - AccountConfig.STATE_REGISTERED - ) + get() = registrationState == AccountConfig.RegistrationState.REGISTERED val isInError: Boolean get() { val state = registrationState - return (state.contentEquals(AccountConfig.STATE_ERROR) - || state.contentEquals(AccountConfig.STATE_ERROR_AUTH) - || state.contentEquals(AccountConfig.STATE_ERROR_CONF_STUN) - || state.contentEquals(AccountConfig.STATE_ERROR_EXIST_STUN) - || state.contentEquals(AccountConfig.STATE_ERROR_GENERIC) - || state.contentEquals(AccountConfig.STATE_ERROR_HOST) - || state.contentEquals(AccountConfig.STATE_ERROR_NETWORK) - || state.contentEquals(AccountConfig.STATE_ERROR_NOT_ACCEPTABLE) - || state.contentEquals(AccountConfig.STATE_ERROR_SERVICE_UNAVAILABLE) - || state.contentEquals(AccountConfig.STATE_REQUEST_TIMEOUT)) + return (state == AccountConfig.RegistrationState.ERROR_AUTH + || state == AccountConfig.RegistrationState.ERROR_GENERIC + || state == AccountConfig.RegistrationState.ERROR_HOST + || state == AccountConfig.RegistrationState.ERROR_NETWORK + || state == AccountConfig.RegistrationState.ERROR_SERVICE_UNAVAILABLE) } val isIP2IP: Boolean get() { @@ -562,9 +557,7 @@ class Account( return if (isIP2IP) defaultNameSip.toString() else displayUri!! } - fun needsMigration(): Boolean { - return AccountConfig.STATE_NEED_MIGRATION == registrationState - } + fun needsMigration(): Boolean = AccountConfig.RegistrationState.ERROR_NEED_MIGRATION == registrationState val deviceId: String get() = getDetail(ConfigKey.ACCOUNT_DEVICE_ID)!! diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/AccountConfig.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/AccountConfig.kt index 1bc37a8481d8f56b31c9b0eeb5120e0e6ca17cd9..59e6acc21e7938de6692712d547304a89ab86a0a 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/AccountConfig.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/AccountConfig.kt @@ -55,23 +55,22 @@ class AccountConfig(details: Map<String, String>) { const val FALSE_STR = "false" const val ACCOUNT_TYPE_JAMI = "RING" const val ACCOUNT_TYPE_SIP = "SIP" - const val STATE_REGISTERED = "REGISTERED" - const val STATE_READY = "READY" - const val STATE_UNREGISTERED = "UNREGISTERED" - const val STATE_TRYING = "TRYING" - const val STATE_ERROR = "ERROR" - const val STATE_ERROR_GENERIC = "ERROR_GENERIC" - const val STATE_ERROR_AUTH = "ERROR_AUTH" - const val STATE_ERROR_NETWORK = "ERROR_NETWORK" - const val STATE_ERROR_HOST = "ERROR_HOST" - const val STATE_ERROR_CONF_STUN = "ERROR_CONF_STUN" - const val STATE_ERROR_EXIST_STUN = "ERROR_EXIST_STUN" - const val STATE_ERROR_SERVICE_UNAVAILABLE = "ERROR_SERVICE_UNAVAILABLE" - const val STATE_ERROR_NOT_ACCEPTABLE = "ERROR_NOT_ACCEPTABLE" const val STATE_REQUEST_TIMEOUT = "Request Timeout" - const val STATE_INITIALIZING = "INITIALIZING" - const val STATE_NEED_MIGRATION = "ERROR_NEED_MIGRATION" const val STATE_SUCCESS = "SUCCESS" const val STATE_INVALID = "INVALID" } + + enum class RegistrationState { + UNLOADED, + UNREGISTERED, + TRYING, + REGISTERED, + ERROR_GENERIC, + ERROR_AUTH, + ERROR_NETWORK, + ERROR_HOST, + ERROR_SERVICE_UNAVAILABLE, + ERROR_NEED_MIGRATION, + INITIALIZING + } } \ No newline at end of file diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt index 5ce0165c9238d4b4a49a05cd7e77c8a9e8a8c3f1..89107d624b9b63469d5021f8518b0d80468529bf 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt @@ -997,25 +997,26 @@ class AccountService( } fun registrationStateChanged(accountId: String, newState: String, code: Int, detailString: String?) { - //Log.d(TAG, "registrationStateChanged: " + accountId + ", " + newState + ", " + code + ", " + detailString); + Log.d(TAG, "registrationStateChanged: $accountId, $newState, $code, $detailString") val account = getAccount(accountId) ?: return + val state = AccountConfig.RegistrationState.valueOf(newState) val oldState = account.registrationState - if (oldState.contentEquals(AccountConfig.STATE_INITIALIZING) && !newState.contentEquals(AccountConfig.STATE_INITIALIZING)) { + if (oldState == AccountConfig.RegistrationState.INITIALIZING && state != AccountConfig.RegistrationState.INITIALIZING) { account.setDetails(JamiService.getAccountDetails(account.accountId).toNative()) account.setCredentials(JamiService.getCredentials(account.accountId).toNative()) account.devices = JamiService.getKnownRingDevices(account.accountId).toNative() account.setVolatileDetails(JamiService.getVolatileAccountDetails(account.accountId).toNative()) } else { - account.setRegistrationState(newState, code) + account.setRegistrationState(state, code) } - if (oldState != newState) { + if (oldState != state) { observableAccounts.onNext(account) } } fun accountDetailsChanged(accountId: String, details: Map<String, String>) { val account = getAccount(accountId) ?: return - Log.d(TAG, "accountDetailsChanged: " + accountId + " " + details.size) + Log.d(TAG, "accountDetailsChanged: $accountId ${details.size}") account.setDetails(details) observableAccounts.onNext(account) }