Single toggle action, persist state, updated strings

Combine start/stop into single toggle action
persist state via GBDeviceEventUpdatePreferences
added subdivisions as translatable strings
changed start/stop strings to generic ones
This commit is contained in:
Mathias Micheel
2026-06-19 23:33:45 +02:00
committed by José Rebelo
parent 5fb412c296
commit a3abd2516e
4 changed files with 39 additions and 58 deletions
@@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
import android.content.Context;
import java.util.regex.Pattern;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@@ -69,26 +69,32 @@ public class SoundbrennerCoordinator extends AbstractBLEDeviceCoordinator {
public DeviceSpecificSettingsCustomizer getDeviceSpecificSettingsCustomizer(@NonNull final GBDevice device) {
return new SoundbrennerSettingsCustomizer();
}
// -------------------------------------------------------------------------
// Start/Stop buttons in gadget card
// Start/Stop button in gadget card
// -------------------------------------------------------------------------
//
// The running state is mirrored into the device-specific SharedPreferences
// by SoundbrennerSupport (see persistMetronomeRunning()), so a single
// action can read it and flip its icon/label accordingly instead of
// exposing two separate Start/Stop buttons.
@Override
public List<DeviceCardAction> getCustomActions() {
return Arrays.asList(
// START
return Collections.singletonList(
new DeviceCardAction() {
@Override
public int getIcon(@NonNull final GBDevice device) {
return R.drawable.ic_play;
return isMetronomeRunning(device) ? R.drawable.ic_stop : R.drawable.ic_play;
}
@NonNull
@Override
public String getDescription(@NonNull final GBDevice device,
@NonNull final Context context) {
return context.getString(R.string.soundbrenner_action_start);
return isMetronomeRunning(device)
? context.getString(R.string.stop)
: context.getString(R.string.start);
}
@Override
@@ -96,30 +102,13 @@ public class SoundbrennerCoordinator extends AbstractBLEDeviceCoordinator {
@NonNull final Context context) {
GBApplication.deviceService(device)
.onSendConfiguration(
SoundbrennerConstants.PREF_METRONOME_RUNNING + "_start");
}
},
// STOP
new DeviceCardAction() {
@Override
public int getIcon(@NonNull final GBDevice device) {
return R.drawable.ic_stop;
SoundbrennerConstants.PREF_METRONOME_RUNNING + "_toggle");
}
@NonNull
@Override
public String getDescription(@NonNull final GBDevice device,
@NonNull final Context context) {
return context.getString(R.string.soundbrenner_action_stop);
}
@Override
public void onClick(@NonNull final GBDevice device,
@NonNull final Context context) {
GBApplication.deviceService(device)
.onSendConfiguration(
SoundbrennerConstants.PREF_METRONOME_RUNNING + "_stop");
private boolean isMetronomeRunning(@NonNull final GBDevice device) {
return GBApplication
.getDeviceSpecificSharedPrefs(device.getAddress())
.getBoolean(SoundbrennerConstants.PREF_METRONOME_RUNNING, false);
}
}
);
@@ -15,6 +15,7 @@ import java.nio.ByteOrder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.soundbrenner.SoundbrennerConstants;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLESingleDeviceSupport;
@@ -159,23 +160,6 @@ public class SoundbrennerSupport extends AbstractBTLESingleDeviceSupport {
toggleMetronome();
return;
}
// Device-card Start/Stop buttons
if ((SoundbrennerConstants.PREF_METRONOME_RUNNING + "_start").equals(key)) {
try {
startMetronome();
} catch (IOException e) {
LOG.error("Failed to start metronome", e);
}
return;
}
if ((SoundbrennerConstants.PREF_METRONOME_RUNNING + "_stop").equals(key)) {
try {
stopMetronome();
} catch (IOException e) {
LOG.error("Failed to stop metronome", e);
}
return;
}
boolean isSoundbrennerPref = key.startsWith(SoundbrennerConstants.PREF_SOUNDBRENNER_PREFIX)
|| key.startsWith(SoundbrennerConstants.PREF_BEAT_ACCENT_PREFIX);
@@ -505,11 +489,17 @@ public class SoundbrennerSupport extends AbstractBTLESingleDeviceSupport {
}
}
/** Persist the running flag so it survives reconnects. */
/**
* Persist the running flag so it survives reconnects. Goes through
* GBDeviceEventUpdatePreferences (instead of writing SharedPreferences
* directly) so that other listeners relying on this event are notified.
*/
private void persistMetronomeRunning(boolean running) {
GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress())
.edit()
.putBoolean(SoundbrennerConstants.PREF_METRONOME_RUNNING, running)
.apply();
GBDeviceEventUpdatePreferences eventUpdatePreferences = new GBDeviceEventUpdatePreferences();
eventUpdatePreferences.withPreference(
SoundbrennerConstants.PREF_METRONOME_RUNNING,
running
);
evaluateGBDeviceEvent(eventUpdatePreferences);
}
}
+5 -5
View File
@@ -4563,7 +4563,7 @@
<item>anc_mode__anc_off</item>
<item>anc_mode__transparency_off</item>
</string-array>
<string-array name="soundbrenner_time_sig_entries">
<item>4/4</item>
<item>2/4</item>
@@ -4595,10 +4595,10 @@
</string-array>
<string-array name="soundbrenner_subdivision_entries">
<item>Quarter notes</item>
<item>Eighth notes</item>
<item>Triplets</item>
<item>Sixteenth notes</item>
<item>@string/soundbrenner_pref_subdivision_quarter</item>
<item>@string/soundbrenner_pref_subdivision_eighth</item>
<item>@string/soundbrenner_pref_subdivision_triplet</item>
<item>@string/soundbrenner_pref_subdivision_sixteenth</item>
</string-array>
<string-array name="soundbrenner_subdivision_values">
+4 -2
View File
@@ -3540,8 +3540,6 @@
<string name="soundbrenner_category_control">Control</string>
<string name="soundbrenner_pref_metronome_toggle_title">Start/Stop</string>
<string name="soundbrenner_pref_metronome_toggle_summary">Start or stop metronome</string>
<string name="soundbrenner_action_start">Start</string>
<string name="soundbrenner_action_stop">Stop</string>
<string name="soundbrenner_category_tempo">Tempo</string>
<string name="soundbrenner_category_time_signature">Time Signature</string>
<string name="soundbrenner_category_subdivision">Subdivision</string>
@@ -3566,6 +3564,10 @@
<string name="soundbrenner_pref_beat_14_title">Beat 14 Accent</string>
<string name="soundbrenner_pref_beat_15_title">Beat 15 Accent</string>
<string name="soundbrenner_pref_beat_16_title">Beat 16 Accent</string>
<string name="soundbrenner_pref_subdivision_quarter">Quarter notes</string>
<string name="soundbrenner_pref_subdivision_eighth">Eighth notes</string>
<string name="soundbrenner_pref_subdivision_sixteenth">Sixteenth notes</string>
<string name="soundbrenner_pref_subdivision_triplet">Triplets</string>
<string name="soundcore_voice_prompts">Voice Prompts</string>
<string name="soundcore_button_brightness">Button Brightness</string>
<string name="soundcore_button_brightness_low">Low</string>