Search in sources :

Example 6 with Storage

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

the class CloudStorage method deleteCloudFile.

/**
 * Deletes a file from the cloud storage
 *
 * @param fileId the file id to delete
 * @return true if the operation was successful
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public boolean deleteCloudFile(String fileId) {
    if (CloudPersona.getCurrentPersona().getToken() == null) {
        CloudPersona.createAnonymous();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/fileStoreDelete");
    req.addArgument("i", fileId);
    req.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    NetworkManager.getInstance().addToQueueAndWait(req);
    if (req.getResponseCode() == 200) {
        return new String(req.getResponseData()).equals("OK");
    }
    return false;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 7 with Storage

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

the class TestUtils method screenshotTest.

/**
 * The screenshot test takes a screenshot of the screen and compares it to
 * a prior screenshot, if both are 100% identical the test passes. If not
 * the test fails.<br>
 * If this is the first time the test is run then the screenshot is taken
 * and saved under the given name in the devices storage. The test passes
 * for this case but a warning is printed to the console. The name will have
 * .png appended to it so it will be identified.<br>
 * This test will only work on devices that support the ImageIO API with PNG
 * file format.
 *
 * @param screenshotName the name to use for the storage, must be unique!
 * @return true if the screenshots are identical or no prior screenshot exists
 * or if the test can't be run on this device. False if a screenshot exists and
 * it isn't 100% identical.
 */
public static boolean screenshotTest(String screenshotName) {
    if (verbose) {
        log("screenshotTest(" + screenshotName + ")");
    }
    try {
        ImageIO io = ImageIO.getImageIO();
        if (io == null || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
            log("screenshot test skipped due to no image IO support for PNG format");
            return true;
        }
        Image mute = Image.createImage(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
        Display.getInstance().getCurrent().paint(mute.getGraphics());
        screenshotName = screenshotName + ".png";
        if (Storage.getInstance().exists(screenshotName)) {
            int[] rgba = mute.getRGBCached();
            Image orig = Image.createImage(Storage.getInstance().createInputStream(screenshotName));
            int[] origRgba = orig.getRGBCached();
            orig = null;
            for (int iter = 0; iter < rgba.length; iter++) {
                if (rgba[iter] != origRgba[iter]) {
                    log("screenshots do not match at offset " + iter + " saving additional image under " + screenshotName + ".fail");
                    io.save(mute, Storage.getInstance().createOutputStream(screenshotName + ".fail"), ImageIO.FORMAT_PNG, 1);
                    return false;
                }
            }
        } else {
            io.save(mute, Storage.getInstance().createOutputStream(screenshotName), ImageIO.FORMAT_PNG, 1);
        }
        return true;
    } catch (IOException err) {
        log(err);
        return false;
    }
}
Also used : IOException(java.io.IOException) Image(com.codename1.ui.Image) ImageIO(com.codename1.ui.util.ImageIO)

Example 8 with Storage

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

Example 9 with Storage

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

the class CodenameOneImplementation method downloadImageToStorage.

/**
 * Downloads an image to storage. This will *not* first check to see if the image is located in storage
 * 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 downloadImageToStorage(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.downloadImageToStorage(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 10 with Storage

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

the class ConnectionRequest method downloadImage.

private void downloadImage(final SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail, boolean useCache) {
    setReadResponseForErrors(false);
    if (useCache) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                if (getDestinationFile() != null) {
                    String file = getDestinationFile();
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(file)) {
                        try {
                            EncodedImage img = EncodedImage.create(fs.openInputStream(file), (int) fs.getLength(file));
                            if (img == null) {
                                throw new IOException("Failed to load image at " + file);
                            }
                            CallbackDispatcher.dispatchSuccess(onSuccess, img);
                        } catch (Exception ex) {
                            CallbackDispatcher.dispatchError(onFail, ex);
                        }
                    } else {
                        downloadImage(onSuccess, onFail, false);
                    }
                } else if (getDestinationStorage() != null) {
                    String file = getDestinationStorage();
                    Storage fs = Storage.getInstance();
                    if (fs.exists(file)) {
                        try {
                            EncodedImage img = EncodedImage.create(fs.createInputStream(file), fs.entrySize(file));
                            if (img == null) {
                                throw new IOException("Failed to load image at " + file);
                            }
                            CallbackDispatcher.dispatchSuccess(onSuccess, img);
                        } catch (Exception ex) {
                            CallbackDispatcher.dispatchError(onFail, ex);
                        }
                    } else {
                        downloadImage(onSuccess, onFail, false);
                    }
                }
            }
        });
    } else {
        final ActionListener onDownload = new ActionListener<NetworkEvent>() {

            public void actionPerformed(NetworkEvent nevt) {
                int rc = nevt.getResponseCode();
                if (rc == 200 || rc == 201) {
                    downloadImage(onSuccess, onFail, true);
                } else {
                    if (nevt.getError() == null) {
                        nevt.setError(new IOException("Failed to get image:  Code was " + nevt.getResponseCode()));
                    }
                    CallbackDispatcher.dispatchError(onFail, nevt.getError());
                }
                removeResponseListener(this);
            }
        };
        addResponseListener(onDownload);
        NetworkManager.getInstance().addToQueue(this);
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) IOException(java.io.IOException) ParseException(com.codename1.l10n.ParseException)

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