mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-07-30 18:44:11 +02:00
Small refactoring for initial support for Mi2 Alarms #323
This commit is contained in:
parent
04673923b6
commit
c87d08bf4b
|
@ -17,7 +17,6 @@ import java.util.GregorianCalendar;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
|
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,17 +76,18 @@ public class SleepAlarmWidget extends AppWidgetProvider {
|
||||||
// add preferred sleep duration
|
// add preferred sleep duration
|
||||||
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
|
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
|
||||||
|
|
||||||
int hours = calendar.get(calendar.HOUR_OF_DAY);
|
|
||||||
int minutes = calendar.get(calendar.MINUTE);
|
|
||||||
|
|
||||||
// overwrite the first alarm and activate it
|
// overwrite the first alarm and activate it
|
||||||
GBAlarm alarm = new GBAlarm(0, true, true, Alarm.ALARM_ONCE, hours, minutes);
|
GBAlarm alarm = GBAlarm.createSingleShot(0, true, calendar);
|
||||||
alarm.store();
|
alarm.store();
|
||||||
|
|
||||||
if (GBApplication.isRunningLollipopOrLater()) {
|
if (GBApplication.isRunningLollipopOrLater()) {
|
||||||
setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
|
setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int hours = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
|
int minutes = calendar.get(Calendar.MINUTE);
|
||||||
|
|
||||||
GB.toast(context,
|
GB.toast(context,
|
||||||
String.format(context.getString(R.string.appwidget_alarms_set), hours, minutes),
|
String.format(context.getString(R.string.appwidget_alarms_set), hours, minutes),
|
||||||
Toast.LENGTH_SHORT, GB.INFO);
|
Toast.LENGTH_SHORT, GB.INFO);
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class MiBand2Coordinator extends MiBandCoordinator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsAlarmConfiguration() {
|
public boolean supportsAlarmConfiguration() {
|
||||||
return false; // not yet
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class MiBand2Service {
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC1 = UUID.fromString("00000001-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC1 = UUID.fromString("00000001-0000-3512-2118-0009af100700");
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC2 = UUID.fromString("00000002-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC2 = UUID.fromString("00000002-0000-3512-2118-0009af100700");
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC3 = UUID.fromString("00000003-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC3 = UUID.fromString("00000003-0000-3512-2118-0009af100700");
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC4 = UUID.fromString("00000004-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC4 = UUID.fromString("00000004-0000-3512-2118-0009af100700"); // Alarm related
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC5 = UUID.fromString("00000005-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC5 = UUID.fromString("00000005-0000-3512-2118-0009af100700");
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC6 = UUID.fromString("00000006-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC6 = UUID.fromString("00000006-0000-3512-2118-0009af100700");
|
||||||
public static final UUID UUID_UNKNOWN_CHARACTERISTIC7 = UUID.fromString("00000007-0000-3512-2118-0009af100700");
|
public static final UUID UUID_UNKNOWN_CHARACTERISTIC7 = UUID.fromString("00000007-0000-3512-2118-0009af100700");
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class GBAlarm implements Alarm {
|
||||||
public static final String[] DEFAULT_ALARMS = {"2,false,false,0,15,30", "1,false,false,96,8,0", "0,false,true,31,7,30"};
|
public static final String[] DEFAULT_ALARMS = {"2,false,false,0,15,30", "1,false,false,96,8,0", "0,false,true,31,7,30"};
|
||||||
|
|
||||||
|
|
||||||
public GBAlarm(int index, boolean enabled, boolean smartWakeup, byte repetition, int hour, int minute) {
|
public GBAlarm(int index, boolean enabled, boolean smartWakeup, int repetition, int hour, int minute) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
this.smartWakeup = smartWakeup;
|
this.smartWakeup = smartWakeup;
|
||||||
|
@ -47,6 +47,10 @@ public class GBAlarm implements Alarm {
|
||||||
this.minute = Integer.parseInt(tokens[5]);
|
this.minute = Integer.parseInt(tokens[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static GBAlarm createSingleShot(int index, boolean smartWakeup, Calendar calendar) {
|
||||||
|
return new GBAlarm(index, true, smartWakeup, Alarm.ALARM_ONCE, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
|
||||||
|
}
|
||||||
|
|
||||||
private static GBAlarm readFromParcel(Parcel pc) {
|
private static GBAlarm readFromParcel(Parcel pc) {
|
||||||
int index = pc.readInt();
|
int index = pc.readInt();
|
||||||
boolean enabled = Boolean.parseBoolean(pc.readString());
|
boolean enabled = Boolean.parseBoolean(pc.readString());
|
||||||
|
@ -54,7 +58,7 @@ public class GBAlarm implements Alarm {
|
||||||
int repetition = pc.readInt();
|
int repetition = pc.readInt();
|
||||||
int hour = pc.readInt();
|
int hour = pc.readInt();
|
||||||
int minute = pc.readInt();
|
int minute = pc.readInt();
|
||||||
return new GBAlarm(index, enabled, smartWakeup, (byte) repetition, hour, minute);
|
return new GBAlarm(index, enabled, smartWakeup, repetition, hour, minute);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -33,6 +33,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandFWHelper;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandFWHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
||||||
|
@ -226,6 +227,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
|
||||||
builder.notify(getCharacteristic(GattService.UUID_SERVICE_CURRENT_TIME), enable);
|
builder.notify(getCharacteristic(GattService.UUID_SERVICE_CURRENT_TIME), enable);
|
||||||
// Notify CHARACTERISTIC9 to receive random auth code
|
// Notify CHARACTERISTIC9 to receive random auth code
|
||||||
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_AUTH), enable);
|
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_AUTH), enable);
|
||||||
|
builder.notify(getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC4), enable);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -532,7 +534,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
|
||||||
@Override
|
@Override
|
||||||
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
||||||
try {
|
try {
|
||||||
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
|
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC4);
|
||||||
TransactionBuilder builder = performInitialized("Set alarm");
|
TransactionBuilder builder = performInitialized("Set alarm");
|
||||||
boolean anyAlarmEnabled = false;
|
boolean anyAlarmEnabled = false;
|
||||||
for (Alarm alarm : alarms) {
|
for (Alarm alarm : alarms) {
|
||||||
|
@ -1054,6 +1056,8 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void queueAlarm(Calendar calender, boolean enabled, boolean smartWakeup, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Convert an alarm from the GB internal structure to a Mi Band message and put on the specified
|
* Convert an alarm from the GB internal structure to a Mi Band message and put on the specified
|
||||||
* builder queue as a write message for the passed characteristic
|
* builder queue as a write message for the passed characteristic
|
||||||
|
@ -1066,16 +1070,16 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
|
||||||
byte[] alarmCalBytes = MiBandDateConverter.calendarToRawBytes(alarm.getAlarmCal());
|
byte[] alarmCalBytes = MiBandDateConverter.calendarToRawBytes(alarm.getAlarmCal());
|
||||||
|
|
||||||
byte[] alarmMessage = new byte[]{
|
byte[] alarmMessage = new byte[]{
|
||||||
MiBandService.COMMAND_SET_TIMER,
|
// MiBandService.COMMAND_SET_TIMER,
|
||||||
(byte) alarm.getIndex(),
|
|
||||||
(byte) (alarm.isEnabled() ? 1 : 0),
|
(byte) (alarm.isEnabled() ? 1 : 0),
|
||||||
|
(byte) alarm.getIndex(),
|
||||||
alarmCalBytes[0],
|
alarmCalBytes[0],
|
||||||
alarmCalBytes[1],
|
alarmCalBytes[1],
|
||||||
alarmCalBytes[2],
|
alarmCalBytes[2],
|
||||||
alarmCalBytes[3],
|
alarmCalBytes[3],
|
||||||
alarmCalBytes[4],
|
alarmCalBytes[4],
|
||||||
alarmCalBytes[5],
|
alarmCalBytes[5],
|
||||||
(byte) (alarm.isSmartWakeup() ? 30 : 0),
|
(byte) (alarm.isSmartWakeup() ? 38 : 0),
|
||||||
(byte) alarm.getRepetitionMask()
|
(byte) alarm.getRepetitionMask()
|
||||||
};
|
};
|
||||||
builder.write(characteristic, alarmMessage);
|
builder.write(characteristic, alarmMessage);
|
||||||
|
@ -1181,22 +1185,8 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
|
||||||
int slotToUse = 2 - iteration;
|
int slotToUse = 2 - iteration;
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
calendar.setTimeInMillis(mEvt.getBegin());
|
calendar.setTimeInMillis(mEvt.getBegin());
|
||||||
byte[] calBytes = MiBandDateConverter.calendarToRawBytes(calendar);
|
Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
|
||||||
|
queueAlarm(alarm, builder, characteristic);
|
||||||
byte[] alarmMessage = new byte[]{
|
|
||||||
MiBandService.COMMAND_SET_TIMER,
|
|
||||||
(byte) slotToUse,
|
|
||||||
(byte) 1,
|
|
||||||
calBytes[0],
|
|
||||||
calBytes[1],
|
|
||||||
calBytes[2],
|
|
||||||
calBytes[3],
|
|
||||||
calBytes[4],
|
|
||||||
calBytes[5],
|
|
||||||
(byte) 0,
|
|
||||||
(byte) 0
|
|
||||||
};
|
|
||||||
builder.write(characteristic, alarmMessage);
|
|
||||||
iteration++;
|
iteration++;
|
||||||
}
|
}
|
||||||
builder.queue(getQueue());
|
builder.queue(getQueue());
|
||||||
|
|
|
@ -37,6 +37,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
|
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
||||||
|
@ -1193,22 +1194,8 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
int slotToUse = 2 - iteration;
|
int slotToUse = 2 - iteration;
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
calendar.setTimeInMillis(mEvt.getBegin());
|
calendar.setTimeInMillis(mEvt.getBegin());
|
||||||
byte[] calBytes = MiBandDateConverter.calendarToRawBytes(calendar);
|
Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
|
||||||
|
queueAlarm(alarm, builder, characteristic);
|
||||||
byte[] alarmMessage = new byte[]{
|
|
||||||
MiBandService.COMMAND_SET_TIMER,
|
|
||||||
(byte) slotToUse,
|
|
||||||
(byte) 1,
|
|
||||||
calBytes[0],
|
|
||||||
calBytes[1],
|
|
||||||
calBytes[2],
|
|
||||||
calBytes[3],
|
|
||||||
calBytes[4],
|
|
||||||
calBytes[5],
|
|
||||||
(byte) 0,
|
|
||||||
(byte) 0
|
|
||||||
};
|
|
||||||
builder.write(characteristic, alarmMessage);
|
|
||||||
iteration++;
|
iteration++;
|
||||||
}
|
}
|
||||||
builder.queue(getQueue());
|
builder.queue(getQueue());
|
||||||
|
|
Loading…
Reference in New Issue
Block a user