private ImageDownloader getDownloader() {
   ImageDownloader d;
   if (engine.isNetworkDenied()) {
     d = networkDeniedDownloader;
   } else if (engine.isSlowNetwork()) {
     d = slowNetworkDownloader;
   } else {
     d = downloader;
   }
   return d;
 }
 /** @return <b>true</b> - if task should be interrupted; <b>false</b> - otherwise */
 private boolean waitIfPaused() {
   AtomicBoolean pause = engine.getPause();
   if (pause.get()) {
     synchronized (engine.getPauseLock()) {
       if (pause.get()) {
         log(LOG_WAITING_FOR_RESUME);
         try {
           engine.getPauseLock().wait();
         } catch (InterruptedException e) {
           L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
           return true;
         }
         log(LOG_RESUME_AFTER_PAUSE);
       }
     }
   }
   return isTaskNotActual();
 }
 /**
  * @return <b>true</b> - if current ImageAware is reused for displaying another image;
  *     <b>false</b> - otherwise
  */
 private boolean isViewReused() {
   String currentCacheKey = engine.getLoadingUriForView(imageAware);
   // Check whether memory cache key (image URI) for current ImageAware is actual.
   // If ImageAware is reused for another task then current task should be cancelled.
   boolean imageAwareWasReused = !memoryCacheKey.equals(currentCacheKey);
   if (imageAwareWasReused) {
     log(LOG_TASK_CANCELLED_IMAGEAWARE_REUSED);
     return true;
   }
   return false;
 }
  /**
   * Check whether the image URI of this task matches to image URI which is actual for current
   * ImageView at this moment and fire {@link ImageLoadingListener#onLoadingCancelled()} event if it
   * doesn't.
   */
  private boolean checkTaskIsNotActual() {
    String currentCacheKey = engine.getLoadingUriForView(imageView);
    // Check whether memory cache key (image URI) for current ImageView is actual.
    // If ImageView is reused for another task then current task should be cancelled.
    boolean imageViewWasReused = !memoryCacheKey.equals(currentCacheKey);
    if (imageViewWasReused) {
      handler.post(
          new Runnable() {
            @Override
            public void run() {
              listener.onLoadingCancelled();
            }
          });
    }

    if (imageViewWasReused) log(LOG_TASK_CANCELLED, memoryCacheKey);
    return imageViewWasReused;
  }
Esempio n. 5
0
 /**
  * Pause ImageLoader. All new "load&display" tasks won't be executed until ImageLoader is {@link
  * #resume() resumed}.<br>
  * Already running tasks are not paused.
  */
 public void pause() {
   engine.pause();
 }
Esempio n. 6
0
 /**
  * Sets option whether ImageLoader will use {@link FlushedInputStream} for network downloads to
  * handle <a href="http://code.google.com/p/android/issues/detail?id=6066">this known problem</a>
  * or not.
  *
  * @param handleSlowNetwork pass <b>true</b> - to use {@link FlushedInputStream} for network
  *     downloads; <b>false</b> - otherwise.
  */
 public void handleSlowNetwork(boolean handleSlowNetwork) {
   engine.handleSlowNetwork(handleSlowNetwork);
 }
Esempio n. 7
0
 /**
  * Denies or allows ImageLoader to download images from the network.<br>
  * <br>
  * If downloads are denied and if image isn't cached then {@link
  * ImageLoadingListener#onLoadingFailed(String, View, FailReason)} callback will be fired with
  * {@link FailReason.FailType#NETWORK_DENIED}
  *
  * @param denyNetworkDownloads pass <b>true</b> - to deny engine to download images from the
  *     network; <b>false</b> - to allow engine to download images from network.
  */
 public void denyNetworkDownloads(boolean denyNetworkDownloads) {
   engine.denyNetworkDownloads(denyNetworkDownloads);
 }
Esempio n. 8
0
 /**
  * Cancel the task of loading and displaying image for passed {@link ImageView}.
  *
  * @param imageView {@link ImageView} for which display task will be cancelled
  */
 public void cancelDisplayTask(ImageView imageView) {
   engine.cancelDisplayTaskFor(imageView);
 }
Esempio n. 9
0
 /** Returns URI of image which is loading at this moment into passed {@link ImageView} */
 public String getLoadingUriForView(ImageView imageView) {
   return engine.getLoadingUriForView(imageView);
 }
