mobile/android/thirdparty/com/squareup/picasso/Request.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/Request.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,307 @@
     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.net.Uri;
    1.22 +import java.util.ArrayList;
    1.23 +import java.util.List;
    1.24 +
    1.25 +import static java.util.Collections.unmodifiableList;
    1.26 +
    1.27 +/** Immutable data about an image and the transformations that will be applied to it. */
    1.28 +public final class Request {
    1.29 +  /**
    1.30 +   * The image URI.
    1.31 +   * <p>
    1.32 +   * This is mutually exclusive with {@link #resourceId}.
    1.33 +   */
    1.34 +  public final Uri uri;
    1.35 +  /**
    1.36 +   * The image resource ID.
    1.37 +   * <p>
    1.38 +   * This is mutually exclusive with {@link #uri}.
    1.39 +   */
    1.40 +  public final int resourceId;
    1.41 +  /** List of custom transformations to be applied after the built-in transformations. */
    1.42 +  public final List<Transformation> transformations;
    1.43 +  /** Target image width for resizing. */
    1.44 +  public final int targetWidth;
    1.45 +  /** Target image height for resizing. */
    1.46 +  public final int targetHeight;
    1.47 +  /**
    1.48 +   * True if the final image should use the 'centerCrop' scale technique.
    1.49 +   * <p>
    1.50 +   * This is mutually exclusive with {@link #centerInside}.
    1.51 +   */
    1.52 +  public final boolean centerCrop;
    1.53 +  /**
    1.54 +   * True if the final image should use the 'centerInside' scale technique.
    1.55 +   * <p>
    1.56 +   * This is mutually exclusive with {@link #centerCrop}.
    1.57 +   */
    1.58 +  public final boolean centerInside;
    1.59 +  /** Amount to rotate the image in degrees. */
    1.60 +  public final float rotationDegrees;
    1.61 +  /** Rotation pivot on the X axis. */
    1.62 +  public final float rotationPivotX;
    1.63 +  /** Rotation pivot on the Y axis. */
    1.64 +  public final float rotationPivotY;
    1.65 +  /** Whether or not {@link #rotationPivotX} and {@link #rotationPivotY} are set. */
    1.66 +  public final boolean hasRotationPivot;
    1.67 +
    1.68 +  private Request(Uri uri, int resourceId, List<Transformation> transformations, int targetWidth,
    1.69 +      int targetHeight, boolean centerCrop, boolean centerInside, float rotationDegrees,
    1.70 +      float rotationPivotX, float rotationPivotY, boolean hasRotationPivot) {
    1.71 +    this.uri = uri;
    1.72 +    this.resourceId = resourceId;
    1.73 +    if (transformations == null) {
    1.74 +      this.transformations = null;
    1.75 +    } else {
    1.76 +      this.transformations = unmodifiableList(transformations);
    1.77 +    }
    1.78 +    this.targetWidth = targetWidth;
    1.79 +    this.targetHeight = targetHeight;
    1.80 +    this.centerCrop = centerCrop;
    1.81 +    this.centerInside = centerInside;
    1.82 +    this.rotationDegrees = rotationDegrees;
    1.83 +    this.rotationPivotX = rotationPivotX;
    1.84 +    this.rotationPivotY = rotationPivotY;
    1.85 +    this.hasRotationPivot = hasRotationPivot;
    1.86 +  }
    1.87 +
    1.88 +  String getName() {
    1.89 +    if (uri != null) {
    1.90 +      return uri.getPath();
    1.91 +    }
    1.92 +    return Integer.toHexString(resourceId);
    1.93 +  }
    1.94 +
    1.95 +  public boolean hasSize() {
    1.96 +    return targetWidth != 0;
    1.97 +  }
    1.98 +
    1.99 +  boolean needsTransformation() {
   1.100 +    return needsMatrixTransform() || hasCustomTransformations();
   1.101 +  }
   1.102 +
   1.103 +  boolean needsMatrixTransform() {
   1.104 +    return targetWidth != 0 || rotationDegrees != 0;
   1.105 +  }
   1.106 +
   1.107 +  boolean hasCustomTransformations() {
   1.108 +    return transformations != null;
   1.109 +  }
   1.110 +
   1.111 +  public Builder buildUpon() {
   1.112 +    return new Builder(this);
   1.113 +  }
   1.114 +
   1.115 +  /** Builder for creating {@link Request} instances. */
   1.116 +  public static final class Builder {
   1.117 +    private Uri uri;
   1.118 +    private int resourceId;
   1.119 +    private int targetWidth;
   1.120 +    private int targetHeight;
   1.121 +    private boolean centerCrop;
   1.122 +    private boolean centerInside;
   1.123 +    private float rotationDegrees;
   1.124 +    private float rotationPivotX;
   1.125 +    private float rotationPivotY;
   1.126 +    private boolean hasRotationPivot;
   1.127 +    private List<Transformation> transformations;
   1.128 +
   1.129 +    /** Start building a request using the specified {@link Uri}. */
   1.130 +    public Builder(Uri uri) {
   1.131 +      setUri(uri);
   1.132 +    }
   1.133 +
   1.134 +    /** Start building a request using the specified resource ID. */
   1.135 +    public Builder(int resourceId) {
   1.136 +      setResourceId(resourceId);
   1.137 +    }
   1.138 +
   1.139 +    Builder(Uri uri, int resourceId) {
   1.140 +      this.uri = uri;
   1.141 +      this.resourceId = resourceId;
   1.142 +    }
   1.143 +
   1.144 +    private Builder(Request request) {
   1.145 +      uri = request.uri;
   1.146 +      resourceId = request.resourceId;
   1.147 +      targetWidth = request.targetWidth;
   1.148 +      targetHeight = request.targetHeight;
   1.149 +      centerCrop = request.centerCrop;
   1.150 +      centerInside = request.centerInside;
   1.151 +      rotationDegrees = request.rotationDegrees;
   1.152 +      rotationPivotX = request.rotationPivotX;
   1.153 +      rotationPivotY = request.rotationPivotY;
   1.154 +      hasRotationPivot = request.hasRotationPivot;
   1.155 +      if (request.transformations != null) {
   1.156 +        transformations = new ArrayList<Transformation>(request.transformations);
   1.157 +      }
   1.158 +    }
   1.159 +
   1.160 +    boolean hasImage() {
   1.161 +      return uri != null || resourceId != 0;
   1.162 +    }
   1.163 +
   1.164 +    boolean hasSize() {
   1.165 +      return targetWidth != 0;
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Set the target image Uri.
   1.170 +     * <p>
   1.171 +     * This will clear an image resource ID if one is set.
   1.172 +     */
   1.173 +    public Builder setUri(Uri uri) {
   1.174 +      if (uri == null) {
   1.175 +        throw new IllegalArgumentException("Image URI may not be null.");
   1.176 +      }
   1.177 +      this.uri = uri;
   1.178 +      this.resourceId = 0;
   1.179 +      return this;
   1.180 +    }
   1.181 +
   1.182 +    /**
   1.183 +     * Set the target image resource ID.
   1.184 +     * <p>
   1.185 +     * This will clear an image Uri if one is set.
   1.186 +     */
   1.187 +    public Builder setResourceId(int resourceId) {
   1.188 +      if (resourceId == 0) {
   1.189 +        throw new IllegalArgumentException("Image resource ID may not be 0.");
   1.190 +      }
   1.191 +      this.resourceId = resourceId;
   1.192 +      this.uri = null;
   1.193 +      return this;
   1.194 +    }
   1.195 +
   1.196 +    /** Resize the image to the specified size in pixels. */
   1.197 +    public Builder resize(int targetWidth, int targetHeight) {
   1.198 +      if (targetWidth <= 0) {
   1.199 +        throw new IllegalArgumentException("Width must be positive number.");
   1.200 +      }
   1.201 +      if (targetHeight <= 0) {
   1.202 +        throw new IllegalArgumentException("Height must be positive number.");
   1.203 +      }
   1.204 +      this.targetWidth = targetWidth;
   1.205 +      this.targetHeight = targetHeight;
   1.206 +      return this;
   1.207 +    }
   1.208 +
   1.209 +    /** Clear the resize transformation, if any. This will also clear center crop/inside if set. */
   1.210 +    public Builder clearResize() {
   1.211 +      targetWidth = 0;
   1.212 +      targetHeight = 0;
   1.213 +      centerCrop = false;
   1.214 +      centerInside = false;
   1.215 +      return this;
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Crops an image inside of the bounds specified by {@link #resize(int, int)} rather than
   1.220 +     * distorting the aspect ratio. This cropping technique scales the image so that it fills the
   1.221 +     * requested bounds and then crops the extra.
   1.222 +     */
   1.223 +    public Builder centerCrop() {
   1.224 +      if (centerInside) {
   1.225 +        throw new IllegalStateException("Center crop can not be used after calling centerInside");
   1.226 +      }
   1.227 +      centerCrop = true;
   1.228 +      return this;
   1.229 +    }
   1.230 +
   1.231 +    /** Clear the center crop transformation flag, if set. */
   1.232 +    public Builder clearCenterCrop() {
   1.233 +      centerCrop = false;
   1.234 +      return this;
   1.235 +    }
   1.236 +
   1.237 +    /**
   1.238 +     * Centers an image inside of the bounds specified by {@link #resize(int, int)}. This scales
   1.239 +     * the image so that both dimensions are equal to or less than the requested bounds.
   1.240 +     */
   1.241 +    public Builder centerInside() {
   1.242 +      if (centerCrop) {
   1.243 +        throw new IllegalStateException("Center inside can not be used after calling centerCrop");
   1.244 +      }
   1.245 +      centerInside = true;
   1.246 +      return this;
   1.247 +    }
   1.248 +
   1.249 +    /** Clear the center inside transformation flag, if set. */
   1.250 +    public Builder clearCenterInside() {
   1.251 +      centerInside = false;
   1.252 +      return this;
   1.253 +    }
   1.254 +
   1.255 +    /** Rotate the image by the specified degrees. */
   1.256 +    public Builder rotate(float degrees) {
   1.257 +      rotationDegrees = degrees;
   1.258 +      return this;
   1.259 +    }
   1.260 +
   1.261 +    /** Rotate the image by the specified degrees around a pivot point. */
   1.262 +    public Builder rotate(float degrees, float pivotX, float pivotY) {
   1.263 +      rotationDegrees = degrees;
   1.264 +      rotationPivotX = pivotX;
   1.265 +      rotationPivotY = pivotY;
   1.266 +      hasRotationPivot = true;
   1.267 +      return this;
   1.268 +    }
   1.269 +
   1.270 +    /** Clear the rotation transformation, if any. */
   1.271 +    public Builder clearRotation() {
   1.272 +      rotationDegrees = 0;
   1.273 +      rotationPivotX = 0;
   1.274 +      rotationPivotY = 0;
   1.275 +      hasRotationPivot = false;
   1.276 +      return this;
   1.277 +    }
   1.278 +
   1.279 +    /**
   1.280 +     * Add a custom transformation to be applied to the image.
   1.281 +     * <p/>
   1.282 +     * Custom transformations will always be run after the built-in transformations.
   1.283 +     */
   1.284 +    public Builder transform(Transformation transformation) {
   1.285 +      if (transformation == null) {
   1.286 +        throw new IllegalArgumentException("Transformation must not be null.");
   1.287 +      }
   1.288 +      if (transformations == null) {
   1.289 +        transformations = new ArrayList<Transformation>(2);
   1.290 +      }
   1.291 +      transformations.add(transformation);
   1.292 +      return this;
   1.293 +    }
   1.294 +
   1.295 +    /** Create the immutable {@link Request} object. */
   1.296 +    public Request build() {
   1.297 +      if (centerInside && centerCrop) {
   1.298 +        throw new IllegalStateException("Center crop and center inside can not be used together.");
   1.299 +      }
   1.300 +      if (centerCrop && targetWidth == 0) {
   1.301 +        throw new IllegalStateException("Center crop requires calling resize.");
   1.302 +      }
   1.303 +      if (centerInside && targetWidth == 0) {
   1.304 +        throw new IllegalStateException("Center inside requires calling resize.");
   1.305 +      }
   1.306 +      return new Request(uri, resourceId, transformations, targetWidth, targetHeight, centerCrop,
   1.307 +          centerInside, rotationDegrees, rotationPivotX, rotationPivotY, hasRotationPivot);
   1.308 +    }
   1.309 +  }
   1.310 +}

mercurial