-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMauiProgram.cs
88 lines (78 loc) · 2.52 KB
/
MauiProgram.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
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui;
using Microsoft.Maui.LifecycleEvents;
using System.Diagnostics;
namespace DirectSFTP;
// I changed Project File and launchSettings to make it unpackaged
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
GlobalHooks.StartHooks();
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
}).ConfigureLifecycleEvents(e =>
{
#if ANDROID
e.AddAndroid(android => android.OnActivityResult((act, req, res, intent) =>
{
if (req == 70){
var folderPath = ToPhysicalPath(intent.Data);
Preferences.Default.Set("DownloadFolder",folderPath);
Debug.WriteLine(SFTP.GetDownloadFolder());
}
}));
#endif
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
#if ANDROID
public static string ToPhysicalPath(Android.Net.Uri uri)
{
const string uriSchemeFolder = "content";
if (uri.Scheme is null || !uri.Scheme.Equals(uriSchemeFolder, StringComparison.OrdinalIgnoreCase))
{
return null;
}
if (uri.PathSegments?.Count < 2)
{
return null;
}
// Example path would be /tree/primary:DCIM, or /tree/SDCare:DCIM
var path = uri.PathSegments?[1];
if (path is null)
{
return null;
}
var pathSplit = path.Split(':');
if (pathSplit.Length < 2)
{
return null;
}
// Primary is the device's internal storage, and anything else is an SD card or other external storage
if (pathSplit[0].Equals("primary", StringComparison.OrdinalIgnoreCase))
{
// Example for internal path /storage/emulated/0/DCIM
return $"{Android.OS.Environment.ExternalStorageDirectory?.Path}/{pathSplit[1]}";
}
// Example for external path /storage/1B0B-0B1C/DCIM
return $"/{"storage"}/{pathSplit[0]}/{pathSplit[1]}";
}
/// <summary>
/// Get External Directory Path
/// </summary>
public static string GetExternalDirectory()
{
return Android.OS.Environment.ExternalStorageDirectory?.Path ?? "/storage/emulated/0";
}
#endif
}