Exemplo n.º 1
0
 protected void onGetImageError(String cacheKey, VolleyError error) {
   BatchedImageRequest request = (BatchedImageRequest) this.mInFlightRequests.remove(cacheKey);
   if (request != null) {
     request.setError(error);
     batchResponse(cacheKey, request);
   }
 }
Exemplo n.º 2
0
 protected void onGetImageSuccess(String cacheKey, Bitmap response) {
   this.mCache.putBitmap(cacheKey, response);
   BatchedImageRequest request = (BatchedImageRequest) this.mInFlightRequests.remove(cacheKey);
   if (request != null) {
     request.mResponseBitmap = response;
     batchResponse(cacheKey, request);
   }
 }
Exemplo n.º 3
0
  /**
   * Issues a bitmap request with the given URL if that image is not available in the cache, and
   * returns a bitmap container that contains all of the data relating to the request (as well as
   * the default image if the requested image is not available).
   *
   * @param requestUrl The url of the remote image
   * @param imageListener The listener to call when the remote image is loaded
   * @param maxWidth The maximum width of the returned image.
   * @param maxHeight The maximum height of the returned image.
   * @return A container object that contains all of the properties of the request, as well as the
   *     currently available image (default if remote is not loaded).
   */
  public ImageContainer get(
      String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
      // Return the cached bitmap.
      ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
      imageListener.onResponse(container, true);
      return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
      // If it is, add this request to the list of listeners.
      request.addContainer(imageContainer);
      return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(
            requestUrl,
            new Listener<Bitmap>() {
              @Override
              public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
              }
            },
            maxWidth,
            maxHeight,
            Config.RGB_565,
            new ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
              }
            },
            mExpiresTime);

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
  }
Exemplo n.º 4
0
  /**
   * Handler for when an image failed to load.
   *
   * @param cacheKey The cache key that is associated with the image request.
   */
  protected void onGetImageError(String cacheKey, VolleyError error) {
    // Notify the requesters that something failed via a null result.
    // Remove this request from the list of in-flight requests.
    BatchedImageRequest request = mInFlightRequests.remove(cacheKey);

    if (request != null) {
      // Set the error for this request
      request.setError(error);

      // Send the batched response
      batchResponse(cacheKey, request);
    }
  }
Exemplo n.º 5
0
  /**
   * Handler for when an image was successfully loaded.
   *
   * @param cacheKey The cache key that is associated with the image request.
   * @param response The bitmap that was returned from the network.
   */
  protected void onGetImageSuccess(String cacheKey, Bitmap response) {
    // cache the image that was fetched.
    mCache.putBitmap(cacheKey, response);

    // remove the request from the list of in-flight requests.
    BatchedImageRequest request = mInFlightRequests.remove(cacheKey);

    if (request != null) {
      // Update the response bitmap.
      request.mResponseBitmap = response;

      // Send the batched response
      batchResponse(cacheKey, request);
    }
  }
Exemplo n.º 6
0
 public void cancelRequest() {
   if (this.mListener != null) {
     BatchedImageRequest request =
         (BatchedImageRequest) ImageLoader.this.mInFlightRequests.get(this.mCacheKey);
     if (request == null) {
       request = (BatchedImageRequest) ImageLoader.this.mBatchedResponses.get(this.mCacheKey);
       if (request != null) {
         request.removeContainerAndCancelIfNecessary(this);
         if (request.mContainers.size() == 0) {
           ImageLoader.this.mBatchedResponses.remove(this.mCacheKey);
         }
       }
     } else if (request.removeContainerAndCancelIfNecessary(this)) {
       ImageLoader.this.mInFlightRequests.remove(this.mCacheKey);
     }
   }
 }
Exemplo n.º 7
0
  /**
   * Issues a bitmap request with the given URL if that image is not available in the cache, and
   * returns a bitmap container that contains all of the data relating to the request (as well as
   * the default image if the requested image is not available).
   *
   * @param requestUrl The url of the remote image
   * @param imageListener The listener to call when the remote image is loaded
   * @param maxWidth The maximum width of the returned image.
   * @param maxHeight The maximum height of the returned image.
   * @param scaleType The ImageViews ScaleType used to calculate the needed image size.
   * @return A container object that contains all of the properties of the request, as well as the
   *     currently available image (default if remote is not loaded).
   */
  public ImageContainer get(
      String requestUrl,
      ImageListener imageListener,
      int maxWidth,
      int maxHeight,
      ScaleType scaleType) {

    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
      // Return the cached bitmap.
      ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
      imageListener.onResponse(container, true);
      return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
      // If it is, add this request to the list of listeners.
      request.addContainer(imageContainer);
      return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<Bitmap> newRequest =
        makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType, cacheKey);

    mRequestQueue.addRequest(newRequest);
    mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
  }
Exemplo n.º 8
0
 public ImageContainer get(
     String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight) {
   throwIfNotOnMainThread();
   String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
   Bitmap cachedBitmap = this.mCache.getBitmap(cacheKey);
   if (cachedBitmap != null) {
     ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
     imageListener.onResponse(container, true);
     return container;
   }
   ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, imageListener);
   imageListener.onResponse(imageContainer, true);
   BatchedImageRequest request = (BatchedImageRequest) this.mInFlightRequests.get(cacheKey);
   if (request != null) {
     request.addContainer(imageContainer);
     return imageContainer;
   }
   Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, cacheKey);
   this.mRequestQueue.add(newRequest);
   this.mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
   return imageContainer;
 }
Exemplo n.º 9
0
    /** Releases interest in the in-flight request (and cancels it if no one else is listening). */
    public void cancelRequest() {
      if (mListener == null) {
        return;
      }

      BatchedImageRequest request = mInFlightRequests.get(mCacheKey);
      if (request != null) {
        boolean canceled = request.removeContainerAndCancelIfNecessary(this);
        if (canceled) {
          mInFlightRequests.remove(mCacheKey);
        }
      } else {
        // check to see if it is already batched for delivery.
        request = mBatchedResponses.get(mCacheKey);
        if (request != null) {
          request.removeContainerAndCancelIfNecessary(this);
          if (request.mContainers.size() == 0) {
            mBatchedResponses.remove(mCacheKey);
          }
        }
      }
    }