Skip to content

Commit

Permalink
should not install plugin if null
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Jan 12, 2024
1 parent 6db25e4 commit 931ba2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 48 deletions.
4 changes: 3 additions & 1 deletion android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ open class Amplitude(
}
this.createIdentityContainer(identityConfiguration)

add(AndroidNetworkConnectivityCheckerPlugin())
if (this.configuration.offline != null) {
add(AndroidNetworkConnectivityCheckerPlugin())
}
androidContextPlugin = AndroidContextPlugin()
add(androidContextPlugin)
add(GetAmpliExtrasPlugin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,35 @@ import kotlinx.coroutines.launch
class AndroidNetworkConnectivityCheckerPlugin : Plugin {
override val type: Plugin.Type = Plugin.Type.Before
override lateinit var amplitude: Amplitude
internal var networkConnectivityChecker: AndroidNetworkConnectivityChecker? = null
internal var networkListener: AndroidNetworkListener? = null
internal lateinit var networkConnectivityChecker: AndroidNetworkConnectivityChecker
internal lateinit var networkListener: AndroidNetworkListener

override fun setup(amplitude: Amplitude) {
super.setup(amplitude)
if (amplitude.configuration.offline != null) {
amplitude.logger.debug("Installing AndroidNetworkConnectivityPlugin, offline feature should be supported.")
networkConnectivityChecker = AndroidNetworkConnectivityChecker((amplitude.configuration as Configuration).context, amplitude.logger)
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
amplitude.configuration.offline = !networkConnectivityChecker!!.isConnected()
}
val networkChangeHandler =
object : AndroidNetworkListener.NetworkChangeCallback {
override fun onNetworkAvailable() {
amplitude.logger.debug("AndroidNetworkListener, onNetworkAvailable.")
amplitude.configuration.offline = false
amplitude.flush()
}
amplitude.logger.debug("Installing AndroidNetworkConnectivityPlugin, offline feature should be supported.")
networkConnectivityChecker = AndroidNetworkConnectivityChecker((amplitude.configuration as Configuration).context, amplitude.logger)
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
amplitude.configuration.offline = !networkConnectivityChecker.isConnected()
}
val networkChangeHandler =
object : AndroidNetworkListener.NetworkChangeCallback {
override fun onNetworkAvailable() {
amplitude.logger.debug("AndroidNetworkListener, onNetworkAvailable.")
amplitude.configuration.offline = false
amplitude.flush()
}

override fun onNetworkUnavailable() {
amplitude.logger.debug("AndroidNetworkListener, onNetworkUnavailable.")
amplitude.configuration.offline = true
}
override fun onNetworkUnavailable() {
amplitude.logger.debug("AndroidNetworkListener, onNetworkUnavailable.")
amplitude.configuration.offline = true
}
networkListener = AndroidNetworkListener((amplitude.configuration as Configuration).context)
networkListener!!.setNetworkChangeCallback(networkChangeHandler)
networkListener!!.startListening()
}
}
networkListener = AndroidNetworkListener((amplitude.configuration as Configuration).context)
networkListener.setNetworkChangeCallback(networkChangeHandler)
networkListener.startListening()
}

override fun teardown() {
if (amplitude.configuration.offline != null) {
// offline can be changed for custom offline implementation
// so use ?. instead of !!
networkListener?.stopListening()
}
networkListener.stopListening()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull

class AndroidNetworkConnectivityCheckerPluginTest {

Expand Down Expand Up @@ -49,17 +48,6 @@ class AndroidNetworkConnectivityCheckerPluginTest {
assertNotNull(plugin.networkListener)
}

@Test
fun `should not set up if offline is disabled`() {
amplitude.configuration.offline = null
plugin.setup(amplitude)
assertEquals(amplitude, plugin.amplitude)
assertNull(plugin.networkConnectivityChecker)
// Unit tests are run on JVM so default to online
assertEquals(null, amplitude.configuration.offline)
assertNull(plugin.networkListener)
}

@Test
fun `should teardown correctly`() {
plugin.setup(amplitude)
Expand All @@ -70,11 +58,4 @@ class AndroidNetworkConnectivityCheckerPluginTest {
verify { networkListener.stopListening() }
}
}

@Test
fun `should not stop listening if offline is disabled`() {
amplitude.configuration.offline = null
plugin.setup(amplitude)
assertNull(plugin.networkListener)
}
}

0 comments on commit 931ba2c

Please sign in to comment.