Search in sources :

Example 1 with IOCallback

use of com.codename1.ui.html.IOCallback in project CodenameOne by codenameone.

the class Ads method setAd.

/**
 * HTML ad received from the server
 * @param ad the ad to set
 */
public void setAd(String ad) {
    HTMLComponent html = new HTMLComponent(new AsyncDocumentRequestHandlerImpl() {

        protected ConnectionRequest createConnectionRequest(DocumentInfo docInfo, IOCallback callback, Object[] response) {
            ConnectionRequest req = super.createConnectionRequest(docInfo, callback, response);
            req.setFailSilently(true);
            req.addResponseCodeListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                // do nothing, just make sure the html won't throw an error
                }
            });
            return req;
        }
    });
    html.setSupressExceptions(true);
    html.setHTMLCallback(this);
    html.setBodyText("<html><body><div align='center'>" + ad + "</div></body></html>");
    replace(getComponentAt(0), html, null);
    revalidate();
    html.setPageUIID("Container");
    html.getStyle().setBgTransparency(0);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) AsyncDocumentRequestHandlerImpl(com.codename1.ui.html.AsyncDocumentRequestHandlerImpl) IOCallback(com.codename1.ui.html.IOCallback) HTMLComponent(com.codename1.ui.html.HTMLComponent) DocumentInfo(com.codename1.ui.html.DocumentInfo)

Example 2 with IOCallback

use of com.codename1.ui.html.IOCallback in project CodenameOne by codenameone.

the class DefaultDocumentRequestHandler method resourceRequested.

private InputStream resourceRequested(final DocumentInfo docInfo, final IOCallback callback) {
    String url = docInfo.getUrl();
    visitingURL(url);
    // trim anchors
    int hash = url.indexOf('#');
    if (hash != -1) {
        url = url.substring(0, hash);
    }
    if (url.startsWith("jar://")) {
        callback.streamReady(Display.getInstance().getResourceAsStream(getClass(), docInfo.getUrl().substring(6)), docInfo);
        return null;
    } else {
        try {
            if (url.startsWith("local://")) {
                Image img = resFile.getImage(url.substring(8));
                if (img instanceof EncodedImage) {
                    callback.streamReady(new ByteArrayInputStream(((EncodedImage) img).getImageData()), docInfo);
                }
            }
            if (url.startsWith("res://")) {
                InputStream i = Display.getInstance().getResourceAsStream(getClass(), docInfo.getUrl().substring(6));
                Resources r = Resources.open(i);
                i.close();
                i = r.getData(docInfo.getParams());
                if (i != null) {
                    callback.streamReady(i, docInfo);
                } else {
                    Image img = r.getImage(docInfo.getParams());
                    if (img instanceof EncodedImage) {
                        callback.streamReady(new ByteArrayInputStream(((EncodedImage) img).getImageData()), docInfo);
                    }
                }
                return null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage)

Example 3 with IOCallback

use of com.codename1.ui.html.IOCallback in project CodenameOne by codenameone.

the class AsyncDocumentRequestHandlerImpl method resourceRequested.

private InputStream resourceRequested(final DocumentInfo docInfo, final IOCallback callback) {
    try {
        if (docInfo.getUrl().startsWith("file://")) {
            String url = docInfo.getUrl();
            // trim anchors
            int hash = url.indexOf('#');
            if (hash != -1) {
                url = url.substring(0, hash);
            }
            callback.streamReady(FileSystemStorage.getInstance().openInputStream(url), docInfo);
            return null;
        }
    } catch (IOException ex) {
        Log.e(ex);
    }
    final Object[] response = new Object[1];
    ConnectionRequest reqest = createConnectionRequest(docInfo, callback, response);
    reqest.setPost(docInfo.isPostRequest());
    if (docInfo.isPostRequest()) {
        reqest.setUrl(docInfo.getUrl());
        reqest.setWriteRequest(true);
    } else {
        reqest.setUrl(docInfo.getFullUrl());
    }
    NetworkManager.getInstance().addToQueue(reqest);
    if (callback == null) {
        synchronized (LOCK) {
            while (response[0] == null) {
                try {
                    LOCK.wait(50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            if (response[0] instanceof InputStream) {
                return (InputStream) response[0];
            }
            // we need a better way to handle this...
            if (response[0] instanceof Throwable) {
                ((Throwable) response[0]).printStackTrace();
            }
        }
    }
    return null;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 4 with IOCallback

use of com.codename1.ui.html.IOCallback in project CodenameOne by codenameone.

the class AsyncDocumentRequestHandlerImpl method createConnectionRequest.

protected ConnectionRequest createConnectionRequest(final DocumentInfo docInfo, final IOCallback callback, final Object[] response) {
    return new ConnectionRequest() {

        protected void buildRequestBody(OutputStream os) throws IOException {
            if (isPost()) {
                if (docInfo.getParams() != null) {
                    OutputStreamWriter w = new OutputStreamWriter(os, docInfo.getEncoding());
                    w.write(docInfo.getParams());
                }
            }
        }

        protected void handleIOException(IOException err) {
            if (callback == null) {
                response[0] = err;
            }
            super.handleIOException(err);
        }

        protected boolean shouldAutoCloseResponse() {
            return callback != null;
        }

        protected void readResponse(InputStream input) throws IOException {
            if (callback != null) {
                callback.streamReady(input, docInfo);
            } else {
                response[0] = input;
                synchronized (LOCK) {
                    LOCK.notify();
                }
            }
        }
    };
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 AsyncDocumentRequestHandlerImpl (com.codename1.ui.html.AsyncDocumentRequestHandlerImpl)1 DocumentInfo (com.codename1.ui.html.DocumentInfo)1 HTMLComponent (com.codename1.ui.html.HTMLComponent)1 IOCallback (com.codename1.ui.html.IOCallback)1 Resources (com.codename1.ui.util.Resources)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1