Skip to content

Commit

Permalink
Controls whether or not the sessions SDK hooks up any lifecycle callb…
Browse files Browse the repository at this point in the history
…acks based on the sdk enablement in SessionsSettings.
  • Loading branch information
bryanatkinson committed Oct 18, 2023
1 parent f00c59e commit 34e72fa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,53 @@ import com.google.firebase.FirebaseApp
import com.google.firebase.app
import com.google.firebase.sessions.api.FirebaseSessionsDependencies
import com.google.firebase.sessions.api.SessionSubscriber
import com.google.firebase.sessions.settings.SessionsSettings
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/** The [FirebaseSessions] API provides methods to register a [SessionSubscriber]. */
class FirebaseSessions internal constructor(private val firebaseApp: FirebaseApp) {
class FirebaseSessions
internal constructor(
private val firebaseApp: FirebaseApp,
private val settings: SessionsSettings,
backgroundDispatcher: CoroutineContext,
) {

init {
val appContext = firebaseApp.applicationContext.applicationContext
if (appContext is Application) {
SessionLifecycleClient.bindToService(appContext)
appContext.registerActivityLifecycleCallbacks(SessionsActivityLifecycleCallbacks)
Log.d(TAG, "Initializing Firebase Sessions SDK.")
CoroutineScope(backgroundDispatcher).launch {
val appContext = firebaseApp.applicationContext.applicationContext
settings.updateSettings()
if (!settings.sessionsEnabled) {
Log.d(TAG, "Sessions SDK disabled. Not listening to lifecycle events.")
} else if (appContext is Application) {
SessionLifecycleClient.bindToService(appContext)
appContext.registerActivityLifecycleCallbacks(SessionsActivityLifecycleCallbacks)

firebaseApp.addLifecycleEventListener { _, _ ->
Log.w(TAG, "FirebaseApp instance deleted. Sessions library will not collect session data.")
appContext.unregisterActivityLifecycleCallbacks(SessionsActivityLifecycleCallbacks)
firebaseApp.addLifecycleEventListener { _, _ ->
Log.w(
TAG,
"FirebaseApp instance deleted. Sessions library will not collect session data."
)
appContext.unregisterActivityLifecycleCallbacks(SessionsActivityLifecycleCallbacks)
}
} else {
Log.e(
TAG,
"Failed to register lifecycle callbacks, unexpected context ${appContext.javaClass}."
)
}
} else {
Log.e(
TAG,
"Failed to register lifecycle callbacks, unexpected context ${appContext.javaClass}."
)
}
}

/** Register the [subscriber]. This must be called for every dependency. */
fun register(subscriber: SessionSubscriber) {
if (!settings.sessionsEnabled) {
Log.d(TAG, "Sessions SDK disabled. Ignoring dependency registration.")
return
}

FirebaseSessionsDependencies.register(subscriber)

Log.d(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ internal class FirebaseSessionsRegistrar : ComponentRegistrar {
Component.builder(FirebaseSessions::class.java)
.name(LIBRARY_NAME)
.add(Dependency.required(firebaseApp))
.add(Dependency.required(sessionsSettings))
.add(Dependency.required(backgroundDispatcher))
.factory { container ->
FirebaseSessions(
container.get(firebaseApp),
container.get(sessionsSettings),
container.get(backgroundDispatcher),
)
}
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ internal class RemoteSettings(
fetchInProgress.withLock {
// Double check if cache is expired. If not, return.
if (!settingsCache.hasCacheExpired()) {
Log.d(TAG, "Remote settings cache not expired. Using cached values.")
return
}

Expand All @@ -89,9 +90,11 @@ internal class RemoteSettings(
"X-Crashlytics-API-Client-Version" to appInfo.sessionSdkVersion
)

Log.d(TAG, "Fetching settings from server.")
configsFetcher.doConfigFetch(
headerOptions = options,
onSuccess = {
Log.d(TAG, "Fetched settings: $it")
var sessionsEnabled: Boolean? = null
var sessionSamplingRate: Double? = null
var sessionTimeoutSeconds: Int? = null
Expand Down

0 comments on commit 34e72fa

Please sign in to comment.