Refactor some common Huami stuff (Mi Band 2 / Bip)

This also fixes a bug where every event from the device (like not worn detection) counted as a button press
This commit is contained in:
Andreas Shimokawa 2017-10-21 22:50:28 +02:00
parent 5262c6e3d7
commit a809ad214d
11 changed files with 95 additions and 95 deletions

View File

@ -48,7 +48,7 @@ public class MiBand2Service {
public static final UUID UUID_CHARACTERISTIC_8_USER_SETTINGS = UUID.fromString("00000008-0000-3512-2118-0009af100700");
// service uuid fee1
public static final UUID UUID_CHARACTERISTIC_AUTH = UUID.fromString("00000009-0000-3512-2118-0009af100700");
public static final UUID UUID_CHARACTERISTIC_10_BUTTON = UUID.fromString("00000010-0000-3512-2118-0009af100700");
public static final UUID UUID_CHARACTERISTIC_DEVICEEVENT = UUID.fromString("00000010-0000-3512-2118-0009af100700");
public static final int ALERT_LEVEL_NONE = 0;
public static final int ALERT_LEVEL_MESSAGE = 1;

View File

@ -31,7 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWInsta
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.FirmwareType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
import nodomain.freeyourgadget.gadgetbridge.util.Version;
public class MiBand2FWInstallHandler extends AbstractMiBandFWInstallHandler {
@ -49,16 +49,16 @@ public class MiBand2FWInstallHandler extends AbstractMiBandFWInstallHandler {
}
private void maybeAddFontHint(InstallActivity installActivity) {
FirmwareType type = getFirmwareType();
if (type == FirmwareType.FIRMWARE) {
HuamiFirmwareType type = getFirmwareType();
if (type == HuamiFirmwareType.FIRMWARE) {
String newInfoText = installActivity.getInfoText() + "\n\n" + "Note: you may install Mili_pro.ft or Mili_pro.ft.en to enable text notifications.";
installActivity.setInfoText(newInfoText);
}
}
private void maybeAddFw53Hint(InstallActivity installActivity, GBDevice device) {
FirmwareType type = getFirmwareType();
if (type != FirmwareType.FIRMWARE) {
HuamiFirmwareType type = getFirmwareType();
if (type != HuamiFirmwareType.FIRMWARE) {
return;
}
@ -91,12 +91,12 @@ public class MiBand2FWInstallHandler extends AbstractMiBandFWInstallHandler {
}
}
private FirmwareType getFirmwareType() {
private HuamiFirmwareType getFirmwareType() {
AbstractMiBandFWHelper helper = getHelper();
if (helper instanceof MiBand2FWHelper) {
return ((MiBand2FWHelper) helper).getFirmwareInfo().getFirmwareType();
}
return FirmwareType.INVALID;
return HuamiFirmwareType.INVALID;
}
@Override

View File

@ -18,7 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.amazfitbip;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.FirmwareType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2FirmwareInfo;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
@ -73,24 +73,24 @@ public class AmazfitBipFirmwareInfo extends Mi2FirmwareInfo {
}
@Override
protected FirmwareType determineFirmwareType(byte[] bytes) {
protected HuamiFirmwareType determineFirmwareType(byte[] bytes) {
if (ArrayUtils.startsWith(bytes, RES_HEADER)) {
return FirmwareType.RES;
return HuamiFirmwareType.RES;
}
if (ArrayUtils.startsWith(bytes, GPS_HEADER)) {
return FirmwareType.GPS;
return HuamiFirmwareType.GPS;
}
if (ArrayUtils.startsWith(bytes, GPS_ALMANAC_HEADER)) {
return FirmwareType.GPS_ALMANAC;
return HuamiFirmwareType.GPS_ALMANAC;
}
if (ArrayUtils.startsWith(bytes, GPS_CEP_HEADER)) {
return FirmwareType.GPS_CEP;
return HuamiFirmwareType.GPS_CEP;
}
if (ArrayUtils.equals(bytes, FW_HEADER, FW_HEADER_OFFSET)) {
// TODO: this is certainly not a correct validation, but it works for now
return FirmwareType.FIRMWARE;
return HuamiFirmwareType.FIRMWARE;
}
return FirmwareType.INVALID;
return HuamiFirmwareType.INVALID;
}
@Override

View File

@ -31,11 +31,10 @@ import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.devices.amazfitbip.AmazfitBipService;
import nodomain.freeyourgadget.gadgetbridge.devices.amazfitbip.AmazfitBipWeatherConditions;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
import nodomain.freeyourgadget.gadgetbridge.devices.miband2.MiBand2Icon;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
@ -87,7 +86,7 @@ public class AmazfitBipSupport extends MiBand2Support {
AlertNotificationProfile<?> profile = new AlertNotificationProfile(this);
profile.setMaxLength(230);
byte customIconId = MiBand2Icon.mapToIconId(notificationSpec.type);
byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type);
AlertCategory alertCategory = AlertCategory.CustomMiBand2;
@ -96,7 +95,7 @@ public class AmazfitBipSupport extends MiBand2Support {
alertCategory = AlertCategory.SMS;
}
// EMAIL icon does not work in FW 0.0.8.74, it did in 0.0.7.90
else if (customIconId == MiBand2Icon.EMAIL) {
else if (customIconId == HuamiIcon.EMAIL) {
alertCategory = AlertCategory.Email;
}
@ -117,48 +116,8 @@ public class AmazfitBipSupport extends MiBand2Support {
}
@Override
public void handleButtonPressed(byte[] value) {
if (value == null || value.length != 1) {
return;
}
GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
switch (value[0]) {
case AmazfitBipEvent.CALL_REJECT:
callCmd.event = GBDeviceEventCallControl.Event.REJECT;
evaluateGBDeviceEvent(callCmd);
break;
case AmazfitBipEvent.CALL_ACCEPT:
callCmd.event = GBDeviceEventCallControl.Event.ACCEPT;
evaluateGBDeviceEvent(callCmd);
break;
case AmazfitBipEvent.BUTTON_PRESSED:
LOG.info("button pressed");
break;
case AmazfitBipEvent.BUTTON_PRESSED_LONG:
LOG.info("button long-pressed ");
break;
case AmazfitBipEvent.START_NONWEAR:
LOG.info("non-wear start detected");
break;
case AmazfitBipEvent.ALARM_TOGGLED:
LOG.info("An alarm was toggled"); // TODO: sync alarms watch -> GB
break;
case AmazfitBipEvent.FELL_ASLEEP:
LOG.info("Fell asleep");
break;
case AmazfitBipEvent.WOKE_UP:
LOG.info("Woke up");
break;
case AmazfitBipEvent.STEPSGOAL_REACHED:
LOG.info("Steps goal reached");
break;
case AmazfitBipEvent.TICK_30MIN:
LOG.info("Tick 30 min (?)");
break;
default:
LOG.warn("unhandled event " + value[0]);
}
public void handleButtonEvent() {
// ignore
}
@Override

View File

@ -14,10 +14,10 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.amazfitbip;
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami;
public class AmazfitBipEvent {
public class HuamiDeviceEvent {
public static final byte FELL_ASLEEP = 0x01;
public static final byte WOKE_UP = 0x02;
public static final byte STEPSGOAL_REACHED = 0x03;

View File

@ -14,12 +14,11 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband2;
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami;
public enum FirmwareType {
public enum HuamiFirmwareType {
FIRMWARE((byte) 0),
FONT((byte) 1),
// Amazfit Bip only from here on
RES((byte) 2),
GPS((byte) 3),
GPS_CEP((byte) 4),
@ -28,7 +27,7 @@ public enum FirmwareType {
private final byte value;
FirmwareType(byte value) {
HuamiFirmwareType(byte value) {
this.value = value;
}

View File

@ -15,12 +15,12 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.miband2;
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
public class MiBand2Icon {
public class HuamiIcon {
// icons which are unsure which app they are for are suffixed with _NN
public static final byte WECHAT = 0;
public static final byte PENGUIN_1 = 1;

View File

@ -21,6 +21,7 @@ import java.util.Map;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
@ -67,7 +68,7 @@ public class Mi2FirmwareInfo {
crcToVersion.put(6377, "Font (En)");
}
private FirmwareType firmwareType = FirmwareType.FIRMWARE;
private HuamiFirmwareType firmwareType = HuamiFirmwareType.FIRMWARE;
public static String toVersion(int crc16) {
return crcToVersion.get(crc16);
@ -89,15 +90,15 @@ public class Mi2FirmwareInfo {
firmwareType = determineFirmwareType(bytes);
}
protected FirmwareType determineFirmwareType(byte[] bytes) {
protected HuamiFirmwareType determineFirmwareType(byte[] bytes) {
if (ArrayUtils.startsWith(bytes, FT_HEADER)) {
return FirmwareType.FONT;
return HuamiFirmwareType.FONT;
}
if (ArrayUtils.equals(bytes, FW_HEADER, FW_HEADER_OFFSET)) {
// TODO: this is certainly not a correct validation, but it works for now
return FirmwareType.FIRMWARE;
return HuamiFirmwareType.FIRMWARE;
}
return FirmwareType.INVALID;
return HuamiFirmwareType.INVALID;
}
public boolean isGenerallyCompatibleWith(GBDevice device) {
@ -105,7 +106,7 @@ public class Mi2FirmwareInfo {
}
public boolean isHeaderValid() {
return getFirmwareType() != FirmwareType.INVALID;
return getFirmwareType() != HuamiFirmwareType.INVALID;
}
public void checkValid() throws IllegalArgumentException {
@ -131,7 +132,7 @@ public class Mi2FirmwareInfo {
return getCrc16(); // HACK until we know how to determine the version from the fw bytes
}
public FirmwareType getFirmwareType() {
public HuamiFirmwareType getFirmwareType() {
return firmwareType;
}
}

View File

@ -19,9 +19,8 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miband2;
import android.bluetooth.BluetoothGattCharacteristic;
import android.support.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
import nodomain.freeyourgadget.gadgetbridge.devices.miband2.MiBand2Icon;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic;
@ -69,9 +68,9 @@ public class Mi2TextNotificationStrategy extends Mi2NotificationStrategy {
case Email:
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.Email.getId()), BLETypeConversions.fromUint8(numAlerts)};
case InstantMessage:
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.CustomMiBand2.getId()), BLETypeConversions.fromUint8(numAlerts), MiBand2Icon.WECHAT};
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), MiBand2Icon.PENGUIN_1};
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.CustomMiBand2.getId()), BLETypeConversions.fromUint8(numAlerts), HuamiIcon.PENGUIN_1};
}
}
return new byte[] { BLETypeConversions.fromUint8(AlertCategory.SMS.getId()), BLETypeConversions.fromUint8(numAlerts)};

View File

@ -52,6 +52,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.DateTimeDisplay;
@ -94,6 +95,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotificat
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.heartrate.HeartRateProfile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.RealtimeSamplesSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.actions.StopNotificationAction;
@ -263,7 +265,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
public MiBand2Support enableFurtherNotifications(TransactionBuilder builder, boolean enable) {
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), enable);
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO), enable);
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON), enable);
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_DEVICEEVENT), enable);
BluetoothGattCharacteristic heartrateCharacteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
if (heartrateCharacteristic != null) {
builder.notify(heartrateCharacteristic, enable);
@ -876,8 +878,52 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
currentButtonPressTime = System.currentTimeMillis();
}
public void handleButtonPressed(byte[] value) {
LOG.info("Button pressed");
public void handleDeviceEvent(byte[] value) {
if (value == null || value.length != 1) {
return;
}
GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
switch (value[0]) {
case HuamiDeviceEvent.CALL_REJECT:
callCmd.event = GBDeviceEventCallControl.Event.REJECT;
evaluateGBDeviceEvent(callCmd);
break;
case HuamiDeviceEvent.CALL_ACCEPT:
callCmd.event = GBDeviceEventCallControl.Event.ACCEPT;
evaluateGBDeviceEvent(callCmd);
break;
case HuamiDeviceEvent.BUTTON_PRESSED:
LOG.info("button pressed");
handleButtonEvent();
break;
case HuamiDeviceEvent.BUTTON_PRESSED_LONG:
LOG.info("button long-pressed ");
break;
case HuamiDeviceEvent.START_NONWEAR:
LOG.info("non-wear start detected");
break;
case HuamiDeviceEvent.ALARM_TOGGLED:
LOG.info("An alarm was toggled");
break;
case HuamiDeviceEvent.FELL_ASLEEP:
LOG.info("Fell asleep");
break;
case HuamiDeviceEvent.WOKE_UP:
LOG.info("Woke up");
break;
case HuamiDeviceEvent.STEPSGOAL_REACHED:
LOG.info("Steps goal reached");
break;
case HuamiDeviceEvent.TICK_30MIN:
LOG.info("Tick 30 min (?)");
break;
default:
LOG.warn("unhandled event " + value[0]);
}
}
public void handleButtonEvent() {
///logMessageContent(value);
// If disabled we return from function immediately
@ -944,8 +990,8 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
LOG.info("AUTHENTICATION?? " + characteristicUUID);
logMessageContent(characteristic.getValue());
return true;
} else if (MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON.equals(characteristicUUID)) {
handleButtonPressed(characteristic.getValue());
} else if (MiBand2Service.UUID_CHARACTERISTIC_DEVICEEVENT.equals(characteristicUUID)) {
handleDeviceEvent(characteristic.getValue());
return true;
} else if (MiBand2Service.UUID_CHARACTERISTIC_7_REALTIME_STEPS.equals(characteristicUUID)) {
handleRealtimeSteps(characteristic.getValue());
@ -958,10 +1004,6 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
return false;
}
private void handleUnknownCharacteristic(byte[] value) {
}
@Override
public boolean onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
@ -980,8 +1022,8 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
} else if (MiBand2Service.UUID_CHARACTERISTIC_7_REALTIME_STEPS.equals(characteristicUUID)) {
handleRealtimeSteps(characteristic.getValue());
return true;
} else if (MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON.equals(characteristicUUID)) {
handleButtonPressed(characteristic.getValue());
} else if (MiBand2Service.UUID_CHARACTERISTIC_DEVICEEVENT.equals(characteristicUUID)) {
handleDeviceEvent(characteristic.getValue());
return true;
} else {
LOG.info("Unhandled characteristic read: " + characteristicUUID);

View File

@ -39,7 +39,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.AbstractMiBand2Operation;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.FirmwareType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2FirmwareInfo;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -146,7 +146,7 @@ public class UpdateFirmwareOperation extends AbstractMiBand2Operation {
break;
}
case MiBand2Service.COMMAND_FIRMWARE_CHECKSUM: {
if (getFirmwareInfo().getFirmwareType() == FirmwareType.FIRMWARE) {
if (getFirmwareInfo().getFirmwareType() == HuamiFirmwareType.FIRMWARE) {
TransactionBuilder builder = performInitialized("reboot");
getSupport().sendReboot(builder);
builder.queue(getQueue());
@ -196,7 +196,7 @@ public class UpdateFirmwareOperation extends AbstractMiBand2Operation {
int fwSize = getFirmwareInfo().getSize();
byte[] sizeBytes = BLETypeConversions.fromUint24(fwSize);
int arraySize = 4;
boolean isFirmwareCode = getFirmwareInfo().getFirmwareType() == FirmwareType.FIRMWARE;
boolean isFirmwareCode = getFirmwareInfo().getFirmwareType() == HuamiFirmwareType.FIRMWARE;
if (!isFirmwareCode) {
arraySize++;
}