Skip to content

Commit

Permalink
Add use of onsharedpreferencechanged
Browse files Browse the repository at this point in the history
  • Loading branch information
parallaxv committed Sep 25, 2023
1 parent bd1e59f commit 8d61000
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class MainActivity :
vpnModel.destroy()
sessionModel.destroy()
replicaModel.destroy()
Datadog.destroy()
EventBus.getDefault().unregister(this)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.getlantern.lantern.datadog

import android.content.SharedPreferences
import android.util.Log
import com.datadog.android.Datadog as DatadogMain
import com.datadog.android.DatadogSite
import com.datadog.android.core.configuration.BatchSize
import com.datadog.android.core.configuration.Configuration
Expand All @@ -14,17 +14,19 @@ import com.datadog.android.rum.RumErrorSource
import com.datadog.android.rum.RumMonitor
import com.datadog.android.rum.tracking.ActivityViewTrackingStrategy
import org.getlantern.lantern.LanternApp
import org.getlantern.lantern.BuildConfig
import org.getlantern.mobilesdk.Logger
import org.getlantern.mobilesdk.SessionManager
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.concurrent.atomic.AtomicBoolean
import com.datadog.android.Datadog as DatadogMain

object Datadog {
private val tracedHosts = listOf(
"datadoghq.eu",
"127.0.0.1",
)
object Datadog : SharedPreferences.OnSharedPreferenceChangeListener {
private val tracedHosts =
listOf(
"datadoghq.eu",
"127.0.0.1",
)
private val initialized = AtomicBoolean()
private lateinit var datadogConfig: Configuration

Expand All @@ -34,13 +36,14 @@ object Datadog {
DatadogMain.setVerbosity(Log.VERBOSE)
datadogConfig = createDatadogConfiguration()

val datadogCredentials = Credentials(
clientToken = "puba617ab01333a95a25a9d3709f04e1654",
envName = "prod",
rumApplicationId = "f8eabf3c-5db3-4f7e-8e6a-5a72433b46d2",
variant = "release",
serviceName = "lantern-android",
)
val datadogCredentials =
Credentials(
clientToken = "puba617ab01333a95a25a9d3709f04e1654",
envName = "prod",
rumApplicationId = "f8eabf3c-5db3-4f7e-8e6a-5a72433b46d2",
variant = "release",
serviceName = "lantern-android",
)

DatadogMain.initialize(
LanternApp.getAppContext(),
Expand All @@ -59,9 +62,29 @@ object Datadog {
val session = LanternApp.getSession()
val country = session.getCountryCode()
GlobalRum.addAttribute(GEO_COUNTRY_CODE, country)
// check for the country code to change via a shared preference change listener
val prefs = getSharedPreferences(SessionManager.PREFERENCES_SCHEMA, 0)
prefs.registerOnSharedPreferenceChangeListener(this)

initialized.set(true)
}

fun destroy() {
val prefs = getSharedPreferences(SessionManager.PREFERENCES_SCHEMA, 0)
prefs.unregisterOnSharedPreferenceChangeListener(this)
}

override fun onSharedPreferenceChanged(
_: SharedPreferences,
key: String,
) {
if (key == SessionManager.GEO_COUNTRY_CODE) {
val session = LanternApp.getSession()
val country = session.getCountryCode()
GlobalRum.addAttribute(GEO_COUNTRY_CODE, country)
}
}

fun addError(
message: String,
throwable: Throwable? = null,
Expand All @@ -78,7 +101,7 @@ object Datadog {
name: String,
actionAttributes: Map<String, Any?> = emptyMap(),
) {
GlobalRum.get().addUserAction(actionType, name, actionAttributes)
GlobalRum.get().addUserAction(actionType, name, actionAttributes)
}

// trackUserClick is used to track user clicks with RumMonitor
Expand Down

0 comments on commit 8d61000

Please sign in to comment.