/
FileListFragment.cs
97 lines (84 loc) · 2.97 KB
/
FileListFragment.cs
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
namespace tts
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Android.App;
using Android.OS;
using Android.Support.V4.App;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Content;
using Android.Runtime;
public class FileListFragment : ListFragment
{
public static readonly string DefaultInitialDirectory = "/";
private FileListAdapter _adapter;
private DirectoryInfo _directory;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
_adapter = new FileListAdapter(Activity, new FileSystemInfo[0]);
ListAdapter = _adapter;
}
public override void OnListItemClick(ListView l, View v, int position, long id)
{
var fileSystemInfo = _adapter.GetItem(position);
if (fileSystemInfo.IsFile())
{
Context packageContext = Activity;
Intent i = new Intent(packageContext, typeof(MainActivity));
i.PutExtra("1", fileSystemInfo.FullName);
Activity.SetResult(Result.Ok, i);
Activity.Finish();
}
else
{
RefreshFilesList(fileSystemInfo.FullName);
}
base.OnListItemClick(l, v, position, id);
}
public override void OnResume()
{
base.OnResume();
RefreshFilesList(DefaultInitialDirectory);
}
public bool txt(String filename)
{
int lastdot = filename.LastIndexOf(".");
if (lastdot > 0)
{
return (filename.Substring(lastdot + 1) == "txt");
}
return false;
}
public void RefreshFilesList(string directory)
{
IList<FileSystemInfo> visibleThings = new List<FileSystemInfo>();
var dir = new DirectoryInfo(directory);
try
{
if (dir.ToString()!="/")
visibleThings.Add(dir.Parent);
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
bool b;
if (item.IsDirectory()) b = true; else b = txt(item.Name);
if (b) visibleThings.Add(item);
}
}
catch (Exception ex)
{
Log.Error("FileListFragment", "Íåò äîñòóïà ê äèðåêòîðèè " + _directory.FullName + "; " + ex);
Toast.MakeText(Activity, "Ïðîáëåìà äîñòóïà ê äèðåêòîðèè " + directory, ToastLength.Long).Show();
return;
}
_directory = dir;
_adapter.AddDirectoryContents(visibleThings);
ListView.RefreshDrawableState();
Log.Verbose("FileListFragment", "Ïîêàçàí êîíòåíò äèðåêòîðèè {0}.", directory);
}
}
}