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

View File

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

View File

@ -63,30 +63,32 @@ public class SendNotificationRequest extends Request {
else
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(
paramsProvider,
supportProvider.getNotificationId(),
getNotificationType(notificationSpec.type),
Notifications.TextEncoding.standard,
supportProvider.getHuaweiCoordinator().getContentFormat(),
title,
Notifications.TextEncoding.standard,
notificationSpec.sender,
Notifications.TextEncoding.standard,
notificationSpec.body,
body,
notificationSpec.sourceAppId
);
}
public void buildNotificationTLVFromCallSpec(CallSpec callSpec) {
this.packet = new Notifications.NotificationActionRequest(
paramsProvider,
supportProvider.getNotificationId(),
Notifications.NotificationType.call,
Notifications.TextEncoding.standard,
supportProvider.getHuaweiCoordinator().getContentFormat(),
callSpec.name,
Notifications.TextEncoding.standard,
callSpec.name,
Notifications.TextEncoding.standard,
callSpec.name,
null
);

View File

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

View File

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