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);
}
}
}
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();
}
}
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();
}
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);
}
}
Aggregations