From 022cde2c0018c0d876e786e7ebac3a566c7cb9c4 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 9 Mar 2020 14:03:47 -0400 Subject: [PATCH] Fix secure screen option subscription memory leak --- .../ui/base/activity/BaseActivity.kt | 13 +++++++++++-- .../ui/base/activity/BaseRxActivity.kt | 13 +++++++++++-- .../ui/security/SecureActivityDelegate.kt | 19 ++++++++++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index a3c36a1cb3..88b2faca19 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -15,6 +15,9 @@ abstract class BaseActivity : AppCompatActivity() { val preferences: PreferencesHelper by injectLazy() + @Suppress("LeakingThis") + private val secureActivityDelegate = SecureActivityDelegate(this) + private val darkTheme: Int by lazy { when (preferences.themeDark()) { Values.THEME_DARK_DEFAULT -> R.style.Theme_Tachiyomi_Dark @@ -43,12 +46,18 @@ abstract class BaseActivity : AppCompatActivity() { super.onCreate(savedInstanceState) - SecureActivityDelegate.onCreate(this) + secureActivityDelegate.onCreate() } override fun onResume() { super.onResume() - SecureActivityDelegate.onResume(this) + secureActivityDelegate.onResume() + } + + override fun onDestroy() { + secureActivityDelegate.onDestroy() + + super.onDestroy() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt index 4da986bb9f..97071bd88c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt @@ -8,6 +8,9 @@ import nucleus.view.NucleusAppCompatActivity abstract class BaseRxActivity

> : NucleusAppCompatActivity

() { + @Suppress("LeakingThis") + private val secureActivityDelegate = SecureActivityDelegate(this) + init { @Suppress("LeakingThis") LocaleHelper.updateConfiguration(this) @@ -16,12 +19,18 @@ abstract class BaseRxActivity

> : NucleusAppCompatActivity

() - var locked: Boolean = true + private var secureScreenSubscription: Subscription? = null - fun onCreate(activity: FragmentActivity) { - preferences.secureScreen().asObservable() + fun onCreate() { + secureScreenSubscription = preferences.secureScreen().asObservable() .subscribe { if (it) { activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) @@ -26,7 +27,7 @@ object SecureActivityDelegate { } } - fun onResume(activity: FragmentActivity) { + fun onResume() { val lockApp = preferences.useBiometricLock().getOrDefault() if (lockApp && BiometricManager.from(activity).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { if (isAppLocked()) { @@ -39,9 +40,17 @@ object SecureActivityDelegate { } } + fun onDestroy() { + secureScreenSubscription?.unsubscribe() + } + private fun isAppLocked(): Boolean { return locked && (preferences.lockAppAfter().getOrDefault() <= 0 || Date().time >= preferences.lastAppUnlock().getOrDefault() + 60 * 1000 * preferences.lockAppAfter().getOrDefault()) } + + companion object { + var locked: Boolean = true + } }