Amazfit Bip: Support sending notification body, not only title

This commit is contained in:
Andreas Shimokawa 2017-08-20 23:58:41 +02:00
parent a969d4b7dd
commit fdcc51cb98
3 changed files with 21 additions and 13 deletions

View File

@ -33,12 +33,16 @@ import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class AlertNotificationProfile<T extends AbstractBTLEDeviceSupport> extends AbstractBleProfile<T> {
private static final Logger LOG = LoggerFactory.getLogger(AlertNotificationProfile.class);
private static final int MAX_MSG_LENGTH = 18;
private int maxLength = 18; // Mi2-ism?
public AlertNotificationProfile(T support) {
super(support);
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void configure(TransactionBuilder builder, AlertNotificationControl control) {
BluetoothGattCharacteristic characteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_ALERT_NOTIFICATION_CONTROL_POINT);
if (characteristic != null) {
@ -57,21 +61,21 @@ public class AlertNotificationProfile<T extends AbstractBTLEDeviceSupport> exten
BluetoothGattCharacteristic characteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_NEW_ALERT);
if (characteristic != null) {
String message = StringUtils.ensureNotNull(alert.getMessage());
if (message.length() > MAX_MSG_LENGTH && strategy == OverflowStrategy.TRUNCATE) {
message = StringUtils.truncate(message, MAX_MSG_LENGTH);
if (message.length() > maxLength && strategy == OverflowStrategy.TRUNCATE) {
message = StringUtils.truncate(message, maxLength);
}
int numChunks = message.length() / MAX_MSG_LENGTH;
if (message.length() % MAX_MSG_LENGTH > 0) {
int numChunks = message.length() / maxLength;
if (message.length() % maxLength > 0) {
numChunks++;
}
try {
boolean hasAlerted = false;
for (int i = 0; i < numChunks; i++) {
int offset = i * MAX_MSG_LENGTH;
int offset = i * maxLength;
int restLength = message.length() - offset;
message = message.substring(offset, offset + Math.min(MAX_MSG_LENGTH, restLength));
message = message.substring(offset, offset + Math.min(maxLength, restLength));
if (hasAlerted && message.length() == 0) {
// no need to do it again when there is no text content
break;

View File

@ -33,7 +33,6 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class AmazfitBipSupport extends MiBand2Support {
@ -48,13 +47,17 @@ public class AmazfitBipSupport extends MiBand2Support {
onAlarmClock(notificationSpec);
return;
}
String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()).trim();
// Fixup purging notification text in getPreferredTextFor() in case of UNKNOWN
if (notificationSpec.type == NotificationType.UNKNOWN) {
message = StringUtils.getFirstOf(notificationSpec.title, notificationSpec.body);
String senderOrTiltle = StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
String message = StringUtils.truncate(senderOrTiltle, 32) + "\0";
if (notificationSpec.subject != null) {
message += StringUtils.truncate(notificationSpec.subject, 128) + "\n\n";
}
if (notificationSpec.body != null) {
message += StringUtils.truncate(notificationSpec.body, 128);
}
String origin = notificationSpec.type.getGenericType();
SimpleNotification simpleNotification = new SimpleNotification(message, BLETypeConversions.toAlertCategory(notificationSpec.type));
performPreferredNotification(origin + " received", origin, simpleNotification, MiBand2Service.ALERT_LEVEL_MESSAGE, null);

View File

@ -46,6 +46,7 @@ class AmazfitBipTextNotificationStrategy extends Mi2TextNotificationStrategy {
@Override
protected void sendAlert(@NonNull SimpleNotification simpleNotification, TransactionBuilder builder) {
AlertNotificationProfile<?> profile = new AlertNotificationProfile<>(getSupport());
profile.setMaxLength(255); // TODO: find out real limit, certainly it is more than 18 which is default
AlertCategory category = simpleNotification.getAlertCategory();
switch (simpleNotification.getAlertCategory()) {
@ -59,6 +60,6 @@ class AmazfitBipTextNotificationStrategy extends Mi2TextNotificationStrategy {
category = AlertCategory.SMS;
}
NewAlert alert = new NewAlert(category, 1, simpleNotification.getMessage());
profile.newAlert(builder, alert, OverflowStrategy.MAKE_MULTIPLE);
profile.newAlert(builder, alert, OverflowStrategy.TRUNCATE);
}
}