|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.sessions.settings |
| 18 | + |
| 19 | +import androidx.datastore.core.DataStore |
| 20 | +import androidx.datastore.preferences.core.Preferences |
| 21 | +import androidx.datastore.preferences.core.booleanPreferencesKey |
| 22 | +import androidx.datastore.preferences.core.doublePreferencesKey |
| 23 | +import androidx.datastore.preferences.core.edit |
| 24 | +import androidx.datastore.preferences.core.intPreferencesKey |
| 25 | +import androidx.datastore.preferences.core.longPreferencesKey |
| 26 | +import kotlinx.coroutines.flow.first |
| 27 | + |
| 28 | +internal data class SessionConfigs( |
| 29 | + val sessionEnabled: Boolean? = null, |
| 30 | + val sessionSamplingRate: Double? = null, |
| 31 | + val cacheDuration: Int? = null, |
| 32 | + val sessionRestartTimeout: Int? = null, |
| 33 | + val cacheUpdatedTime: Long? = null |
| 34 | +) |
| 35 | + |
| 36 | +internal class SettingsCache(private val store: DataStore<Preferences>) { |
| 37 | + private var sessionConfigs = SessionConfigs() |
| 38 | + |
| 39 | + private object SettingsCacheKeys { |
| 40 | + val SETTINGS_CACHE_SESSIONS_ENABLED = booleanPreferencesKey("firebase_sessions_enabled") |
| 41 | + val SETTINGS_CACHE_SAMPLING_RATE = doublePreferencesKey("firebase_sessions_sampling_rate") |
| 42 | + val SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS = |
| 43 | + intPreferencesKey("firebase_sessions_restart_timeout") |
| 44 | + val SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS = |
| 45 | + intPreferencesKey("firebase_sessions_cache_duration") |
| 46 | + val SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME = |
| 47 | + longPreferencesKey("firebase_sessions_cache_updated_time") |
| 48 | + } |
| 49 | + |
| 50 | + private suspend fun updateSessionConfigs() = mapSessionConfigs(store.data.first().toPreferences()) |
| 51 | + |
| 52 | + private fun mapSessionConfigs(settings: Preferences): SessionConfigs { |
| 53 | + val sessionEnabled = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED] |
| 54 | + val sessionSamplingRate = settings[SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE] |
| 55 | + val sessionRestartTimeout = |
| 56 | + settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS] |
| 57 | + val cacheDuration = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS] |
| 58 | + val cacheUpdatedTime = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME] |
| 59 | + |
| 60 | + sessionConfigs = |
| 61 | + SessionConfigs( |
| 62 | + sessionEnabled = sessionEnabled, |
| 63 | + sessionSamplingRate = sessionSamplingRate, |
| 64 | + sessionRestartTimeout = sessionRestartTimeout, |
| 65 | + cacheDuration = cacheDuration, |
| 66 | + cacheUpdatedTime = cacheUpdatedTime |
| 67 | + ) |
| 68 | + return sessionConfigs |
| 69 | + } |
| 70 | + |
| 71 | + internal fun hasCacheExpired(): Boolean { |
| 72 | + if (sessionConfigs.cacheUpdatedTime != null) { |
| 73 | + val currentTimestamp = System.currentTimeMillis() |
| 74 | + val timeDifferenceSeconds = (currentTimestamp - sessionConfigs.cacheUpdatedTime!!) / 1000 |
| 75 | + if (timeDifferenceSeconds < sessionConfigs.cacheDuration!!) return false |
| 76 | + } |
| 77 | + return true |
| 78 | + } |
| 79 | + |
| 80 | + fun sessionsEnabled(): Boolean? = sessionConfigs.sessionEnabled |
| 81 | + |
| 82 | + fun sessionSamplingRate(): Double? = sessionConfigs.sessionSamplingRate |
| 83 | + |
| 84 | + fun sessionRestartTimeout(): Int? = sessionConfigs.sessionRestartTimeout |
| 85 | + |
| 86 | + suspend fun updateSettingsEnabled(enabled: Boolean?) { |
| 87 | + store.edit { preferences -> |
| 88 | + enabled?.run { preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED] = enabled } |
| 89 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED) } |
| 90 | + } |
| 91 | + updateSessionConfigs() |
| 92 | + } |
| 93 | + |
| 94 | + suspend fun updateSamplingRate(rate: Double?) { |
| 95 | + store.edit { preferences -> |
| 96 | + rate?.run { preferences[SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE] = rate } |
| 97 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE) } |
| 98 | + } |
| 99 | + updateSessionConfigs() |
| 100 | + } |
| 101 | + |
| 102 | + suspend fun updateSessionRestartTimeout(timeoutInSeconds: Int?) { |
| 103 | + store.edit { preferences -> |
| 104 | + timeoutInSeconds?.run { |
| 105 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS] = |
| 106 | + timeoutInSeconds |
| 107 | + } |
| 108 | + ?: run { |
| 109 | + preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS) |
| 110 | + } |
| 111 | + } |
| 112 | + updateSessionConfigs() |
| 113 | + } |
| 114 | + |
| 115 | + suspend fun updateSessionCacheDuration(cacheDurationInSeconds: Int?) { |
| 116 | + store.edit { preferences -> |
| 117 | + cacheDurationInSeconds?.run { |
| 118 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS] = |
| 119 | + cacheDurationInSeconds |
| 120 | + } |
| 121 | + ?: run { |
| 122 | + preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS) |
| 123 | + } |
| 124 | + } |
| 125 | + updateSessionConfigs() |
| 126 | + } |
| 127 | + |
| 128 | + suspend fun updateSessionCacheUpdatedTime(cacheUpdatedTime: Long?) { |
| 129 | + store.edit { preferences -> |
| 130 | + cacheUpdatedTime?.run { |
| 131 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME] = cacheUpdatedTime |
| 132 | + } |
| 133 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME) } |
| 134 | + } |
| 135 | + updateSessionConfigs() |
| 136 | + } |
| 137 | + |
| 138 | + suspend fun removeConfigs() { |
| 139 | + store.edit { preferences -> |
| 140 | + preferences.clear() |
| 141 | + updateSessionConfigs() |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments