This is solved...
I subclassed ByteArrayInputStream and called it "AsyncByteArrayInputStream"; I 
overloaded the read() function.This object is then passed to the 
WebResourceResponse object.

Regards,Alexander


From: [email protected]
To: [email protected]; [email protected]
Date: Thu, 23 Apr 2015 17:13:16 -0700
Subject: Re: [Crosswalk-help] How is it possible to async download images from 
shouldInterceptLoadRequest() ?




Unfortunately requestLayout() doesn't work to solve my problem....
Any other ideas ??

    // concrete implementation for my interface downloadCallback. Note: 
mWebView is actually XWalkView    @Override    synchronized public void 
finishedDownload() {        mFilesDownloading--;        Log.d("downloader", 
"files still downloading: "+mFilesDownloading);
        mWebView.post(new Runnable() {            @Override            public 
void run() {                mWebView.requestLayout();            }        });   
 }

From: [email protected]
To: [email protected]; [email protected]
Date: Thu, 23 Apr 2015 15:46:46 -0700
Subject: Re: [Crosswalk-help] How is it possible to async download images from 
shouldInterceptLoadRequest() ?




I'm not giving up on this...
my latest idea (another day invested) is to :1) from within 
shouldInterceptLoadRequest(), return a WebResourceResponse containing a 
place-holder image, and launch a thread that keeps a reference to the same 
WebResourceResponse2) the separate thread downloads the real image.3) after the 
image is downloaded, use WebResourceResponse.setData() to pass the downloaded 
image data.

Unfortunately, the XWalkView doesn't detect that the underlying data has 
changed and therefore only the place-holder image shows.
Is there some "refresh" method to make XWalkView re-layout with the data it 
already has ?
Thanks !!

Below is some key code to convey the idea ...

    @Override    public WebResourceResponse 
shouldInterceptLoadRequest(XWalkView view, String url) {
            // load a place-holder image, execute asynctask         InputStream 
is = mWebView.getContext().getResources().openRawResource(R.raw.placeholder);   
    try {               byte[] data = new byte[is.available()];         while 
(is.read(data) != -1){}           WebResourceResponse wrr = new 
WebResourceResponse("application/octet-stream", "UTF-8", new 
ByteArrayInputStream(data));
                // download the real data               ContentResourceTask crr 
= new ContentResourceTask();            crr.setWebResourceResponse(wrr);        
        crr.execute(url);
                responseToReturn = wrr;
            } catch(IOException e){             
Log.e(this.getClass().getSimpleName(), "error: "+e.getMessage());               
e.printStackTrace();        }
            return responseToReturn;    }



import android.os.AsyncTask;import android.webkit.WebResourceResponse;
import com.google.gson.reflect.TypeToken;import 
com.squareup.okhttp.OkHttpClient;import com.squareup.okhttp.Request;import 
com.squareup.okhttp.Response;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;import java.util.Map;import 
java.util.concurrent.TimeUnit;
/** * Created by alexbiemann on 4/23/15. */public class ContentResourceTask 
extends AsyncTask<String, Void, Void> {
    private static final Logger logger = 
LoggerFactory.getLogger(ContentResourceTask.class);    private 
WebResourceResponse mWebResourceResponse;
    /**     * setting this is required to store reference to placeholder image 
that was previously returned     * @param webResourceResponse     */    public 
void setWebResourceResponse(WebResourceResponse webResourceResponse){        
mWebResourceResponse = webResourceResponse;    }
    /**     * triggered by execute()     * @param urls     * @return     */    
@Override    protected Void doInBackground(String... urls) {
        String externalUrl = params[0];
        final OkHttpClient client = new OkHttpClient();        
client.setConnectTimeout(10, TimeUnit.SECONDS);        
client.setWriteTimeout(10, TimeUnit.SECONDS);        client.setReadTimeout(30, 
TimeUnit.SECONDS);
        try{            Request request = new Request.Builder()                 
   .url(externalUrl)                    .build();
            Response response = client.newCall(request).execute();            
if (!response.isSuccessful()) {                logger.error("Failed downloading 
external asset. Error: {}",response.message());            } else {             
   byte []assetData = response.body().bytes();                
mWebResourceResponse.setData(new ByteArrayInputStream(assetData));            }
        }        catch (Exception ex){            logger.error("Failed 
downloading external asset. Exception: {}", ex);        }
        return null; // a return is expected    }}






From: [email protected]
To: [email protected]; [email protected]
Date: Wed, 22 Apr 2015 16:35:23 -0700
Subject: Re: [Crosswalk-help] [crosswalk-help] How is it possible to async 
download images from shouldInterceptLoadRequest() ?




That's too bad that shouldInterceptLoadRequest() is synchronous because we're 
seeing that slow loading of the requested data holds up the UI of the 
XWalkView.So, on a webpage with 10 images that need to load from a remote 
server, it takes about 10 seconds to show the webpage.
The documentation for the shouldInterceptLoadRequest states the request is made 
on the network thread, see 
:https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkResourceClient.html#shouldInterceptLoadRequest(org.xwalk.core.XWalkView,
 java.lang.String)but we're seeing that it holds up the javascript execution of 
the webpage.
Is there possibly a XWalkView configuration that increases the number of 
threads for the data I/O requests ? It feels like 1 thread, but maybe I can 
increase the thread-pool to use about 20 ?

With regards to changing the URL, the use-case is as follows :1) the image is 
marked as "external" by the webpage developer. He does this by altering the 
scheme of the URL, i.e. external://server.com/myimage.jpg2) the app intercepts 
the data request with shouldInterceptLoadRequest(), captures the scheme for 
metrics, and changes the URL scheme to http://3) the data is downloaded with 
OkHttpClient, a WebResourceResponse is generated much like your example, and 
shouldInterceptLoadRequest() returns.
snippet of code that performs step #3 :Request request = new 
Request.Builder().url(externalUrl).build();Response response = 
client.newCall(request).execute();byte []assetData = 
response.body().bytes();WebResourceResponse responseToReturn = new 
WebResourceResponse("image/jpeg", "UTF-8", new ByteArrayInputStream(assetData));

As you can see, step #3 must be done without affecting the UI and must be done 
multi-threaded.

Is there any configuration / any solution at all to prevent the image download 
from affecting the UI/javascript animation?At the moment, animation/javascript 
is paused until all the images have been completely downloaded for the webpage.

Regards,Alexander


From: [email protected]
To: [email protected]; [email protected]
Subject: RE: [Crosswalk-help] [crosswalk-help] How is it possible to async 
download images from shouldInterceptLoadRequest() ?
Date: Tue, 21 Apr 2015 02:00:02 +0000









Is there a response that practically states "hang on whilst we're downloading 
this image, I'll let you get back to it later, in the meantime go on to the 
next resource" ?

ð 
No. This API is suggested to be used synchronously.  You need to return 
WebResourceResponse asap.

 
If the answer to my first question is "no", is it possible to alter the URL to 
the resource and then have XWalkView download it ?

ð 
I did several experiments,  cannot alter the url.  I do not know your user 
case, but this will be weird to users.  BTW, you can alter the url to a local
 page like this:
   private static final String RESTRICTED = "<html><body>not 
allowed</body></html>";
          @Override
          public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, 
String url) {
                       try {
                                       return new 
WebResourceResponse("text/html","UTF-8",
                                                       new 
ByteArrayInputStream(RESTRICTED.getBytes("UTF-8")));
                       }
                       catch (Exception ex) {
                                       Log.e(TAG, "Bad  file" + ex.toString());
                       }
                       return null;
       }
 


From: Crosswalk-help [mailto:[email protected]]
On Behalf Of Alexander Biemann

Sent: Tuesday, April 21, 2015 7:36 AM

To: [email protected]

Subject: [Crosswalk-help] [crosswalk-help] How is it possible to async download 
images from shouldInterceptLoadRequest() ?


 

My app intercepts the shouldInterceptLoadRequest()  and needs to download 
graphics asynchronously so that multiple images don't download one after 
another, but mostly in parallel.

 



public WebResourceResponse shouldInterceptLoadRequest (XWalkView view,
 String url)


 


My initial idea is to have my app start multiple download threads, however, 
after the URL is handed off to the download thread there needs to be some kind 
of returned data in the WebResourceResponse.


 


Is there a response that practically states "hang on whilst we're downloading 
this image, I'll let you get back to it later, in the meantime go on to the 
next resource" ?


 


I would return null, so that XWalkView can download it (hopefully 
asynchronously - I haven't tested it), but the URL has to be altered so that 
the resource can be downloaded.


If the answer to my first question is "no", is it possible to alter the URL to 
the resource and then have XWalkView download it ?


 


Thanks !


                                          

_______________________________________________
Crosswalk-help mailing list
[email protected]
https://lists.crosswalk-project.org/mailman/listinfo/crosswalk-help             
                          

_______________________________________________
Crosswalk-help mailing list
[email protected]
https://lists.crosswalk-project.org/mailman/listinfo/crosswalk-help             
                          

_______________________________________________
Crosswalk-help mailing list
[email protected]
https://lists.crosswalk-project.org/mailman/listinfo/crosswalk-help             
                          
_______________________________________________
Crosswalk-help mailing list
[email protected]
https://lists.crosswalk-project.org/mailman/listinfo/crosswalk-help

Reply via email to