diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/test/ConcurrencyHelper.java b/instrumentation/src/androidTest/java/com/bumptech/glide/test/ConcurrencyHelper.java index 8fac96134e..1580247d04 100644 --- a/instrumentation/src/androidTest/java/com/bumptech/glide/test/ConcurrencyHelper.java +++ b/instrumentation/src/androidTest/java/com/bumptech/glide/test/ConcurrencyHelper.java @@ -4,6 +4,7 @@ import android.os.Debug; import android.os.Handler; import android.os.Looper; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.test.InstrumentationRegistry; import android.widget.ImageView; @@ -101,7 +102,7 @@ private void loadUntilFirstFinish( final CountDownLatch latch = new CountDownLatch(1); callOnMainThread(new Callable>() { @Override - public Target call() throws Exception { + public Target call() { builder.into(new Target() { @Override public void onStart() { @@ -119,7 +120,8 @@ public void onDestroy() { } @Override - public void onResourceReady(T resource, Transition transition) { + public void onResourceReady(@NonNull T resource, + @Nullable Transition transition) { target.onResourceReady(resource, transition); latch.countDown(); } @@ -141,12 +143,12 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) { } @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { target.getSize(cb); } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { target.removeCallback(cb); } @@ -171,7 +173,7 @@ private void loadOnMainThread(final RequestBuilder builder, final Target< final CountDownLatch latch = new CountDownLatch(1); callOnMainThread(new Callable>() { @Override - public Target call() throws Exception { + public Target call() { builder.into(new Target() { @Override public void onStart() { @@ -189,7 +191,8 @@ public void onDestroy() { } @Override - public void onResourceReady(T resource, Transition transition) { + public void onResourceReady(@NonNull T resource, + @Nullable Transition transition) { target.onResourceReady(resource, transition); if (!Preconditions.checkNotNull(getRequest()).isRunning()) { latch.countDown(); @@ -215,12 +218,12 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) { } @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { target.getSize(cb); } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { target.removeCallback(cb); } @@ -253,7 +256,7 @@ public void run() { public void runOnMainThread(final Runnable runnable) { callOnMainThread(new Callable() { @Override - public Void call() throws Exception { + public Void call() { runnable.run(); return null; } @@ -291,22 +294,22 @@ private interface Waiter { private static void wait(Waiter waiter) { boolean isFinished = false; - do { - try { - try { - isFinished = waiter.await(TIMEOUT_SECONDS, TIMEOUT_UNIT); - if (!isFinished) { - throw new RuntimeException("Timed out while waiting"); - } - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } catch (RuntimeException e) { - if (Debug.isDebuggerConnected()) { - continue; - } - throw e; - } - } while (Debug.isDebuggerConnected() && !isFinished); + do { + try { + try { + isFinished = waiter.await(TIMEOUT_SECONDS, TIMEOUT_UNIT); + if (!isFinished) { + throw new RuntimeException("Timed out while waiting"); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } catch (RuntimeException e) { + if (Debug.isDebuggerConnected()) { + continue; + } + throw e; + } + } while (Debug.isDebuggerConnected() && !isFinished); } } diff --git a/library/src/main/java/com/bumptech/glide/ListPreloader.java b/library/src/main/java/com/bumptech/glide/ListPreloader.java index 806463d37c..b30952d12d 100644 --- a/library/src/main/java/com/bumptech/glide/ListPreloader.java +++ b/library/src/main/java/com/bumptech/glide/ListPreloader.java @@ -249,17 +249,18 @@ private static final class PreloadTarget extends BaseTarget { PreloadTarget() { } @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Do nothing. } @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { cb.onSizeReady(photoWidth, photoHeight); } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { // Do nothing because we don't retain references to SizeReadyCallbacks. } } diff --git a/library/src/main/java/com/bumptech/glide/RequestManager.java b/library/src/main/java/com/bumptech/glide/RequestManager.java index d4553d1c70..f2093dc5d2 100644 --- a/library/src/main/java/com/bumptech/glide/RequestManager.java +++ b/library/src/main/java/com/bumptech/glide/RequestManager.java @@ -671,7 +671,8 @@ private static class ClearTarget extends ViewTarget { } @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Do nothing. } } diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java index f34c11adc5..9c26b83dff 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java @@ -23,7 +23,7 @@ public LruResourceCache(long size) { } @Override - public void setResourceRemovedListener(ResourceRemovedListener listener) { + public void setResourceRemovedListener(@NonNull ResourceRemovedListener listener) { this.listener = listener; } @@ -35,8 +35,12 @@ protected void onItemEvicted(@NonNull Key key, @Nullable Resource item) { } @Override - protected int getSize(Resource item) { - return item.getSize(); + protected int getSize(@Nullable Resource item) { + if (item == null) { + return super.getSize(null); + } else { + return item.getSize(); + } } @SuppressLint("InlinedApi") diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCache.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCache.java index 654d1686be..3210e68216 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCache.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCache.java @@ -43,7 +43,7 @@ interface ResourceRemovedListener { * @param key The key. */ @Nullable - Resource remove(Key key); + Resource remove(@NonNull Key key); /** * Add bitmap to the cache with the given key. @@ -53,14 +53,14 @@ interface ResourceRemovedListener { * @return The old value of key (null if key is not in map). */ @Nullable - Resource put(Key key, Resource resource); + Resource put(@NonNull Key key, @Nullable Resource resource); /** * Set the listener to be called when a bitmap is removed from the cache. * * @param listener The listener. */ - void setResourceRemovedListener(ResourceRemovedListener listener); + void setResourceRemovedListener(@NonNull ResourceRemovedListener listener); /** * Evict all items from the memory cache. diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCacheAdapter.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCacheAdapter.java index bf1d0ef112..70deb5ce6d 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCacheAdapter.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/MemoryCacheAdapter.java @@ -1,5 +1,7 @@ package com.bumptech.glide.load.engine.cache; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.load.Key; import com.bumptech.glide.load.engine.Resource; @@ -25,19 +27,23 @@ public void setSizeMultiplier(float multiplier) { // Do nothing. } + @Nullable @Override - public Resource remove(Key key) { + public Resource remove(@NonNull Key key) { return null; } + @Nullable @Override - public Resource put(Key key, Resource resource) { - listener.onResourceRemoved(resource); + public Resource put(@NonNull Key key, @Nullable Resource resource) { + if (resource != null) { + listener.onResourceRemoved(resource); + } return null; } @Override - public void setResourceRemovedListener(ResourceRemovedListener listener) { + public void setResourceRemovedListener(@NonNull ResourceRemovedListener listener) { this.listener = listener; } diff --git a/library/src/main/java/com/bumptech/glide/load/model/Model.java b/library/src/main/java/com/bumptech/glide/load/model/Model.java index 3d610f1405..b82f75f553 100644 --- a/library/src/main/java/com/bumptech/glide/load/model/Model.java +++ b/library/src/main/java/com/bumptech/glide/load/model/Model.java @@ -1,5 +1,7 @@ package com.bumptech.glide.load.model; +import android.support.annotation.Nullable; + /** * An optional interface that models can implement to enhance control over Glide behaviors. */ @@ -24,5 +26,5 @@ public interface Model { * the same image. However two requests made with the different models are not exactly the * same because the way the image is loaded will differ. */ - boolean isEquivalentTo(Object other); + boolean isEquivalentTo(@Nullable Object other); } diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameLoader.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameLoader.java index f8a0442f7d..bee286b776 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameLoader.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameLoader.java @@ -8,6 +8,7 @@ import android.os.Looper; import android.os.Message; import android.os.SystemClock; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import com.bumptech.glide.Glide; @@ -322,7 +323,8 @@ Bitmap getResource() { } @Override - public void onResourceReady(Bitmap resource, Transition transition) { + public void onResourceReady(@NonNull Bitmap resource, + @Nullable Transition transition) { this.resource = resource; Message msg = handler.obtainMessage(FrameLoaderCallback.MSG_DELAY, this); handler.sendMessageAtTime(msg, targetTime); diff --git a/library/src/main/java/com/bumptech/glide/request/RequestFutureTarget.java b/library/src/main/java/com/bumptech/glide/request/RequestFutureTarget.java index 560548eeec..0acdf35aa7 100644 --- a/library/src/main/java/com/bumptech/glide/request/RequestFutureTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/RequestFutureTarget.java @@ -130,12 +130,12 @@ public R get(long time, @NonNull TimeUnit timeUnit) * A callback that should never be invoked directly. */ @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { cb.onSizeReady(width, height); } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { // Do nothing because we do not retain references to SizeReadyCallbacks. } @@ -154,7 +154,7 @@ public Request getRequest() { * A callback that should never be invoked directly. */ @Override - public void onLoadCleared(Drawable placeholder) { + public void onLoadCleared(@Nullable Drawable placeholder) { // Do nothing. } @@ -162,7 +162,7 @@ public void onLoadCleared(Drawable placeholder) { * A callback that should never be invoked directly. */ @Override - public void onLoadStarted(Drawable placeholder) { + public void onLoadStarted(@Nullable Drawable placeholder) { // Do nothing. } @@ -170,7 +170,7 @@ public void onLoadStarted(Drawable placeholder) { * A callback that should never be invoked directly. */ @Override - public synchronized void onLoadFailed(Drawable errorDrawable) { + public synchronized void onLoadFailed(@Nullable Drawable errorDrawable) { // Ignored, synchronized for backwards compatibility. } @@ -178,7 +178,8 @@ public synchronized void onLoadFailed(Drawable errorDrawable) { * A callback that should never be invoked directly. */ @Override - public synchronized void onResourceReady(R resource, Transition transition) { + public synchronized void onResourceReady(@NonNull R resource, + @Nullable Transition transition) { // Ignored, synchronized for backwards compatibility. } diff --git a/library/src/main/java/com/bumptech/glide/request/target/AppWidgetTarget.java b/library/src/main/java/com/bumptech/glide/request/target/AppWidgetTarget.java index aa50cd4c68..85ebda0de1 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/AppWidgetTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/AppWidgetTarget.java @@ -4,6 +4,8 @@ import android.content.ComponentName; import android.content.Context; import android.graphics.Bitmap; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.widget.RemoteViews; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.util.Preconditions; @@ -118,7 +120,8 @@ private void update() { } @Override - public void onResourceReady(Bitmap resource, Transition transition) { + public void onResourceReady(@NonNull Bitmap resource, + @Nullable Transition transition) { this.remoteViews.setImageViewBitmap(this.viewId, resource); this.update(); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java index 2210547f9d..6f270b9a66 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java @@ -2,6 +2,7 @@ import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.widget.ImageView; import com.bumptech.glide.request.transition.Transition; @@ -98,7 +99,7 @@ public void onLoadCleared(@Nullable Drawable placeholder) { } @Override - public void onResourceReady(Z resource, @Nullable Transition transition) { + public void onResourceReady(@NonNull Z resource, @Nullable Transition transition) { if (transition == null || !transition.transition(resource, this)) { setResourceInternal(resource); } else { diff --git a/library/src/main/java/com/bumptech/glide/request/target/NotificationTarget.java b/library/src/main/java/com/bumptech/glide/request/target/NotificationTarget.java index e30dca60f2..393b40cf1c 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/NotificationTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/NotificationTarget.java @@ -5,6 +5,8 @@ import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.widget.RemoteViews; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.util.Preconditions; @@ -31,12 +33,12 @@ public class NotificationTarget extends SimpleTarget { * Notification in order to update it that uses {@link #SIZE_ORIGINAL} as the target width and * height. * - * @param context Context to use in the AppWidgetManager initialization. - * @param viewId The id of the ImageView view that will load the image. - * @param remoteViews RemoteViews object which contains the ImageView that will load the - * bitmap. - * @param notification The Notification object that we want to update. - * @param notificationId The notificationId of the Notification that we want to load the Bitmap. + * @param context Context to use in the AppWidgetManager initialization. + * @param viewId The id of the ImageView view that will load the image. + * @param remoteViews RemoteViews object which contains the ImageView that will load the + * bitmap. + * @param notification The Notification object that we want to update. + * @param notificationId The notificationId of the Notification that we want to load the Bitmap. */ public NotificationTarget(Context context, int viewId, RemoteViews remoteViews, Notification notification, int notificationId) { @@ -60,7 +62,7 @@ public NotificationTarget(Context context, public NotificationTarget(Context context, int viewId, RemoteViews remoteViews, Notification notification, int notificationId, String notificationTag) { this(context, SIZE_ORIGINAL, SIZE_ORIGINAL, viewId, remoteViews, notification, notificationId, - notificationTag); + notificationTag); } /** @@ -105,7 +107,8 @@ private void update() { } @Override - public void onResourceReady(Bitmap resource, Transition transition) { + public void onResourceReady(@NonNull Bitmap resource, + @Nullable Transition transition) { this.remoteViews.setImageViewBitmap(this.viewId, resource); this.update(); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/PreloadTarget.java b/library/src/main/java/com/bumptech/glide/request/target/PreloadTarget.java index caab4ac195..90284f80c4 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/PreloadTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/PreloadTarget.java @@ -4,6 +4,8 @@ import android.os.Handler.Callback; import android.os.Looper; import android.os.Message; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.transition.Transition; @@ -45,7 +47,7 @@ private PreloadTarget(RequestManager requestManager, int width, int height) { } @Override - public void onResourceReady(Z resource, Transition transition) { + public void onResourceReady(@NonNull Z resource, @Nullable Transition transition) { HANDLER.obtainMessage(MESSAGE_CLEAR, this).sendToTarget(); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java b/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java index 06befead60..a3ebfba763 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/SimpleTarget.java @@ -1,6 +1,7 @@ package com.bumptech.glide.request.target; import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; import android.view.View; import com.bumptech.glide.util.Util; @@ -94,7 +95,7 @@ public SimpleTarget(int width, int height) { * @param cb {@inheritDoc} */ @Override - public final void getSize(SizeReadyCallback cb) { + public final void getSize(@NonNull SizeReadyCallback cb) { if (!Util.isValidDimensions(width, height)) { throw new IllegalArgumentException( "Width and height must both be > 0 or Target#SIZE_ORIGINAL, but given" + " width: " @@ -105,7 +106,7 @@ public final void getSize(SizeReadyCallback cb) { } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { // Do nothing, we never retain a reference to the callback. } } diff --git a/library/src/main/java/com/bumptech/glide/request/target/Target.java b/library/src/main/java/com/bumptech/glide/request/target/Target.java index dbdbc47798..3aa385ce5e 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/Target.java +++ b/library/src/main/java/com/bumptech/glide/request/target/Target.java @@ -1,6 +1,7 @@ package com.bumptech.glide.request.target; import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.bumptech.glide.manager.LifecycleListener; import com.bumptech.glide.request.Request; @@ -63,7 +64,7 @@ public interface Target extends LifecycleListener { * * @param resource the loaded resource. */ - void onResourceReady(R resource, Transition transition); + void onResourceReady(@NonNull R resource, @Nullable Transition transition); /** * A lifecycle callback that is called when a load is cancelled and its resources are freed. @@ -81,14 +82,14 @@ public interface Target extends LifecycleListener { * * @param cb The callback that must be called when the size of the target has been determined */ - void getSize(SizeReadyCallback cb); + void getSize(@NonNull SizeReadyCallback cb); /** * Removes the given callback from the pending set if it's still retained. * * @param cb The callback to remove. */ - void removeCallback(SizeReadyCallback cb); + void removeCallback(@NonNull SizeReadyCallback cb); /** * Sets the current request for this target to retain, should not be called outside of Glide. diff --git a/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java index 47743b9455..e017e209b3 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java @@ -4,6 +4,7 @@ import android.graphics.Point; import android.graphics.drawable.Drawable; import android.support.annotation.CallSuper; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.util.Log; @@ -56,7 +57,7 @@ public abstract class ViewTarget extends BaseTarget { /** * Constructor that defaults {@code waitForLayout} to {@code false}. */ - public ViewTarget(T view) { + public ViewTarget(@NonNull T view) { this.view = Preconditions.checkNotNull(view); sizeDeterminer = new SizeDeterminer(view); } @@ -76,7 +77,7 @@ public ViewTarget(T view) { // Public API. @SuppressWarnings("WeakerAccess") @Deprecated - public ViewTarget(T view, boolean waitForLayout) { + public ViewTarget(@NonNull T view, boolean waitForLayout) { this(view); if (waitForLayout) { waitForLayout(); @@ -102,6 +103,7 @@ public ViewTarget(T view, boolean waitForLayout) { * improving your memory usage in the cases you care about. */ // Public API. + @NonNull @SuppressWarnings({"UnusedReturnValue", "WeakerAccess"}) public final ViewTarget clearOnDetach() { if (attachStateListener != null) { @@ -149,6 +151,7 @@ public void onViewDetachedFromWindow(View v) { * still be used instead of the {@link View}'s dimensions even if this method is called. This * parameter is a fallback only. */ + @NonNull public final ViewTarget waitForLayout() { sizeDeterminer.waitForLayout = true; return this; @@ -182,6 +185,7 @@ private void maybeRemoveAttachStateListener() { /** * Returns the wrapped {@link android.view.View}. */ + @NonNull public T getView() { return view; } @@ -197,19 +201,19 @@ public T getView() { */ @CallSuper @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { sizeDeterminer.getSize(cb); } @CallSuper @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { sizeDeterminer.removeCallback(cb); } @CallSuper @Override - public void onLoadCleared(Drawable placeholder) { + public void onLoadCleared(@Nullable Drawable placeholder) { super.onLoadCleared(placeholder); sizeDeterminer.clearCallbacksAndListener(); @@ -299,11 +303,11 @@ private Object getTag() { // Public API. @SuppressWarnings("unused") public static void setTagId(int tagId) { - if (ViewTarget.tagId != null || isTagUsedAtLeastOnce) { - throw new IllegalArgumentException("You cannot set the tag id more than once or change" - + " the tag id after the first request has been made"); - } - ViewTarget.tagId = tagId; + if (ViewTarget.tagId != null || isTagUsedAtLeastOnce) { + throw new IllegalArgumentException("You cannot set the tag id more than once or change" + + " the tag id after the first request has been made"); + } + ViewTarget.tagId = tagId; } @VisibleForTesting @@ -319,12 +323,12 @@ static final class SizeDeterminer { @Nullable private SizeDeterminerLayoutListener layoutListener; - SizeDeterminer(View view) { + SizeDeterminer(@NonNull View view) { this.view = view; } // Use the maximum to avoid depending on the device's current orientation. - private static int getMaxDisplayLength(Context context) { + private static int getMaxDisplayLength(@NonNull Context context) { if (maxDisplayLength == null) { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); @@ -362,7 +366,7 @@ void checkCurrentDimens() { clearCallbacksAndListener(); } - void getSize(SizeReadyCallback cb) { + void getSize(@NonNull SizeReadyCallback cb) { int currentWidth = getTargetWidth(); int currentHeight = getTargetHeight(); if (isViewStateAndSizeValid(currentWidth, currentHeight)) { @@ -388,7 +392,7 @@ void getSize(SizeReadyCallback cb) { * *

See #2237. */ - void removeCallback(SizeReadyCallback cb) { + void removeCallback(@NonNull SizeReadyCallback cb) { cbs.remove(cb); } @@ -490,7 +494,7 @@ private static final class SizeDeterminerLayoutListener implements ViewTreeObserver.OnPreDrawListener { private final WeakReference sizeDeterminerRef; - SizeDeterminerLayoutListener(SizeDeterminer sizeDeterminer) { + SizeDeterminerLayoutListener(@NonNull SizeDeterminer sizeDeterminer) { sizeDeterminerRef = new WeakReference<>(sizeDeterminer); } diff --git a/library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java b/library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java index 8600873c96..e1b73aa342 100644 --- a/library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java +++ b/library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java @@ -1,6 +1,7 @@ package com.bumptech.glide.util; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -25,7 +26,8 @@ private ByteBufferUtil() { // Utility class. } - public static ByteBuffer fromFile(File file) throws IOException { + @NonNull + public static ByteBuffer fromFile(@NonNull File file) throws IOException { RandomAccessFile raf = null; FileChannel channel = null; try { @@ -60,7 +62,7 @@ public static ByteBuffer fromFile(File file) throws IOException { } } - public static void toFile(ByteBuffer buffer, File file) throws IOException { + public static void toFile(@NonNull ByteBuffer buffer, @NonNull File file) throws IOException { buffer.position(0); RandomAccessFile raf = null; FileChannel channel = null; @@ -89,7 +91,8 @@ public static void toFile(ByteBuffer buffer, File file) throws IOException { } } - public static void toStream(ByteBuffer byteBuffer, OutputStream os) throws IOException { + public static void toStream(@NonNull ByteBuffer byteBuffer, + @NonNull OutputStream os) throws IOException { SafeArray safeArray = getSafeArray(byteBuffer); if (safeArray != null) { os.write(safeArray.data, safeArray.offset, safeArray.offset + safeArray.limit); @@ -109,7 +112,8 @@ public static void toStream(ByteBuffer byteBuffer, OutputStream os) throws IOExc } } - public static byte[] toBytes(ByteBuffer byteBuffer) { + @NonNull + public static byte[] toBytes(@NonNull ByteBuffer byteBuffer) { final byte[] result; SafeArray safeArray = getSafeArray(byteBuffer); if (safeArray != null && safeArray.offset == 0 && safeArray.limit == safeArray.data.length) { @@ -123,11 +127,13 @@ public static byte[] toBytes(ByteBuffer byteBuffer) { return result; } - public static InputStream toStream(ByteBuffer buffer) { + @NonNull + public static InputStream toStream(@NonNull ByteBuffer buffer) { return new ByteBufferStream(buffer); } - public static ByteBuffer fromStream(InputStream stream) throws IOException { + @NonNull + public static ByteBuffer fromStream(@NonNull InputStream stream) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE); byte[] buffer = BUFFER_REF.getAndSet(null); @@ -148,7 +154,8 @@ public static ByteBuffer fromStream(InputStream stream) throws IOException { return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0); } - private static SafeArray getSafeArray(ByteBuffer byteBuffer) { + @Nullable + private static SafeArray getSafeArray(@NonNull ByteBuffer byteBuffer) { if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) { return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit()); } @@ -160,7 +167,7 @@ static final class SafeArray { @Synthetic final int limit; @Synthetic final byte[] data; - SafeArray(byte[] data, int offset, int limit) { + SafeArray(@NonNull byte[] data, int offset, int limit) { this.data = data; this.offset = offset; this.limit = limit; @@ -169,20 +176,20 @@ static final class SafeArray { private static class ByteBufferStream extends InputStream { private static final int UNSET = -1; - private final ByteBuffer byteBuffer; + @NonNull private final ByteBuffer byteBuffer; private int markPos = UNSET; - ByteBufferStream(ByteBuffer byteBuffer) { + ByteBufferStream(@NonNull ByteBuffer byteBuffer) { this.byteBuffer = byteBuffer; } @Override - public int available() throws IOException { + public int available() { return byteBuffer.remaining(); } @Override - public int read() throws IOException { + public int read() { if (!byteBuffer.hasRemaining()) { return -1; } @@ -200,7 +207,7 @@ public boolean markSupported() { } @Override - public int read(@NonNull byte[] buffer, int byteOffset, int byteCount) throws IOException { + public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { if (!byteBuffer.hasRemaining()) { return -1; } diff --git a/library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java b/library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java index 7fd555ade9..2b0283e03d 100644 --- a/library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java +++ b/library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java @@ -1,6 +1,7 @@ package com.bumptech.glide.util; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; import java.io.FilterInputStream; @@ -18,15 +19,18 @@ public final class ContentLengthInputStream extends FilterInputStream { private final long contentLength; private int readSoFar; - public static InputStream obtain(InputStream other, String contentLengthHeader) { + @NonNull + public static InputStream obtain(@NonNull InputStream other, + @Nullable String contentLengthHeader) { return obtain(other, parseContentLength(contentLengthHeader)); } - public static InputStream obtain(InputStream other, long contentLength) { + @NonNull + public static InputStream obtain(@NonNull InputStream other, long contentLength) { return new ContentLengthInputStream(other, contentLength); } - private static int parseContentLength(String contentLengthHeader) { + private static int parseContentLength(@Nullable String contentLengthHeader) { int result = UNKNOWN; if (!TextUtils.isEmpty(contentLengthHeader)) { try { @@ -40,7 +44,7 @@ private static int parseContentLength(String contentLengthHeader) { return result; } - private ContentLengthInputStream(InputStream in, long contentLength) { + private ContentLengthInputStream(@NonNull InputStream in, long contentLength) { super(in); this.contentLength = contentLength; } @@ -58,12 +62,12 @@ public synchronized int read() throws IOException { } @Override - public int read(@NonNull byte[] buffer) throws IOException { + public int read(byte[] buffer) throws IOException { return read(buffer, 0 /*byteOffset*/, buffer.length /*byteCount*/); } @Override - public synchronized int read(@NonNull byte[] buffer, int byteOffset, int byteCount) + public synchronized int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { return checkReadSoFarOrThrow(super.read(buffer, byteOffset, byteCount)); } diff --git a/library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java b/library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java index 8ef68ad7cd..3f5b05fb06 100644 --- a/library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java +++ b/library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java @@ -1,6 +1,7 @@ package com.bumptech.glide.util; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.util.Queue; @@ -20,7 +21,8 @@ public class ExceptionCatchingInputStream extends InputStream { private InputStream wrapped; private IOException exception; - public static ExceptionCatchingInputStream obtain(InputStream toWrap) { + @NonNull + public static ExceptionCatchingInputStream obtain(@NonNull InputStream toWrap) { ExceptionCatchingInputStream result; synchronized (QUEUE) { result = QUEUE.poll(); @@ -43,7 +45,7 @@ static void clearQueue() { // Do nothing. } - void setInputStream(InputStream toWrap) { + void setInputStream(@NonNull InputStream toWrap) { wrapped = toWrap; } @@ -68,7 +70,7 @@ public boolean markSupported() { } @Override - public int read(@NonNull byte[] buffer) throws IOException { + public int read(byte[] buffer) { int read; try { read = wrapped.read(buffer); @@ -80,7 +82,7 @@ public int read(@NonNull byte[] buffer) throws IOException { } @Override - public int read(@NonNull byte[] buffer, int byteOffset, int byteCount) throws IOException { + public int read(byte[] buffer, int byteOffset, int byteCount) { int read; try { read = wrapped.read(buffer, byteOffset, byteCount); @@ -97,7 +99,7 @@ public synchronized void reset() throws IOException { } @Override - public long skip(long byteCount) throws IOException { + public long skip(long byteCount) { long skipped; try { skipped = wrapped.skip(byteCount); @@ -109,7 +111,7 @@ public long skip(long byteCount) throws IOException { } @Override - public int read() throws IOException { + public int read() { int result; try { result = wrapped.read(); @@ -120,6 +122,7 @@ public int read() throws IOException { return result; } + @Nullable public IOException getException() { return exception; } diff --git a/library/src/main/java/com/bumptech/glide/util/LruCache.java b/library/src/main/java/com/bumptech/glide/util/LruCache.java index e106e578f9..5f7402aea2 100644 --- a/library/src/main/java/com/bumptech/glide/util/LruCache.java +++ b/library/src/main/java/com/bumptech/glide/util/LruCache.java @@ -53,7 +53,7 @@ public synchronized void setSizeMultiplier(float multiplier) { * * @param item The item to get the size of. */ - protected int getSize(Y item) { + protected int getSize(@Nullable Y item) { return 1; } @@ -94,7 +94,7 @@ public synchronized long getCurrentSize() { * @param key The key to check. */ - public synchronized boolean contains(T key) { + public synchronized boolean contains(@NonNull T key) { return cache.containsKey(key); } @@ -104,7 +104,7 @@ public synchronized boolean contains(T key) { * @param key The key to check. */ @Nullable - public synchronized Y get(T key) { + public synchronized Y get(@NonNull T key) { return cache.get(key); } @@ -119,17 +119,18 @@ public synchronized Y get(T key) { * @param key The key to add the item at. * @param item The item to add. */ - public synchronized Y put(T key, @Nullable Y item) { + @Nullable + public synchronized Y put(@NonNull T key, @Nullable Y item) { final int itemSize = getSize(item); if (itemSize >= maxSize) { onItemEvicted(key, item); return null; } - @Nullable final Y old = cache.put(key, item); if (item != null) { - currentSize += getSize(item); + currentSize += itemSize; } + @Nullable final Y old = cache.put(key, item); if (old != null) { currentSize -= getSize(old); @@ -148,7 +149,7 @@ public synchronized Y put(T key, @Nullable Y item) { * @param key The key to remove the item at. */ @Nullable - public synchronized Y remove(T key) { + public synchronized Y remove(@NonNull T key) { final Y value = cache.remove(key); if (value != null) { currentSize -= getSize(value); diff --git a/library/src/main/java/com/bumptech/glide/util/MarkEnforcingInputStream.java b/library/src/main/java/com/bumptech/glide/util/MarkEnforcingInputStream.java index 8d12be88f4..de2e5f1463 100644 --- a/library/src/main/java/com/bumptech/glide/util/MarkEnforcingInputStream.java +++ b/library/src/main/java/com/bumptech/glide/util/MarkEnforcingInputStream.java @@ -15,7 +15,7 @@ public class MarkEnforcingInputStream extends FilterInputStream { private int availableBytes = UNSET; - public MarkEnforcingInputStream(InputStream in) { + public MarkEnforcingInputStream(@NonNull InputStream in) { super(in); } @@ -37,7 +37,7 @@ public int read() throws IOException { } @Override - public int read(@NonNull byte[] buffer, int byteOffset, int byteCount) throws IOException { + public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { int toRead = (int) getBytesToRead(byteCount); if (toRead == END_OF_STREAM) { return END_OF_STREAM; diff --git a/library/src/main/java/com/bumptech/glide/util/MultiClassKey.java b/library/src/main/java/com/bumptech/glide/util/MultiClassKey.java index 18fd5e04e4..54fac73100 100644 --- a/library/src/main/java/com/bumptech/glide/util/MultiClassKey.java +++ b/library/src/main/java/com/bumptech/glide/util/MultiClassKey.java @@ -1,5 +1,8 @@ package com.bumptech.glide.util; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + /** * A key of two {@link Class}es to be used in hashed collections. */ @@ -13,19 +16,20 @@ public MultiClassKey() { // leave them null } - public MultiClassKey(Class first, Class second) { + public MultiClassKey(@NonNull Class first, @NonNull Class second) { set(first, second); } - public MultiClassKey(Class first, Class second, Class third) { + public MultiClassKey(@NonNull Class first, @NonNull Class second, + @Nullable Class third) { set(first, second, third); } - public void set(Class first, Class second) { + public void set(@NonNull Class first, @NonNull Class second) { set(first, second, null); } - public void set(Class first, Class second, Class third) { + public void set(@NonNull Class first, @NonNull Class second, @Nullable Class third) { this.first = first; this.second = second; this.third = third; diff --git a/library/src/main/java/com/bumptech/glide/util/Util.java b/library/src/main/java/com/bumptech/glide/util/Util.java index 3eccad550a..7e294e2c79 100644 --- a/library/src/main/java/com/bumptech/glide/util/Util.java +++ b/library/src/main/java/com/bumptech/glide/util/Util.java @@ -4,6 +4,8 @@ import android.graphics.Bitmap; import android.os.Build; import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.load.model.Model; import com.bumptech.glide.request.target.Target; import java.util.ArrayDeque; @@ -29,7 +31,8 @@ private Util() { /** * Returns the hex string of the given byte array representing a SHA256 hash. */ - public static String sha256BytesToHex(byte[] bytes) { + @NonNull + public static String sha256BytesToHex(@NonNull byte[] bytes) { synchronized (SHA_256_CHARS) { return bytesToHex(bytes, SHA_256_CHARS); } @@ -39,7 +42,8 @@ public static String sha256BytesToHex(byte[] bytes) { // http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java // /9655275#9655275 @SuppressWarnings("PMD.UseVarargs") - private static String bytesToHex(byte[] bytes, char[] hexChars) { + @NonNull + private static String bytesToHex(@NonNull byte[] bytes, @NonNull char[] hexChars) { int v; for (int j = 0; j < bytes.length; j++) { v = bytes[j] & 0xFF; @@ -57,7 +61,7 @@ private static String bytesToHex(byte[] bytes, char[] hexChars) { * removed in Glide 4.0. */ @Deprecated - public static int getSize(Bitmap bitmap) { + public static int getSize(@NonNull Bitmap bitmap) { return getBitmapByteSize(bitmap); } @@ -65,7 +69,7 @@ public static int getSize(Bitmap bitmap) { * Returns the in memory size of the given {@link Bitmap} in bytes. */ @TargetApi(Build.VERSION_CODES.KITKAT) - public static int getBitmapByteSize(Bitmap bitmap) { + public static int getBitmapByteSize(@NonNull Bitmap bitmap) { // The return value of getAllocationByteCount silently changes for recycled bitmaps from the // internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we // instead assert here. @@ -88,11 +92,11 @@ public static int getBitmapByteSize(Bitmap bitmap) { * Returns the in memory size of {@link android.graphics.Bitmap} with the given width, height, and * {@link android.graphics.Bitmap.Config}. */ - public static int getBitmapByteSize(int width, int height, Bitmap.Config config) { + public static int getBitmapByteSize(int width, int height, @Nullable Bitmap.Config config) { return width * height * getBytesPerPixel(config); } - private static int getBytesPerPixel(Bitmap.Config config) { + private static int getBytesPerPixel(@Nullable Bitmap.Config config) { // A bitmap by decoding a GIF has null "config" in certain environments. if (config == null) { config = Bitmap.Config.ARGB_8888; @@ -162,6 +166,7 @@ public static boolean isOnBackgroundThread() { /** * Creates a {@link java.util.Queue} of the given size using Glide's preferred implementation. */ + @NonNull public static Queue createQueue(int size) { return new ArrayDeque<>(size); } @@ -172,8 +177,9 @@ public static Queue createQueue(int size) { * *

See #303, #375, #322, #2262. */ + @NonNull @SuppressWarnings("UseBulkOperation") - public static List getSnapshot(Collection other) { + public static List getSnapshot(@NonNull Collection other) { // toArray creates a new ArrayList internally and does not guarantee that the values it contains // are non-null. Collections.addAll in ArrayList uses toArray internally and therefore also // doesn't guarantee that entries are non-null. WeakHashMap's iterator does avoid returning null @@ -192,11 +198,11 @@ public static List getSnapshot(Collection other) { * * @see java.util.Objects#equals */ - public static boolean bothNullOrEqual(Object a, Object b) { + public static boolean bothNullOrEqual(@Nullable Object a, @Nullable Object b) { return a == null ? b == null : a.equals(b); } - public static boolean bothModelsNullEquivalentOrEquals(Object a, Object b) { + public static boolean bothModelsNullEquivalentOrEquals(@Nullable Object a, @Nullable Object b) { if (a == null) { return b == null; } @@ -222,7 +228,7 @@ public static int hashCode(float value, int accumulator) { return hashCode(Float.floatToIntBits(value), accumulator); } - public static int hashCode(Object object, int accumulator) { + public static int hashCode(@Nullable Object object, int accumulator) { return hashCode(object == null ? 0 : object.hashCode(), accumulator); } @@ -233,5 +239,4 @@ public static int hashCode(boolean value, int accumulator) { public static int hashCode(boolean value) { return hashCode(value, HASH_ACCUMULATOR); } - } diff --git a/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java b/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java index 4a85618faf..90580d5447 100644 --- a/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java +++ b/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java @@ -41,8 +41,8 @@ public ViewPreloadSizeProvider() { */ // Public API. @SuppressWarnings("WeakerAccess") - public ViewPreloadSizeProvider(View view) { - this.viewTarget = new SizeViewTarget(view, this); + public ViewPreloadSizeProvider(@NonNull View view) { + viewTarget = new SizeViewTarget(view, this); } @Nullable @@ -51,13 +51,13 @@ public int[] getPreloadSize(@NonNull T item, int adapterPosition, int itemPositi if (size == null) { return null; } else { - return Arrays.copyOf(this.size, this.size.length); + return Arrays.copyOf(size, size.length); } } @Override public void onSizeReady(int width, int height) { - this.size = new int[] { width, height }; + size = new int[]{width, height}; viewTarget = null; } @@ -70,21 +70,22 @@ public void onSizeReady(int width, int height) { * @param view A not null View the size will be extracted async with an {@link * android.view.ViewTreeObserver .OnPreDrawListener} */ - public void setView(View view) { - if (this.size != null || viewTarget != null) { + public void setView(@NonNull View view) { + if (size != null || viewTarget != null) { return; } - this.viewTarget = new SizeViewTarget(view, this); + viewTarget = new SizeViewTarget(view, this); } private static final class SizeViewTarget extends ViewTarget { - SizeViewTarget(View view, SizeReadyCallback callback) { + SizeViewTarget(@NonNull View view, @NonNull SizeReadyCallback callback) { super(view); getSize(callback); } @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Do nothing } } diff --git a/library/src/test/java/com/bumptech/glide/GlideTest.java b/library/src/test/java/com/bumptech/glide/GlideTest.java index f85f1692f6..68ee3870f9 100644 --- a/library/src/test/java/com/bumptech/glide/GlideTest.java +++ b/library/src/test/java/com/bumptech/glide/GlideTest.java @@ -29,6 +29,8 @@ import android.net.Uri; import android.os.Handler; import android.os.ParcelFileDescriptor; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.view.ViewGroup; import android.widget.ImageView; import com.bumptech.glide.load.DataSource; @@ -62,7 +64,6 @@ import com.bumptech.glide.util.Preconditions; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; @@ -118,11 +119,11 @@ public class GlideTest { private Context context; @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); context = RuntimeEnvironment.application; - // Run all tasks on the main thread so they complete synchronously. + // Run all tasks on the main thread so they complete synchronously. GlideExecutor executor = MockGlideExecutor.newMainThreadExecutor(); when(diskCacheFactory.build()).thenReturn(diskCache); Glide.init( @@ -149,7 +150,7 @@ public void setUp() throws Exception { when(bgHandler.post(isA(Runnable.class))).thenAnswer(new Answer() { @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { + public Boolean answer(InvocationOnMock invocation) { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return true; @@ -249,13 +250,13 @@ public void testTrimMemory() { } @Test - public void testFileDefaultLoaderWithInputStream() throws Exception { + public void testFileDefaultLoaderWithInputStream() { registerFailFactory(File.class, ParcelFileDescriptor.class); runTestFileDefaultLoader(); } @Test - public void testFileDefaultLoaderWithFileDescriptor() throws Exception { + public void testFileDefaultLoaderWithFileDescriptor() { registerFailFactory(File.class, InputStream.class); runTestFileDefaultLoader(); } @@ -353,13 +354,13 @@ public void testLoadColorDrawable_withNonUnitBitmapTransformation_returnsBitmapD } @Test - public void testUriDefaultLoaderWithInputStream() throws Exception { + public void testUriDefaultLoaderWithInputStream() { registerFailFactory(Uri.class, ParcelFileDescriptor.class); runTestUriDefaultLoader(); } @Test - public void testUriDefaultLoaderWithFileDescriptor() throws Exception { + public void testUriDefaultLoaderWithFileDescriptor() { registerFailFactory(Uri.class, InputStream.class); runTestUriDefaultLoader(); } @@ -388,13 +389,13 @@ public void testStringDefaultLoaderWithUrl() { } @Test - public void testFileStringDefaultLoaderWithInputStream() throws Exception { + public void testFileStringDefaultLoaderWithInputStream() { registerFailFactory(String.class, ParcelFileDescriptor.class); runTestFileStringDefaultLoader(); } @Test - public void testFileStringDefaultLoaderWithFileDescriptor() throws Exception { + public void testFileStringDefaultLoaderWithFileDescriptor() { registerFailFactory(String.class, ParcelFileDescriptor.class); runTestFileStringDefaultLoader(); } @@ -411,13 +412,13 @@ private void runTestFileStringDefaultLoader() { } @Test - public void testUriStringDefaultLoaderWithInputStream() throws Exception { + public void testUriStringDefaultLoaderWithInputStream() { registerFailFactory(String.class, ParcelFileDescriptor.class); runTestUriStringDefaultLoader(); } @Test - public void testUriStringDefaultLoaderWithFileDescriptor() throws Exception { + public void testUriStringDefaultLoaderWithFileDescriptor() { registerFailFactory(String.class, InputStream.class); runTestUriStringDefaultLoader(); } @@ -456,13 +457,13 @@ public boolean onResourceReady(Drawable resource, Object model, Target } @Test - public void testIntegerDefaultLoaderWithInputStream() throws Exception { + public void testIntegerDefaultLoaderWithInputStream() { registerFailFactory(Integer.class, ParcelFileDescriptor.class); runTestIntegerDefaultLoader(); } @Test - public void testIntegerDefaultLoaderWithFileDescriptor() throws Exception { + public void testIntegerDefaultLoaderWithFileDescriptor() { registerFailFactory(Integer.class, InputStream.class); runTestIntegerDefaultLoader(); } @@ -513,7 +514,7 @@ public void testNonDefaultModelWithRegisteredFactoryDoesNotThrow() { } @Test - public void testReceivesGif() throws IOException { + public void testReceivesGif() { String fakeUri = "content://fake"; InputStream testGifData = openGif(); mockUri(Uri.parse(fakeUri), testGifData); @@ -524,7 +525,7 @@ public void testReceivesGif() throws IOException { } @Test - public void testReceivesGifBytes() throws IOException { + public void testReceivesGifBytes() { String fakeUri = "content://fake"; InputStream testGifData = openGif(); mockUri(Uri.parse(fakeUri), testGifData); @@ -686,7 +687,8 @@ public void testByteData() { public void removeFromManagers_afterRequestManagerRemoved_clearsRequest() { target = requestManager.load(mockUri("content://uri")).into(new SimpleTarget() { @Override - public void onResourceReady(Drawable resource, Transition transition) { + public void onResourceReady(@NonNull Drawable resource, + @Nullable Transition transition) { // Do nothing. } }); @@ -700,7 +702,7 @@ public void onResourceReady(Drawable resource, Transition tran } @Test - public void testClone() throws IOException { + public void testClone() { Target firstTarget = mock(Target.class); doAnswer(new CallSizeReady(100, 100)).when(firstTarget).getSize(isA(SizeReadyCallback.class)); Target secondTarget = mock(Target.class); @@ -813,7 +815,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { } private static void registerMockModelLoader(Class modelClass, Class dataClass, - Y loadedData, Registry registry) { + Y loadedData, Registry registry) { DataFetcher mockStreamFetcher = mock(DataFetcher.class); when(mockStreamFetcher.getDataClass()).thenReturn(dataClass); try { diff --git a/library/src/test/java/com/bumptech/glide/RequestManagerTest.java b/library/src/test/java/com/bumptech/glide/RequestManagerTest.java index 5336e7c3ad..f033791816 100644 --- a/library/src/test/java/com/bumptech/glide/RequestManagerTest.java +++ b/library/src/test/java/com/bumptech/glide/RequestManagerTest.java @@ -13,6 +13,8 @@ import android.app.Application; import android.content.Context; import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.manager.ConnectivityMonitor; import com.bumptech.glide.manager.ConnectivityMonitor.ConnectivityListener; import com.bumptech.glide.manager.ConnectivityMonitorFactory; @@ -65,25 +67,28 @@ public void setUp() { when(factory.build(isA(Context.class), isA(ConnectivityMonitor.ConnectivityListener.class))) .thenAnswer(new Answer() { @Override - public ConnectivityMonitor answer(InvocationOnMock invocation) throws Throwable { + public ConnectivityMonitor answer(InvocationOnMock invocation) { connectivityListener = (ConnectivityListener) invocation.getArguments()[1]; return connectivityMonitor; } }); - target = new BaseTarget() { - @Override - public void onResourceReady(Drawable resource, Transition transition) { - // Empty. - } - @Override - public void getSize(SizeReadyCallback cb) { - // Empty. - } - @Override - public void removeCallback(SizeReadyCallback cb) { - // Empty. - } + target = new BaseTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, + @Nullable Transition transition) { + // Empty. + } + + @Override + public void getSize(@NonNull SizeReadyCallback cb) { + // Empty. + } + + @Override + public void removeCallback(@NonNull SizeReadyCallback cb) { + // Empty. + } }; requestTracker = mock(RequestTracker.class); @@ -194,7 +199,7 @@ public Set getDescendants() { return Collections.emptySet(); } }, context); - final RequestManager child2 = new RequestManager(Glide.get(context), lifecycle, + final RequestManager child2 = new RequestManager(Glide.get(context), lifecycle, new RequestManagerTreeNode() { @Override public Set getDescendants() { diff --git a/library/src/test/java/com/bumptech/glide/load/engine/cache/LruCacheTest.java b/library/src/test/java/com/bumptech/glide/load/engine/cache/LruCacheTest.java index 2a46e27fc2..a1c3f2d555 100644 --- a/library/src/test/java/com/bumptech/glide/load/engine/cache/LruCacheTest.java +++ b/library/src/test/java/com/bumptech/glide/load/engine/cache/LruCacheTest.java @@ -31,7 +31,7 @@ public class LruCacheTest { private String currentKey; @Before - public void setUp() throws Exception { + public void setUp() { currentKey = ""; listener = mock(CacheListener.class); cache = new TestLruCache(SIZE, listener); @@ -379,7 +379,7 @@ protected void onItemEvicted(@NonNull String key, @Nullable Object item) { } @Override - protected int getSize(Object item) { + protected int getSize(@Nullable Object item) { return listener.getSize(item); } } diff --git a/library/src/test/java/com/bumptech/glide/request/SingleRequestTest.java b/library/src/test/java/com/bumptech/glide/request/SingleRequestTest.java index 9f91a107ee..b415962507 100644 --- a/library/src/test/java/com/bumptech/glide/request/SingleRequestTest.java +++ b/library/src/test/java/com/bumptech/glide/request/SingleRequestTest.java @@ -21,6 +21,7 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.GlideContext; import com.bumptech.glide.Priority; import com.bumptech.glide.load.DataSource; @@ -210,7 +211,7 @@ public void testResourceIsNotCompleteWhenAskingCoordinatorIfCanSetImage() { RequestCoordinator requestCoordinator = mock(RequestCoordinator.class); doAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { + public Object answer(InvocationOnMock invocation) { Request request = (Request) invocation.getArguments()[0]; assertFalse(request.isComplete()); return true; @@ -571,7 +572,7 @@ public void testRequestListenerIsCalledWithLoadedFromMemoryIfLoadCompletesSynchr any(ResourceCallback.class))) .thenAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { + public Object answer(InvocationOnMock invocation) { request.onResourceReady(builder.resource, DataSource.MEMORY_CACHE); return null; } @@ -1090,41 +1091,43 @@ private static class MockTarget implements Target { private Drawable currentPlaceholder; @Override - public void onLoadCleared(Drawable placeholder) { + public void onLoadCleared(@Nullable Drawable placeholder) { currentPlaceholder = placeholder; } @Override - public void onLoadStarted(Drawable placeholder) { + public void onLoadStarted(@Nullable Drawable placeholder) { currentPlaceholder = placeholder; } @Override - public void onLoadFailed(Drawable errorDrawable) { + public void onLoadFailed(@Nullable Drawable errorDrawable) { currentPlaceholder = errorDrawable; } @Override - public void onResourceReady(List resource, Transition transition) { + public void onResourceReady(@NonNull List resource, + @Nullable Transition transition) { currentPlaceholder = null; } @Override - public void getSize(SizeReadyCallback cb) { + public void getSize(@NonNull SizeReadyCallback cb) { } @Override - public void removeCallback(SizeReadyCallback cb) { + public void removeCallback(@NonNull SizeReadyCallback cb) { // Do nothing. } @Override - public void setRequest(Request request) { + public void setRequest(@Nullable Request request) { } + @Nullable @Override public Request getRequest() { return null; diff --git a/library/src/test/java/com/bumptech/glide/request/target/SimpleTargetTest.java b/library/src/test/java/com/bumptech/glide/request/target/SimpleTargetTest.java index c4fe4d5ab3..3093df1fbc 100644 --- a/library/src/test/java/com/bumptech/glide/request/target/SimpleTargetTest.java +++ b/library/src/test/java/com/bumptech/glide/request/target/SimpleTargetTest.java @@ -2,6 +2,8 @@ import static org.mockito.Mockito.mock; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.request.transition.Transition; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,7 +36,8 @@ public void testThrowsOnGetSizeIfGivenHeightIsEqualToZero() { public void testCanBeConstructedWithoutDimensions() { new SimpleTarget() { @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Do nothing. } }; @@ -53,7 +56,8 @@ public void testGetSizeDoesNotThrowWithSizeOriginal() { private SimpleTarget getTarget(int width, int height) { return new SimpleTarget(width, height) { @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Do nothing. } }; diff --git a/library/src/test/java/com/bumptech/glide/request/target/ViewTargetTest.java b/library/src/test/java/com/bumptech/glide/request/target/ViewTargetTest.java index 6a44b1aa4a..ea32faba28 100644 --- a/library/src/test/java/com/bumptech/glide/request/target/ViewTargetTest.java +++ b/library/src/test/java/com/bumptech/glide/request/target/ViewTargetTest.java @@ -17,6 +17,7 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Build; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.View; import android.view.View.OnAttachStateChangeListener; @@ -789,7 +790,8 @@ private static final class AttachStateTarget extends ViewTarget { } @Override - public void onResourceReady(Object resource, Transition transition) { } + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { } } private static final class TestViewTarget extends ViewTarget { @@ -799,12 +801,13 @@ private static final class TestViewTarget extends ViewTarget { } @Override - public void onResourceReady(Object resource, Transition transition) { + public void onResourceReady(@NonNull Object resource, + @Nullable Transition transition) { // Avoid calling super. } @Override - public void onLoadCleared(Drawable placeholder) { + public void onLoadCleared(@Nullable Drawable placeholder) { // Avoid calling super. }