-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from square/master
Merge Picasso 2.0 changes
- Loading branch information
Showing
62 changed files
with
5,056 additions
and
3,024 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Picasso Pollexor Request Transformer | ||
==================================== | ||
|
||
A request transformer which uses a remote [Thumbor][1] install to perform | ||
image transformation on the server. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Create a `PollexorRequestTransformer` using the remote host and optional encryption key. | ||
|
||
```java | ||
RequestTransformer transformer = | ||
new PollexorRequestTransformer("http://example.com", "secretpassword"); | ||
``` | ||
|
||
Pass the transformer when creating a `Picasso` instance. | ||
|
||
```java | ||
Picasso p = new Picasso.Builder(context) | ||
.setRequestTransformer(transformer) | ||
.build(); | ||
``` | ||
|
||
_Note: This can only be used with an instance you create yourself. You cannot set a request | ||
transformer on the global singleton instance (`Picasso.with`)._ | ||
|
||
|
||
|
||
[1]: https://github.com/globocom/thumbor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.squareup.picasso</groupId> | ||
<artifactId>picasso-parent</artifactId> | ||
<version>2.0.1-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>picasso-pollexor</artifactId> | ||
<name>Picasso Pollexor Transformer</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.picasso</groupId> | ||
<artifactId>picasso</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup</groupId> | ||
<artifactId>pollexor</artifactId> | ||
<version>1.2.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup</groupId> | ||
<artifactId>fest-android</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.robolectric</groupId> | ||
<artifactId>robolectric</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.android</groupId> | ||
<artifactId>android</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
picasso-pollexor/src/main/java/com/squareup/picasso/pollexor/PollexorRequestTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.squareup.picasso.pollexor; | ||
|
||
import android.net.Uri; | ||
import com.squareup.picasso.Request; | ||
import com.squareup.pollexor.Pollexor; | ||
|
||
import static com.squareup.picasso.Picasso.RequestTransformer; | ||
|
||
/** | ||
* A {@link RequestTransformer} that changes requests to use Thumbor for some remote | ||
* transformations. | ||
*/ | ||
public class PollexorRequestTransformer implements RequestTransformer { | ||
private final String host; | ||
private final String key; | ||
|
||
/** Create a transformer for the specified Thumbor host. This will not use URL encryption. */ | ||
public PollexorRequestTransformer(String host) { | ||
this(host, null); | ||
} | ||
|
||
/** Create a transformer for the specified Thumbor host using the provided URL encryption key. */ | ||
public PollexorRequestTransformer(String host, String key) { | ||
this.host = host; | ||
this.key = key; | ||
} | ||
|
||
@Override public Request transformRequest(Request request) { | ||
if (request.resourceId != 0) { | ||
return request; // Don't transform resource requests. | ||
} | ||
Uri uri = request.uri; | ||
String scheme = uri.getScheme(); | ||
if (!"https".equals(scheme) && !"http".equals(scheme)) { | ||
return request; // Thumbor only supports remote images. | ||
} | ||
if (!request.hasSize()) { | ||
return request; // Thumbor only works with resizing images. | ||
} | ||
if (request.centerCrop) { | ||
return request; // Can't center crop yet. See: https://github.com/globocom/thumbor/issues/201 | ||
} | ||
|
||
// Start building a new request for us to mutate. | ||
Request.Builder newRequest = request.buildUpon(); | ||
|
||
// Start creating the Thumbor URL with the image and host. Add the encryption key, if present. | ||
Pollexor pollexor = Pollexor.image(uri.toString()).host(host); | ||
if (key != null) { | ||
pollexor.key(key); | ||
} | ||
|
||
// Resize the image to the target size. | ||
pollexor.resize(request.targetWidth, request.targetHeight); | ||
newRequest.clearResize(); | ||
|
||
// If the center inside flag is set, perform that with Thumbor as well. | ||
if (request.centerInside) { | ||
pollexor.fitIn(); | ||
newRequest.clearCenterInside(); | ||
} | ||
|
||
// Update the request with the completed Thumbor URL. | ||
newRequest.setUri(Uri.parse(pollexor.toUrl())); | ||
|
||
return newRequest.build(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...-pollexor/src/test/java/com/squareup/picasso/pollexor/PollexorRequestTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.squareup.picasso.pollexor; | ||
|
||
import android.net.Uri; | ||
import com.squareup.picasso.Request; | ||
import com.squareup.pollexor.Pollexor; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import static com.squareup.picasso.Picasso.RequestTransformer; | ||
import static org.fest.assertions.api.Assertions.assertThat; | ||
import static org.robolectric.annotation.Config.NONE; | ||
|
||
@RunWith(RobolectricTestRunner.class) // | ||
@Config(manifest = NONE) | ||
public class PollexorRequestTransformerTest { | ||
private static final String HOST = "http://example.com/"; | ||
private static final String KEY = "omgsecretpassword"; | ||
private static final String IMAGE = "http://google.com/logo.png"; | ||
private static final Uri IMAGE_URI = Uri.parse(IMAGE); | ||
|
||
private RequestTransformer transformer = new PollexorRequestTransformer(HOST); | ||
private RequestTransformer secureTransformer = new PollexorRequestTransformer(HOST, KEY); | ||
|
||
@Test public void resourceIdRequestsAreNotTransformed() { | ||
Request input = new Request.Builder(12).build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isSameAs(input); | ||
} | ||
|
||
@Test public void nonHttpRequestsAreNotTransformed() { | ||
Request input = new Request.Builder(IMAGE_URI).build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isSameAs(input); | ||
} | ||
|
||
@Test public void nonResizedRequestsAreNotTransformed() { | ||
Request input = new Request.Builder(IMAGE_URI).build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isSameAs(input); | ||
} | ||
|
||
@Test public void centerCropRequestsAreNotTransformed() { | ||
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerCrop().build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isSameAs(input); | ||
} | ||
|
||
@Test public void simpleResize() { | ||
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isNotSameAs(input); | ||
assertThat(output.hasSize()).isFalse(); | ||
|
||
String expected = Pollexor.image(IMAGE).host(HOST).resize(50, 50).toUrl(); | ||
assertThat(output.uri.toString()).isEqualTo(expected); | ||
} | ||
|
||
@Test public void simpleResizeWithCenterInside() { | ||
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerInside().build(); | ||
Request output = transformer.transformRequest(input); | ||
assertThat(output).isNotSameAs(input); | ||
assertThat(output.hasSize()).isFalse(); | ||
assertThat(output.centerInside).isFalse(); | ||
|
||
String expected = Pollexor.image(IMAGE).host(HOST).resize(50, 50).fitIn().toUrl(); | ||
assertThat(output.uri.toString()).isEqualTo(expected); | ||
} | ||
|
||
@Test public void simpleResizeWithEncryption() { | ||
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); | ||
Request output = secureTransformer.transformRequest(input); | ||
assertThat(output).isNotSameAs(input); | ||
assertThat(output.hasSize()).isFalse(); | ||
|
||
String expected = Pollexor.image(IMAGE).host(HOST).key(KEY).resize(50, 50).toUrl(); | ||
assertThat(output.uri.toString()).isEqualTo(expected); | ||
} | ||
|
||
@Test public void simpleResizeWithCenterInsideAndEncryption() { | ||
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerInside().build(); | ||
Request output = secureTransformer.transformRequest(input); | ||
assertThat(output).isNotSameAs(input); | ||
assertThat(output.hasSize()).isFalse(); | ||
assertThat(output.centerInside).isFalse(); | ||
|
||
String expected = Pollexor.image(IMAGE).host(HOST).key(KEY).resize(50, 50).fitIn().toUrl(); | ||
assertThat(output.uri.toString()).isEqualTo(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
picasso-sample/src/main/java/com/example/picasso/CropSquareTransformation.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.