Search in sources :

Example 1 with Alarm

use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method sendCalendarEvents.

/**
     * Fetch the events from the android device calendars and set the alarms on the miband.
     * @param builder
     */
private MiBand2Support sendCalendarEvents(TransactionBuilder builder) {
    BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION);
    Prefs prefs = GBApplication.getPrefs();
    int availableSlots = prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_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 (iteration >= availableSlots || iteration > 2) {
                break;
            }
            int slotToUse = 2 - iteration;
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mEvt.getBegin());
            Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
            queueAlarm(alarm, builder, characteristic);
            iteration++;
        }
        builder.queue(getQueue());
    }
    return this;
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GBAlarm(nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm) CalendarEvents(nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 2 with Alarm

use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.

the class MiBand2Support method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION);
        TransactionBuilder builder = performInitialized("Set alarm");
        boolean anyAlarmEnabled = false;
        for (Alarm alarm : alarms) {
            anyAlarmEnabled |= alarm.isEnabled();
            queueAlarm(alarm, builder, characteristic);
        }
        builder.queue(getQueue());
        if (anyAlarmEnabled) {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_ok), Toast.LENGTH_SHORT, GB.INFO);
        } else {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
        }
    } catch (IOException ex) {
        GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_failed), Toast.LENGTH_LONG, GB.ERROR, ex);
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GBAlarm(nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 3 with Alarm

use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.

the class WidgetAlarmsActivity method setAlarm.

public void setAlarm(int duration) {
    // current timestamp
    GregorianCalendar calendar = new GregorianCalendar();
    if (duration > 0) {
        calendar.add(Calendar.MINUTE, duration);
    } else {
        int userSleepDuration = new ActivityUser().getSleepDurationGoal();
        // 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 = this.getApplicationContext();
    if (appContext instanceof GBApplication) {
        GBApplication gbApp = (GBApplication) appContext;
        GBDevice selectedDevice = gbApp.getDeviceManager().getSelectedDevice();
        if (selectedDevice == null || !selectedDevice.isInitialized()) {
            GB.toast(this, this.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(this, this.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);
}
Also used : Context(android.content.Context) GBApplication(nodomain.freeyourgadget.gadgetbridge.GBApplication) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GregorianCalendar(java.util.GregorianCalendar) ArrayList(java.util.ArrayList) ActivityUser(nodomain.freeyourgadget.gadgetbridge.model.ActivityUser) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 4 with Alarm

use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.

the class LefunDeviceSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    int i = 0;
    for (Alarm alarm : alarms) {
        try {
            TransactionBuilder builder = performInitialized(SetAlarmRequest.class.getSimpleName());
            SetAlarmRequest request = new SetAlarmRequest(this, builder);
            request.setIndex(i);
            request.setEnabled(alarm.getEnabled());
            request.setDayOfWeek(alarm.getRepetition());
            request.setHour(alarm.getHour());
            request.setMinute(alarm.getMinute());
            request.perform();
            inProgressRequests.add(request);
            performConnected(builder.getTransaction());
        } catch (IOException e) {
            GB.toast(getContext(), "Failed to set alarm", Toast.LENGTH_SHORT, GB.ERROR, e);
        }
        ++i;
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) SetAlarmRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetAlarmRequest)

Example 5 with Alarm

use of nodomain.freeyourgadget.gadgetbridge.model.Alarm in project Gadgetbridge by Freeyourgadget.

the class MakibesHR3DeviceSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    TransactionBuilder transactionBuilder = this.createTransactionBuilder("setalarms");
    for (int i = 0; i < alarms.size(); ++i) {
        Alarm alarm = alarms.get(i);
        byte repetition = 0x00;
        switch(alarm.getRepetition()) {
            case Alarm.ALARM_ONCE:
                repetition = MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_ONE_TIME;
                break;
            case Alarm.ALARM_MON:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_MONDAY;
            case Alarm.ALARM_TUE:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_TUESDAY;
            case Alarm.ALARM_WED:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_WEDNESDAY;
            case Alarm.ALARM_THU:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_THURSDAY;
            case Alarm.ALARM_FRI:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_FRIDAY;
            case Alarm.ALARM_SAT:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_SATURDAY;
            case Alarm.ALARM_SUN:
                repetition |= MakibesHR3Constants.ARG_SET_ALARM_REMINDER_REPEAT_SUNDAY;
                break;
            default:
                LOG.warn("invalid alarm repetition " + alarm.getRepetition());
                break;
        }
        // Should we use @alarm.getPosition() rather than @i?
        this.setAlarmReminder(transactionBuilder, i, alarm.getEnabled(), alarm.getHour(), alarm.getMinute(), repetition);
    }
    try {
        this.performConnected(transactionBuilder.getTransaction());
    } catch (Exception ex) {
        LoggerFactory.getLogger(this.getClass()).error("setalarms failed");
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Aggregations

Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)25 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)16 IOException (java.io.IOException)15 GregorianCalendar (java.util.GregorianCalendar)10 Calendar (java.util.Calendar)9 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)5 ArrayList (java.util.ArrayList)5 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)5 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)3 ActivityUser (nodomain.freeyourgadget.gadgetbridge.model.ActivityUser)3 CalendarEvents (nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents)3 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)3 Context (android.content.Context)2 UUID (java.util.UUID)2 GBAlarm (nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1