-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44146c9
commit 8025d40
Showing
34 changed files
with
361 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
GalaxyBudsClient.Platform.Linux/NotificationListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GalaxyBudsClient.Platform.Interfaces; | ||
using GalaxyBudsClient.Platform.Model; | ||
using Serilog; | ||
using Tmds.DBus.Protocol; | ||
|
||
namespace GalaxyBudsClient.Platform.Linux; | ||
|
||
public class NotificationListener : INotificationListener | ||
{ | ||
public bool IsEnabled | ||
{ | ||
get => _isEnabled; | ||
set | ||
{ | ||
_isEnabled = value; | ||
if (value) | ||
{ | ||
_cancelSource = new CancellationTokenSource(); | ||
_loop = Task.Run(WatchBusAsync, _cancelSource.Token); | ||
} | ||
else | ||
_cancelSource.Cancel(); | ||
} | ||
} | ||
|
||
public event EventHandler<Notification>? NotificationReceived; | ||
|
||
private CancellationTokenSource _cancelSource = new(); | ||
private Task _loop; | ||
Check warning on line 33 in GalaxyBudsClient.Platform.Linux/NotificationListener.cs GitHub Actions / build-arm
Check warning on line 33 in GalaxyBudsClient.Platform.Linux/NotificationListener.cs GitHub Actions / build-x64
Check warning on line 33 in GalaxyBudsClient.Platform.Linux/NotificationListener.cs GitHub Actions / build-x64-musl
|
||
private bool _isEnabled; | ||
|
||
private record NotifyMessageBody | ||
{ | ||
public required string AppName { get; init; } | ||
public required uint ReplacesId { get; init; } | ||
public required string AppIcon { get; init; } | ||
public required string Summary { get; init; } | ||
public required string Body { get; init; } | ||
public required string[] Actions { get; init; } | ||
public required Dictionary<string, object> Hints { get; init; } | ||
public required int ExpireTimeout { get; init; } | ||
} | ||
|
||
private NotifyMessageBody? ReadNotifyMessage(Reader reader) | ||
{ | ||
try | ||
{ | ||
return new NotifyMessageBody | ||
{ | ||
AppName = reader.ReadString(), | ||
ReplacesId = reader.ReadUInt32(), | ||
AppIcon = reader.ReadString(), | ||
Summary = reader.ReadString(), | ||
Body = reader.ReadString(), | ||
Actions = reader.ReadArray<string>(), | ||
Hints = reader.ReadDictionary<string, object>(), | ||
ExpireTimeout = reader.ReadInt32() | ||
}; | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "Failed to read Notify message body"); | ||
return null; | ||
} | ||
} | ||
|
||
private async Task WatchBusAsync() | ||
{ | ||
Log.Information("Notification listener started"); | ||
|
||
string address = Address.Session ?? throw new ArgumentNullException(nameof(address)); | ||
|
||
var rules = new List<MatchRule> | ||
{ | ||
new() | ||
{ | ||
Type = MessageType.MethodCall, | ||
Path = "/org/freedesktop/Notifications", | ||
Interface = "org.freedesktop.Notifications", | ||
Member = "Notify" | ||
} | ||
}; | ||
|
||
var token = _cancelSource.Token; | ||
|
||
while (token.IsCancellationRequested == false) | ||
{ | ||
await foreach (var dmsg in Connection.MonitorBusAsync(address, rules, token)) | ||
{ | ||
using var _ = dmsg; | ||
var msg = dmsg.Message; | ||
|
||
if (msg.MemberAsString != "Notify") | ||
continue; | ||
|
||
var body = ReadNotifyMessage(msg.GetBodyReader()); | ||
if (body is null) | ||
continue; | ||
|
||
NotificationReceived?.Invoke(this, | ||
new Notification(body.Summary, body.Body, body.AppName, body.AppName)); | ||
} | ||
} | ||
|
||
Log.Information("Notification listener stopped"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
using System; | ||
using GalaxyBudsClient.Platform.Model; | ||
|
||
namespace GalaxyBudsClient.Platform.Interfaces; | ||
|
||
public interface INotificationListener | ||
{ | ||
|
||
public bool IsEnabled { set; get; } | ||
public event EventHandler<Notification>? NotificationReceived; | ||
} |
2 changes: 1 addition & 1 deletion
2
GalaxyBudsClient.Platform/BluetoothCoD.cs → ...BudsClient.Platform/Model/BluetoothCoD.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace GalaxyBudsClient.Platform | ||
namespace GalaxyBudsClient.Platform.Model | ||
{ | ||
public class BluetoothCoD | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace GalaxyBudsClient.Platform.Model; | ||
|
||
public class BluetoothDevice(string name, string address, bool isConnected, bool isPaired, BluetoothCoD cod, Guid[]? serviceUuids = null) | ||
{ | ||
protected BluetoothDevice(uint cod) : this(string.Empty, string.Empty, false, false, new BluetoothCoD(cod)) | ||
{ | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Name} ({Address})"; //$"BluetoothDevice[Name={Name},Address={Address},IsConnected={IsConnected},IsPaired='{IsPaired}',CoD='{Class}']"; | ||
} | ||
|
||
public virtual string Name { get; } = name; | ||
public virtual string Address { get; } = address; | ||
public virtual bool IsConnected { get; } = isConnected; | ||
public virtual bool IsPaired { get; } = isPaired; | ||
public BluetoothCoD Class { get; } = cod; | ||
public virtual Guid[]? ServiceUuids { get; } = serviceUuids; | ||
|
||
public static IEnumerable<BluetoothDevice> DummyDevices() | ||
{ | ||
/* Dummy devices for testing */ | ||
var cod = new BluetoothCoD(0); | ||
return new BluetoothDevice[] | ||
{ | ||
new("Galaxy Buds (36FD) [Dummy]", "36:AB:38:F5:04:FD", true, true, cod), | ||
new("Galaxy Buds+ (A2D5) [Dummy]", "A2:BF:D4:4A:52:D5", true, true, cod), | ||
new("Galaxy Buds Live (4AC3) [Dummy]", "4A:6B:87:E5:12:C3", true, true, cod), | ||
new("Galaxy Buds Pro (E43F) [Dummy]", "E4:25:FA:6D:B9:3F", true, true, cod), | ||
new("Galaxy Buds2 (D592) [Dummy]", "D5:97:B8:23:AB:92", true, true, cod), | ||
new("Galaxy Buds2 Pro (3292) [Dummy]", "32:97:B8:23:AB:92", true, true, cod), | ||
new("Galaxy Buds FE (A7D4) [Dummy]", "A7:97:B8:23:AB:D4", true, true, cod) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace GalaxyBudsClient.Platform.Model; | ||
|
||
/** | ||
* <param name="Title">The title of the notification</param> | ||
* <param name="Content">The text content of the notification</param> | ||
* <param name="AppName">A user-friendly name of the application that sent the notification</param> | ||
* <param name="AppId">An unique identifier of the sender application</param> | ||
*/ | ||
public record Notification( | ||
string Title, | ||
string Content, | ||
string AppName, | ||
string AppId | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
GalaxyBudsClient.Platform/Stubs/NotificationListener.Dummy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
using System; | ||
using GalaxyBudsClient.Platform.Interfaces; | ||
using GalaxyBudsClient.Platform.Model; | ||
|
||
namespace GalaxyBudsClient.Platform.Stubs; | ||
|
||
public class DummyNotificationListener : INotificationListener | ||
{ | ||
public bool IsEnabled { get; set; } | ||
|
||
#pragma warning disable CS0067 | ||
public event EventHandler<Notification>? NotificationReceived; | ||
#pragma warning restore CS0067 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.