Skip to content

Commit

Permalink
Visualize upgrade via icon
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Oct 22, 2024
1 parent 66500f4 commit 23126db
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,9 +14,11 @@ public sealed class NearShareSender(ConnectedDevicesPlatform platform)
{
public ConnectedDevicesPlatform Platform { get; } = platform;

public event EventHandler<CdpTransportType>? TransportUpgraded;

async Task<SenderStateMachine> 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();

Expand Down
6 changes: 5 additions & 1 deletion lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal static CdpSession GetOrCreate(ConnectedDevicesPlatform platform, Endpoi
), out _);
}

internal static async Task<CdpSession> ConnectClientAsync(ConnectedDevicesPlatform platform, CdpSocket socket)
internal static async Task<CdpSession> ConnectClientAsync(ConnectedDevicesPlatform platform, CdpSocket socket, ConnectOptions? options = null)
{
var session = _sessionRegistry.Create(localSessionId => new(
platform,
Expand All @@ -81,6 +81,10 @@ internal static async Task<CdpSession> ConnectClientAsync(ConnectedDevicesPlatfo
), out _);

var connectHandler = (ClientConnectHandler)session._connectHandler;

if (options is not null)
connectHandler.UpgradeHandler.Upgraded += options.TransportUpgraded;

await connectHandler.ConnectAsync(socket);

return session;
Expand Down
8 changes: 8 additions & 0 deletions lib/ShortDev.Microsoft.ConnectedDevices/ConnectOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ShortDev.Microsoft.ConnectedDevices.Transports;

namespace ShortDev.Microsoft.ConnectedDevices;

public record ConnectOptions
{
public EventHandler<CdpTransportType>? TransportUpgraded { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ await Task.WhenAll(_transportMap.Values
}
}

public async Task<CdpSession> ConnectAsync([NotNull] EndpointInfo endpoint)
public async Task<CdpSession> 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<CdpSocket> CreateSocketAsync(EndpointInfo endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CdpTransportType>? 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;
Expand Down
47 changes: 29 additions & 18 deletions src/SendActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -78,13 +78,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
view.FindViewById<ImageView>(Resource.Id.deviceTypeImageView)!.SetImageResource(
device.Type.IsMobile() ? Resource.Drawable.ic_fluent_phone_24_regular : Resource.Drawable.ic_fluent_desktop_24_regular
);
view.FindViewById<ImageView>(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<ImageView>(Resource.Id.transportTypeImageView)!.SetImageResource(GetTransportIcon(device.Endpoint.TransportType));
view.FindViewById<TextView>(Resource.Id.deviceNameTextView)!.Text = device.Name;
view.Click += (s, e) => SendData(device);
}
Expand Down Expand Up @@ -137,7 +131,7 @@ void InitializePlatform()
_cdp.DeviceDiscovered += Platform_DeviceDiscovered;
_cdp.Discover(_discoverCancellationTokenSource.Token);

NearShareSender = new NearShareSender(_cdp);
_nearShareSender = new NearShareSender(_cdp);
}

readonly ObservableCollection<CdpDevice> RemoteSystems = [];
Expand Down Expand Up @@ -190,13 +184,10 @@ private async void SendData(CdpDevice remoteSystem)
sendingDataLayout.FindViewById<ImageView>(Resource.Id.deviceTypeImageView)!.SetImageResource(
remoteSystem.Type.IsMobile() ? Resource.Drawable.ic_fluent_phone_24_regular : Resource.Drawable.ic_fluent_desktop_24_regular
);
sendingDataLayout.FindViewById<ImageView>(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<ImageView>(Resource.Id.transportTypeImageView)!;
transportTypeImage.SetImageResource(GetTransportIcon(remoteSystem.Endpoint.TransportType));
_nearShareSender.TransportUpgraded += OnTransportUpgrade;

var deviceNameTextView = sendingDataLayout.FindViewById<TextView>(Resource.Id.deviceNameTextView)!;
var progressIndicator = sendingDataLayout.FindViewById<CircularProgressIndicator>(Resource.Id.sendProgressIndicator)!;
Expand All @@ -220,7 +211,7 @@ private async void SendData(CdpDevice remoteSystem)
if (files != null)
{
progress = new();
transferPromise = NearShareSender.SendFilesAsync(
transferPromise = _nearShareSender.SendFilesAsync(
remoteSystem,
files,
progress,
Expand All @@ -229,7 +220,7 @@ private async void SendData(CdpDevice remoteSystem)
}
else if (uri != null)
{
transferPromise = NearShareSender.SendUriAsync(
transferPromise = _nearShareSender.SendUriAsync(
remoteSystem,
uri
);
Expand Down Expand Up @@ -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))
);
}
}

Expand Down Expand Up @@ -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) { }
Expand Down

0 comments on commit 23126db

Please sign in to comment.