From c62c77f14c90b3816ab1522965bb15de8ddabeda Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 8 Oct 2019 12:00:40 +0200 Subject: [PATCH] Stop sending bus event from SDK to App. --- .../matrix/android/api/session/Session.kt | 6 +++ .../internal/session/DefaultSession.kt | 11 +++++ .../internal/session/SessionListeners.kt | 19 ++++++-- vector/build.gradle | 3 -- .../java/im/vector/riotx/VectorApplication.kt | 4 +- .../vector/riotx/core/di/VectorComponent.kt | 3 ++ .../vector/riotx/core/extensions/Session.kt | 5 ++- .../riotx/core/platform/VectorBaseActivity.kt | 30 ++++--------- .../features/home/HomeActivityViewModel.kt | 16 +------ .../riotx/features/login/LoginViewModel.kt | 8 ++-- .../riotx/features/session/SessionListener.kt | 45 +++++++++++++++++++ 11 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/session/SessionListener.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt index 31e96bb3b8..16de5178ff 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt @@ -19,6 +19,7 @@ package im.vector.matrix.android.api.session import androidx.annotation.MainThread import androidx.lifecycle.LiveData import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.session.cache.CacheService import im.vector.matrix.android.api.session.content.ContentUploadStateTracker @@ -140,6 +141,11 @@ interface Session : */ fun onInvalidToken() + /** + * A M_CONSENT_NOT_GIVEN error has been received from the homeserver + */ + fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) + } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 319cce491b..53e07e77af 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -23,6 +23,7 @@ import androidx.lifecycle.LiveData import dagger.Lazy import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.session.InitialSyncProgressService import im.vector.matrix.android.api.session.Session @@ -45,6 +46,9 @@ import im.vector.matrix.android.internal.crypto.DefaultCryptoService import im.vector.matrix.android.internal.database.LiveEntityObserver import im.vector.matrix.android.internal.session.sync.job.SyncThread import im.vector.matrix.android.internal.session.sync.job.SyncWorker +import org.greenrobot.eventbus.EventBus +import org.greenrobot.eventbus.Subscribe +import org.greenrobot.eventbus.ThreadMode import timber.log.Timber import javax.inject.Inject import javax.inject.Provider @@ -96,6 +100,7 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se assert(!isOpen) isOpen = true liveEntityObservers.forEach { it.start() } + EventBus.getDefault().register(this) } override fun requireBackgroundSync() { @@ -135,6 +140,7 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se liveEntityObservers.forEach { it.dispose() } cryptoService.get().close() isOpen = false + EventBus.getDefault().unregister(this) } override fun syncState(): LiveData { @@ -163,6 +169,11 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se }) } + @Subscribe(threadMode = ThreadMode.MAIN) + fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) { + sessionListeners.dispatchConsentNotGiven(consentNotGivenError) + } + override fun contentUrlResolver() = contentUrlResolver override fun contentUploadProgressTracker() = contentUploadProgressTracker diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt index 51b7354785..446dcdbd41 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt @@ -16,19 +16,32 @@ package im.vector.matrix.android.internal.session +import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.session.Session import javax.inject.Inject -internal class SessionListeners @Inject constructor(){ +internal class SessionListeners @Inject constructor() { private val listeners = ArrayList() fun addListener(listener: Session.Listener) { - listeners.add(listener) + synchronized(listeners) { + listeners.add(listener) + } } fun removeListener(listener: Session.Listener) { - listeners.remove(listener) + synchronized(listeners) { + listeners.remove(listener) + } + } + + fun dispatchConsentNotGiven(consentNotGivenError: ConsentNotGivenError) { + synchronized(listeners) { + listeners.forEach { + it.onConsentNotGivenError(consentNotGivenError) + } + } } } \ No newline at end of file diff --git a/vector/build.gradle b/vector/build.gradle index e6e5667b94..3992bba8a1 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -288,9 +288,6 @@ dependencies { implementation 'me.saket:better-link-movement-method:2.2.0' implementation 'com.google.android:flexbox:1.1.1' - // Bus - implementation 'org.greenrobot:eventbus:3.1.1' - // Passphrase strength helper implementation 'com.nulab-inc:zxcvbn:1.2.7' diff --git a/vector/src/main/java/im/vector/riotx/VectorApplication.kt b/vector/src/main/java/im/vector/riotx/VectorApplication.kt index 4884354ef3..cdbe242d99 100644 --- a/vector/src/main/java/im/vector/riotx/VectorApplication.kt +++ b/vector/src/main/java/im/vector/riotx/VectorApplication.kt @@ -49,6 +49,7 @@ import im.vector.riotx.features.notifications.NotificationDrawerManager import im.vector.riotx.features.notifications.NotificationUtils import im.vector.riotx.features.notifications.PushRuleTriggerListener import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler +import im.vector.riotx.features.session.SessionListener import im.vector.riotx.features.settings.VectorPreferences import im.vector.riotx.features.version.VersionProvider import im.vector.riotx.push.fcm.FcmHelper @@ -68,6 +69,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration. @Inject lateinit var emojiCompatWrapper: EmojiCompatWrapper @Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler @Inject lateinit var activeSessionHolder: ActiveSessionHolder + @Inject lateinit var sessionListener: SessionListener @Inject lateinit var notificationDrawerManager: NotificationDrawerManager @Inject lateinit var pushRuleTriggerListener: PushRuleTriggerListener @Inject lateinit var vectorPreferences: VectorPreferences @@ -117,7 +119,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration. if (authenticator.hasAuthenticatedSessions() && !activeSessionHolder.hasActiveSession()) { val lastAuthenticatedSession = authenticator.getLastAuthenticatedSession()!! activeSessionHolder.setActiveSession(lastAuthenticatedSession) - lastAuthenticatedSession.configureAndStart(pushRuleTriggerListener) + lastAuthenticatedSession.configureAndStart(pushRuleTriggerListener, sessionListener) } ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleObserver { diff --git a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt index 4aab312e2f..a3d8281b7d 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt @@ -40,6 +40,7 @@ import im.vector.riotx.features.notifications.* import im.vector.riotx.features.rageshake.BugReporter import im.vector.riotx.features.rageshake.VectorFileLogger import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler +import im.vector.riotx.features.session.SessionListener import im.vector.riotx.features.settings.VectorPreferences import im.vector.riotx.features.ui.UiStateRepository import javax.inject.Singleton @@ -54,6 +55,8 @@ interface VectorComponent { fun matrix(): Matrix + fun sessionListener(): SessionListener + fun currentSession(): Session fun notificationUtils(): NotificationUtils diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt index 713c764490..fac86697cd 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt @@ -21,10 +21,13 @@ import androidx.lifecycle.ProcessLifecycleOwner import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.sync.FilterService import im.vector.riotx.features.notifications.PushRuleTriggerListener +import im.vector.riotx.features.session.SessionListener import timber.log.Timber -fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener) { +fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener, + sessionListener: SessionListener) { open() + addListener(sessionListener) setFilter(FilterService.FilterPreset.RiotFilter) Timber.i("Configure and start session for ${this.myUserId}") val isAtLeastStarted = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) diff --git a/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt index cb65280907..ac574feeba 100644 --- a/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt @@ -36,11 +36,11 @@ import butterknife.Unbinder import com.airbnb.mvrx.BaseMvRxActivity import com.bumptech.glide.util.Util import com.google.android.material.snackbar.Snackbar -import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.riotx.BuildConfig import im.vector.riotx.R import im.vector.riotx.core.di.* import im.vector.riotx.core.dialogs.DialogLocker +import im.vector.riotx.core.extensions.observeEvent import im.vector.riotx.core.utils.toast import im.vector.riotx.features.configuration.VectorConfiguration import im.vector.riotx.features.consent.ConsentNotGivenHelper @@ -48,14 +48,12 @@ import im.vector.riotx.features.navigation.Navigator import im.vector.riotx.features.rageshake.BugReportActivity import im.vector.riotx.features.rageshake.BugReporter import im.vector.riotx.features.rageshake.RageShake +import im.vector.riotx.features.session.SessionListener import im.vector.riotx.features.themes.ActivityOtherThemes import im.vector.riotx.features.themes.ThemeUtils import im.vector.riotx.receivers.DebugReceiver import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable -import org.greenrobot.eventbus.EventBus -import org.greenrobot.eventbus.Subscribe -import org.greenrobot.eventbus.ThreadMode import timber.log.Timber import kotlin.system.measureTimeMillis @@ -76,6 +74,7 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector { protected lateinit var viewModelFactory: ViewModelProvider.Factory private lateinit var configurationViewModel: ConfigurationViewModel + private lateinit var sessionListener: SessionListener protected lateinit var bugReporter: BugReporter private lateinit var rageShake: RageShake protected lateinit var navigator: Navigator @@ -132,6 +131,7 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector { viewModelFactory = screenComponent.viewModelFactory() configurationViewModel = ViewModelProviders.of(this, viewModelFactory).get(ConfigurationViewModel::class.java) bugReporter = screenComponent.bugReporter() + // Shake detector rageShake = screenComponent.rageShake() navigator = screenComponent.navigator() activeSessionHolder = screenComponent.activeSessionHolder() @@ -143,7 +143,11 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector { } }) - // Shake detector + sessionListener = getVectorComponent().sessionListener() + sessionListener.consentNotGivenLiveData.observeEvent(this) { + consentNotGivenHelper.displayDialog(it.consentUri, + activeSessionHolder.getActiveSession().sessionParams.homeServerConnectionConfig.homeServerUri.host ?: "") + } doBeforeSetContentView() @@ -408,22 +412,6 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector { .apply { restorables.add(this) } } - override fun onStart() { - super.onStart() - EventBus.getDefault().register(this) - } - - override fun onStop() { - super.onStop() - EventBus.getDefault().unregister(this) - } - - @Subscribe(threadMode = ThreadMode.MAIN) - fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) { - consentNotGivenHelper.displayDialog(consentNotGivenError.consentUri, - activeSessionHolder.getActiveSession().sessionParams.homeServerConnectionConfig.homeServerUri.host ?: "") - } - /* ========================================================================================== * Temporary method * ========================================================================================== */ diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt index 6d52d87e7d..33f3bda7ac 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt @@ -40,7 +40,7 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState: private val session: Session, private val selectedGroupStore: SelectedGroupStore, private val homeRoomListStore: HomeRoomListObservableStore -) : VectorViewModel(initialState), Session.Listener { +) : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { @@ -58,7 +58,6 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState: init { - session.addListener(this) observeRoomAndGroup() } @@ -96,17 +95,4 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState: .disposeOnClear() } - override fun onCleared() { - super.onCleared() - session.removeListener(this) - } - - /* ========================================================================================== - * Session listener - * ========================================================================================== */ - - override fun onInvalidToken() { - - } - } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt index 2367e1be18..cce345deea 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt @@ -34,12 +34,14 @@ import im.vector.riotx.core.extensions.configureAndStart import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.utils.LiveEvent import im.vector.riotx.features.notifications.PushRuleTriggerListener +import im.vector.riotx.features.session.SessionListener import timber.log.Timber class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginViewState, private val authenticator: Authenticator, private val activeSessionHolder: ActiveSessionHolder, - private val pushRuleTriggerListener: PushRuleTriggerListener) + private val pushRuleTriggerListener: PushRuleTriggerListener, + private val sessionListener: SessionListener) : VectorViewModel(initialState) { @AssistedInject.Factory @@ -114,7 +116,7 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi private fun onSessionCreated(session: Session) { activeSessionHolder.setActiveSession(session) - session.configureAndStart(pushRuleTriggerListener) + session.configureAndStart(pushRuleTriggerListener, sessionListener) setState { copy( @@ -139,7 +141,7 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi private fun handleUpdateHomeserver(action: LoginActions.UpdateHomeServer) = withState { state -> - var newConfig : HomeServerConnectionConfig? = null + var newConfig: HomeServerConnectionConfig? = null Try { val homeServerUri = action.homeServerUrl newConfig = HomeServerConnectionConfig.Builder() diff --git a/vector/src/main/java/im/vector/riotx/features/session/SessionListener.kt b/vector/src/main/java/im/vector/riotx/features/session/SessionListener.kt new file mode 100644 index 0000000000..c9a4692444 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/session/SessionListener.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2019 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.riotx.features.session + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import im.vector.matrix.android.api.failure.ConsentNotGivenError +import im.vector.matrix.android.api.session.Session +import im.vector.riotx.core.extensions.postLiveEvent +import im.vector.riotx.core.utils.LiveEvent +import timber.log.Timber +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class SessionListener @Inject constructor() : Session.Listener { + + private val _consentNotGivenLiveData = MutableLiveData>() + val consentNotGivenLiveData: LiveData> + get() = _consentNotGivenLiveData + + override fun onInvalidToken() { + // TODO Handle this error + Timber.e("Token is not valid anymore: handle this properly") + } + + override fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) { + _consentNotGivenLiveData.postLiveEvent(consentNotGivenError) + } + +} \ No newline at end of file