3

I am developing an android/tizen app using crosswalk project. Now i need to change the user agent to view page in desktop version. Is it possible to change user agent and how? Thank you!

Jonson
  • 171
  • 2
  • 6

5 Answers5

5

I used reflection to solve this problem until this API goes public again in Crosswalk 12. This works in Crosswalk 9.38.208.10.

private void setWebViewUserAgent(XWalkView webView, String userAgent)
{
    try
    {
        Method ___getBridge = XWalkView.class.getDeclaredMethod("getBridge");
        ___getBridge.setAccessible(true);
        XWalkViewBridge xWalkViewBridge = null;
        xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(webView);
        XWalkSettings xWalkSettings = xWalkViewBridge.getSettings();
        xWalkSettings.setUserAgentString(userAgent);
    }
    catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
    {
        // Could not set user agent
        e.printStackTrace();
    }
}
cprcrack
  • 17,118
  • 7
  • 88
  • 91
2

In documentation is nothing bout it. Propably only way is use setResourceClient and WebResourceResponse to change user agent. Example use:

setResourceClient(new XWalkResourceClient(this) {
    @Override
    public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
        try {
            URL u = new URL(url);
            HttpURLConnection c = (HttpURLConnection) u.openConnection(Proxy.NO_PROXY);
            c.setRequestProperty("User-agent", "test user agent");
            return new WebResourceResponse(null, "utf-8", c.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.shouldInterceptLoadRequest(view, url);
    }
});
Kenumir
  • 664
  • 8
  • 24
  • It works, but it slows down the performance and loading times. It is a very interesting answer, I've learned a lot. Thank You! – Jonson Oct 06 '14 at 16:02
  • The solution may be use this only for first url (skil for loading other resources, images etc) – Kenumir Oct 07 '14 at 07:12
1

Use this snippet:

XWalkView mXWalkView;
mXWalkView.getSettings().setUserAgentString("Your User Agent");
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
Farhad Ahmadi
  • 32
  • 2
  • 2
  • 1
    Please add some explanation of what your code is doing and how it works, so that newbies like the OP will be able to learn from the answer. – i alarmed alien Oct 04 '14 at 10:48
  • Didn't solve my actual problem, but the server recognizes the new user-agent. Thank You! :) – Jonson Oct 06 '14 at 16:00
1

The new API to set user agent is in draft and will be released in Crosswalk-12, about the end of January next year.

It will be like: xwalkView.setUserAgentString(newUserAgentString);

lincsoon
  • 401
  • 3
  • 3
0

This is for web-apps where you can alter JS-Files.:

I use this workaround, it works as well with Crosswalk 9.

xview.addJavascriptInterface(new Object(){
            @JavascriptInterface
            public String getNewUserAgent(){
                return "My User Agent";
            }
        }, "NativeInterface");

And in my web-app i just call:

navigator.ua = NativeInterface.getNewUserAgent();
Ostkontentitan
  • 6,930
  • 5
  • 53
  • 71