Skip to content

Commit

Permalink
Logging for permission result
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Nov 5, 2023
1 parent ec9d11a commit 4bb3d3e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 24 deletions.
9 changes: 9 additions & 0 deletions Nearby Sharing Windows/AppLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Logging;

namespace Nearby_Sharing_Windows;

internal static partial class AppLog
{
[LoggerMessage(EventId = 1, Level = LogLevel.Debug, Message = "Permission result (code {RequestCode}) for permissions {Permissions} with result {GrantResults}")]
public static partial void RequestPermissionResult(this ILogger logger, int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults);
}
17 changes: 13 additions & 4 deletions Nearby Sharing Windows/ReceiveActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using AndroidX.RecyclerView.Widget;
using Google.Android.Material.Dialog;
using Google.Android.Material.ProgressIndicator;
using Microsoft.Extensions.Logging;
using Nearby_Sharing_Windows.Settings;
using ShortDev.Android.UI;
using ShortDev.Microsoft.ConnectedDevices;
Expand All @@ -33,6 +34,9 @@ public sealed class ReceiveActivity : AppCompatActivity
readonly List<TransferToken> _notifications = new();

PhysicalAddress? btAddress = null;

ILogger<ReceiveActivity> _logger = null!;
ILoggerFactory _loggerFactory = null!;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
Expand All @@ -47,7 +51,6 @@ protected override void OnCreate(Bundle? savedInstanceState)

SetContentView(Resource.Layout.activity_receive);

UIHelper.RequestReceivePermissions(this);
UIHelper.SetupToolBar(this, GetString(Resource.String.app_titlebar_title_receive));

notificationsRecyclerView = FindViewById<RecyclerView>(Resource.Id.notificationsRecyclerView)!;
Expand All @@ -56,6 +59,11 @@ protected override void OnCreate(Bundle? savedInstanceState)
FindViewById<Button>(Resource.Id.openFAQButton)!.Click += (s, e) => UIHelper.OpenFAQ(this);

adapterDescriptor = new(Resource.Layout.item_transfer_notification, OnInflateNotification);

_loggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern());
_logger = _loggerFactory.CreateLogger<ReceiveActivity>();

UIHelper.RequestReceivePermissions(this);
}

void OnInflateNotification(View view, TransferToken transfer)
Expand Down Expand Up @@ -187,9 +195,8 @@ void InitializeCDP()
Name = deviceName,
OemModelName = Build.Model ?? string.Empty,
OemManufacturerName = Build.Manufacturer ?? string.Empty,
DeviceCertificate = ConnectedDevicesPlatform.CreateDeviceCertificate(CdpEncryptionParams.Default),
LoggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern())
});
DeviceCertificate = ConnectedDevicesPlatform.CreateDeviceCertificate(CdpEncryptionParams.Default)
}, _loggerFactory);

IBluetoothHandler bluetoothHandler = new AndroidBluetoothHandler(_btAdapter, btAddress);
_cdp.AddTransport<BluetoothTransport>(new(bluetoothHandler));
Expand All @@ -214,6 +221,8 @@ void InitializeCDP()

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
_logger.RequestPermissionResult(requestCode, permissions, grantResults);

if (grantResults.Contains(Permission.Denied))
{
Toast.MakeText(this, this.Localize(Resource.String.receive_missing_permissions), ToastLength.Long)!.Show();
Expand Down
14 changes: 11 additions & 3 deletions Nearby Sharing Windows/SendActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using AndroidX.RecyclerView.Widget;
using Google.Android.Material.ProgressIndicator;
using Google.Android.Material.Snackbar;
using Microsoft.Extensions.Logging;
using Nearby_Sharing_Windows.Settings;
using ShortDev.Android.UI;
using ShortDev.Microsoft.ConnectedDevices;
Expand All @@ -31,6 +32,9 @@ public sealed class SendActivity : AppCompatActivity, View.IOnApplyWindowInsetsL
[AllowNull] TextView StatusTextView;
[AllowNull] FrameLayout bottomSheetFrame;
[AllowNull] Button cancelButton;

ILogger<SendActivity> _logger = null!;
ILoggerFactory _loggerFactory = null!;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
Expand Down Expand Up @@ -69,6 +73,9 @@ protected override void OnCreate(Bundle? savedInstanceState)

cancelButton.Click += CancelButton_Click;

_loggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern());
_logger = _loggerFactory.CreateLogger<SendActivity>();

