Android Open Source - Aviary-Android-SDK Async Image Manager






From Project

Back to project page Aviary-Android-SDK.

License

The source code is released under:

AVIARY API TERMS OF USE Full Legal Agreement The following terms and conditions and the terms and conditions at http://www.aviary.com/terms (collectively, the ?Terms??) govern your use of any and ...

If you think the Android project Aviary-Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.aviary.android.feather.async_tasks;
/*from w w  w  . ja  v a2  s .  c  o m*/
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.aviary.android.feather.R;
import com.aviary.android.feather.common.log.LoggerFactory;
import com.aviary.android.feather.common.log.LoggerFactory.Logger;
import com.aviary.android.feather.common.log.LoggerFactory.LoggerType;
import com.aviary.android.feather.common.threading.PriorityThreadFactory;
import com.aviary.android.feather.utils.SimpleBitmapCache;

/**
 * Load an internal asset asynchronous.
 * 
 * @author alessandro
 */
public class AsyncImageManager {

  public static enum Priority {
    HIGH, LOW
  };

  private static final int THUMBNAIL_LOADED = 1;

  abstract static class MyRunnable implements Runnable {

    public SoftReference<ImageView> mView;

    public MyRunnable ( ImageView image ) {
      mView = new SoftReference<ImageView>( image );
    }
  }

  public static interface OnImageLoadListener {
    public void onLoadComplete( ImageView view, Bitmap bitmap, int tag );
  }

  ExecutorService mExecutor1;
  ExecutorService mExecutor2;

  private volatile Boolean mStopped = false;

  private SimpleBitmapCache mBitmapCache;
  private OnImageLoadListener mListener;
  private Handler mHandler;

  private static Logger logger = LoggerFactory.getLogger( "AsyncImageManager", LoggerType.ConsoleLoggerType );

  public AsyncImageManager ( boolean useCache ) {

    mExecutor1 = Executors.newCachedThreadPool( new PriorityThreadFactory( "async-image-high", android.os.Process.THREAD_PRIORITY_BACKGROUND ) );
    mExecutor2 = Executors.newFixedThreadPool( 1, new PriorityThreadFactory( "async-image-low", android.os.Process.THREAD_PRIORITY_LOWEST ) );

    if ( useCache ) {
      mBitmapCache = new SimpleBitmapCache();
    } else {
      mBitmapCache = null;
    }

    mHandler = new MyHandler( this );
    mListener = null;
  }

  public void setOnLoadCompleteListener( OnImageLoadListener listener ) {
    mListener = listener;
  }

  private static class MyHandler extends Handler {

    WeakReference<AsyncImageManager> mParent;

    public MyHandler ( AsyncImageManager parent ) {
      mParent = new WeakReference<AsyncImageManager>( parent );
    }

    @Override
    public void handleMessage( Message msg ) {

      switch ( msg.what ) {
        case AsyncImageManager.THUMBNAIL_LOADED:

          Thumb thumb = (Thumb) msg.obj;

          AsyncImageManager parent = mParent.get();

          ImageView view = thumb.image;
          Bitmap bitmap = thumb.bitmap;

          if ( null != parent ) {
            if ( parent.mListener != null ) {
              if ( null != view ) {
                parent.mListener.onLoadComplete( view, bitmap, thumb.tag );
              }
              return;
            }
          }

          if ( view != null && bitmap != null ) {
            view.setImageBitmap( bitmap );
          }

          break;
      }
    }
  }

  public void shutDownNow() {
    mStopped = true;

    mExecutor1.shutdownNow();
    mExecutor2.shutdownNow();

    mHandler = null;
    clearCache();
  }

  public void executeDelayed( final Callable<Bitmap> executor, final String hash, final ImageView view, final int tag, final Priority priority, long delayMillis ) {
    if ( null != mHandler ) {
      mHandler.postDelayed( new Runnable() {

        @Override
        public void run() {
          if ( !mStopped ) {
            execute( executor, hash, view, tag, priority );
          }
        }
      }, delayMillis );
    }
  }

