From 4014974f022d1ca226c240e3e6422b5879d0c1f2 Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Thu, 11 May 2017 14:40:23 -0500 Subject: [PATCH 1/4] Made onlyScaleDown method set a flag if fit() is called so that the deferred resize() call can have onlyScaleDown() called after it --- .../com/squareup/picasso/DeferredRequestCreator.java | 9 ++++++++- picasso/src/main/java/com/squareup/picasso/Request.java | 5 ++++- .../main/java/com/squareup/picasso/RequestCreator.java | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/picasso/src/main/java/com/squareup/picasso/DeferredRequestCreator.java b/picasso/src/main/java/com/squareup/picasso/DeferredRequestCreator.java index dfa112357b..fe29b62d80 100644 --- a/picasso/src/main/java/com/squareup/picasso/DeferredRequestCreator.java +++ b/picasso/src/main/java/com/squareup/picasso/DeferredRequestCreator.java @@ -21,6 +21,7 @@ import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.ImageView; + import java.lang.ref.WeakReference; class DeferredRequestCreator implements OnPreDrawListener, OnAttachStateChangeListener { @@ -72,7 +73,13 @@ class DeferredRequestCreator implements OnPreDrawListener, OnAttachStateChangeLi vto.removeOnPreDrawListener(this); this.target.clear(); - this.creator.unfit().resize(width, height).into(target, callback); + this.creator.unfit().resize(width, height); + if (this.creator.doOnlyScaleDown) { + this.creator.onlyScaleDown(); + } + + this.creator.into(target, callback); + return true; } diff --git a/picasso/src/main/java/com/squareup/picasso/Request.java b/picasso/src/main/java/com/squareup/picasso/Request.java index dbe2d160ee..21e8ee13c4 100644 --- a/picasso/src/main/java/com/squareup/picasso/Request.java +++ b/picasso/src/main/java/com/squareup/picasso/Request.java @@ -22,7 +22,9 @@ import android.support.annotation.Nullable; import android.support.annotation.Px; import android.view.Gravity; + import com.squareup.picasso.Picasso.Priority; + import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -391,7 +393,8 @@ public Builder clearCenterInside() { */ public Builder onlyScaleDown() { if (targetHeight == 0 && targetWidth == 0) { - throw new IllegalStateException("onlyScaleDown can not be applied without resize"); + throw new IllegalStateException( + "onlyScaleDown can not be applied without either calling resize or fit"); } onlyScaleDown = true; return this; diff --git a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java index b645edb754..bd15e4f1fa 100644 --- a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java +++ b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java @@ -29,6 +29,7 @@ import android.view.Gravity; import android.widget.ImageView; import android.widget.RemoteViews; + import java.io.IOException; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -60,6 +61,7 @@ public class RequestCreator { private boolean noFade; private boolean deferred; + boolean doOnlyScaleDown; private boolean setPlaceholder = true; private int placeholderResId; private int errorResId; @@ -268,7 +270,11 @@ public RequestCreator centerInside() { * specified by {@link #resize(int, int)}. */ public RequestCreator onlyScaleDown() { - data.onlyScaleDown(); + if (!deferred) { + data.onlyScaleDown(); + } else { + doOnlyScaleDown = true; + } return this; } From 8f6e1c78338eff29e4470f269a3deb33a3a859cc Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Thu, 11 May 2017 15:31:03 -0500 Subject: [PATCH 2/4] Added test case to verify that onlyScaleDown still throws without fit() or resize() called beforehand --- .../squareup/picasso/RequestCreatorTest.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java index 61b88d172b..267bc8ced3 100644 --- a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java +++ b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java @@ -20,10 +20,7 @@ import android.graphics.drawable.Drawable; import android.widget.ImageView; import android.widget.RemoteViews; -import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CountDownLatch; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,6 +30,11 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; + import static android.graphics.Bitmap.Config.ARGB_8888; import static com.squareup.picasso.Picasso.LoadedFrom.MEMORY; import static com.squareup.picasso.Picasso.Priority.HIGH; @@ -547,12 +549,17 @@ public void intoRemoteViewsNotificationWithFitThrows() { public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { try { new RequestCreator(picasso, URI_1, 0).centerInside().into(mockTarget()); - fail("Center inside with unknown width should throw exception."); + fail("Center inside with unknown height/width should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator(picasso, URI_1, 0).centerCrop().into(mockTarget()); - fail("Center inside with unknown height should throw exception."); + fail("Center inside with unknown height/width should throw exception."); + } catch (IllegalStateException ignored) { + } + try { + new RequestCreator(picasso, URI_1, 0).onlyScaleDown().into(mockTarget()); + fail("Only scale down with unknown height/width should throw exception."); } catch (IllegalStateException ignored) { } } From 4e70c1d3b609c0328ae57bc3a5377e369e84f027 Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Thu, 11 May 2017 15:35:48 -0500 Subject: [PATCH 3/4] Added suppression of serious warnings regarding invalid resource ID types when it was intentionally provided as invalid --- .../java/com/squareup/picasso/RequestCreatorTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java index 267bc8ced3..15aa2ed65f 100644 --- a/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java +++ b/picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java @@ -529,6 +529,7 @@ public void intoRemoteViewsNotificationWithNullNotificationThrows() { public void intoRemoteViewsWidgetWithFitThrows() { try { RemoteViews remoteViews = mockRemoteViews(); + //noinspection ResourceType new RequestCreator(picasso, URI_1, 0).fit().into(remoteViews, 1, new int[] { 1, 2, 3 }); fail("Calling fit() into remote views should throw exception"); } catch (IllegalStateException ignored) { @@ -539,6 +540,7 @@ public void intoRemoteViewsWidgetWithFitThrows() { public void intoRemoteViewsNotificationWithFitThrows() { try { RemoteViews remoteViews = mockRemoteViews(); + //noinspection ResourceType new RequestCreator(picasso, URI_1, 0).fit().into(remoteViews, 1, 1, mockNotification()); fail("Calling fit() into remote views should throw exception"); } catch (IllegalStateException ignored) { @@ -705,11 +707,13 @@ public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { } catch (IllegalArgumentException ignored) { } try { + //noinspection ResourceType new RequestCreator().placeholder(1).placeholder(new ColorDrawable(0)); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } try { + //noinspection ResourceType new RequestCreator().placeholder(new ColorDrawable(0)).placeholder(1); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { @@ -723,11 +727,13 @@ public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { } catch (IllegalStateException ignored) { } try { + //noinspection ResourceType new RequestCreator().noPlaceholder().placeholder(1); fail("Placeholder after no placeholder should throw exception."); } catch (IllegalStateException ignored) { } try { + //noinspection ResourceType new RequestCreator().placeholder(1).noPlaceholder(); fail("No placeholder after placeholder should throw exception."); } catch (IllegalStateException ignored) { @@ -751,11 +757,13 @@ public void intoTargetNoResizeWithCenterInsideOrCenterCropThrows() { } catch (IllegalArgumentException ignored) { } try { + //noinspection ResourceType new RequestCreator().error(1).error(new ColorDrawable(0)); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } try { + //noinspection ResourceType new RequestCreator().error(new ColorDrawable(0)).error(1); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { From bbee4794454fee1dfe1d5bc1ba48bf3dca40d909 Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Thu, 11 May 2017 15:45:54 -0500 Subject: [PATCH 4/4] Changed @NonNull annotation to @Nullable annotation to match the method that it actually calls --- picasso/src/main/java/com/squareup/picasso/RequestCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java index bd15e4f1fa..eff3323b83 100644 --- a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java +++ b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java @@ -305,7 +305,7 @@ public RequestCreator config(@NonNull Bitmap.Config config) { * Sets the stable key for this request to be used instead of the URI or resource ID when * caching. Two requests with the same value are considered to be for the same resource. */ - public RequestCreator stableKey(@NonNull String stableKey) { + public RequestCreator stableKey(@Nullable String stableKey) { data.stableKey(stableKey); return this; }