From 94fe2a8f700bea69c1bb289a3bf99cd9d67a7b42 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 26 Mar 2024 21:32:18 -0400 Subject: [PATCH] only refresh User when app is foregrounded Ensure cache for the user is refreshed once per cold start when app is in the foreground. This saves resources as there are a number of events (such as push received or non-OneSignal events) that start the app in the background but will never read/write any user properties. --- .../com/onesignal/internal/OneSignalImp.kt | 6 --- .../java/com/onesignal/user/UserModule.kt | 4 ++ .../internal/service/UserRefreshService.kt | 49 +++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/service/UserRefreshService.kt diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt index 53877bf80b..15fa601abb 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt @@ -323,12 +323,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { } } else { Logging.debug("initWithContext: using cached user ${identityModelStore!!.model.onesignalId}") - operationRepo!!.enqueue( - RefreshUserOperation( - configModel!!.appId, - identityModelStore!!.model.onesignalId, - ), - ) } startupService!!.start() diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/UserModule.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/UserModule.kt index 996b37178f..f371e6d391 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/UserModule.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/UserModule.kt @@ -4,6 +4,7 @@ import com.onesignal.common.modules.IModule import com.onesignal.common.services.ServiceBuilder import com.onesignal.core.internal.operations.IOperationExecutor import com.onesignal.core.internal.startup.IBootstrapService +import com.onesignal.core.internal.startup.IStartableService import com.onesignal.user.internal.UserManager import com.onesignal.user.internal.backend.IIdentityBackendService import com.onesignal.user.internal.backend.ISubscriptionBackendService @@ -24,6 +25,7 @@ import com.onesignal.user.internal.operations.impl.listeners.IdentityModelStoreL import com.onesignal.user.internal.operations.impl.listeners.PropertiesModelStoreListener import com.onesignal.user.internal.operations.impl.listeners.SubscriptionModelStoreListener import com.onesignal.user.internal.properties.PropertiesModelStore +import com.onesignal.user.internal.service.UserRefreshService import com.onesignal.user.internal.subscriptions.ISubscriptionManager import com.onesignal.user.internal.subscriptions.SubscriptionModelStore import com.onesignal.user.internal.subscriptions.impl.SubscriptionManager @@ -61,5 +63,7 @@ internal class UserModule : IModule { builder.register().provides() builder.register().provides() builder.register().provides() + + builder.register().provides() } } diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/service/UserRefreshService.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/service/UserRefreshService.kt new file mode 100644 index 0000000000..b2371d3214 --- /dev/null +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/service/UserRefreshService.kt @@ -0,0 +1,49 @@ +package com.onesignal.user.internal.service + +import com.onesignal.core.internal.application.IApplicationLifecycleHandler +import com.onesignal.core.internal.application.IApplicationService +import com.onesignal.core.internal.config.ConfigModelStore +import com.onesignal.core.internal.operations.IOperationRepo +import com.onesignal.core.internal.startup.IStartableService +import com.onesignal.user.internal.identity.IdentityModelStore +import com.onesignal.user.internal.operations.RefreshUserOperation + +// Ensure cache for the user is refreshed once per cold start when app +// is in the foreground. This saves resources as there are a number of +// events (such as push received or non-OneSignal events) that start +// the app in the background but will never read/write any user +// properties. +class UserRefreshService( + private val _applicationService: IApplicationService, + private val _operationRepo: IOperationRepo, + private val _configModelStore: ConfigModelStore, + private val _identityModelStore: IdentityModelStore, +) : IStartableService, + IApplicationLifecycleHandler { + private fun refreshUser() { + _operationRepo.enqueue( + RefreshUserOperation( + _configModelStore.model.appId, + _identityModelStore.model.onesignalId, + ), + ) + } + + override fun start() { + if (_applicationService.isInForeground) { + refreshUser() + } else { + _applicationService.addApplicationLifecycleHandler(this) + } + } + + private var onFocusCalled: Boolean = false + + override fun onFocus() { + if (onFocusCalled) return + onFocusCalled = true + refreshUser() + } + + override fun onUnfocused() { } +}