Skip to content

Commit

Permalink
ProcessText context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Oct 27, 2023
1 parent 52fd803 commit 0f5a635
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Nearby Sharing Windows/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Nearby_Sharing_Windows;

internal static class FileUtils
{
public static CdpFileProvider CreateNearShareFileFromContentUriAsync(this ContentResolver contentResolver, AndroidUri contentUri)
public static CdpFileProvider CreateNearShareFileFromContentUri(this ContentResolver contentResolver, AndroidUri contentUri)
{
var fileName = contentResolver.QueryContentName(contentUri);

Expand Down
1 change: 1 addition & 0 deletions Nearby Sharing Windows/Resources/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<!-- #region ReceiveActivity.cs -->
<string name="share_file">Datei teilen</string>
<string name="share_url">Webseite teilen</string>
<string name="share_text">Text teilen</string>

<string name="send_missing_permissions">Fehlende Berechtigungen!</string>
<string name="wait_for_acceptance">Warte auf Akzeptierung...</string>
Expand Down
1 change: 1 addition & 0 deletions Nearby Sharing Windows/Resources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<!-- #region ReceiveActivity.cs -->
<string name="share_file">Share File</string>
<string name="share_url">Share Url</string>
<string name="share_text">Share Text</string>

<string name="send_missing_permissions">Missing permission!</string>
<string name="wait_for_acceptance">Waiting for acceptance...</string>
Expand Down
115 changes: 74 additions & 41 deletions Nearby Sharing Windows/SendActivity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Android.Bluetooth;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
Expand All @@ -15,12 +14,12 @@
using ShortDev.Microsoft.ConnectedDevices.NearShare;
using ShortDev.Microsoft.ConnectedDevices.Platforms;
using ShortDev.Microsoft.ConnectedDevices.Transports;
using ShortDev.Networking;
using System.Diagnostics.CodeAnalysis;
using System.Net.NetworkInformation;

namespace Nearby_Sharing_Windows;

[IntentFilter(new[] { Intent.ActionProcessText }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "text/plain", Label = "@string/share_text")]
[IntentFilter(new[] { Intent.ActionSend, Intent.ActionSendMultiple }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "*/*", Label = "@string/share_file")]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "text/plain", Label = "@string/share_url")]
[Activity(Label = "@string/app_name", Exported = true, Theme = "@style/AppTheme.TranslucentOverlay", ConfigurationChanges = UIHelper.ConfigChangesFlags)]
Expand Down Expand Up @@ -172,51 +171,26 @@ private async void SendData(CdpDevice remoteSystem)
try
{
Task? fileTransferOperation = null;
Progress<NearShareProgress> fileSendProgress = new();
Progress<NearShareProgress> progress = new();
Task? uriTransferOperation = null;
if (Intent?.Action == Intent.ActionSend)
{
if (Intent.HasExtra(Intent.ExtraStream))
{
AndroidUri file = Intent.GetParcelableExtra<AndroidUri>(Intent.ExtraStream)!;
fileTransferOperation = NearShareSender.SendFileAsync(
remoteSystem,
ContentResolver!.CreateNearShareFileFromContentUriAsync(file),
fileSendProgress,
_fileSendCancellationTokenSource.Token
);
}
else
{
var text = Intent.GetStringExtra(Intent.ExtraText);
if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
{
uriTransferOperation = NearShareSender.SendUriAsync(
remoteSystem,
new Uri(text)
);
}
else
{
fileTransferOperation = NearShareSender.SendFileAsync(
remoteSystem,
CdpFileProvider.FromContent($"Text-Transfer-{DateTime.Now:dd_MM_yyyy-HH_mm_ss}.txt", text ?? throw new NullReferenceException("Text was null")),
fileSendProgress,
_fileSendCancellationTokenSource.Token
);
}
}
}
else if (Intent?.Action == Intent.ActionSendMultiple)

var (files, uri) = ParseIntentAsync();
if (files != null)
{
var files = Intent.GetParcelableArrayListExtra<AndroidUri>(Intent.ExtraStream) ?? throw new InvalidDataException("Could not get extra files from intent");
fileTransferOperation = NearShareSender.SendFilesAsync(
remoteSystem,
files.Select(x => ContentResolver!.CreateNearShareFileFromContentUriAsync(x)).ToArray(),
fileSendProgress,
files,
progress,
_fileSendCancellationTokenSource.Token
);
}
else if (uri != null)
{
uriTransferOperation = NearShareSender.SendUriAsync(
remoteSystem,
uri
);
}

FindViewById<View>(Resource.Id.selectDeviceLayout)!.Visibility = ViewStates.Gone;
FindViewById<View>(Resource.Id.sendingDataLayout)!.Visibility = ViewStates.Visible;
Expand All @@ -226,7 +200,7 @@ private async void SendData(CdpDevice remoteSystem)
CircularProgressIndicator progressIndicator = FindViewById<CircularProgressIndicator>(Resource.Id.sendProgressIndicator)!;
if (fileTransferOperation != null)
{
fileSendProgress.ProgressChanged += (s, args) =>
progress.ProgressChanged += (s, args) =>
{
RunOnUiThread(() =>
{
Expand Down Expand Up @@ -278,6 +252,65 @@ private async void SendData(CdpDevice remoteSystem)
}
}

(IReadOnlyList<CdpFileProvider>? files, Uri? uri) ParseIntentAsync()
{
ArgumentNullException.ThrowIfNull(Intent);

if (Intent.Action == Intent.ActionProcessText)
{
return (
files: new[] { SendText(Intent.GetStringExtra(Intent.ExtraProcessText)) },
null
);
}

if (Intent.Action == Intent.ActionSendMultiple)
{
return (
files: (Intent.GetParcelableArrayListExtra<AndroidUri>(Intent.ExtraStream) ?? throw new InvalidDataException("Could not get extra files from intent"))
.Select(ContentResolver!.CreateNearShareFileFromContentUri)
.ToArray(),
null
);
}

if (Intent.Action == Intent.ActionSend)
{
if (Intent.HasExtra(Intent.ExtraStream))
{
AndroidUri fileUri = Intent.GetParcelableExtra<AndroidUri>(Intent.ExtraStream) ?? throw new InvalidDataException("Could not get ExtraStream");
return (
files: new[] { ContentResolver!.CreateNearShareFileFromContentUri(fileUri) },
null
);
}

var text = Intent.GetStringExtra(Intent.ExtraText) ?? "";
if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
{
return (
null,
uri: new(text)
);
}

return (
files: new[] { SendText(text) },
null
);
}

return (null, null);

static CdpFileProvider SendText(string? text)
{
return CdpFileProvider.FromContent(
$"Text-Transfer-{DateTime.Now:dd_MM_yyyy-HH_mm_ss}.txt",
text ?? throw new NullReferenceException("Text was null")
);
}
}

void OnRequestAccepted()
{
FindViewById(Resource.Id.loadingProgressIndicator)!.Visibility = ViewStates.Gone;
Expand Down
16 changes: 8 additions & 8 deletions ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task SendUriAsync(CdpDevice device, Uri uri)
public async Task SendFileAsync(CdpDevice device, CdpFileProvider file, IProgress<NearShareProgress> progress, CancellationToken cancellationToken = default)
=> await SendFilesAsync(device, new[] { file }, progress, cancellationToken);

public async Task SendFilesAsync(CdpDevice device, CdpFileProvider[] files, IProgress<NearShareProgress> progress, CancellationToken cancellationToken = default)
public async Task SendFilesAsync(CdpDevice device, IReadOnlyList<CdpFileProvider> files, IProgress<NearShareProgress> progress, CancellationToken cancellationToken = default)
{
var senderStateMachine = await PrepareTransferInternalAsync(device);
await senderStateMachine.SendFilesAsync(files, progress, cancellationToken);
Expand Down Expand Up @@ -100,17 +100,17 @@ public async Task SendUriAsync(Uri uri)
await _promise.Task;
}

CdpFileProvider[]? _files;
IReadOnlyList<CdpFileProvider>? _files;
IProgress<NearShareProgress>? _fileProgress;
CancellationToken? _fileCancellationToken;
ulong _bytesToSend;
public async Task SendFilesAsync(CdpFileProvider[] files, IProgress<NearShareProgress> progress, CancellationToken cancellationToken)
public async Task SendFilesAsync(IReadOnlyList<CdpFileProvider> files, IProgress<NearShareProgress> progress, CancellationToken cancellationToken)
{
_files = files;
_fileProgress = progress;
_fileCancellationToken = cancellationToken;

uint fileCount = (uint)files.Length;
uint fileCount = (uint)files.Count;
_bytesToSend = CalcBytesToSend(files);

ValueSet valueSet = new();
Expand Down Expand Up @@ -142,10 +142,10 @@ static uint[] GenerateContentIds(uint fileCount)
return ids;
}

static ulong CalcBytesToSend(CdpFileProvider[] files)
static ulong CalcBytesToSend(IReadOnlyList<CdpFileProvider> files)
{
ulong sum = 0;
for (int i = 0; i < files.Length; i++)
for (int i = 0; i < files.Count; i++)
sum += files[i].FileSize;
return sum;
}
Expand Down Expand Up @@ -187,15 +187,15 @@ void HandleDataRequest(BinaryMsgHeader header, ValueSet payload)
var start = payload.Get<ulong>("BlobPosition");
var length = payload.Get<uint>("BlobSize");

var fileProvider = _files?[contentId] ?? throw new NullReferenceException("Could not access files to transfer");
var fileProvider = _files?[(int)contentId] ?? throw new NullReferenceException("Could not access files to transfer");
var blob = fileProvider.ReadBlob(start, length);

_fileProgress?.Report(new()
{
BytesSent = Interlocked.Add(ref _bytesSent, length),
FilesSent = contentId + 1, // ToDo: How to calculate?
TotalBytesToSend = _bytesToSend,
TotalFilesToSend = (uint)_files.Length
TotalFilesToSend = (uint)_files.Count
});

ValueSet response = new();
Expand Down

0 comments on commit 0f5a635

Please sign in to comment.