Search in sources :

Example 16 with Alarm

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

the class FitProDeviceSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    LOG.debug("FitPro set alarms");
    if (alarms.toArray().length == 1 && alarms.get(0).getRepetition() == 0) {
        Alarm oneshot = alarms.get(0);
        DBHandler db = null;
        try {
            db = GBApplication.acquireDB();
            DaoSession daoSession = db.getDaoSession();
            Device device = DBHelper.getDevice(gbDevice, daoSession);
            User user = DBHelper.getUser(daoSession);
            nodomain.freeyourgadget.gadgetbridge.entities.Alarm tmpAlarm = new nodomain.freeyourgadget.gadgetbridge.entities.Alarm(device.getId(), user.getId(), 7, true, false, false, 0, oneshot.getHour(), oneshot.getMinute(), // kind of indicate the specialty of this alarm
            true, "", "");
            daoSession.insertOrReplace(tmpAlarm);
            GBApplication.releaseDB();
        } catch (GBException e) {
            LOG.error("error storing one shot quick alarm");
        }
    }
    try {
        TransactionBuilder builder = performInitialized("Set alarm");
        boolean anyAlarmEnabled = false;
        byte[] all_alarms = new byte[] {};
        for (Alarm alarm : alarms) {
            Calendar calendar = AlarmUtils.toCalendar(alarm);
            anyAlarmEnabled |= alarm.getEnabled();
            LOG.debug("alarms: " + alarm.getPosition());
            int maxAlarms = 8;
            if (alarm.getPosition() >= maxAlarms) {
                // we should never encounter this, but just in case
                if (alarm.getEnabled()) {
                    GB.toast(getContext(), "Only 8 alarms are supported.", Toast.LENGTH_LONG, GB.WARN);
                }
                return;
            }
            if (alarm.getEnabled()) {
                long datetime = (long) alarm.getRepetition() | ((long) (calendar.get(Calendar.YEAR) - 2000) << 34 | (long) (calendar.get(Calendar.MONTH) + 1) << 30 | (long) (calendar.get(Calendar.DAY_OF_MONTH)) << 25 | (long) (calendar.get(Calendar.HOUR_OF_DAY)) << 20 | (long) (calendar.get(Calendar.MINUTE)) << 14 | 1L << 11);
                byte[] single_alarm = new byte[] { (byte) (datetime >> 32), (byte) (datetime >> 24), (byte) (datetime >> 16), (byte) (datetime >> 8), (byte) (datetime) };
                all_alarms = ArrayUtils.addAll(all_alarms, single_alarm);
            }
        }
        builder.write(writeCharacteristic, craftData(CMD_GROUP_GENERAL, CMD_ALARM, all_alarms));
        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 : User(nodomain.freeyourgadget.gadgetbridge.entities.User) ActivityUser(nodomain.freeyourgadget.gadgetbridge.model.ActivityUser) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) IOException(java.io.IOException) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 17 with Alarm

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

the class BangleJSDeviceSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        JSONObject o = new JSONObject();
        o.put("t", "alarm");
        JSONArray jsonalarms = new JSONArray();
        o.put("d", jsonalarms);
        for (Alarm alarm : alarms) {
            if (!alarm.getEnabled())
                continue;
            JSONObject jsonalarm = new JSONObject();
            jsonalarms.put(jsonalarm);
            Calendar calendar = AlarmUtils.toCalendar(alarm);
            // TODO: getRepetition to ensure it only happens on correct day?
            jsonalarm.put("h", alarm.getHour());
            jsonalarm.put("m", alarm.getMinute());
        }
        uartTxJSON("onSetAlarms", o);
    } catch (JSONException e) {
        LOG.info("JSONException: " + e.getLocalizedMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 18 with Alarm

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

the class HPlusSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        TransactionBuilder builder = performInitialized("alarm");
        for (Alarm alarm : alarms) {
            if (!alarm.getEnabled())
                continue;
            if (// Not available
            alarm.getSmartWakeup())
                continue;
            Calendar t = AlarmUtils.toCalendar(alarm);
            setAlarm(builder, t);
            builder.queue(getQueue());
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_ok), Toast.LENGTH_SHORT, GB.INFO);
            // Only first alarm
            return;
        }
        setAlarm(builder, null);
        builder.queue(getQueue());
        GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
    } catch (Exception e) {
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 19 with Alarm

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

the class HuamiSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        TransactionBuilder builder = performInitialized("Set alarm");
        boolean anyAlarmEnabled = false;
        for (Alarm alarm : alarms) {
            anyAlarmEnabled |= alarm.getEnabled();
            queueAlarm(alarm, builder);
        }
        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) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 20 with Alarm

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

the class Watch9DeviceSupport method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        TransactionBuilder builder = performInitialized("setAlarms");
        for (Alarm alarm : alarms) {
            setAlarm(alarm, alarm.getPosition() + 1, builder);
        }
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn("Unable to set alarms", e);
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

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