Skip to content

Commit

Permalink
Save to public Download directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Sep 1, 2023
1 parent a1d7cee commit 561a954
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 46 deletions.
37 changes: 9 additions & 28 deletions Nearby Sharing Windows/FileUtils.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
}
19 changes: 1 addition & 18 deletions Nearby Sharing Windows/UIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 561a954

Please sign in to comment.