Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-budiyev committed Mar 1, 2018
1 parent 2b64632 commit c2f2203
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import android.graphics.Bitmap;
import android.support.annotation.AnyThread;
Expand All @@ -44,9 +45,9 @@ abstract class BaseLoadImageAction<T> implements Callable<Void> {
private final ExecutorService mCacheExecutor;
private final LoadCallback mLoadCallback;
private final ErrorCallback mErrorCallback;
private final AtomicBoolean mCancelled = new AtomicBoolean();
private volatile Future<?> mLoadFuture;
private volatile CacheImageAction mCacheAction;
private volatile boolean mCancelled;

protected BaseLoadImageAction(@NonNull DataDescriptor<T> descriptor, @NonNull BitmapLoader<T> bitmapLoader,
@Nullable Size requiredSize, @Nullable BitmapTransformation transformation,
Expand Down Expand Up @@ -76,27 +77,29 @@ protected BaseLoadImageAction(@NonNull DataDescriptor<T> descriptor, @NonNull Bi

@AnyThread
public final void submit(@NonNull ExecutorService executor) {
if (mCancelled) {
if (isCancelled()) {
return;
}
mLoadFuture = executor.submit(this);
}

@AnyThread
public final void cancel() {
if (mCancelled) {
return;
}
mCancelled = true;
Future<?> loadFuture = mLoadFuture;
if (loadFuture != null) {
loadFuture.cancel(false);
}
CacheImageAction cacheAction = mCacheAction;
if (cacheAction != null) {
cacheAction.cancel();
if (mCancelled.compareAndSet(false, true)) {
Future<?> loadFuture = mLoadFuture;
if (loadFuture != null) {
loadFuture.cancel(false);
}
CacheImageAction cacheAction = mCacheAction;
if (cacheAction != null) {
cacheAction.cancel();
}
onCancelled();
}
onCancelled();
}

public final boolean isCancelled() {
return mCancelled.get();
}

@Override
Expand Down Expand Up @@ -151,20 +154,16 @@ protected final ErrorCallback getErrorCallback() {
return mErrorCallback;
}

protected final boolean isCancelled() {
return mCancelled;
}

@WorkerThread
protected final void loadImage() {
while (!mCancelled && !mPauseLock.shouldInterruptEarly() && mPauseLock.isPaused()) {
while (!isCancelled() && !mPauseLock.shouldInterruptEarly() && mPauseLock.isPaused()) {
try {
mPauseLock.await();
} catch (InterruptedException e) {
return;
}
}
if (mCancelled || mPauseLock.shouldInterruptEarly()) {
if (isCancelled() || mPauseLock.shouldInterruptEarly()) {
return;
}
DataDescriptor<T> descriptor = mDescriptor;
Expand All @@ -180,7 +179,7 @@ protected final void loadImage() {
return;
}
}
if (mCancelled) {
if (isCancelled()) {
return;
}
// Storage cache
Expand All @@ -195,7 +194,7 @@ protected final void loadImage() {
return;
}
}
if (mCancelled) {
if (isCancelled()) {
return;
}
// Load new image
Expand All @@ -210,6 +209,9 @@ protected final void loadImage() {
processError(new ImageNotLoadedException());
return;
}
if (isCancelled()) {
return;
}
// Transform image
BitmapTransformation transformation = mTransformation;
if (transformation != null) {
Expand All @@ -224,10 +226,10 @@ protected final void loadImage() {
return;
}
}
if (mCancelled) {
processImage(image);
if (isCancelled()) {
return;
}
processImage(image);
if (key != null) {
if (memoryCache != null) {
memoryCache.put(key, image);
Expand All @@ -246,6 +248,9 @@ protected final void loadImage() {

@WorkerThread
private void processImage(@NonNull Bitmap image) {
if (isCancelled()) {
return;
}
LoadCallback loadCallback = mLoadCallback;
if (loadCallback != null) {
loadCallback.onLoaded(image);
Expand All @@ -255,7 +260,7 @@ private void processImage(@NonNull Bitmap image) {

@WorkerThread
private void processError(@NonNull Throwable error) {
if (mCancelled) {
if (isCancelled()) {
return;
}
ErrorCallback errorCallback = mErrorCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean hasSameKey(@Nullable String key) {

@Override
protected void onImageLoaded(@NonNull Bitmap image) {
if (isCancelled() || mView.get() == null || mResources.get() == null) {
if (mView.get() == null || mResources.get() == null) {
return;
}
mMainThreadHandler.post(new SetImageAction(image));
Expand Down

0 comments on commit c2f2203

Please sign in to comment.