1 package org.iotivity.ca.service;
3 import android.app.Activity;
4 import android.app.Dialog;
5 import android.os.Environment;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.view.WindowManager.LayoutParams;
9 import android.widget.AdapterView;
10 import android.widget.ArrayAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
14 import java.io.FileFilter;
15 import java.util.Arrays;
17 public class FileChooser {
18 private static final String PARENT_DIR = "..";
20 private final Activity activity;
21 private ListView list;
22 private Dialog dialog;
23 private File currentPath;
25 // filter on file extension
26 private String extension = null;
27 public void setExtension(String extension) {
28 this.extension = (extension == null) ? null :
29 extension.toLowerCase();
32 // file selection event handling
33 public interface FileSelectedListener {
34 void fileSelected(File file);
36 public FileChooser setFileListener(FileSelectedListener fileListener) {
37 this.fileListener = fileListener;
40 private FileSelectedListener fileListener;
42 public FileChooser(Activity activity) {
43 this.activity = activity;
44 dialog = new Dialog(activity);
45 list = new ListView(activity);
46 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
47 @Override public void onItemClick(AdapterView<?> parent, View view, int which, long id) {
48 String fileChosen = (String) list.getItemAtPosition(which);
49 File chosenFile = getChosenFile(fileChosen);
50 if (chosenFile.isDirectory()) {
53 if (fileListener != null) {
54 fileListener.fileSelected(chosenFile);
60 dialog.setContentView(list);
61 dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
62 refresh(Environment.getExternalStorageDirectory());
65 public void showDialog() {
71 * Sort, filter and display the files for the given path.
73 private void refresh(File path) {
74 this.currentPath = path;
76 File[] dirs = path.listFiles(new FileFilter() {
77 @Override public boolean accept(File file) {
78 return (file.isDirectory() && file.canRead());
81 File[] files = path.listFiles(new FileFilter() {
82 @Override public boolean accept(File file) {
83 if (!file.isDirectory()) {
84 if (!file.canRead()) {
86 } else if (extension == null) {
89 return file.getName().toLowerCase().endsWith(extension);
97 // convert to an array
100 if (path.getParentFile() == null) {
101 fileList = new String[dirs.length + files.length];
103 fileList = new String[dirs.length + files.length + 1];
104 fileList[i++] = PARENT_DIR;
108 for (File dir : dirs) { fileList[i++] = dir.getName(); }
109 for (File file : files ) { fileList[i++] = file.getName(); }
111 // refresh the user interface
112 dialog.setTitle(currentPath.getPath());
113 list.setAdapter(new ArrayAdapter(activity,
114 android.R.layout.simple_list_item_1, fileList) {
115 @Override public View getView(int pos, View view, ViewGroup parent) {
116 view = super.getView(pos, view, parent);
117 ((TextView) view).setSingleLine(true);
126 * Convert a relative filename into an actual File object.
128 private File getChosenFile(String fileChosen) {
129 if (fileChosen.equals(PARENT_DIR)) {
130 return currentPath.getParentFile();
132 return new File(currentPath, fileChosen);