Skip to content

Commit

Permalink
move network listener to plugin and rename to offline
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Jan 9, 2024
1 parent ea4753e commit 9c5702f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
17 changes: 0 additions & 17 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.amplitude.android

import AndroidNetworkListener
import android.content.Context
import com.amplitude.android.migration.ApiKeyStorageMigration
import com.amplitude.android.migration.RemnantDataMigration
Expand All @@ -23,18 +22,6 @@ open class Amplitude(

internal var inForeground = false
private lateinit var androidContextPlugin: AndroidContextPlugin
private var networkListener: AndroidNetworkListener
private val networkChangeHandler =
object : AndroidNetworkListener.NetworkChangeCallback {
override fun onNetworkAvailable() {
configuration.isNetworkConnected = true
flush()
}

override fun onNetworkUnavailable() {
configuration.isNetworkConnected = false
}
}

val sessionId: Long
get() {
Expand All @@ -43,9 +30,6 @@ open class Amplitude(

init {
registerShutdownHook()
networkListener = AndroidNetworkListener((this.configuration as Configuration).context)
networkListener.setNetworkChangeCallback(networkChangeHandler)
networkListener.startListening()
}

override fun createTimeline(): Timeline {
Expand Down Expand Up @@ -134,7 +118,6 @@ open class Amplitude(
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
(this@Amplitude.timeline as Timeline).stop()
this@Amplitude.networkListener.stopListening()
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amplitude.android.plugins

import AndroidNetworkListener
import com.amplitude.android.Configuration
import com.amplitude.android.utilities.AndroidNetworkConnectivityChecker
import com.amplitude.core.Amplitude
Expand All @@ -11,15 +12,32 @@ class AndroidNetworkConnectivityCheckerPlugin : Plugin {
override val type: Plugin.Type = Plugin.Type.Before
override lateinit var amplitude: Amplitude
private lateinit var networkConnectivityChecker: AndroidNetworkConnectivityChecker
private lateinit var networkListener: AndroidNetworkListener
private val networkChangeHandler =
object : AndroidNetworkListener.NetworkChangeCallback {
override fun onNetworkAvailable() {
println("AndroidNetworkListener, onNetworkAvailable")
amplitude.configuration.offline = false
amplitude.flush()
}

override fun onNetworkUnavailable() {
println("AndroidNetworkListener, onNetworkUnavailable")
amplitude.configuration.offline = true
}
}

override fun setup(amplitude: Amplitude) {
super.setup(amplitude)
networkConnectivityChecker = AndroidNetworkConnectivityChecker((amplitude.configuration as Configuration).context, amplitude.logger)
networkListener = AndroidNetworkListener((amplitude.configuration as Configuration).context)
networkListener.setNetworkChangeCallback(networkChangeHandler)
networkListener.startListening()
}

override fun execute(event: BaseEvent): BaseEvent? {
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
amplitude.configuration.isNetworkConnected = networkConnectivityChecker.isConnected()
amplitude.configuration.offline = !networkConnectivityChecker.isConnected()
}
return super.execute(event)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class Configuration @JvmOverloads constructor(
open var identityStorageProvider: IdentityStorageProvider = IMIdentityStorageProvider(),
) {

var isNetworkConnected: Boolean = true
var offline: Boolean = false

companion object {
const val FLUSH_QUEUE_SIZE = 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class EventPipeline(
}

// Skip flush when offline
if (!amplitude.configuration.isNetworkConnected) {
if (amplitude.configuration.offline) {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EventPipelineTest {
fun `should not flush when put and offline`() =
runTest(testDispatcher) {
amplitude.isBuilt.await()
amplitude.configuration.isNetworkConnected = false
amplitude.configuration.offline = true
val eventPipeline = spyk(EventPipeline(amplitude))
val event = BaseEvent().apply { eventType = "test_event" }

Expand All @@ -58,7 +58,7 @@ class EventPipelineTest {
fun `should flush when put and online`() =
runTest(testDispatcher) {
amplitude.isBuilt.await()
amplitude.configuration.isNetworkConnected = true
amplitude.configuration.offline = false
val eventPipeline = spyk(EventPipeline(amplitude))
val event = BaseEvent().apply { eventType = "test_event" }

Expand Down

0 comments on commit 9c5702f

Please sign in to comment.