use of com.codename1.io.Storage in project CodenameOne by codenameone.
the class Purchase method addPendingPurchase.
/**
* Adds a receipt to be pushed to the server.
* @param receipt
*/
private void addPendingPurchase(Receipt receipt) {
synchronized (PENDING_PURCHASE_KEY) {
Storage s = Storage.getInstance();
List<Receipt> pendingPurchases = getPendingPurchases();
pendingPurchases.add(receipt);
s.writeObject(PENDING_PURCHASE_KEY, pendingPurchases);
}
}
use of com.codename1.io.Storage in project CodenameOne by codenameone.
the class Util method downloadUrlTo.
private static boolean downloadUrlTo(String url, String fileName, boolean showProgress, boolean background, boolean storage, ActionListener callback) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setReadResponseForErrors(false);
cr.setDuplicateSupported(true);
cr.setUrl(url);
if (callback != null) {
cr.addResponseListener(callback);
}
if (storage) {
cr.setDestinationStorage(fileName);
} else {
cr.setDestinationFile(fileName);
}
if (background) {
NetworkManager.getInstance().addToQueue(cr);
return true;
}
if (showProgress) {
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(cr);
d.dispose();
} else {
NetworkManager.getInstance().addToQueueAndWait(cr);
}
int rc = cr.getResponseCode();
return rc == 200 || rc == 201;
}
use of com.codename1.io.Storage in project CodenameOne by codenameone.
the class URLImage method fetch.
/**
* Images are normally fetched from storage or network only as needed,
* however if the download must start before the image is drawn this method
* can be invoked. Notice that "immediately" doesn't mean synchronously, it just
* means that the image will be added to the queue right away but probably won't be
* available by the time the method completes.
*/
public void fetch() {
if (fetching || imageData != null) {
return;
}
fetching = true;
try {
locked = super.isLocked();
if (storageFile != null) {
if (Storage.getInstance().exists(storageFile)) {
super.unlock();
imageData = new byte[Storage.getInstance().entrySize(storageFile)];
InputStream is = Storage.getInstance().createInputStream(storageFile);
Util.readFully(is, imageData);
resetCache();
fetching = false;
repaintImage = true;
return;
}
if (adapter != null) {
Util.downloadImageToStorage(url, storageFile + IMAGE_SUFFIX, new SuccessCallback<Image>() {
public void onSucess(Image value) {
DownloadCompleted onComplete = new DownloadCompleted();
onComplete.setSourceImage(value);
onComplete.actionPerformed(new ActionEvent(value));
}
});
} else {
Util.downloadImageToStorage(url, storageFile, new SuccessCallback<Image>() {
public void onSucess(Image value) {
DownloadCompleted onComplete = new DownloadCompleted();
onComplete.setSourceImage(value);
onComplete.actionPerformed(new ActionEvent(value));
}
});
}
} else {
if (FileSystemStorage.getInstance().exists(fileSystemFile)) {
super.unlock();
imageData = new byte[(int) FileSystemStorage.getInstance().getLength(fileSystemFile)];
InputStream is = FileSystemStorage.getInstance().openInputStream(fileSystemFile);
Util.readFully(is, imageData);
resetCache();
fetching = false;
repaintImage = true;
return;
}
if (adapter != null) {
Util.downloadImageToFileSystem(url, fileSystemFile + IMAGE_SUFFIX, new SuccessCallback<Image>() {
public void onSucess(Image value) {
DownloadCompleted onComplete = new DownloadCompleted();
onComplete.setSourceImage(value);
onComplete.actionPerformed(new ActionEvent(value));
}
});
} else {
Util.downloadImageToFileSystem(url, fileSystemFile, new SuccessCallback<Image>() {
public void onSucess(Image value) {
DownloadCompleted onComplete = new DownloadCompleted();
onComplete.setSourceImage(value);
onComplete.actionPerformed(new ActionEvent(value));
}
});
}
}
} catch (IOException ioErr) {
throw new RuntimeException(ioErr.toString());
}
}
use of com.codename1.io.Storage in project CodenameOne by codenameone.
the class URLImage method createCachedImage.
/**
* Creates an image that will be downloaded on the fly as necessary. On platforms that support a native
* image cache (e.g. Javascript), the image will be loaded directly from the native cache (i.e. it defers to the
* platform to handle all caching considerations. On platforms that don't have a native image cache but
* do have a caches directory {@link FileSystemStorage#hasCachesDir()}, this will call {@link #createToFileSystem(com.codename1.ui.EncodedImage, java.lang.String, java.lang.String, com.codename1.ui.URLImage.ImageAdapter) }
* with a file location in the caches directory. In all other cases, this will call {@link #createToStorage(com.codename1.ui.EncodedImage, java.lang.String, java.lang.String) }.
*
* @param imageName The name of the image.
* @param url the URL from which the image is fetched
* @param placeholder the image placeholder is shown as the image is loading/downloading
* and serves as the guideline to the size of the downloaded image.
* @param resizeRule One of {@link #FLAG_RESIZE_FAIL}, {@link #FLAG_RESIZE_SCALE}, or {@link #FLAG_RESIZE_SCALE_TO_FILL}.
* @return a Image that will initially just delegate to the placeholder
*/
public static Image createCachedImage(String imageName, String url, Image placeholder, int resizeRule) {
if (Display.getInstance().supportsNativeImageCache()) {
CachedImage im = new CachedImage(placeholder, url, resizeRule);
im.setImageName(imageName);
return im;
} else {
ImageAdapter adapter = null;
switch(resizeRule) {
case FLAG_RESIZE_FAIL:
adapter = RESIZE_FAIL;
break;
case FLAG_RESIZE_SCALE:
adapter = RESIZE_SCALE;
break;
case FLAG_RESIZE_SCALE_TO_FILL:
adapter = RESIZE_SCALE_TO_FILL;
break;
default:
adapter = RESIZE_SCALE_TO_FILL;
break;
}
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;
// System.out.println("Creating to file system "+filePath);
URLImage im = createToFileSystem(EncodedImage.createFromImage(placeholder, false), filePath, url, adapter);
im.setImageName(imageName);
return im;
} else {
// System.out.println("Creating to storage ");
URLImage im = createToStorage(EncodedImage.createFromImage(placeholder, false), "cn1_image_cache[" + url + "@" + placeholder.getWidth() + "x" + placeholder.getHeight(), url, adapter);
im.setImageName(imageName);
return im;
}
}
}
use of com.codename1.io.Storage in project CodenameOne by codenameone.
the class PropertyIndex method loadJSON.
/**
* Loads JSON for the object from storage with the given name
* @param name the name of the storage
*/
public void loadJSON(String name) {
try {
InputStream is = Storage.getInstance().createInputStream(name);
JSONParser jp = new JSONParser();
JSONParser.setUseBoolean(true);
JSONParser.setUseLongs(true);
populateFromMap(jp.parseJSON(new InputStreamReader(is, "UTF-8")), parent.getClass());
} catch (IOException err) {
Log.e(err);
throw new RuntimeException(err.toString());
}
}
Aggregations