0

I've recently migrated from android webview to Crosswalk 13. The only issue i've run into is telling the XWalkView to load content from the app cache.

In my android webview implementation i had implmemented as this

//check connection on a loop
public void CheckConnectivityTask(){


    new AsyncTask<Void, Void, Void>() {         
        @Override
        protected Void doInBackground(Void... params) {

            //runs every 0.5s
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            CheckConnectivity(true);
        }

    }.execute();
}


public void CheckConnectivity(boolean recursiveTask){
    cm = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
    if(cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()){
        Log.v("ConnectivityGG", "IS CONNECTED");
        mainWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }
    else{
        mainWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }
    if(recursiveTask){
        CheckConnectivityTask();
    }
}

As getSettings() has now been removed from XWalk 13, I've been trying to set this using XWalkSettings

inside OnCreate in MainActivity

xWalkSettings = new XWalkSettings(mainWebView.getContext(), null , false);
xWalkSettings.setAppCacheEnabled(true);

and then modifying my looped task

public void CheckConnectivity(boolean recursiveTask){
    cm = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
    if(cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()){
        xWalkSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    }
    else{
        xWalkSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }
    if(recursiveTask){
        CheckConnectivityTask();
    }
}

However any attempt to load cached pages fails with "Internet connection has been lost" alert dialogue. Am I instantiating the XWalkSettings instance incorrectly, or is there another way of achieving this?

user3679725
  • 11
  • 1
  • 3

2 Answers2

1

I found a way from this link. And changed it slightly. Basically need to use reflection to get access to a non public (afaik) method.

Method ___getBridge;
    try {
        ___getBridge = XWalkView.class.getDeclaredMethod("getBridge");
        ___getBridge.setAccessible(true);
         XWalkViewBridge xWalkViewBridge = null;
        xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(mainWebView);
        xWalkSettings = xWalkViewBridge.getSettings();
        xWalkSettings.setAppCacheEnabled(true);
    } catch (NoSuchMethodException e1) {
        // TODO Auto-generated catch block
        //e1.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }

If there's a nicer, cleaner way of doing this I'd love to know :)

user3679725
  • 11
  • 1
  • 3
0

Crosswalk didn't expose setCacheMode API before, but it has been exposed recently, please see this JIRA, https://crosswalk-project.org/jira/browse/XWALK-6832

It should be available in Crosswalk 21, you can use it like below:

mXWalkView.getSettings().setCacheMode(XWalkSettings.LOAD_NO_CACHE);

So, enjoy it.. :)

Hill
  • 379
  • 3
  • 9