From d7298baa2f850089846d96e4cf6801db75d78c49 Mon Sep 17 00:00:00 2001 From: Patryk Klatka Date: Wed, 24 Apr 2024 12:14:12 +0200 Subject: [PATCH] feat: set userAgent in RNSession --- .../com/reactnativeturbowebview/RNSession.kt | 11 +++++++++++ .../reactnativeturbowebview/RNSessionManager.kt | 3 ++- .../reactnativeturbowebview/RNVisitableView.kt | 17 ++--------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt index a5184d2a..9bc3a817 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt @@ -1,6 +1,7 @@ package com.reactnativeturbowebview import android.webkit.JavascriptInterface +import android.webkit.WebSettings import android.webkit.WebView import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Lifecycle @@ -24,6 +25,7 @@ import org.json.JSONObject class RNSession( private val reactContext: ReactApplicationContext, private val sessionHandle: String, + private val applicationNameForUserAgent: String? ) : SessionCallbackAdapter { var visitableView: SessionSubscriber? = null @@ -36,6 +38,7 @@ class RNSession( WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG) webView.settings.setJavaScriptEnabled(true) webView.addJavascriptInterface(JavaScriptInterface(), "AndroidInterface") + setUserAgentString(webView, applicationNameForUserAgent) webView.webChromeClient = RNWebChromeClient(reactContext, this@RNSession) session.isRunningInAndroidNavigation = false session @@ -43,6 +46,14 @@ class RNSession( val webView: TurboWebView get() = turboSession.webView val currentVisit: TurboVisit? get() = turboSession.currentVisit + private fun setUserAgentString(webView: TurboWebView, applicationNameForUserAgent: String?) { + var userAgentString = WebSettings.getDefaultUserAgent(webView.context) + if (applicationNameForUserAgent != null) { + userAgentString = "$userAgentString $applicationNameForUserAgent" + } + webView.settings.userAgentString = userAgentString + } + internal fun registerVisitableView(newView: SessionSubscriber) { visitableView = newView } diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSessionManager.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSessionManager.kt index 1321158b..11be7ea5 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSessionManager.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSessionManager.kt @@ -18,8 +18,9 @@ class RNSessionManager(reactContext: ReactApplicationContext) : fun findOrCreateSession( reactContext: ReactApplicationContext, sessionHandle: String, + applicationNameForUserAgent: String? ): RNSession = sessions.getOrPut(sessionHandle) { - RNSession(reactContext, sessionHandle) + RNSession(reactContext, sessionHandle, applicationNameForUserAgent) } fun clearSnapshotCaches() { diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt index d53b5563..c98dd298 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt @@ -43,10 +43,6 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib lateinit var sessionHandle: String var applicationNameForUserAgent: String? = null - set(value) { - field = value - updateWebViewConfiguration() - } var scrollEnabled: Boolean = true set(value) { field = value @@ -70,7 +66,7 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib return null } - _session = RNSessionManager.findOrCreateSession(reactContext, sessionHandle) + _session = RNSessionManager.findOrCreateSession(reactContext, sessionHandle, applicationNameForUserAgent) return _session } @@ -108,16 +104,8 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib private fun updateWebViewConfiguration() { if (webView == null) return - setUserAgentString(webView!!, applicationNameForUserAgent) - setOnTouchListener(webView!!, scrollEnabled) - } - private fun setUserAgentString(webView: TurboWebView, applicationNameForUserAgent: String?) { - var userAgentString = WebSettings.getDefaultUserAgent(webView.context) - if (applicationNameForUserAgent != null) { - userAgentString = "$userAgentString $applicationNameForUserAgent" - } - webView.settings.userAgentString = userAgentString + setOnTouchListener(webView!!, scrollEnabled) } private fun setOnTouchListener(webView: TurboWebView, scrollEnabled: Boolean) { @@ -217,7 +205,6 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib requestLayout() turboView.attachWebView(webView!!) { attachedToNewDestination -> - updateWebViewConfiguration() onReady(attachedToNewDestination) } }