For #7448: Move LP to IO dispatcher (#8558)

fennec/beta
MarcLeclair 4 years ago committed by GitHub
parent 82979afb48
commit 41f9311e20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,14 @@ import com.leanplum.Leanplum
import com.leanplum.LeanplumActivityHelper import com.leanplum.LeanplumActivityHelper
import com.leanplum.annotations.Parser import com.leanplum.annotations.Parser
import com.leanplum.internal.LeanplumInternal import com.leanplum.internal.LeanplumInternal
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.Job
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.Main
import mozilla.components.support.locale.LocaleManager import mozilla.components.support.locale.LocaleManager
import org.mozilla.fenix.components.metrics.MozillaProductDetector.MozillaProducts
import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
import java.util.Locale import java.util.Locale
@ -37,6 +44,9 @@ private val Event.name: String?
} }
class LeanplumMetricsService(private val application: Application) : MetricsService { class LeanplumMetricsService(private val application: Application) : MetricsService {
val scope = CoroutineScope(Dispatchers.IO)
var leanplumJob: Job? = null
data class Token(val id: String, val token: String) { data class Token(val id: String, val token: String) {
enum class Type { Development, Production, Invalid } enum class Type { Development, Production, Invalid }
@ -58,46 +68,56 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
private val token = Token(LeanplumId, LeanplumToken) private val token = Token(LeanplumId, LeanplumToken)
override fun start() { override fun start() {
if (!application.settings().isMarketingTelemetryEnabled) return
val applicationSetLocale = LocaleManager.getCurrentLocale(application) if (!application.settings().isMarketingTelemetryEnabled) return
val currentLocale = when (applicationSetLocale != null) {
true -> applicationSetLocale.isO3Language
false -> Locale.getDefault().isO3Language
}
if (!isLeanplumEnabled(currentLocale)) {
Log.i(LOGTAG, "Leanplum is not available for this locale: $currentLocale")
return
}
when (token.type) {
Token.Type.Production -> Leanplum.setAppIdForProductionMode(token.id, token.token)
Token.Type.Development -> Leanplum.setAppIdForDevelopmentMode(token.id, token.token)
Token.Type.Invalid -> {
Log.i(LOGTAG, "Invalid or missing Leanplum token")
return
}
}
Leanplum.setIsTestModeEnabled(false) Leanplum.setIsTestModeEnabled(false)
Leanplum.setApplicationContext(application) Leanplum.setApplicationContext(application)
Leanplum.setDeviceId(randomUUID().toString()) Leanplum.setDeviceId(randomUUID().toString())
Parser.parseVariables(application) Parser.parseVariables(application)
LeanplumActivityHelper.enableLifecycleCallbacks(application) leanplumJob = scope.launch {
val installedApps = MozillaProductDetector.getInstalledMozillaProducts(application) val applicationSetLocale = LocaleManager.getCurrentLocale(application)
val currentLocale = when (applicationSetLocale != null) {
true -> applicationSetLocale.isO3Language
false -> Locale.getDefault().isO3Language
}
if (!isLeanplumEnabled(currentLocale)) {
Log.i(LOGTAG, "Leanplum is not available for this locale: $currentLocale")
return@launch
}
Leanplum.start(application, hashMapOf( when (token.type) {
"default_browser" to MozillaProductDetector.getMozillaBrowserDefault(application).orEmpty(), Token.Type.Production -> Leanplum.setAppIdForProductionMode(token.id, token.token)
"fennec_installed" to installedApps.contains(MozillaProductDetector.MozillaProducts.FIREFOX.productName), Token.Type.Development -> Leanplum.setAppIdForDevelopmentMode(token.id, token.token)
"focus_installed" to installedApps.contains(MozillaProductDetector.MozillaProducts.FOCUS.productName), Token.Type.Invalid -> {
"klar_installed" to installedApps.contains(MozillaProductDetector.MozillaProducts.KLAR.productName), Log.i(LOGTAG, "Invalid or missing Leanplum token")
"fxa_signed_in" to application.settings().fxaSignedIn, return@launch
"fxa_has_synced_items" to application.settings().fxaHasSyncedItems, }
"search_widget_installed" to application.settings().searchWidgetInstalled, }
"fenix" to true
)) LeanplumActivityHelper.enableLifecycleCallbacks(application)
val installedApps = MozillaProductDetector.getInstalledMozillaProducts(application)
Leanplum.start(application, hashMapOf(
"default_browser" to MozillaProductDetector.getMozillaBrowserDefault(application).orEmpty(),
"fennec_installed" to installedApps.contains(MozillaProducts.FIREFOX.productName),
"focus_installed" to installedApps.contains(MozillaProducts.FOCUS.productName),
"klar_installed" to installedApps.contains(MozillaProducts.KLAR.productName),
"fxa_signed_in" to application.settings().fxaSignedIn,
"fxa_has_synced_items" to application.settings().fxaHasSyncedItems,
"search_widget_installed" to application.settings().searchWidgetInstalled,
"fenix" to true
))
withContext(Main) {
LeanplumInternal.setCalledStart(true)
LeanplumInternal.setHasStarted(true)
LeanplumInternal.setStartedInBackground(true)
}
}
} }
override fun stop() { override fun stop() {
@ -113,6 +133,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
// as LP stores it's state internally and check against it // as LP stores it's state internally and check against it
LeanplumInternal.setCalledStart(false) LeanplumInternal.setCalledStart(false)
LeanplumInternal.setHasStarted(false) LeanplumInternal.setHasStarted(false)
leanplumJob?.cancel()
} }
override fun track(event: Event) { override fun track(event: Event) {

Loading…
Cancel
Save