package epson.print.imgsel;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.os.AsyncTask;

import epson.print.ImageItem;
import epson.print.Util.EPLog;
import epson.print.imgsel.ImageFinder;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.LinkedList;

public class ImageThumbnailTask extends AsyncTask<Void, ImageItem, Void> implements ImageFinder.Canceller {
    private static final int RETRY_CHECKLIST = 100;
    private static final int RETRY_SUSPEND = 1000;
    private static final String TAG = "ImageThumbnailTask";
    private volatile boolean finish = false;
    private volatile LinkedList<ImageItem> imageItems;
    WeakReference<ImageGridFragment> mFragmentReference;
    private volatile boolean suspend = false;

    public ImageThumbnailTask(ImageGridFragment imageGridFragment) {
        this.mFragmentReference = new WeakReference<>(imageGridFragment);
        this.imageItems = new LinkedList<>();
    }

    protected Void doInBackground(Void... voidArr) {
        Bitmap bitmap;
        EPLog.d(TAG, "Enter doInBackground");
        ContentResolver resolver = getResolver();
        while (true) {
            ImageItem first = getFirst();
            if ((first != null || !this.finish) && !isCancelled()) {
                if (first == null) {
                    try {
                        EPLog.d(TAG, "Wait doInBackground");
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        EPLog.d(TAG, "createThumbnail " + first.getPath());
                        bitmap = AltThumbnailCache.createThumbnail(first.getPath(), resolver, first.getDatabaseId());
                    } catch (IOException e2) {
                        e2.printStackTrace();
                        bitmap = null;
                    }
                    publishProgress(new ImageItem[]{new ImageItem(bitmap, 0, first.getPath(), first.getDatabaseId())});
                    if (this.suspend) {
                        EPLog.d(TAG, "Enter suspend");
                        while (this.suspend && !isCancelled()) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException unused) {
                            }
                        }
                        EPLog.d(TAG, "Leave suspend");
                    }
                }
            }
        }
        EPLog.d(TAG, "Leave doInBackground");
        return null;
    }

    protected void onProgressUpdate(ImageItem... imageItemArr) {
        ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
        if (imageGridFragment != null && imageItemArr != null && imageItemArr.length > 0) {
            imageGridFragment.updateItem(imageItemArr[0]);
        }
    }

    private synchronized ImageItem getFirst() {
        if (this.imageItems != null) {
            if (this.imageItems.size() > 0) {
                return this.imageItems.remove(0);
            }
        }
        return null;
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0037, code lost:
        return;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized void moveToFirst(epson.print.ImageItem r5) {
        /*
            r4 = this;
            monitor-enter(r4)
            java.util.LinkedList<epson.print.ImageItem> r0 = r4.imageItems     // Catch:{ all -> 0x0038 }
            if (r0 != 0) goto L_0x0007
            monitor-exit(r4)
            return
        L_0x0007:
            java.util.LinkedList<epson.print.ImageItem> r0 = r4.imageItems     // Catch:{ all -> 0x0038 }
            int r0 = r0.indexOf(r5)     // Catch:{ all -> 0x0038 }
            r1 = -1
            if (r0 != r1) goto L_0x0011
            goto L_0x0036
        L_0x0011:
            java.lang.String r1 = "ImageThumbnailTask"
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch:{ all -> 0x0038 }
            r2.<init>()     // Catch:{ all -> 0x0038 }
            java.lang.String r3 = "moveToFirst "
            r2.append(r3)     // Catch:{ all -> 0x0038 }
            java.lang.String r5 = r5.getPath()     // Catch:{ all -> 0x0038 }
            r2.append(r5)     // Catch:{ all -> 0x0038 }
            java.lang.String r5 = r2.toString()     // Catch:{ all -> 0x0038 }
            epson.print.Util.EPLog.d(r1, r5)     // Catch:{ all -> 0x0038 }
            java.util.LinkedList<epson.print.ImageItem> r5 = r4.imageItems     // Catch:{ all -> 0x0038 }
            java.util.LinkedList<epson.print.ImageItem> r1 = r4.imageItems     // Catch:{ all -> 0x0038 }
            java.lang.Object r0 = r1.remove(r0)     // Catch:{ all -> 0x0038 }
            r5.addFirst(r0)     // Catch:{ all -> 0x0038 }
        L_0x0036:
            monitor-exit(r4)
            return
        L_0x0038:
            r5 = move-exception
            monitor-exit(r4)
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.ImageThumbnailTask.moveToFirst(epson.print.ImageItem):void");
    }

    private synchronized void addLast(ImageItem imageItem) {
        if (this.imageItems != null) {
            this.imageItems.addLast(imageItem);
        }
    }

    public void setFinish(boolean z) {
        this.finish = z;
    }

    public void setSuspend(boolean z) {
        this.suspend = z;
    }

    private ContentResolver getResolver() {
        FragmentActivity activity;
        ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
        if (imageGridFragment == null || (activity = imageGridFragment.getActivity()) == null) {
            return null;
        }
        return activity.getContentResolver();
    }

    public boolean checkCanceled() {
        return isCancelled();
    }
}
