package com.parnswir.unmp.media;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.parnswir.unmp.core.ProgressObservable;
import com.parnswir.unmp.core.ProjectResources;

import android.database.sqlite.SQLiteDatabase;

public abstract class FileCrawlerThread extends Thread {
	
	protected SQLiteDatabase db;
	private List<String> folders;
	private List<String> files;
	protected boolean stop = false;
	
	public ProgressObservable callback = new ProgressObservable();
	
	
	public FileCrawlerThread(SQLiteDatabase db, String folder) {
		ArrayList<String> wrapper = new ArrayList<String>();
		wrapper.add(folder);
		init(db, wrapper);
	}
	
	public FileCrawlerThread(SQLiteDatabase db, List<String> folders) {
		init(db, folders);
	}
	
	private void init(SQLiteDatabase db, List<String> folders) {
		this.folders = folders;
		this.db = db;
		files = new ArrayList<String>();
	}
	
	public void run() {
		files.clear();
		for (String folderRoot : folders) {
			File root = new File(folderRoot);
			searchForFilesIn(root);
		}
		processFiles();
	}
	
	private void searchForFilesIn(File file) {
		if (stop) return;
		if (file.canRead()) {
			if (file.isDirectory()) {
				searchForFilesInDirectory(file);
			} else {
				addToFileListIfSuitable(file);
			}
		}
	}
	
	private void searchForFilesInDirectory(File file) {
		setProgress("Looking for files in " + file.getAbsolutePath(), -1, -1);
		for (File child : file.listFiles()) {
			searchForFilesIn(child);
		}
	}
	
	private void addToFileListIfSuitable(File file) {
		if (isSuitable(file)) {
			files.add(file.getAbsolutePath());
		}
	}
	
	private boolean isSuitable(File file) {
		return file.getAbsolutePath().endsWith(".mp3") && !stop;
	}
	
	private void setProgress(String text, float value, float count) {
		callback.change(new ProjectResources.ProgressItem(text, value, count));
	}
	
	private void processFiles() {
		for (String filePath : files) {
			File current = new File(filePath);
			if (current != null) {
				setProgress(filePath, files.indexOf(filePath), files.size());
				handleFile(current);
			}
		}
		setProgress("Done.", files.size(), files.size());
	}
	
	abstract void handleFile(File file);

	public void kill() {
		stop = true;		
	}

}

@@ -93,20 +93,166 @@
 va.util.
-List
+Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Locale;%0Aimport java.util.Set;%0A%0Aimport android.database.sqlite.SQLiteDatabase
 ;%0A%0Aimpor
@@ -349,56 +349,8 @@
 s;%0A%0A
-import android.database.sqlite.SQLiteDatabase;%0A%0A
 publ
@@ -404,16 +404,143 @@
 ead %7B%0A%09%0A
+%09public static final Set%3CString%3E SUPPORTED_FILETYPES = new HashSet%3CString%3E(Arrays.asList(%0A%09%09new String%5B%5D %7B%22mp3%22, %22wpl%22%7D%0A%09));%0A%09%0A
 %09protect
@@ -1885,63 +1885,291 @@
 %7B%0A%09%09
-return file.getAbsolutePath().endsWith(%22.mp3%22) && !stop
+String extension = getFileExt(file.getName());%0A%09%09return (SUPPORTED_FILETYPES.contains(extension)) && !stop;%0A%09%7D%0A%09%0A%09public static String getFileExt(String fileName) %7B       %0A%09     return fileName.toLowerCase(Locale.ENGLISH).substring((fileName.lastIndexOf(%22.%22) + 1), fileName.length())
 ;%0A%09%7D

Add supported filetypes to crawler