  /**
   * Retrive the bitmap either using the internal cache or executing the passed
   * {@link Callable} instance.
   * 
   * @param executor
   *            the executor
   * @param hash
   *            the unique hash used to store/retrieve the bitmap from the cache
   * @param view
   *            the final {@link ImageView} where the bitmap will be shown
   * @param tag
   *            a custom tag
   * @param priority
   *            the process priority
   */
  public void execute( final Callable<Bitmap> executor, final String hash, final ImageView view, final int tag, final Priority priority ) {

    if ( mStopped ) {
      return;
    }

    if ( null != mBitmapCache ) {
      mBitmapCache.resetPurgeTimer();
    }

    MyRunnable task = new MyRunnable( view ) {

      @Override
      public void run() {

        if ( mStopped ) {
          return;
        }

        MyRunnable bitmapTask = getBitmapTask( mView.get() );
        if ( !this.equals( bitmapTask ) ) {
          return;
        }

        Message message = Message.obtain();
        Bitmap bitmap = null;

        if ( null != mBitmapCache ) bitmap = mBitmapCache.getBitmapFromCache( hash );

        if ( bitmap != null ) {
          message.what = THUMBNAIL_LOADED;
          message.obj = new Thumb( bitmap, mView.get(), tag );
        } else {
          try {
            bitmap = executor.call();
          } catch ( Exception e ) {
            e.printStackTrace();
            return;
          }

          if ( bitmap != null ) {
            if ( null != mBitmapCache ) mBitmapCache.addBitmapToCache( hash, bitmap );
          }

          ImageView imageView = mView.get();

          if ( imageView != null ) {
            if ( this.equals( bitmapTask ) ) {
              imageView.setTag( R.integer.aviary_asyncimagemanager_tag, null );
              message.what = THUMBNAIL_LOADED;
              message.obj = new Thumb( bitmap, imageView, tag );
            } else {
              logger.warn( "image tag is different than current task!" );
            }
          } else {
            logger.warn( "imageView null" );
          }
        }

        if ( message.what == THUMBNAIL_LOADED && null != mHandler ) {
          mHandler.sendMessage( message );
        }
      }
    };

    view.setTag( R.integer.aviary_asyncimagemanager_tag, new CustomTag( task ) );

    if ( priority == Priority.HIGH ) {
      mExecutor1.execute( task );
    } else {
      mExecutor2.execute( task );
    }

  }

  static class CustomTag {

    private final WeakReference<MyRunnable> taskReference;

    public CustomTag ( MyRunnable task ) {
      super();
      taskReference = new WeakReference<MyRunnable>( task );
    }

    public MyRunnable getDownloaderTask() {
      return taskReference.get();
    }
  }

  private static MyRunnable getBitmapTask( ImageView imageView ) {
    if ( imageView != null ) {
      Object tag = imageView.getTag( R.integer.aviary_asyncimagemanager_tag );
      if ( tag instanceof CustomTag ) {
        CustomTag runnableTag = (CustomTag) tag;
        return runnableTag.getDownloaderTask();
      }
    }
    return null;
  }

  /**
   * Clears the image cache used internally to improve performance. Note that for memory
   * efficiency reasons, the cache will
   * automatically be cleared after a certain inactivity delay.
   */
  public void clearCache() {
    if ( null != mBitmapCache ) mBitmapCache.clearCache();
  }

  /**
   * The Class Thumb.
   */
  static class Thumb {

    public Bitmap bitmap;
    public ImageView image;
    public final int tag;

    public Thumb ( Bitmap bmp, ImageView img ) {
      this( bmp, img, -1 );
    }

    public Thumb ( Bitmap bmp, ImageView img, int ntag ) {
      image = img;
      bitmap = bmp;
      tag = ntag;
    }
  }
}




Java Source Code List

