Search in sources :

Example 1 with AlertNotificationProfile

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile in project Gadgetbridge by Freeyourgadget.

the class PineTimeJFSupport method onNotification.

@Override
public void onNotification(NotificationSpec notificationSpec) {
    TransactionBuilder builder = new TransactionBuilder("notification");
    String message;
    if (notificationSpec.body == null) {
        notificationSpec.body = "";
    }
    if (isFirmwareAtLeastVersion0_15()) {
        String senderOrTitle = nodomain.freeyourgadget.gadgetbridge.util.StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
        message = senderOrTitle + "\0" + notificationSpec.body;
    } else {
        message = notificationSpec.body;
    }
    NewAlert alert = new NewAlert(AlertCategory.CustomHuami, 1, message);
    AlertNotificationProfile<?> profile = new AlertNotificationProfile<>(this);
    profile.setMaxLength(MaxNotificationLength);
    profile.newAlert(builder, alert, OverflowStrategy.TRUNCATE);
    builder.queue(getQueue());
}
Also used : AlertNotificationProfile(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile) NewAlert(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Example 2 with AlertNotificationProfile

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile in project Gadgetbridge by Freeyourgadget.

the class HuamiSupport method sendNotificationNew.

protected void sendNotificationNew(NotificationSpec notificationSpec, boolean hasExtraHeader, int maxLength) {
    if (notificationSpec.type == NotificationType.GENERIC_ALARM_CLOCK) {
        onAlarmClock(notificationSpec);
        return;
    }
    String senderOrTitle = StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
    String message = StringUtils.truncate(senderOrTitle, 32) + "\0";
    if (notificationSpec.subject != null) {
        message += StringUtils.truncate(notificationSpec.subject, 128) + "\n\n";
    }
    if (notificationSpec.body != null) {
        message += StringUtils.truncate(notificationSpec.body, 512);
    }
    if (notificationSpec.body == null && notificationSpec.subject == null) {
        // if we have no body we have to send at least something on some devices, else they reboot (Bip S)
        message += " ";
    }
    try {
        TransactionBuilder builder = performInitialized("new notification");
        byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type);
        AlertCategory alertCategory = AlertCategory.CustomHuami;
        // The SMS icon for AlertCategory.SMS is unique and not available as iconId
        if (notificationSpec.type == NotificationType.GENERIC_SMS) {
            alertCategory = AlertCategory.SMS;
        } else // EMAIL icon does not work in FW 0.0.8.74, it did in 0.0.7.90
        if (customIconId == HuamiIcon.EMAIL) {
            alertCategory = AlertCategory.Email;
        }
        if (characteristicChunked != null) {
            int prefixlength = 2;
            // We also need a (fake) source name for Mi Band 3 for SMS/EMAIL, else the message is not displayed
            byte[] appSuffix = "\0 \0".getBytes();
            int suffixlength = appSuffix.length;
            if (alertCategory == AlertCategory.CustomHuami) {
                String appName;
                prefixlength = 3;
                final PackageManager pm = getContext().getPackageManager();
                ApplicationInfo ai = null;
                try {
                    ai = pm.getApplicationInfo(notificationSpec.sourceAppId, 0);
                } catch (PackageManager.NameNotFoundException ignored) {
                }
                if (ai != null) {
                    appName = "\0" + pm.getApplicationLabel(ai) + "\0";
                } else {
                    appName = "\0" + "UNKNOWN" + "\0";
                }
                appSuffix = appName.getBytes();
                suffixlength = appSuffix.length;
            }
            if (hasExtraHeader) {
                prefixlength += 4;
            }
            byte[] rawmessage = message.getBytes();
            int length = Math.min(rawmessage.length, maxLength - prefixlength);
            if (length < rawmessage.length) {
                length = StringUtils.utf8ByteLength(message, length);
            }
            byte[] command = new byte[length + prefixlength + suffixlength];
            int pos = 0;
            command[pos++] = (byte) alertCategory.getId();
            if (hasExtraHeader) {
                // TODO
                command[pos++] = 0;
                command[pos++] = 0;
                command[pos++] = 0;
                command[pos++] = 0;
            }
            command[pos++] = 1;
            if (alertCategory == AlertCategory.CustomHuami) {
                command[pos] = customIconId;
            }
            System.arraycopy(rawmessage, 0, command, prefixlength, length);
            System.arraycopy(appSuffix, 0, command, prefixlength + length, appSuffix.length);
            writeToChunked(builder, 0, command);
        } else {
            AlertNotificationProfile<?> profile = new AlertNotificationProfile(this);
            NewAlert alert = new NewAlert(alertCategory, 1, message, customIconId);
            profile.setMaxLength(maxLength);
            profile.newAlert(builder, alert);
        }
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send notification to device", ex);
    }
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) AlertNotificationProfile(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile) PackageManager(android.content.pm.PackageManager) NewAlert(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert) AlertCategory(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory)

Example 3 with AlertNotificationProfile

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile in project Gadgetbridge by Freeyourgadget.

the class AmazfitBipTextNotificationStrategy method sendAlert.

@Override
protected void sendAlert(@NonNull SimpleNotification simpleNotification, TransactionBuilder builder) {
    AlertNotificationProfile<?> profile = new AlertNotificationProfile<>(getSupport());
    // TODO: find out real limit, certainly it is more than 18 which is default
    profile.setMaxLength(255);
    AlertCategory category = simpleNotification.getAlertCategory();
    switch(simpleNotification.getAlertCategory()) {
        // only these are confirmed working so far on Amazfit Bip
        case Email:
        case IncomingCall:
        case SMS:
            break;
        // default to SMS for non working categories
        default:
            category = AlertCategory.SMS;
    }
    NewAlert alert = new NewAlert(category, 1, simpleNotification.getMessage());
    profile.newAlert(builder, alert, OverflowStrategy.TRUNCATE);
}
Also used : AlertNotificationProfile(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile) NewAlert(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert) AlertCategory(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory)

Example 4 with AlertNotificationProfile

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile in project Gadgetbridge by Freeyourgadget.

the class Mi2TextNotificationStrategy method sendAlert.

protected void sendAlert(@NonNull SimpleNotification simpleNotification, TransactionBuilder builder) {
    AlertNotificationProfile<?> profile = new AlertNotificationProfile<>(getSupport());
    // override the alert category,  since only SMS and incoming call support text notification
    AlertCategory category = AlertCategory.SMS;
    if (simpleNotification.getAlertCategory() == AlertCategory.IncomingCall) {
        category = simpleNotification.getAlertCategory();
    }
    NewAlert alert = new NewAlert(category, 1, simpleNotification.getMessage());
    profile.newAlert(builder, alert, OverflowStrategy.MAKE_MULTIPLE);
}
Also used : AlertNotificationProfile(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile) NewAlert(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert) AlertCategory(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory)

Example 5 with AlertNotificationProfile

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method onTestNewFunction.

@Override
public void onTestNewFunction() {
    try {
        TransactionBuilder builder = performInitialized("incoming call from peter");
        NewAlert alert = new NewAlert(AlertCategory.Custom, 1, new String(new byte[] { 0x19 }));
        AlertNotificationProfile<MiBand2Support> profile = new AlertNotificationProfile<>(this);
        profile.newAlert(builder, alert, OverflowStrategy.MAKE_MULTIPLE);
        builder.queue(getQueue());
    } catch (IOException e) {
    }
}
Also used : AlertNotificationProfile(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile) NewAlert(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Aggregations

AlertNotificationProfile (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile)7 NewAlert (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert)7 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)4 AlertCategory (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory)4 IOException (java.io.IOException)2 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1