Search in sources :

Example 26 with ConnectionRequest

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);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Form(com.codename1.ui.Form) Component(com.codename1.ui.Component)

Example 27 with ConnectionRequest

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();
    }
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InfiniteProgress(com.codename1.components.InfiniteProgress) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Example 28 with ConnectionRequest

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;
    }
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService) ConnectionRequest(com.codename1.io.ConnectionRequest) WebBrowser(com.codename1.components.WebBrowser) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback)

Example 29 with ConnectionRequest

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);
}
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 30 with ConnectionRequest

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;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)41 IOException (java.io.IOException)11 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 JSONParser (com.codename1.io.JSONParser)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 InputStreamReader (java.io.InputStreamReader)5 InfiniteProgress (com.codename1.components.InfiniteProgress)4 NetworkEvent (com.codename1.io.NetworkEvent)4 Dialog (com.codename1.ui.Dialog)4 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)3 DataInputStream (java.io.DataInputStream)3 Hashtable (java.util.Hashtable)3 Component (com.codename1.ui.Component)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2