aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/FileDialog.java
blob: 78b0326d6d3307621ae066dddf4357d499f6a64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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<FileSelectedListener> fileListenerList = new ListenerList<FileDialog.FileSelectedListener>();
	private ListenerList<DirectorySelectedListener> dirListenerList = new ListenerList<FileDialog.DirectorySelectedListener>();
	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<FileSelectedListener>() {
			public void fireEvent(FileSelectedListener listener) {
				listener.fileSelected(file);
			}
		});
	}

	private void fireDirectorySelectedEvent(final File directory) {
		dirListenerList.fireEvent(new ListenerList.FireHandler<DirectorySelectedListener>() {
			public void fireEvent(DirectorySelectedListener listener) {
				listener.directorySelected(directory);
			}
		});
	}

	private void loadFileList(File path) {
		this.currentPath = path;

		ArrayList<String> dirs = new ArrayList<>();
		ArrayList<String> files = new ArrayList<>();
		ArrayList<String> 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<String> comparator = new Comparator<String>() {
				@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<L> {
	private List<L> listenerList = new ArrayList<L>();

	public interface FireHandler<L> {
		void fireEvent(L listener);
	}

	public void add(L listener) {
		listenerList.add(listener);
	}

	public void fireEvent(FireHandler<L> fireHandler) {
		List<L> copy = new ArrayList<L>(listenerList);
		for (L l : copy) {
			fireHandler.fireEvent(l);
		}
	}

	public void remove(L listener) {
		listenerList.remove(listener);
	}

	public List<L> getListenerList() {
		return listenerList;
	}
}