From ae0e6ec634d8ab515ae381145a89d9ce649ba082 Mon Sep 17 00:00:00 2001 From: evgenyzinoviev Date: Fri, 14 Aug 2015 17:11:48 +0300 Subject: initial --- src/org/happysanta/gd/FileDialog.java | 209 ++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/org/happysanta/gd/FileDialog.java (limited to 'src/org/happysanta/gd/FileDialog.java') diff --git a/src/org/happysanta/gd/FileDialog.java b/src/org/happysanta/gd/FileDialog.java new file mode 100644 index 0000000..78b0326 --- /dev/null +++ b/src/org/happysanta/gd/FileDialog.java @@ -0,0 +1,209 @@ +package org.happysanta.gd; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Environment; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import static org.happysanta.gd.Helpers.logDebug; + +public class FileDialog { + private static final String PARENT_DIR = ".."; + private final String TAG = getClass().getName(); + private String[] fileList; + private File currentPath; + + public interface FileSelectedListener { + void fileSelected(File file); + } + + public interface DirectorySelectedListener { + void directorySelected(File directory); + } + + private ListenerList fileListenerList = new ListenerList(); + private ListenerList dirListenerList = new ListenerList(); + private final Activity activity; + private boolean selectDirectoryOption = false; + private String fileEndsWith; + + public FileDialog(Activity activity, File path, String fileEndsWith) { + this.activity = activity; + this.fileEndsWith = fileEndsWith != null ? fileEndsWith.toLowerCase() : fileEndsWith; + if (!path.exists()) path = Environment.getExternalStorageDirectory(); + // logDebug("FileDialog contrustor, path = " + path); + loadFileList(path); + } + + /** + * @return file dialog + */ + public Dialog createFileDialog() { + Dialog dialog = null; + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + + builder.setTitle(currentPath.getPath()); + if (selectDirectoryOption) { + builder.setPositiveButton("Select directory", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Helpers.logDebug(currentPath.getParent()); + // Log.d(TAG, currentPath.getPath()); + fireDirectorySelectedEvent(currentPath); + } + }); + } + + builder.setItems(fileList, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + String fileChosen = fileList[which]; + File chosenFile = getChosenFile(fileChosen); + if (chosenFile.isDirectory()) { + loadFileList(chosenFile); + dialog.cancel(); + dialog.dismiss(); + showDialog(); + } else fireFileSelectedEvent(chosenFile); + } + }); + + dialog = builder.show(); + return dialog; + } + + + public void addFileListener(FileSelectedListener listener) { + fileListenerList.add(listener); + } + + public void removeFileListener(FileSelectedListener listener) { + fileListenerList.remove(listener); + } + + public void setSelectDirectoryOption(boolean selectDirectoryOption) { + this.selectDirectoryOption = selectDirectoryOption; + } + + public void addDirectoryListener(DirectorySelectedListener listener) { + dirListenerList.add(listener); + } + + public void removeDirectoryListener(DirectorySelectedListener listener) { + dirListenerList.remove(listener); + } + + /** + * Show file dialog + */ + public void showDialog() { + createFileDialog().show(); + } + + private void fireFileSelectedEvent(final File file) { + fileListenerList.fireEvent(new ListenerList.FireHandler() { + public void fireEvent(FileSelectedListener listener) { + listener.fileSelected(file); + } + }); + } + + private void fireDirectorySelectedEvent(final File directory) { + dirListenerList.fireEvent(new ListenerList.FireHandler() { + public void fireEvent(DirectorySelectedListener listener) { + listener.directorySelected(directory); + } + }); + } + + private void loadFileList(File path) { + this.currentPath = path; + + ArrayList dirs = new ArrayList<>(); + ArrayList files = new ArrayList<>(); + ArrayList totalList = new ArrayList<>(); + + if (path.exists()) { + if (path.getParentFile() != null) dirs.add(PARENT_DIR); + FilenameFilter filter = new FilenameFilter() { + public boolean accept(File dir, String filename) { + File sel = new File(dir, filename); + if (!sel.canRead()) return false; + if (sel.isHidden()) return false; + if (selectDirectoryOption) return sel.isDirectory(); + else { + boolean endsWith = fileEndsWith != null ? filename.toLowerCase().endsWith(fileEndsWith) : true; + return endsWith || sel.isDirectory(); + } + } + }; + + File[] list = path.listFiles(filter); + try { + if (list != null && list.length > 0) { + for (File file : list) { + if (file.isDirectory()) + dirs.add(file.getName() + "/"); + else + files.add(file.getName()); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + Comparator comparator = new Comparator() { + @Override + public int compare(String lhs, String rhs) { + return lhs.compareTo(rhs); + } + }; + + Collections.sort(dirs, comparator); + Collections.sort(files, comparator); + + totalList.addAll(dirs); + totalList.addAll(files); + } + + fileList = totalList.toArray(new String[]{}); + } + + private File getChosenFile(String fileChosen) { + if (fileChosen.equals(PARENT_DIR)) return currentPath.getParentFile(); + else return new File(currentPath, fileChosen); + } +} + +class ListenerList { + private List listenerList = new ArrayList(); + + public interface FireHandler { + void fireEvent(L listener); + } + + public void add(L listener) { + listenerList.add(listener); + } + + public void fireEvent(FireHandler fireHandler) { + List copy = new ArrayList(listenerList); + for (L l : copy) { + fireHandler.fireEvent(l); + } + } + + public void remove(L listener) { + listenerList.remove(listener); + } + + public List getListenerList() { + return listenerList; + } +} \ No newline at end of file -- cgit v1.2.3