Skip to content

Commit

Permalink
feat: set userAgent in RNSession
Browse files Browse the repository at this point in the history
  • Loading branch information
pklatka committed Apr 24, 2024
1 parent e4f6517 commit d7298ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -36,13 +38,22 @@ 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
}
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -217,7 +205,6 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib
requestLayout()

turboView.attachWebView(webView!!) { attachedToNewDestination ->
updateWebViewConfiguration()
onReady(attachedToNewDestination)
}
}
Expand Down

0 comments on commit d7298ba

Please sign in to comment.