|
| 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?, |
| 30 | + val sessionSamplingRate: Double?, |
| 31 | + val cacheDuration: Int?, |
| 32 | + val sessionRestartTimeout: Int?, |
| 33 | + val cacheUpdatedTime: Long? |
| 34 | +) |
| 35 | + |
| 36 | +internal class SettingsCache(private val store: DataStore<Preferences>) { |
| 37 | + private var sessionConfigs = |
| 38 | + SessionConfigs( |
| 39 | + sessionEnabled = null, |
| 40 | + sessionSamplingRate = null, |
| 41 | + sessionRestartTimeout = null, |
| 42 | + cacheDuration = null, |
| 43 | + cacheUpdatedTime = null |
| 44 | + ) |
| 45 | + |
| 46 | + private object SettingsCacheKeys { |
| 47 | + val SETTINGS_CACHE_SESSIONS_ENABLED = booleanPreferencesKey("firebase_sessions_enabled") |
| 48 | + val SETTINGS_CACHE_SAMPLING_RATE = doublePreferencesKey("firebase_sessions_sampling_rate") |
| 49 | + val SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS = |
| 50 | + intPreferencesKey("firebase_sessions_restart_timeout") |
| 51 | + val SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS = |
| 52 | + intPreferencesKey("firebase_sessions_cache_duration") |
| 53 | + val SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME = |
| 54 | + longPreferencesKey("firebase_sessions_cache_updated_time") |
| 55 | + } |
| 56 | + |
| 57 | + private suspend fun updateSessionConfigs() = mapSessionConfigs(store.data.first().toPreferences()) |
| 58 | + |
| 59 | + private fun mapSessionConfigs(settings: Preferences): SessionConfigs { |
| 60 | + val sessionEnabled = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED] |
| 61 | + val sessionSamplingRate = settings[SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE] |
| 62 | + val sessionRestartTimeout = |
| 63 | + settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS] |
| 64 | + val cacheDuration = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS] |
| 65 | + val cacheUpdatedTime = settings[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME] |
| 66 | + |
| 67 | + sessionConfigs = |
| 68 | + SessionConfigs( |
| 69 | + sessionEnabled = sessionEnabled, |
| 70 | + sessionSamplingRate = sessionSamplingRate, |
| 71 | + sessionRestartTimeout = sessionRestartTimeout, |
| 72 | + cacheDuration = cacheDuration, |
| 73 | + cacheUpdatedTime = cacheUpdatedTime |
| 74 | + ) |
| 75 | + return sessionConfigs |
| 76 | + } |
| 77 | + |
| 78 | + internal fun hasCacheExpired(): Boolean { |
| 79 | + if (sessionConfigs.cacheUpdatedTime != null) { |
| 80 | + val currentTimestamp = System.currentTimeMillis() |
| 81 | + val timeDifferenceSeconds = (currentTimestamp - sessionConfigs.cacheUpdatedTime!!) / 1000 |
| 82 | + if (timeDifferenceSeconds < sessionConfigs.cacheDuration!!) return false |
| 83 | + } |
| 84 | + return true |
| 85 | + } |
| 86 | + |
| 87 | + fun sessionsEnabled(): Boolean? { |
| 88 | + return sessionConfigs.sessionEnabled |
| 89 | + } |
| 90 | + |
| 91 | + fun sessionSamplingRate(): Double? { |
| 92 | + return sessionConfigs.sessionSamplingRate |
| 93 | + } |
| 94 | + |
| 95 | + fun sessionRestartTimeout(): Int? { |
| 96 | + return sessionConfigs.sessionRestartTimeout |
| 97 | + } |
| 98 | + |
| 99 | + suspend fun updateSettingsEnabled(enabled: Boolean?) { |
| 100 | + store.edit { preferences -> |
| 101 | + enabled?.run { preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED] = enabled } |
| 102 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_ENABLED) } |
| 103 | + } |
| 104 | + updateSessionConfigs() |
| 105 | + } |
| 106 | + |
| 107 | + suspend fun updateSamplingRate(rate: Double?) { |
| 108 | + store.edit { preferences -> |
| 109 | + rate?.run { preferences[SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE] = rate } |
| 110 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SAMPLING_RATE) } |
| 111 | + } |
| 112 | + updateSessionConfigs() |
| 113 | + } |
| 114 | + |
| 115 | + suspend fun updateSessionRestartTimeout(timeoutInSeconds: Int?) { |
| 116 | + store.edit { preferences -> |
| 117 | + timeoutInSeconds?.run { |
| 118 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS] = |
| 119 | + timeoutInSeconds |
| 120 | + } |
| 121 | + ?: run { |
| 122 | + preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_RESTART_TIMEOUT_SECONDS) |
| 123 | + } |
| 124 | + } |
| 125 | + updateSessionConfigs() |
| 126 | + } |
| 127 | + |
| 128 | + suspend fun updateSessionCacheDuration(cacheDurationInSeconds: Int?) { |
| 129 | + store.edit { preferences -> |
| 130 | + cacheDurationInSeconds?.run { |
| 131 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS] = |
| 132 | + cacheDurationInSeconds |
| 133 | + } |
| 134 | + ?: run { |
| 135 | + preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_DURATION_SECONDS) |
| 136 | + } |
| 137 | + } |
| 138 | + updateSessionConfigs() |
| 139 | + } |
| 140 | + |
| 141 | + suspend fun updateSessionCacheUpdatedTime(cacheUpdatedTime: Long?) { |
| 142 | + store.edit { preferences -> |
| 143 | + cacheUpdatedTime?.run { |
| 144 | + preferences[SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME] = cacheUpdatedTime |
| 145 | + } |
| 146 | + ?: run { preferences.remove(SettingsCacheKeys.SETTINGS_CACHE_SESSIONS_CACHE_UPDATED_TIME) } |
| 147 | + } |
| 148 | + updateSessionConfigs() |
| 149 | + } |
| 150 | + |
| 151 | + suspend fun removeConfigs() { |
| 152 | + val configs = store |
| 153 | + store.edit { preferences -> |
| 154 | + preferences.clear() |
| 155 | + updateSessionConfigs() |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + companion object { |
| 160 | + private const val TAG = "SessionSettings" |
| 161 | + } |
| 162 | +} |
0 commit comments