use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class FullScreenAdService method bindTransitionAd.
/**
* Binds an ad to appear periodically after a given timeout
* @param timeForNext the timeout in which an ad should be shown in milliseconds
*/
public void bindTransitionAd(final int timeForNext) {
Runnable onTransitionAndExit = new Runnable() {
private long lastTime = System.currentTimeMillis();
public void run() {
long t = System.currentTimeMillis();
if (t - lastTime > timeForNext) {
lastTime = t;
Component c = getPendingAd();
if (c != null) {
Form adForm = new AdForm(c);
adForm.show();
}
}
}
};
CodenameOneImplementation.setOnCurrentFormChange(onTransitionAndExit);
CodenameOneImplementation.setOnExit(onTransitionAndExit);
Timer t = new Timer();
int tm = Math.max(5000, timeForNext - 600);
t.schedule(new TimerTask() {
public void run() {
if (!hasPendingAd()) {
ConnectionRequest r = createAdRequest();
r.setPriority(ConnectionRequest.PRIORITY_LOW);
r.setTimeout(timeout);
NetworkManager.getInstance().addToQueue(r);
}
}
}, tm, tm);
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class FullScreenAdService method showWelcomeAd.
/**
* Invoked on application startup, this code will download an ad or timeout
*/
public void showWelcomeAd() {
if (!UIManager.getInstance().wasThemeInstalled()) {
if (Display.getInstance().hasNativeTheme()) {
Display.getInstance().installNativeTheme();
}
}
ConnectionRequest r = createAdRequest();
r.setPriority(ConnectionRequest.PRIORITY_HIGH);
r.setTimeout(timeout);
InfiniteProgress ip = new InfiniteProgress();
Dialog ipDialog = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(r);
if (failed()) {
ipDialog.dispose();
if (!allowWithoutNetwork) {
ipDialog.dispose();
Dialog.show("Network Error", "Please try again later", "Exit", null);
Display.getInstance().exitApplication();
} else {
return;
}
}
Component c = getPendingAd();
if (c != null) {
Form adForm = new AdForm(c);
adForm.setTransitionInAnimator(CommonTransitions.createEmpty());
adForm.setTransitionOutAnimator(CommonTransitions.createEmpty());
adForm.show();
}
}
use of com.codename1.io.ConnectionRequest 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.io.ConnectionRequest 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.io.ConnectionRequest in project CodenameOne by codenameone.
the class FaceBookAccess method logOut.
/**
* log out the current user
*/
public static void logOut() {
ConnectionRequest req = new ConnectionRequest();
req.setPost(false);
req.setUrl("https://www.facebook.com/logout.php?access_token=" + token + "&confirm=1&next=" + redirectURI);
NetworkManager.getInstance().addToQueueAndWait(req);
token = null;
}
Aggregations