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();
public void ResetCache()
{
lock(_lock)
{
_current = null;
}
}
public IAudioSession GetActiveSession(FallbackBehavior fallbackBehavior)
{
lock (_lock)
@ -67,11 +75,11 @@ public class AudioHelper
if(_current == null)
{
if(fallbackBehavior == FallbackBehavior.SystemSounds)
if(fallbackBehavior == FallbackBehavior.SystemSounds && _current is not SystemSoundsAudioSession)
{
_current = GetSystemSounds();
}
else if(fallbackBehavior == FallbackBehavior.SystemVolume)
else if(fallbackBehavior == FallbackBehavior.SystemVolume && _current is not SystemVolumeAudioSession)
{
_current = GetSystemVolume();
}

View File

@ -73,54 +73,56 @@ public class DialAction : EncoderBase
_ = 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)
{
//dial pressed down
try
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down");
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)
{
try
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press");
if (payload.IsLongPress)
{
_audioHelper.ResetAll();
await ResetAllAsync();
}
else
{
await ToggleMuteAsync();
}
}
async Task ToggleMuteAsync()
{
try
{
if (_currentAudioSession != null)
{
_currentAudioSession.ToggleMute();
await UpdateStateIfNeeded();
}
else
{
await Connection.ShowAlert();
}
}
catch (Exception ex)
{
await Connection.ShowAlert();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}");
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in TouchPress:\n {ex}");
}
}
public override async void DialRotate(DialRotatePayload payload)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
try
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
if (_currentAudioSession != null)
{
_currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks);
@ -133,24 +135,46 @@ public class DialAction : EncoderBase
}
catch (Exception ex)
{
_audioHelper.ResetCache();
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");
if (_foregroundWindowChangedEvent != IntPtr.Zero)
try
{
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()
@ -169,7 +193,8 @@ public class DialAction : EncoderBase
}
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
{
if (_currentAudioSession != null)
{
@ -213,20 +237,40 @@ public class DialAction : EncoderBase
public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
try
{
Tools.AutoPopulateSettings(settings, payload.Settings);
SaveSettings();
}
private Task SaveSettings()
catch (Exception ex)
{
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)
{
try
{
OnTick();
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in DialDown:\n {ex}");
}
}
}