Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app not locked after timeout when screen turned off [WPB-5682] [WPB-5832] #2517

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/kotlin/com/wire/android/di/AppModule.kt
Original file line number Diff line number Diff line change
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() }
}
Original file line number Diff line number Diff line change
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)
saleniuk marked this conversation as resolved.
Show resolved Hide resolved
}

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
Original file line number Diff line number Diff line change
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
Loading