Search in sources :

Example 1 with Storage

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

the class Purchase method removePendingPurchase.

/**
 * Removes a receipt from pending purchases.
 * @param transactionId
 * @return
 */
private Receipt removePendingPurchase(String transactionId) {
    synchronized (PENDING_PURCHASE_KEY) {
        Storage s = Storage.getInstance();
        List<Receipt> pendingPurchases = getPendingPurchases();
        Receipt found = null;
        for (Receipt r : pendingPurchases) {
            if (r.getTransactionId() != null && r.getTransactionId().equals(transactionId)) {
                found = r;
                break;
            }
        }
        if (found != null) {
            pendingPurchases.remove(found);
            s.writeObject(PENDING_PURCHASE_KEY, pendingPurchases);
            return found;
        } else {
            return null;
        }
    }
}
Also used : Storage(com.codename1.io.Storage)

Example 2 with Storage

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

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

the class CodenameOneImplementation method installTar.

/**
 * Installs a tar file from the build server into the file system storage so it can be used with respect for hierarchy
 */
public void installTar() throws IOException {
    String p = Preferences.get("cn1$InstallKey", null);
    String buildKey = Display.getInstance().getProperty("build_key", null);
    if (p == null || !p.equals(buildKey)) {
        FileSystemStorage fs = FileSystemStorage.getInstance();
        String tardir = fs.getAppHomePath() + "cn1html/";
        fs.mkdir(tardir);
        TarInputStream is = new TarInputStream(Display.getInstance().getResourceAsStream(getClass(), "/html.tar"));
        TarEntry t = is.getNextEntry();
        byte[] data = new byte[8192];
        while (t != null) {
            String name = t.getName();
            if (t.isDirectory()) {
                fs.mkdir(tardir + name);
            } else {
                String path = tardir + name;
                String dir = path.substring(0, path.lastIndexOf('/'));
                if (!fs.exists(dir)) {
                    mkdirs(fs, dir);
                }
                OutputStream os = fs.openOutputStream(tardir + name);
                int count;
                while ((count = is.read(data)) != -1) {
                    os.write(data, 0, count);
                }
                os.close();
            }
            t = is.getNextEntry();
        }
        Util.cleanup(is);
        Preferences.set("cn1$InstallKey", buildKey);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage) TarInputStream(com.codename1.io.tar.TarInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TarEntry(com.codename1.io.tar.TarEntry)

Example 4 with Storage

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

the class ImageDownloadService method createImageToFileSystem.

/**
 * 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 targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param destFile local file to store the data into the given path
 */
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    i.cacheImages = true;
    i.destinationFile = destFile;
    i.toScale = toScale;
    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 5 with Storage

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

the class CloudStorage method deleteAllCloudFilesForUser.

/**
 * Deletes all the cloud files under this user, notice that this method
 * is asynchronous and a background server process performs the actual deletion
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public void deleteAllCloudFilesForUser() {
    if (CloudPersona.getCurrentPersona().getToken() == null) {
        return;
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/purgeCloudFiles");
    req.addArgument("own", CloudPersona.getCurrentPersona().getToken());
    req.addArgument("u", Display.getInstance().getProperty("built_by_user", ""));
    NetworkManager.getInstance().addToQueue(req);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

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