use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class GoogleImpl method nativelogin.
@Override
public void nativelogin() {
loginCompleted = false;
nativeInterface.googleLogin(this);
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (!loginCompleted) {
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
}
}
}
});
if (callback != null) {
if (nativeIsLoggedIn()) {
this.setAccessToken(new AccessToken(nativeInterface.getGoogleToken(), null));
callback.loginSuccessful();
} else {
callback.loginFailed(loginMessage);
}
}
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class RequestBuilder method getAsStringAsync.
/**
* Executes the request asynchronously and writes the response to the provided
* Callback
* @param callback writes the response to this callback
*/
public void getAsStringAsync(final Callback<Response<String>> callback) {
ConnectionRequest request = createRequest(false);
request.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
Response res = null;
try {
res = new Response(evt.getResponseCode(), new String(evt.getConnectionRequest().getResponseData(), "UTF-8"), evt.getMessage());
callback.onSucess(res);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
});
CN.addToQueue(request);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class ImageDownloadService method createImageToFileSystem.
/**
* Constructs an image request that will automatically populate the given Label
* when the response arrives, it will cache the file locally.
*
* @param url the image URL
* @param callback the callback that should be updated when the data arrives
* @param destFile local file to store the data into the given path
*/
public static void createImageToFileSystem(String url, ActionListener callback, String destFile) {
Image im = cacheImage(null, false, destFile, null, null, defaultMaintainAspectRatio);
if (im != null) {
callback.actionPerformed(new NetworkEvent(null, im));
return;
}
// image not found on cache go and download from the url
ImageDownloadService i = new ImageDownloadService(url, callback);
i.cacheImages = true;
i.destinationFile = destFile;
i.setFailSilently(true);
NetworkManager.getInstance().addToQueue(i);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class ImageDownloadService method createImageToStorage.
/**
* Constructs an image request that will automatically populate the given Label
* when the response arrives, it will cache the file locally.
*
* @param url the image URL
* @param callback the callback that should be updated when the data arrives
* @param cacheId a unique identifier to be used to store the image into storage
* @param keep if set to true keeps the file in RAM once loaded
*/
public static void createImageToStorage(String url, ActionListener callback, String cacheId, boolean keep) {
Image im = cacheImage(cacheId, keep, null, null, null, defaultMaintainAspectRatio);
if (im != null) {
callback.actionPerformed(new NetworkEvent(null, im));
return;
}
// image not found on cache go and download from the url
ImageDownloadService i = new ImageDownloadService(url, callback);
i.cacheImages = true;
i.cacheId = cacheId;
i.setFailSilently(true);
NetworkManager.getInstance().addToQueue(i);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class NetworkManager method addErrorListener.
/**
* Adds a generic listener to a network error that is invoked before the exception is propagated.
* Notice that this doesn't apply to server error codes!
* Consume the event in order to prevent it from propagating further.
*
* @param e callback will be invoked with the Exception as the source object
*/
public void addErrorListener(ActionListener<NetworkEvent> e) {
if (errorListeners == null) {
errorListeners = new EventDispatcher();
errorListeners.setBlocking(true);
}
errorListeners.addListener(e);
}
Aggregations