Search in sources :

Example 31 with Callback

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

Example 32 with Callback

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

Example 33 with Callback

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);
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 34 with Callback

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);
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 35 with Callback

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);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Aggregations

ActionListener (com.codename1.ui.events.ActionListener)14 ActionEvent (com.codename1.ui.events.ActionEvent)12 IOException (java.io.IOException)11 ConnectionRequest (com.codename1.io.ConnectionRequest)10 EventDispatcher (com.codename1.ui.util.EventDispatcher)8 NetworkEvent (com.codename1.io.NetworkEvent)6 InputStream (java.io.InputStream)5 Intent (android.content.Intent)4 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)4 ArrayList (java.util.ArrayList)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)3 Uri (android.net.Uri)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 AccessToken (com.codename1.io.AccessToken)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)3 EncodedImage (com.codename1.ui.EncodedImage)3 Image (com.codename1.ui.Image)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3