Mi Band 2: support app icons and textual notifications for more apps

This commit is contained in:
Andreas Shimokawa 2017-10-22 00:02:36 +02:00
parent a809ad214d
commit f07b77f903
9 changed files with 51 additions and 29 deletions

View File

@ -264,20 +264,29 @@ public class BLETypeConversions {
case GENERIC_SMS: case GENERIC_SMS:
return AlertCategory.SMS; return AlertCategory.SMS;
case GENERIC_EMAIL: case GENERIC_EMAIL:
case GMAIL:
case OUTLOOK:
case YAHOO_MAIL:
return AlertCategory.Email; return AlertCategory.Email;
case GENERIC_NAVIGATION: case GENERIC_NAVIGATION:
return AlertCategory.Simple; return AlertCategory.Simple;
case CONVERSATIONS:
case FACEBOOK_MESSENGER:
case GOOGLE_MESSENGER:
case GOOGLE_HANGOUTS:
case HIPCHAT:
case KAKAO_TALK:
case LINE:
case RIOT: case RIOT:
case SIGNAL: case SIGNAL:
case SKYPE:
case SNAPCHAT:
case TELEGRAM: case TELEGRAM:
case WHATSAPP:
case CONVERSATIONS:
case FACEBOOK:
case FACEBOOK_MESSENGER:
case TWITTER: case TWITTER:
case WHATSAPP:
case VIBER:
case WECHAT:
return AlertCategory.InstantMessage; return AlertCategory.InstantMessage;
case UNKNOWN:
return AlertCategory.Simple;
} }
return AlertCategory.Simple; return AlertCategory.Simple;
} }

View File

@ -38,7 +38,7 @@ public enum AlertCategory {
// 251-255 defined by service specification // 251-255 defined by service specification
Any(255), Any(255),
Custom(-1), Custom(-1),
CustomMiBand2(-6); CustomHuami(-6);
private final int id; private final int id;

View File

@ -103,7 +103,7 @@ public class AlertNotificationProfile<T extends AbstractBTLEDeviceSupport> exten
ByteArrayOutputStream stream = new ByteArrayOutputStream(100); ByteArrayOutputStream stream = new ByteArrayOutputStream(100);
stream.write(BLETypeConversions.fromUint8(alert.getCategory().getId())); stream.write(BLETypeConversions.fromUint8(alert.getCategory().getId()));
stream.write(BLETypeConversions.fromUint8(alert.getNumAlerts())); stream.write(BLETypeConversions.fromUint8(alert.getNumAlerts()));
if (alert.getCategory() == AlertCategory.CustomMiBand2) { if (alert.getCategory() == AlertCategory.CustomHuami) {
stream.write(BLETypeConversions.fromUint8(alert.getCustomIcon())); stream.write(BLETypeConversions.fromUint8(alert.getCustomIcon()));
} }

View File

@ -88,7 +88,7 @@ public class AmazfitBipSupport extends MiBand2Support {
byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type); byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type);
AlertCategory alertCategory = AlertCategory.CustomMiBand2; AlertCategory alertCategory = AlertCategory.CustomHuami;
// The SMS icon for AlertCategory.SMS is unique and not available as iconId // The SMS icon for AlertCategory.SMS is unique and not available as iconId
if (notificationSpec.type == NotificationType.GENERIC_SMS) { if (notificationSpec.type == NotificationType.GENERIC_SMS) {

View File

@ -16,21 +16,28 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.common; package nodomain.freeyourgadget.gadgetbridge.service.devices.common;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory;
public class SimpleNotification { public class SimpleNotification {
private final String message; private final String message;
private final AlertCategory alertCategory; private final AlertCategory alertCategory;
private final NotificationType notificationType;
public SimpleNotification(String message, AlertCategory alertCategory) { public SimpleNotification(String message, AlertCategory alertCategory, NotificationType notificationType) {
this.message = message; this.message = message;
this.alertCategory = alertCategory; this.alertCategory = alertCategory;
this.notificationType = notificationType;
} }
public AlertCategory getAlertCategory() { public AlertCategory getAlertCategory() {
return alertCategory; return alertCategory;
} }
public NotificationType getNotificationType() {
return notificationType;
}
public String getMessage() { public String getMessage() {
return message; return message;
} }

View File

@ -587,7 +587,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} }
}; };
String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()); String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext());
SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.HighPriorityAlert); SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.HighPriorityAlert, null);
performPreferredNotification("alarm clock ringing", simpleNotification, MiBandConst.ORIGIN_ALARM_CLOCK, abortAction); performPreferredNotification("alarm clock ringing", simpleNotification, MiBandConst.ORIGIN_ALARM_CLOCK, abortAction);
} }
@ -653,7 +653,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} }
}; };
String message = NotificationUtils.getPreferredTextFor(callSpec); String message = NotificationUtils.getPreferredTextFor(callSpec);
SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.IncomingCall); SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.IncomingCall, null);
performPreferredNotification("incoming call", simpleNotification, MiBandConst.ORIGIN_INCOMING_CALL, abortAction); performPreferredNotification("incoming call", simpleNotification, MiBandConst.ORIGIN_INCOMING_CALL, abortAction);
} else if ((callSpec.command == CallSpec.CALL_START) || (callSpec.command == CallSpec.CALL_END)) { } else if ((callSpec.command == CallSpec.CALL_START) || (callSpec.command == CallSpec.CALL_END)) {
telephoneRinging = false; telephoneRinging = false;
@ -745,7 +745,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
return !isLocatingDevice; return !isLocatingDevice;
} }
}; };
SimpleNotification simpleNotification = new SimpleNotification(getContext().getString(R.string.find_device_you_found_it), AlertCategory.HighPriorityAlert); SimpleNotification simpleNotification = new SimpleNotification(getContext().getString(R.string.find_device_you_found_it), AlertCategory.HighPriorityAlert, null);
performDefaultNotification("locating device", simpleNotification, (short) 255, abortAction); performDefaultNotification("locating device", simpleNotification, (short) 255, abortAction);
} }
} }

