Esempio n. 1
0
  /** Check weather this imageTask has cache Drawable data. */
  public boolean queryCache(ImageTask imageTask, CubeImageView imageView) {
    if (null == mImageProvider) {
      return false;
    }
    BitmapDrawable drawable = mImageProvider.getBitmapFromMemCache(imageTask);

    if (imageTask.getStatistics() != null) {
      imageTask.getStatistics().s0_afterCheckMemoryCache(drawable != null);
    }
    if (drawable == null) {
      return false;
    }

    if (DEBUG) {
      CLog.d(
          LOG_TAG,
          MSG_HIT_CACHE,
          imageTask,
          drawable.getIntrinsicWidth(),
          drawable.getIntrinsicHeight());
    }
    imageTask.addImageView(imageView);
    imageTask.onLoadTaskFinish(drawable, mImageLoadHandler);
    return true;
  }
Esempio n. 2
0
 @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());
 }
Esempio n. 3
0
  public ImageTask createImageTask(ImageLoadRequest request) {
    ImageTask imageTask = ImageTask.obtain();
    if (imageTask == null) {
      imageTask = new ImageTask();
    }

    imageTask.renewForRequest(request);
    return imageTask;
  }
Esempio n. 4
0
 /** Load the image in advance. */
 public void preLoadImages(String[] urls) {
   int len = urls.length;
   for (int i = 0; i < len; i++) {
     ImageLoadRequest request = new ImageLoadRequest(urls[i]);
     final ImageTask imageTask = createImageTask(request);
     imageTask.setIsPreLoad();
     addImageTask(imageTask, null);
   }
 }
Esempio n. 5
0
 /**
  * Create an ImageTask. You can override this method to return a customized {@link ImageTask}.
  *
  * @param url
  * @param requestWidth
  * @param requestHeight
  * @param imageReuseInfo
  * @return
  */
 @Deprecated
 public ImageTask createImageTask(
     String url, int requestWidth, int requestHeight, ImageReuseInfo imageReuseInfo) {
   ImageTask imageTask = ImageTask.obtain();
   if (imageTask == null) {
     imageTask = new ImageTask();
   }
   ImageLoadRequest imageLoadRequest =
       new ImageLoadRequest(url, requestWidth, requestHeight, -1, imageReuseInfo);
   imageTask.renewForRequest(imageLoadRequest);
   return imageTask;
 }
Esempio n. 6
0
    @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());
    }
Esempio n. 7
0
    @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();
        }
      }
    }
Esempio n. 8
0
  private void tryLoadImage() {

    if (TextUtils.isEmpty(mUrl)) {
      return;
    }

    int width = getWidth();
    int height = getHeight();

    ViewGroup.LayoutParams lyp = getLayoutParams();
    boolean isFullyWrapContent =
        lyp != null
            && lyp.height == LayoutParams.WRAP_CONTENT
            && lyp.width == LayoutParams.WRAP_CONTENT;
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    if (width == 0 && height == 0 && !isFullyWrapContent) {
      return;
    }

    if (mSpecifiedWidth != 0) {
      width = mSpecifiedWidth;
    }

    if (mSpecifiedHeight != 0) {
      height = mSpecifiedHeight;
    }

    // 1. Check the previous ImageTask related to this ImageView
    if (null != mImageTask) {

      // duplicated ImageTask, return directly.
      if (mImageTask.isLoadingThisUrl(mUrl)) {
        return;
      }
      // ImageView is reused, detach it from the related ImageViews of the previous ImageTask.
      else {
        mImageLoader.detachImageViewFromImageTask(mImageTask, this);
      }
    }

    // 2. Let the ImageView hold this ImageTask. When ImageView is reused next time, check it in
    // step 1.
    ImageTask imageTask = mImageLoader.createImageTask(mUrl, width, height, mImageReuseInfo);
    mImageTask = imageTask;

    // 3. Query cache, if hit, return at once.
    boolean hitCache = mImageLoader.queryCache(imageTask, this);
    if (hitCache) {
      return;
    } else {
      mImageLoader.addImageTask(mImageTask, this);
    }
  }
Esempio n. 9
0
  /**
   * 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);
  }
Esempio n. 10
0
 /**
  * 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();
   }
 }
Esempio n. 11
0
 public void onPause() {
   super.onPause();
   if (task != null) task.cancel();
 }
Esempio n. 12
0
  private void setTask() {
    if (task != null) task.cancel();

    task = new ImageTask();
    timer.schedule(task, 5000, 5000);
  }