Search in sources :

Example 1 with SuccessCallback

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

the class CodenameOneImplementation method downloadImageToCache.

/**
 * Downloads an image from a URL to the cache. Platforms
 * that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
 * platform's handling of cached images.  Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
 * will use that directory to cache the image.  Other platforms will just download to storage.
 *
 * @param url The URL of the image to download.
 * @param onSuccess Callback on success.
 * @param onFail Callback on fail.
 *
 * @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
 */
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
    FileSystemStorage fs = FileSystemStorage.getInstance();
    if (fs.hasCachesDir()) {
        String name = "cn1_image_cache[" + url + "]";
        name = StringUtil.replaceAll(name, "/", "_");
        name = StringUtil.replaceAll(name, "\\", "_");
        name = StringUtil.replaceAll(name, "%", "_");
        name = StringUtil.replaceAll(name, "?", "_");
        name = StringUtil.replaceAll(name, "*", "_");
        name = StringUtil.replaceAll(name, ":", "_");
        name = StringUtil.replaceAll(name, "=", "_");
        String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
        // We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
        // because we want it to try to load from file system first.
        Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
    } else {
        // We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
        // because we want it to try to load from storage first.
        Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 2 with SuccessCallback

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

the class RequestBuilder method getAsJsonMap.

/**
 * Executes the request asynchronously and writes the response to the provided
 * Callback
 * @param callback writes the response to this callback
 * @param onError the error callback
 * @return returns the Connection Request object so it can be killed if necessary
 */
public ConnectionRequest getAsJsonMap(final SuccessCallback<Response<Map>> callback, final FailureCallback<? extends Object> onError) {
    ConnectionRequest request = createRequest(true);
    request.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            if (onError != null) {
                // this is an error response code and should be handled as an error
                if (evt.getResponseCode() > 310) {
                    return;
                }
            }
            Response res = null;
            Map response = (Map) evt.getMetaData();
            res = new Response(evt.getResponseCode(), response, evt.getMessage());
            callback.onSucess(res);
        }
    });
    bindOnError(request, onError);
    CN.addToQueue(request);
    return request;
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with SuccessCallback

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

the class GoogleImpl method onConnected.

public void onConnected(Bundle bundle) {
    List<SuccessCallback<GoogleApiClient>> callbacks;
    synchronized (onConnectedCallbacks) {
        if (!onConnectedCallbacks.isEmpty()) {
            callbacks = new ArrayList<SuccessCallback<GoogleApiClient>>(onConnectedCallbacks);
            onConnectedCallbacks.clear();
        } else {
            callbacks = new ArrayList<SuccessCallback<GoogleApiClient>>();
        }
    }
    GoogleApiClient client = mGoogleApiClient;
    for (SuccessCallback<GoogleApiClient> cb : callbacks) {
        cb.onSucess(client);
    }
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) SuccessCallback(com.codename1.util.SuccessCallback)

Example 4 with SuccessCallback

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

the class ComponentSelector method fadeOut.

/**
 * Fades out components in this set.
 * @param duration Duration of animation.
 * @param callback Callback to run when animation completes.
 * @return Self for chaining.
 */
public ComponentSelector fadeOut(int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
                animations.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])), new Runnable() {

            public void run() {
                for (final Component c : animatingComponents) {
                    // c.setHidden(true);
                    c.setVisible(false);
                    final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    c.putClientProperty(placeholderProperty, null);
                    if (placeholder != null) {
                        Container parent = placeholder.getParent();
                        /*
                            if (parent == null) {
                                System.out.println("Deferring replace back");
                                $(new Runnable() {
                                    public void run() {
                                        Container parent = placeholder.getParent();
                                        if (parent != null) {
                                            System.out.println("Found parent after deferral");
                                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                                        }
                                    }
                                });
                            } else {
                            */
                        if (parent != null) {
                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                        }
                    // }
                    }
                }
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 5 with SuccessCallback

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

the class CodenameOneImplementation method downloadImageToFileSystem.

/**
 * Downloads an image to file system. This will *not* first check to see if the file exists already.
 * It will download and overwrite any existing image at the provided location.
 *
 * <p>Some platforms may override this method to use platform-level caching.  E.g. Javascript will use
 * the browser cache for downloading the image.</p>
 *
 * @param url The URL of the image to download.
 * @param fileName The storage key to be used to store the image.
 * @param onSuccess Callback on success.  Will be executed on EDT.
 * @param onFail Callback on failure.  Will be executed on EDT.
 */
public void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    cr.downloadImageToFileSystem(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Aggregations

SuccessCallback (com.codename1.util.SuccessCallback)4 ArrayList (java.util.ArrayList)4 ConnectionRequest (com.codename1.io.ConnectionRequest)3 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)3 IOException (java.io.IOException)3 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 FileSystemStorage (com.codename1.io.FileSystemStorage)1 NetworkEvent (com.codename1.io.NetworkEvent)1 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)1 ParseException (com.codename1.l10n.ParseException)1 Location (com.codename1.location.Location)1 Result (com.codename1.processing.Result)1 BrowserComponent (com.codename1.ui.BrowserComponent)1 JSRef (com.codename1.ui.BrowserComponent.JSRef)1 EncodedImage (com.codename1.ui.EncodedImage)1 BrowserNavigationCallback (com.codename1.ui.events.BrowserNavigationCallback)1 Point (com.codename1.ui.geom.Point)1 Style (com.codename1.ui.plaf.Style)1 Callback (com.codename1.util.Callback)1