View File

@ -20,7 +20,6 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile; import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic;
@ -30,6 +29,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotificat
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.OverflowStrategy; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.OverflowStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification; import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class Mi2TextNotificationStrategy extends Mi2NotificationStrategy { public class Mi2TextNotificationStrategy extends Mi2NotificationStrategy {
@ -63,15 +63,13 @@ public class Mi2TextNotificationStrategy extends Mi2NotificationStrategy {
protected byte[] getNotifyMessage(SimpleNotification simpleNotification) { protected byte[] getNotifyMessage(SimpleNotification simpleNotification) {
int numAlerts = 1; int numAlerts = 1;
if (simpleNotification != null) { if (simpleNotification != null && simpleNotification.getNotificationType() != null && simpleNotification.getAlertCategory() != AlertCategory.SMS) {
switch (simpleNotification.getAlertCategory()) { byte customIconId = HuamiIcon.mapToIconId(simpleNotification.getNotificationType());
case Email: if (customIconId == HuamiIcon.EMAIL) {
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.Email.getId()), BLETypeConversions.fromUint8(numAlerts)}; // unfortunately. the email icon breaks the notification, fall back to a standard AlertCategory
case InstantMessage: return new byte[]{BLETypeConversions.fromUint8(AlertCategory.Email.getId()), BLETypeConversions.fromUint8(numAlerts)};
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.CustomMiBand2.getId()), BLETypeConversions.fromUint8(numAlerts), HuamiIcon.WECHAT};
case News:
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.CustomMiBand2.getId()), BLETypeConversions.fromUint8(numAlerts), HuamiIcon.PENGUIN_1};
} }
return new byte[]{BLETypeConversions.fromUint8(AlertCategory.CustomHuami.getId()), BLETypeConversions.fromUint8(numAlerts), customIconId};
} }
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.SMS.getId()), BLETypeConversions.fromUint8(numAlerts)}; return new byte[] { BLETypeConversions.fromUint8(AlertCategory.SMS.getId()), BLETypeConversions.fromUint8(numAlerts)};
} }

View File

