84 lines
4.0 KiB
C#
84 lines
4.0 KiB
C#
using Android.App;
|
|
using Android.Content;
|
|
using Android.Content.PM;
|
|
using Android.Database;
|
|
using Android.OS;
|
|
using Android.Provider;
|
|
using LMEMappeCatastaliApp.Services;
|
|
|
|
namespace LMEMappeCatastaliApp;
|
|
|
|
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/json")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/geo+json")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/geojson")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/vnd.google-earth.kml+xml")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/vnd.google-earth.kmz")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/xml")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "text/xml")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/zip")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/x-zip-compressed")]
|
|
[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = "application/octet-stream")]
|
|
public class MainActivity : MauiAppCompatActivity
|
|
{
|
|
protected override void OnCreate(Bundle? savedInstanceState)
|
|
{
|
|
base.OnCreate(savedInstanceState);
|
|
QueueViewedFile(Intent);
|
|
}
|
|
|
|
protected override void OnNewIntent(Intent? intent)
|
|
{
|
|
base.OnNewIntent(intent);
|
|
Intent = intent;
|
|
QueueViewedFile(intent);
|
|
}
|
|
|
|
private void QueueViewedFile(Intent? intent)
|
|
{
|
|
if (intent?.Action == Intent.ActionView && intent.Data is not null)
|
|
{
|
|
_ = CopyViewedFileAsync(intent);
|
|
}
|
|
}
|
|
|
|
private async Task CopyViewedFileAsync(Intent intent)
|
|
{
|
|
try
|
|
{
|
|
var uri = intent.Data!;
|
|
var fileName = ReadDisplayName(uri) ?? $"file-{Guid.NewGuid():N}";
|
|
fileName = string.Concat(fileName.Select(character => Path.GetInvalidFileNameChars().Contains(character) ? '_' : character));
|
|
var directory = Path.Combine(FileSystem.CacheDirectory, "Incoming");
|
|
Directory.CreateDirectory(directory);
|
|
var path = Path.Combine(directory, $"{Guid.NewGuid():N}-{fileName}");
|
|
await using var input = ContentResolver!.OpenInputStream(uri)
|
|
?? throw new IOException("Impossibile leggere il file selezionato.");
|
|
await using var output = File.Create(path);
|
|
await input.CopyToAsync(output);
|
|
IncomingFileService.Shared.Enqueue(new IncomingFile(path, fileName, intent.Type));
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Apertura file LME non riuscita: {error}");
|
|
}
|
|
}
|
|
|
|
private string? ReadDisplayName(Android.Net.Uri uri)
|
|
{
|
|
if (uri.Scheme == ContentResolver.SchemeFile)
|
|
{
|
|
return Path.GetFileName(uri.Path);
|
|
}
|
|
|
|
using ICursor? cursor = ContentResolver?.Query(uri, [IOpenableColumns.DisplayName], null, null, null);
|
|
if (cursor is null || !cursor.MoveToFirst())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var index = cursor.GetColumnIndex(IOpenableColumns.DisplayName);
|
|
return index >= 0 ? cursor.GetString(index) : null;
|
|
}
|
|
}
|