Search in sources :

Example 16 with Storage

use of com.codename1.io.Storage 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 17 with Storage

use of com.codename1.io.Storage in project CodenameOne by codenameone.

the class ImageDownloadService method cacheImage.

private static Image cacheImage(String cacheKey, boolean keep, String destFile, Dimension scale, Image placeholderImage, boolean maintainAspectRatio) {
    if (destFile != null) {
        if (FileSystemStorage.getInstance().exists(destFile)) {
            Image f;
            if (placeholderImage != null) {
                f = FileEncodedImageAsync.create(destFile, placeholderImage);
            } else {
                if (fastScale && scale != null) {
                    int w = scale.getWidth();
                    int h = scale.getHeight();
                    if (maintainAspectRatio) {
                        f = FileEncodedImage.create(destFile, -1, -1);
                        float actualW = f.getWidth();
                        float actualH = f.getHeight();
                        float r2 = Math.min(((float) w) / actualW, ((float) h) / actualH);
                        w = (int) (actualW * r2);
                        h = (int) (actualH * r2);
                    }
                    f = FileEncodedImage.create(destFile, w, h);
                } else {
                    f = FileEncodedImage.create(destFile, -1, -1);
                }
            }
            return f;
        }
    } else if (cacheKey != null) {
        if (Storage.getInstance().exists(cacheKey)) {
            Image s;
            if (placeholderImage != null) {
                s = StorageImageAsync.create(cacheKey, placeholderImage);
            } else {
                if (fastScale && scale != null) {
                    int w = scale.getWidth();
                    int h = scale.getHeight();
                    if (maintainAspectRatio) {
                        s = StorageImage.create(cacheKey, -1, -1);
                        float actualW = s.getWidth();
                        float actualH = s.getHeight();
                        float r2 = Math.min(((float) w) / actualW, ((float) h) / actualH);
                        w = (int) (actualW * r2);
                        h = (int) (actualH * r2);
                    }
                    s = StorageImage.create(cacheKey, w, h, keep);
                } else {
                    s = StorageImage.create(cacheKey, -1, -1, keep);
                }
                // due to the way the storage image works the data might be corrupted!
                if (((StorageImage) s).getImageData() == null) {
                    return null;
                }
            }
            return s;
        }
    }
    return null;
}
Also used : StorageImage(com.codename1.components.StorageImage) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 18 with Storage

use of com.codename1.io.Storage 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 to the Storage
 *
 * @param url the image URL
 * @param l the Label that should be updated when the data arrives
 * to just use storage and the url as the key
 * @param cacheId a unique identifier to be used to store the image into storage
 * @param toScale the scale dimension or null
 * @param priority the priority for the task
 */
private static void createImageToStorage(final String url, final Label l, final String cacheId, final boolean keep, final Dimension toScale, final byte priority, final Image placeholder, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, l, cacheId, keep, toScale, priority, placeholder, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, toScale, placeholder, maintainAspectRatio);
    if (im != null) {
        if (!fastScale && toScale != null) {
            im = scaleImage(im, toScale, defaultMaintainAspectRatio);
        }
        final Image i = im;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                l.setIcon(i);
                Form f = l.getComponentForm();
                if (f != null) {
                    f.revalidate();
                }
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, l);
    i.maintainAspectRatio = maintainAspectRatio;
    i.setDuplicateSupported(true);
    i.cacheImages = true;
    i.toScale = toScale;
    i.cacheId = cacheId;
    i.placeholder = placeholder;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : Form(com.codename1.ui.Form) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 19 with Storage

use of com.codename1.io.Storage in project CodenameOne by codenameone.

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given list
 * when the response arrives, it will cache the file locally as a file
 * in the file storage.
 * This assumes the GenericListCellRenderer style of
 * list which relies on a map based model approach.
 *
 * @param url the image URL
 * @param targetList the list that should be updated when the data arrives
 * @param targetModel the model
 * @param targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @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
 * @param scale the scale of the image to put in the List or null
 */
private static void createImageToStorage(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String cacheId, final boolean keep, final Dimension scale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, targetList, targetModel, targetOffset, targetKey, cacheId, keep, scale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, scale, placeholderImage, maintainAspectRatio);
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    // image not found on cache go and download from the url
    i.cacheImages = true;
    i.cacheId = cacheId;
    i.keep = keep;
    i.toScale = scale;
    i.placeholder = placeholderImage;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 20 with Storage

use of com.codename1.io.Storage in project CodenameOne by codenameone.

the class Purchase method getPendingPurchases.

/**
 * Gets a list of purchases that haven't yet been sent to the server.
 * @return
 */
private List<Receipt> getPendingPurchases() {
    synchronized (PENDING_PURCHASE_KEY) {
        Storage s = Storage.getInstance();
        Util.register(new Receipt());
        if (s.exists(PENDING_PURCHASE_KEY)) {
            return (List<Receipt>) s.readObject(PENDING_PURCHASE_KEY);
        } else {
            return new ArrayList<Receipt>();
        }
    }
}
Also used : Storage(com.codename1.io.Storage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)7 Image (com.codename1.ui.Image)7 FileEncodedImage (com.codename1.components.FileEncodedImage)6 StorageImage (com.codename1.components.StorageImage)5 ConnectionRequest (com.codename1.io.ConnectionRequest)5 IOException (java.io.IOException)5 FileSystemStorage (com.codename1.io.FileSystemStorage)3 Storage (com.codename1.io.Storage)3 InputStream (java.io.InputStream)3 BufferedInputStream (com.codename1.io.BufferedInputStream)2 BufferedOutputStream (com.codename1.io.BufferedOutputStream)2 Form (com.codename1.ui.Form)2 FontFormatException (java.awt.FontFormatException)2 Point (java.awt.Point)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 EOFException (java.io.EOFException)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2