From de6ba209d32a50a8b3f0fbd0dc4542c9b4f2ea92 Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Sun, 27 Dec 2020 14:54:18 +0100 Subject: [PATCH] working notificationstate service --- Program.cs | 14 ++++- .../UserNotificationStateDetector.cs | 59 +++++++++++++++++++ Worker.cs | 7 ++- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 StateDetectors/FullscreenDetector/UserNotificationStateDetector.cs diff --git a/Program.cs b/Program.cs index e4db8e6..b10e81a 100644 --- a/Program.cs +++ b/Program.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using hass_desktop_service.StateDetectors.Windows.Fullscreen; namespace hass_desktop_service { @@ -11,13 +13,23 @@ namespace hass_desktop_service { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + CreateHostBuilder(args).Build().Run(); + } + else + { + // we only support MS Windows for now + throw new NotImplementedException("Your platform is not yet supported"); + } } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { + services.AddSingleton(); services.AddHostedService(); }); } diff --git a/StateDetectors/FullscreenDetector/UserNotificationStateDetector.cs b/StateDetectors/FullscreenDetector/UserNotificationStateDetector.cs new file mode 100644 index 0000000..91a9f0b --- /dev/null +++ b/StateDetectors/FullscreenDetector/UserNotificationStateDetector.cs @@ -0,0 +1,59 @@ +using System.Runtime.InteropServices; +using PInvoke; +using static PInvoke.Shell32; + +namespace hass_desktop_service.StateDetectors.Windows.Fullscreen +{ +public enum UserNotificationState + { + /// + /// A screen saver is displayed, the machine is locked, + /// or a nonactive Fast User Switching session is in progress. + /// + NotPresent = 1, + + /// + /// A full-screen application is running or Presentation Settings are applied. + /// Presentation Settings allow a user to put their machine into a state fit + /// for an uninterrupted presentation, such as a set of PowerPoint slides, with a single click. + /// + Busy = 2, + + /// + /// A full-screen (exclusive mode) Direct3D application is running. + /// + RunningDirect3dFullScreen = 3, + + /// + /// The user has activated Windows presentation settings to block notifications and pop-up messages. + /// + PresentationMode = 4, + + /// + /// None of the other states are found, notifications can be freely sent. + /// + AcceptsNotifications = 5, + + /// + /// Introduced in Windows 7. The current user is in "quiet time", which is the first hour after + /// a new user logs into his or her account for the first time. During this time, most notifications + /// should not be sent or shown. This lets a user become accustomed to a new computer system + /// without those distractions. + /// Quiet time also occurs for each user after an operating system upgrade or clean installation. + /// + QuietTime = 6 + }; + + public class UserNotificationStateDetector + { + [DllImport("shell32.dll")] + static extern int SHQueryUserNotificationState(out UserNotificationState state); + + public UserNotificationState GetState(){ + UserNotificationState state; + SHQueryUserNotificationState(out state); + + return state; + } + } +} \ No newline at end of file diff --git a/Worker.cs b/Worker.cs index 8f4772c..5bc61fb 100644 --- a/Worker.cs +++ b/Worker.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using hass_desktop_service.StateDetectors.Windows.Fullscreen; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -11,10 +12,12 @@ namespace hass_desktop_service public class Worker : BackgroundService { private readonly ILogger _logger; + private readonly UserNotificationStateDetector _userNotificationStateDetector; - public Worker(ILogger logger) + public Worker(ILogger logger, UserNotificationStateDetector userNotificationStateDetector) { _logger = logger; + this._userNotificationStateDetector = userNotificationStateDetector; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -22,6 +25,8 @@ namespace hass_desktop_service while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + _logger.LogInformation($"Notificationstate: {this._userNotificationStateDetector.GetState()}"); + await Task.Delay(1000, stoppingToken); } }