Skip to content

Commit

Permalink
Merge branch 'develop' into fix/wrong-federation-support-link
Browse files Browse the repository at this point in the history
  • Loading branch information
yamilmedina authored Dec 13, 2023
2 parents 9c4f432 + a6490b1 commit a72d432
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
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)
}

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

0 comments on commit a72d432

Please sign in to comment.