diff options
Diffstat (limited to 'app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt')
-rw-r--r-- | app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt b/app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt new file mode 100644 index 0000000000..6e0d70fe08 --- /dev/null +++ b/app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt @@ -0,0 +1,67 @@ +/* + * Nextcloud Android client application + * + * @author Álvaro Brey + * Copyright (C) 2022 Álvaro Brey + * Copyright (C) 2022 Nextcloud GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package com.nextcloud.client.network + +import com.nextcloud.client.core.Clock +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class WalledCheckCache @Inject constructor(private val clock: Clock) { + + private var value: Pair<Long, Boolean>? = null + + @Synchronized + fun isExpired(): Boolean { + return when (val timestamp = value?.first) { + null -> true + else -> { + val diff = clock.millisSinceBoot - timestamp + diff >= CACHE_TIME_MS + } + } + } + + @Synchronized + fun setValue(value: Boolean) { + this.value = Pair(clock.millisSinceBoot, value) + } + + @Synchronized + fun getValue(): Boolean? { + return when (isExpired()) { + true -> null + else -> value?.second + } + } + + @Synchronized + fun clear() { + value = null + } + + companion object { + // 10 minutes + private const val CACHE_TIME_MS = 10 * 60 * 1000 + } +} |