1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 06:22:55 +01:00

Liveview: addressed the first feedback.

- centralized string encoding and byte order
- replaced printStrackTrace with LOG.error
This commit is contained in:
Daniele Gobbetti 2016-12-04 19:10:58 +01:00
parent e0a844b60a
commit 8c01123a48
3 changed files with 45 additions and 50 deletions

View File

@ -1,6 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.devices.liveview; package nodomain.freeyourgadget.gadgetbridge.devices.liveview;
//Changed by Renze: Fixed brightness constants //Changed by Renze: Fixed brightness constants
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/** /**
* Message constants reverse-engineered by Andrew de Quincey (<a * Message constants reverse-engineered by Andrew de Quincey (<a
* href="http://adq.livejournal.com">http://adq.livejournal.com</a>). * href="http://adq.livejournal.com">http://adq.livejournal.com</a>).
@ -9,6 +13,12 @@ package nodomain.freeyourgadget.gadgetbridge.devices.liveview;
*/ */
public final class LiveviewConstants { public final class LiveviewConstants {
public static Charset ENCODING = StandardCharsets.ISO_8859_1;
public static ByteOrder BYTE_ORDER = ByteOrder.BIG_ENDIAN;
public static final byte CLOCK_24H = 0;
public static final byte CLOCK_12H = 1;
public static final byte MSG_GETCAPS = 1; public static final byte MSG_GETCAPS = 1;
public static final byte MSG_GETCAPS_RESP = 2; public static final byte MSG_GETCAPS_RESP = 2;

View File

@ -14,10 +14,10 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.devices.liveview.LiveviewConstants;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
@ -45,7 +45,7 @@ public class LiveviewIoThread extends GBDeviceIoThread {
try { try {
mBtSocket.close(); mBtSocket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error(e.getMessage());
} }
} }
} }
@ -99,6 +99,7 @@ public class LiveviewIoThread extends GBDeviceIoThread {
} }
} catch (IOException e) { } catch (IOException e) {
if (e.getMessage() != null && e.getMessage().contains("socket closed")) { //FIXME: this does not feel right if (e.getMessage() != null && e.getMessage().contains("socket closed")) { //FIXME: this does not feel right
LOG.info(e.getMessage()); LOG.info(e.getMessage());
mIsConnected = false; mIsConnected = false;
@ -116,7 +117,7 @@ public class LiveviewIoThread extends GBDeviceIoThread {
try { try {
mBtSocket.close(); mBtSocket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error(e.getMessage());
} }
mBtSocket = null; mBtSocket = null;
} }
@ -143,8 +144,8 @@ public class LiveviewIoThread extends GBDeviceIoThread {
mOutStream = mBtSocket.getOutputStream(); mOutStream = mBtSocket.getOutputStream();
setUpdateState(GBDevice.State.CONNECTED); setUpdateState(GBDevice.State.CONNECTED);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error("Server socket cannot be started.");
LOG.error("Server socket cannot be started"); //LOG.error(e.getMessage());
setUpdateState(originalState); setUpdateState(originalState);
mInStream = null; mInStream = null;
mOutStream = null; mOutStream = null;
@ -214,7 +215,7 @@ public class LiveviewIoThread extends GBDeviceIoThread {
private int getLastInt(ByteArrayOutputStream stream) { private int getLastInt(ByteArrayOutputStream stream) {
byte[] array = stream.toByteArray(); byte[] array = stream.toByteArray();
ByteBuffer buffer = ByteBuffer.wrap(array, array.length - 4, 4); ByteBuffer buffer = ByteBuffer.wrap(array, array.length - 4, 4);
buffer.order(ByteOrder.BIG_ENDIAN); buffer.order(LiveviewConstants.BYTE_ORDER);
return buffer.getInt(); return buffer.getInt();
} }
} }

View File

@ -1,8 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.liveview; package nodomain.freeyourgadget.gadgetbridge.service.devices.liveview;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Calendar; import java.util.Calendar;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
@ -14,11 +12,6 @@ import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
public class LiveviewProtocol extends GBDeviceProtocol { public class LiveviewProtocol extends GBDeviceProtocol {
@Override
public GBDevice getDevice() {
return super.getDevice();
}
@Override @Override
public byte[] encodeFindDevice(boolean start) { public byte[] encodeFindDevice(boolean start) {
return encodeVibrateRequest((short) 100, (short) 200); return encodeVibrateRequest((short) 100, (short) 200);
@ -67,8 +60,9 @@ public class LiveviewProtocol extends GBDeviceProtocol {
time += Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 1000; time += Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 1000;
time += Calendar.getInstance().get(Calendar.DST_OFFSET) / 1000; time += Calendar.getInstance().get(Calendar.DST_OFFSET) / 1000;
ByteBuffer buffer = ByteBuffer.allocate(5); ByteBuffer buffer = ByteBuffer.allocate(5);
buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.putInt(time); buffer.putInt(time);
buffer.put((byte) 0); // 24 hour buffer.put(LiveviewConstants.CLOCK_24H);
return constructMessage(LiveviewConstants.MSG_GETTIME_RESP, buffer.array()); return constructMessage(LiveviewConstants.MSG_GETTIME_RESP, buffer.array());
} }
@ -85,10 +79,9 @@ public class LiveviewProtocol extends GBDeviceProtocol {
String footerText = (null != notificationSpec.sourceName) ? notificationSpec.sourceName : ""; String footerText = (null != notificationSpec.sourceName) ? notificationSpec.sourceName : "";
String bodyText = (null != notificationSpec.body) ? notificationSpec.body : ""; String bodyText = (null != notificationSpec.body) ? notificationSpec.body : "";
try { byte[] headerTextArray = headerText.getBytes(LiveviewConstants.ENCODING);
byte[] headerTextArray = headerText.getBytes("iso-8859-1"); byte[] footerTextArray = footerText.getBytes(LiveviewConstants.ENCODING);
byte[] footerTextArray = footerText.getBytes("iso-8859-1"); byte[] bodyTextArray = bodyText.getBytes(LiveviewConstants.ENCODING);
byte[] bodyTextArray = bodyText.getBytes("iso-8859-1");
int size = 15 + headerTextArray.length + bodyTextArray.length + footerTextArray.length; int size = 15 + headerTextArray.length + bodyTextArray.length + footerTextArray.length;
ByteBuffer buffer = ByteBuffer.allocate(size); ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put((byte) 1); buffer.put((byte) 1);
@ -105,10 +98,6 @@ public class LiveviewProtocol extends GBDeviceProtocol {
buffer.putShort((short) footerTextArray.length); buffer.putShort((short) footerTextArray.length);
buffer.put(footerTextArray); buffer.put(footerTextArray);
return constructMessage(LiveviewConstants.MSG_DISPLAYPANEL, buffer.array()); return constructMessage(LiveviewConstants.MSG_DISPLAYPANEL, buffer.array());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return encodeVibrateRequest((short) 100, (short) 200);
}
} }
@ -116,7 +105,7 @@ public class LiveviewProtocol extends GBDeviceProtocol {
public static byte[] constructMessage(byte messageType, byte[] payload) { public static byte[] constructMessage(byte messageType, byte[] payload) {
ByteBuffer msgBuffer = ByteBuffer.allocate(payload.length + 6); ByteBuffer msgBuffer = ByteBuffer.allocate(payload.length + 6);
msgBuffer.order(ByteOrder.BIG_ENDIAN); msgBuffer.order(LiveviewConstants.BYTE_ORDER);
msgBuffer.put(messageType); msgBuffer.put(messageType);
msgBuffer.put((byte) 4); msgBuffer.put((byte) 4);
msgBuffer.putInt(payload.length); msgBuffer.putInt(payload.length);
@ -126,23 +115,18 @@ public class LiveviewProtocol extends GBDeviceProtocol {
public byte[] encodeVibrateRequest(short delay, short time) { public byte[] encodeVibrateRequest(short delay, short time) {
ByteBuffer buffer = ByteBuffer.allocate(4); ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN); buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.putShort(delay); buffer.putShort(delay);
buffer.putShort(time); buffer.putShort(time);
return constructMessage(LiveviewConstants.MSG_SETVIBRATE, buffer.array()); return constructMessage(LiveviewConstants.MSG_SETVIBRATE, buffer.array());
} }
public byte[] encodeCapabilitiesRequest() { public byte[] encodeCapabilitiesRequest() {
try { byte[] version = LiveviewConstants.CLIENT_SOFTWARE_VERSION.getBytes(LiveviewConstants.ENCODING);
byte[] version = LiveviewConstants.CLIENT_SOFTWARE_VERSION.getBytes("iso-8859-1");
ByteBuffer buffer = ByteBuffer.allocate(version.length + 1); ByteBuffer buffer = ByteBuffer.allocate(version.length + 1);
buffer.order(ByteOrder.BIG_ENDIAN); buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.put((byte) version.length); buffer.put((byte) version.length);
buffer.put(version); buffer.put(version);
return constructMessage(LiveviewConstants.MSG_GETCAPS, buffer.array()); return constructMessage(LiveviewConstants.MSG_GETCAPS, buffer.array());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
} }
} }