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);
}
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;
}
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;
}
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();
}
}
}
};
}
Aggregations