예제 #1
0
 /** @return <b>true</b> - if task should be interrupted; <b>false</b> - otherwise */
 private boolean delayIfNeed() {
   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 true;
     }
     return isTaskNotActual();
   }
   return false;
 }
예제 #2
0
 private void fireCancelEvent() {
   if (options.isSyncLoading() || isTaskInterrupted()) return;
   Runnable r =
       new Runnable() {
         @Override
         public void run() {
           listener.onLoadingCancelled(uri, imageAware.getWrappedView());
         }
       };
   runTask(r, false, handler);
 }
예제 #3
0
 /**
  * @return <b>true</b> - if loading should be continued; <b>false</b> - if loading should be
  *     interrupted
  */
 private boolean fireProgressEvent(final int current, final int total) {
   if (options.isSyncLoading() || isTaskInterrupted() || isTaskNotActual()) return false;
   Runnable r =
       new Runnable() {
         @Override
         public void run() {
           progressListener.onProgressUpdate(uri, imageAware.getWrappedView(), current, total);
         }
       };
   runTask(r, false, handler);
   return true;
 }
예제 #4
0
 private void fireFailEvent(final FailType failType, final Throwable failCause) {
   if (options.isSyncLoading() || isTaskInterrupted() || isTaskNotActual()) return;
   Runnable r =
       new Runnable() {
         @Override
         public void run() {
           if (options.shouldShowImageOnFail()) {
             imageAware.setImageDrawable(options.getImageOnFail(configuration.resources));
           }
           listener.onLoadingFailed(
               uri, imageAware.getWrappedView(), new FailReason(failType, failCause));
         }
       };
   runTask(r, false, handler);
 }
예제 #5
0
 private boolean downloadImage(File targetFile) throws IOException {
   InputStream is = getDownloader().getStream(uri, options.getExtraForDownloader());
   boolean loaded;
   try {
     OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile), BUFFER_SIZE);
     try {
       loaded = IoUtils.copyStream(is, os, this);
     } finally {
       IoUtils.closeSilently(os);
     }
   } finally {
     IoUtils.closeSilently(is);
   }
   return loaded;
 }
예제 #6
0
  private Bitmap tryLoadBitmap() throws TaskCancelledException {
    File imageFile = getImageFileInDiscCache();

    Bitmap bitmap = null;
    try {
      String cacheFileUri = Scheme.FILE.wrap(imageFile.getAbsolutePath());
      if (imageFile.exists()) {
        log(LOG_LOAD_IMAGE_FROM_DISC_CACHE);
        loadedFrom = LoadedFrom.DISC_CACHE;

        checkTaskNotActual();
        bitmap = decodeImage(cacheFileUri);
      }
      if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
        log(LOG_LOAD_IMAGE_FROM_NETWORK);
        loadedFrom = LoadedFrom.NETWORK;

        String imageUriForDecoding =
            options.isCacheOnDisc() && tryCacheImageOnDisc(imageFile) ? cacheFileUri : uri;

        checkTaskNotActual();
        bitmap = decodeImage(imageUriForDecoding);

        if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
          fireFailEvent(FailType.DECODING_ERROR, null);
        }
      }
    } catch (IllegalStateException e) {
      fireFailEvent(FailType.NETWORK_DENIED, null);
    } catch (TaskCancelledException e) {
      throw e;
    } catch (IOException e) {
      L.e(e);
      fireFailEvent(FailType.IO_ERROR, e);
      if (imageFile.exists()) {
        imageFile.delete();
      }
    } catch (OutOfMemoryError e) {
      L.e(e);
      fireFailEvent(FailType.OUT_OF_MEMORY, e);
    } catch (Throwable e) {
      L.e(e);
      fireFailEvent(FailType.UNKNOWN, e);
    }
    return bitmap;
  }
 private void initEmptyFieldsWithDefaultValues() {
   if (taskExecutor == null) {
     taskExecutor =
         DefaultConfigurationFactory.createExecutor(
             threadPoolSize, threadPriority, tasksProcessingType);
   } else {
     customExecutor = true;
   }
   if (taskExecutorForCachedImages == null) {
     taskExecutorForCachedImages =
         DefaultConfigurationFactory.createExecutor(
             threadPoolSize, threadPriority, tasksProcessingType);
   } else {
     customExecutorForCachedImages = true;
   }
   if (diskCache == null) {
     if (diskCacheFileNameGenerator == null) {
       diskCacheFileNameGenerator = DefaultConfigurationFactory.createFileNameGenerator();
     }
     diskCache =
         DefaultConfigurationFactory.createDiskCache(
             context, diskCacheFileNameGenerator, diskCacheSize, diskCacheFileCount);
   }
   if (memoryCache == null) {
     memoryCache = DefaultConfigurationFactory.createMemoryCache(memoryCacheSize);
   }
   if (denyCacheImageMultipleSizesInMemory) {
     memoryCache =
         new FuzzyKeyMemoryCache(memoryCache, MemoryCacheUtils.createFuzzyKeyComparator());
   }
   if (downloader == null) {
     downloader = DefaultConfigurationFactory.createImageDownloader(context);
   }
   if (decoder == null) {
     decoder = DefaultConfigurationFactory.createImageDecoder(writeLogs);
   }
   if (defaultDisplayImageOptions == null) {
     defaultDisplayImageOptions = DisplayImageOptions.createSimple();
   }
 }
예제 #8
0
  @Override
  public void run() {
    if (waitIfPaused()) return;
    if (delayIfNeed()) return;

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

    loadFromUriLock.lock();
    Bitmap bmp;
    try {
      checkTaskNotActual();

      bmp = configuration.memoryCache.get(memoryCacheKey);
      if (bmp == null) {
        bmp = tryLoadBitmap();
        if (bmp == null) return; // listener callback already was fired

        checkTaskNotActual();
        checkTaskInterrupted();

        if (options.shouldPreProcess()) {
          log(LOG_PREPROCESS_IMAGE);
          bmp = options.getPreProcessor().process(bmp);
          if (bmp == null) {
            L.e(ERROR_PRE_PROCESSOR_NULL, memoryCacheKey);
          }
        }

        if (bmp != null && options.isCacheInMemory()) {
          log(LOG_CACHE_IMAGE_IN_MEMORY);
          configuration.memoryCache.put(memoryCacheKey, bmp);
        }
      } else {
        loadedFrom = LoadedFrom.MEMORY_CACHE;
        log(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING);
      }

      if (bmp != null && options.shouldPostProcess()) {
        log(LOG_POSTPROCESS_IMAGE);
        bmp = options.getPostProcessor().process(bmp);
        if (bmp == null) {
          L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);
        }
      }
      checkTaskNotActual();
      checkTaskInterrupted();
    } catch (TaskCancelledException e) {
      fireCancelEvent();
      return;
    } finally {
      loadFromUriLock.unlock();
    }

    DisplayBitmapTask displayBitmapTask =
        new DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);
    displayBitmapTask.setLoggingEnabled(writeLogs);
    runTask(displayBitmapTask, options.isSyncLoading(), handler);
  }