From 23126db99caa046887bba6360f5abc6be4c16ccf Mon Sep 17 00:00:00 2001 From: Lukas Kurz Date: Tue, 22 Oct 2024 11:52:16 +0200 Subject: [PATCH] Visualize upgrade via icon --- .../NearShareSender.cs | 5 +- .../CdpSession.cs | 6 ++- .../ConnectOptions.cs | 8 ++++ .../ConnectedDevicesPlatform.cs | 4 +- .../Session/Upgrade/UpgradeHandler.cs | 13 ++++- src/SendActivity.cs | 47 ++++++++++++------- 6 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 lib/ShortDev.Microsoft.ConnectedDevices/ConnectOptions.cs diff --git a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs index 05f3074..68b4331 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs @@ -4,6 +4,7 @@ using ShortDev.Microsoft.ConnectedDevices.NearShare.Apps; using ShortDev.Microsoft.ConnectedDevices.NearShare.Messages; using ShortDev.Microsoft.ConnectedDevices.Serialization; +using ShortDev.Microsoft.ConnectedDevices.Transports; using System.Buffers; using System.Diagnostics; @@ -13,9 +14,11 @@ public sealed class NearShareSender(ConnectedDevicesPlatform platform) { public ConnectedDevicesPlatform Platform { get; } = platform; + public event EventHandler? TransportUpgraded; + async Task PrepareTransferInternalAsync(EndpointInfo endpoint, CancellationToken cancellationToken) { - var session = await Platform.ConnectAsync(endpoint); + var session = await Platform.ConnectAsync(endpoint, options: new() { TransportUpgraded = TransportUpgraded }); Guid operationId = Guid.NewGuid(); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs index 6c689fd..9413002 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs @@ -72,7 +72,7 @@ internal static CdpSession GetOrCreate(ConnectedDevicesPlatform platform, Endpoi ), out _); } - internal static async Task ConnectClientAsync(ConnectedDevicesPlatform platform, CdpSocket socket) + internal static async Task ConnectClientAsync(ConnectedDevicesPlatform platform, CdpSocket socket, ConnectOptions? options = null) { var session = _sessionRegistry.Create(localSessionId => new( platform, @@ -81,6 +81,10 @@ internal static async Task ConnectClientAsync(ConnectedDevicesPlatfo ), out _); var connectHandler = (ClientConnectHandler)session._connectHandler; + + if (options is not null) + connectHandler.UpgradeHandler.Upgraded += options.TransportUpgraded; + await connectHandler.ConnectAsync(socket); return session; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/ConnectOptions.cs b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectOptions.cs new file mode 100644 index 0000000..3b2998c --- /dev/null +++ b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectOptions.cs @@ -0,0 +1,8 @@ +using ShortDev.Microsoft.ConnectedDevices.Transports; + +namespace ShortDev.Microsoft.ConnectedDevices; + +public record ConnectOptions +{ + public EventHandler? TransportUpgraded { get; init; } +} diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs index 64adb3f..8610ef1 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs @@ -140,10 +140,10 @@ await Task.WhenAll(_transportMap.Values } } - public async Task ConnectAsync([NotNull] EndpointInfo endpoint) + public async Task ConnectAsync([NotNull] EndpointInfo endpoint, ConnectOptions? options = null) { var socket = await CreateSocketAsync(endpoint).ConfigureAwait(false); - return await CdpSession.ConnectClientAsync(this, socket).ConfigureAwait(false); + return await CdpSession.ConnectClientAsync(this, socket, options).ConfigureAwait(false); } internal async Task CreateSocketAsync(EndpointInfo endpoint) diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Session/Upgrade/UpgradeHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Session/Upgrade/UpgradeHandler.cs index 80b912d..0436100 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Session/Upgrade/UpgradeHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Session/Upgrade/UpgradeHandler.cs @@ -13,7 +13,18 @@ internal abstract class UpgradeHandler(CdpSession session, EndpointInfo initialE public bool IsSocketAllowed(CdpSocket socket) => _allowedAddresses.Contains(socket.Endpoint.Address); - public EndpointInfo RemoteEndpoint { get; protected set; } = initialEndpoint; + public event EventHandler? Upgraded; + + EndpointInfo _remoteEndpoint = initialEndpoint; + public EndpointInfo RemoteEndpoint + { + get => _remoteEndpoint; + protected set + { + _remoteEndpoint = value; + Upgraded?.Invoke(this, value.TransportType); + } + } public bool IsUpgradeSupported => (/* ToDo: header131Value & */ _session.ClientCapabilities & _session.HostCapabilities & PeerCapabilities.UpgradeSupport) != 0; diff --git a/src/SendActivity.cs b/src/SendActivity.cs index 471cae9..b8a9e93 100644 --- a/src/SendActivity.cs +++ b/src/SendActivity.cs @@ -30,7 +30,7 @@ namespace NearShare.Droid; [Activity(Label = "@string/app_name", Exported = true, Theme = "@style/AppTheme.TranslucentOverlay", ConfigurationChanges = UIHelper.ConfigChangesFlags)] public sealed class SendActivity : AppCompatActivity { - NearShareSender NearShareSender = null!; + NearShareSender _nearShareSender = null!; BottomSheetDialog _dialog = null!; RecyclerView DeviceDiscoveryListView = null!; @@ -78,13 +78,7 @@ protected override void OnCreate(Bundle? savedInstanceState) view.FindViewById(Resource.Id.deviceTypeImageView)!.SetImageResource( device.Type.IsMobile() ? Resource.Drawable.ic_fluent_phone_24_regular : Resource.Drawable.ic_fluent_desktop_24_regular ); - view.FindViewById(Resource.Id.transportTypeImageView)!.SetImageResource(device.Endpoint.TransportType switch - { - CdpTransportType.Tcp => Resource.Drawable.ic_fluent_wifi_1_20_regular, - CdpTransportType.Rfcomm => Resource.Drawable.ic_fluent_bluetooth_20_regular, - CdpTransportType.WifiDirect => Resource.Drawable.ic_fluent_live_20_regular, - _ => Resource.Drawable.ic_fluent_question_circle_20_regular - }); + view.FindViewById(Resource.Id.transportTypeImageView)!.SetImageResource(GetTransportIcon(device.Endpoint.TransportType)); view.FindViewById(Resource.Id.deviceNameTextView)!.Text = device.Name; view.Click += (s, e) => SendData(device); } @@ -137,7 +131,7 @@ void InitializePlatform() _cdp.DeviceDiscovered += Platform_DeviceDiscovered; _cdp.Discover(_discoverCancellationTokenSource.Token); - NearShareSender = new NearShareSender(_cdp); + _nearShareSender = new NearShareSender(_cdp); } readonly ObservableCollection RemoteSystems = []; @@ -190,13 +184,10 @@ private async void SendData(CdpDevice remoteSystem) sendingDataLayout.FindViewById(Resource.Id.deviceTypeImageView)!.SetImageResource( remoteSystem.Type.IsMobile() ? Resource.Drawable.ic_fluent_phone_24_regular : Resource.Drawable.ic_fluent_desktop_24_regular ); - sendingDataLayout.FindViewById(Resource.Id.transportTypeImageView)!.SetImageResource(remoteSystem.Endpoint.TransportType switch - { - CdpTransportType.Tcp => Resource.Drawable.ic_fluent_wifi_1_20_regular, - CdpTransportType.Rfcomm => Resource.Drawable.ic_fluent_bluetooth_20_regular, - CdpTransportType.WifiDirect => Resource.Drawable.ic_fluent_live_20_regular, - _ => Resource.Drawable.ic_fluent_question_circle_20_regular - }); + + var transportTypeImage = sendingDataLayout.FindViewById(Resource.Id.transportTypeImageView)!; + transportTypeImage.SetImageResource(GetTransportIcon(remoteSystem.Endpoint.TransportType)); + _nearShareSender.TransportUpgraded += OnTransportUpgrade; var deviceNameTextView = sendingDataLayout.FindViewById(Resource.Id.deviceNameTextView)!; var progressIndicator = sendingDataLayout.FindViewById(Resource.Id.sendProgressIndicator)!; @@ -220,7 +211,7 @@ private async void SendData(CdpDevice remoteSystem) if (files != null) { progress = new(); - transferPromise = NearShareSender.SendFilesAsync( + transferPromise = _nearShareSender.SendFilesAsync( remoteSystem, files, progress, @@ -229,7 +220,7 @@ private async void SendData(CdpDevice remoteSystem) } else if (uri != null) { - transferPromise = NearShareSender.SendUriAsync( + transferPromise = _nearShareSender.SendUriAsync( remoteSystem, uri ); @@ -308,6 +299,15 @@ private async void SendData(CdpDevice remoteSystem) progressIndicator.Indeterminate = false; progressIndicator.Progress = progressIndicator.Max; + + _nearShareSender.TransportUpgraded -= OnTransportUpgrade; + } + + void OnTransportUpgrade(object? sender, CdpTransportType transportType) + { + RunOnUiThread(() => + transportTypeImage.SetImageResource(GetTransportIcon(transportType)) + ); } } @@ -386,6 +386,17 @@ public override void Finish() _cdp?.Dispose(); } + static int GetTransportIcon(CdpTransportType transportType) + { + return transportType switch + { + CdpTransportType.Tcp => Resource.Drawable.ic_fluent_wifi_1_20_regular, + CdpTransportType.Rfcomm => Resource.Drawable.ic_fluent_bluetooth_20_regular, + CdpTransportType.WifiDirect => Resource.Drawable.ic_fluent_live_20_regular, + _ => Resource.Drawable.ic_fluent_question_circle_20_regular + }; + } + sealed class FinishActivityBottomSheetCallback(Activity activity) : BottomSheetBehavior.BottomSheetCallback { public override void OnSlide(View bottomSheet, float newState) { }