Search in sources :

Example 1 with Alarm

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

the class ConfigureAlarms method addMissingAlarms.

private void addMissingAlarms(List<Alarm> alarms) {
    DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(getGbDevice());
    int supportedNumAlarms = coordinator.getAlarmSlotCount();
    if (supportedNumAlarms > alarms.size()) {
        try (DBHandler db = GBApplication.acquireDB()) {
            DaoSession daoSession = db.getDaoSession();
            Device device = DBHelper.getDevice(getGbDevice(), daoSession);
            User user = DBHelper.getUser(daoSession);
            for (int position = 0; position < supportedNumAlarms; position++) {
                boolean found = false;
                for (Alarm alarm : alarms) {
                    if (alarm.getPosition() == position) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    LOG.info("adding missing alarm at position " + position);
                    alarms.add(position, createDefaultAlarm(device, user, position));
                }
            }
        } catch (Exception e) {
            LOG.error("Error accessing database", e);
        }
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) User(nodomain.freeyourgadget.gadgetbridge.entities.User) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Alarm(nodomain.freeyourgadget.gadgetbridge.entities.Alarm) DeviceCoordinator(nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 2 with Alarm

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

the class AlarmUtils method readAlarmsFromPrefs.

/**
 * Just for backward compatibility, do not call in new code.
 * @param gbDevice
 * @return
 * @deprecated use {@link DBHelper#getAlarms(GBDevice)} instead
 */
@NonNull
public static List<Alarm> readAlarmsFromPrefs(GBDevice gbDevice) {
    Prefs prefs = GBApplication.getPrefs();
    Set<String> stringAlarms = prefs.getStringSet(MiBandConst.PREF_MIBAND_ALARMS, new HashSet<String>());
    List<Alarm> alarms = new ArrayList<>(stringAlarms.size());
    try (DBHandler db = GBApplication.acquireDB()) {
        DaoSession daoSession = db.getDaoSession();
        User user = DBHelper.getUser(daoSession);
        Device device = DBHelper.getDevice(gbDevice, daoSession);
        for (String stringAlarm : stringAlarms) {
            alarms.add(createAlarmFromPreference(stringAlarm, device, user));
        }
        Collections.sort(alarms, AlarmUtils.createComparator());
        return alarms;
    } catch (Exception e) {
        GB.log("Error accessing database", GB.ERROR, e);
        return Collections.emptyList();
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) User(nodomain.freeyourgadget.gadgetbridge.entities.User) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) Alarm(nodomain.freeyourgadget.gadgetbridge.entities.Alarm) ArrayList(java.util.ArrayList) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession) NonNull(androidx.annotation.NonNull)

Example 3 with Alarm

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

the class DBHelper method getAlarms.

/**
 * Returns all user-configurable alarms for the given user and device. The list is sorted by
 * {@link Alarm#position}. Calendar events that may also be modeled as alarms are not stored
 * in the database and hence not returned by this method.
 * @param gbDevice the device for which the alarms shall be loaded
 * @return the list of alarms for the given device
 */
@NonNull
public static List<Alarm> getAlarms(@NonNull GBDevice gbDevice) {
    DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
    Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
    int reservedSlots = prefs.getInt(DeviceSettingsPreferenceConst.PREF_RESERVER_ALARMS_CALENDAR, 0);
    int alarmSlots = coordinator.getAlarmSlotCount();
    try (DBHandler db = GBApplication.acquireDB()) {
        DaoSession daoSession = db.getDaoSession();
        User user = getUser(daoSession);
        Device dbDevice = DBHelper.findDevice(gbDevice, daoSession);
        if (dbDevice != null) {
            AlarmDao alarmDao = daoSession.getAlarmDao();
            Long deviceId = dbDevice.getId();
            QueryBuilder<Alarm> qb = alarmDao.queryBuilder();
            qb.where(AlarmDao.Properties.UserId.eq(user.getId()), AlarmDao.Properties.DeviceId.eq(deviceId)).orderAsc(AlarmDao.Properties.Position).limit(alarmSlots - reservedSlots);
            return qb.build().list();
        }
    } catch (Exception e) {
        LOG.warn("Error reading alarms from db", e);
    }
    return Collections.emptyList();
}
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) Alarm(nodomain.freeyourgadget.gadgetbridge.entities.Alarm) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) AlarmDao(nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao) DeviceCoordinator(nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator) IOException(java.io.IOException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession) NonNull(androidx.annotation.NonNull)

Example 4 with Alarm

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

the class GBAlarmListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    final Alarm alarm = alarmList.get(position);
    holder.alarmDayMonday.setChecked(alarm.getRepetition(Alarm.ALARM_MON));
    holder.alarmDayTuesday.setChecked(alarm.getRepetition(Alarm.ALARM_TUE));
    holder.alarmDayWednesday.setChecked(alarm.getRepetition(Alarm.ALARM_WED));
    holder.alarmDayThursday.setChecked(alarm.getRepetition(Alarm.ALARM_THU));
    holder.alarmDayFriday.setChecked(alarm.getRepetition(Alarm.ALARM_FRI));
    holder.alarmDaySaturday.setChecked(alarm.getRepetition(Alarm.ALARM_SAT));
    holder.alarmDaySunday.setChecked(alarm.getRepetition(Alarm.ALARM_SUN));
    holder.container.setAlpha(alarm.getUnused() ? 0.5f : 1.0f);
    holder.isEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            alarm.setEnabled(isChecked);
            updateInDB(alarm);
        }
    });
    holder.container.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((ConfigureAlarms) mContext).configureAlarm(alarm);
        }
    });
    holder.container.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            alarm.setUnused(!alarm.getUnused());
            holder.container.setAlpha(alarm.getUnused() ? 0.5f : 1.0f);
            updateInDB(alarm);
            return true;
        }
    });
    holder.alarmTime.setText(DateTimeUtils.formatTime(alarm.getHour(), alarm.getMinute()));
    holder.isEnabled.setChecked(alarm.getEnabled());
    if (alarm.getSmartWakeup()) {
        holder.isSmartWakeup.setVisibility(TextView.VISIBLE);
    } else {
        holder.isSmartWakeup.setVisibility(TextView.GONE);
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.entities.Alarm) TextView(android.widget.TextView) CheckedTextView(android.widget.CheckedTextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) CompoundButton(android.widget.CompoundButton)

Aggregations

Alarm (nodomain.freeyourgadget.gadgetbridge.entities.Alarm)4 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)3 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)3 User (nodomain.freeyourgadget.gadgetbridge.entities.User)3 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)3 NonNull (androidx.annotation.NonNull)2 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)2 DeviceCoordinator (nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)2 View (android.view.View)1 CheckedTextView (android.widget.CheckedTextView)1 CompoundButton (android.widget.CompoundButton)1 TextView (android.widget.TextView)1 CardView (androidx.cardview.widget.CardView)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AlarmDao (nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao)1 ActivityUser (nodomain.freeyourgadget.gadgetbridge.model.ActivityUser)1 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)1