From 299bcc2bc774b6eeac9b7bd5f61c4e8180012f01 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 23 Sep 2020 21:22:17 +0200 Subject: [PATCH] Kill the task if PinActivity is cancelled --- .../app/core/platform/VectorBaseActivity.kt | 5 ++-- .../im/vector/app/features/pin/PinLocker.kt | 24 ++++++------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt index d39f42ff4f..91255cd492 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt @@ -318,11 +318,12 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasScreenInjector { if (requestCode == PinActivity.PIN_REQUEST_CODE) { when (resultCode) { Activity.RESULT_OK -> { + Timber.v("Pin ok, unlock app") pinLocker.unlock() } else -> { - pinLocker.block() - moveTaskToBack(true) + // Remove the task, to be sure that PIN code will be requested when resumed + finishAndRemoveTask() } } } diff --git a/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt b/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt index c35e481b18..3fc4152ee6 100644 --- a/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt +++ b/vector/src/main/java/im/vector/app/features/pin/PinLocker.kt @@ -47,16 +47,12 @@ class PinLocker @Inject constructor( // App is locked, can be unlock LOCKED, - // App is blocked and can't be unlocked as long as the app is in foreground - BLOCKED, - - // is unlocked, the app can be used + // App is unlocked, the app can be used UNLOCKED } private val liveState = MutableLiveData() - private var isBlocked = false private var shouldBeLocked = true private var entersBackgroundTs = 0L @@ -66,13 +62,13 @@ class PinLocker @Inject constructor( private fun computeState() { GlobalScope.launch { - val state = if (isBlocked) { - State.BLOCKED - } else if (shouldBeLocked && pinCodeStore.hasEncodedPin()) { + val state = if (shouldBeLocked && pinCodeStore.hasEncodedPin()) { State.LOCKED } else { State.UNLOCKED } + .also { Timber.v("New state: $it") } + if (liveState.value != state) { liveState.postValue(state) } @@ -85,23 +81,17 @@ class PinLocker @Inject constructor( computeState() } - fun block() { - Timber.v("Block app") - isBlocked = true - computeState() - } - @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun entersForeground() { val timeElapsedSinceBackground = SystemClock.elapsedRealtime() - entersBackgroundTs shouldBeLocked = shouldBeLocked || timeElapsedSinceBackground >= getGracePeriod() - Timber.v("App enters foreground after $timeElapsedSinceBackground ms spent in background") + Timber.v("App enters foreground after $timeElapsedSinceBackground ms spent in background shouldBeLocked: $shouldBeLocked") computeState() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun entersBackground() { - isBlocked = false + Timber.v("App enters background") entersBackgroundTs = SystemClock.elapsedRealtime() } @@ -109,7 +99,7 @@ class PinLocker @Inject constructor( return if (vectorPreferences.useGracePeriod()) { PERIOD_OF_GRACE_IN_MS } else { - 0L + 0L } } }