com.aviary.android.feather.AlertActivity.java
com.aviary.android.feather.AviaryMainController.java
com.aviary.android.feather.FeatherActivity.java
com.aviary.android.feather.async_tasks.AsyncImageManager.java
com.aviary.android.feather.async_tasks.DownloadImageAsyncTask.java
com.aviary.android.feather.async_tasks.ExifTask.java
com.aviary.android.feather.effects.AbstractContentPanel.java
com.aviary.android.feather.effects.AbstractOptionPanel.java
com.aviary.android.feather.effects.AbstractPanelLoaderService.java
com.aviary.android.feather.effects.AbstractPanel.java
com.aviary.android.feather.effects.AdjustEffectPanel.java
com.aviary.android.feather.effects.BordersPanel.java
com.aviary.android.feather.effects.ColorSplashPanel.java
com.aviary.android.feather.effects.CropPanel.java
com.aviary.android.feather.effects.DelayedSpotDrawPanel.java
com.aviary.android.feather.effects.DrawingPanel.java
com.aviary.android.feather.effects.EffectsPanel.java
com.aviary.android.feather.effects.EnhanceEffectPanel.java
com.aviary.android.feather.effects.MemePanel.java
com.aviary.android.feather.effects.NativeEffectRangePanel.java
com.aviary.android.feather.effects.SimpleStatusMachine.java
com.aviary.android.feather.effects.SliderEffectPanel.java
com.aviary.android.feather.effects.StickersPanel.java
com.aviary.android.feather.effects.TextPanel.java
com.aviary.android.feather.effects.TiltShiftPanel.java
com.aviary.android.feather.graphics.CdsPreviewTransformer.java
com.aviary.android.feather.graphics.GalleryBottomIndicatorDrawable.java
com.aviary.android.feather.graphics.GalleryTopIndicatorDrawable.java
com.aviary.android.feather.graphics.GlowBitmapDrawable.java
com.aviary.android.feather.graphics.GlowDrawable.java
com.aviary.android.feather.graphics.PluginDividerDrawable.java
com.aviary.android.feather.graphics.PreviewFillColorDrawable.java
com.aviary.android.feather.graphics.PreviewSpotDrawable.java
com.aviary.android.feather.graphics.RepeatableHorizontalDrawable.java
com.aviary.android.feather.opengl.AviaryGLSurfaceView.java
com.aviary.android.feather.utils.PackIconCallable.java
com.aviary.android.feather.utils.SimpleBitmapCache.java
com.aviary.android.feather.utils.ThreadUtils.java
com.aviary.android.feather.utils.TypefaceUtils.java
com.aviary.android.feather.utils.UIUtils.java
com.aviary.android.feather.widget.AdjustImageView.java
com.aviary.android.feather.widget.AviaryAbsSpinner.java
com.aviary.android.feather.widget.AviaryAdapterView.java
com.aviary.android.feather.widget.AviaryBadgeToolLayout.java
com.aviary.android.feather.widget.AviaryBottomBarViewFlipper.java
com.aviary.android.feather.widget.AviaryButton.java
com.aviary.android.feather.widget.AviaryEdgeEffect.java
com.aviary.android.feather.widget.AviaryGalleryTopIndicatorView.java
com.aviary.android.feather.widget.AviaryGallery.java
com.aviary.android.feather.widget.AviaryHighlightImageButton.java
com.aviary.android.feather.widget.AviaryImageRestoreSwitcher.java
com.aviary.android.feather.widget.AviaryImageSwitcher.java
com.aviary.android.feather.widget.AviaryNavBarViewFlipper.java
com.aviary.android.feather.widget.AviarySeekBar.java
com.aviary.android.feather.widget.AviaryTextView.java
com.aviary.android.feather.widget.AviaryToast.java
com.aviary.android.feather.widget.AviaryToggleButton.java
com.aviary.android.feather.widget.AviaryWheel.java
com.aviary.android.feather.widget.AviaryWorkspaceIndicator.java
com.aviary.android.feather.widget.AviaryWorkspace.java
com.aviary.android.feather.widget.CellLayout.java
com.aviary.android.feather.widget.CropImageView.java
com.aviary.android.feather.widget.DrawableHighlightView.java
com.aviary.android.feather.widget.EffectThumbLayout.java
com.aviary.android.feather.widget.HighlightView.java
com.aviary.android.feather.widget.IAPBuyButton.java
com.aviary.android.feather.widget.IAPDialogDetail.java
com.aviary.android.feather.widget.IAPDialogList.java
com.aviary.android.feather.widget.IAPDialogMain.java
com.aviary.android.feather.widget.ImageViewDrawableOverlay.java
com.aviary.android.feather.widget.ImageViewSpotDraw.java
com.aviary.android.feather.widget.ImageViewTiltiShiftTouch.java
com.aviary.android.feather.widget.ImageViewTouchAndDraw.java
com.aviary.android.feather.widget.PointCloud.java
com.aviary.android.feather.widget.ScrollerRunnable.java
com.aviary.android.feather.widget.VibrationHelper.java
com.aviary.android.feather.widget.VibrationWidget.java