mirror of
https://github.com/status-im/react-native-webview.git
synced 2026-08-31 11:41:08 +00:00
feat(android): polyfill applicationNameForUserAgent on Android (#707)
This commit is contained in:
committed by
Thibault Malbranche
parent
2333e45208
commit
9c592d621c
@@ -117,6 +117,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||
|
||||
protected RNCWebChromeClient mWebChromeClient = null;
|
||||
protected boolean mAllowsFullscreenVideo = false;
|
||||
protected @Nullable String mUserAgent = null;
|
||||
protected @Nullable String mUserAgentWithApplicationName = null;
|
||||
|
||||
public RNCWebViewManager() {
|
||||
mWebViewConfig = new WebViewConfig() {
|
||||
@@ -298,8 +300,34 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||
@ReactProp(name = "userAgent")
|
||||
public void setUserAgent(WebView view, @Nullable String userAgent) {
|
||||
if (userAgent != null) {
|
||||
// TODO(8496850): Fix incorrect behavior when property is unset (uA == null)
|
||||
view.getSettings().setUserAgentString(userAgent);
|
||||
mUserAgent = userAgent;
|
||||
} else {
|
||||
mUserAgent = null;
|
||||
}
|
||||
this.setUserAgentString(view);
|
||||
}
|
||||
|
||||
@ReactProp(name = "applicationNameForUserAgent")
|
||||
public void setApplicationNameForUserAgent(WebView view, @Nullable String applicationName) {
|
||||
if(applicationName != null) {
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
String defaultUserAgent = WebSettings.getDefaultUserAgent(view.getContext());
|
||||
mUserAgentWithApplicationName = defaultUserAgent + " " + applicationName;
|
||||
}
|
||||
} else {
|
||||
mUserAgentWithApplicationName = null;
|
||||
}
|
||||
this.setUserAgentString(view);
|
||||
}
|
||||
|
||||
protected void setUserAgentString(WebView view) {
|
||||
if(mUserAgent != null) {
|
||||
view.getSettings().setUserAgentString(mUserAgent);
|
||||
} else if(mUserAgentWithApplicationName != null) {
|
||||
view.getSettings().setUserAgentString(mUserAgentWithApplicationName);
|
||||
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
// handle unsets of `userAgent` prop as long as device is >= API 17
|
||||
view.getSettings().setUserAgentString(WebSettings.getDefaultUserAgent(view.getContext()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user