Skip to content

Commit

Permalink
Fix custom beeps (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
valterc authored Aug 2, 2024
1 parent 030d0b5 commit 2410082
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 75 deletions.
7 changes: 6 additions & 1 deletion app/src/main/java/com/valterc/ki2/data/input/KarooKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public enum KarooKey {
/**
* Virtual key to do double beep.
*/
VIRTUAL_DOUBLE_BEEP(VIRTUAL_NONE.keyCode + 9);
VIRTUAL_DOUBLE_BEEP(VIRTUAL_NONE.keyCode + 9),

/**
* Virtual key to do a bell beep.
*/
VIRTUAL_BELL(VIRTUAL_NONE.keyCode + 10);

public static KarooKey fromKeyCode(int keyCode) {
for (KarooKey karooKey : values()) {
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/valterc/ki2/input/InputManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class InputManager {
preferenceToSwitchKeyMap.put("press_enable_audio_alerts", (switchEvent, converter) -> new KarooKeyEvent(KarooKey.VIRTUAL_ENABLE_AUDIO_ALERTS, KeyAction.SINGLE_PRESS, switchEvent.getRepeat()));
preferenceToSwitchKeyMap.put("press_single_beep", (switchEvent, converter) -> new KarooKeyEvent(KarooKey.VIRTUAL_SINGLE_BEEP, KeyAction.SINGLE_PRESS, switchEvent.getRepeat()));
preferenceToSwitchKeyMap.put("press_double_beep", (switchEvent, converter) -> new KarooKeyEvent(KarooKey.VIRTUAL_DOUBLE_BEEP, KeyAction.SINGLE_PRESS, switchEvent.getRepeat()));
preferenceToSwitchKeyMap.put("press_bell", (switchEvent, converter) -> new KarooKeyEvent(KarooKey.VIRTUAL_BELL, KeyAction.SINGLE_PRESS, switchEvent.getRepeat()));

/*
* Double press events
Expand Down Expand Up @@ -140,6 +141,18 @@ public class InputManager {
}
return new KarooKeyEvent(KarooKey.VIRTUAL_SINGLE_BEEP, KeyAction.SINGLE_PRESS, switchEvent.getRepeat());
});
preferenceToSwitchKeyMap.put("hold_short_single_bell", (switchEvent, converter) -> {
if (switchEvent.getCommand() != SwitchCommand.LONG_PRESS_DOWN) {
return null;
}
return new KarooKeyEvent(KarooKey.VIRTUAL_BELL, KeyAction.SINGLE_PRESS, switchEvent.getRepeat());
});
preferenceToSwitchKeyMap.put("hold_continuous_bell", (switchEvent, converter) -> {
if (switchEvent.getCommand() == SwitchCommand.LONG_PRESS_UP) {
return null;
}
return new KarooKeyEvent(KarooKey.VIRTUAL_BELL, KeyAction.SINGLE_PRESS, switchEvent.getRepeat());
});
}

private final SharedPreferences preferences;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public VirtualInputAdapter(Ki2Context ki2Context) {
this.keyMapping.put(KarooKey.VIRTUAL_ENABLE_AUDIO_ALERTS, karooKeyEvent -> enableAudioAlerts(ki2Context));
this.keyMapping.put(KarooKey.VIRTUAL_SINGLE_BEEP, karooKeyEvent -> ki2Context.getServiceClient().sendMessage(new AudioAlertMessage(ki2Context.getSdkContext().getString(R.string.value_preference_audio_alert_single_beep))));
this.keyMapping.put(KarooKey.VIRTUAL_DOUBLE_BEEP, karooKeyEvent -> ki2Context.getServiceClient().sendMessage(new AudioAlertMessage(ki2Context.getSdkContext().getString(R.string.value_preference_audio_alert_double_beep))));
this.keyMapping.put(KarooKey.VIRTUAL_BELL, karooKeyEvent -> ki2Context.getServiceClient().sendMessage(new AudioAlertMessage(ki2Context.getSdkContext().getString(R.string.value_preference_audio_alert_bell))));
}

private void showToast(Ki2Context ki2Context, int textResId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@ private Supplier<Boolean> getAudioAlertCaller(String audioAlertName) {
return () -> true;

case "karoo_workout_interval":
return () -> ActivityServiceAudioAlertManagerHook.beepKarooWorkoutInterval(context.getSdkContext());
return () -> ActivityServiceAudioAlertManagerHook.beepKarooWorkoutInterval(context.getSdkContext())
|| AudioAlertHook.triggerWorkoutNewInterval(context.getSdkContext());

case "karoo_auto_lap":
return () -> ActivityServiceAudioAlertManagerHook.beepKarooAutoLap(context.getSdkContext());
return () -> ActivityServiceAudioAlertManagerHook.beepKarooAutoLap(context.getSdkContext())
|| AudioAlertHook.triggerAutoLap(context.getSdkContext());

case "karoo_bell":
return () -> ActivityServiceAudioAlertManagerHook.beepKarooBell(context.getSdkContext())
|| AudioAlertHook.triggerBell(context.getSdkContext());

case "custom_single_beep":
return () -> ActivityServiceAudioAlertManagerHook.beepSingle(context.getSdkContext());
return () -> ActivityServiceAudioAlertManagerHook.beepSingle(context.getSdkContext())
|| AudioAlertHook.triggerAutoLap(context.getSdkContext());

case "custom_double_beep":
return () -> ActivityServiceAudioAlertManagerHook.beepDouble(context.getSdkContext());
return () -> ActivityServiceAudioAlertManagerHook.beepDouble(context.getSdkContext())
|| AudioAlertHook.triggerAutoLap(context.getSdkContext());
}

return null;
Expand All @@ -119,43 +127,35 @@ private void tryTriggerAudioAlert(Runnable audioTrigger) {
public void triggerShiftingLowestGearAudioAlert() {
tryTriggerAudioAlert(() -> {
Supplier<Boolean> audioAlertCaller = getAudioAlertCaller(audioAlertLowestGear);
boolean attempt = audioAlertCaller != null && audioAlertCaller.get();
if (!attempt) {
Log.w("KI2", "Unable to use audio alert manager hook, using fallback");
AudioAlertHook.triggerAutoLap(context.getSdkContext());
if (audioAlertCaller != null) {
audioAlertCaller.get();
}
});
}

public void triggerShiftingHighestGearAudioAlert() {
tryTriggerAudioAlert(() -> {
Supplier<Boolean> audioAlertCaller = getAudioAlertCaller(audioAlertHighestGear);
boolean attempt = audioAlertCaller != null && audioAlertCaller.get();
if (!attempt) {
Log.w("KI2", "Unable to use audio alert manager hook, using fallback");
AudioAlertHook.triggerAutoLap(context.getSdkContext());
if (audioAlertCaller != null) {
audioAlertCaller.get();
}
});
}

public void triggerShiftingLimitAudioAlert() {
tryTriggerAudioAlert(() -> {
Supplier<Boolean> audioAlertCaller = getAudioAlertCaller(audioAlertShiftingLimit);
boolean attempt = audioAlertCaller != null && audioAlertCaller.get();
if (!attempt) {
Log.w("KI2", "Unable to use audio alert manager hook, using fallback");
AudioAlertHook.triggerAutoLap(context.getSdkContext());
if (audioAlertCaller != null) {
audioAlertCaller.get();
}
});
}

public void triggerSynchroShiftAudioAlert() {
tryTriggerAudioAlert(() -> {
Supplier<Boolean> audioAlertCaller = getAudioAlertCaller(audioAlertUpcomingSynchroShift);
boolean attempt = audioAlertCaller != null && audioAlertCaller.get();
if (!attempt) {
Log.w("KI2", "Unable to use audio alert manager hook, using fallback");
AudioAlertHook.triggerWorkoutNewInterval(context.getSdkContext());
if (audioAlertCaller != null) {
audioAlertCaller.get();
}
});
}
Expand Down
Loading

0 comments on commit 2410082

Please sign in to comment.