blob: 9cccb5cfbfc28c20c856ef85461939f2e74c1732 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
Alexandre Lision8ad39592013-11-21 13:23:58 -050031package org.sflphone.fragments;
32
33import java.io.File;
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.Comparator;
37import java.util.List;
38import java.util.Locale;
39
40import org.sflphone.R;
41
42import android.app.DialogFragment;
43import android.os.Bundle;
44import android.os.Environment;
Alexandre Lision8ad39592013-11-21 13:23:58 -050045import android.view.LayoutInflater;
46import android.view.View;
47import android.view.View.OnClickListener;
48import android.view.ViewGroup;
49import android.widget.AdapterView;
50import android.widget.AdapterView.OnItemClickListener;
51import android.widget.ArrayAdapter;
52import android.widget.Button;
53import android.widget.ListView;
54import android.widget.TextView;
55
56public class FileExplorerDFragment extends DialogFragment implements OnItemClickListener {
57
58 private String prefKey;
59
60 public interface onFileSelectedListener {
61 public void onFileSelected(String path, String key);
62 }
63
64 public static FileExplorerDFragment newInstance() {
65 FileExplorerDFragment f = new FileExplorerDFragment();
66 return f;
67 }
68
69 private List<String> item = null;
70 private List<String> path = null;
71 private String root;
72 private TextView myPath;
73 public onFileSelectedListener mDelegateListener;
74
75 private String currentPath;
76 Comparator<? super File> comparator;
77
78 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
79 super.onCreateView(inflater, container, savedInstanceState);
80 View rootView = inflater.inflate(R.layout.file_explorer_dfrag, container);
81 myPath = (TextView) rootView.findViewById(R.id.path);
82
83 getDialog().setTitle(getResources().getString(R.string.file_explorer_title));
84
85 comparator = filecomparatorByAlphabetically;
86 root = Environment.getExternalStorageDirectory().getPath();
87 getDir(root, rootView);
88
89 Button btnAlphabetically = (Button) rootView.findViewById(R.id.button_alphabetically);
90 btnAlphabetically.setOnClickListener(new OnClickListener() {
91
92 @Override
93 public void onClick(View arg0) {
94 comparator = filecomparatorByAlphabetically;
95 getDir(currentPath, getView());
96
97 }
98 });
99
100 Button btnLastDateModified = (Button) rootView.findViewById(R.id.button_lastDateModified);
101 btnLastDateModified.setOnClickListener(new OnClickListener() {
102
103 @Override
104 public void onClick(View arg0) {
105 comparator = filecomparatorByLastModified;
106 getDir(currentPath, getView());
107
108 }
109 });
110 return rootView;
111 }
112
113 private void getDir(String dirPath, View parent) {
114 currentPath = dirPath;
115
116 myPath.setText("Location: " + dirPath);
117 item = new ArrayList<String>();
118 path = new ArrayList<String>();
119 File f = new File(dirPath);
120 File[] files = f.listFiles();
121
122 if (!dirPath.equals(root)) {
123 item.add(root);
124 path.add(root);
125 item.add("../");
126 path.add(f.getParent());
127 }
128
129 Arrays.sort(files, comparator);
130
131 for (int i = 0; i < files.length; i++) {
132 File file = files[i];
133
134 if (!file.isHidden() && file.canRead()) {
135 path.add(file.getPath());
136 if (file.isDirectory()) {
137 item.add(file.getName() + "/");
138 } else {
139 item.add(file.getName());
140 }
141 }
142 }
143
144 ArrayAdapter<String> fileList = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, item);
145
146 ((ListView) parent.findViewById(android.R.id.list)).setAdapter(fileList);
147 ((ListView) parent.findViewById(android.R.id.list)).setOnItemClickListener(this);
148 }
149
150 Comparator<? super File> filecomparatorByLastModified = new Comparator<File>() {
151
152 public int compare(File file1, File file2) {
153
154 if (file1.isDirectory()) {
155 if (file2.isDirectory()) {
156 return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
157 } else {
158 return -1;
159 }
160 } else {
161 if (file2.isDirectory()) {
162 return 1;
163 } else {
164 return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
165 }
166 }
167
168 }
169 };
170
171 Comparator<? super File> filecomparatorByAlphabetically = new Comparator<File>() {
172
173 public int compare(File file1, File file2) {
174
175 if (file1.isDirectory()) {
176 if (file2.isDirectory()) {
177 return String.valueOf(file1.getName().toLowerCase(Locale.getDefault())).compareTo(file2.getName().toLowerCase());
178 } else {
179 return -1;
180 }
181 } else {
182 if (file2.isDirectory()) {
183 return 1;
184 } else {
185 return String.valueOf(file1.getName().toLowerCase(Locale.getDefault())).compareTo(file2.getName().toLowerCase());
186 }
187 }
188
189 }
190 };
191
192 public void setOnFileSelectedListener(onFileSelectedListener listener, String key) {
193 mDelegateListener = listener;
194 prefKey = key;
195 }
196
197 @Override
198 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
199 File file = new File(path.get(pos));
200
201
202// if (file.isDirectory()) {
203// selectButton.setEnabled(false);
204// if (file.canRead()) {
205// lastPositions.put(currentPath, position);
206// getDir(path.get(position));
207// if (canSelectDir) {
208// selectedFile = file;
209// v.setSelected(true);
210// selectButton.setEnabled(true);
211// }
212// } else {
213// new AlertDialog.Builder(this).setIcon(R.drawable.icon)
214// .setTitle("[" + file.getName() + "] " + getText(R.string.cant_read_folder))
215// .setPositiveButton("OK", new DialogInterface.OnClickListener() {
216//
217// @Override
218// public void onClick(DialogInterface dialog, int which) {
219//
220// }
221// }).show();
222// }
223// } else {
224// selectedFile = file;
225// v.setSelected(true);
226// selectButton.setEnabled(true);
227// }
228//
229
230
231
232 if (mDelegateListener != null) {
233 mDelegateListener.onFileSelected((String) ((ListView) getView().findViewById(android.R.id.list)).getAdapter().getItem(pos), prefKey);
234 }
235 }
236}