Release 0.4.8 with Android file imports
This commit is contained in:
1 parent
b600412285
commit
3dfeef54ea
9 files changed
+328
-32
No files matched your search
@@ -1,10 +1,75 @@
|
||||
using Android.App;
|
||||
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, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
|
||||
[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 == Android.Content.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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user