diff --git a/Nearby Sharing Windows/FileUtils.cs b/Nearby Sharing Windows/FileUtils.cs index fec780e..14375b6 100644 --- a/Nearby Sharing Windows/FileUtils.cs +++ b/Nearby Sharing Windows/FileUtils.cs @@ -1,8 +1,7 @@ using Android.Content; using Android.Provider; using ShortDev.Microsoft.ConnectedDevices.NearShare; -using static Android.Provider.MediaStore; -using static Java.Util.Jar.Attributes; +using Environment = Android.OS.Environment; namespace Nearby_Sharing_Windows; @@ -27,35 +26,17 @@ public static string QueryContentName(this ContentResolver resolver, AndroidUri public static Stream CreateDownloadFile(this Activity activity, string fileName, ulong size) { - if (!OperatingSystem.IsAndroidVersionAtLeast(29) || true) - { - var filePath = Path.Combine(activity.GetDownloadDirectory().FullName, fileName); - return File.Create(filePath); - } - - // ToDo: Register as Download ... - // We need a seekable stream! - - var resolver = activity.ContentResolver ?? throw new InvalidOperationException("Could not get ContentResolver"); - - ContentValues contentValues = new(); - contentValues.Put(Downloads.InterfaceConsts.Title, fileName); - contentValues.Put(Downloads.InterfaceConsts.DisplayName, fileName); - contentValues.Put(Downloads.InterfaceConsts.MimeType, "*/*"); - contentValues.Put(Downloads.InterfaceConsts.Size, (long)size); - contentValues.Put(Downloads.InterfaceConsts.RelativePath, Path.Combine("Download", fileName)); - contentValues.Put(Downloads.InterfaceConsts.IsDownload, true); - - // Insert into the database - var contentUri = resolver.Insert(Downloads.ExternalContentUri, contentValues) ?? throw new IOException("Could not insert file into database"); - - using var fd = resolver.OpenAssetFileDescriptor(contentUri, "wt") ?? throw new IOException("Could not open file"); - return fd.CreateOutputStream() ?? throw new IOException("Could not open input stream"); + var filePath = Path.Combine(activity.GetDownloadDirectory().FullName, fileName); + return File.Create(filePath); } public static DirectoryInfo GetDownloadDirectory(this Activity activity) { - DirectoryInfo rootDir = new(Path.Combine(activity.GetExternalMediaDirs()?.FirstOrDefault()?.AbsolutePath ?? "/sdcard/")); - return rootDir.CreateSubdirectory("Download"); + var publicDownloadDir = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads)?.AbsolutePath; + DirectoryInfo downloadDir = new(publicDownloadDir ?? Path.Combine(activity.GetExternalMediaDirs()?.FirstOrDefault()?.AbsolutePath ?? "/sdcard/", "Download")); + if (!downloadDir.Exists) + downloadDir.Create(); + + return downloadDir; } } diff --git a/Nearby Sharing Windows/UIHelper.cs b/Nearby Sharing Windows/UIHelper.cs index 9355979..f3b30ab 100644 --- a/Nearby Sharing Windows/UIHelper.cs +++ b/Nearby Sharing Windows/UIHelper.cs @@ -5,7 +5,6 @@ using AndroidX.AppCompat.App; using AndroidX.Browser.CustomTabs; using AndroidX.Core.App; -using AndroidX.Core.Content; using Google.Android.Material.Dialog; using Nearby_Sharing_Windows.Settings; using CompatToolbar = AndroidX.AppCompat.Widget.Toolbar; @@ -61,7 +60,7 @@ public static void DisplayWebSite(Activity activity, string url) { CustomTabsIntent intent = new CustomTabsIntent.Builder() .Build(); - intent.LaunchUrl(activity, Android.Net.Uri.Parse(url)); + intent.LaunchUrl(activity, AndroidUri.Parse(url)); } public static void OpenLocaleSettings(Activity activity) @@ -90,22 +89,6 @@ public static void OpenLocaleSettings(Activity activity) } } - public static void OpenFile(Activity activity, string path) - { - Intent intent = new(Intent.ActionView); - var contentUri = FileProvider.GetUriForFile(activity, "de.shortdev.nearshare.FileProvider", new Java.IO.File(path))!; - - var mimeType = activity.ContentResolver?.GetType(contentUri); - if (string.IsNullOrEmpty(mimeType)) - intent.SetData(contentUri); - else - intent.SetDataAndType(contentUri, mimeType); - - intent.SetFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.NewTask); - var chooserIntent = Intent.CreateChooser(intent, $"Open {Path.GetFileName(path)}"); - activity.StartActivity(chooserIntent); - } - public static void ViewDownloads(this Activity activity) { Intent intent = new(DownloadManager.ActionViewDownloads);