Search in sources :

Example 11 with SimpleFuture

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;
}
Also used : Bitmap(android.graphics.Bitmap) Point(android.graphics.Point) File(java.io.File) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 12 with SimpleFuture

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;
}
Also used : HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) Cancellable(com.koushikdutta.async.future.Cancellable) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 13 with SimpleFuture

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;
}
Also used : HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture) Future(com.koushikdutta.async.future.Future) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FutureCallback(com.koushikdutta.async.future.FutureCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 14 with SimpleFuture

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;
}
Also used : FileDataSink(com.koushikdutta.async.stream.FileDataSink) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) File(java.io.File) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 15 with SimpleFuture

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;
}
Also used : Cancellable(com.koushikdutta.async.future.Cancellable) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Aggregations

SimpleFuture (com.koushikdutta.async.future.SimpleFuture)19 IOException (java.io.IOException)6 Bitmap (android.graphics.Bitmap)5 Point (android.graphics.Point)5 BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)5 ByteBufferList (com.koushikdutta.async.ByteBufferList)4 DataEmitter (com.koushikdutta.async.DataEmitter)4 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)4 DataCallback (com.koushikdutta.async.callback.DataCallback)4 File (java.io.File)4 InetSocketAddress (java.net.InetSocketAddress)4 TimeoutException (java.util.concurrent.TimeoutException)4 AsyncSSLException (com.koushikdutta.async.AsyncSSLException)3 Cancellable (com.koushikdutta.async.future.Cancellable)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 FileNotFoundException (java.io.FileNotFoundException)3 Future (com.koushikdutta.async.future.Future)2 FutureCallback (com.koushikdutta.async.future.FutureCallback)2 InputStream (java.io.InputStream)2 CancelledKeyException (java.nio.channels.CancelledKeyException)2