Esempio n. 10
0
  /**
   * Adds display image task to execution pool. Image will be set to ImageView when it's turn.<br>
   * <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method
   * call
   *
   * @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
   * @param imageView {@link ImageView} which should display image
   * @param options {@linkplain DisplayImageOptions Display image options} for image displaying. If
   *     <b>null</b> - default display image options {@linkplain
   *     ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
   *     configuration} will be used.
   * @param listener {@linkplain ImageLoadingListener Listener} for image loading process. Listener
   *     fires events on UI thread.
   * @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called
   *     before
   * @throws IllegalArgumentException if passed <b>imageView</b> is null
   */
  public void displayImage(
      String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener) {
    checkConfiguration();

    if (imageView == null) {
      throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);
    }

    if (listener == null) {
      listener = emptyListener;
    }

    if (options == null) {
      options = configuration.defaultDisplayImageOptions;
    }

    if (TextUtils.isEmpty(uri)) {
      engine.cancelDisplayTaskFor(imageView);
      listener.onLoadingStarted(uri, imageView);

      if (options.shouldShowImageResForEmptyUri()) {
        imageView.setImageResource(options.getImageResForEmptyUri());

      } else if (options.shouldShowImageForEmptyUri()) {
        imageView.setImageDrawable(options.getImageForEmptyUri());

      } else {
        imageView.setImageDrawable(null);
      }

      listener.onLoadingComplete(uri, imageView, null);
      return;
    }

    ImageSize targetSize =
        ImageSizeUtils.defineTargetSizeForView(
            imageView,
            configuration.maxImageWidthForMemoryCache,
            configuration.maxImageHeightForMemoryCache);
    String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
    engine.prepareDisplayTaskFor(imageView, memoryCacheKey);

    listener.onLoadingStarted(uri, imageView);
    Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);

    if (bmp != null && !bmp.isRecycled()) {
      if (configuration.writeLogs) {
        L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);
      }

      if (options.shouldPostProcess()) {
        ImageLoadingInfo imageLoadingInfo =
            new ImageLoadingInfo(
                uri,
                imageView,
                targetSize,
                memoryCacheKey,
                options,
                listener,
                engine.getLockForUri(uri));
        ProcessAndDisplayImageTask displayTask =
            new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, options.getHandler());
        engine.submit(displayTask);

      } else {
        options.getDisplayer().display(bmp, imageView, LoadedFrom.MEMORY_CACHE);
        listener.onLoadingComplete(uri, imageView, bmp);
      }

    } else {
      if (options.shouldShowImageResOnLoading()) {
        imageView.setImageResource(options.getImageResOnLoading());

      } else if (options.shouldShowImageOnLoading()) {
        imageView.setImageDrawable(options.getImageOnLoading());

      } else {
        if (options.isResetViewBeforeLoading()) {
          imageView.setImageDrawable(null);
        }
      }

      ImageLoadingInfo imageLoadingInfo =
          new ImageLoadingInfo(
              uri,
              imageView,
              targetSize,
              memoryCacheKey,
              options,
              listener,
              engine.getLockForUri(uri));
      LoadAndDisplayImageTask displayTask =
          new LoadAndDisplayImageTask(engine, imageLoadingInfo, options.getHandler());
      engine.submit(displayTask);
    }
  }
 /**
  * Cancel the task of loading and displaying image for passed {@link android.widget.ImageView
  * ImageView}.
  *
  * @param imageView {@link android.widget.ImageView ImageView} for which display task will be
  *     cancelled
  */
 public void cancelDisplayTask(ImageView imageView) {
   engine.cancelDisplayTaskFor(new ImageViewAware(imageView));
 }
 /**
  * Cancel the task of loading and displaying image for passed {@link
  * com.nostra13.universalimageloader.core.imageaware.ImageAware ImageAware}.
  *
  * @param imageAware {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  *     ImageAware} for which display task will be cancelled
  */
 public void cancelDisplayTask(ImageAware imageAware) {
   engine.cancelDisplayTaskFor(imageAware);
 }
 /**
  * Returns URI of image which is loading at this moment into passed {@link
  * android.widget.ImageView ImageView}
  */
 public String getLoadingUriForView(ImageView imageView) {
   return engine.getLoadingUriForView(new ImageViewAware(imageView));
 }
 /**
  * Returns URI of image which is loading at this moment into passed {@link
  * com.nostra13.universalimageloader.core.imageaware.ImageAware ImageAware}
  */
 public String getLoadingUriForView(ImageAware imageAware) {
   return engine.getLoadingUriForView(imageAware);
 }
Esempio n. 15
0
 /** Resumes waiting "load&display" tasks */
 public void resume() {
   engine.resume();
 }
Esempio n. 16
0
 /**
  * Cancels all running and scheduled display image tasks.<br>
  * ImageLoader still can be used after calling this method.
  */
 public void stop() {
   engine.stop();
 }
  @Override
  public void run() {
    AtomicBoolean pause = engine.getPause();
    if (pause.get()) {
      synchronized (pause) {
        log(LOG_WAITING_FOR_RESUME, memoryCacheKey);
        try {
          pause.wait();
        } catch (InterruptedException e) {
          L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
          return;
        }
        log(LOG_RESUME_AFTER_PAUSE, memoryCacheKey);
      }
    }
    if (checkTaskIsNotActual()) return;

    if (options.shouldDelayBeforeLoading()) {
      log(LOG_DELAY_BEFORE_LOADING, options.getDelayBeforeLoading(), memoryCacheKey);
      try {
        Thread.sleep(options.getDelayBeforeLoading());
      } catch (InterruptedException e) {
        L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
        return;
      }

      if (checkTaskIsNotActual()) return;
    }

    ReentrantLock loadFromUriLock = imageLoadingInfo.loadFromUriLock;
    log(LOG_START_DISPLAY_IMAGE_TASK, memoryCacheKey);
    if (loadFromUriLock.isLocked()) {
      log(LOG_WAITING_FOR_IMAGE_LOADED, memoryCacheKey);
    }

    loadFromUriLock.lock();
    Bitmap bmp;
    try {
      if (checkTaskIsNotActual()) return;

      bmp = configuration.memoryCache.get(memoryCacheKey);
      if (bmp == null) {
        bmp = tryLoadBitmap();
        if (bmp == null) return;

        if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;

        if (options.isCacheInMemory()) {
          if (options.shouldPreProcess()) {
            log(LOG_PREPROCESS_IMAGE, memoryCacheKey);
            bmp = options.getPreProcessor().process(bmp, imageView);
          }

          log(LOG_CACHE_IMAGE_IN_MEMORY, memoryCacheKey);
          configuration.memoryCache.put(memoryCacheKey, bmp);
        }
      } else {
        log(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING, memoryCacheKey);
      }

      if (options.shouldPostProcess()) {
        log(LOG_POSTPROCESS_IMAGE, memoryCacheKey);
        bmp = options.getPostProcessor().process(bmp, imageView);
      }
    } finally {
      loadFromUriLock.unlock();
    }

    if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;

    DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine);
    displayBitmapTask.setLoggingEnabled(loggingEnabled);
    handler.post(displayBitmapTask);
  }