Repository: tauri-apps/wry (language: rust)

Issue:
Support for modifying user-agent on Android
**Is your feature request related to a problem? Please describe.**

Currently, I can't modify the user agent on Android.

**Describe the solution you'd like**

If possible, set the user-agent in `InnerWebView::new()` for Android.


**Would you want to assign yourself to implement this feature?**
- [ ] Yes
- [x] No





Support for modifying user-agent on Android
**Is your feature request related to a problem? Please describe.**

Currently, I can't modify the user agent on Android.

**Describe the solution you'd like**

If possible, set the user-agent in `InnerWebView::new()` for Android.


**Would you want to assign yourself to implement this feature?**
- [ ] Yes
- [x] No







Discussion / hints:

Fix patch:
diff --git a/.changes/android-useragent.md b/.changes/android-useragent.md
new file mode 100644
index 000000000..be7428dc6
--- /dev/null
+++ b/.changes/android-useragent.md
@@ -0,0 +1,5 @@
+---
+"wry": "patch"
+---
+
+Support modifying user agent string on Android.
diff --git a/src/webview/android/kotlin/RustWebView.kt b/src/webview/android/kotlin/RustWebView.kt
index eb4ceb1e5..02960de10 100644
--- a/src/webview/android/kotlin/RustWebView.kt
+++ b/src/webview/android/kotlin/RustWebView.kt
@@ -52,5 +52,12 @@ class RustWebView(context: Context): WebView(context) {
         }
     }
 
+    fun setUserAgent(ua: String) {
+        post {
+          val settings = super.getSettings()
+          settings.setUserAgentString(ua)
+        }
+    }
+
     {{class-extension}}
 }
diff --git a/src/webview/android/kotlin/WryActivity.kt b/src/webview/android/kotlin/WryActivity.kt
index 4a2de4ec8..863f891bc 100644
--- a/src/webview/android/kotlin/WryActivity.kt
+++ b/src/webview/android/kotlin/WryActivity.kt
@@ -13,7 +13,11 @@ import android.view.KeyEvent
 import androidx.appcompat.app.AppCompatActivity
 
 abstract class WryActivity : AppCompatActivity() {
-    lateinit var m_webview: RustWebView
+    private lateinit var mWebView: RustWebView
+
+    private fun setWebView(webView: RustWebView) {
+        mWebView = webView
+    }
 
     val version: String
         @SuppressLint("WebViewApiAvailability", "ObsoleteSdkInt")
@@ -95,8 +99,8 @@ abstract class WryActivity : AppCompatActivity() {
     }
 
     override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
-        if (keyCode == KeyEvent.KEYCODE_BACK && m_webview?.canGoBack()) {
-            m_webview?.goBack()
+        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView?.canGoBack()) {
+            mWebView?.goBack()
             return true
         }
         return super.onKeyDown(keyCode, event)
diff --git a/src/webview/android/kotlin/proguard-wry.pro b/src/webview/android/kotlin/proguard-wry.pro
index 6573208c4..63369da78 100644
--- a/src/webview/android/kotlin/proguard-wry.pro
+++ b/src/webview/android/kotlin/proguard-wry.pro
@@ -7,13 +7,9 @@
 }
 
 -keep class {{package}}.WryActivity {
-  {{package}}.RustWebView m_webview;
-
   public <init>(...);
 
-  void setM_webview({{package}}.RustWebView);
-  {{package}}.RustWebView getM_webview();
-
+  void setWebView({{package}}.RustWebView);
   java.lang.Class getAppClass(...);
   java.lang.String getVersion();
 }
@@ -29,6 +25,7 @@
 
   void loadUrlMainThread(...);
   void setAutoPlay(...);
+  void setUserAgent(...);
 }
 
 -keep class {{package}}.RustWebChromeClient,{{package}}.RustWebViewClient {
diff --git a/src/webview/android/main_pipe.rs b/src/webview/android/main_pipe.rs
index af572953e..d81b4a4eb 100644
--- a/src/webview/android/main_pipe.rs
+++ b/src/webview/android/main_pipe.rs
@@ -54,6 +54,7 @@ impl MainPipe<'_> {
             headers,
             on_webview_created,
             autoplay,
+            user_agent,
             ..
           } = attrs;
           // Create webview
@@ -71,11 +72,22 @@ impl MainPipe<'_> {
           // set media autoplay
           env.call_method(webview, "setAutoPlay", "(Z)V", &[autoplay.into()])?;
 
-          env.set_field(
+          // set user-agent
+          if let Some(user_agent) = user_agent {
+            let user_agent = env.new_string(user_agent)?;
+            env.call_method(
+              webview,
+              "setUserAgent",
+              "(Ljava/lang/String;)V",
+              &[user_agent.into()],
+            )?;
+          }
+
+          env.call_method(
             activity,
-            "m_webview",
-            format!("L{}/RustWebView;", PACKAGE.get().unwrap()),
-            webview.into(),
+            "setWebView",
+            format!("(L{}/RustWebView;)V", PACKAGE.get().unwrap()),
+            &[webview.into()],
           )?;
 
           // Load URL
@@ -289,12 +301,6 @@ pub(crate) struct CreateWebViewAttributes {
   pub background_color: Option<RGBA>,
   pub headers: Option<http::HeaderMap>,
   pub autoplay: bool,
-  pub on_webview_created: Option<
-    Box<
-      dyn Fn(
-          super::Context,
-        ) -> std::result::Result<(), tao::platform::android::ndk_glue::jni::errors::Error>
-        + Send,
-    >,
-  >,
+  pub on_webview_created: Option<Box<dyn Fn(super::Context) -> Result<(), JniError> + Send>>,
+  pub user_agent: Option<String>,
 }
diff --git a/src/webview/android/mod.rs b/src/webview/android/mod.rs
index bc8281c76..8913f10f8 100644
--- a/src/webview/android/mod.rs
+++ b/src/webview/android/mod.rs
@@ -182,6 +182,7 @@ impl InnerWebView {
       transparent,
       headers,
       autoplay,
+      user_agent,
       ..
     } = attributes;
 
@@ -210,6 +211,7 @@ impl InnerWebView {
         headers,
         on_webview_created,
         autoplay,
+        user_agent,
       }));
     }
 


Test patch: