Compare commits

..

No commits in common. "f9b23a62a36fa62d0e18784eb8f830b2fdad274e" and "1fea2a2e1186d258193aeb309872e167b9f388b7" have entirely different histories.

2 changed files with 45 additions and 97 deletions

View File

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

View File

@ -73,56 +73,54 @@ 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)
{ {
try //dial pressed down
{
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)
{ {
await ResetAllAsync(); _audioHelper.ResetAll();
} }
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)
{ {
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in TouchPress:\n {ex}"); await Connection.ShowAlert();
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)
{
try
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate"); Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left //dial rotated. ticks positive for right, negative for left
try
{
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
_currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks); _currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks);
@ -135,46 +133,24 @@ 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 increment volume:\n {ex}"); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}");
} }
} }
async Task ResetAllAsync() public override void DialUp(DialPayload payload)
{ {
try //dial unpressed
{
_audioHelper.ResetAll();
}
catch
{
_audioHelper.ResetCache();
await Connection.ShowAlert();
throw;
}
} }
async Task ToggleMuteAsync() public override void Dispose()
{ {
try Logger.Instance.LogMessage(TracingLevel.DEBUG, "Disposing");
if (_foregroundWindowChangedEvent != IntPtr.Zero)
{ {
if (_currentAudioSession != null) Native.UnhookWinEvent(_foregroundWindowChangedEvent);
{
_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()
@ -193,8 +169,7 @@ public class DialAction : EncoderBase
} }
catch (Exception ex) catch (Exception ex)
{ {
_audioHelper.ResetCache(); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on WinEventProc\n {ex}");
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on Tick:\n {ex}");
} }
} }
@ -202,6 +177,7 @@ public class DialAction : EncoderBase
{ {
try try
{ {
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
@ -224,7 +200,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}");
} }
@ -237,40 +213,20 @@ 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)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in SaveSettings:\n {ex}");
}
}
private async Task SaveSettings() private Task SaveSettings()
{ {
try return Connection.SetSettingsAsync(JObject.FromObject(settings));
{
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}");
}
}
} }