Search in sources :

Example 11 with Alarm

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

the class HuamiSupport method decodeAndUpdateAlarmStatus.

private void decodeAndUpdateAlarmStatus(byte[] response, boolean withTimes) {
    List<nodomain.freeyourgadget.gadgetbridge.entities.Alarm> alarms = DBHelper.getAlarms(gbDevice);
    int maxAlarms = 10;
    // FIXME: we can rather have a full struct here probably
    boolean[] alarmsInUse = new boolean[maxAlarms];
    boolean[] alarmsEnabled = new boolean[maxAlarms];
    byte[] alarmsMinute = new byte[maxAlarms];
    byte[] alarmsHour = new byte[maxAlarms];
    byte[] alarmsRepetition = new byte[maxAlarms];
    int nr_alarms;
    byte enable_flag;
    if (withTimes) {
        nr_alarms = (response.length - 1) / 4;
        enable_flag = (byte) 0x80;
    } else {
        nr_alarms = response[8];
        enable_flag = (byte) 0x10;
    }
    for (int i = 0; i < nr_alarms; i++) {
        int offset;
        if (withTimes) {
            offset = i * 4 + 1;
        } else {
            offset = 9 + i;
        }
        byte alarm_data = response[offset];
        int index = alarm_data & 0xf;
        if (index >= maxAlarms) {
            GB.toast("Unexpected alarm index from device, ignoring: " + index, Toast.LENGTH_SHORT, GB.ERROR);
            return;
        }
        alarmsInUse[index] = true;
        boolean enabled = (alarm_data & enable_flag) == enable_flag;
        alarmsEnabled[index] = enabled;
        if (withTimes) {
            alarmsHour[index] = response[offset + 1];
            alarmsMinute[index] = response[offset + 2];
            alarmsRepetition[index] = response[offset + 3];
        }
        LOG.info("alarm " + index + " is enabled:" + enabled);
    }
    for (nodomain.freeyourgadget.gadgetbridge.entities.Alarm alarm : alarms) {
        int pos = alarm.getPosition();
        boolean enabled = alarmsEnabled[pos];
        boolean unused = !alarmsInUse[pos];
        if (alarm.getEnabled() != enabled || alarm.getUnused() != unused || (withTimes && !unused && (alarm.getHour() != alarmsHour[pos] || alarm.getMinute() != alarmsMinute[pos] || alarm.getRepetition() != alarmsRepetition[pos]))) {
            LOG.info("updating alarm index " + pos + " unused=" + unused + ", enabled=" + enabled);
            alarm.setEnabled(enabled);
            alarm.setUnused(unused);
            if (withTimes && !unused) {
                alarm.setHour(alarmsHour[pos]);
                alarm.setMinute(alarmsMinute[pos]);
                alarm.setRepetition(alarmsRepetition[pos]);
            }
            DBHelper.store(alarm);
            Intent intent = new Intent(DeviceService.ACTION_SAVE_ALARMS);
            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
        }
    }
}
Also used : Intent(android.content.Intent) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm)

Example 12 with Alarm

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

the class WaspOSDeviceSupport 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) Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 13 with Alarm

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

the class MiBandSupport method onSetAlarms.

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

Example 14 with Alarm

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

the class MiBandSupport method sendCalendarEvents.

/**
 * Fetch the events from the android device calendars and set the alarms on the miband.
 */
private void sendCalendarEvents() {
    try {
        TransactionBuilder builder = performInitialized("Send upcoming events");
        BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
        Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
        int availableSlots = prefs.getInt(DeviceSettingsPreferenceConst.PREF_RESERVER_ALARMS_CALENDAR, 0);
        if (availableSlots > 3) {
            availableSlots = 3;
        }
        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) {
                    break;
                }
                int slotToUse = 2 - iteration;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(mEvt.getBegin());
                Alarm alarm = AlarmUtils.createSingleShot(slotToUse, false, false, calendar);
                queueAlarm(alarm, builder, characteristic);
                iteration++;
            }
            builder.queue(getQueue());
        }
    } catch (IOException ex) {
        LOG.error("Unable to send Events to MI device", ex);
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) CalendarEvents(nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents) IOException(java.io.IOException) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 15 with Alarm

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

the class No1F1Support 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();
            Calendar calendar = AlarmUtils.toCalendar(alarm);
            int maxAlarms = 3;
            if (alarm.getPosition() >= maxAlarms) {
                if (alarm.getEnabled()) {
                    GB.toast(getContext(), "Only 3 alarms are supported.", Toast.LENGTH_LONG, GB.WARN);
                }
                return;
            }
            int daysMask = 0;
            if (alarm.getEnabled()) {
                daysMask = alarm.getRepetition();
                // Mask for this device starts from sunday and not from monday.
                daysMask = (daysMask / 64) + (daysMask >> 1);
            }
            byte[] alarmMessage = new byte[] { No1F1Constants.CMD_ALARM, (byte) daysMask, (byte) calendar.get(Calendar.HOUR_OF_DAY), (byte) calendar.get(Calendar.MINUTE), // vibration duration
            (byte) (alarm.getEnabled() ? 2 : 0), // vibration count
            (byte) (alarm.getEnabled() ? 10 : 0), // unknown
            (byte) (alarm.getEnabled() ? 2 : 0), (byte) 0, (byte) (alarm.getPosition() + 1) };
            builder.write(ctrlCharacteristic, alarmMessage);
        }
        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) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) 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