Skip to content

Commit

Permalink
feat(android): add support modifying user agent string (#933)
Browse files Browse the repository at this point in the history
* feat(android): add support modifying useragent string, closes #928

* change file
  • Loading branch information
amrbashir authored Apr 20, 2023
1 parent ed36c0b commit 4a320b0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changes/android-useragent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

Support modifying user agent string on Android.
7 changes: 7 additions & 0 deletions src/webview/android/kotlin/RustWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ class RustWebView(context: Context): WebView(context) {
}
}

fun setUserAgent(ua: String) {
post {
val settings = super.getSettings()
settings.setUserAgentString(ua)
}
}

{{class-extension}}
}
10 changes: 7 additions & 3 deletions src/webview/android/kotlin/WryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions src/webview/android/kotlin/proguard-wry.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -29,6 +25,7 @@

void loadUrlMainThread(...);
void setAutoPlay(...);
void setUserAgent(...);
}

-keep class {{package}}.RustWebChromeClient,{{package}}.RustWebViewClient {
Expand Down
30 changes: 18 additions & 12 deletions src/webview/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl MainPipe<'_> {
headers,
on_webview_created,
autoplay,
user_agent,
..
} = attrs;
// Create webview
Expand All @@ -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
Expand Down Expand Up @@ -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>,
}
2 changes: 2 additions & 0 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl InnerWebView {
transparent,
headers,
autoplay,
user_agent,
..
} = attributes;

Expand Down Expand Up @@ -210,6 +211,7 @@ impl InnerWebView {
headers,
on_webview_created,
autoplay,
user_agent,
}));
}

Expand Down

0 comments on commit 4a320b0

Please sign in to comment.