Skip to content

Commit

Permalink
feat: sdk 1351 refresh sourceconfig on set to foreground (#421)
Browse files Browse the repository at this point in the history
feat: add core inputs, library and name
feat: added from_background and "version" to application opened event
feat: added listener for config download.
feat: check if x(default 90 minutes) have passed and
download source config

feat: add test cases.
fix: refactored the package name.
fix: removed unnecessary items from storage

chore: removed unused and commented out code
  • Loading branch information
itsdebs authored May 6, 2024
1 parent b42e718 commit 79095c3
Show file tree
Hide file tree
Showing 26 changed files with 516 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ internal class RudderPreferenceManager(application: Application,
fun saveTrackAutoSession(trackAutoSession: Boolean) {
preferences.edit().putBoolean(RUDDER_TRACK_AUTO_SESSION_KEY.key, trackAutoSession).apply()
}

internal val trackAutoSession: Boolean
get() = preferences.getBoolean(RUDDER_TRACK_AUTO_SESSION_KEY, false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,48 @@

package com.rudderstack.android.internal.infrastructure

import android.os.SystemClock
import com.rudderstack.android.LifecycleListenerPlugin
import com.rudderstack.android.androidStorage
import com.rudderstack.android.currentConfigurationAndroid
import com.rudderstack.core.Analytics
import com.rudderstack.core.ConfigDownloadService
import com.rudderstack.core.Configuration
import com.rudderstack.core.InfrastructurePlugin
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong

class LifecycleObserverPlugin : InfrastructurePlugin, LifecycleListenerPlugin {
const val EVENT_NAME_APPLICATION_OPENED = "Application Opened"
const val EVENT_NAME_APPLICATION_STOPPED = "Application Backgrounded"
private const val MAX_CONFIG_DOWNLOAD_INTERVAL = 90 * 60 * 1000 // 90 MINUTES

companion object {
const val EVENT_NAME_APPLICATION_OPENED = "Application Opened"
const val EVENT_NAME_APPLICATION_STOPPED = "Application Backgrounded"
}
internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long) = { SystemClock.uptimeMillis() }) :
InfrastructurePlugin, LifecycleListenerPlugin {

private var _isFirstLaunch = AtomicBoolean(true)

private var _lastSuccessfulDownloadTimeInMillis = AtomicLong(-1L)
private var analytics: Analytics? = null
private var currentActivityName: String? = null
private val listener = ConfigDownloadService.Listener {
if (it) {
_lastSuccessfulDownloadTimeInMillis.set(currentMillisGenerator())
}
}

private fun sendLifecycleStart() {
withTrackLifeCycle {
analytics?.track {
event(EVENT_NAME_APPLICATION_OPENED)
analytics?.also { analytics ->
analytics.track {
event(EVENT_NAME_APPLICATION_OPENED)
trackProperties {
_isFirstLaunch.getAndSet(false).also { isFirstLaunch ->
add("from_background" to !isFirstLaunch)
}.takeIf { it }?.let {
add("version" to (analytics.androidStorage.versionName ?: ""))
}
}
}
}
}
}
Expand All @@ -48,9 +70,20 @@ class LifecycleObserverPlugin : InfrastructurePlugin, LifecycleListenerPlugin {

override fun setup(analytics: Analytics) {
this.analytics = analytics
analytics.applyInfrastructureClosure {
if (this is ConfigDownloadService) {
addListener(listener, 1)
}
}
}

override fun shutdown() {
_lastSuccessfulDownloadTimeInMillis.set(0)
analytics?.applyInfrastructureClosure {
if (this is ConfigDownloadService) {
removeListener(listener)
}
}
analytics = null
}

Expand All @@ -60,6 +93,15 @@ class LifecycleObserverPlugin : InfrastructurePlugin, LifecycleListenerPlugin {

override fun onAppForegrounded() {
sendLifecycleStart()
checkAndDownloadSourceConfig()
}

private fun checkAndDownloadSourceConfig() {
analytics?.takeIf { it.currentConfiguration?.shouldVerifySdk == true }?.apply {
if (currentMillisGenerator() - (_lastSuccessfulDownloadTimeInMillis.get()) >= MAX_CONFIG_DOWNLOAD_INTERVAL) {
analytics?.updateSourceConfig()
}
}
}

override fun onAppBackgrounded() {
Expand All @@ -81,12 +123,12 @@ class LifecycleObserverPlugin : InfrastructurePlugin, LifecycleListenerPlugin {
}

override fun onScreenChange(name: String, arguments: Map<String, Any>?) {
val activityName = currentActivityName?:""
val activityName = currentActivityName ?: ""
withRecordScreenViews {
analytics?.screen {
screenName(activityName)
this.category(name)
this.screenProperties{
this.screenProperties {
add(arguments ?: mapOf())
}
}
Expand All @@ -98,14 +140,17 @@ class LifecycleObserverPlugin : InfrastructurePlugin, LifecycleListenerPlugin {
body()
}
}

private fun withRecordScreenViews(body: () -> Unit) {
if (analytics?.currentConfigurationAndroid?.recordScreenViews == true) {
body()
}
}

private fun withTrackLifeCycleAndRecordScreenViews(body: () -> Unit) {
if (analytics?.currentConfigurationAndroid?.trackLifecycleEvents == true
&& analytics?.currentConfigurationAndroid?.recordScreenViews == true) {
&& analytics?.currentConfigurationAndroid?.recordScreenViews == true
) {
body()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
* permissions and limitations under the License.
*/

package com.rudderstack.android.android
package com.rudderstack.android

import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.rudderstack.android.ConfigurationAndroid
import com.rudderstack.android.android.utils.TestExecutor
import com.rudderstack.android.android.utils.busyWait
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.android.utils.busyWait
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.core.Analytics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* permissions and limitations under the License.
*/

package com.rudderstack.android.android
package com.rudderstack.android

import org.junit.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@
* permissions and limitations under the License.
*/

package com.rudderstack.android.android
package com.rudderstack.android

import android.os.Build
import androidx.test.core.app.ApplicationProvider
import com.rudderstack.android.AnalyticsRegistry
import com.rudderstack.android.ConfigurationAndroid
import com.rudderstack.android.createInstance
import com.rudderstack.android.currentConfigurationAndroid
import com.rudderstack.android.setAnonymousId
import com.rudderstack.core.Analytics
import com.rudderstack.jacksonrudderadapter.JacksonAdapter
import org.hamcrest.MatcherAssert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@
* permissions and limitations under the License.
*/

package com.rudderstack.android.android
package com.rudderstack.android

import android.app.Application
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.testing.TestWorkerBuilder
import com.rudderstack.android.ConfigurationAndroid
import com.rudderstack.android.android.utils.TestLogger
import com.rudderstack.android.currentConfigurationAndroid
import com.rudderstack.android.internal.AndroidLogger
import com.rudderstack.android.utils.TestLogger
import com.rudderstack.android.internal.infrastructure.sync.RudderSyncWorker
import com.rudderstack.android.internal.infrastructure.sync.WorkManagerAnalyticsFactory
import com.rudderstack.android.internal.infrastructure.sync.registerWorkManager
import com.rudderstack.core.Analytics
import com.rudderstack.core.Base64Generator
import com.rudderstack.jacksonrudderadapter.JacksonAdapter
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -38,7 +33,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.anyOrNull

import org.robolectric.annotation.Config
import java.util.concurrent.ExecutorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Application
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import com.rudderstack.android.ConfigurationAndroid
import com.rudderstack.android.android.utils.TestExecutor
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.core.Analytics
Expand Down
Loading

0 comments on commit 79095c3

Please sign in to comment.