mobile/android/thirdparty/com/squareup/picasso/RequestCreator.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/RequestCreator.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,374 @@
     1.4 +/*
     1.5 + * Copyright (C) 2013 Square, Inc.
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +package com.squareup.picasso;
    1.20 +
    1.21 +import android.content.res.Resources;
    1.22 +import android.graphics.Bitmap;
    1.23 +import android.graphics.drawable.Drawable;
    1.24 +import android.net.Uri;
    1.25 +import android.widget.ImageView;
    1.26 +import java.io.IOException;
    1.27 +
    1.28 +import static com.squareup.picasso.BitmapHunter.forRequest;
    1.29 +import static com.squareup.picasso.Picasso.LoadedFrom.MEMORY;
    1.30 +import static com.squareup.picasso.Utils.checkNotMain;
    1.31 +import static com.squareup.picasso.Utils.createKey;
    1.32 +
    1.33 +/** Fluent API for building an image download request. */
    1.34 +@SuppressWarnings("UnusedDeclaration") // Public API.
    1.35 +public class RequestCreator {
    1.36 +  private final Picasso picasso;
    1.37 +  private final Request.Builder data;
    1.38 +
    1.39 +  private boolean skipMemoryCache;
    1.40 +  private boolean noFade;
    1.41 +  private boolean deferred;
    1.42 +  private int placeholderResId;
    1.43 +  private Drawable placeholderDrawable;
    1.44 +  private int errorResId;
    1.45 +  private Drawable errorDrawable;
    1.46 +
    1.47 +  RequestCreator(Picasso picasso, Uri uri, int resourceId) {
    1.48 +    if (picasso.shutdown) {
    1.49 +      throw new IllegalStateException(
    1.50 +          "Picasso instance already shut down. Cannot submit new requests.");
    1.51 +    }
    1.52 +    this.picasso = picasso;
    1.53 +    this.data = new Request.Builder(uri, resourceId);
    1.54 +  }
    1.55 +
    1.56 +  /**
    1.57 +   * A placeholder drawable to be used while the image is being loaded. If the requested image is
    1.58 +   * not immediately available in the memory cache then this resource will be set on the target
    1.59 +   * {@link ImageView}.
    1.60 +   */
    1.61 +  public RequestCreator placeholder(int placeholderResId) {
    1.62 +    if (placeholderResId == 0) {
    1.63 +      throw new IllegalArgumentException("Placeholder image resource invalid.");
    1.64 +    }
    1.65 +    if (placeholderDrawable != null) {
    1.66 +      throw new IllegalStateException("Placeholder image already set.");
    1.67 +    }
    1.68 +    this.placeholderResId = placeholderResId;
    1.69 +    return this;
    1.70 +  }
    1.71 +
    1.72 +  /**
    1.73 +   * A placeholder drawable to be used while the image is being loaded. If the requested image is
    1.74 +   * not immediately available in the memory cache then this resource will be set on the target
    1.75 +   * {@link ImageView}.
    1.76 +   * <p>
    1.77 +   * If you are not using a placeholder image but want to clear an existing image (such as when
    1.78 +   * used in an {@link android.widget.Adapter adapter}), pass in {@code null}.
    1.79 +   */
    1.80 +  public RequestCreator placeholder(Drawable placeholderDrawable) {
    1.81 +    if (placeholderResId != 0) {
    1.82 +      throw new IllegalStateException("Placeholder image already set.");
    1.83 +    }
    1.84 +    this.placeholderDrawable = placeholderDrawable;
    1.85 +    return this;
    1.86 +  }
    1.87 +
    1.88 +  /** An error drawable to be used if the request image could not be loaded. */
    1.89 +  public RequestCreator error(int errorResId) {
    1.90 +    if (errorResId == 0) {
    1.91 +      throw new IllegalArgumentException("Error image resource invalid.");
    1.92 +    }
    1.93 +    if (errorDrawable != null) {
    1.94 +      throw new IllegalStateException("Error image already set.");
    1.95 +    }
    1.96 +    this.errorResId = errorResId;
    1.97 +    return this;
    1.98 +  }
    1.99 +
   1.100 +  /** An error drawable to be used if the request image could not be loaded. */
   1.101 +  public RequestCreator error(Drawable errorDrawable) {
   1.102 +    if (errorDrawable == null) {
   1.103 +      throw new IllegalArgumentException("Error image may not be null.");
   1.104 +    }
   1.105 +    if (errorResId != 0) {
   1.106 +      throw new IllegalStateException("Error image already set.");
   1.107 +    }
   1.108 +    this.errorDrawable = errorDrawable;
   1.109 +    return this;
   1.110 +  }
   1.111 +
   1.112 +  /**
   1.113 +   * Attempt to resize the image to fit exactly into the target {@link ImageView}'s bounds. This
   1.114 +   * will result in delayed execution of the request until the {@link ImageView} has been measured.
   1.115 +   * <p/>
   1.116 +   * <em>Note:</em> This method works only when your target is an {@link ImageView}.
   1.117 +   */
   1.118 +  public RequestCreator fit() {
   1.119 +    deferred = true;
   1.120 +    return this;
   1.121 +  }
   1.122 +
   1.123 +  /** Internal use only. Used by {@link DeferredRequestCreator}. */
   1.124 +  RequestCreator unfit() {
   1.125 +    deferred = false;
   1.126 +    return this;
   1.127 +  }
   1.128 +
   1.129 +  /** Resize the image to the specified dimension size. */
   1.130 +  public RequestCreator resizeDimen(int targetWidthResId, int targetHeightResId) {
   1.131 +    Resources resources = picasso.context.getResources();
   1.132 +    int targetWidth = resources.getDimensionPixelSize(targetWidthResId);
   1.133 +    int targetHeight = resources.getDimensionPixelSize(targetHeightResId);
   1.134 +    return resize(targetWidth, targetHeight);
   1.135 +  }
   1.136 +
   1.137 +  /** Resize the image to the specified size in pixels. */
   1.138 +  public RequestCreator resize(int targetWidth, int targetHeight) {
   1.139 +    data.resize(targetWidth, targetHeight);
   1.140 +    return this;
   1.141 +  }
   1.142 +
   1.143 +  /**
   1.144 +   * Crops an image inside of the bounds specified by {@link #resize(int, int)} rather than
   1.145 +   * distorting the aspect ratio. This cropping technique scales the image so that it fills the
   1.146 +   * requested bounds and then crops the extra.
   1.147 +   */
   1.148 +  public RequestCreator centerCrop() {
   1.149 +    data.centerCrop();
   1.150 +    return this;
   1.151 +  }
   1.152 +
   1.153 +  /**
   1.154 +   * Centers an image inside of the bounds specified by {@link #resize(int, int)}. This scales
   1.155 +   * the image so that both dimensions are equal to or less than the requested bounds.
   1.156 +   */
   1.157 +  public RequestCreator centerInside() {
   1.158 +    data.centerInside();
   1.159 +    return this;
   1.160 +  }
   1.161 +
   1.162 +  /** Rotate the image by the specified degrees. */
   1.163 +  public RequestCreator rotate(float degrees) {
   1.164 +    data.rotate(degrees);
   1.165 +    return this;
   1.166 +  }
   1.167 +
   1.168 +  /** Rotate the image by the specified degrees around a pivot point. */
   1.169 +  public RequestCreator rotate(float degrees, float pivotX, float pivotY) {
   1.170 +    data.rotate(degrees, pivotX, pivotY);
   1.171 +    return this;
   1.172 +  }
   1.173 +
   1.174 +  /**
   1.175 +   * Add a custom transformation to be applied to the image.
   1.176 +   * <p/>
   1.177 +   * Custom transformations will always be run after the built-in transformations.
   1.178 +   */
   1.179 +  // TODO show example of calling resize after a transform in the javadoc
   1.180 +  public RequestCreator transform(Transformation transformation) {
   1.181 +    data.transform(transformation);
   1.182 +    return this;
   1.183 +  }
   1.184 +
   1.185 +  /**
   1.186 +   * Indicate that this action should not use the memory cache for attempting to load or save the
   1.187 +   * image. This can be useful when you know an image will only ever be used once (e.g., loading
   1.188 +   * an image from the filesystem and uploading to a remote server).
   1.189 +   */
   1.190 +  public RequestCreator skipMemoryCache() {
   1.191 +    skipMemoryCache = true;
   1.192 +    return this;
   1.193 +  }
   1.194 +
   1.195 +  /** Disable brief fade in of images loaded from the disk cache or network. */
   1.196 +  public RequestCreator noFade() {
   1.197 +    noFade = true;
   1.198 +    return this;
   1.199 +  }
   1.200 +
   1.201 +  /** Synchronously fulfill this request. Must not be called from the main thread. */
   1.202 +  public Bitmap get() throws IOException {
   1.203 +    checkNotMain();
   1.204 +    if (deferred) {
   1.205 +      throw new IllegalStateException("Fit cannot be used with get.");
   1.206 +    }
   1.207 +    if (!data.hasImage()) {
   1.208 +      return null;
   1.209 +    }
   1.210 +
   1.211 +    Request finalData = picasso.transformRequest(data.build());
   1.212 +    String key = createKey(finalData);
   1.213 +
   1.214 +    Action action = new GetAction(picasso, finalData, skipMemoryCache, key);
   1.215 +    return forRequest(picasso.context, picasso, picasso.dispatcher, picasso.cache, picasso.stats,
   1.216 +        action, picasso.dispatcher.downloader).hunt();
   1.217 +  }
   1.218 +
   1.219 +  /**
   1.220 +   * Asynchronously fulfills the request without a {@link ImageView} or {@link Target}. This is
   1.221 +   * useful when you want to warm up the cache with an image.
   1.222 +   */
   1.223 +  public void fetch() {
   1.224 +    if (deferred) {
   1.225 +      throw new IllegalStateException("Fit cannot be used with fetch.");
   1.226 +    }
   1.227 +    if (data.hasImage()) {
   1.228 +      Request finalData = picasso.transformRequest(data.build());
   1.229 +      String key = createKey(finalData);
   1.230 +
   1.231 +      Action action = new FetchAction(picasso, finalData, skipMemoryCache, key);
   1.232 +      picasso.enqueueAndSubmit(action);
   1.233 +    }
   1.234 +  }
   1.235 +
   1.236 +  /**
   1.237 +   * Asynchronously fulfills the request into the specified {@link Target}. In most cases, you
   1.238 +   * should use this when you are dealing with a custom {@link android.view.View View} or view
   1.239 +   * holder which should implement the {@link Target} interface.
   1.240 +   * <p>
   1.241 +   * Implementing on a {@link android.view.View View}:
   1.242 +   * <blockquote><pre>
   1.243 +   * public class ProfileView extends FrameLayout implements Target {
   1.244 +   *   {@literal @}Override public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
   1.245 +   *     setBackgroundDrawable(new BitmapDrawable(bitmap));
   1.246 +   *   }
   1.247 +   *
   1.248 +   *   {@literal @}Override public void onBitmapFailed() {
   1.249 +   *     setBackgroundResource(R.drawable.profile_error);
   1.250 +   *   }
   1.251 +   * }
   1.252 +   * </pre></blockquote>
   1.253 +   * Implementing on a view holder object for use inside of an adapter:
   1.254 +   * <blockquote><pre>
   1.255 +   * public class ViewHolder implements Target {
   1.256 +   *   public FrameLayout frame;
   1.257 +   *   public TextView name;
   1.258 +   *
   1.259 +   *   {@literal @}Override public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
   1.260 +   *     frame.setBackgroundDrawable(new BitmapDrawable(bitmap));
   1.261 +   *   }
   1.262 +   *
   1.263 +   *   {@literal @}Override public void onBitmapFailed() {
   1.264 +   *     frame.setBackgroundResource(R.drawable.profile_error);
   1.265 +   *   }
   1.266 +   * }
   1.267 +   * </pre></blockquote>
   1.268 +   * <p>
   1.269 +   * <em>Note:</em> This method keeps a weak reference to the {@link Target} instance and will be
   1.270 +   * garbage collected if you do not keep a strong reference to it. To receive callbacks when an
   1.271 +   * image is loaded use {@link #into(android.widget.ImageView, Callback)}.
   1.272 +   */
   1.273 +  public void into(Target target) {
   1.274 +    if (target == null) {
   1.275 +      throw new IllegalArgumentException("Target must not be null.");
   1.276 +    }
   1.277 +    if (deferred) {
   1.278 +      throw new IllegalStateException("Fit cannot be used with a Target.");
   1.279 +    }
   1.280 +
   1.281 +    Drawable drawable =
   1.282 +        placeholderResId != 0 ? picasso.context.getResources().getDrawable(placeholderResId)
   1.283 +            : placeholderDrawable;
   1.284 +
   1.285 +    if (!data.hasImage()) {
   1.286 +      picasso.cancelRequest(target);
   1.287 +      target.onPrepareLoad(drawable);
   1.288 +      return;
   1.289 +    }
   1.290 +
   1.291 +    Request finalData = picasso.transformRequest(data.build());
   1.292 +    String requestKey = createKey(finalData);
   1.293 +
   1.294 +    if (!skipMemoryCache) {
   1.295 +      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
   1.296 +      if (bitmap != null) {
   1.297 +        picasso.cancelRequest(target);
   1.298 +        target.onBitmapLoaded(bitmap, MEMORY);
   1.299 +        return;
   1.300 +      }
   1.301 +    }
   1.302 +
   1.303 +    target.onPrepareLoad(drawable);
   1.304 +
   1.305 +    Action action = new TargetAction(picasso, target, finalData, skipMemoryCache, requestKey);
   1.306 +    picasso.enqueueAndSubmit(action);
   1.307 +  }
   1.308 +
   1.309 +  /**
   1.310 +   * Asynchronously fulfills the request into the specified {@link ImageView}.
   1.311 +   * <p/>
   1.312 +   * <em>Note:</em> This method keeps a weak reference to the {@link ImageView} instance and will
   1.313 +   * automatically support object recycling.
   1.314 +   */
   1.315 +  public void into(ImageView target) {
   1.316 +    into(target, null);
   1.317 +  }
   1.318 +
   1.319 +  /**
   1.320 +   * Asynchronously fulfills the request into the specified {@link ImageView} and invokes the
   1.321 +   * target {@link Callback} if it's not {@code null}.
   1.322 +   * <p/>
   1.323 +   * <em>Note:</em> The {@link Callback} param is a strong reference and will prevent your
   1.324 +   * {@link android.app.Activity} or {@link android.app.Fragment} from being garbage collected. If
   1.325 +   * you use this method, it is <b>strongly</b> recommended you invoke an adjacent
   1.326 +   * {@link Picasso#cancelRequest(android.widget.ImageView)} call to prevent temporary leaking.
   1.327 +   */
   1.328 +  public void into(ImageView target, Callback callback) {
   1.329 +    if (target == null) {
   1.330 +      throw new IllegalArgumentException("Target must not be null.");
   1.331 +    }
   1.332 +
   1.333 +    if (!data.hasImage()) {
   1.334 +      picasso.cancelRequest(target);
   1.335 +      PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable);
   1.336 +      return;
   1.337 +    }
   1.338 +
   1.339 +    if (deferred) {
   1.340 +      if (data.hasSize()) {
   1.341 +        throw new IllegalStateException("Fit cannot be used with resize.");
   1.342 +      }
   1.343 +      int measuredWidth = target.getMeasuredWidth();
   1.344 +      int measuredHeight = target.getMeasuredHeight();
   1.345 +      if (measuredWidth == 0 || measuredHeight == 0) {
   1.346 +        PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable);
   1.347 +        picasso.defer(target, new DeferredRequestCreator(this, target, callback));
   1.348 +        return;
   1.349 +      }
   1.350 +      data.resize(measuredWidth, measuredHeight);
   1.351 +    }
   1.352 +
   1.353 +    Request finalData = picasso.transformRequest(data.build());
   1.354 +    String requestKey = createKey(finalData);
   1.355 +
   1.356 +    if (!skipMemoryCache) {
   1.357 +      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
   1.358 +      if (bitmap != null) {
   1.359 +        picasso.cancelRequest(target);
   1.360 +        PicassoDrawable.setBitmap(target, picasso.context, bitmap, MEMORY, noFade,
   1.361 +            picasso.debugging);
   1.362 +        if (callback != null) {
   1.363 +          callback.onSuccess();
   1.364 +        }
   1.365 +        return;
   1.366 +      }
   1.367 +    }
   1.368 +
   1.369 +    PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable);
   1.370 +
   1.371 +    Action action =
   1.372 +        new ImageViewAction(picasso, target, finalData, skipMemoryCache, noFade, errorResId,
   1.373 +            errorDrawable, requestKey, callback);
   1.374 +
   1.375 +    picasso.enqueueAndSubmit(action);
   1.376 +  }
   1.377 +}

mercurial