Search in sources :

Example 6 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class VideoTransition method fadeOut.

private AsyncResource<Component> fadeOut(Component cmp, int duration) {
    AsyncResource<Component> out = new AsyncResource<Component>();
    Motion m = Motion.createEaseInMotion(cmp.getStyle().getOpacity(), 0, duration);
    Timer[] ts = new Timer[1];
    Timer t = CN.setInterval(30, () -> {
        int currOpacity = cmp.getStyle().getOpacity();
        int newOpacity = m.getValue();
        if (currOpacity != newOpacity) {
            cmp.getStyle().setBgTransparency(newOpacity);
            cmp.getStyle().setOpacity(newOpacity);
            cmp.repaint();
        }
        if (newOpacity == m.getDestinationValue()) {
            ts[0].cancel();
            out.complete(cmp);
        }
    });
    ts[0] = t;
    m.start();
    return out;
}
Also used : Motion(com.codename1.ui.animations.Motion) Timer(java.util.Timer) Component(com.codename1.ui.Component) AsyncResource(com.codename1.util.AsyncResource)

Example 7 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class ConnectionRequest method fetchJSONAsync.

/**
 * Fetches JSON asynchronously.
 * @param url The URL to fetch.
 * @return AsyncResource that will resolve with either an exception or the parsed JSON data.
 * @since 7.0
 */
public static AsyncResource<Map<String, Object>> fetchJSONAsync(String url) {
    final AsyncResource<Map<String, Object>> out = new AsyncResource<Map<String, Object>>();
    final ConnectionRequest cr = new ConnectionRequest();
    cr.setFailSilently(true);
    cr.setPost(false);
    cr.setUrl(url);
    cr.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            if (out.isDone()) {
                return;
            }
            if (cr.getResponseData() == null) {
                if (cr.failureException != null) {
                    out.error(new IOException(cr.failureException.toString()));
                    return;
                } else {
                    out.error(new IOException("Server returned error code: " + cr.failureErrorCode));
                    return;
                }
            }
            JSONParser jp = new JSONParser();
            Map<String, Object> result = null;
            try {
                result = jp.parseJSON(new InputStreamReader(new ByteArrayInputStream(cr.getResponseData()), "UTF-8"));
            } catch (IOException ex) {
                out.error(ex);
                return;
            }
            out.complete(result);
        }
    });
    NetworkManager.getInstance().addToQueue(cr);
    return out;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) AsyncResource(com.codename1.util.AsyncResource) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class BrowserComponent method ready.

/**
 * Returns a promise that will complete when the browser component is "ready".  It is considered to be
 * ready once it has received the start or load event from at least one page.
 * @return AsyncResouce that will complete when the browser component is ready.
 * @param timeout Timeout in milliseconds to wait.
 * @since 7.0
 */
public AsyncResource<BrowserComponent> ready(int timeout) {
    final AsyncResource<BrowserComponent> out = new AsyncResource<BrowserComponent>();
    if (ready) {
        out.complete(this);
    } else {
        class LoadWrapper {

            Timer timer;

            ActionListener l;
        }
        final LoadWrapper w = new LoadWrapper();
        if (timeout > 0) {
            w.timer = CN.setTimeout(timeout, new Runnable() {

                public void run() {
                    w.timer = null;
                    if (w.l != null) {
                        removeWebEventListener(onStart, w.l);
                        removeWebEventListener(onLoad, w.l);
                    }
                    if (!out.isDone()) {
                        out.error(new RuntimeException("Timeout exceeded waiting for browser component to be ready"));
                    }
                }
            });
        }
        w.l = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                w.l = null;
                if (w.timer != null) {
                    w.timer.cancel();
                    w.timer = null;
                }
                removeWebEventListener(onStart, this);
                removeWebEventListener(onLoad, this);
                if (!out.isDone()) {
                    out.complete(BrowserComponent.this);
                }
            }
        };
        addWebEventListener(onStart, w.l);
        addWebEventListener(onLoad, w.l);
    }
    return out;
}
Also used : UITimer(com.codename1.ui.util.UITimer) Timer(java.util.Timer) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) AsyncResource(com.codename1.util.AsyncResource)

Example 9 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class BillingSupport method requireConnection.

private AsyncResource requireConnection() {
    final AsyncResource out;
    synchronized (this) {
        if (pendingConnection != null) {
            return pendingConnection;
        }
        out = new AsyncResource();
        pendingConnection = out;
    }
    if (!activity.isBillingEnabled()) {
        out.error(new UnsupportedOperationException("Billing is not enabled."));
        return out;
    }
    if (billingClient == null) {
        billingClient = BillingClient.newBuilder(activity).setListener(purchasesUpdatedListener).enablePendingPurchases().build();
    }
    if (billingConnected) {
        out.complete(true);
    } else {
        billingClient.startConnection(new com.android.billingclient.api.BillingClientStateListener() {

            @Override
            public void onBillingSetupFinished(com.android.billingclient.api.BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    billingConnected = true;
                    synchronized (BillingSupport.this) {
                        pendingConnection = null;
                    }
                    consumeAndAcknowlegePurchases();
                    out.complete(true);
                } else {
                    synchronized (BillingSupport.this) {
                        pendingConnection = null;
                    }
                    System.err.println("Failed to connect to billing service: " + billingResult.getDebugMessage());
                    out.error(new IOException(billingResult.getDebugMessage()));
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                billingConnected = false;
            }
        });
    }
    return out;
}
Also used : BillingResult(com.android.billingclient.api.BillingResult) IOException(java.io.IOException) AsyncResource(com.codename1.util.AsyncResource)

Example 10 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class LoadingTextAnimationSample method fetchDataAsync.

private AsyncResource<MyData> fetchDataAsync() {
    final AsyncResource<MyData> out = new AsyncResource<>();
    Timer t = new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            out.complete(new MyData());
        }
    }, 2000);
    return out;
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AsyncResource(com.codename1.util.AsyncResource)

Aggregations

AsyncResource (com.codename1.util.AsyncResource)20 IOException (java.io.IOException)6 Timer (java.util.Timer)5 AbstractMedia (com.codename1.media.AbstractMedia)4 AsyncMedia (com.codename1.media.AsyncMedia)4 Media (com.codename1.media.Media)4 Map (java.util.Map)4 JFrame (javax.swing.JFrame)4 Element (com.codename1.xml.Element)3 InputStreamReader (java.io.InputStreamReader)3 Component (com.codename1.ui.Component)2 Form (com.codename1.ui.Form)2 Toolbar (com.codename1.ui.Toolbar)2 Motion (com.codename1.ui.animations.Motion)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 StringReader (java.io.StringReader)2 TimerTask (java.util.TimerTask)2 BillingResult (com.android.billingclient.api.BillingResult)1