move message functions, refactor into Abstract Protocol

This commit is contained in:
duhow
2026-06-14 10:34:05 +02:00
committed by José Rebelo
parent 9fd323789e
commit afbf2df4c6
7 changed files with 85 additions and 109 deletions
@@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
@@ -38,4 +39,36 @@ public abstract class AbstractSoundcoreProtocol extends GBDeviceProtocol {
else return 0x00;
}
protected SoundcorePacket decodePacket(final byte[] responseData) {
return SoundcorePacket.decode(ByteBuffer.wrap(responseData));
}
protected byte[] encodeRequest(final short command) {
return new SoundcorePacket(command).encode();
}
protected byte[] encodeCommand(final short command, final byte[] payload) {
return new SoundcorePacket(command, payload).encode();
}
protected byte[] encodeBooleanCommand(final short command, final boolean enabled) {
return encodeCommand(command, new byte[]{encodeBoolean(enabled)});
}
protected byte[] encodeByteCommand(final short command, final byte value) {
return encodeCommand(command, new byte[]{value});
}
protected byte[] encodeAutoPowerOff(final short command, final int duration, final byte disabledDuration) {
final byte[] payload;
if (duration > 0) {
payload = new byte[]{0x01, (byte) (duration - 1)};
} else {
payload = new byte[]{0x00, disabledDuration};
}
return encodeCommand(command, payload);
}
}
@@ -105,7 +105,7 @@ public class SoundcoreAeroFitProtocol extends SoundcoreProtocolImplV1 {
return encodeBooleanCommand(CMD_SET_GAMING_MODE, gamingMode);
case PREF_SOUNDCORE_AUTO_POWER_OFF:
int duration = Integer.parseInt(prefs.getString(PREF_SOUNDCORE_AUTO_POWER_OFF, "3"));
return encodeAutoPowerOff(duration, (byte) 0x03);
return encodeAutoPowerOff(CMD_SET_AUTO_POWER_OFF, duration, (byte) 0x03);
default:
LOG.debug("Unsupported CONFIG: " + config);
}
@@ -98,50 +98,50 @@ public class SoundcoreLibertyProtocol extends SoundcoreProtocolImplV1 {
// Control
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_DISABLED:
return encodeControlTouchLockMessage(TapAction.SINGLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_DISABLED, false));
return encodeControlTouchLock(TapAction.SINGLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_DISABLED, false));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_DISABLED:
return encodeControlTouchLockMessage(TapAction.DOUBLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_DISABLED, false));
return encodeControlTouchLock(TapAction.DOUBLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_DISABLED, false));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_DISABLED:
return encodeControlTouchLockMessage(TapAction.TRIPLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_DISABLED, false));
return encodeControlTouchLock(TapAction.TRIPLE_TAP, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_DISABLED, false));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_DISABLED:
return encodeControlTouchLockMessage(TapAction.LONG_PRESS, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_DISABLED, false));
return encodeControlTouchLock(TapAction.LONG_PRESS, prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_DISABLED, false));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_LEFT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_LEFT, "");
return encodeControlFunctionMessage(TapAction.SINGLE_TAP, false, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.SINGLE_TAP, false, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_RIGHT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_RIGHT, "");
return encodeControlFunctionMessage(TapAction.SINGLE_TAP, true, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.SINGLE_TAP, true, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_LEFT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_LEFT, "");
return encodeControlFunctionMessage(TapAction.DOUBLE_TAP, false, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.DOUBLE_TAP, false, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_RIGHT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_RIGHT, "");
return encodeControlFunctionMessage(TapAction.DOUBLE_TAP, true, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.DOUBLE_TAP, true, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_ACTION_LEFT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_ACTION_LEFT, "");
return encodeControlFunctionMessage(TapAction.TRIPLE_TAP, false, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.TRIPLE_TAP, false, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_ACTION_RIGHT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_TRIPLE_TAP_ACTION_RIGHT, "");
return encodeControlFunctionMessage(TapAction.TRIPLE_TAP, true, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.TRIPLE_TAP, true, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_LEFT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_LEFT, "");
return encodeControlFunctionMessage(TapAction.LONG_PRESS, false, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.LONG_PRESS, false, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_RIGHT:
pref_string = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_RIGHT, "");
return encodeControlFunctionMessage(TapAction.LONG_PRESS, true, TapFunction.valueOf(pref_string));
return encodeControlFunction(TapAction.LONG_PRESS, true, TapFunction.valueOf(pref_string));
case DeviceSettingsPreferenceConst.PREF_SONY_AMBIENT_SOUND_CONTROL_BUTTON_MODE:
AmbientSoundControlButtonMode modes = AmbientSoundControlButtonMode.fromPreferences(prefs.getPreferences());
switch (modes) {
case NC_AS_OFF:
return encodeControlAmbientModeMessage(true, true, true);
return encodeControlAmbientMode(true, true, true);
case NC_AS:
return encodeControlAmbientModeMessage(true, true, false);
return encodeControlAmbientMode(true, true, false);
case NC_OFF:
return encodeControlAmbientModeMessage(true, false, true);
return encodeControlAmbientMode(true, false, true);
case AS_OFF:
return encodeControlAmbientModeMessage(false, true, true);
return encodeControlAmbientMode(false, true, true);
}
// Miscellaneous Settings
@@ -179,7 +179,7 @@ public class SoundcoreLibertyProtocol extends SoundcoreProtocolImplV1 {
* @param disabled If the action should be enabled or disabled
* @return
*/
private byte[] encodeControlTouchLockMessage(TapAction action, boolean disabled) {
private byte[] encodeControlTouchLock(TapAction action, boolean disabled) {
boolean enabled = !disabled;
byte enabled_byte;
byte[] payload;
@@ -207,7 +207,7 @@ public class SoundcoreLibertyProtocol extends SoundcoreProtocolImplV1 {
* @param function The byte that encodes the triggered function (eg play/pause)
* @return The encoded message
*/
private byte[] encodeControlFunctionMessage(TapAction action, boolean right, TapFunction function) {
private byte[] encodeControlFunction(TapAction action, boolean right, TapFunction function) {
byte function_byte;
switch (action) {
case SINGLE_TAP:
@@ -224,14 +224,14 @@ public class SoundcoreLibertyProtocol extends SoundcoreProtocolImplV1 {
LOG.error("Invalid Tap action");
return null;
}
return encodeControlFunctionMessage(right, action.getCode(), function_byte);
return encodeControlFunction(right, action.getCode(), function_byte);
}
/**
* Encodes between which Audio Modes a tap should switch, if it is set to switch the Audio Mode.
* Zb ANC -> -> Transparency -> Normal -> ANC -> ....
*/
private byte[] encodeControlAmbientModeMessage(boolean anc, boolean transparency, boolean normal) {
private byte[] encodeControlAmbientMode(boolean anc, boolean transparency, boolean normal) {
// Original app does not allow only one true flag. Unsure if Earbuds accept this state.
byte ambientModes = (byte) (4 * (normal?1:0) + 2 * (transparency?1:0) + (anc?1:0));
return encodeCommand(CMD_SET_AMBIENT_SOUND_CONTROL_BUTTON_MODE, new byte[] {ambientModes});
@@ -41,7 +41,7 @@ public class SoundcoreMotion300DeviceSupport extends AbstractHeadphoneSerialDevi
// Device requires a little delay to respond to commands
handler.postDelayed(() -> {
final TransactionBuilder builderDelayed = createTransactionBuilder("initialize delayed");
builderDelayed.write(mDeviceProtocol.encodeGetDeviceInfo());
builderDelayed.write(mDeviceProtocol.encodeDeviceInfoRequest());
builderDelayed.queue();
}, 500);
@@ -33,8 +33,8 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePref
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.AbstractSoundcoreProtocol;
import nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.SoundcorePacket;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.*;
@@ -43,7 +43,7 @@ import static nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.pro
import static nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.protocol.impl.v1.SoundcoreProtocolImplV1.CMD_NOTIFY_CHARGING_INFO;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.protocol.impl.v1.SoundcoreProtocolImplV1.CMD_SET_AUTO_POWER_OFF;
public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
public class SoundcoreMotion300Protocol extends AbstractSoundcoreProtocol {
private static final Logger LOG = LoggerFactory.getLogger(SoundcoreMotion300Protocol.class);
// Some of these commands are not used right now, they serve as documentation
@@ -97,8 +97,7 @@ public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
@Override
public GBDeviceEvent[] decodeResponse(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
SoundcorePacket packet = SoundcorePacket.decode(buf);
final SoundcorePacket packet = decodePacket(data);
if (packet == null)
return null;
@@ -262,17 +261,17 @@ public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
switch (config) {
case PREF_SOUNDCORE_VOICE_PROMPTS:
return encodeSetBoolean(prefs, PREF_SOUNDCORE_VOICE_PROMPTS, CMD_SET_VOICE_PROMPTS);
return encodeBooleanPreference(prefs, PREF_SOUNDCORE_VOICE_PROMPTS, CMD_SET_VOICE_PROMPTS);
case PREF_SOUNDCORE_BUTTON_BRIGHTNESS:
return encodeSetByte(prefs, PREF_SOUNDCORE_BUTTON_BRIGHTNESS, CMD_SET_BUTTON_BRIGHTNESS);
return encodeBytePreference(prefs, PREF_SOUNDCORE_BUTTON_BRIGHTNESS, CMD_SET_BUTTON_BRIGHTNESS);
case PREF_SOUNDCORE_AUTO_POWER_OFF:
return encodeSetAutoPowerOff(prefs);
return encodeAutoPowerOffPreference(prefs);
case PREF_SOUNDCORE_LDAC_MODE:
return encodeSetBoolean(prefs, PREF_SOUNDCORE_LDAC_MODE, CMD_SET_LDAC_MODE);
return encodeBooleanPreference(prefs, PREF_SOUNDCORE_LDAC_MODE, CMD_SET_LDAC_MODE);
case PREF_SOUNDCORE_ADAPTIVE_DIRECTION:
return encodeSetBoolean(prefs, PREF_SOUNDCORE_ADAPTIVE_DIRECTION, CMD_SET_ADAPTIVE_DIRECTION);
return encodeBooleanPreference(prefs, PREF_SOUNDCORE_ADAPTIVE_DIRECTION, CMD_SET_ADAPTIVE_DIRECTION);
case PREF_SOUNDCORE_EQUALIZER_PRESET:
return encodeSetByte(prefs, PREF_SOUNDCORE_EQUALIZER_PRESET, CMD_SET_EQUALIZER_PRESET);
return encodeBytePreference(prefs, PREF_SOUNDCORE_EQUALIZER_PRESET, CMD_SET_EQUALIZER_PRESET);
case PREF_SOUNDCORE_EQUALIZER_DIRECTION:
return encodeRequest(CMD_GET_EQUALIZER);
case PREF_SOUNDCORE_EQUALIZER_BAND1_FREQ:
@@ -293,17 +292,13 @@ public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
case PREF_SOUNDCORE_EQUALIZER_BAND8_VALUE:
case PREF_SOUNDCORE_EQUALIZER_BAND9_FREQ:
case PREF_SOUNDCORE_EQUALIZER_BAND9_VALUE:
return encodeSetEqualizerCustom(prefs);
return encodeCustomEqualizer(prefs);
}
return super.encodeSendConfiguration(config);
}
private byte[] encodeRequest(short cmd) {
return new SoundcorePacket(cmd).encode();
}
public byte[] encodeGetDeviceInfo() {
public byte[] encodeDeviceInfoRequest() {
return encodeRequest(CMD_GET_DEVICE_INFO);
}
@@ -312,33 +307,22 @@ public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
return encodeRequest(CMD_POWER_OFF);
}
private byte[] encodeSetBoolean(Prefs prefs, String pref, short cmd) {
private byte[] encodeBooleanPreference(Prefs prefs, String pref, short cmd) {
boolean enabled = prefs.getBoolean(pref, true);
byte[] payload = new byte[] { enabled ? (byte)0x01 : (byte)0x00 };
return new SoundcorePacket(cmd, payload).encode();
return encodeBooleanCommand(cmd, enabled);
}
private byte[] encodeSetByte(Prefs prefs, String pref, short cmd) {
private byte[] encodeBytePreference(Prefs prefs, String pref, short cmd) {
byte value = (byte)Integer.parseInt(prefs.getString(pref, "0"));
byte[] payload = new byte[] { value };
return new SoundcorePacket(cmd, payload).encode();
return encodeByteCommand(cmd, value);
}
private byte[] encodeSetAutoPowerOff(Prefs prefs) {
byte duration = (byte)Integer.parseInt(prefs.getString(PREF_SOUNDCORE_AUTO_POWER_OFF, "2"));
byte[] payload;
if (duration > 0)
payload = new byte[] { (byte)0x01, (byte)(duration - 1) };
else
payload = new byte[] { (byte)0x00, (byte)0x00 };
return new SoundcorePacket(CMD_SET_AUTO_POWER_OFF, payload).encode();
private byte[] encodeAutoPowerOffPreference(Prefs prefs) {
final int duration = Integer.parseInt(prefs.getString(PREF_SOUNDCORE_AUTO_POWER_OFF, "2"));
return encodeAutoPowerOff(CMD_SET_AUTO_POWER_OFF, duration, (byte)0x00);
}
private byte[] encodeSetEqualizerCustom(Prefs prefs) {
private byte[] encodeCustomEqualizer(Prefs prefs) {
ByteBuffer buf = ByteBuffer.allocate(21);
int eqDirection = Integer.parseInt(prefs.getString(PREF_SOUNDCORE_EQUALIZER_DIRECTION, "0"));
byte[] equalizer = equalizerFromPrefs(prefs);
@@ -349,7 +333,7 @@ public class SoundcoreMotion300Protocol extends GBDeviceProtocol {
buf.put((byte)0xff);
buf.put(equalizer);
return new SoundcorePacket(CMD_SET_EQUALIZER_CUSTOM, buf.array()).encode();
return encodeCommand(CMD_SET_EQUALIZER_CUSTOM, buf.array());
}
private Map<String, Object> equalizerToPrefs(byte[] equalizer) {
@@ -2,13 +2,11 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.protocol.
import android.content.SharedPreferences;
import java.nio.ByteBuffer;
import java.util.Arrays;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.AbstractSoundcoreProtocol;
import nodomain.freeyourgadget.gadgetbridge.service.devices.soundcore.SoundcorePacket;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public abstract class SoundcoreProtocolImplV1 extends AbstractSoundcoreProtocol {
@@ -30,22 +28,6 @@ public abstract class SoundcoreProtocolImplV1 extends AbstractSoundcoreProtocol
super(device);
}
protected SoundcorePacket decodePacket(final byte[] responseData) {
return SoundcorePacket.decode(ByteBuffer.wrap(responseData));
}
protected byte[] encodeRequest(final short command) {
return new SoundcorePacket(command).encode();
}
protected byte[] encodeCommand(final short command, final byte[] payload) {
return new SoundcorePacket(command, payload).encode();
}
protected byte[] encodeBooleanCommand(final short command, final boolean enabled) {
return encodeCommand(command, new byte[]{encodeBoolean(enabled)});
}
public byte[] encodeDeviceInfoRequest() {
return encodeRequest(CMD_GET_DEVICE_INFO);
}
@@ -63,19 +45,7 @@ public abstract class SoundcoreProtocolImplV1 extends AbstractSoundcoreProtocol
return encodeCommand(CMD_ENABLE_PAIRING_MODE, new byte[]{0x00, (byte) 0x90});
}
protected byte[] encodeAutoPowerOff(final int duration, final byte disabledDuration) {
final byte[] payload;
if (duration > 0) {
payload = new byte[]{0x01, (byte) (duration - 1)};
} else {
payload = new byte[]{0x00, disabledDuration};
}
return encodeCommand(CMD_SET_AUTO_POWER_OFF, payload);
}
protected byte[] encodeControlFunctionMessage(final boolean right, final byte action, final byte function) {
protected byte[] encodeControlFunction(final boolean right, final byte action, final byte function) {
return encodeCommand(CMD_SET_CONTROL_FUNCTION, new byte[]{encodeBoolean(right), action, function});
}
@@ -84,28 +84,28 @@ public class SoundcoreSportX20Protocol extends SoundcoreLibertyProtocol {
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_LEFT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_LEFT, "PLAYPAUSE");
return encodeControlFunctionMessage(TapAction.SINGLE_TAP, false, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.SINGLE_TAP, false, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_RIGHT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_SINGLE_TAP_ACTION_RIGHT, "PLAYPAUSE");
return encodeControlFunctionMessage(TapAction.SINGLE_TAP, true, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.SINGLE_TAP, true, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_LEFT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_LEFT, "MEDIA_PREV");
return encodeControlFunctionMessage(TapAction.DOUBLE_TAP, false, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.DOUBLE_TAP, false, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_RIGHT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_DOUBLE_TAP_ACTION_RIGHT, "MEDIA_NEXT");
return encodeControlFunctionMessage(TapAction.DOUBLE_TAP, true, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.DOUBLE_TAP, true, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_LEFT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_LEFT, "AMBIENT_SOUND_CONTROL");
return encodeControlFunctionMessage(TapAction.LONG_PRESS, false, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.LONG_PRESS, false, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_RIGHT:
prefString = prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_CONTROL_LONG_PRESS_ACTION_RIGHT, "AMBIENT_SOUND_CONTROL");
return encodeControlFunctionMessage(TapAction.LONG_PRESS, true, TapFunction.valueOf(prefString));
return encodeControlFunction(TapAction.LONG_PRESS, true, TapFunction.valueOf(prefString));
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_AUTO_POWER_OFF:
final int duration = Integer.parseInt(prefs.getString(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_AUTO_POWER_OFF, "3"));
return encodeAutoPowerOff(duration);
return encodeAutoPowerOff(CMD_SET_AUTO_POWER_OFF, duration, (byte) 0x03);
case DeviceSettingsPreferenceConst.PREF_SOUNDCORE_TOUCH_TONE:
final boolean pressAlert = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_SOUNDCORE_TOUCH_TONE, false);
@@ -134,7 +134,7 @@ public class SoundcoreSportX20Protocol extends SoundcoreLibertyProtocol {
}
}
private byte[] encodeControlFunctionMessage(final TapAction action, final boolean right, final TapFunction function) {
private byte[] encodeControlFunction(final TapAction action, final boolean right, final TapFunction function) {
final byte functionByte;
switch (action) {
case SINGLE_TAP:
@@ -150,18 +150,7 @@ public class SoundcoreSportX20Protocol extends SoundcoreLibertyProtocol {
return null;
}
return encodeControlFunctionMessage(right, action.getCode(), functionByte);
}
/**
* 0: No Auto Power off
* 1: Auto Power off 10 min
* 2: Auto Power off 20 min
* 3: Auto Power off 30 min
* 4: Auto Power off 60 min
*/
private byte[] encodeAutoPowerOff(final int duration) {
return encodeAutoPowerOff(duration, (byte) 0x03);
return encodeControlFunction(right, action.getCode(), functionByte);
}
private byte[] encodeEqualizer() {