Search in sources :

Example 1 with AlertCategory

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory 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 2 with AlertCategory

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

the class AmazfitGTS2MiniSupport method sendNotificationNew.

@Override
protected void sendNotificationNew(NotificationSpec notificationSpec, boolean hasExtraHeader, int maxLength) {
    if (notificationSpec.type == NotificationType.GENERIC_ALARM_CLOCK) {
        onAlarmClock(notificationSpec);
        return;
    }
    // step 2: (formerly in try block) get notification type
    AlertCategory alertCategory = AlertCategory.CustomHuami;
    byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type);
    // step 3: build notification (sender+body)
    /*
         * Format followed by the device:
         * <SENDER> \0 <BODY> \0 <APP SUFFIX>
         * sender will get ignored except for the icons
         * specified on the HuamiIcon class.
         * for email, App Suffix will be taken as sender
         */
    String senderOrTitle = StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
    boolean acceptsSender = HuamiIcon.acceptsSender(customIconId);
    String message;
    if (!acceptsSender && !senderOrTitle.equals(notificationSpec.sourceName)) {
        // make sure we always include the notification sender/title
        // leave title blank, it's useless
        message = "-\0";
        message += StringUtils.truncate(senderOrTitle, 64) + "\n";
    } else {
        message = StringUtils.truncate(senderOrTitle, 64) + "\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");
        // step 4: append suffix
        byte[] appSuffix = "\0 \0".getBytes();
        int suffixlength = appSuffix.length;
        // The SMS icon for AlertCategory.SMS is unique and not available as iconId
        if (notificationSpec.type == NotificationType.GENERIC_SMS) {
            alertCategory = AlertCategory.SMS;
        } else // EMAIL will take the sender from the suffix instead
        if (customIconId == HuamiIcon.EMAIL) {
            alertCategory = AlertCategory.Email;
            appSuffix = ("\0" + senderOrTitle + "\0").getBytes();
            suffixlength = appSuffix.length;
        }
        // if I understood correctly, we don't need the extra logic for mi band 2 here
        int prefixlength = 2;
        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" + "UNKNOWN" + "\0";
            } else {
                appName = "\0" + pm.getApplicationLabel(ai) + "\0";
            }
            appSuffix = appName.getBytes();
            suffixlength = appSuffix.length;
        }
        if (hasExtraHeader) {
            prefixlength += 4;
        }
        // final step: build command
        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);
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send notification to device", ex);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) AlertCategory(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory)

Example 3 with AlertCategory

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory 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 AlertCategory

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory 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 AlertCategory

use of nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory 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)

Aggregations

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