Search in sources :

Example 1 with AlarmDao

use of nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao 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)

Aggregations

NonNull (androidx.annotation.NonNull)1 IOException (java.io.IOException)1 DeviceCoordinator (nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)1 Alarm (nodomain.freeyourgadget.gadgetbridge.entities.Alarm)1 AlarmDao (nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 User (nodomain.freeyourgadget.gadgetbridge.entities.User)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 ActivityUser (nodomain.freeyourgadget.gadgetbridge.model.ActivityUser)1 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)1