diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index 90bc076c3..e31e3f99d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -39,7 +39,6 @@ import eu.kanade.tachiyomi.glance.UpdatesGridGlanceWidget import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.ui.base.delegate.SecureActivityDelegate import eu.kanade.tachiyomi.util.preference.asHotFlow -import eu.kanade.tachiyomi.util.system.AuthenticatorUtil import eu.kanade.tachiyomi.util.system.WebViewUtil import eu.kanade.tachiyomi.util.system.animatorDurationScale import eu.kanade.tachiyomi.util.system.isDevFlavor @@ -60,7 +59,6 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.security.Security -import java.util.Date class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory { @@ -175,10 +173,7 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory { } override fun onStop(owner: LifecycleOwner) { - preferences.lastAppClosed().set(Date().time) - if (!AuthenticatorUtil.isAuthenticating && preferences.lockAppAfter().get() >= 0) { - SecureActivityDelegate.locked = true - } + SecureActivityDelegate.onApplicationStopped() } override fun getPackageName(): String { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 1e20a9189..81de52429 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -53,6 +53,10 @@ class PreferencesHelper(val context: Context) { fun lockAppAfter() = flowPrefs.getInt("lock_app_after", 0) + /** + * For app lock. Will be set when there is a pending timed lock. + * Otherwise this pref should be deleted. + */ fun lastAppClosed() = flowPrefs.getLong("last_app_closed", 0) fun secureScreen() = flowPrefs.getEnum("secure_screen_v2", Values.SecureScreenMode.INCOGNITO) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/SecureActivityDelegate.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/SecureActivityDelegate.kt index 151891344..803cd232f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/SecureActivityDelegate.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/SecureActivityDelegate.kt @@ -8,11 +8,14 @@ import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.security.UnlockActivity +import eu.kanade.tachiyomi.util.system.AuthenticatorUtil import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.isAuthenticationSupported import eu.kanade.tachiyomi.util.view.setSecureScreen import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.util.Date @@ -20,10 +23,36 @@ interface SecureActivityDelegate { fun registerSecureActivity(activity: AppCompatActivity) companion object { - var locked: Boolean = true + fun onApplicationStopped() { + val preferences = Injekt.get() + if (!preferences.useAuthenticator().get()) return + if (lockState != LockState.ACTIVE) { + preferences.lastAppClosed().set(Date().time) + } + if (!AuthenticatorUtil.isAuthenticating) { + lockState = if (preferences.lockAppAfter().get() >= 0) { + LockState.PENDING + } else { + LockState.ACTIVE + } + } + } + + fun unlock() { + lockState = LockState.INACTIVE + Injekt.get().lastAppClosed().delete() + } } } +private var lockState = LockState.INACTIVE + +private enum class LockState { + INACTIVE, + PENDING, + ACTIVE +} + class SecureActivityDelegateImpl : SecureActivityDelegate, DefaultLifecycleObserver { private lateinit var activity: AppCompatActivity @@ -57,6 +86,7 @@ class SecureActivityDelegateImpl : SecureActivityDelegate, DefaultLifecycleObser private fun setAppLock() { if (!preferences.useAuthenticator().get()) return if (activity.isAuthenticationSupported()) { + updatePendingLockStatus() if (!isAppLocked()) return activity.startActivity(Intent(activity, UnlockActivity::class.java)) activity.overridePendingTransition(0, 0) @@ -65,9 +95,23 @@ class SecureActivityDelegateImpl : SecureActivityDelegate, DefaultLifecycleObser } } + private fun updatePendingLockStatus() { + val lastClosedPref = preferences.lastAppClosed() + val lockDelay = 60000 * preferences.lockAppAfter().get() + if (lastClosedPref.isSet() && lockDelay > 0) { + // Restore pending status in case app was killed + lockState = LockState.PENDING + } + if (lockState != LockState.PENDING) { + return + } + if (Date().time >= lastClosedPref.get() + lockDelay) { + // Activate lock after delay + lockState = LockState.ACTIVE + } + } + private fun isAppLocked(): Boolean { - if (!SecureActivityDelegate.locked) return false - return preferences.lockAppAfter().get() <= 0 || - Date().time >= preferences.lastAppClosed().get() + 60 * 1000 * preferences.lockAppAfter().get() + return lockState == LockState.ACTIVE } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/UnlockActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/UnlockActivity.kt index e33760af1..0997c12e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/UnlockActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/UnlockActivity.kt @@ -37,7 +37,7 @@ class UnlockActivity : BaseActivity() { result: BiometricPrompt.AuthenticationResult, ) { super.onAuthenticationSucceeded(activity, result) - SecureActivityDelegate.locked = false + SecureActivityDelegate.unlock() finish() } },