diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventFmFrequency.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventFmFrequency.java index e21c1061f..8e56be5dc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventFmFrequency.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventFmFrequency.java @@ -17,5 +17,9 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents; public class GBDeviceEventFmFrequency extends GBDeviceEvent { - public float frequency; + public final float frequency; + + public GBDeviceEventFmFrequency(final float frequency) { + this.frequency = frequency; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventLEDColor.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventLEDColor.java index 845857046..86f104f6c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventLEDColor.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventLEDColor.java @@ -17,5 +17,9 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents; public class GBDeviceEventLEDColor extends GBDeviceEvent { - public int color; + public final int color; + + public GBDeviceEventLEDColor(final int color) { + this.color = color; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi1Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi1Coordinator.java index 09c380d78..bfbc082fe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi1Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi1Coordinator.java @@ -31,15 +31,15 @@ public class Roidmi1Coordinator extends RoidmiCoordinator { @NonNull @Override - public DeviceType getSupportedType(GBDeviceCandidate candidate) { + public DeviceType getSupportedType(final GBDeviceCandidate candidate) { try { - BluetoothDevice device = candidate.getDevice(); - String name = device.getName(); + final BluetoothDevice device = candidate.getDevice(); + final String name = device.getName(); if (name != null && name.contains("睿米车载蓝牙播放器")) { return DeviceType.ROIDMI; } - } catch (Exception ex) { + } catch (final Exception ex) { LOG.error("unable to check device support", ex); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi3Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi3Coordinator.java index 277989a16..33fe5c9b1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi3Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/roidmi/Roidmi3Coordinator.java @@ -31,9 +31,9 @@ public class Roidmi3Coordinator extends RoidmiCoordinator { @NonNull @Override - public DeviceType getSupportedType(GBDeviceCandidate candidate) { + public DeviceType getSupportedType(final GBDeviceCandidate candidate) { try { - BluetoothDevice device = candidate.getDevice(); + final BluetoothDevice device = candidate.getDevice(); final String name = device.getName(); if (name == null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi1Protocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi1Protocol.java index 041390c0e..319e1d436 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi1Protocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi1Protocol.java @@ -56,7 +56,7 @@ public class Roidmi1Protocol extends RoidmiProtocol { private static final byte[] COMMAND_PERIODIC = new byte[]{(byte) 0xaa, 0x55, 0x02, 0x01, (byte) 0x85, (byte) 0x88, (byte) 0xc3, 0x3c}; @Override - public GBDeviceEvent[] decodeResponse(byte[] responseData) { + public GBDeviceEvent[] decodeResponse(final byte[] responseData) { if (responseData.length <= PACKET_MIN_LENGTH) { LOG.info("Response too small"); return null; @@ -83,17 +83,15 @@ public class Roidmi1Protocol extends RoidmiProtocol { switch (responseData[3]) { case COMMAND_GET_COLOR: - int color = responseData[5]; + final int color = responseData[5]; LOG.debug("Got color: " + color); - GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(); - evColor.color = RoidmiConst.COLOR_PRESETS[color - 1]; + final GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(RoidmiConst.COLOR_PRESETS[color - 1]); return new GBDeviceEvent[]{evColor}; case COMMAND_GET_FREQUENCY: - String frequencyHex = GB.hexdump(responseData, 4, 2); - float frequency = Float.valueOf(frequencyHex) / 10.0f; + final String frequencyHex = GB.hexdump(responseData, 4, 2); + final float frequency = Float.parseFloat(frequencyHex) / 10.0f; LOG.debug("Got frequency: " + frequency); - GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(); - evFrequency.frequency = frequency; + final GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(frequency); return new GBDeviceEvent[]{evFrequency}; default: LOG.error("Unrecognized response type 0x" + GB.hexdump(responseData, packetHeader().length, 1)); @@ -102,8 +100,8 @@ public class Roidmi1Protocol extends RoidmiProtocol { } @Override - public byte[] encodeLedColor(int color) { - int[] presets = RoidmiConst.COLOR_PRESETS; + public byte[] encodeLedColor(final int color) { + final int[] presets = RoidmiConst.COLOR_PRESETS; int color_id = -1; for (int i = 0; i < presets.length; i++) { if (presets[i] == color) { @@ -119,11 +117,11 @@ public class Roidmi1Protocol extends RoidmiProtocol { } @Override - public byte[] encodeFmFrequency(float frequency) { + public byte[] encodeFmFrequency(final float frequency) { if (frequency < 87.5 || frequency > 108.0) throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0"); - byte[] freq = frequencyToBytes(frequency); + final byte[] freq = frequencyToBytes(frequency); return encodeCommand(COMMAND_SET_FREQUENCY, freq[0], freq[1]); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi3Protocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi3Protocol.java index 3df7cf07a..bd5d9eba0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi3Protocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/Roidmi3Protocol.java @@ -73,37 +73,37 @@ public class Roidmi3Protocol extends RoidmiProtocol { LOG.warn("Potentially unsupported response: " + GB.hexdump(res, 0, res.length)); } - if (res[1] == RESPONSE_VOLTAGE) { - String voltageHex = GB.hexdump(res, 3, 2); - float voltage = Float.valueOf(voltageHex) / 100.0f; - LOG.debug("Got voltage: " + voltage); - GBDeviceEventBatteryInfo evBattery = new GBDeviceEventBatteryInfo(); - evBattery.state = BatteryState.NO_BATTERY; - evBattery.level = GBDevice.BATTERY_UNKNOWN; - evBattery.voltage = voltage; - return new GBDeviceEvent[]{evBattery}; - } else if (res[1] == RESPONSE_COLOR) { - LOG.debug("Got color: #" + GB.hexdump(res, 3, 3)); - int color = 0xFF000000 | ((res[3] << 16) & 0xFF0000) | ((res[4] << 8) & 0xFF00) | (res[5] & 0xFF); - GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(); - evColor.color = color; - return new GBDeviceEvent[]{evColor}; - } else if (res[1] == RESPONSE_FREQUENCY) { - String frequencyHex = GB.hexdump(res, 3, 2); - float frequency = Float.valueOf(frequencyHex) / 10.0f; - LOG.debug("Got frequency: " + frequency); - GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(); - evFrequency.frequency = frequency; - return new GBDeviceEvent[]{evFrequency}; - } else { - LOG.error("Unrecognized response: " + GB.hexdump(res, 0, res.length)); - return null; + switch(res[1]) { + case RESPONSE_VOLTAGE: + final String voltageHex = GB.hexdump(res, 3, 2); + final float voltage = Float.parseFloat(voltageHex) / 100.0f; + LOG.debug("Got voltage: " + voltage); + GBDeviceEventBatteryInfo evBattery = new GBDeviceEventBatteryInfo(); + evBattery.state = BatteryState.NO_BATTERY; + evBattery.level = GBDevice.BATTERY_UNKNOWN; + evBattery.voltage = voltage; + return new GBDeviceEvent[]{evBattery}; + case RESPONSE_COLOR: + LOG.debug("Got color: #" + GB.hexdump(res, 3, 3)); + final int color = 0xFF000000 | ((res[3] << 16) & 0xFF0000) | ((res[4] << 8) & 0xFF00) | (res[5] & 0xFF); + final GBDeviceEventLEDColor evColor = new GBDeviceEventLEDColor(color); + return new GBDeviceEvent[]{evColor}; + case RESPONSE_FREQUENCY: + final String frequencyHex = GB.hexdump(res, 3, 2); + final float frequency = Float.parseFloat(frequencyHex) / 10.0f; + LOG.debug("Got frequency: " + frequency); + final GBDeviceEventFmFrequency evFrequency = new GBDeviceEventFmFrequency(frequency); + return new GBDeviceEvent[]{evFrequency}; + default: + LOG.error("Unrecognized response: " + GB.hexdump(res, 0, res.length)); } + + return null; } @Override - public byte[] encodeLedColor(int color) { - byte[] cmd = COMMAND_SET_COLOR.clone(); + public byte[] encodeLedColor(final int color) { + final byte[] cmd = COMMAND_SET_COLOR.clone(); cmd[2] = (byte) (color >> 16); cmd[3] = (byte) (color >> 8); @@ -113,12 +113,12 @@ public class Roidmi3Protocol extends RoidmiProtocol { } @Override - public byte[] encodeFmFrequency(float frequency) { + public byte[] encodeFmFrequency(final float frequency) { if (frequency < 87.5 || frequency > 108.0) throw new IllegalArgumentException("Frequency must be >= 87.5 and <= 180.0"); - byte[] cmd = COMMAND_SET_FREQUENCY.clone(); - byte[] freq = frequencyToBytes(frequency); + final byte[] cmd = COMMAND_SET_FREQUENCY.clone(); + final byte[] freq = frequencyToBytes(frequency); cmd[2] = freq[0]; cmd[3] = freq[1]; @@ -149,8 +149,8 @@ public class Roidmi3Protocol extends RoidmiProtocol { return encodeCommand(COMMAND_GET_VOLTAGE); } - public byte[] encodeDenoise(boolean enabled) { - byte[] cmd = enabled ? COMMAND_DENOISE_ON : COMMAND_DENOISE_OFF; + public byte[] encodeDenoise(final boolean enabled) { + final byte[] cmd = enabled ? COMMAND_DENOISE_ON : COMMAND_DENOISE_OFF; return encodeCommand(cmd); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiIoThread.java index 12fad2cba..702ebe2b4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiIoThread.java @@ -45,16 +45,16 @@ public class RoidmiIoThread extends BtClassicIoThread { @Override protected byte[] parseIncoming(InputStream inputStream) throws IOException { - ByteArrayOutputStream msgStream = new ByteArrayOutputStream(); + final ByteArrayOutputStream msgStream = new ByteArrayOutputStream(); boolean finished = false; - byte[] incoming = new byte[1]; + final byte[] incoming = new byte[1]; while (!finished) { inputStream.read(incoming); msgStream.write(incoming); - byte[] arr = msgStream.toByteArray(); + final byte[] arr = msgStream.toByteArray(); if (arr.length > HEADER.length) { int expectedLength = HEADER.length + TRAILER.length + arr[HEADER.length] + 2; if (arr.length == expectedLength) { @@ -63,7 +63,7 @@ public class RoidmiIoThread extends BtClassicIoThread { } } - byte[] msgArray = msgStream.toByteArray(); + final byte[] msgArray = msgStream.toByteArray(); LOG.debug("Packet: " + GB.hexdump(msgArray, 0, msgArray.length)); return msgArray; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiProtocol.java index 1d658af54..c801cc6af 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiProtocol.java @@ -54,7 +54,7 @@ public abstract class RoidmiProtocol extends GBDeviceProtocol { public abstract byte[] packetTrailer(); public byte[] encodeCommand(byte... params) { - byte[] cmd = new byte[packetHeader().length + packetTrailer().length + params.length + 2]; + final byte[] cmd = new byte[packetHeader().length + packetTrailer().length + params.length + 2]; for (int i = 0; i < packetHeader().length; i++) cmd[i] = packetHeader()[i]; @@ -79,13 +79,13 @@ public abstract class RoidmiProtocol extends GBDeviceProtocol { } public byte[] frequencyToBytes(float frequency) { - byte[] res = new byte[2]; - String format = String.format(Locale.getDefault(), "%04d", (int) (10.0f * frequency)); + final byte[] res = new byte[2]; + final String format = String.format(Locale.ROOT, "%04d", (int) (10.0f * frequency)); try { res[0] = (byte) (Integer.parseInt(format.substring(0, 2), 16) & 255); res[1] = (byte) (Integer.parseInt(format.substring(2), 16) & 255); - } catch (Exception e) { - LOG.error(e.getMessage()); + } catch (final Exception e) { + LOG.error("Failed to format frequency {}", frequency, e); } return res; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiSupport.java index 0303d71a5..f9470875c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/roidmi/RoidmiSupport.java @@ -70,7 +70,7 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport { LOG.error("Failed to get Roidmi infos after 6 tries"); } } - } catch (Exception e) { + } catch (final Exception e) { LOG.error("Failed to get Roidmi infos", e); } } @@ -91,13 +91,17 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport { @Override protected GBDeviceProtocol createDeviceProtocol() { - if (getDevice().getType() == DeviceType.ROIDMI) { - return new Roidmi1Protocol(getDevice()); - } else if (getDevice().getType() == DeviceType.ROIDMI3) { - return new Roidmi3Protocol(getDevice()); + final DeviceType deviceType = getDevice().getType(); + + switch(deviceType) { + case ROIDMI: + return new Roidmi1Protocol(getDevice()); + case ROIDMI3: + return new Roidmi3Protocol(getDevice()); + default: + LOG.error("Unsupported device type {} with key = {}", deviceType, deviceType.getKey()); } - LOG.error("Unsupported device type with key = " + getDevice().getType().getKey()); return null; } @@ -105,8 +109,8 @@ public class RoidmiSupport extends AbstractSerialDeviceSupport { public void onSendConfiguration(final String config) { LOG.debug("onSendConfiguration " + config); - RoidmiIoThread roidmiIoThread = getDeviceIOThread(); - RoidmiProtocol roidmiProtocol = (RoidmiProtocol) getDeviceProtocol(); + final RoidmiIoThread roidmiIoThread = getDeviceIOThread(); + final RoidmiProtocol roidmiProtocol = (RoidmiProtocol) getDeviceProtocol(); switch (config) { case RoidmiConst.ACTION_GET_LED_COLOR: