001.
package
app.main;
002.
003.
import
java.io.File;
004.
import
java.io.IOException;
005.
import
java.util.ArrayList;
006.
import
java.util.Collections;
007.
008.
import
android.content.Context;
009.
import
android.util.AttributeSet;
010.
import
android.view.View;
011.
import
android.widget.AdapterView;
012.
import
android.widget.ArrayAdapter;
013.
import
android.widget.ListView;
014.
015.
public
class
FileList
extends
ListView {
016.
017.
public
FileList(Context context, AttributeSet attrs,
int
defStyle) {
018.
super
(context, attrs, defStyle);
019.
020.
init(context);
021.
}
022.
023.
public
FileList(Context context, AttributeSet attrs) {
024.
super
(context, attrs);
025.
026.
init(context);
027.
}
028.
029.
public
FileList(Context context) {
030.
super
(context);
031.
032.
init(context);
033.
}
034.
035.
private
void
init(Context context) {
036.
_Context = context;
037.
setOnItemClickListener(_OnItemClick);
038.
}
039.
040.
private
Context _Context =
null
;
041.
private
ArrayList<String> _List =
new
ArrayList<String>();
042.
private
ArrayList<String> _FolderList =
new
ArrayList<String>();
043.
private
ArrayList<String> _FileList =
new
ArrayList<String>();
044.
private
ArrayAdapter<String> _Adapter =
null
;
045.
046.
047.
private
String _Path =
""
;
048.
049.
050.
private
OnPathChangedListener _OnPathChangedListener =
null
;
051.
private
OnFileSelectedListener _OnFileSelectedListener =
null
;
052.
053.
private
boolean
openPath(String path) {
054.
_FolderList.clear();
055.
_FileList.clear();
056.
057.
File file =
new
File(path);
058.
File[] files = file.listFiles();
059.
if
(files ==
null
)
return
false
;
060.
061.
for
(
int
i=
0
; i<files.length; i++) {
062.
if
(files[i].isDirectory()) {
063.
_FolderList.add(
"<"
+ files[i].getName() +
">"
);
064.
}
else
{
065.
_FileList.add(files[i].getName());
066.
}
067.
}
068.
069.
Collections.sort(_FolderList);
070.
Collections.sort(_FileList);
071.
072.
_FolderList.add(
0
,
"<..>"
);
073.
074.
return
true
;
075.
}
076.
077.
private
void
updateAdapter() {
078.
_List.clear();
079.
_List.addAll(_FolderList);
080.
_List.addAll(_FileList);
081.
082.
_Adapter =
new
ArrayAdapter<String>(_Context, android.R.layout.simple_list_item_1, _List);
083.
setAdapter(_Adapter);
084.
}
085.
086.
public
void
setPath(String value) {
087.
if
(value.length() ==
0
) {
088.
value =
"/"
;
089.
}
else
{
090.
String lastChar = value.substring(value.length()-
1
, value.length());
091.
if
(lastChar.matches(
"/"
) ==
false
) value = value +
"/"
;
092.
}
093.
094.
if
(openPath(value)) {
095.
_Path = value;
096.
updateAdapter();
097.
if
(_OnPathChangedListener !=
null
) _OnPathChangedListener.onChanged(value);
098.
}
099.
}
100.
101.
public
String getPath() {
102.
return
_Path;
103.
}
104.
105.
public
void
setOnPathChangedListener(OnPathChangedListener value) {
106.
_OnPathChangedListener = value;
107.
}
108.
109.
public
OnPathChangedListener getOnPathChangedListener() {
110.
return
_OnPathChangedListener;
111.
}
112.
113.
public
void
setOnFileSelected(OnFileSelectedListener value) {
114.
_OnFileSelectedListener = value;
115.
}
116.
117.
public
OnFileSelectedListener getOnFileSelected() {
118.
return
_OnFileSelectedListener;
119.
}
120.
121.
public
String DelteRight(String value, String border) {
122.
String list[] = value.split(border);
123.
124.
String result =
""
;
125.
126.
for
(
int
i=
0
; i<list.length; i++) {
127.
result = result + list[i] + border;
128.
}
129.
130.
return
result;
131.
}
132.
133.
private
String delteLastFolder(String value) {
134.
String list[] = value.split(
"/"
);
135.
136.
String result =
""
;
137.
138.
for
(
int
i=
0
; i<list.length-
1
; i++) {
139.
result = result + list[i] +
"/"
;
140.
}
141.
142.
return
result;
143.
}
144.
145.
private
String getRealPathName(String newPath) {
146.
String path = newPath.substring(
1
, newPath.length()-
1
);
147.
148.
if
(path.matches(
".."
)) {
149.
return
delteLastFolder(_Path);
150.
}
else
{
151.
return
_Path + path +
"/"
;
152.
}
153.
}
154.
155.
private
AdapterView.OnItemClickListener _OnItemClick =
new
AdapterView.OnItemClickListener() {
156.
@Override
157.
public
void
onItemClick(AdapterView<?> arg0, View arg1,
int
position,
158.
long
id) {
159.
String fileName = getItemAtPosition(position).toString();
160.
if
(fileName.matches(
"<.*>"
)) {
161.
setPath(getRealPathName(fileName));
162.
}
else
{
163.
if
(_OnFileSelectedListener !=
null
) _OnFileSelectedListener.onSelected(_Path, fileName);
164.
}
165.
}
166.
};
167.
168.
}