@Override public void onCancel() { if (DEBUG) { CLog.d(LOG_TAG, MSG_TASK_CANCEL, this, mImageTask); } mImageLoader.getImageProvider().cancelTask(mImageTask); mImageTask.onLoadTaskCancel(); mImageLoader.mLoadWorkList.remove(mImageTask.getIdentityKey()); }
@Override public void onFinish(boolean canceled) { if (DEBUG) { CLog.d(LOG_TAG, MSG_TASK_FINISH, this, mImageTask, mImageLoader.mExitTasksEarly); } if (mImageLoader.mExitTasksEarly) { return; } if (!isCancelled() && !mImageLoader.mExitTasksEarly) { mImageTask.onLoadTaskFinish(mDrawable, mImageLoader.mImageLoadHandler); } mImageLoader.mLoadWorkList.remove(mImageTask.getIdentityKey()); }
/** * Add the ImageTask into loading list. * * @param imageTask * @param imageView */ public void addImageTask(ImageTask imageTask, CubeImageView imageView) { if (!mHasBeenAddedToComponentManager) { CLog.w(LOG_TAG, "ImageLoader has not been add to a Component Manager.", this); } LoadImageTask runningTask = mLoadWorkList.get(imageTask.getIdentityKey()); if (runningTask != null) { if (imageView != null) { if (DEBUG) { CLog.d(LOG_TAG, MSG_ATTACK_TO_RUNNING_TASK, imageTask, runningTask.getImageTask()); } runningTask.getImageTask().addImageView(imageView); runningTask.getImageTask().notifyLoading(mImageLoadHandler, imageView); } return; } else { imageTask.addImageView(imageView); } imageTask.onLoading(mImageLoadHandler); LoadImageTask loadImageTask = createLoadImageTask(imageTask); mLoadWorkList.put(imageTask.getIdentityKey(), loadImageTask); mImageTaskExecutor.execute(loadImageTask); }
@Override public void doInBackground() { if (DEBUG) { CLog.d(LOG_TAG, MSG_TASK_DO_IN_BACKGROUND, this, mImageTask); } if (mImageTask.getStatistics() != null) { mImageTask.getStatistics().s1_beginLoad(); } Bitmap bitmap = null; // Wait here if work is paused and the task is not cancelled synchronized (mImageLoader.mPauseWorkLock) { while (mImageLoader.mPauseWork && !isCancelled()) { try { if (DEBUG) { CLog.d(LOG_TAG, MSG_TASK_WAITING, this, mImageTask); } mImageLoader.mPauseWorkLock.wait(); } catch (InterruptedException e) { } } } // If this task has not been cancelled by another // thread and the ImageView that was originally bound to this task is still bound back // to this task and our "exit early" flag is not set then try and fetch the bitmap from // the cache if (!isCancelled() && !mImageLoader.mExitTasksEarly && (mImageTask.isPreLoad() || mImageTask.stillHasRelatedImageView())) { try { bitmap = mImageLoader.mImageProvider.fetchBitmapData( mImageLoader, mImageTask, mImageLoader.mImageReSizer); if (DEBUG) { CLog.d(LOG_TAG, MSG_TASK_AFTER_fetchBitmapData, this, mImageTask, isCancelled()); } mDrawable = mImageLoader.mImageProvider.createBitmapDrawable(mImageLoader.mResources, bitmap); mImageLoader.mImageProvider.addBitmapToMemCache(mImageTask.getIdentityKey(), mDrawable); } catch (Exception e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); } } }
/** * Detach the ImageView from the ImageTask. * * @param imageTask * @param imageView */ public void detachImageViewFromImageTask(ImageTask imageTask, CubeImageView imageView) { imageTask.removeImageView(imageView); if (imageTask.isLoading()) { if (!imageTask.isPreLoad() && !imageTask.stillHasRelatedImageView()) { LoadImageTask task = mLoadWorkList.get(imageTask.getIdentityKey()); if (task != null) { task.cancel(); } if (DEBUG) { CLog.d(LOG_TAG, "%s previous work is cancelled.", imageTask); } } } if (!imageTask.stillHasRelatedImageView()) { imageTask.tryToRecycle(); } }