@ -597,7 +597,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
} }
String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()).trim(); String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()).trim();
String origin = notificationSpec.type.getGenericType(); String origin = notificationSpec.type.getGenericType();
SimpleNotification simpleNotification = new SimpleNotification(message, BLETypeConversions.toAlertCategory(notificationSpec.type)); SimpleNotification simpleNotification = new SimpleNotification(message, BLETypeConversions.toAlertCategory(notificationSpec.type), notificationSpec.type);
performPreferredNotification(origin + " received", origin, simpleNotification, alertLevel, null); performPreferredNotification(origin + " received", origin, simpleNotification, alertLevel, null);
} }
@ -610,7 +610,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
} }
}; };
String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()); String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext());
SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.HighPriorityAlert); SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.HighPriorityAlert, notificationSpec.type);
performPreferredNotification("alarm clock ringing", MiBandConst.ORIGIN_ALARM_CLOCK, simpleNotification, MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY, abortAction); performPreferredNotification("alarm clock ringing", MiBandConst.ORIGIN_ALARM_CLOCK, simpleNotification, MiBand2Service.ALERT_LEVEL_VIBRATE_ONLY, abortAction);
} }
@ -643,7 +643,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
} }
}; };
String message = NotificationUtils.getPreferredTextFor(callSpec); String message = NotificationUtils.getPreferredTextFor(callSpec);
SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.IncomingCall); SimpleNotification simpleNotification = new SimpleNotification(message, AlertCategory.IncomingCall, null);
performPreferredNotification("incoming call", MiBandConst.ORIGIN_INCOMING_CALL, simpleNotification, MiBand2Service.ALERT_LEVEL_PHONE_CALL, abortAction); performPreferredNotification("incoming call", MiBandConst.ORIGIN_INCOMING_CALL, simpleNotification, MiBand2Service.ALERT_LEVEL_PHONE_CALL, abortAction);
} else if ((callSpec.command == CallSpec.CALL_START) || (callSpec.command == CallSpec.CALL_END)) { } else if ((callSpec.command == CallSpec.CALL_START) || (callSpec.command == CallSpec.CALL_END)) {
telephoneRinging = false; telephoneRinging = false;
@ -742,7 +742,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
return !isLocatingDevice; return !isLocatingDevice;
} }
}; };
SimpleNotification simpleNotification = new SimpleNotification(getContext().getString(R.string.find_device_you_found_it), AlertCategory.HighPriorityAlert); SimpleNotification simpleNotification = new SimpleNotification(getContext().getString(R.string.find_device_you_found_it), AlertCategory.HighPriorityAlert, null);
performDefaultNotification("locating device", simpleNotification, (short) 255, abortAction); performDefaultNotification("locating device", simpleNotification, (short) 255, abortAction);
} }
} }

View File

@ -34,14 +34,22 @@ public class NotificationUtils {
return formatText(notificationSpec.sender, notificationSpec.subject, notificationSpec.body, lengthBody, lengthSubject, context); return formatText(notificationSpec.sender, notificationSpec.subject, notificationSpec.body, lengthBody, lengthSubject, context);
case GENERIC_NAVIGATION: case GENERIC_NAVIGATION:
return StringUtils.getFirstOf(notificationSpec.title, notificationSpec.body); return StringUtils.getFirstOf(notificationSpec.title, notificationSpec.body);
case CONVERSATIONS:
case FACEBOOK_MESSENGER:
case GOOGLE_MESSENGER:
case GOOGLE_HANGOUTS:
case HIPCHAT:
case KAKAO_TALK:
case LINE:
case RIOT: case RIOT:
case SIGNAL: case SIGNAL:
case SKYPE:
case SNAPCHAT:
case TELEGRAM: case TELEGRAM:
case TWITTER: case TWITTER:
case WHATSAPP: case WHATSAPP:
case CONVERSATIONS: case VIBER:
case FACEBOOK: case WECHAT:
case FACEBOOK_MESSENGER:
return StringUtils.ensureNotNull(notificationSpec.body); return StringUtils.ensureNotNull(notificationSpec.body);
} }
return ""; return "";