Compare commits

...

2 Commits

2 changed files with 103 additions and 51 deletions

View File

@ -54,6 +54,14 @@ public class AudioHelper
static object _lock = new object(); static object _lock = new object();
public void ResetCache()
{
lock(_lock)
{
_current = null;
}
}
public IAudioSession GetActiveSession(FallbackBehavior fallbackBehavior) public IAudioSession GetActiveSession(FallbackBehavior fallbackBehavior)
{ {
lock (_lock) lock (_lock)
@ -67,11 +75,11 @@ public class AudioHelper
if(_current == null) if(_current == null)
{ {
if(fallbackBehavior == FallbackBehavior.SystemSounds) if(fallbackBehavior == FallbackBehavior.SystemSounds && _current is not SystemSoundsAudioSession)
{ {
_current = GetSystemSounds(); _current = GetSystemSounds();
} }
else if(fallbackBehavior == FallbackBehavior.SystemVolume) else if(fallbackBehavior == FallbackBehavior.SystemVolume && _current is not SystemVolumeAudioSession)
{ {
_current = GetSystemVolume(); _current = GetSystemVolume();
} }

View File

@ -73,54 +73,56 @@ public class DialAction : EncoderBase
_ = UpdateStateIfNeeded(); _ = UpdateStateIfNeeded();
} }
public override void Dispose()
{
Logger.Instance.LogMessage(TracingLevel.DEBUG, "Disposing");
if (_foregroundWindowChangedEvent != IntPtr.Zero)
{
Native.UnhookWinEvent(_foregroundWindowChangedEvent);
}
_dispatcher.InvokeShutdown();
}
public override async void DialDown(DialPayload payload) public override async void DialDown(DialPayload payload)
{ {
//dial pressed down try
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down"); Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down");
await ToggleMuteAsync(); await ToggleMuteAsync();
} }
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in DialDown:\n {ex}");
}
}
public override void DialUp(DialPayload payload) { }
public override async void TouchPress(TouchpadPressPayload payload) public override async void TouchPress(TouchpadPressPayload payload)
{
try
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press"); Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press");
if (payload.IsLongPress) if (payload.IsLongPress)
{ {
_audioHelper.ResetAll(); await ResetAllAsync();
} }
else else
{ {
await ToggleMuteAsync(); await ToggleMuteAsync();
} }
} }
async Task ToggleMuteAsync()
{
try
{
if (_currentAudioSession != null)
{
_currentAudioSession.ToggleMute();
await UpdateStateIfNeeded();
}
else
{
await Connection.ShowAlert();
}
}
catch (Exception ex) catch (Exception ex)
{ {
await Connection.ShowAlert(); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in TouchPress:\n {ex}");
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}");
} }
} }
public override async void DialRotate(DialRotatePayload payload) public override async void DialRotate(DialRotatePayload payload)
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
try try
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
_currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks); _currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks);
@ -133,24 +135,46 @@ public class DialAction : EncoderBase
} }
catch (Exception ex) catch (Exception ex)
{ {
_audioHelper.ResetCache();
await Connection.ShowAlert(); await Connection.ShowAlert();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}"); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to increment volume:\n {ex}");
} }
} }
public override void DialUp(DialPayload payload) async Task ResetAllAsync()
{ {
//dial unpressed try
{
_audioHelper.ResetAll();
}
catch
{
_audioHelper.ResetCache();
await Connection.ShowAlert();
throw;
}
} }
public override void Dispose() async Task ToggleMuteAsync()
{ {
Logger.Instance.LogMessage(TracingLevel.DEBUG, "Disposing"); try
if (_foregroundWindowChangedEvent != IntPtr.Zero)
{ {
Native.UnhookWinEvent(_foregroundWindowChangedEvent); if (_currentAudioSession != null)
{
_currentAudioSession.ToggleMute();
await UpdateStateIfNeeded();
}
else
{
await Connection.ShowAlert();
}
}
catch
{
_audioHelper.ResetCache();
await Connection.ShowAlert();
throw;
} }
_dispatcher.InvokeShutdown();
} }
public override async void OnTick() public override async void OnTick()
@ -169,7 +193,8 @@ public class DialAction : EncoderBase
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on WinEventProc\n {ex}"); _audioHelper.ResetCache();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on Tick:\n {ex}");
} }
} }
@ -177,7 +202,6 @@ public class DialAction : EncoderBase
{ {
try try
{ {
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
@ -200,7 +224,7 @@ public class DialAction : EncoderBase
_previousState = uiState; _previousState = uiState;
} }
} }
catch (Exception ex ) catch (Exception ex)
{ {
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Failed to update screen\n {ex}"); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Failed to update screen\n {ex}");
} }
@ -213,20 +237,40 @@ public class DialAction : EncoderBase
public override void ReceivedSettings(ReceivedSettingsPayload payload) public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
try
{ {
Tools.AutoPopulateSettings(settings, payload.Settings); Tools.AutoPopulateSettings(settings, payload.Settings);
SaveSettings(); SaveSettings();
} }
catch (Exception ex)
private Task SaveSettings()
{ {
return Connection.SetSettingsAsync(JObject.FromObject(settings)); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in SaveSettings:\n {ex}");
}
}
private async Task SaveSettings()
{
try
{
await Connection.SetSettingsAsync(JObject.FromObject(settings));
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in SaveSettings:\n {ex}");
}
} }
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
try
{ {
OnTick(); OnTick();
} }
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in DialDown:\n {ex}");
}
}
} }