Skip to content

Commit

Permalink
fix: app not locked after timeout when screen turned off [WPB-5682] […
Browse files Browse the repository at this point in the history
…WPB-5832] (#2517)
  • Loading branch information
saleniuk committed Dec 13, 2023
1 parent 69a2f20 commit 11b2441
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/src/main/kotlin/com/wire/android/di/AppModule.kt
Expand Up @@ -27,6 +27,7 @@ import android.media.MediaPlayer
import androidx.core.app.NotificationManagerCompat
import com.wire.android.BuildConfig
import com.wire.android.mapper.MessageResourceProvider
import com.wire.android.ui.home.appLock.CurrentTimestampProvider
import com.wire.android.util.dispatchers.DefaultDispatcherProvider
import com.wire.android.util.dispatchers.DispatcherProvider
import dagger.Module
Expand Down Expand Up @@ -79,4 +80,8 @@ object AppModule {
)
}
}

@Singleton
@Provides
fun provideCurrentTimestampProvider(): CurrentTimestampProvider = { System.currentTimeMillis() }
}
Expand Up @@ -55,9 +55,11 @@ class LockCodeTimeManager @Inject constructor(
currentScreenManager: CurrentScreenManager,
observeAppLockConfigUseCase: ObserveAppLockConfigUseCase,
globalDataStore: GlobalDataStore,
currentTimestamp: CurrentTimestampProvider,
) {

private lateinit var isLockedFlow: MutableStateFlow<Boolean>
private var isLockedFlow: MutableStateFlow<Boolean>
private var lockTimeoutStarted: Long? = null

init {
runBlocking {
Expand All @@ -77,13 +79,26 @@ class LockCodeTimeManager @Inject constructor(
when {
appLockConfig is AppLockConfig.Disabled -> flowOf(false)

!isInForeground && !isLockedFlow.value -> flow {
!isInForeground && !isLockedFlow.value && appLockConfig is AppLockConfig.Enabled -> flow {
appLogger.i("$TAG lock is enabled and app in the background, lock count started")
if (lockTimeoutStarted == null) {
lockTimeoutStarted = currentTimestamp()
}
delay(appLockConfig.timeout.inWholeMilliseconds)
appLogger.i("$TAG lock count ended, app state should be locked if passcode is set")
appLogger.i("$TAG lock count ended while app in the background, app state should be locked")
emit(true)
}

if (appLockConfig is AppLockConfig.Enabled) {
isInForeground && !isLockedFlow.value && appLockConfig is AppLockConfig.Enabled -> flow {
if (lockTimeoutStarted != null
&& (lockTimeoutStarted!! + appLockConfig.timeout.inWholeMilliseconds) < currentTimestamp()
) {
appLogger.i("$TAG app put into foreground and lock count ended, app state should be locked")
emit(true)
} else {
appLogger.i("$TAG app put into foreground but lock count hasn't ended, app state should not be changed")
emit(false)
lockTimeoutStarted = null
}
}

Expand All @@ -101,6 +116,7 @@ class LockCodeTimeManager @Inject constructor(
fun appUnlocked() {
appLogger.i("$TAG app unlocked")
isLockedFlow.value = false
lockTimeoutStarted = null
}

fun isAppLocked(): Boolean = isLockedFlow.value
Expand All @@ -111,3 +127,5 @@ class LockCodeTimeManager @Inject constructor(
private const val TAG = "LockCodeTimeManager"
}
}

typealias CurrentTimestampProvider = () -> Long
Expand Up @@ -175,6 +175,7 @@ class LockCodeTimeManagerTest {
arrangement.withIsAppVisible(false)
advanceTimeBy(startDelay)
arrangement.withIsAppVisible(true)
advanceUntilIdle()
// then
assertEquals(expected, manager.observeAppLock().first())
}
Expand Down Expand Up @@ -297,7 +298,8 @@ class LockCodeTimeManagerTest {
CoroutineScope(dispatcher),
currentScreenManager,
observeAppLockConfigUseCase,
globalDataStore
globalDataStore,
dispatcher.scheduler::currentTime
)
}

Expand Down

0 comments on commit 11b2441

Please sign in to comment.