Skip to content

Commit 9300b74

Browse files
authored
Setup configuration layer for the library. (#4853)
1 parent 69de793 commit 9300b74

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionInitiator.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import android.app.Activity
2020
import android.app.Application.ActivityLifecycleCallbacks
2121
import android.os.Bundle
2222
import kotlin.time.Duration
23-
import kotlin.time.Duration.Companion.minutes
2423

2524
/**
2625
* The [SessionInitiator] is responsible for calling the [initiateSessionStart] callback whenever a
@@ -34,7 +33,6 @@ internal class SessionInitiator(
3433
private val initiateSessionStart: () -> Unit
3534
) {
3635
private var backgroundTime = elapsedRealtime()
37-
private val sessionTimeout = 30.minutes // TODO(mrober): Get session timeout from settings
3836

3937
init {
4038
initiateSessionStart()
@@ -46,6 +44,7 @@ internal class SessionInitiator(
4644

4745
fun appForegrounded() {
4846
val interval = elapsedRealtime() - backgroundTime
47+
val sessionTimeout = SessionsSettings().sessionRestartTimeout
4948
if (interval > sessionTimeout) {
5049
initiateSessionStart()
5150
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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
18+
19+
import kotlin.time.Duration
20+
import kotlin.time.Duration.Companion.minutes
21+
22+
/**
23+
* [SessionsSettings] manages all the configs that are relevant to the sessions library.
24+
*
25+
* @hide
26+
*/
27+
internal class SessionsSettings {
28+
// Setting to qualify if sessions service is enabled.
29+
val sessionsEnabled: Boolean
30+
get() {
31+
return true
32+
}
33+
34+
// Setting that provides the sessions sampling rate.
35+
val samplingRate: Double
36+
get() {
37+
return 1.0
38+
}
39+
40+
// Background timeout config value before which a new session is generated
41+
val sessionRestartTimeout: Duration
42+
get() = 30.minutes
43+
44+
// Update the settings for all the settings providers
45+
fun updateSettings() {
46+
// Placeholder to initiate settings update on different sources
47+
// Expected sources: RemoteSettings, ManifestOverrides, SDK Defaults
48+
}
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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
18+
19+
import com.google.common.truth.Truth.assertThat
20+
import kotlin.time.Duration.Companion.minutes
21+
import org.junit.Test
22+
23+
class SessionsSettingsTest {
24+
25+
@Test
26+
fun sessionSettings_fetchDefaults() {
27+
val sessionsSettings = SessionsSettings()
28+
assertThat(sessionsSettings.sessionsEnabled).isTrue()
29+
assertThat(sessionsSettings.samplingRate).isEqualTo(1.0)
30+
assertThat(sessionsSettings.sessionRestartTimeout).isEqualTo(30.minutes)
31+
}
32+
}

0 commit comments

Comments
 (0)