Kill the task if PinActivity is cancelled

This commit is contained in:
Benoit Marty 2020-09-23 21:22:17 +02:00 committed by Benoit Marty
parent 03c66315cb
commit 299bcc2bc7
2 changed files with 10 additions and 19 deletions

View File

@ -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()
}
}
}

View File

@ -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<State>()
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
}
}
}