[Huawei] Fix long notification and clean Notifications class

This commit is contained in:
Damien 'Psolyca' Gaignon 2024-04-02 00:42:43 +02:00
parent 64a0b716f0
commit a29187a034
No known key found for this signature in database
GPG Key ID: 9E9404E5D9E11843
5 changed files with 81 additions and 85 deletions

View File

@ -32,6 +32,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsScreen; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsScreen;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications.NotificationConstraintsType; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications.NotificationConstraintsType;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -68,8 +69,8 @@ public class HuaweiCoordinator {
this.notificationConstraints = ByteBuffer.wrap(GB.hexStringToByteArray( this.notificationConstraints = ByteBuffer.wrap(GB.hexStringToByteArray(
getCapabilitiesSharedPreferences().getString( getCapabilitiesSharedPreferences().getString(
key, key,
"00F00002001E0002001E0002001E") GB.hexdump(Notifications.defaultConstraints)
)); )));
} }
} }
} }
@ -176,7 +177,7 @@ public class HuaweiCoordinator {
} }
private int getNotificationConstraint(byte which) { private int getNotificationConstraint(byte which) {
return notificationConstraints.get(which); return (int)notificationConstraints.getShort(which);
} }
public DeviceSpecificSettings getDeviceSpecificSettings(final GBDevice device) { public DeviceSpecificSettings getDeviceSpecificSettings(final GBDevice device) {
@ -469,32 +470,36 @@ public class HuaweiCoordinator {
return supportsNotificationConstraint(NotificationConstraintsType.incomingNumberSupport); return supportsNotificationConstraint(NotificationConstraintsType.incomingNumberSupport);
} }
public byte getYellowPagesFormat() { public int getContentFormat() {
return (byte)getNotificationConstraint(NotificationConstraintsType.yellowPagesFormat); return getNotificationConstraint(NotificationConstraintsType.contentFormat);
} }
public byte getContentSignFormat() { public int getYellowPagesFormat() {
return (byte)getNotificationConstraint(NotificationConstraintsType.contentSignFormat); return getNotificationConstraint(NotificationConstraintsType.yellowPagesFormat);
} }
public byte getIncomingFormatFormat() { public int getContentSignFormat() {
return (byte)getNotificationConstraint(NotificationConstraintsType.incomingNumberFormat); return getNotificationConstraint(NotificationConstraintsType.contentSignFormat);
} }
public short getContentLength() { public int getIncomingFormatFormat() {
return (short)getNotificationConstraint(NotificationConstraintsType.contentLength); return getNotificationConstraint(NotificationConstraintsType.incomingNumberFormat);
} }
public short getYellowPagesLength() { public int getContentLength() {
return (short)getNotificationConstraint(NotificationConstraintsType.yellowPagesLength); return getNotificationConstraint(NotificationConstraintsType.contentLength);
} }
public short getContentSignLength() { public int getYellowPagesLength() {
return (short)getNotificationConstraint(NotificationConstraintsType.contentSignLength); return getNotificationConstraint(NotificationConstraintsType.yellowPagesLength);
} }
public short getIncomingNumberLength() { public int getContentSignLength() {
return (short)getNotificationConstraint(NotificationConstraintsType.incomingNumberLength); return getNotificationConstraint(NotificationConstraintsType.contentSignLength);
}
public int getIncomingNumberLength() {
return getNotificationConstraint(NotificationConstraintsType.incomingNumberLength);
} }
public int getAlarmSlotCount(GBDevice gbDevice) { public int getAlarmSlotCount(GBDevice gbDevice) {

View File

@ -17,13 +17,18 @@
package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets; package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV;
public class Notifications { public class Notifications {
public static final byte id = 0x02; public static final byte id = 0x02;
public static final byte[] defaultConstraints = new byte[]{
(short)0x02, (short)0x0F,
(short)0x00, (short)0x02, (short)0x1E,
(short)0x00, (short)0x02, (short)0x1E,
(short)0x00, (short)0x02, (short)0x1E
};
public static class NotificationActionRequest extends HuaweiPacket { public static class NotificationActionRequest extends HuaweiPacket {
public static final byte id = 0x01; public static final byte id = 0x01;
@ -51,11 +56,9 @@ public class Notifications {
ParamsProvider paramsProvider, ParamsProvider paramsProvider,
short notificationId, short notificationId,
byte notificationType, byte notificationType,
byte titleEncoding, int encoding,
String titleContent, String titleContent,
byte senderEncoding,
String senderContent, String senderContent,
byte bodyEncoding,
String bodyContent, String bodyContent,
String sourceAppId String sourceAppId
) { ) {
@ -74,22 +77,22 @@ public class Notifications {
HuaweiTLV subTlv = new HuaweiTLV(); HuaweiTLV subTlv = new HuaweiTLV();
if (titleContent != null) if (titleContent != null)
subTlv.put(0x8D, new HuaweiTLV() subTlv.put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x03) .put(0x0E, (byte) TextType.title)
.put(0x0F, titleEncoding) .put(0x0F, (byte) encoding)
.put(0x10, titleContent) .put(0x10, titleContent)
); );
if (senderContent != null) if (senderContent != null)
subTlv.put(0x8D, new HuaweiTLV() subTlv.put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x02) .put(0x0E, (byte) TextType.sender)
.put(0x0F, senderEncoding) .put(0x0F, (byte) encoding)
.put(0x10, senderContent) .put(0x10, senderContent)
); );
if (bodyContent != null) if (bodyContent != null)
subTlv.put(0x8D, new HuaweiTLV() subTlv.put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x01) .put(0x0E, (byte) TextType.text)
.put(0x0F, bodyEncoding) .put(0x0F, (byte) encoding)
.put(0x10, bodyContent) .put(0x10, bodyContent)
); );
@ -142,29 +145,30 @@ public class Notifications {
@Override @Override
public void parseTlv() throws ParseException { public void parseTlv() throws ParseException {
this.constraints = ByteBuffer.allocate(14); this.constraints = ByteBuffer.allocate(22);
List<HuaweiTLV> subContainers = this.tlv HuaweiTLV container = this.tlv
.getObject(0x81) .getObject(0x81)
.getObject(0x82) .getObject(0x82)
.getObjects(0x90); .getObject(0x90);
for (HuaweiTLV subContainer : subContainers) { for (HuaweiTLV subContainer : container.getObjects(0x91)) {
HuaweiTLV subSubContainer = subContainer.getObject(0x91); if (subContainer.getByte(0x12) == 0x01) {
if (subSubContainer.getByte(0x12) == 0x01) putByteBuffer(constraints, NotificationConstraintsType.contentFormat, new byte[] {0x02}); //Always 0x02 even if gadget report 0x03
putByteBuffer(constraints, NotificationConstraintsType.contentLength,subSubContainer.getBytes(0x14)); putByteBuffer(constraints, NotificationConstraintsType.contentLength, subContainer.getBytes(0x14));
if (subSubContainer.getByte(0x12) == 0x05) {
constraints.put(NotificationConstraintsType.yellowPagesSupport,(byte)0x01);
constraints.put(NotificationConstraintsType.yellowPagesFormat,subSubContainer.getByte(0x13));
putByteBuffer(constraints, NotificationConstraintsType.yellowPagesLength,subSubContainer.getBytes(0x14));
} }
if (subSubContainer.getByte(0x12) == 0x06) { if (subContainer.getByte(0x12) == 0x05) {
constraints.put(NotificationConstraintsType.contentSignSupport,(byte)0x01); constraints.putShort(NotificationConstraintsType.yellowPagesSupport,(short)0x01);
constraints.put(NotificationConstraintsType.contentSignFormat,subSubContainer.getByte(0x13)); putByteBuffer(constraints, NotificationConstraintsType.yellowPagesFormat,subContainer.getBytes(0x13));
putByteBuffer(constraints, NotificationConstraintsType.contentSignLength,subSubContainer.getBytes(0x14)); putByteBuffer(constraints, NotificationConstraintsType.yellowPagesLength,subContainer.getBytes(0x14));
} }
if (subSubContainer.getByte(0x12) == 0x07 ) { if (subContainer.getByte(0x12) == 0x06) {
constraints.put(NotificationConstraintsType.incomingNumberSupport,(byte)0x01); constraints.putShort(NotificationConstraintsType.contentSignSupport,(short)0x01);
constraints.put(NotificationConstraintsType.incomingNumberFormat,subSubContainer.getByte(0x13)); putByteBuffer(constraints, NotificationConstraintsType.contentSignFormat,subContainer.getBytes(0x13));
putByteBuffer(constraints, NotificationConstraintsType.incomingNumberLength,subSubContainer.getBytes(0x14)); putByteBuffer(constraints, NotificationConstraintsType.contentSignLength,subContainer.getBytes(0x14));
}
if (subContainer.getByte(0x12) == 0x07 ) {
constraints.putShort(NotificationConstraintsType.incomingNumberSupport,(short)0x01);
putByteBuffer(constraints, NotificationConstraintsType.incomingNumberFormat,subContainer.getBytes(0x13));
putByteBuffer(constraints, NotificationConstraintsType.incomingNumberLength,subContainer.getBytes(0x14));
} }
} }
constraints.rewind(); constraints.rewind();
@ -174,22 +178,21 @@ public class Notifications {
public static class NotificationConstraintsType { public static class NotificationConstraintsType {
// TODO: enum? // TODO: enum?
public static final byte contentFormat = 0x00;
public static final byte contentLength = 0x00; public static final byte contentLength = 0x02;
public static final byte yellowPagesSupport = 0x02; public static final byte yellowPagesSupport = 0x04;
public static final byte yellowPagesFormat = 0x03; public static final byte yellowPagesFormat = 0x06;
public static final byte yellowPagesLength = 0x04; public static final byte yellowPagesLength = 0x08;
public static final byte contentSignSupport = 0x06; public static final byte contentSignSupport = 0x0A;
public static final byte contentSignFormat = 0x07; public static final byte contentSignFormat = 0x0C;
public static final byte contentSignLength = 0x08; public static final byte contentSignLength = 0x0E;
public static final byte incomingNumberSupport = 0x0A; public static final byte incomingNumberSupport = 0x10;
public static final byte incomingNumberFormat = 0x0B; public static final byte incomingNumberFormat = 0x12;
public static final byte incomingNumberLength = 0x0C; public static final byte incomingNumberLength = 0x14;
} }
public static class NotificationType { public static class NotificationType {
// TODO: enum? // TODO: enum?
public static final byte call = 0x01; public static final byte call = 0x01;
public static final byte sms = 0x02; public static final byte sms = 0x02;
public static final byte weChat = 0x03; public static final byte weChat = 0x03;
@ -202,7 +205,6 @@ public class Notifications {
public static class TextType { public static class TextType {
// TODO: enum? // TODO: enum?
public static final int text = 0x01; public static final int text = 0x01;
public static final int sender = 0x02; public static final int sender = 0x02;
public static final int title = 0x03; public static final int title = 0x03;
@ -214,13 +216,6 @@ public class Notifications {
public static final int weather = 0x0A; public static final int weather = 0x0A;
} }
public static class TextEncoding {
// TODO: enum?
public static final byte unknown = 0x01;
public static final byte standard = 0x02;
}
public static class NotificationStateRequest extends HuaweiPacket { public static class NotificationStateRequest extends HuaweiPacket {
public static final byte id = 0x04; public static final byte id = 0x04;

View File

@ -63,30 +63,32 @@ public class SendNotificationRequest extends Request {
else else
title = notificationSpec.sourceName; title = notificationSpec.sourceName;
String body = notificationSpec.body;
if (body.length() > supportProvider.getHuaweiCoordinator().getContentLength()) {
body = notificationSpec.body.substring(0x0, supportProvider.getHuaweiCoordinator().getContentLength() - 0xC);
body += "...";
}
this.packet = new Notifications.NotificationActionRequest( this.packet = new Notifications.NotificationActionRequest(
paramsProvider, paramsProvider,
supportProvider.getNotificationId(), supportProvider.getNotificationId(),
getNotificationType(notificationSpec.type), getNotificationType(notificationSpec.type),
Notifications.TextEncoding.standard, supportProvider.getHuaweiCoordinator().getContentFormat(),
title, title,
Notifications.TextEncoding.standard,
notificationSpec.sender, notificationSpec.sender,
Notifications.TextEncoding.standard, body,
notificationSpec.body,
notificationSpec.sourceAppId notificationSpec.sourceAppId
); );
} }
public void buildNotificationTLVFromCallSpec(CallSpec callSpec) { public void buildNotificationTLVFromCallSpec(CallSpec callSpec) {
this.packet = new Notifications.NotificationActionRequest( this.packet = new Notifications.NotificationActionRequest(
paramsProvider, paramsProvider,
supportProvider.getNotificationId(), supportProvider.getNotificationId(),
Notifications.NotificationType.call, Notifications.NotificationType.call,
Notifications.TextEncoding.standard, supportProvider.getHuaweiCoordinator().getContentFormat(),
callSpec.name, callSpec.name,
Notifications.TextEncoding.standard,
callSpec.name, callSpec.name,
Notifications.TextEncoding.standard,
callSpec.name, callSpec.name,
null null
); );

View File

@ -37,11 +37,9 @@ public class StopNotificationRequest extends Request {
paramsProvider, paramsProvider,
supportProvider.getNotificationId(), supportProvider.getNotificationId(),
Notifications.NotificationType.stopNotification, Notifications.NotificationType.stopNotification,
Notifications.TextEncoding.standard, supportProvider.getHuaweiCoordinator().getContentFormat(),
null, null,
Notifications.TextEncoding.standard,
null, null,
Notifications.TextEncoding.standard,
null, null,
null null
).serialize(); ).serialize();

View File

@ -64,11 +64,9 @@ public class TestNotifications {
public void testNotificationActionRequest() throws NoSuchFieldException, IllegalAccessException, HuaweiPacket.CryptoException { public void testNotificationActionRequest() throws NoSuchFieldException, IllegalAccessException, HuaweiPacket.CryptoException {
short notificationId = 0x01; short notificationId = 0x01;
byte notificationType = 0x02; byte notificationType = 0x02;
byte titleEncoding = 0x02; byte encoding = 0x02;
String titleContent = "Title"; String titleContent = "Title";
byte senderEncoding = 0x02;
String senderContent = "Sender"; String senderContent = "Sender";
byte bodyEncoding = 0x02;
String bodyContent = "Body"; String bodyContent = "Body";
String sourceAppId = "SourceApp"; String sourceAppId = "SourceApp";
@ -83,17 +81,17 @@ public class TestNotifications {
.put(0x8C, new HuaweiTLV() .put(0x8C, new HuaweiTLV()
.put(0x8D, new HuaweiTLV() .put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x03) .put(0x0E, (byte) 0x03)
.put(0x0F, titleEncoding) .put(0x0F, encoding)
.put(0x10, titleContent) .put(0x10, titleContent)
) )
.put(0x8D, new HuaweiTLV() .put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x02) .put(0x0E, (byte) 0x02)
.put(0x0F, senderEncoding) .put(0x0F, encoding)
.put(0x10, senderContent) .put(0x10, senderContent)
) )
.put(0x8D, new HuaweiTLV() .put(0x8D, new HuaweiTLV()
.put(0x0E, (byte) 0x01) .put(0x0E, (byte) 0x01)
.put(0x0F, bodyEncoding) .put(0x0F, encoding)
.put(0x10, bodyContent) .put(0x10, bodyContent)
) )
) )
@ -104,11 +102,9 @@ public class TestNotifications {
secretsProvider, secretsProvider,
notificationId, notificationId,
notificationType, notificationType,
titleEncoding, encoding,
titleContent, titleContent,
senderEncoding,
senderContent, senderContent,
bodyEncoding,
bodyContent, bodyContent,
sourceAppId sourceAppId
); );