UIHelper.RequestSendPermissions(this);
}

Expand Down Expand Up @@ -102,6 +109,8 @@ public WindowInsets OnApplyWindowInsets(View? v, WindowInsets? windowInsets)

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
_logger.RequestPermissionResult(requestCode, permissions, grantResults);

if (grantResults.Contains(Android.Content.PM.Permission.Denied))
{
Snackbar.Make(Window!.DecorView, GetString(Resource.String.send_missing_permissions), Snackbar.LengthLong).Show();
Expand All @@ -124,9 +133,8 @@ void InitializePlatform()
Name = SettingsFragment.GetDeviceName(this, adapter),
OemModelName = Build.Model ?? string.Empty,
OemManufacturerName = Build.Manufacturer ?? string.Empty,
DeviceCertificate = ConnectedDevicesPlatform.CreateDeviceCertificate(CdpEncryptionParams.Default),
LoggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern())
});
DeviceCertificate = ConnectedDevicesPlatform.CreateDeviceCertificate(CdpEncryptionParams.Default)
}, _loggerFactory);

AndroidBluetoothHandler bluetoothHandler = new(adapter, PhysicalAddress.None);
Platform.AddTransport<BluetoothTransport>(new(bluetoothHandler));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using System.Collections;
using System.Runtime.InteropServices;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare.Internal;
namespace ShortDev.Microsoft.ConnectedDevices.NearShare.Apps;

