Search in sources :

Example 51 with Data

use of com.codename1.ui.util.xml.Data 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 52 with Data

use of com.codename1.ui.util.xml.Data 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 53 with Data

use of com.codename1.ui.util.xml.Data 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 54 with Data

use of com.codename1.ui.util.xml.Data 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 55 with Data

use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.

the class Purchase method loadReceipts.

/**
 * Fetches receipts from the IAP service so that we know we are dealing
 * with current data.  This method should be called before checking a
 * subscription expiry date so that any changes the user has made in the
 * store is reflected here (e.g. cancelling or renewing subscription).
 * @param ifOlderThanMs Update is only performed if more than {@code ifOlderThanMs} milliseconds has elapsed
 * since the last successful fetch.
 * @param callback Callback called when request is complete.  Passed {@code true} if
 * the data was successfully fetched.  {@code false} otherwise.
 */
private final void loadReceipts(long ifOlderThanMs, final SuccessCallback<Boolean> callback) {
    if (loadInProgress) {
        Log.p("Did not load receipts because another load is in progress");
        callback.onSucess(false);
        return;
    }
    loadInProgress = true;
    Date lastRefreshTime = getReceiptsRefreshTime();
    Date now = new Date();
    if (lastRefreshTime.getTime() + ifOlderThanMs > now.getTime()) {
        Log.p("Receipts were last refreshed at " + lastRefreshTime + " so we won't refetch.");
        loadInProgress = false;
        callback.onSucess(true);
        return;
    }
    List<Receipt> oldData = new ArrayList<Receipt>();
    oldData.addAll(getReceipts());
    SuccessCallback<Receipt[]> onSuccess = new SuccessCallback<Receipt[]>() {

        public void onSucess(Receipt[] value) {
            if (value != null) {
                setReceipts(Arrays.asList(value));
                setReceiptsRefreshTime(new Date());
                loadInProgress = false;
                callback.onSucess(Boolean.TRUE);
            } else {
                loadInProgress = false;
                callback.onSucess(Boolean.FALSE);
            }
        }
    };
    if (receiptStore != null) {
        receiptStore.fetchReceipts(onSuccess);
    } else {
        Log.p("No receipt store is currently registered so no receipts were fetched");
        loadInProgress = false;
        callback.onSucess(Boolean.FALSE);
    }
}
Also used : SuccessCallback(com.codename1.util.SuccessCallback) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

IOException (java.io.IOException)18 EncodedImage (com.codename1.ui.EncodedImage)12 Hashtable (java.util.Hashtable)12 Image (com.codename1.ui.Image)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 FileInputStream (java.io.FileInputStream)8 ActionEvent (com.codename1.ui.events.ActionEvent)7 ActionListener (com.codename1.ui.events.ActionListener)7 FileEncodedImage (com.codename1.components.FileEncodedImage)6 ConnectionRequest (com.codename1.io.ConnectionRequest)6 File (java.io.File)6 Intent (android.content.Intent)5 StorageImage (com.codename1.components.StorageImage)5 IntentResultListener (com.codename1.impl.android.IntentResultListener)5 EditableResources (com.codename1.ui.util.EditableResources)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DataInputStream (java.io.DataInputStream)5 Vector (java.util.Vector)5 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)4