diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java index 942972cb7..11149bd6f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java @@ -288,4 +288,47 @@ public class DaFitConstants { throw new IllegalArgumentException(e); } } + + // The notification types used by CMD_SEND_MESSAGE + public static final byte NOTIFICATION_TYPE_CALL_OFF_HOOK = -1; + public static final byte NOTIFICATION_TYPE_CALL = 0; + public static final byte NOTIFICATION_TYPE_MESSAGE_SMS = 1; + public static final byte NOTIFICATION_TYPE_MESSAGE_WECHAT = 2; + public static final byte NOTIFICATION_TYPE_MESSAGE_QQ = 3; + public static final byte NOTIFICATION_TYPE_MESSAGE_FACEBOOK = 4; + public static final byte NOTIFICATION_TYPE_MESSAGE_TWITTER = 5; + public static final byte NOTIFICATION_TYPE_MESSAGE_INSTAGRAM = 6; + public static final byte NOTIFICATION_TYPE_MESSAGE_SKYPE = 7; + public static final byte NOTIFICATION_TYPE_MESSAGE_WHATSAPP = 8; + public static final byte NOTIFICATION_TYPE_MESSAGE_LINE = 9; + public static final byte NOTIFICATION_TYPE_MESSAGE_KAKAO = 10; + public static final byte NOTIFICATION_TYPE_MESSAGE_OTHER = 11; + + public static byte notificationType(NotificationType type) + { + switch(type) + { + case FACEBOOK: + case FACEBOOK_MESSENGER: + return NOTIFICATION_TYPE_MESSAGE_FACEBOOK; + case GENERIC_SMS: + return NOTIFICATION_TYPE_MESSAGE_SMS; + case INSTAGRAM: + return NOTIFICATION_TYPE_MESSAGE_INSTAGRAM; + case KAKAO_TALK: + return NOTIFICATION_TYPE_MESSAGE_KAKAO; + case LINE: + return NOTIFICATION_TYPE_MESSAGE_LINE; + case SKYPE: + return NOTIFICATION_TYPE_MESSAGE_SKYPE; + case TWITTER: + return NOTIFICATION_TYPE_MESSAGE_TWITTER; + case WECHAT: + return NOTIFICATION_TYPE_MESSAGE_WECHAT; + case WHATSAPP: + return NOTIFICATION_TYPE_MESSAGE_WHATSAPP; + default: + return NOTIFICATION_TYPE_MESSAGE_OTHER; + } + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java index bc313a00b..aee99e600 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java @@ -35,6 +35,8 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.Logging; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.devices.dafit.DaFitConstants; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; @@ -55,6 +57,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.Batter import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.BatteryInfoProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; +import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; // TODO: figure out the training data @@ -158,7 +162,46 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport { private boolean handlePacket(byte packetType, byte[] payload) { - LOG.warn("Got packet " + packetType + ": " + Logging.formatBytes(payload)); + if (packetType == DaFitConstants.CMD_NOTIFY_PHONE_OPERATION) + { + byte operation = payload[0]; + if (operation == DaFitConstants.ARG_OPERATION_PLAY_PAUSE) + { + GBDeviceEventMusicControl musicCmd = new GBDeviceEventMusicControl(); + musicCmd.event = GBDeviceEventMusicControl.Event.PLAYPAUSE; + evaluateGBDeviceEvent(musicCmd); + return true; + } + if (operation == DaFitConstants.ARG_OPERATION_PREV_SONG) + { + GBDeviceEventMusicControl musicCmd = new GBDeviceEventMusicControl(); + musicCmd.event = GBDeviceEventMusicControl.Event.PREVIOUS; + evaluateGBDeviceEvent(musicCmd); + return true; + } + if (operation == DaFitConstants.ARG_OPERATION_NEXT_SONG) + { + GBDeviceEventMusicControl musicCmd = new GBDeviceEventMusicControl(); + musicCmd.event = GBDeviceEventMusicControl.Event.NEXT; + evaluateGBDeviceEvent(musicCmd); + return true; + } + if (operation == DaFitConstants.ARG_OPERATION_DROP_INCOMING_CALL) + { + GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl(); + callCmd.event = GBDeviceEventCallControl.Event.REJECT; + evaluateGBDeviceEvent(callCmd); + return true; + } + } + + if (packetType == DaFitConstants.CMD_SWITCH_CAMERA_VIEW) + { + // TODO: trigger camera photo + return true; + } + + LOG.warn("Unhandled packet " + packetType + ": " + Logging.formatBytes(payload)); return false; } @@ -180,9 +223,33 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport { return true; } + private void sendNotification(byte type, String text) + { + try { + TransactionBuilder builder = performInitialized("sendNotification"); + byte[] str = text.getBytes(); + byte[] payload = new byte[str.length + 1]; + payload[0] = type; + System.arraycopy(str, 0, payload, 1, str.length); + sendPacket(builder, DaFitPacketOut.buildPacket(DaFitConstants.CMD_SEND_MESSAGE, payload)); + builder.queue(getQueue()); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override public void onNotification(NotificationSpec notificationSpec) { - // TODO + String sender = StringUtils.getFirstOf(notificationSpec.sender, StringUtils.getFirstOf(notificationSpec.sourceName, notificationSpec.sourceAppId)); + if (sender.isEmpty()) + sender = "(unknown)"; + + String text = NotificationUtils.getPreferredTextFor(notificationSpec, 0, 75, getContext()); + if (text.isEmpty()) + text = StringUtils.getFirstOf(StringUtils.getFirstOf(notificationSpec.title, notificationSpec.subject), notificationSpec.body); + + // The notification is split at first : into sender and text + sendNotification(DaFitConstants.notificationType(notificationSpec.type), sender + ":" + text); } @Override @@ -192,7 +259,10 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport { @Override public void onSetCallState(CallSpec callSpec) { - // TODO + if (callSpec.command == CallSpec.CALL_INCOMING) + sendNotification(DaFitConstants.NOTIFICATION_TYPE_CALL, NotificationUtils.getPreferredTextFor(callSpec)); + else + sendNotification(DaFitConstants.NOTIFICATION_TYPE_CALL_OFF_HOOK, ""); } @Override