internal sealed class NearShareApp(ConnectedDevicesPlatform cdp) : CdpAppBase(cdp)
{
const uint PartitionSize = 102400u; // 131072u
public static string Name { get; } = "NearSharePlatform";

readonly ILogger<NearShareApp> _logger = cdp.DeviceInfo.LoggerFactory.CreateLogger<NearShareApp>();
readonly ILogger<NearShareApp> _logger = cdp.CreateLogger<NearShareApp>();

public required string Id { get; init; }

Expand Down Expand Up @@ -49,7 +49,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload)
{
var fileNames = payload.Get<List<string>>("FileNames");

_logger.LogInformation("Receiving file {FileNames} from session {SessionId:X} via {TransportType}",
_logger.ReceivingFile(
fileNames,
msg.Header.SessionId,
Channel.Socket.TransportType
Expand Down Expand Up @@ -79,7 +79,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload)
case DataKind.Uri:
{
var uri = payload.Get<string>("Uri");
_logger.LogInformation("Received uri {Uri} from session {SessionId:X} via {TransportType}",
_logger.ReceivedUrl(
uri,
msg.Header.SessionId,
Channel.Socket.TransportType
Expand Down Expand Up @@ -162,7 +162,6 @@ void HandleFetchDataResponse(ValueSet payload)
return;
}

// PlatformHandler.Log(0, $"BlobPosition: {position}; ({newPosition * 100 / bytesToSend}%)");
lock (_fileTransferToken)
{
var stream = _fileTransferToken.GetStream(contentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ShortDev.Microsoft.ConnectedDevices.Messages;
using ShortDev.Microsoft.ConnectedDevices.Serialization;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare.Internal;
namespace ShortDev.Microsoft.ConnectedDevices.NearShare.Apps;

public class NearShareHandshakeApp(ConnectedDevicesPlatform cdp) : CdpAppBase(cdp), ICdpAppId
{
Expand Down
13 changes: 13 additions & 0 deletions ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.Logging;
using ShortDev.Microsoft.ConnectedDevices.Transports;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare;

internal static partial class NearShareLog
{
[LoggerMessage(EventId = 501, Level = LogLevel.Information, Message = "Receiving file {FileNames} from session {SessionId:X} via {TransportType}")]
public static partial void ReceivingFile(this ILogger logger, IEnumerable<string> fileNames, ulong sessionId, CdpTransportType transportType);

[LoggerMessage(EventId = 502, Level = LogLevel.Information, Message = "Received uri {Uri} from session {SessionId:X} via {TransportType}")]
public static partial void ReceivedUrl(this ILogger logger, string uri, ulong sessionId, CdpTransportType transportType);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ShortDev.Microsoft.ConnectedDevices.NearShare.Internal;
using ShortDev.Microsoft.ConnectedDevices.NearShare.Apps;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ShortDev.Microsoft.ConnectedDevices.Exceptions;
using ShortDev.Microsoft.ConnectedDevices.Messages;
using ShortDev.Microsoft.ConnectedDevices.Messages.Session;
using ShortDev.Microsoft.ConnectedDevices.NearShare.Internal;
using ShortDev.Microsoft.ConnectedDevices.NearShare.Apps;
using ShortDev.Microsoft.ConnectedDevices.NearShare.Messages;
using ShortDev.Microsoft.ConnectedDevices.Platforms;
using ShortDev.Microsoft.ConnectedDevices.Serialization;
Expand Down
4 changes: 2 additions & 2 deletions ShortDev.Microsoft.ConnectedDevices/CdpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private CdpSession(ConnectedDevicesPlatform platform, CdpDevice device, SessionI
Device = device;
SessionId = sessionId;

_logger = platform.DeviceInfo.LoggerFactory.CreateLogger<CdpSession>();
_logger = platform.CreateLogger<CdpSession>();
_upgradeHandler = new(this, device);
_connectHandler = new(this, _upgradeHandler);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ public void HandleMessage(CdpSocket socket, CommonHeader header, ref EndianReade
CdpEncryptionInfo? _remoteEncryption = null;
sealed class ConnectHandler(CdpSession session, UpgradeHandler upgradeHandler)
{
readonly ILogger<ConnectHandler> _logger = session.Platform.DeviceInfo.LoggerFactory.CreateLogger<ConnectHandler>();
readonly ILogger<ConnectHandler> _logger = session.Platform.CreateLogger<ConnectHandler>();
readonly CdpSession _session = session;
readonly UpgradeHandler _upgradeHandler = upgradeHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

namespace ShortDev.Microsoft.ConnectedDevices;

public sealed class ConnectedDevicesPlatform(LocalDeviceInfo deviceInfo) : IDisposable
public sealed class ConnectedDevicesPlatform(LocalDeviceInfo deviceInfo, ILoggerFactory loggerFactory) : IDisposable
{
public LocalDeviceInfo DeviceInfo { get; } = deviceInfo;

readonly ILogger<ConnectedDevicesPlatform> _logger = deviceInfo.LoggerFactory.CreateLogger<ConnectedDevicesPlatform>();
readonly ILogger<ConnectedDevicesPlatform> _logger = loggerFactory.CreateLogger<ConnectedDevicesPlatform>();

#region Transport
readonly ConcurrentDictionary<Type, ICdpTransport> _transports = new();
Expand Down Expand Up @@ -261,6 +261,9 @@ public CdpDeviceInfo GetCdpDeviceInfo()
return DeviceInfo.ToCdpDeviceInfo(endpoints);
}

public ILogger<T> CreateLogger<T>()
=> loggerFactory.CreateLogger<T>();

public void Dispose()
{
Extensions.DisposeAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class UpgradeHandler
public UpgradeHandler(CdpSession session, CdpDevice initalDevice)
{
_session = session;
_logger = session.Platform.DeviceInfo.LoggerFactory.CreateLogger<UpgradeHandler>();
_logger = session.Platform.CreateLogger<UpgradeHandler>();

// Initial address is always allowed
_allowedAddresses.Add(initalDevice.Endpoint.Address);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo;
using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo;
using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade;
using System;
using System.Collections.Generic;
Expand All @@ -17,8 +16,6 @@ public sealed class LocalDeviceInfo

public required X509Certificate2 DeviceCertificate { get; init; }

public required ILoggerFactory LoggerFactory { get; init; }

public required string OemManufacturerName { get; init; }
public required string OemModelName { get; init; }

Expand Down

0 comments on commit 4bb3d3e

Please sign in to comment.