Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MT-1531] - TagManagement: Fix terminate webView on Tealium shutdown #239

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mobile/src/main/java/com/tealium/mobile/TealiumHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ object TealiumHelper : ActivityDataCollector {
)
}

fun shutdown() {
Tealium.destroy(BuildConfig.TEALIUM_INSTANCE)
}

fun retrieveDatalayer(): Map<String, Any>? {
return Tealium[BuildConfig.TEALIUM_INSTANCE]?.gatherTrackData()
}
Expand Down
2 changes: 1 addition & 1 deletion tagmanagementdispatcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'
apply from: '../jacoco.gradle'

version = '1.2.2'
version = '1.2.3'

android {
compileSdkVersion 33
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.tealium.remotecommands.RemoteCommandRequest
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import org.json.JSONObject
import java.lang.ref.WeakReference
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
Expand All @@ -28,7 +29,8 @@ class WebViewLoader(
private val connectivityRetriever: Connectivity = ConnectivityRetriever.getInstance(context.config.application),
private val webViewProvider: () -> WebView = { WebView(context.config.application) }
) : LibrarySettingsUpdatedListener,
SessionStartedListener {
SessionStartedListener,
InstanceShutdownListener {

val webViewStatus = AtomicReference<PageStatus>(PageStatus.INIT)
var lastUrlLoadTimestamp = 0L
Expand Down Expand Up @@ -410,6 +412,19 @@ class WebViewLoader(
registerNewSessionIfNeeded(sessionId)
}

override fun onInstanceShutdown(name: String, instance: WeakReference<Tealium>) {
mainScope.launch {
try {
webView.destroy()
} catch (t: Throwable) {
Logger.dev(
BuildConfig.TAG,
"Error destroying WebView on shutdown: ${t.localizedMessage}"
)
}
}
}

private fun registerNewSessionIfNeeded(sessionId: Long) {
if (sessionId == INVALID_SESSION_ID) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.net.Uri
import android.webkit.*
import com.tealium.core.Logger
import com.tealium.core.Tealium
import com.tealium.core.TealiumConfig
import com.tealium.core.TealiumContext
import com.tealium.core.messaging.AfterDispatchSendCallbacks
Expand All @@ -21,6 +22,7 @@ import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import java.io.File
import java.lang.ref.WeakReference
import java.util.*

class WebViewClientTest {
Expand Down Expand Up @@ -290,4 +292,37 @@ class WebViewClientTest {
mockWebView.destroy()
}
}

@Test
fun webViewClient_DestroyWebViewOnInstanceShutdown() = runBlocking {
webViewLoader = WebViewLoader(mockTealiumContext, "testUrl", mockDispatchSendCallbacks, mockConnectivity, mockWebViewProvider)
delay(1000)

webViewLoader.onInstanceShutdown("testName", WeakReference(Tealium::class.java.cast(null)))
delay(1000)

verify(exactly = 1, timeout = 1000) {
mockWebView.destroy()
}
}

@Test
fun webViewClient_DestroyWebViewHandlesExceptionOnInstanceShutdown() = runBlocking {
every { mockWebView.destroy() } throws RuntimeException("Test WebView destruction exception")

webViewLoader = WebViewLoader(mockTealiumContext, "testUrl", mockDispatchSendCallbacks, mockConnectivity, mockWebViewProvider)
delay(1000)

webViewLoader.onInstanceShutdown("testName", WeakReference(Tealium::class.java.cast(null)))
delay(1000)

verify(exactly = 1) {
mockWebView.destroy()
}
verify {
Logger.dev(any(), match {
it.startsWith("Error destroying WebView on shutdown:")
})
}
}
}
Loading