use of com.codename1.ui.events.BrowserNavigationCallback in project CodenameOne by codenameone.
the class VServAds method getPendingAd.
/**
* {@inheritDoc}
*/
protected Component getPendingAd() {
if (imageURL == null) {
return null;
}
if (renderNotify != null && renderNotify.length() > 0) {
ConnectionRequest c = new ConnectionRequest();
c.setFailSilently(true);
c.setUrl(renderNotify);
c.setPost(false);
NetworkManager.getInstance().addToQueue(c);
}
if ("image".equalsIgnoreCase(contentType)) {
Button adComponent = new Button() {
public void setIcon(Image icon) {
if (icon != null && isScaleMode()) {
icon = icon.scaledWidth(Display.getInstance().getDisplayWidth());
}
super.setIcon(icon);
}
};
adComponent.setUIID("Container");
adComponent.getStyle().setBgColor(backgroundColor);
adComponent.getStyle().setOpacity(0xff);
ImageDownloadService imd = new ImageDownloadService(imageURL, adComponent);
NetworkManager.getInstance().addToQueueAndWait(imd);
/*adComponent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(getAdDestination());
}
});*/
return adComponent;
} else {
WebBrowser wb = new WebBrowser();
if (wb.getInternal() instanceof BrowserComponent) {
BrowserComponent bc = (BrowserComponent) wb.getInternal();
bc.setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(final String url) {
unlock(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(url);
}
});
return false;
}
});
}
wb.setURL(imageURL);
return wb;
}
}
use of com.codename1.ui.events.BrowserNavigationCallback in project CodenameOne by codenameone.
the class BrowserComponent method fireBrowserNavigationCallbacks.
/**
* Fires all of the registered browser navigation callbacks against the provided URL.
* @param url The URL to fire the navigation callbacks against.
* @return True if all of the callbacks say that they can browse. False otherwise.
*/
public boolean fireBrowserNavigationCallbacks(String url) {
boolean shouldNavigate = true;
if (browserNavigationCallback != null && !browserNavigationCallback.shouldNavigate(url)) {
shouldNavigate = false;
}
if (browserNavigationCallbacks != null) {
for (BrowserNavigationCallback cb : browserNavigationCallbacks) {
if (!cb.shouldNavigate(url)) {
shouldNavigate = false;
}
}
}
if (!url.startsWith("javascript:") && url.indexOf(RETURN_URL_PREFIX) != -1) {
// System.out.println("Received browser navigation callback "+url);
String result = decodeURL(url.substring(url.indexOf(RETURN_URL_PREFIX) + RETURN_URL_PREFIX.length()), "UTF-8");
// System.out.println("After decode "+result);
Result structResult = Result.fromContent(result, Result.JSON);
int callbackId = structResult.getAsInteger("callbackId");
final String value = structResult.getAsString("value");
final String type = structResult.getAsString("type");
final String errorMessage = structResult.getAsString("errorMessage");
final SuccessCallback<JSRef> callback = popReturnValueCallback(callbackId);
if (jsCallbacks != null && jsCallbacks.contains(callback)) {
// If this is a registered callback, then we treat it more like
// an event listener, and we retain it for future callbacks.
returnValueCallbacks.put(callbackId, callback);
}
if (callback != null) {
if (errorMessage != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (callback instanceof Callback) {
((Callback) callback).onError(this, new RuntimeException(errorMessage), 0, errorMessage);
}
}
});
} else {
Display.getInstance().callSerially(new Runnable() {
public void run() {
callback.onSucess(new JSRef(value, type));
}
});
}
} else {
Log.e(new RuntimeException("Received return value from javascript, but no callback could be found for that ID"));
}
shouldNavigate = false;
}
return shouldNavigate;
}
Aggregations