use of com.koushikdutta.async.future.SimpleFuture in project ion by koush.
the class VideoLoader method loadBitmap.
@Override
public Future<BitmapInfo> loadBitmap(Context context, Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, boolean animateGif) {
if (!uri.startsWith(ContentResolver.SCHEME_FILE))
return null;
final MediaFile.MediaFileType type = MediaFile.getFileType(uri);
if (type == null || !MediaFile.isVideoFileType(type.fileType))
return null;
final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
final File file = new File(URI.create(uri));
if (ret.isCancelled()) {
// Log.d("VideoLoader", "Bitmap load cancelled (no longer needed)");
return;
}
try {
Bitmap bmp;
if (mustUseThumbnailUtils() || Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1)
bmp = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
else
bmp = createVideoThumbnail(file.getAbsolutePath());
if (bmp == null)
throw new Exception("video bitmap failed to load");
// downsample this if its obscenely large
Point originalSize = new Point(bmp.getWidth(), bmp.getHeight());
if (bmp.getWidth() > resizeWidth * 2 && bmp.getHeight() > resizeHeight * 2) {
float xratio = (float) resizeWidth / bmp.getWidth();
float yratio = (float) resizeHeight / bmp.getHeight();
float ratio = Math.min(xratio, yratio);
if (ratio != 0)
bmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() * ratio), (int) (bmp.getHeight() * ratio), true);
}
BitmapInfo info = new BitmapInfo(key, type.mimeType, bmp, originalSize);
info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
ret.setComplete(info);
} catch (OutOfMemoryError e) {
ret.setComplete(new Exception(e));
} catch (Exception e) {
ret.setComplete(e);
}
}
});
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncHttpClient method websocket.
public Future<WebSocket> websocket(final AsyncHttpRequest req, String protocol, final WebSocketConnectCallback callback) {
WebSocketImpl.addWebSocketUpgradeHeaders(req, protocol);
final SimpleFuture<WebSocket> ret = new SimpleFuture<WebSocket>();
Cancellable connect = execute(req, new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
if (ex != null) {
if (ret.setComplete(ex)) {
if (callback != null)
callback.onCompleted(ex, null);
}
return;
}
WebSocket ws = WebSocketImpl.finishHandshake(req.getHeaders(), response);
if (ws == null) {
ex = new WebSocketHandshakeException("Unable to complete websocket handshake");
if (!ret.setComplete(ex))
return;
} else {
if (!ret.setComplete(ws))
return;
}
if (callback != null)
callback.onCompleted(ex, ws);
}
});
ret.setParent(connect);
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncHttpClient method execute.
public <T> SimpleFuture<T> execute(AsyncHttpRequest req, final AsyncParser<T> parser, final RequestCallback<T> callback) {
final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
final SimpleFuture<T> ret = new SimpleFuture<T>();
execute(req, 0, cancel, new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
if (ex != null) {
invoke(callback, ret, response, ex, null);
return;
}
invokeConnect(callback, response);
Future<T> parsed = parser.parse(response).setCallback(new FutureCallback<T>() {
@Override
public void onCompleted(Exception e, T result) {
invoke(callback, ret, response, e, result);
}
});
// reparent to the new parser future
ret.setParent(parsed);
}
});
ret.setParent(cancel);
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project ion by koush.
the class FileCacheStore method put.
private <T> Future<T> put(final T value, final AsyncParser<T> parser) {
final SimpleFuture<T> ret = new SimpleFuture<T>();
Ion.getIoExecutorService().execute(new Runnable() {
@Override
public void run() {
final String key = computeKey();
final File file = cache.getTempFile();
final FileDataSink sink = new FileDataSink(ion.getServer(), file);
parser.write(sink, value, new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
sink.end();
if (ex != null) {
file.delete();
ret.setComplete(ex);
return;
}
cache.commitTempFiles(key, file);
ret.setComplete(value);
}
});
}
});
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncHttpClient method websocket.
public Future<WebSocket> websocket(final AsyncHttpRequest req, String[] protocols, final WebSocketConnectCallback callback) {
WebSocketImpl.addWebSocketUpgradeHeaders(req, protocols);
final SimpleFuture<WebSocket> ret = new SimpleFuture<>();
Cancellable connect = execute(req, (ex, response) -> {
if (ex != null) {
if (ret.setComplete(ex)) {
if (callback != null)
callback.onCompleted(ex, null);
}
return;
}
WebSocket ws = WebSocketImpl.finishHandshake(req.getHeaders(), response);
if (ws == null) {
ex = new WebSocketHandshakeException("Unable to complete websocket handshake");
response.close();
if (!ret.setComplete(ex))
return;
} else {
if (!ret.setComplete(ws))
return;
}
if (callback != null)
callback.onCompleted(ex, ws);
});
ret.setParent(connect);
return ret;
}
Aggregations