Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onlyScaleDown() compatibility with fit() #1623

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion picasso/src/main/java/com/squareup/picasso/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/RequestCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -299,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;
}
Expand Down
27 changes: 21 additions & 6 deletions picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -527,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) {
Expand All @@ -537,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) {
Expand All @@ -547,12 +551,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) {
}
}
Expand Down Expand Up @@ -698,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) {
Expand All @@ -716,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) {
Expand All @@ -744,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) {
Expand Down