use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.
the class CasioGBX100DeviceSupport method onSetAlarms.
@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
byte[] data1 = new byte[5];
byte[] data2 = new byte[17];
if (!isConnected())
return;
data1[0] = CasioConstants.characteristicToByte.get("CASIO_SETTING_FOR_ALM");
data2[0] = CasioConstants.characteristicToByte.get("CASIO_SETTING_FOR_ALM2");
for (int i = 0; i < alarms.size(); i++) {
byte[] settings = new byte[4];
Alarm alm = alarms.get(i);
if (alm.getEnabled()) {
settings[0] = 0x40;
} else {
settings[0] = 0;
}
settings[1] = 0x40;
settings[2] = (byte) alm.getHour();
settings[3] = (byte) alm.getMinute();
if (i == 0) {
System.arraycopy(settings, 0, data1, 1, settings.length);
} else {
System.arraycopy(settings, 0, data2, 1 + (i - 1) * 4, settings.length);
}
}
try {
TransactionBuilder builder = performInitialized("setAlarm");
writeAllFeatures(builder, data1);
writeAllFeatures(builder, data2);
builder.queue(getQueue());
} catch (IOException e) {
LOG.error("Error setting alarm: " + e.getMessage());
}
}
use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.
the class SetAlarmOperation method onCharacteristicRead.
@Override
public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
UUID characteristicUUID = characteristic.getUuid();
byte[] data = characteristic.getValue();
if (data.length == 0)
return true;
if (characteristicUUID.equals(CasioConstants.CASIO_SETTING_FOR_ALM_CHARACTERISTIC_UUID)) {
StringBuilder str = new StringBuilder("onCharacteristicRead: Received alarm settings: ");
for (int i = 0; i < data.length; i++) {
str.append(String.format("0x%1x ", data[i]));
}
LOG.info(str.toString());
int alarmOffset = 4;
for (int i = 0; i < mAlarms.size(); i++) {
Alarm alm = mAlarms.get(i);
data[i * alarmOffset] &= ~(0x60);
if (alm.getEnabled()) {
data[i * alarmOffset] |= 0x40;
}
if (alm.getRepetition() == Alarm.ALARM_ONCE) {
data[i * alarmOffset] |= 0x20;
}
data[i * alarmOffset + 1] = 0;
data[i * alarmOffset + 2] = (byte) alm.getHour();
data[i * alarmOffset + 3] = (byte) alm.getMinute();
}
try {
TransactionBuilder builder = performInitialized("setAlarm");
builder.write(getCharacteristic(CasioConstants.CASIO_SETTING_FOR_ALM_CHARACTERISTIC_UUID), data);
builder.queue(getQueue());
} catch (IOException e) {
LOG.error("Error setting alarm: " + e.getMessage());
}
operationFinished();
} else {
return super.onCharacteristicRead(gatt, characteristic, status);
}
return true;
}
use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.
the class FossilWatchAdapter method onSetAlarms.
@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
// throw new RuntimeException("noope");
ArrayList<nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.Alarm> activeAlarms = new ArrayList<>();
for (Alarm alarm : alarms) {
if (!alarm.getEnabled())
continue;
if (alarm.getRepetition() == 0) {
activeAlarms.add(new nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.Alarm((byte) alarm.getMinute(), (byte) alarm.getHour(), alarm.getTitle(), alarm.getDescription()));
continue;
}
int repitition = alarm.getRepetition();
repitition = (repitition << 1) | ((repitition >> 6) & 1);
activeAlarms.add(new nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.Alarm((byte) alarm.getMinute(), (byte) alarm.getHour(), (byte) repitition, alarm.getTitle(), alarm.getDescription()));
}
queueWrite(new AlarmsSetRequest(activeAlarms.toArray(new nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.alarm.Alarm[0]), this) {
@Override
public void onFilePut(boolean success) {
super.onFilePut(success);
if (success)
GB.toast("successfully set alarms", Toast.LENGTH_SHORT, GB.INFO);
else
GB.toast("error setting alarms", Toast.LENGTH_SHORT, GB.INFO);
}
});
}
use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.
the class SleepAlarmWidget method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (ACTION.equals(intent.getAction())) {
int userSleepDuration = new ActivityUser().getSleepDurationGoal();
// current timestamp
GregorianCalendar calendar = new GregorianCalendar();
// add preferred sleep duration
if (userSleepDuration > 0) {
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
} else {
// probably testing
calendar.add(Calendar.MINUTE, 1);
}
// overwrite the first alarm and activate it, without
Context appContext = context.getApplicationContext();
if (appContext instanceof GBApplication) {
GBApplication gbApp = (GBApplication) appContext;
GBDevice selectedDevice = gbApp.getDeviceManager().getSelectedDevice();
if (selectedDevice == null || !selectedDevice.isInitialized()) {
GB.toast(context, context.getString(R.string.appwidget_not_connected), Toast.LENGTH_LONG, GB.WARN);
return;
}
}
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
GB.toast(context, context.getString(R.string.appwidget_setting_alarm, hours, minutes), Toast.LENGTH_SHORT, GB.INFO);
Alarm alarm = AlarmUtils.createSingleShot(0, true, false, calendar);
ArrayList<Alarm> alarms = new ArrayList<>(1);
alarms.add(alarm);
GBApplication.deviceService().onSetAlarms(alarms);
// if (GBApplication.isRunningLollipopOrLater()) {
// setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
// }
}
}
use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.
the class HuamiSupport method sendCalendarEvents.
/**
* Fetch the events from the android device calendars and set the alarms on the miband.
* @param builder
*/
private HuamiSupport sendCalendarEvents(TransactionBuilder builder) {
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
int availableSlots = prefs.getInt(PREF_RESERVER_ALARMS_CALENDAR, 0);
if (availableSlots > 0) {
CalendarEvents upcomingEvents = new CalendarEvents();
List<CalendarEvents.CalendarEvent> mEvents = upcomingEvents.getCalendarEventList(getContext());
int iteration = 0;
for (CalendarEvents.CalendarEvent mEvt : mEvents) {
if (mEvt.isAllDay()) {
continue;
}
if (iteration >= availableSlots || iteration > 2) {
break;
}
int slotToUse = 2 - iteration;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(mEvt.getBegin());
Alarm alarm = AlarmUtils.createSingleShot(slotToUse, false, true, calendar);
queueAlarm(alarm, builder);
iteration++;
}
}
return this;
}
Aggregations