package epson.print.imgsel;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import epson.print.imgsel.ImageFinder;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Collection;

class FolderFindTask extends AsyncTask<ImageFinder, Object, Void> implements ImageFinder.Canceller {
    private Context mApplicationContext;
    private WeakReference<ImageFolderListFragment> mFragmentReference;

    public FolderFindTask(ImageFolderListFragment imageFolderListFragment) {
        this.mFragmentReference = new WeakReference<>(imageFolderListFragment);
        this.mApplicationContext = imageFolderListFragment.getActivity().getApplicationContext();
    }

    protected void onPostExecute(Void voidR) {
        ImageFolderListFragment imageFolderListFragment = (ImageFolderListFragment) this.mFragmentReference.get();
        if (imageFolderListFragment != null) {
            imageFolderListFragment.onFolderFindTaskEnd();
        }
    }

    protected Void doInBackground(ImageFinder... imageFinderArr) {
        Collection<ImageFolderInfo> folderPhotoList;
        if (!isCancelled() && imageFinderArr != null && imageFinderArr.length > 0 && (folderPhotoList = imageFinderArr[0].getFolderPhotoList(this, this.mApplicationContext.getContentResolver())) != null) {
            for (ImageFolderInfo next : folderPhotoList) {
                if (isCancelled()) {
                    return null;
                }
                taskAddItem(next);
            }
        }
        return null;
    }

    private void localSleep(int i) {
        try {
            Thread.sleep((long) i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void taskAddItem(ImageFolderInfo imageFolderInfo) {
        Bitmap bitmap;
        try {
            bitmap = AltThumbnailCache.createThumbnail(imageFolderInfo.getFileName(0), this.mApplicationContext.getContentResolver(), imageFolderInfo.getId(0));
        } catch (IOException e) {
            e.printStackTrace();
            bitmap = null;
        }
        publishProgress(new Object[]{imageFolderInfo, bitmap});
    }

    protected void onProgressUpdate(Object... objArr) {
        ImageFolderListFragment imageFolderListFragment;
        if (!isCancelled() && (imageFolderListFragment = (ImageFolderListFragment) this.mFragmentReference.get()) != null) {
            imageFolderListFragment.addItem(objArr[0], objArr[1]);
        }
    }

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