1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-05 09:47:01 +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;
//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
* 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 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_RESP = 2;

View File

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

View File

@ -1,8 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.liveview;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Calendar;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
@ -14,11 +12,6 @@ import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
public class LiveviewProtocol extends GBDeviceProtocol {
@Override
public GBDevice getDevice() {
return super.getDevice();
}
@Override
public byte[] encodeFindDevice(boolean start) {
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.DST_OFFSET) / 1000;
ByteBuffer buffer = ByteBuffer.allocate(5);
buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.putInt(time);
buffer.put((byte) 0); // 24 hour
buffer.put(LiveviewConstants.CLOCK_24H);
return constructMessage(LiveviewConstants.MSG_GETTIME_RESP, buffer.array());
}
@ -85,30 +79,25 @@ public class LiveviewProtocol extends GBDeviceProtocol {
String footerText = (null != notificationSpec.sourceName) ? notificationSpec.sourceName : "";
String bodyText = (null != notificationSpec.body) ? notificationSpec.body : "";
try {
byte[] headerTextArray = headerText.getBytes("iso-8859-1");
byte[] footerTextArray = footerText.getBytes("iso-8859-1");
byte[] bodyTextArray = bodyText.getBytes("iso-8859-1");
int size = 15 + headerTextArray.length + bodyTextArray.length + footerTextArray.length;
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put((byte) 1);
buffer.putShort((short) 0);
buffer.putShort((short) 0);
buffer.putShort((short) 0);
buffer.put((byte) 80); //should alert but it doesn't make the liveview vibrate
byte[] headerTextArray = headerText.getBytes(LiveviewConstants.ENCODING);
byte[] footerTextArray = footerText.getBytes(LiveviewConstants.ENCODING);
byte[] bodyTextArray = bodyText.getBytes(LiveviewConstants.ENCODING);
int size = 15 + headerTextArray.length + bodyTextArray.length + footerTextArray.length;
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put((byte) 1);
buffer.putShort((short) 0);
buffer.putShort((short) 0);
buffer.putShort((short) 0);
buffer.put((byte) 80); //should alert but it doesn't make the liveview vibrate
buffer.put((byte) 0); //0 is for plaintext vs bitmapimage (1) strings
buffer.putShort((short) headerTextArray.length);
buffer.put(headerTextArray);
buffer.putShort((short) bodyTextArray.length);
buffer.put(bodyTextArray);
buffer.putShort((short) footerTextArray.length);
buffer.put(footerTextArray);
return constructMessage(LiveviewConstants.MSG_DISPLAYPANEL, buffer.array());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return encodeVibrateRequest((short) 100, (short) 200);
}
buffer.put((byte) 0); //0 is for plaintext vs bitmapimage (1) strings
buffer.putShort((short) headerTextArray.length);
buffer.put(headerTextArray);
buffer.putShort((short) bodyTextArray.length);
buffer.put(bodyTextArray);
buffer.putShort((short) footerTextArray.length);
buffer.put(footerTextArray);
return constructMessage(LiveviewConstants.MSG_DISPLAYPANEL, buffer.array());
}
@ -116,7 +105,7 @@ public class LiveviewProtocol extends GBDeviceProtocol {
public static byte[] constructMessage(byte messageType, byte[] payload) {
ByteBuffer msgBuffer = ByteBuffer.allocate(payload.length + 6);
msgBuffer.order(ByteOrder.BIG_ENDIAN);
msgBuffer.order(LiveviewConstants.BYTE_ORDER);
msgBuffer.put(messageType);
msgBuffer.put((byte) 4);
msgBuffer.putInt(payload.length);
@ -126,23 +115,18 @@ public class LiveviewProtocol extends GBDeviceProtocol {
public byte[] encodeVibrateRequest(short delay, short time) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.putShort(delay);
buffer.putShort(time);
return constructMessage(LiveviewConstants.MSG_SETVIBRATE, buffer.array());
}
public byte[] encodeCapabilitiesRequest() {
try {
byte[] version = LiveviewConstants.CLIENT_SOFTWARE_VERSION.getBytes("iso-8859-1");
ByteBuffer buffer = ByteBuffer.allocate(version.length + 1);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put((byte) version.length);
buffer.put(version);
return constructMessage(LiveviewConstants.MSG_GETCAPS, buffer.array());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
byte[] version = LiveviewConstants.CLIENT_SOFTWARE_VERSION.getBytes(LiveviewConstants.ENCODING);
ByteBuffer buffer = ByteBuffer.allocate(version.length + 1);
buffer.order(LiveviewConstants.BYTE_ORDER);
buffer.put((byte) version.length);
buffer.put(version);
return constructMessage(LiveviewConstants.MSG_GETCAPS, buffer.array());
}
}