Skip to content

Commit

Permalink
fix: FileSavePicker does not return the picked file in Android
Browse files Browse the repository at this point in the history
  • Loading branch information
aesteves900 committed Apr 24, 2022
1 parent 19fbedf commit 9be9dd1
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions src/Uno.UWP/Storage/Pickers/FileSavePicker.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,14 @@
using Android.App;
using Android.Content;
using Uno.UI;
using Uno.Helpers.Activities;



namespace Windows.Storage.Pickers
{
public partial class FileSavePicker
{
internal const int RequestCode = 6003;
private static TaskCompletionSource<Intent?>? _currentFileOpenPickerRequest;

internal static bool TryHandleIntent(Intent intent, Result resultCode)
{
if (_currentFileOpenPickerRequest == null)
{
return false;
}
if (resultCode == Result.Canceled)
{
_currentFileOpenPickerRequest.SetResult(null);
}
else
{
_currentFileOpenPickerRequest.SetResult(intent);
}
return true;
}

private async Task<StorageFile?> PickSaveFileTaskAsync(CancellationToken token)
{
if (!(ContextHelper.Current is Activity appActivity))
Expand All @@ -43,21 +26,24 @@ internal static bool TryHandleIntent(Intent intent, Result resultCode)

var intent = new Intent(action);
intent.SetType("*/*");

intent.AddCategory(Intent.CategoryOpenable);
var mimeTypes = GetMimeTypes();
intent.PutExtra(Intent.ExtraMimeTypes, mimeTypes);

this.ParseMimeTypeForXml(mimeTypes);

foreach (var mime in mimeTypes)
{
intent.PutExtra(Intent.ExtraMimeTypes, mime);
}

if (!string.IsNullOrEmpty(SuggestedFileName))
{
intent.PutExtra(Intent.ExtraTitle, SuggestedFileName);
}

_currentFileOpenPickerRequest = new TaskCompletionSource<Intent?>();

appActivity.StartActivityForResult(intent, RequestCode);

var resultIntent = await _currentFileOpenPickerRequest.Task;
_currentFileOpenPickerRequest = null;
var activity = await AwaitableResultActivity.StartAsync();
var result = await activity.StartActivityForResultAsync(intent, cacellationToken: token);
var resultIntent = result?.Intent;

if (resultIntent?.Data != null)
{
Expand All @@ -78,6 +64,20 @@ internal static bool TryHandleIntent(Intent intent, Result resultCode)
return null;
}

private void ParseMimeTypeForXml(string[]? mimes)
{
if (mimes is not null)
{
for (int i = 0; i < mimes.Length; i++)
{
if (mimes[i] == XmlIncorrectMimeToActionCreateDocument)
{
mimes[i] = XmlCorrectMimeToActionCreateDocument;
}
}
}
}

private string[] GetMimeTypes()
{
return FileTypeChoices
Expand All @@ -87,5 +87,8 @@ private string[] GetMimeTypes()
.ToArray();
}

private const string XmlCorrectMimeToActionCreateDocument = "application/xhtml+xml";
private const string XmlIncorrectMimeToActionCreateDocument = "application/xml";

}
}

0 comments on commit 9be9dd1

Please sign in to comment.