// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/module/main/MainView.java
// public interface MainView {
//     void notifyDataSetChanged();
// 
//     void initView();
// 
//     void makeCommonSnake(String message);
// 
//     void setProgressBarVisible(boolean visible);
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/task/ScanHandler.java
// public abstract class ScanHandler {
//     public static final String SUFFIX_JPG = "jpg";
//     private int taskCount;
// 
//     private FileFilter fileFilter;
// 
//     private long mStartTime=Long.MIN_VALUE;
// 
//     public long getStartTime() {
//         return mStartTime;
//     }
// 
//     public ScanHandler() {
//         fileFilter=new FileFilterBySuffix(SUFFIX_JPG);
//         mStartTime=System.currentTimeMillis();
//     }
// 
//     public FileFilter getFileFilter() {
//         return fileFilter;
//     }
// 
//     public abstract void onFinish(long finishTime);
// 
//     public synchronized void addTaskCount() {
//         taskCount += 1;
//     }
// 
//     public synchronized int devideTaskCount() {
//         taskCount -= 1;
//         return taskCount;
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/task/ScanImageFileTask.java
// public class ScanImageFileTask implements Runnable {
// 
//     private File mDirectory;
//     private ExecutorService mExecutor;
//     private List<File> mResultFileSet;
//     private ScanHandler mHandler;
// 
//     public ScanImageFileTask(File directory, ExecutorService executor, List<File> resultFileSet, ScanHandler handler) {
//         this.mHandler = handler;
//         handler.addTaskCount();
//         this.mDirectory = directory;
//         this.mExecutor = executor;
//         this.mResultFileSet = resultFileSet;
//     }
// 
//     @Override
//     public void run() {
//         File[] files = mDirectory.listFiles();
//         if (files != null) {
//             for (File file : files) {
//                 if (file.isDirectory()) {
//                     mExecutor.execute(new ScanImageFileTask(file, mExecutor, mResultFileSet, mHandler));
//                 } else {
//                     if (mHandler.getFileFilter().accept(file)) {
//                         mResultFileSet.add(file);
//                     }
//                 }
//             }
//         }
//         //traversal finish
//         if (mHandler.devideTaskCount() == 0) {
//             mHandler.onFinish(System.currentTimeMillis());
//         }
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/utils/FileFilterBySuffix.java
// public class FileFilterBySuffix implements FileFilter {
// 
//     private String suffix;
// 
//     public FileFilterBySuffix(String suffix) {
//         super();
//         this.suffix = suffix;
//     }
// 
//     @Override
//     public boolean accept(File pathname) {
// 
//         return pathname.getName().endsWith(suffix);
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/utils/SimpleQueue.java
// public class SimpleQueue<E> {
//     private LinkedList<E> link;
// 
// 
//     public SimpleQueue() {
//         link = new LinkedList<E>();
//     }
// 
//     public void add(E obj) {
//         link.addFirst(obj);
//     }
// 
// 
//     public E peek() {
//         return link.removeLast();
//     }
// 
// 
//     public boolean isEmpty() {
//         return link.isEmpty();
//     }
// }


