5 string m_rootDirectory;
6 public static string fName =
"AndroidSdCardExternalContentProvider";
7 public string DisplayName => LanguageControl.Get(fName, 1);
9 public string Description {
11 InitializeFilesystemAccess();
12 return m_rootDirectory;
16 public bool SupportsListing =>
true;
18 public bool SupportsLinks =>
false;
20 public bool RequiresLogin =>
false;
22 public bool IsLoggedIn =>
true;
24 public bool IsLocalProvider =>
true;
26 public void Dispose() { }
28 public void Login(CancellableProgress progress, Action success, Action<Exception> failure) => failure(
new NotSupportedException());
30 public void Logout() {
31 throw new NotSupportedException();
34 public void List(
string path, CancellableProgress progress, Action<ExternalContentEntry> success, Action<Exception> failure) {
35 ThreadPool.QueueUserWorkItem(
38 InitializeFilesystemAccess();
39 string internalPath = ToInternalPath(path);
40 ExternalContentEntry entry = GetDirectoryEntry(internalPath,
true);
41 Dispatcher.Dispatch(delegate { success(entry); });
43 catch (Exception ex) {
44 Dispatcher.Dispatch(delegate { failure(ex); });
50 public void Download(
string path, CancellableProgress progress, Action<Stream> success, Action<Exception> failure) {
51 ThreadPool.QueueUserWorkItem(
54 InitializeFilesystemAccess();
55 string path2 = ToInternalPath(path);
56 FileStream stream =
new(path2, FileMode.Open, FileAccess.Read, FileShare.Read);
57 Dispatcher.Dispatch(delegate { success(stream); });
59 catch (Exception ex) {
60 Dispatcher.Dispatch(delegate { failure(ex); });
66 public void Upload(
string path, Stream stream, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
67 ThreadPool.QueueUserWorkItem(
70 InitializeFilesystemAccess();
71 string uniquePath = GetUniquePath(ToInternalPath(path));
72 string po = uniquePath;
73 if (po.StartsWith(
"android:")) {
74 po = Storage.GetSystemPath(po);
76 string pp = Storage.GetDirectoryName(po);
78 using (FileStream destination =
new(po, FileMode.Create, FileAccess.Write, FileShare.None)) {
79 stream.CopyTo(destination);
81 Dispatcher.Dispatch(delegate { success(uniquePath); });
83 catch (Exception ex) {
84 Dispatcher.Dispatch(delegate { failure(ex); });
90 public void Link(
string path, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
91 failure(
new NotSupportedException());
94 public ExternalContentEntry GetDirectoryEntry(
string internalPath,
bool scanContents) {
95 ExternalContentEntry externalContentEntry =
new() {
96 Type =
ExternalContentType.Directory, Path = ToExternalPath(internalPath), Time =
new DateTime(1970, 1, 1)
99 string[] directories =
Directory.GetDirectories(internalPath);
100 foreach (
string internalPath2
in directories) {
101 externalContentEntry.
ChildEntries.Add(GetDirectoryEntry(internalPath2,
false));
103 directories =
Directory.GetFiles(internalPath);
104 foreach (
string text
in directories) {
105 FileInfo fileInfo =
new(text);
106 ExternalContentEntry externalContentEntry2 =
new() {
107 Type = ExternalContentManager.ExtensionToType(Path.GetExtension(text)),
108 Path = ToExternalPath(text),
109 Size = fileInfo.Length,
110 Time = fileInfo.CreationTime
112 externalContentEntry.
ChildEntries.Add(externalContentEntry2);
115 return externalContentEntry;
118 public static string GetUniquePath(
string path) {
121 string directoryName = Path.GetDirectoryName(path);
122 if (directoryName ==
null) {
125 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
126 string extension = Path.GetExtension(path);
127 while (Storage.FileExists(text)
129 string path2 = fileNameWithoutExtension + num + extension;
130 text = Path.Combine(directoryName, path2);
136 public string ToExternalPath(
string internalPath) => Path.GetFullPath(internalPath);
138 public string ToInternalPath(
string externalPath) => Path.Combine(m_rootDirectory, externalPath);
140 public void InitializeFilesystemAccess() {
142 m_rootDirectory = $
"{RunPath.AndroidFilePath}/files";
143 if (!Storage.DirectoryExists(m_rootDirectory)) {
144 Storage.CreateDirectory(m_rootDirectory);
List< ExternalContentEntry > ChildEntries