diff --git a/SafeExamBrowser.Client.UnitTests/Operations/ShellOperationTests.cs b/SafeExamBrowser.Client.UnitTests/Operations/ShellOperationTests.cs index 12ff42fc..f080d10f 100644 --- a/SafeExamBrowser.Client.UnitTests/Operations/ShellOperationTests.cs +++ b/SafeExamBrowser.Client.UnitTests/Operations/ShellOperationTests.cs @@ -18,6 +18,7 @@ using SafeExamBrowser.SystemComponents.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Shell; using SafeExamBrowser.WindowsApi.Contracts; @@ -40,12 +41,11 @@ namespace SafeExamBrowser.Client.UnitTests.Operations private Mock logInfo; private Mock logController; private Mock powerSupply; - // TODO - //private Mock> wirelessNetwork; private Mock systemInfo; private Mock taskbar; private Mock text; private Mock uiFactory; + private Mock wirelessAdapter; private ShellOperation sut; @@ -63,14 +63,13 @@ namespace SafeExamBrowser.Client.UnitTests.Operations logInfo = new Mock(); logController = new Mock(); powerSupply = new Mock(); - // TODO - //wirelessNetwork = new Mock>(); systemInfo = new Mock(); taskbar = new Mock(); taskbarSettings = new TaskbarSettings(); terminationActivator = new Mock(); text = new Mock(); uiFactory = new Mock(); + wirelessAdapter = new Mock(); uiFactory.Setup(u => u.CreateNotificationControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); @@ -86,14 +85,13 @@ namespace SafeExamBrowser.Client.UnitTests.Operations logInfo.Object, logController.Object, powerSupply.Object, - // TODO - //wirelessNetwork.Object, systemInfo.Object, taskbar.Object, taskbarSettings, terminationActivator.Object, text.Object, - uiFactory.Object); + uiFactory.Object, + wirelessAdapter.Object); } [TestMethod] @@ -201,14 +199,13 @@ namespace SafeExamBrowser.Client.UnitTests.Operations uiFactory.Setup(f => f.CreateAudioControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); - uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny())).Returns(new Mock().Object); + uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); sut.Perform(); audio.Verify(a => a.Initialize(), Times.Once); powerSupply.Verify(p => p.Initialize(), Times.Once); - // TODO - //wirelessNetwork.Verify(w => w.Initialize(), Times.Once); + wirelessAdapter.Verify(w => w.Initialize(), Times.Once); keyboard.Verify(k => k.Initialize(), Times.Once); actionCenter.Verify(a => a.AddSystemControl(It.IsAny()), Times.Exactly(4)); taskbar.Verify(t => t.AddSystemControl(It.IsAny()), Times.Exactly(4)); @@ -230,14 +227,13 @@ namespace SafeExamBrowser.Client.UnitTests.Operations uiFactory.Setup(f => f.CreateAudioControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); - uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny())).Returns(new Mock().Object); + uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); sut.Perform(); audio.Verify(a => a.Initialize(), Times.Once); powerSupply.Verify(p => p.Initialize(), Times.Once); - // TODO - //wirelessNetwork.Verify(w => w.Initialize(), Times.Once); + wirelessAdapter.Verify(w => w.Initialize(), Times.Once); keyboard.Verify(k => k.Initialize(), Times.Once); actionCenter.Verify(a => a.AddSystemControl(It.IsAny()), Times.Never); taskbar.Verify(t => t.AddSystemControl(It.IsAny()), Times.Never); @@ -296,8 +292,7 @@ namespace SafeExamBrowser.Client.UnitTests.Operations logController.Verify(c => c.Terminate(), Times.Once); powerSupply.Verify(p => p.Terminate(), Times.Once); keyboard.Verify(k => k.Terminate(), Times.Once); - // TODO - //wirelessNetwork.Verify(w => w.Terminate(), Times.Once); + wirelessAdapter.Verify(w => w.Terminate(), Times.Once); } } } diff --git a/SafeExamBrowser.Client/CompositionRoot.cs b/SafeExamBrowser.Client/CompositionRoot.cs index f8b52f13..c0dd9de2 100644 --- a/SafeExamBrowser.Client/CompositionRoot.cs +++ b/SafeExamBrowser.Client/CompositionRoot.cs @@ -43,6 +43,7 @@ using SafeExamBrowser.SystemComponents.Audio; using SafeExamBrowser.SystemComponents.Contracts; using SafeExamBrowser.SystemComponents.Keyboard; using SafeExamBrowser.SystemComponents.PowerSupply; +using SafeExamBrowser.SystemComponents.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.MessageBox; using SafeExamBrowser.UserInterface.Contracts.Shell; @@ -263,7 +264,7 @@ namespace SafeExamBrowser.Client var logInfo = new LogNotificationInfo(text); var logController = new LogNotificationController(logger, uiFactory); var powerSupply = new PowerSupply(new ModuleLogger(logger, nameof(PowerSupply))); - var wirelessNetwork = new WirelessNetwork(new ModuleLogger(logger, nameof(WirelessNetwork)), text); + var wirelessAdapter = new WirelessAdapter(new ModuleLogger(logger, nameof(WirelessAdapter)), text); var activators = new IActionCenterActivator[] { new KeyboardActivator(new ModuleLogger(logger, nameof(KeyboardActivator))), @@ -281,14 +282,13 @@ namespace SafeExamBrowser.Client logInfo, logController, powerSupply, - // TODO - //wirelessNetwork, systemInfo, taskbar, configuration.Settings.Taskbar, terminationActivator, text, - uiFactory); + uiFactory, + wirelessAdapter); return operation; } diff --git a/SafeExamBrowser.Client/Operations/ShellOperation.cs b/SafeExamBrowser.Client/Operations/ShellOperation.cs index 4ab573e8..f4647eb6 100644 --- a/SafeExamBrowser.Client/Operations/ShellOperation.cs +++ b/SafeExamBrowser.Client/Operations/ShellOperation.cs @@ -17,6 +17,7 @@ using SafeExamBrowser.SystemComponents.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Shell; using SafeExamBrowser.WindowsApi.Contracts; @@ -36,13 +37,13 @@ namespace SafeExamBrowser.Client.Operations private INotificationInfo logInfo; private INotificationController logController; private IPowerSupply powerSupply; - // TODO private ISystemComponent wirelessNetwork; private ISystemInfo systemInfo; private ITaskbar taskbar; private TaskbarSettings taskbarSettings; private ITerminationActivator terminationActivator; private IText text; private IUserInterfaceFactory uiFactory; + private IWirelessAdapter wirelessAdapter; public event ActionRequiredEventHandler ActionRequired { add { } remove { } } public event StatusChangedEventHandler StatusChanged; @@ -59,13 +60,13 @@ namespace SafeExamBrowser.Client.Operations INotificationInfo logInfo, INotificationController logController, IPowerSupply powerSupply, - // TODO ISystemComponent wirelessNetwork, ISystemInfo systemInfo, ITaskbar taskbar, TaskbarSettings taskbarSettings, ITerminationActivator terminationActivator, IText text, - IUserInterfaceFactory uiFactory) + IUserInterfaceFactory uiFactory, + IWirelessAdapter wirelessAdapter) { this.aboutInfo = aboutInfo; this.aboutController = aboutController; @@ -84,7 +85,7 @@ namespace SafeExamBrowser.Client.Operations this.text = text; this.taskbar = taskbar; this.uiFactory = uiFactory; - // TODO this.wirelessNetwork = wirelessNetwork; + this.wirelessAdapter = wirelessAdapter; } public OperationResult Perform() @@ -173,8 +174,7 @@ namespace SafeExamBrowser.Client.Operations audio.Initialize(); keyboard.Initialize(); powerSupply.Initialize(); - // TODO - //wirelessNetwork.Initialize(); + wirelessAdapter.Initialize(); } private void InitializeAboutNotificationForActionCenter() @@ -283,10 +283,7 @@ namespace SafeExamBrowser.Client.Operations { if (actionCenterSettings.ShowWirelessNetwork) { - var control = uiFactory.CreateWirelessNetworkControl(Location.ActionCenter); - - // TODO wirelessNetwork.Register(control); - actionCenter.AddSystemControl(control); + actionCenter.AddSystemControl(uiFactory.CreateWirelessNetworkControl(wirelessAdapter, Location.ActionCenter)); } } @@ -294,10 +291,7 @@ namespace SafeExamBrowser.Client.Operations { if (taskbarSettings.ShowWirelessNetwork) { - var control = uiFactory.CreateWirelessNetworkControl(Location.Taskbar); - - // TODO wirelessNetwork.Register(control); - taskbar.AddSystemControl(control); + taskbar.AddSystemControl(uiFactory.CreateWirelessNetworkControl(wirelessAdapter, Location.Taskbar)); } } @@ -325,8 +319,7 @@ namespace SafeExamBrowser.Client.Operations audio.Terminate(); keyboard.Terminate(); powerSupply.Terminate(); - // TODO - //wirelessNetwork.Terminate(); + wirelessAdapter.Terminate(); } } } diff --git a/SafeExamBrowser.I18n.Contracts/TextKey.cs b/SafeExamBrowser.I18n.Contracts/TextKey.cs index 44bcb09d..58e6600f 100644 --- a/SafeExamBrowser.I18n.Contracts/TextKey.cs +++ b/SafeExamBrowser.I18n.Contracts/TextKey.cs @@ -119,6 +119,7 @@ namespace SafeExamBrowser.I18n.Contracts SystemControl_BatteryRemainingCharge, SystemControl_KeyboardLayoutTooltip, SystemControl_WirelessConnected, + SystemControl_WirelessConnecting, SystemControl_WirelessDisconnected, SystemControl_WirelessNotAvailable, Version diff --git a/SafeExamBrowser.I18n/Text.xml b/SafeExamBrowser.I18n/Text.xml index 9cbc791a..78a959dc 100644 --- a/SafeExamBrowser.I18n/Text.xml +++ b/SafeExamBrowser.I18n/Text.xml @@ -315,6 +315,9 @@ Connected to "%%NAME%%" + + Connecting... + Disconnected diff --git a/SafeExamBrowser.SystemComponents.Contracts/Audio/Events/AudioVolumeChangedEventHandler.cs b/SafeExamBrowser.SystemComponents.Contracts/Audio/Events/VolumeChangedEventHandler.cs similarity index 85% rename from SafeExamBrowser.SystemComponents.Contracts/Audio/Events/AudioVolumeChangedEventHandler.cs rename to SafeExamBrowser.SystemComponents.Contracts/Audio/Events/VolumeChangedEventHandler.cs index ebd4f373..b6c81bac 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/Audio/Events/AudioVolumeChangedEventHandler.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/Audio/Events/VolumeChangedEventHandler.cs @@ -11,5 +11,5 @@ namespace SafeExamBrowser.SystemComponents.Contracts.Audio.Events /// /// Indicates that the volume of the system audio component has changed. /// - public delegate void AudioVolumeChangedEventHandler(double volume, bool muted); + public delegate void VolumeChangedEventHandler(double volume, bool muted); } diff --git a/SafeExamBrowser.SystemComponents.Contracts/Audio/IAudio.cs b/SafeExamBrowser.SystemComponents.Contracts/Audio/IAudio.cs index 360c9de4..06e2afd6 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/Audio/IAudio.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/Audio/IAudio.cs @@ -43,7 +43,7 @@ namespace SafeExamBrowser.SystemComponents.Contracts.Audio /// /// Fired when the volume of the audio device has changed. /// - event AudioVolumeChangedEventHandler VolumeChanged; + event VolumeChangedEventHandler VolumeChanged; /// /// Mutes the currently active audio device. diff --git a/SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/KeyboardLayoutChangedEventHandler.cs b/SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/LayoutChangedEventHandler.cs similarity index 85% rename from SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/KeyboardLayoutChangedEventHandler.cs rename to SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/LayoutChangedEventHandler.cs index 59b93716..a0e3f9db 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/KeyboardLayoutChangedEventHandler.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/Keyboard/Events/LayoutChangedEventHandler.cs @@ -11,5 +11,5 @@ namespace SafeExamBrowser.SystemComponents.Contracts.Keyboard.Events /// /// Indicates that the active keyboard layout has changed. /// - public delegate void KeyboardLayoutChangedEventHandler(IKeyboardLayout layout); + public delegate void LayoutChangedEventHandler(IKeyboardLayout layout); } diff --git a/SafeExamBrowser.SystemComponents.Contracts/Keyboard/IKeyboard.cs b/SafeExamBrowser.SystemComponents.Contracts/Keyboard/IKeyboard.cs index f622777b..2b2d5d32 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/Keyboard/IKeyboard.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/Keyboard/IKeyboard.cs @@ -20,7 +20,7 @@ namespace SafeExamBrowser.SystemComponents.Contracts.Keyboard /// /// Fired when the active keyboard layout changed. /// - event KeyboardLayoutChangedEventHandler LayoutChanged; + event LayoutChangedEventHandler LayoutChanged; /// /// Activates the keyboard layout with the given identifier. diff --git a/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/PowerSupplyStatusChangedEventHandler.cs b/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/StatusChangedEventHandler.cs similarity index 84% rename from SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/PowerSupplyStatusChangedEventHandler.cs rename to SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/StatusChangedEventHandler.cs index fde5570c..cff8eab7 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/PowerSupplyStatusChangedEventHandler.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/Events/StatusChangedEventHandler.cs @@ -11,5 +11,5 @@ namespace SafeExamBrowser.SystemComponents.Contracts.PowerSupply.Events /// /// Indicates that the status of the power supply for the system has changed. /// - public delegate void PowerSupplyStatusChangedEventHandler(IPowerSupplyStatus status); + public delegate void StatusChangedEventHandler(IPowerSupplyStatus status); } diff --git a/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/IPowerSupply.cs b/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/IPowerSupply.cs index 10e4d4f4..e378937c 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/IPowerSupply.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/PowerSupply/IPowerSupply.cs @@ -18,7 +18,7 @@ namespace SafeExamBrowser.SystemComponents.Contracts.PowerSupply /// /// Fired when the status of the power supply changed. /// - event PowerSupplyStatusChangedEventHandler StatusChanged; + event StatusChangedEventHandler StatusChanged; /// /// Retrieves the current status of the power supply. diff --git a/SafeExamBrowser.SystemComponents.Contracts/SafeExamBrowser.SystemComponents.Contracts.csproj b/SafeExamBrowser.SystemComponents.Contracts/SafeExamBrowser.SystemComponents.Contracts.csproj index 5fac656e..6e97f22d 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/SafeExamBrowser.SystemComponents.Contracts.csproj +++ b/SafeExamBrowser.SystemComponents.Contracts/SafeExamBrowser.SystemComponents.Contracts.csproj @@ -53,22 +53,25 @@ - + - + - + - + + + + - + diff --git a/SafeExamBrowser.SystemComponents.Contracts/WirelessNetworkStatus.cs b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/NetworksChangedEventHandler.cs similarity index 59% rename from SafeExamBrowser.SystemComponents.Contracts/WirelessNetworkStatus.cs rename to SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/NetworksChangedEventHandler.cs index 1c0a46ef..d72f5fd4 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/WirelessNetworkStatus.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/NetworksChangedEventHandler.cs @@ -6,15 +6,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -namespace SafeExamBrowser.SystemComponents.Contracts +namespace SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork.Events { /// - /// Defines all possible network statuses which can be determined by the application. + /// Indicates that the available wireless networks have changed. /// - public enum WirelessNetworkStatus - { - Undefined = 0, - Connected, - Disconnected - } + public delegate void NetworksChangedEventHandler(); } diff --git a/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/StatusChangedEventHandler.cs b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/StatusChangedEventHandler.cs new file mode 100644 index 00000000..3f57e41e --- /dev/null +++ b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/Events/StatusChangedEventHandler.cs @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork.Events +{ + /// + /// Indicates that the wireless network status has changed. + /// + public delegate void StatusChangedEventHandler(WirelessNetworkStatus status); +} diff --git a/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessAdapter.cs b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessAdapter.cs new file mode 100644 index 00000000..191bbb0b --- /dev/null +++ b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessAdapter.cs @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Generic; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork.Events; + +namespace SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork +{ + public interface IWirelessAdapter : ISystemComponent + { + /// + /// Fired when the available wireless networks changed. + /// + event NetworksChangedEventHandler NetworksChanged; + + /// + /// Fired when the wireless network status changed. + /// + event StatusChangedEventHandler StatusChanged; + + /// + /// Indicates whether the system has an active wireless network adapter. + /// + bool IsAvailable { get; } + + /// + /// Attempts to connect to the wireless network with the given ID. + /// + void Connect(Guid id); + + /// + /// Retrieves all currently available networks. + /// + IEnumerable GetNetworks(); + } +} diff --git a/SafeExamBrowser.SystemComponents.Contracts/IWirelessNetwork.cs b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessNetwork.cs similarity index 84% rename from SafeExamBrowser.SystemComponents.Contracts/IWirelessNetwork.cs rename to SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessNetwork.cs index 523bc98c..04a6c1c1 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/IWirelessNetwork.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/IWirelessNetwork.cs @@ -8,7 +8,7 @@ using System; -namespace SafeExamBrowser.SystemComponents.Contracts +namespace SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork { /// /// Defines a wireless network which can be connected to by the application. @@ -26,7 +26,7 @@ namespace SafeExamBrowser.SystemComponents.Contracts string Name { get; } /// - /// The signal strength of this network, as percentage. + /// The signal strength of this network, from 0 (worst) to 100 (best). /// int SignalStrength { get; } diff --git a/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/WirelessNetworkStatus.cs b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/WirelessNetworkStatus.cs new file mode 100644 index 00000000..6fe331ab --- /dev/null +++ b/SafeExamBrowser.SystemComponents.Contracts/WirelessNetwork/WirelessNetworkStatus.cs @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork +{ + /// + /// Defines all possible wireless network statuses which can be determined by the application. + /// + public enum WirelessNetworkStatus + { + Undefined = 0, + + /// + /// A connection has been established. + /// + Connected, + + /// + /// A connection is being established. + /// + Connecting, + + /// + /// No connection is established. + /// + Disconnected + } +} diff --git a/SafeExamBrowser.SystemComponents/Audio/Audio.cs b/SafeExamBrowser.SystemComponents/Audio/Audio.cs index 56277d36..42be2a08 100644 --- a/SafeExamBrowser.SystemComponents/Audio/Audio.cs +++ b/SafeExamBrowser.SystemComponents/Audio/Audio.cs @@ -31,7 +31,7 @@ namespace SafeExamBrowser.SystemComponents.Audio public bool OutputMuted => audioDevice?.AudioEndpointVolume.Mute == true; public double OutputVolume => audioDevice?.AudioEndpointVolume.MasterVolumeLevelScalar ?? 0; - public event AudioVolumeChangedEventHandler VolumeChanged; + public event VolumeChangedEventHandler VolumeChanged; public Audio(AudioSettings settings, ILogger logger) { diff --git a/SafeExamBrowser.SystemComponents/Keyboard/Keyboard.cs b/SafeExamBrowser.SystemComponents/Keyboard/Keyboard.cs index 3e75eddf..ad718ded 100644 --- a/SafeExamBrowser.SystemComponents/Keyboard/Keyboard.cs +++ b/SafeExamBrowser.SystemComponents/Keyboard/Keyboard.cs @@ -23,7 +23,7 @@ namespace SafeExamBrowser.SystemComponents.Keyboard private ILogger logger; private CultureInfo originalLanguage; - public event KeyboardLayoutChangedEventHandler LayoutChanged; + public event LayoutChangedEventHandler LayoutChanged; public Keyboard(ILogger logger) { diff --git a/SafeExamBrowser.SystemComponents/PowerSupply/PowerSupply.cs b/SafeExamBrowser.SystemComponents/PowerSupply/PowerSupply.cs index 67e43749..4ee8c641 100644 --- a/SafeExamBrowser.SystemComponents/PowerSupply/PowerSupply.cs +++ b/SafeExamBrowser.SystemComponents/PowerSupply/PowerSupply.cs @@ -22,7 +22,7 @@ namespace SafeExamBrowser.SystemComponents.PowerSupply private ILogger logger; private Timer timer; - public event PowerSupplyStatusChangedEventHandler StatusChanged; + public event StatusChangedEventHandler StatusChanged; public PowerSupply(ILogger logger) { diff --git a/SafeExamBrowser.SystemComponents/SafeExamBrowser.SystemComponents.csproj b/SafeExamBrowser.SystemComponents/SafeExamBrowser.SystemComponents.csproj index 02982534..6b69d6cb 100644 --- a/SafeExamBrowser.SystemComponents/SafeExamBrowser.SystemComponents.csproj +++ b/SafeExamBrowser.SystemComponents/SafeExamBrowser.SystemComponents.csproj @@ -70,8 +70,8 @@ - - + + diff --git a/SafeExamBrowser.SystemComponents/WirelessNetwork.cs b/SafeExamBrowser.SystemComponents/WirelessNetwork.cs deleted file mode 100644 index f0fb2129..00000000 --- a/SafeExamBrowser.SystemComponents/WirelessNetwork.cs +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Timers; -using SafeExamBrowser.I18n.Contracts; -using SafeExamBrowser.Logging.Contracts; -using SafeExamBrowser.SystemComponents.Contracts; -using SimpleWifi; -using SimpleWifi.Win32; -using SimpleWifi.Win32.Interop; - -namespace SafeExamBrowser.SystemComponents -{ - public class WirelessNetwork // TODO: ISystemComponent - { - private readonly object @lock = new object(); - - // TODOprivate List controls; - private List networks; - private bool hasWifiAdapter; - private ILogger logger; - private IText text; - private Timer timer; - private Wifi wifi; - - public WirelessNetwork(ILogger logger, IText text) - { - // TODOthis.controls = new List(); - this.logger = logger; - this.networks = new List(); - this.text = text; - } - - public void Initialize() - { - wifi = new Wifi(); - wifi.ConnectionStatusChanged += Wifi_ConnectionStatusChanged; - hasWifiAdapter = !wifi.NoWifiAvailable && !IsTurnedOff(); - - if (hasWifiAdapter) - { - UpdateAvailableNetworks(); - StartTimer(); - - logger.Info("Started monitoring the wireless network adapter."); - } - else - { - logger.Info("Wireless networks cannot be monitored, as there is no hardware adapter available or it is turned off."); - } - } - - // TODO - //public void Register(ISystemWirelessNetworkControl control) - //{ - // if (hasWifiAdapter) - // { - // control.HasWirelessNetworkAdapter = true; - // control.NetworkSelected += Control_NetworkSelected; - // } - // else - // { - // control.HasWirelessNetworkAdapter = false; - // control.SetInformation(text.Get(TextKey.SystemControl_WirelessNotAvailable)); - // } - - // lock (@lock) - // { - // controls.Add(control); - // } - - // if (hasWifiAdapter) - // { - // UpdateControls(); - // } - //} - - public void Terminate() - { - if (timer != null) - { - timer.Stop(); - logger.Info("Stopped monitoring the wireless network adapter."); - } - - // TODO - //foreach (var control in controls) - //{ - // control.Close(); - //} - } - - private void Control_NetworkSelected(Guid id) - { - lock (@lock) - { - var network = networks.First(n => n.Id == id); - - try - { - var request = new AuthRequest(network.AccessPoint); - - logger.Info($"Attempting to connect to '{network.Name}'..."); - network.AccessPoint.ConnectAsync(request, false, (success) => AccessPoint_OnConnectComplete(network.Name, success)); - - // TODO - //foreach (var control in controls) - //{ - // control.IsConnecting = true; - //} - } - catch (Exception e) - { - logger.Error($"Failed to connect to wireless network '{network.Name}!'", e); - } - } - } - - private void AccessPoint_OnConnectComplete(string name, bool success) - { - if (success) - { - logger.Info($"Successfully connected to wireless network '{name}'."); - } - else - { - logger.Error($"Failed to connect to wireless network '{name}!'"); - } - - lock (@lock) - { - // TODO - //foreach (var control in controls) - //{ - // control.IsConnecting = false; - //} - } - - UpdateAvailableNetworks(); - UpdateControls(); - } - - private void Timer_Elapsed(object sender, ElapsedEventArgs e) - { - UpdateAvailableNetworks(); - UpdateControls(); - } - - private void Wifi_ConnectionStatusChanged(object sender, WifiStatusEventArgs e) - { - UpdateAvailableNetworks(); - UpdateControls(); - } - - private bool IsTurnedOff() - { - try - { - var client = new WlanClient(); - - foreach (var @interface in client.Interfaces) - { - foreach (var state in @interface.RadioState.PhyRadioState) - { - if (state.dot11SoftwareRadioState == Dot11RadioState.On && state.dot11HardwareRadioState == Dot11RadioState.On) - { - return false; - } - } - } - } - catch (Exception e) - { - logger.Error("Failed to determine the radio state of the wireless adapter(s)! Assuming it is (all are) turned off...", e); - } - - return true; - } - - private void UpdateControls() - { - lock (@lock) - { - try - { - var currentNetwork = networks.FirstOrDefault(n => n.Status == WirelessNetworkStatus.Connected); - - // TODO - //foreach (var control in controls) - //{ - // if (wifi.ConnectionStatus == WifiStatus.Disconnected) - // { - // control.SetInformation(text.Get(TextKey.SystemControl_WirelessDisconnected)); - // } - - // if (currentNetwork != null) - // { - // control.SetInformation(text.Get(TextKey.SystemControl_WirelessConnected).Replace("%%NAME%%", currentNetwork.Name)); - // } - - // control.NetworkStatus = ToStatus(wifi.ConnectionStatus); - // control.Update(networks.ToList()); - //} - } - catch (Exception e) - { - logger.Error("Failed to update the wireless network adapter status!", e); - } - } - } - - private void UpdateAvailableNetworks() - { - lock (@lock) - { - networks.Clear(); - - foreach (var accessPoint in wifi.GetAccessPoints()) - { - // The user may only connect to an already configured wireless network! - if (accessPoint.HasProfile) - { - networks.Add(ToDefinition(accessPoint)); - } - } - } - } - - private void StartTimer() - { - const int FIVE_SECONDS = 5000; - - timer = new Timer(FIVE_SECONDS); - timer.Elapsed += Timer_Elapsed; - timer.AutoReset = true; - timer.Start(); - } - - private WirelessNetworkDefinition ToDefinition(AccessPoint accessPoint) - { - return new WirelessNetworkDefinition - { - AccessPoint = accessPoint, - Name = accessPoint.Name, - SignalStrength = Convert.ToInt32(accessPoint.SignalStrength), - Status = accessPoint.IsConnected ? WirelessNetworkStatus.Connected : WirelessNetworkStatus.Disconnected - }; - } - - private WirelessNetworkStatus ToStatus(WifiStatus status) - { - return status == WifiStatus.Connected ? WirelessNetworkStatus.Connected : WirelessNetworkStatus.Disconnected; - } - } -} diff --git a/SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessAdapter.cs b/SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessAdapter.cs new file mode 100644 index 00000000..0a9bae91 --- /dev/null +++ b/SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessAdapter.cs @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Timers; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork.Events; +using SimpleWifi; +using SimpleWifi.Win32; +using SimpleWifi.Win32.Interop; + +namespace SafeExamBrowser.SystemComponents.WirelessNetwork +{ + public class WirelessAdapter : IWirelessAdapter + { + private readonly object @lock = new object(); + + private List networks; + private ILogger logger; + private IText text; + private Timer timer; + private Wifi wifi; + + public bool IsAvailable { get; private set; } + + public event NetworksChangedEventHandler NetworksChanged; + public event StatusChangedEventHandler StatusChanged; + + public WirelessAdapter(ILogger logger, IText text) + { + this.logger = logger; + this.networks = new List(); + this.text = text; + } + + public void Connect(Guid id) + { + lock (@lock) + { + var network = networks.FirstOrDefault(n => n.Id == id); + + if (network != default(WirelessNetwork)) + { + try + { + var request = new AuthRequest(network.AccessPoint); + + logger.Info($"Attempting to connect to '{network.Name}'..."); + network.AccessPoint.ConnectAsync(request, false, (success) => AccessPoint_OnConnectCompleted(network.Name, success)); + StatusChanged?.Invoke(WirelessNetworkStatus.Connecting); + } + catch (Exception e) + { + logger.Error($"Failed to connect to wireless network '{network.Name}!'", e); + } + } + else + { + logger.Warn($"Could not find network with id '{id}'!"); + } + } + } + + public IEnumerable GetNetworks() + { + lock (@lock) + { + return new List(networks); + } + } + + public void Initialize() + { + const int FIVE_SECONDS = 5000; + + wifi = new Wifi(); + wifi.ConnectionStatusChanged += Wifi_ConnectionStatusChanged; + IsAvailable = !wifi.NoWifiAvailable && !IsTurnedOff(); + + if (IsAvailable) + { + UpdateAvailableNetworks(); + + timer = new Timer(FIVE_SECONDS); + timer.Elapsed += Timer_Elapsed; + timer.AutoReset = true; + timer.Start(); + + logger.Info("Started monitoring the wireless network adapter."); + } + else + { + logger.Info("Wireless networks cannot be monitored, as there is no hardware adapter available or it is turned off."); + } + } + + public void Terminate() + { + if (timer != null) + { + timer.Stop(); + logger.Info("Stopped monitoring the wireless network adapter."); + } + } + + private void AccessPoint_OnConnectCompleted(string name, bool success) + { + if (success) + { + logger.Info($"Successfully connected to wireless network '{name}'."); + } + else + { + logger.Error($"Failed to connect to wireless network '{name}!'"); + } + + UpdateAvailableNetworks(); + } + + private void Timer_Elapsed(object sender, ElapsedEventArgs e) + { + UpdateAvailableNetworks(); + } + + private void Wifi_ConnectionStatusChanged(object sender, WifiStatusEventArgs e) + { + UpdateAvailableNetworks(); + } + + private bool IsTurnedOff() + { + try + { + var client = new WlanClient(); + + foreach (var @interface in client.Interfaces) + { + foreach (var state in @interface.RadioState.PhyRadioState) + { + if (state.dot11SoftwareRadioState == Dot11RadioState.On && state.dot11HardwareRadioState == Dot11RadioState.On) + { + return false; + } + } + } + } + catch (Exception e) + { + logger.Error("Failed to determine the radio state of the wireless adapter(s)! Assuming it is (all are) turned off...", e); + } + + return true; + } + + private void UpdateAvailableNetworks() + { + lock (@lock) + { + try + { + networks.Clear(); + + foreach (var accessPoint in wifi.GetAccessPoints()) + { + // The user may only connect to an already configured wireless network! + if (accessPoint.HasProfile) + { + networks.Add(ToNetwork(accessPoint)); + } + } + + NetworksChanged?.Invoke(); + } + catch (Exception e) + { + logger.Error("Failed to update available networks!", e); + } + } + } + + private WirelessNetwork ToNetwork(AccessPoint accessPoint) + { + return new WirelessNetwork + { + AccessPoint = accessPoint, + Name = accessPoint.Name, + SignalStrength = Convert.ToInt32(accessPoint.SignalStrength), + Status = accessPoint.IsConnected ? WirelessNetworkStatus.Connected : WirelessNetworkStatus.Disconnected + }; + } + + private WirelessNetworkStatus ToStatus(WifiStatus status) + { + return status == WifiStatus.Connected ? WirelessNetworkStatus.Connected : WirelessNetworkStatus.Disconnected; + } + } +} diff --git a/SafeExamBrowser.SystemComponents/WirelessNetworkDefinition.cs b/SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessNetwork.cs similarity index 74% rename from SafeExamBrowser.SystemComponents/WirelessNetworkDefinition.cs rename to SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessNetwork.cs index 6a816be2..21ccca95 100644 --- a/SafeExamBrowser.SystemComponents/WirelessNetworkDefinition.cs +++ b/SafeExamBrowser.SystemComponents/WirelessNetwork/WirelessNetwork.cs @@ -7,12 +7,12 @@ */ using System; -using SafeExamBrowser.SystemComponents.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SimpleWifi; -namespace SafeExamBrowser.SystemComponents +namespace SafeExamBrowser.SystemComponents.WirelessNetwork { - internal class WirelessNetworkDefinition : IWirelessNetwork + internal class WirelessNetwork : IWirelessNetwork { internal AccessPoint AccessPoint { get; set; } @@ -21,7 +21,7 @@ namespace SafeExamBrowser.SystemComponents public int SignalStrength { get; set; } public WirelessNetworkStatus Status { get; set; } - public WirelessNetworkDefinition() + public WirelessNetwork() { Id = Guid.NewGuid(); } diff --git a/SafeExamBrowser.UserInterface.Contracts/IUserInterfaceFactory.cs b/SafeExamBrowser.UserInterface.Contracts/IUserInterfaceFactory.cs index 34aec4ea..dcd8507f 100644 --- a/SafeExamBrowser.UserInterface.Contracts/IUserInterfaceFactory.cs +++ b/SafeExamBrowser.UserInterface.Contracts/IUserInterfaceFactory.cs @@ -15,6 +15,7 @@ using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Shell; using SafeExamBrowser.UserInterface.Contracts.Windows; @@ -90,6 +91,6 @@ namespace SafeExamBrowser.UserInterface.Contracts /// /// Creates a system control which allows to change the wireless network connection of the computer. /// - ISystemWirelessNetworkControl CreateWirelessNetworkControl(Location location); + ISystemControl CreateWirelessNetworkControl(IWirelessAdapter wirelessAdapter, Location location); } } diff --git a/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj b/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj index ad17ad18..79719265 100644 --- a/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj +++ b/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj @@ -75,7 +75,6 @@ - diff --git a/SafeExamBrowser.UserInterface.Contracts/Shell/ISystemWirelessNetworkControl.cs b/SafeExamBrowser.UserInterface.Contracts/Shell/ISystemWirelessNetworkControl.cs deleted file mode 100644 index 04a0b80c..00000000 --- a/SafeExamBrowser.UserInterface.Contracts/Shell/ISystemWirelessNetworkControl.cs +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; - -namespace SafeExamBrowser.UserInterface.Contracts.Shell -{ - /// - /// The control of the wireless network system component. - /// - public interface ISystemWirelessNetworkControl : ISystemControl - { - /// - /// Defines whether the computer has a wireless network adapter. - /// - bool HasWirelessNetworkAdapter { set; } - - /// - /// Indicates to the user that a wireless network connection is being established. - /// - bool IsConnecting { set; } - - // TODO - ///// - ///// Sets the current wireless network status. - ///// - //WirelessNetworkStatus NetworkStatus { set; } - - /// - /// Event fired when the user selected a wireless network. - /// - event WirelessNetworkSelectedEventHandler NetworkSelected; - - // TODO - ///// - ///// Updates the list of available networks. - ///// - //void Update(IEnumerable networks); - } -} diff --git a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterKeyboardLayoutButton.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterKeyboardLayoutButton.xaml.cs index 4142c155..adebc1bc 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterKeyboardLayoutButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterKeyboardLayoutButton.xaml.cs @@ -34,10 +34,10 @@ namespace SafeExamBrowser.UserInterface.Desktop.Controls this.layout = layout; InitializeComponent(); - InitializeEvents(); + InitializeLayoutButton(); } - private void InitializeEvents() + private void InitializeLayoutButton() { Button.Click += (o, args) => LayoutSelected?.Invoke(this, EventArgs.Empty); CultureCodeTextBlock.Text = layout.CultureCode; diff --git a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkButton.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkButton.xaml.cs index 781fa057..fd17d84b 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkButton.xaml.cs @@ -6,10 +6,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using System.Windows; using System.Windows.Controls; -using SafeExamBrowser.SystemComponents.Contracts; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; namespace SafeExamBrowser.UserInterface.Desktop.Controls { @@ -17,34 +17,22 @@ namespace SafeExamBrowser.UserInterface.Desktop.Controls { private IWirelessNetwork network; - public bool IsCurrent - { - set { IsCurrentTextBlock.Visibility = value ? Visibility.Visible : Visibility.Hidden; } - } - - public string NetworkName - { - set { NetworkNameTextBlock.Text = value; } - } - - public int SignalStrength - { - set { SignalStrengthTextBlock.Text = $"{value}%"; } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; + public event EventHandler NetworkSelected; public ActionCenterWirelessNetworkButton(IWirelessNetwork network) { this.network = network; InitializeComponent(); - InitializeEvents(); + InitializeNetworkButton(); } - private void InitializeEvents() + private void InitializeNetworkButton() { - Button.Click += (o, args) => NetworkSelected?.Invoke(network.Id); + Button.Click += (o, args) => NetworkSelected?.Invoke(this, EventArgs.Empty); + IsCurrentTextBlock.Visibility = network.Status == WirelessNetworkStatus.Connected ? Visibility.Visible : Visibility.Hidden; + NetworkNameTextBlock.Text = network.Name; + SignalStrengthTextBlock.Text = $"{network.SignalStrength}%"; } } } diff --git a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkControl.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkControl.xaml.cs index 822d84a8..6ec45f5d 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkControl.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Controls/ActionCenterWirelessNetworkControl.xaml.cs @@ -7,112 +7,35 @@ */ using System; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using FontAwesome.WPF; -using SafeExamBrowser.SystemComponents.Contracts; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts.Shell; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; using SafeExamBrowser.UserInterface.Shared.Utilities; namespace SafeExamBrowser.UserInterface.Desktop.Controls { - public partial class ActionCenterWirelessNetworkControl : UserControl, ISystemWirelessNetworkControl + public partial class ActionCenterWirelessNetworkControl : UserControl, ISystemControl { - public bool HasWirelessNetworkAdapter + private IWirelessAdapter wirelessAdapter; + private IText text; + + public ActionCenterWirelessNetworkControl(IWirelessAdapter wirelessAdapter, IText text) { - set - { - Dispatcher.Invoke(() => - { - Button.IsEnabled = value; - NoAdapterIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } + this.wirelessAdapter = wirelessAdapter; + this.text = text; - public bool IsConnecting - { - set - { - Dispatcher.Invoke(() => - { - LoadingIcon.Visibility = value ? Visibility.Visible : Visibility.Collapsed; - SignalStrengthIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - NetworkStatusIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } - - public WirelessNetworkStatus NetworkStatus - { - set - { - Dispatcher.Invoke(() => - { - var icon = value == WirelessNetworkStatus.Connected ? FontAwesomeIcon.Check : FontAwesomeIcon.Close; - var brush = value == WirelessNetworkStatus.Connected ? Brushes.Green : Brushes.Orange; - - if (value == WirelessNetworkStatus.Disconnected) - { - SignalStrengthIcon.Child = GetIcon(0); - } - - NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(icon, brush); - }); - } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; - - public ActionCenterWirelessNetworkControl() - { InitializeComponent(); InitializeWirelessNetworkControl(); } public void Close() { - Dispatcher.Invoke(() => Popup.IsOpen = false); - } - - public void SetInformation(string text) - { - Dispatcher.Invoke(() => - { - Button.ToolTip = text; - Text.Text = text; - }); - } - - public void Update(IEnumerable networks) - { - Dispatcher.Invoke(() => - { - NetworksStackPanel.Children.Clear(); - - foreach (var network in networks) - { - var button = new ActionCenterWirelessNetworkButton(network); - var isCurrent = network.Status == WirelessNetworkStatus.Connected; - - button.IsCurrent = isCurrent; - button.NetworkName = network.Name; - button.SignalStrength = network.SignalStrength; - button.NetworkSelected += (id) => NetworkSelected?.Invoke(id); - - if (isCurrent) - { - NetworkStatus = network.Status; - SignalStrengthIcon.Child = GetIcon(network.SignalStrength); - } - - NetworksStackPanel.Children.Add(button); - } - }); + Dispatcher.InvokeAsync(() => Popup.IsOpen = false); } private void InitializeWirelessNetworkControl() @@ -125,6 +48,85 @@ namespace SafeExamBrowser.UserInterface.Desktop.Controls Popup.MouseLeave += (o, args) => Task.Delay(250).ContinueWith(_ => Dispatcher.Invoke(() => Popup.IsOpen = IsMouseOver)); Popup.Opened += (o, args) => Grid.Background = Brushes.Gray; Popup.Closed += (o, args) => Grid.Background = originalBrush; + + if (wirelessAdapter.IsAvailable) + { + wirelessAdapter.NetworksChanged += WirelessAdapter_NetworksChanged; + wirelessAdapter.StatusChanged += WirelessAdapter_StatusChanged; + UpdateNetworks(); + } + else + { + Button.IsEnabled = false; + NoAdapterIcon.Visibility = Visibility.Visible; + UpdateText(text.Get(TextKey.SystemControl_WirelessNotAvailable)); + } + } + + private void WirelessAdapter_NetworksChanged() + { + Dispatcher.InvokeAsync(UpdateNetworks); + } + + private void WirelessAdapter_StatusChanged(WirelessNetworkStatus status) + { + Dispatcher.InvokeAsync(() => UpdateStatus(status)); + } + + private void UpdateNetworks() + { + var status = WirelessNetworkStatus.Disconnected; + + NetworksStackPanel.Children.Clear(); + + foreach (var network in wirelessAdapter.GetNetworks()) + { + var button = new ActionCenterWirelessNetworkButton(network); + + button.NetworkSelected += (o, args) => wirelessAdapter.Connect(network.Id); + + if (network.Status == WirelessNetworkStatus.Connected) + { + status = WirelessNetworkStatus.Connected; + SignalStrengthIcon.Child = GetIcon(network.SignalStrength); + UpdateText(text.Get(TextKey.SystemControl_WirelessConnected).Replace("%%NAME%%", network.Name)); + } + + NetworksStackPanel.Children.Add(button); + } + + UpdateStatus(status); + } + + private void UpdateStatus(WirelessNetworkStatus status) + { + LoadingIcon.Visibility = Visibility.Collapsed; + SignalStrengthIcon.Visibility = Visibility.Visible; + NetworkStatusIcon.Visibility = Visibility.Visible; + + switch (status) + { + case WirelessNetworkStatus.Connected: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Check, Brushes.Green); + break; + case WirelessNetworkStatus.Connecting: + LoadingIcon.Visibility = Visibility.Visible; + SignalStrengthIcon.Visibility = Visibility.Collapsed; + NetworkStatusIcon.Visibility = Visibility.Collapsed; + UpdateText(text.Get(TextKey.SystemControl_WirelessConnecting)); + break; + default: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Close, Brushes.Orange); + SignalStrengthIcon.Child = GetIcon(0); + UpdateText(text.Get(TextKey.SystemControl_WirelessDisconnected)); + break; + } + } + + private void UpdateText(string text) + { + Button.ToolTip = text; + Text.Text = text; } private UIElement GetIcon(int signalStrength) diff --git a/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkButton.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkButton.xaml.cs index deda8f82..700d6f54 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkButton.xaml.cs @@ -6,45 +6,33 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using System.Windows; using System.Windows.Controls; -using SafeExamBrowser.SystemComponents.Contracts; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; namespace SafeExamBrowser.UserInterface.Desktop.Controls { public partial class TaskbarWirelessNetworkButton : UserControl { - private readonly IWirelessNetwork network; + private IWirelessNetwork network; - public bool IsCurrent - { - set { IsCurrentTextBlock.Visibility = value ? Visibility.Visible : Visibility.Hidden; } - } - - public string NetworkName - { - set { NetworkNameTextBlock.Text = value; } - } - - public int SignalStrength - { - set { SignalStrengthTextBlock.Text = $"{value}%"; } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; + public event EventHandler NetworkSelected; public TaskbarWirelessNetworkButton(IWirelessNetwork network) { this.network = network; InitializeComponent(); - InitializeEvents(); + InitializeNetworkButton(); } - private void InitializeEvents() + private void InitializeNetworkButton() { - Button.Click += (o, args) => NetworkSelected?.Invoke(network.Id); + Button.Click += (o, args) => NetworkSelected?.Invoke(this, EventArgs.Empty); + IsCurrentTextBlock.Visibility = network.Status == WirelessNetworkStatus.Connected ? Visibility.Visible : Visibility.Hidden; + NetworkNameTextBlock.Text = network.Name; + SignalStrengthTextBlock.Text = $"{network.SignalStrength}%"; } } } diff --git a/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkControl.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkControl.xaml.cs index ed6b5167..bec84b6d 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkControl.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Controls/TaskbarWirelessNetworkControl.xaml.cs @@ -7,108 +7,35 @@ */ using System; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using FontAwesome.WPF; -using SafeExamBrowser.SystemComponents.Contracts; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts.Shell; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; using SafeExamBrowser.UserInterface.Shared.Utilities; namespace SafeExamBrowser.UserInterface.Desktop.Controls { - public partial class TaskbarWirelessNetworkControl : UserControl, ISystemWirelessNetworkControl + public partial class TaskbarWirelessNetworkControl : UserControl, ISystemControl { - public bool HasWirelessNetworkAdapter + private IWirelessAdapter wirelessAdapter; + private IText text; + + public TaskbarWirelessNetworkControl(IWirelessAdapter wirelessAdapter, IText text) { - set - { - Dispatcher.InvokeAsync(() => - { - Button.IsEnabled = value; - NoAdapterIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } + this.wirelessAdapter = wirelessAdapter; + this.text = text; - public bool IsConnecting - { - set - { - Dispatcher.Invoke(() => - { - LoadingIcon.Visibility = value ? Visibility.Visible : Visibility.Collapsed; - SignalStrengthIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - NetworkStatusIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } - - public WirelessNetworkStatus NetworkStatus - { - set - { - Dispatcher.InvokeAsync(() => - { - var icon = value == WirelessNetworkStatus.Connected ? FontAwesomeIcon.Check : FontAwesomeIcon.Close; - var brush = value == WirelessNetworkStatus.Connected ? Brushes.Green : Brushes.Orange; - - if (value == WirelessNetworkStatus.Disconnected) - { - SignalStrengthIcon.Child = GetIcon(0); - } - - NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(icon, brush); - }); - } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; - - public TaskbarWirelessNetworkControl() - { InitializeComponent(); InitializeWirelessNetworkControl(); } public void Close() { - Popup.IsOpen = false; - } - - public void SetInformation(string text) - { - Dispatcher.InvokeAsync(() => Button.ToolTip = text); - } - - public void Update(IEnumerable networks) - { - Dispatcher.InvokeAsync(() => - { - NetworksStackPanel.Children.Clear(); - - foreach (var network in networks) - { - var button = new TaskbarWirelessNetworkButton(network); - var isCurrent = network.Status == WirelessNetworkStatus.Connected; - - button.IsCurrent = isCurrent; - button.NetworkName = network.Name; - button.SignalStrength = network.SignalStrength; - button.NetworkSelected += (id) => NetworkSelected?.Invoke(id); - - if (isCurrent) - { - NetworkStatus = network.Status; - SignalStrengthIcon.Child = GetIcon(network.SignalStrength); - } - - NetworksStackPanel.Children.Add(button); - } - }); + Dispatcher.InvokeAsync(() => Popup.IsOpen = false); } private void InitializeWirelessNetworkControl() @@ -131,6 +58,84 @@ namespace SafeExamBrowser.UserInterface.Desktop.Controls Background = originalBrush; Button.Background = originalBrush; }; + + if (wirelessAdapter.IsAvailable) + { + wirelessAdapter.NetworksChanged += WirelessAdapter_NetworksChanged; + wirelessAdapter.StatusChanged += WirelessAdapter_StatusChanged; + UpdateNetworks(); + } + else + { + Button.IsEnabled = false; + NoAdapterIcon.Visibility = Visibility.Visible; + UpdateText(text.Get(TextKey.SystemControl_WirelessNotAvailable)); + } + } + + private void WirelessAdapter_NetworksChanged() + { + Dispatcher.InvokeAsync(UpdateNetworks); + } + + private void WirelessAdapter_StatusChanged(WirelessNetworkStatus status) + { + Dispatcher.InvokeAsync(() => UpdateStatus(status)); + } + + private void UpdateNetworks() + { + var status = WirelessNetworkStatus.Disconnected; + + NetworksStackPanel.Children.Clear(); + + foreach (var network in wirelessAdapter.GetNetworks()) + { + var button = new TaskbarWirelessNetworkButton(network); + + button.NetworkSelected += (o, args) => wirelessAdapter.Connect(network.Id); + + if (network.Status == WirelessNetworkStatus.Connected) + { + status = WirelessNetworkStatus.Connected; + SignalStrengthIcon.Child = GetIcon(network.SignalStrength); + UpdateText(text.Get(TextKey.SystemControl_WirelessConnected).Replace("%%NAME%%", network.Name)); + } + + NetworksStackPanel.Children.Add(button); + } + + UpdateStatus(status); + } + + private void UpdateStatus(WirelessNetworkStatus status) + { + LoadingIcon.Visibility = Visibility.Collapsed; + SignalStrengthIcon.Visibility = Visibility.Visible; + NetworkStatusIcon.Visibility = Visibility.Visible; + + switch (status) + { + case WirelessNetworkStatus.Connected: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Check, Brushes.Green); + break; + case WirelessNetworkStatus.Connecting: + LoadingIcon.Visibility = Visibility.Visible; + SignalStrengthIcon.Visibility = Visibility.Collapsed; + NetworkStatusIcon.Visibility = Visibility.Collapsed; + UpdateText(text.Get(TextKey.SystemControl_WirelessConnecting)); + break; + default: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Close, Brushes.Orange); + SignalStrengthIcon.Child = GetIcon(0); + UpdateText(text.Get(TextKey.SystemControl_WirelessDisconnected)); + break; + } + } + + private void UpdateText(string text) + { + Button.ToolTip = text; } private UIElement GetIcon(int signalStrength) diff --git a/SafeExamBrowser.UserInterface.Desktop/UserInterfaceFactory.cs b/SafeExamBrowser.UserInterface.Desktop/UserInterfaceFactory.cs index 72e320a8..71253953 100644 --- a/SafeExamBrowser.UserInterface.Desktop/UserInterfaceFactory.cs +++ b/SafeExamBrowser.UserInterface.Desktop/UserInterfaceFactory.cs @@ -19,6 +19,7 @@ using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Shell; @@ -172,15 +173,15 @@ namespace SafeExamBrowser.UserInterface.Desktop return splashScreen; } - public ISystemWirelessNetworkControl CreateWirelessNetworkControl(Location location) + public ISystemControl CreateWirelessNetworkControl(IWirelessAdapter wirelessAdapter, Location location) { if (location == Location.ActionCenter) { - return new ActionCenterWirelessNetworkControl(); + return new ActionCenterWirelessNetworkControl(wirelessAdapter, text); } else { - return new TaskbarWirelessNetworkControl(); + return new TaskbarWirelessNetworkControl(wirelessAdapter, text); } } diff --git a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterKeyboardLayoutButton.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterKeyboardLayoutButton.xaml.cs index d59f6d0c..a8ca1e3c 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterKeyboardLayoutButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterKeyboardLayoutButton.xaml.cs @@ -34,10 +34,10 @@ namespace SafeExamBrowser.UserInterface.Mobile.Controls this.layout = layout; InitializeComponent(); - InitializeEvents(); + InitializeLayoutButton(); } - private void InitializeEvents() + private void InitializeLayoutButton() { Button.Click += (o, args) => LayoutSelected?.Invoke(this, EventArgs.Empty); CultureCodeTextBlock.Text = layout.CultureCode; diff --git a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkButton.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkButton.xaml.cs index b26dfa75..f77a967d 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkButton.xaml.cs @@ -6,10 +6,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using System.Windows; using System.Windows.Controls; -using SafeExamBrowser.SystemComponents.Contracts; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; namespace SafeExamBrowser.UserInterface.Mobile.Controls { @@ -17,34 +17,22 @@ namespace SafeExamBrowser.UserInterface.Mobile.Controls { private IWirelessNetwork network; - public bool IsCurrent - { - set { IsCurrentTextBlock.Visibility = value ? Visibility.Visible : Visibility.Hidden; } - } - - public string NetworkName - { - set { NetworkNameTextBlock.Text = value; } - } - - public int SignalStrength - { - set { SignalStrengthTextBlock.Text = $"{value}%"; } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; + public event EventHandler NetworkSelected; public ActionCenterWirelessNetworkButton(IWirelessNetwork network) { this.network = network; InitializeComponent(); - InitializeEvents(); + InitializeNetworkButton(); } - private void InitializeEvents() + private void InitializeNetworkButton() { - Button.Click += (o, args) => NetworkSelected?.Invoke(network.Id); + Button.Click += (o, args) => NetworkSelected?.Invoke(this, EventArgs.Empty); + IsCurrentTextBlock.Visibility = network.Status == WirelessNetworkStatus.Connected ? Visibility.Visible : Visibility.Hidden; + NetworkNameTextBlock.Text = network.Name; + SignalStrengthTextBlock.Text = $"{network.SignalStrength}%"; } } } diff --git a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkControl.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkControl.xaml.cs index 1949b336..3248fef0 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkControl.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Controls/ActionCenterWirelessNetworkControl.xaml.cs @@ -7,112 +7,35 @@ */ using System; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using FontAwesome.WPF; -using SafeExamBrowser.SystemComponents.Contracts; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts.Shell; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; using SafeExamBrowser.UserInterface.Shared.Utilities; namespace SafeExamBrowser.UserInterface.Mobile.Controls { - public partial class ActionCenterWirelessNetworkControl : UserControl, ISystemWirelessNetworkControl + public partial class ActionCenterWirelessNetworkControl : UserControl, ISystemControl { - public bool HasWirelessNetworkAdapter + private IWirelessAdapter wirelessAdapter; + private IText text; + + public ActionCenterWirelessNetworkControl(IWirelessAdapter wirelessAdapter, IText text) { - set - { - Dispatcher.Invoke(() => - { - Button.IsEnabled = value; - NoAdapterIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } + this.wirelessAdapter = wirelessAdapter; + this.text = text; - public bool IsConnecting - { - set - { - Dispatcher.Invoke(() => - { - LoadingIcon.Visibility = value ? Visibility.Visible : Visibility.Collapsed; - SignalStrengthIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - NetworkStatusIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } - - public WirelessNetworkStatus NetworkStatus - { - set - { - Dispatcher.Invoke(() => - { - var icon = value == WirelessNetworkStatus.Connected ? FontAwesomeIcon.Check : FontAwesomeIcon.Close; - var brush = value == WirelessNetworkStatus.Connected ? Brushes.Green : Brushes.Orange; - - if (value == WirelessNetworkStatus.Disconnected) - { - SignalStrengthIcon.Child = GetIcon(0); - } - - NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(icon, brush); - }); - } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; - - public ActionCenterWirelessNetworkControl() - { InitializeComponent(); InitializeWirelessNetworkControl(); } public void Close() { - Dispatcher.Invoke(() => Popup.IsOpen = false); - } - - public void SetInformation(string text) - { - Dispatcher.Invoke(() => - { - Button.ToolTip = text; - Text.Text = text; - }); - } - - public void Update(IEnumerable networks) - { - Dispatcher.Invoke(() => - { - NetworksStackPanel.Children.Clear(); - - foreach (var network in networks) - { - var button = new ActionCenterWirelessNetworkButton(network); - var isCurrent = network.Status == WirelessNetworkStatus.Connected; - - button.IsCurrent = isCurrent; - button.NetworkName = network.Name; - button.SignalStrength = network.SignalStrength; - button.NetworkSelected += (id) => NetworkSelected?.Invoke(id); - - if (isCurrent) - { - NetworkStatus = network.Status; - SignalStrengthIcon.Child = GetIcon(network.SignalStrength); - } - - NetworksStackPanel.Children.Add(button); - } - }); + Dispatcher.InvokeAsync(() => Popup.IsOpen = false); } private void InitializeWirelessNetworkControl() @@ -125,12 +48,91 @@ namespace SafeExamBrowser.UserInterface.Mobile.Controls Popup.MouseLeave += (o, args) => Task.Delay(250).ContinueWith(_ => Dispatcher.Invoke(() => Popup.IsOpen = IsMouseOver)); Popup.Opened += (o, args) => Grid.Background = Brushes.Gray; Popup.Closed += (o, args) => Grid.Background = originalBrush; + + if (wirelessAdapter.IsAvailable) + { + wirelessAdapter.NetworksChanged += WirelessAdapter_NetworksChanged; + wirelessAdapter.StatusChanged += WirelessAdapter_StatusChanged; + UpdateNetworks(); + } + else + { + Button.IsEnabled = false; + NoAdapterIcon.Visibility = Visibility.Visible; + UpdateText(text.Get(TextKey.SystemControl_WirelessNotAvailable)); + } + } + + private void WirelessAdapter_NetworksChanged() + { + Dispatcher.InvokeAsync(UpdateNetworks); + } + + private void WirelessAdapter_StatusChanged(WirelessNetworkStatus status) + { + Dispatcher.InvokeAsync(() => UpdateStatus(status)); + } + + private void UpdateNetworks() + { + var status = WirelessNetworkStatus.Disconnected; + + NetworksStackPanel.Children.Clear(); + + foreach (var network in wirelessAdapter.GetNetworks()) + { + var button = new ActionCenterWirelessNetworkButton(network); + + button.NetworkSelected += (o, args) => wirelessAdapter.Connect(network.Id); + + if (network.Status == WirelessNetworkStatus.Connected) + { + status = WirelessNetworkStatus.Connected; + SignalStrengthIcon.Child = GetIcon(network.SignalStrength); + UpdateText(text.Get(TextKey.SystemControl_WirelessConnected).Replace("%%NAME%%", network.Name)); + } + + NetworksStackPanel.Children.Add(button); + } + + UpdateStatus(status); + } + + private void UpdateStatus(WirelessNetworkStatus status) + { + LoadingIcon.Visibility = Visibility.Collapsed; + SignalStrengthIcon.Visibility = Visibility.Visible; + NetworkStatusIcon.Visibility = Visibility.Visible; + + switch (status) + { + case WirelessNetworkStatus.Connected: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Check, Brushes.Green); + break; + case WirelessNetworkStatus.Connecting: + LoadingIcon.Visibility = Visibility.Visible; + SignalStrengthIcon.Visibility = Visibility.Collapsed; + NetworkStatusIcon.Visibility = Visibility.Collapsed; + UpdateText(text.Get(TextKey.SystemControl_WirelessConnecting)); + break; + default: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Close, Brushes.Orange); + SignalStrengthIcon.Child = GetIcon(0); + UpdateText(text.Get(TextKey.SystemControl_WirelessDisconnected)); + break; + } + } + + private void UpdateText(string text) + { + Button.ToolTip = text; + Text.Text = text; } private UIElement GetIcon(int signalStrength) { var icon = signalStrength > 66 ? "100" : (signalStrength > 33 ? "66" : (signalStrength > 0 ? "33" : "0")); - var uri = new Uri($"pack://application:,,,/SafeExamBrowser.UserInterface.Desktop;component/Images/WiFi_Light_{icon}.xaml"); + var uri = new Uri($"pack://application:,,,/SafeExamBrowser.UserInterface.Mobile;component/Images/WiFi_Light_{icon}.xaml"); var resource = new XamlIconResource(uri); return IconResourceLoader.Load(resource); diff --git a/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkButton.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkButton.xaml.cs index a41586d7..d6135f1c 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkButton.xaml.cs @@ -6,45 +6,33 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using System.Windows; using System.Windows.Controls; -using SafeExamBrowser.SystemComponents.Contracts; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; namespace SafeExamBrowser.UserInterface.Mobile.Controls { public partial class TaskbarWirelessNetworkButton : UserControl { - private readonly IWirelessNetwork network; + private IWirelessNetwork network; - public bool IsCurrent - { - set { IsCurrentTextBlock.Visibility = value ? Visibility.Visible : Visibility.Hidden; } - } - - public string NetworkName - { - set { NetworkNameTextBlock.Text = value; } - } - - public int SignalStrength - { - set { SignalStrengthTextBlock.Text = $"{value}%"; } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; + public event EventHandler NetworkSelected; public TaskbarWirelessNetworkButton(IWirelessNetwork network) { this.network = network; InitializeComponent(); - InitializeEvents(); + InitializeNetworkButton(); } - private void InitializeEvents() + private void InitializeNetworkButton() { - Button.Click += (o, args) => NetworkSelected?.Invoke(network.Id); + Button.Click += (o, args) => NetworkSelected?.Invoke(this, EventArgs.Empty); + IsCurrentTextBlock.Visibility = network.Status == WirelessNetworkStatus.Connected ? Visibility.Visible : Visibility.Hidden; + NetworkNameTextBlock.Text = network.Name; + SignalStrengthTextBlock.Text = $"{network.SignalStrength}%"; } } } diff --git a/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkControl.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkControl.xaml.cs index 55e0e029..80d05004 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkControl.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Controls/TaskbarWirelessNetworkControl.xaml.cs @@ -7,108 +7,35 @@ */ using System; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using FontAwesome.WPF; -using SafeExamBrowser.SystemComponents.Contracts; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts.Shell; -using SafeExamBrowser.UserInterface.Contracts.Shell.Events; using SafeExamBrowser.UserInterface.Shared.Utilities; namespace SafeExamBrowser.UserInterface.Mobile.Controls { - public partial class TaskbarWirelessNetworkControl : UserControl, ISystemWirelessNetworkControl + public partial class TaskbarWirelessNetworkControl : UserControl, ISystemControl { - public bool HasWirelessNetworkAdapter + private IWirelessAdapter wirelessAdapter; + private IText text; + + public TaskbarWirelessNetworkControl(IWirelessAdapter wirelessAdapter, IText text) { - set - { - Dispatcher.InvokeAsync(() => - { - Button.IsEnabled = value; - NoAdapterIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } + this.wirelessAdapter = wirelessAdapter; + this.text = text; - public bool IsConnecting - { - set - { - Dispatcher.Invoke(() => - { - LoadingIcon.Visibility = value ? Visibility.Visible : Visibility.Collapsed; - SignalStrengthIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - NetworkStatusIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible; - }); - } - } - - public WirelessNetworkStatus NetworkStatus - { - set - { - Dispatcher.InvokeAsync(() => - { - var icon = value == WirelessNetworkStatus.Connected ? FontAwesomeIcon.Check : FontAwesomeIcon.Close; - var brush = value == WirelessNetworkStatus.Connected ? Brushes.Green : Brushes.Orange; - - if (value == WirelessNetworkStatus.Disconnected) - { - SignalStrengthIcon.Child = GetIcon(0); - } - - NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(icon, brush); - }); - } - } - - public event WirelessNetworkSelectedEventHandler NetworkSelected; - - public TaskbarWirelessNetworkControl() - { InitializeComponent(); InitializeWirelessNetworkControl(); } public void Close() { - Popup.IsOpen = false; - } - - public void SetInformation(string text) - { - Dispatcher.InvokeAsync(() => Button.ToolTip = text); - } - - public void Update(IEnumerable networks) - { - Dispatcher.InvokeAsync(() => - { - NetworksStackPanel.Children.Clear(); - - foreach (var network in networks) - { - var button = new TaskbarWirelessNetworkButton(network); - var isCurrent = network.Status == WirelessNetworkStatus.Connected; - - button.IsCurrent = isCurrent; - button.NetworkName = network.Name; - button.SignalStrength = network.SignalStrength; - button.NetworkSelected += (id) => NetworkSelected?.Invoke(id); - - if (isCurrent) - { - NetworkStatus = network.Status; - SignalStrengthIcon.Child = GetIcon(network.SignalStrength); - } - - NetworksStackPanel.Children.Add(button); - } - }); + Dispatcher.InvokeAsync(() => Popup.IsOpen = false); } private void InitializeWirelessNetworkControl() @@ -131,12 +58,90 @@ namespace SafeExamBrowser.UserInterface.Mobile.Controls Background = originalBrush; Button.Background = originalBrush; }; + + if (wirelessAdapter.IsAvailable) + { + wirelessAdapter.NetworksChanged += WirelessAdapter_NetworksChanged; + wirelessAdapter.StatusChanged += WirelessAdapter_StatusChanged; + UpdateNetworks(); + } + else + { + Button.IsEnabled = false; + NoAdapterIcon.Visibility = Visibility.Visible; + UpdateText(text.Get(TextKey.SystemControl_WirelessNotAvailable)); + } + } + + private void WirelessAdapter_NetworksChanged() + { + Dispatcher.InvokeAsync(UpdateNetworks); + } + + private void WirelessAdapter_StatusChanged(WirelessNetworkStatus status) + { + Dispatcher.InvokeAsync(() => UpdateStatus(status)); + } + + private void UpdateNetworks() + { + var status = WirelessNetworkStatus.Disconnected; + + NetworksStackPanel.Children.Clear(); + + foreach (var network in wirelessAdapter.GetNetworks()) + { + var button = new TaskbarWirelessNetworkButton(network); + + button.NetworkSelected += (o, args) => wirelessAdapter.Connect(network.Id); + + if (network.Status == WirelessNetworkStatus.Connected) + { + status = WirelessNetworkStatus.Connected; + SignalStrengthIcon.Child = GetIcon(network.SignalStrength); + UpdateText(text.Get(TextKey.SystemControl_WirelessConnected).Replace("%%NAME%%", network.Name)); + } + + NetworksStackPanel.Children.Add(button); + } + + UpdateStatus(status); + } + + private void UpdateStatus(WirelessNetworkStatus status) + { + LoadingIcon.Visibility = Visibility.Collapsed; + SignalStrengthIcon.Visibility = Visibility.Visible; + NetworkStatusIcon.Visibility = Visibility.Visible; + + switch (status) + { + case WirelessNetworkStatus.Connected: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Check, Brushes.Green); + break; + case WirelessNetworkStatus.Connecting: + LoadingIcon.Visibility = Visibility.Visible; + SignalStrengthIcon.Visibility = Visibility.Collapsed; + NetworkStatusIcon.Visibility = Visibility.Collapsed; + UpdateText(text.Get(TextKey.SystemControl_WirelessConnecting)); + break; + default: + NetworkStatusIcon.Source = ImageAwesome.CreateImageSource(FontAwesomeIcon.Close, Brushes.Orange); + SignalStrengthIcon.Child = GetIcon(0); + UpdateText(text.Get(TextKey.SystemControl_WirelessDisconnected)); + break; + } + } + + private void UpdateText(string text) + { + Button.ToolTip = text; } private UIElement GetIcon(int signalStrength) { var icon = signalStrength > 66 ? "100" : (signalStrength > 33 ? "66" : (signalStrength > 0 ? "33" : "0")); - var uri = new Uri($"pack://application:,,,/SafeExamBrowser.UserInterface.Desktop;component/Images/WiFi_{icon}.xaml"); + var uri = new Uri($"pack://application:,,,/SafeExamBrowser.UserInterface.Mobile;component/Images/WiFi_{icon}.xaml"); var resource = new XamlIconResource(uri); return IconResourceLoader.Load(resource); diff --git a/SafeExamBrowser.UserInterface.Mobile/SafeExamBrowser.UserInterface.Mobile.csproj b/SafeExamBrowser.UserInterface.Mobile/SafeExamBrowser.UserInterface.Mobile.csproj index 99c95f0f..3e240281 100644 --- a/SafeExamBrowser.UserInterface.Mobile/SafeExamBrowser.UserInterface.Mobile.csproj +++ b/SafeExamBrowser.UserInterface.Mobile/SafeExamBrowser.UserInterface.Mobile.csproj @@ -353,38 +353,38 @@ MSBuild:Compile Designer - + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - - + + MSBuild:Compile Designer - + MSBuild:Compile Designer diff --git a/SafeExamBrowser.UserInterface.Mobile/UserInterfaceFactory.cs b/SafeExamBrowser.UserInterface.Mobile/UserInterfaceFactory.cs index 89b9aceb..c869b8c8 100644 --- a/SafeExamBrowser.UserInterface.Mobile/UserInterfaceFactory.cs +++ b/SafeExamBrowser.UserInterface.Mobile/UserInterfaceFactory.cs @@ -19,6 +19,7 @@ using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; +using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Shell; @@ -172,15 +173,15 @@ namespace SafeExamBrowser.UserInterface.Mobile return splashScreen; } - public ISystemWirelessNetworkControl CreateWirelessNetworkControl(Location location) + public ISystemControl CreateWirelessNetworkControl(IWirelessAdapter wirelessAdapter, Location location) { if (location == Location.ActionCenter) { - return new ActionCenterWirelessNetworkControl(); + return new ActionCenterWirelessNetworkControl(wirelessAdapter, text); } else { - return new TaskbarWirelessNetworkControl(); + return new TaskbarWirelessNetworkControl(wirelessAdapter, text); } }