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