Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion;
  
  import  android.graphics.Bitmap;
  import  android.graphics.drawable.BitmapDrawable;
  import  android.graphics.drawable.Drawable;
  import  android.os.Handler;
  import  android.os.Looper;
  import  android.text.TextUtils;
  import  android.view.animation.Animation;
 import  android.widget.ImageView;
 import  com.koushikdutta.async.AsyncServer;
 import  com.koushikdutta.async.ByteBufferList;
 import  com.koushikdutta.async.future.Future;
 import  com.koushikdutta.async.future.FutureCallback;
 import  com.koushikdutta.async.future.SimpleFuture;
 import  com.koushikdutta.async.parser.ByteBufferListParser;
 
Created by koush on 5/23/13.
 
     Ion ion;
 
     @Override
     public Future<Bitmap> load(String uri) {
         .load(uri);
         return intoImageView(.get());
     }
 
     @Override
     public Future<Bitmap> load(String methodString url) {
         .load(methodurl);
         return intoImageView(.get());
     }
 
     WeakReference<ImageView> imageViewPostRef;
     IonBitmapRequestBuilder withImageView(ImageView imageView) {
          = new WeakReference<ImageView>(imageView);
         return this;
     }
 
     public IonBitmapRequestBuilder(IonRequestBuilder builder) {
         this. = builder;
          = builder.ion;
     }
 
 
     @Override
     public IonBitmapRequestBuilder transform(Transform transform) {
         if ( == null)
              = new ArrayList<Transform>();
         .add(transform);
         return this;
     }
 
     static class ByteArrayToBitmapFuture extends MutateFuture<Bitmap, ByteBufferList> {
         Handler handler;
         Ion ion;
         String urlKey;
 
         public ByteArrayToBitmapFuture(Ion ion, Handler handlerString urlKeyExecutorService executorService) {
             this. = executorService;
             this. = handler;
             this. = urlKey;
             this. = ion;
         }
 
         class Setter implements Runnable {
             Bitmap bmp;
             Exception e;
 
             Setter(Exception e, Bitmap bmp) {
                 this. = bmp;
                 this. = e;
             }
 
             @Override
             public void run() {
                 ..remove();
                 if ( != null)
                     setComplete();
                 else
                     setComplete();
                  = null;
                  = null;
            }
        }
        @Override
        public void onCompleted(Exception e, ByteBufferList result) {
            assert Thread.currentThread() == Looper.getMainLooper().getThread();
            if (e != null) {
                AsyncServer.post(new Setter(enull));
                return;
            }
            final ByteArrayInputStream bin = new ByteArrayInputStream(result.getAllByteArray());
            .execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Bitmap bmp = ..loadBitmapFromStream(bin);
                        if (bmp == null)
                            throw new Exception("bitmap failed to load");
                        AsyncServer.post(new Setter(nullbmp));
                    }
                    catch (Exception e) {
                        AsyncServer.post(new Setter(enull));
                    }
                }
            });
        }
    }
    static class BitmapToBitmap extends MutateFuture<IonBitmapCache.ZombieDrawable, Bitmap> {
        Handler handler;
        Ion ion;
        String transformKey;
        ArrayList<Transformtransforms;
        public BitmapToBitmap(Ion ion, Handler handlerString transformKeyArrayList<TransformtransformsExecutorService executorService) {
            this. = executorService;
            this. = handler;
            this. = transformKey;
            this. = transforms;
            this. = ion;
        }
        class Setter implements Runnable {
            Bitmap bmp;
            Exception e;
            Setter(Exception e, Bitmap bmp) {
                this. = bmp;
                this. = e;
            }
            @Override
            public void run() {
                ..remove();
                if ( != null) {
                    setComplete();
                }
                else {
                    IonBitmapCache.ZombieDrawable bd = ..put();
                    setComplete(bd);
                }
                 = null;
                 = null;
            }
        }
        public void transform(final Bitmap result) {
            .execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Bitmap tmpBitmap = result;
                        for (Transform transform : ) {
                            tmpBitmap = transform.transform(tmpBitmap);
                        }
                        AsyncServer.post(new Setter(nulltmpBitmap));
                    }
                    catch (Exception e) {
                        AsyncServer.post(new Setter(enull));
                    }
                }
            });
        }
        @Override
        public void onCompleted(Exception e, Bitmap result) {
            assert Thread.currentThread() == Looper.getMainLooper().getThread();
            if (e != null) {
                AsyncServer.post(new Setter(enull));
                return;
            }
            // no transform necessary
            if ( == null || .size() == 0) {
                AsyncServer.post(new Setter(nullresult));
                return;
            }
            transform(result);
        }
    }
    @Override
    public Future<Bitmap> intoImageView(ImageView imageView) {
        assert Thread.currentThread() == Looper.getMainLooper().getThread();
        if (imageView != null)
            ..remove(imageView);
//        ResponseCacheMiddleware cache = ion.responseCache;
//        IonLog.i("cache hits: " + cache.getCacheHitCount());
//        IonLog.i("cond cache hits: " + cache.getConditionalCacheHitCount());
        // determine the key for this bitmap after all transformations
        String tmpKey = ..getUri().toString();
        if ( != null) {
            for (Transform transform : ) {
                tmpKey += transform.getKey();
            }
        }
        final String transformKey = tmpKey;
        // the future to return to the caller
        final SimpleFuture<Bitmap> ret = new SimpleFuture<Bitmap>();
        // see if this request can be fulfilled from the cache
        BitmapDrawable bd = ...getDrawable(transformKey);
        if (bd != null) {
            setImageView(imageViewbd);
            doAnimation(imageViewnull);
            ret.setComplete(bd.getBitmap());
            return ret;
        }
        // need to either load/transform this, so set up the loading images, and mark this view
        // as in progress.
        if (imageView != null)
            ..put(imageViewtransformKey);
        setPlaceholder(imageView);
        final Handler handler = new Handler();
        // find/create the future for this download.
        String urlKey = ..getUri().toString();
        ByteArrayToBitmapFuture pendingDownload = ..get(urlKey);
        if (pendingDownload == null || pendingDownload.isCancelled()) {
            pendingDownload = new ByteArrayToBitmapFuture(handlerurlKey);
            ..put(urlKeypendingDownload);
            // allow the bitmap load to cancel the downloader
            pendingDownload.setMutateFuture(.execute(new ByteBufferListParser()));
        }
        // find/create the future for the bitmap transform
        BitmapToBitmap pendingTransform = ..get(transformKey);
        if (pendingTransform == null || pendingTransform.isCancelled()) {
            pendingTransform = new BitmapToBitmap(handlertransformKey);
            ..put(transformKeypendingTransform);
            // allow the bitmap transform to cancel the bitmap load
            pendingDownload.addChildFuture(pendingTransform.createMutateFuture());
        }
        // note whether an ImageView was present during invocation, as
        // only a weak reference is held from here on out.
        final WeakReference<ImageView> iv = new WeakReference<ImageView>(imageView);
        // get a child future that can be used to set the ImageView once the drawable is ready
        SimpleFuture<IonBitmapCache.ZombieDrawabledrawableFuture = new SimpleFuture<IonBitmapCache.ZombieDrawable>();
        drawableFuture.setCallback(new FutureCallback<IonBitmapCache.ZombieDrawable>() {
            @Override
            public void onCompleted(final Exception eIonBitmapCache.ZombieDrawable source) {
                assert Thread.currentThread() == Looper.getMainLooper().getThread();
                // see if the imageview is still alive and cares about this result
                ImageView imageView = iv.get();
                if (imageView != null) {
                    // see if the ImageView is still waiting for the same transform key as before
                    String waitingKey = ..get(imageView);
                    if (!TextUtils.equals(waitingKeytransformKey))
                        imageView = null;
                    else
                        ..remove(imageView);
                }
                if (e != null) {
                    setErrorImage(imageView);
                    ret.setComplete(e);
                    return;
                }
                IonBitmapCache.ZombieDrawable result = source.cloneAndIncrementRefCounter();
                if (imageView != null) {
                    imageView.setImageDrawable(result);
                    doAnimation(imageView);
                }
                ret.setComplete(result.getBitmap());
            }
        });
        pendingTransform.addChildFuture(drawableFuture);
        // the user returned future can cancel the future that sets the drawable
        ret.setParent(drawableFuture);
        return ret;
    }
    @Override
    public Future<Bitmap> asBitmap() {
        return intoImageView(null);
    }
    @Override
    public IonBitmapRequestBuilder placeholder(Bitmap bitmap) {
        placeholder(new BitmapDrawable(..get().getResources(), bitmap));
        return this;
    }
    Drawable placeholderDrawable;
    @Override
    public IonBitmapRequestBuilder placeholder(Drawable drawable) {
         = drawable;
        return this;
    }
    @Override
    public IonBitmapRequestBuilder placeholder(int resourceId) {
        if (resourceId == 0) {
             = null;
            return this;
        }
        placeholder(..get().getResources().getDrawable(resourceId));
        return this;
    }
    @Override
    public IonBitmapRequestBuilder error(Bitmap bitmap) {
        error(new BitmapDrawable(..get().getResources(), bitmap));
        return this;
    }
    Drawable errorDrawable;
    @Override
    public IonBitmapRequestBuilder error(Drawable drawable) {
         = drawable;
        return this;
    }
    @Override
    public IonBitmapRequestBuilder error(int resourceId) {
        if (resourceId == 0) {
             = null;
            return this;
        }
        error(..get().getResources().getDrawable(resourceId));
        return this;
    }
    private static void setImageView(ImageView imageView, Drawable drawable) {
        if (imageView == null)
            return;
        imageView.setImageDrawable(drawable);
    }
    private void setPlaceholder(ImageView imageView) {
        if (imageView == null)
            return;
        doAnimation(imageView);
        setImageView(imageView);
    }
    private void setErrorImage(ImageView imageView) {
        if (imageView == null)
            return;
        doAnimation(imageView);
        setImageView(imageView != null ?  : );
    }
    Animation inAnimation;
    @Override
    public IonBitmapRequestBuilder animateIn(Animation in) {
         = in;
        return this;
    }
    Animation loadAnimation;
    @Override
    public IonBitmapRequestBuilder animateLoad(Animation load) {
         = load;
        return this;
    }
    private void doAnimation(ImageView imageView, Animation animation) {
        if (imageView == null)
            return;
        if (animation == null) {
            imageView.setAnimation(null);
            return;
        }
        imageView.startAnimation(animation);
    }
    // adapted from Picasso
    static class DefaultTransform implements Transform {
        @Override
        public Bitmap transform(Bitmap b) {
            return null;
        }
        @Override
        public String getKey() {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X