use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class CodenameOneImplementation method openGallery.
/**
* Opens the device gallery
* The method returns immediately and the response will be sent asynchronously
* to the given ActionListener Object
*
* use this in the actionPerformed to retrieve the file path
* String path = (String) evt.getSource();
*
* @param response a callback Object to retrieve the file path
* @param type one of the following GALLERY_IMAGE, GALLERY_VIDEO, GALLERY_ALL
* @throws RuntimeException if this feature failed or unsupported on the platform
*/
public void openGallery(final ActionListener response, int type) {
final Dialog d = new Dialog("Select a picture");
d.setLayout(new BorderLayout());
FileTreeModel model = new FileTreeModel(true);
if (type == Display.GALLERY_IMAGE) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
} else if (type == Display.GALLERY_VIDEO) {
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
} else if (type == Display.GALLERY_ALL) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
}
FileTree t = new FileTree(model) {
protected Button createNodeComponent(final Object node, int depth) {
if (node == null || !getModel().isLeaf(node)) {
return super.createNodeComponent(node, depth);
}
Hashtable t = (Hashtable) Storage.getInstance().readObject("thumbnails");
if (t == null) {
t = new Hashtable();
}
final Hashtable thumbs = t;
final Button b = super.createNodeComponent(node, depth);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
response.actionPerformed(new ActionEvent(node, ActionEvent.Type.Other));
d.dispose();
}
});
final ImageIO imageio = ImageIO.getImageIO();
if (imageio != null) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
byte[] data = (byte[]) thumbs.get(node);
if (data == null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
imageio.save(FileSystemStorage.getInstance().openInputStream((String) node), out, ImageIO.FORMAT_JPEG, b.getIcon().getWidth(), b.getIcon().getHeight(), 1);
data = out.toByteArray();
thumbs.put(node, data);
Storage.getInstance().writeObject("thumbnails", thumbs);
} catch (IOException ex) {
Log.e(ex);
}
}
Image im = Image.createImage(data, 0, data.length);
b.setIcon(im);
}
});
}
return b;
}
};
d.addComponent(BorderLayout.CENTER, t);
d.placeButtonCommands(new Command[] { new Command("Cancel") });
Command c = d.showAtPosition(2, 2, 2, 2, true);
if (c != null) {
response.actionPerformed(null);
}
}
use of com.codename1.util.Callback 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);
}
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class RequestBuilder method getAsBytesAsync.
/**
* Executes the request asynchronously and writes the response to the provided
* Callback
* @param callback writes the response to this callback
*/
public void getAsBytesAsync(final Callback<Response<byte[]>> callback) {
ConnectionRequest request = createRequest(false);
request.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
Response res = null;
res = new Response(evt.getResponseCode(), evt.getConnectionRequest().getResponseData(), evt.getMessage());
callback.onSucess(res);
}
});
CN.addToQueue(request);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class RequestBuilder method getAsJsonMap.
/**
* Executes the request asynchronously and writes the response to the provided
* Callback
* @param callback writes the response to this callback
* @param onError the error callback
* @return returns the Connection Request object so it can be killed if necessary
*/
public ConnectionRequest getAsJsonMap(final SuccessCallback<Response<Map>> callback, final FailureCallback<? extends Object> onError) {
ConnectionRequest request = createRequest(true);
request.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
if (onError != null) {
// this is an error response code and should be handled as an error
if (evt.getResponseCode() > 310) {
return;
}
}
Response res = null;
Map response = (Map) evt.getMetaData();
res = new Response(evt.getResponseCode(), response, evt.getMessage());
callback.onSucess(res);
}
});
bindOnError(request, onError);
CN.addToQueue(request);
return request;
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class FaceBookAccess method postOnWall.
/**
* Post a message on the users wall
*
* @param userId the userId
* @param message the message to post
* @param name
* @param link
* @param description
* @param picture
* @param caption
*/
public void postOnWall(String userId, String message, String name, String link, String description, String picture, String caption, ActionListener callback) throws IOException {
checkAuthentication();
FacebookRESTService con = new FacebookRESTService(token, userId, FacebookRESTService.FEED, true);
if (message != null) {
con.addArgument("message", message);
}
if (name != null) {
con.addArgument("name", name);
}
if (link != null) {
con.addArgument("link", link);
}
if (description != null) {
con.addArgument("description", description);
}
if (picture != null) {
con.addArgument("picture", picture);
}
if (caption != null) {
con.addArgument("caption", caption);
}
con.addResponseListener(new Listener(con, callback));
if (slider != null) {
SliderBridge.bindProgress(con, slider);
}
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
current = con;
NetworkManager.getInstance().addToQueueAndWait(con);
}
Aggregations