Fixed AccountStateManager -> AuthOrchestrator -> ActivityResultLauncher Memory Leak.

This commit is contained in:
Neil Marietta 2021-05-10 12:45:12 +02:00
parent de6753c7d5
commit 9c50634a72
7 changed files with 54 additions and 35 deletions

View File

@ -343,7 +343,7 @@ dependencies {
)
debugImplementation(
`leakCanary`
`leakcanary`
)
kapt(

View File

@ -165,7 +165,6 @@ public abstract class BaseActivity extends AppCompatActivity implements INetwork
}
}
mCurrentLocale = app.getCurrentLocale();
accountStateManager.register(this);
buildHtmlProcessor();
setContentView(getLayoutId());

View File

@ -168,23 +168,27 @@ abstract class NavigationActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
accountStateManager.state
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.onEach {
when (it) {
is AccountStateManager.State.Processing,
is AccountStateManager.State.PrimaryExist -> Unit
is AccountStateManager.State.AccountNeeded -> {
startSplashActivity()
finish()
with(accountStateManager) {
register(this@NavigationActivity)
state
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.onEach {
when (it) {
is AccountStateManager.State.Processing,
is AccountStateManager.State.PrimaryExist -> Unit
is AccountStateManager.State.AccountNeeded -> {
unregister()
startSplashActivity()
finish()
}
}
}
}.launchIn(lifecycleScope)
}.launchIn(lifecycleScope)
accountStateManager.onAccountSwitched()
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.onEach { switch -> onAccountSwitched(switch) }
.launchIn(lifecycleScope)
onAccountSwitched()
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.onEach { switch -> onAccountSwitched(switch) }
.launchIn(lifecycleScope)
}
sideDrawer.setOnHeaderUserClick {
onUserClicked(true)

View File

@ -21,6 +21,8 @@ package ch.protonmail.android.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import ch.protonmail.android.feature.account.AccountStateManager
import ch.protonmail.android.utils.startMailboxActivity
@ -38,22 +40,30 @@ class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
accountStateManager.register(this)
with(accountStateManager) {
register(this@SplashActivity)
// Start Login or MailboxActivity.
accountStateManager.state
.onEach {
when (it) {
is AccountStateManager.State.Processing -> Unit
is AccountStateManager.State.AccountNeeded -> accountStateManager.login()
is AccountStateManager.State.PrimaryExist -> {
startMailboxActivity()
finish()
// Start Login or MailboxActivity.
state
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.onEach {
when (it) {
is AccountStateManager.State.Processing -> Unit
is AccountStateManager.State.AccountNeeded ->
login()
is AccountStateManager.State.PrimaryExist -> {
unregister()
startMailboxActivity()
finish()
}
}
}
}.launchIn(lifecycleScope)
}.launchIn(lifecycleScope)
// Finish if Login closed.
accountStateManager.onLoginClosed { finish() }
// Finish if Login closed.
onLoginClosed {
unregister()
finish()
}
}
}
}

View File

@ -121,7 +121,7 @@ class AccountStateManager @Inject constructor(
// Raise AccountNeeded on empty/disabled account list.
accountManager.getAccounts()
.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
.flowWithLifecycle(lifecycle, minActiveState = Lifecycle.State.CREATED)
.onEach { accounts ->
if (accounts.isEmpty() || accounts.all { it.isDisabled() }) {
onAccountNeeded()
@ -131,9 +131,15 @@ class AccountStateManager @Inject constructor(
}
fun register(context: ComponentActivity) {
// Make sure there is only 1 ComponentActivity registered at a time.
authOrchestrator.unregister()
authOrchestrator.register(context)
}
fun unregister() {
authOrchestrator.unregister()
}
fun onLoginClosed(block: () -> Unit) {
authOrchestrator.onLoginResult { result -> if (result == null) block() }
}

View File

@ -103,6 +103,7 @@ val DependencyHandler.`firebase-messaging` get() = google("firebase", m
val DependencyHandler.`gotev-cookieStore` get() = dependency("net.gotev", module = "cookie-store") version `gotev-cookieStore version`
val DependencyHandler.`gson` get() = google("code.gson", "gson") version `gson version`
val DependencyHandler.`jsoup` get() = dependency("org.jsoup", module = "jsoup") version `jsoup version`
val DependencyHandler.`leakcanary` get() = dependency("com.squareup.leakcanary", module = "leakcanary-android") version `leakcanary version`
val DependencyHandler.`okhttp-url-connection` get() = squareup("okhttp3", "okhttp-urlconnection") version `okHttp-url-connection version`
val DependencyHandler.`okio` get() = dependency("com.squareup.okio", module = "okio") version `okio version`
val DependencyHandler.`sentry-android` get() = dependency("io.sentry", module = "sentry-android") version `sentry version`
@ -118,7 +119,6 @@ val DependencyHandler.`fasterxml-jackson-anno` get() = dependency("com.fast
val DependencyHandler.`fasterxml-jackson-databind` get() = dependency("com.fasterxml.jackson.core", module = "jackson-databind") version `jackson version`
val DependencyHandler.`remark` get() = dependency("com.overzealous", module = "remark") version `remark version`
val DependencyHandler.`store` get() = dependency("com.dropbox.mobile.store", module = "store4") version `store version`
val DependencyHandler.`leakCanary` get() = dependency("com.squareup.leakcanary", module = "leakcanary-android") version `leakCanary version`
// endregion
// endregion

View File

@ -77,7 +77,7 @@ const val `Proton-data version` = "1.0.3" // Released: Mar
const val `Proton-network version` = "1.0.4" // Released: Apr 07, 2021
const val `Proton-crypto version` = "1.0.2" // Released: Mar 05, 2021
const val `Proton-auth version` = "1.0.7" // Released: May 05, 2021
const val `Proton-auth version` = "1.0.8" // Released: May 10, 2021
const val `Proton-account version` = "1.0.3" // Released: Apr 12, 2021
const val `Proton-account-manager version` = "1.0.9" // Released: May 03, 2021
const val `Proton-user version` = "1.0.6" // Released: May 03, 2021
@ -117,6 +117,7 @@ const val `gotev-cookieStore version` = "1.3.3" // Released: Mar
const val `gson version` = "2.8.5" // Released: May 22, 201
const val `jackson version` = "2.10.2" // Released: Jan 05, 2020
const val `jsoup version` = "1.13.1" // Released: Mar 01, 2020
const val `leakcanary version` = "2.7" // Released: Mar 26, 2021
const val `minidns version` = "0.3.3" // Released: Oct 14, 2018
const val `okHttp3 version` = "4.9.1" // Released: Jan 30, 2021
const val `okHttp-url-connection version` = "4.9.1" // Released: Jan 30, 2021
@ -132,4 +133,3 @@ const val `timber version` = "4.7.1" // Released:
const val `trustKit version` = "1.1.2" // Released: Jun 09, 2019
const val `remark version` = "1.1.0" // Released: Dec 08, 2016
const val `store version` = "4.0.0" // Released: Nov 30, 2020
const val `leakCanary version` = "2.7" // Released: Mar 26, 2021