import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import gov.sequarius.toys.gallery.R;
import gov.sequarius.toys.gallery.module.main.MainView;
import gov.sequarius.toys.gallery.task.ScanHandler;
import gov.sequarius.toys.gallery.task.ScanImageFileTask;
import gov.sequarius.toys.gallery.utils.FileFilterBySuffix;
import gov.sequarius.toys.gallery.utils.SimpleQueue;

        executorService.execute(new ScanImageFileTask(externalCacheDir, executorService,
                mDataSet, new ScanHandler() {
            @Override
            //simple call back when traversal completed
            public void onFinish(long finishTime) {
                mView.notifyDataSetChanged();
                mView.makeCommonSnake(mContext.getString(R.string.message_format_load_result,
                        mDataSet.size(), finishTime - getStartTime()));
                mView.setProgressBarVisible(false);
                //shutdown thread pool
                executorService.shutdown();

            }
        }));
    }
    /**
     * get image files by single thread
     */
    public void getExternalImageBySingleThread() {
        mView.setProgressBarVisible(true);
        new singleThreadScanTask().start();
    }

    private class singleThreadScanTask extends Thread {
        @Override
        public void run() {
            File externalCacheDir = Environment.getExternalStorageDirectory();
            mDataSet.clear();
            long startTime = System.currentTimeMillis();
            FileFilter filter = new FileFilterBySuffix(ScanHandler.SUFFIX_JPG);

// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/module/main/MainView.java
// public interface MainView {
//     void notifyDataSetChanged();
// 
//     void initView();
// 
//     void makeCommonSnake(String message);
// 
//     void setProgressBarVisible(boolean visible);
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/task/ScanHandler.java
// public abstract class ScanHandler {
//     public static final String SUFFIX_JPG = "jpg";
//     private int taskCount;
// 
//     private FileFilter fileFilter;
// 
//     private long mStartTime=Long.MIN_VALUE;
// 
//     public long getStartTime() {
//         return mStartTime;
//     }
// 
//     public ScanHandler() {
//         fileFilter=new FileFilterBySuffix(SUFFIX_JPG);
//         mStartTime=System.currentTimeMillis();
//     }
// 
//     public FileFilter getFileFilter() {
//         return fileFilter;
//     }
// 
//     public abstract void onFinish(long finishTime);
// 
//     public synchronized void addTaskCount() {
//         taskCount += 1;
//     }
// 
//     public synchronized int devideTaskCount() {
//         taskCount -= 1;
//         return taskCount;
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/task/ScanImageFileTask.java
// public class ScanImageFileTask implements Runnable {
// 
//     private File mDirectory;
//     private ExecutorService mExecutor;
//     private List<File> mResultFileSet;
//     private ScanHandler mHandler;
// 
//     public ScanImageFileTask(File directory, ExecutorService executor, List<File> resultFileSet, ScanHandler handler) {
//         this.mHandler = handler;
//         handler.addTaskCount();
//         this.mDirectory = directory;
//         this.mExecutor = executor;
//         this.mResultFileSet = resultFileSet;
//     }
// 
//     @Override
//     public void run() {
//         File[] files = mDirectory.listFiles();
//         if (files != null) {
//             for (File file : files) {
//                 if (file.isDirectory()) {
//                     mExecutor.execute(new ScanImageFileTask(file, mExecutor, mResultFileSet, mHandler));
//                 } else {
//                     if (mHandler.getFileFilter().accept(file)) {
//                         mResultFileSet.add(file);
//                     }
//                 }
//             }
//         }
//         //traversal finish
//         if (mHandler.devideTaskCount() == 0) {
//             mHandler.onFinish(System.currentTimeMillis());
//         }
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/utils/FileFilterBySuffix.java
// public class FileFilterBySuffix implements FileFilter {
// 
//     private String suffix;
// 
//     public FileFilterBySuffix(String suffix) {
//         super();
//         this.suffix = suffix;
//     }
// 
//     @Override
//     public boolean accept(File pathname) {
// 
//         return pathname.getName().endsWith(suffix);
//     }
// }
// 
// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/utils/SimpleQueue.java
// public class SimpleQueue<E> {
//     private LinkedList<E> link;
// 
// 
//     public SimpleQueue() {
//         link = new LinkedList<E>();
//     }
// 
//     public void add(E obj) {
//         link.addFirst(obj);
//     }
// 
// 
//     public E peek() {
//         return link.removeLast();
//     }
// 
// 
//     public boolean isEmpty() {
//         return link.isEmpty();
//     }
// }


// Path: GalleryToy/app/src/main/java/gov/sequarius/toys/gallery/presenter/MainPresenter.java
import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import gov.sequarius.toys.gallery.R;
import gov.sequarius.toys.gallery.module.main.MainView;
import gov.sequarius.toys.gallery.task.ScanHandler;
import gov.sequarius.toys.gallery.task.ScanImageFileTask;
import gov.sequarius.toys.gallery.utils.FileFilterBySuffix;
import gov.sequarius.toys.gallery.utils.SimpleQueue;

        executorService.execute(new ScanImageFileTask(externalCacheDir, executorService,
                mDataSet, new ScanHandler() {
            @Override
            //simple call back when traversal completed
            public void onFinish(long finishTime) {
                mView.notifyDataSetChanged();
                mView.makeCommonSnake(mContext.getString(R.string.message_format_load_result,
                        mDataSet.size(), finishTime - getStartTime()));
                mView.setProgressBarVisible(false);
                //shutdown thread pool
                executorService.shutdown();

            }
        }));
    }
    /**
     * get image files by single thread
     */
    public void getExternalImageBySingleThread() {
        mView.setProgressBarVisible(true);
        new singleThreadScanTask().start();
    }

    private class singleThreadScanTask extends Thread {
        @Override
        public void run() {
            File externalCacheDir = Environment.getExternalStorageDirectory();
            mDataSet.clear();
            long startTime = System.currentTimeMillis();
            FileFilter filter = new FileFilterBySuffix(ScanHandler.SUFFIX_JPG);

            SimpleQueue<File> queue = new SimpleQueue<>();
