mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-03 17:02:13 +01:00
Initial support for per-device alarms and raising the number of available alarms
TODO: - Fix alarm widget (how can we get the deviceId?) - Get rid of GBAlarm in favour of DAO generated Alarm class - Find better defaults - Bonus: migrate old preferece based shared settings
This commit is contained in:
parent
2facd9df6c
commit
740cf103f6
@ -45,7 +45,7 @@ public class GBDaoGenerator {
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Schema schema = new Schema(18, MAIN_PACKAGE + ".entities");
|
||||
Schema schema = new Schema(19, MAIN_PACKAGE + ".entities");
|
||||
|
||||
Entity userAttributes = addUserAttributes(schema);
|
||||
Entity user = addUserInfo(schema, userAttributes);
|
||||
@ -73,6 +73,7 @@ public class GBDaoGenerator {
|
||||
addID115ActivitySample(schema, user, device);
|
||||
|
||||
addCalendarSyncState(schema, device);
|
||||
addAlarms(schema, user, device);
|
||||
|
||||
addBipActivitySummary(schema, user, device);
|
||||
|
||||
@ -342,6 +343,26 @@ public class GBDaoGenerator {
|
||||
calendarSyncState.addIntProperty("hash").notNull();
|
||||
}
|
||||
|
||||
private static void addAlarms(Schema schema, Entity user, Entity device) {
|
||||
Entity alarm = addEntity(schema, "Alarm");
|
||||
Property deviceId = alarm.addLongProperty("deviceId").notNull().getProperty();
|
||||
Property userId = alarm.addLongProperty("userId").notNull().getProperty();
|
||||
Property position = alarm.addIntProperty("position").notNull().getProperty();
|
||||
Index indexUnique = new Index();
|
||||
indexUnique.addProperty(deviceId);
|
||||
indexUnique.addProperty(userId);
|
||||
indexUnique.addProperty(position);
|
||||
indexUnique.makeUnique();
|
||||
alarm.addIndex(indexUnique);
|
||||
alarm.addBooleanProperty("enabled").notNull();
|
||||
alarm.addBooleanProperty("smartAlarm").notNull();
|
||||
alarm.addIntProperty("repetition").notNull();
|
||||
alarm.addIntProperty("hour").notNull();
|
||||
alarm.addIntProperty("minute").notNull();
|
||||
alarm.addToOne(user, userId);
|
||||
alarm.addToOne(device, deviceId);
|
||||
}
|
||||
|
||||
private static void addBipActivitySummary(Schema schema, Entity user, Entity device) {
|
||||
Entity summary = addEntity(schema, "BaseActivitySummary");
|
||||
summary.implementsInterface(ACTIVITY_SUMMARY);
|
||||
|
@ -94,9 +94,10 @@ public class SleepAlarmWidget extends AppWidgetProvider {
|
||||
|
||||
|
||||
// overwrite the first alarm and activate it
|
||||
GBAlarm alarm = GBAlarm.createSingleShot(0, true, calendar);
|
||||
/*
|
||||
GBAlarm alarm = GBAlarm.createSingleShot(0,0, true, calendar); // FIXME!!!!
|
||||
alarm.store();
|
||||
|
||||
*/
|
||||
if (GBApplication.isRunningLollipopOrLater()) {
|
||||
setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
|
||||
}
|
||||
|
@ -23,29 +23,33 @@ import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.greenrobot.dao.query.QueryBuilder;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.adapter.GBAlarmListAdapter;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.Alarm;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS;
|
||||
|
||||
|
||||
public class ConfigureAlarms extends AbstractGBActivity {
|
||||
|
||||
private static final int REQ_CONFIGURE_ALARM = 1;
|
||||
|
||||
private GBAlarmListAdapter mGBAlarmListAdapter;
|
||||
private Set<String> preferencesAlarmListSet;
|
||||
private boolean avoidSendAlarmsToDevice;
|
||||
private GBDevice device;
|
||||
private GBDevice gbDevice;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -53,28 +57,20 @@ public class ConfigureAlarms extends AbstractGBActivity {
|
||||
|
||||
setContentView(R.layout.activity_configure_alarms);
|
||||
|
||||
device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
gbDevice = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
|
||||
Prefs prefs = GBApplication.getPrefs();
|
||||
preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
||||
if (preferencesAlarmListSet.isEmpty()) {
|
||||
//initialize the preferences
|
||||
preferencesAlarmListSet = new HashSet<>(Arrays.asList(GBAlarm.DEFAULT_ALARMS));
|
||||
prefs.getPreferences().edit().putStringSet(PREF_MIBAND_ALARMS, preferencesAlarmListSet).apply();
|
||||
}
|
||||
mGBAlarmListAdapter = new GBAlarmListAdapter(this);
|
||||
|
||||
mGBAlarmListAdapter = new GBAlarmListAdapter(this, preferencesAlarmListSet);
|
||||
|
||||
RecyclerView alarmsRecyclerView = (RecyclerView) findViewById(R.id.alarm_list);
|
||||
RecyclerView alarmsRecyclerView = findViewById(R.id.alarm_list);
|
||||
alarmsRecyclerView.setHasFixedSize(true);
|
||||
alarmsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
alarmsRecyclerView.setAdapter(mGBAlarmListAdapter);
|
||||
updateAlarmsFromPrefs();
|
||||
updateAlarmsFromDB();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
if (!avoidSendAlarmsToDevice) {
|
||||
if (!avoidSendAlarmsToDevice && gbDevice.isInitialized()) {
|
||||
sendAlarmsToDevice();
|
||||
}
|
||||
super.onPause();
|
||||
@ -84,16 +80,45 @@ public class ConfigureAlarms extends AbstractGBActivity {
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQ_CONFIGURE_ALARM) {
|
||||
avoidSendAlarmsToDevice = false;
|
||||
updateAlarmsFromPrefs();
|
||||
updateAlarmsFromDB();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAlarmsFromPrefs() {
|
||||
private void updateAlarmsFromDB() {
|
||||
Prefs prefs = GBApplication.getPrefs();
|
||||
preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
||||
int reservedSlots = prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, 0);
|
||||
|
||||
mGBAlarmListAdapter.setAlarmList(preferencesAlarmListSet, reservedSlots);
|
||||
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
||||
int alarmSlots = coordinator.getAlarmSlotCount();
|
||||
|
||||
Long deviceId;
|
||||
List<Alarm> allAlarms = null;
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
AlarmDao alarmDao = db.getDaoSession().getAlarmDao();
|
||||
Device dbDevice = DBHelper.findDevice(gbDevice, db.getDaoSession());
|
||||
deviceId = dbDevice.getId();
|
||||
QueryBuilder<Alarm> qb = alarmDao.queryBuilder();
|
||||
qb.where(AlarmDao.Properties.DeviceId.eq(deviceId)).orderAsc(AlarmDao.Properties.Position).limit(alarmSlots - reservedSlots);
|
||||
allAlarms = qb.build().list();
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<GBAlarm> gbAlarms = new ArrayList<>();
|
||||
if (allAlarms != null) {
|
||||
for (Alarm alarm : allAlarms) {
|
||||
gbAlarms.add(new GBAlarm(deviceId, alarm.getPosition(), alarm.getEnabled(),
|
||||
alarm.getSmartAlarm(), alarm.getRepetition(), alarm.getHour(), alarm.getMinute()));
|
||||
}
|
||||
}
|
||||
int hour = 5;
|
||||
while (gbAlarms.size() < alarmSlots) {
|
||||
GBAlarm gbAlarm = new GBAlarm(deviceId, gbAlarms.size(), false, false, 31, hour++, 30);
|
||||
gbAlarms.add(gbAlarm);
|
||||
gbAlarm.store();
|
||||
}
|
||||
|
||||
mGBAlarmListAdapter.setAlarmList(gbAlarms);
|
||||
mGBAlarmListAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@ -112,12 +137,12 @@ public class ConfigureAlarms extends AbstractGBActivity {
|
||||
avoidSendAlarmsToDevice = true;
|
||||
Intent startIntent = new Intent(getApplicationContext(), AlarmDetails.class);
|
||||
startIntent.putExtra("alarm", alarm);
|
||||
startIntent.putExtra(GBDevice.EXTRA_DEVICE, getDevice());
|
||||
startIntent.putExtra(GBDevice.EXTRA_DEVICE, getGbDevice());
|
||||
startActivityForResult(startIntent, REQ_CONFIGURE_ALARM);
|
||||
}
|
||||
|
||||
private GBDevice getDevice() {
|
||||
return device;
|
||||
private GBDevice getGbDevice() {
|
||||
return gbDevice;
|
||||
}
|
||||
|
||||
private void sendAlarmsToDevice() {
|
||||
|
@ -19,6 +19,7 @@ package nodomain.freeyourgadget.gadgetbridge.adapter;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.CardView;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
@ -30,9 +31,7 @@ import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
|
||||
@ -48,58 +47,32 @@ public class GBAlarmListAdapter extends RecyclerView.Adapter<GBAlarmListAdapter.
|
||||
private final Context mContext;
|
||||
private List<GBAlarm> alarmList;
|
||||
|
||||
public GBAlarmListAdapter(Context context, List<GBAlarm> alarmList) {
|
||||
public GBAlarmListAdapter(Context context) {
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
public void setAlarmList(List<GBAlarm> alarmList) {
|
||||
this.alarmList = alarmList;
|
||||
}
|
||||
|
||||
public GBAlarmListAdapter(Context context, Set<String> preferencesAlarmListSet) {
|
||||
this.mContext = context;
|
||||
alarmList = new ArrayList<>();
|
||||
|
||||
for (String alarmString : preferencesAlarmListSet) {
|
||||
alarmList.add(new GBAlarm(alarmString));
|
||||
}
|
||||
|
||||
Collections.sort(alarmList);
|
||||
}
|
||||
|
||||
public void setAlarmList(Set<String> preferencesAlarmListSet, int reservedSlots) {
|
||||
alarmList = new ArrayList<>();
|
||||
|
||||
for (String alarmString : preferencesAlarmListSet) {
|
||||
alarmList.add(new GBAlarm(alarmString));
|
||||
}
|
||||
|
||||
Collections.sort(alarmList);
|
||||
|
||||
//cannot do this earlier because the Set is not guaranteed to be in order by ID
|
||||
alarmList.subList(alarmList.size() - reservedSlots, alarmList.size()).clear();
|
||||
}
|
||||
|
||||
public ArrayList<? extends Alarm> getAlarmList() {
|
||||
public ArrayList getAlarmList() {
|
||||
return (ArrayList) alarmList;
|
||||
}
|
||||
|
||||
|
||||
public void update(GBAlarm alarm) {
|
||||
for (GBAlarm a : alarmList) {
|
||||
if (alarm.equals(a)) {
|
||||
a = alarm;
|
||||
}
|
||||
}
|
||||
alarm.store();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public GBAlarmListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
public GBAlarmListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alarm, parent, false);
|
||||
ViewHolder vh = new ViewHolder(view);
|
||||
return vh;
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, final int position) {
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
|
||||
|
||||
final GBAlarm alarm = alarmList.get(position);
|
||||
|
||||
@ -159,21 +132,19 @@ public class GBAlarmListAdapter extends RecyclerView.Adapter<GBAlarmListAdapter.
|
||||
ViewHolder(View view) {
|
||||
super(view);
|
||||
|
||||
container = (CardView) view.findViewById(R.id.card_view);
|
||||
|
||||
alarmTime = (TextView) view.findViewById(R.id.alarm_item_time);
|
||||
isEnabled = (Switch) view.findViewById(R.id.alarm_item_toggle);
|
||||
isSmartWakeup = (TextView) view.findViewById(R.id.alarm_smart_wakeup);
|
||||
|
||||
alarmDayMonday = (CheckedTextView) view.findViewById(R.id.alarm_item_monday);
|
||||
alarmDayTuesday = (CheckedTextView) view.findViewById(R.id.alarm_item_tuesday);
|
||||
alarmDayWednesday = (CheckedTextView) view.findViewById(R.id.alarm_item_wednesday);
|
||||
alarmDayThursday = (CheckedTextView) view.findViewById(R.id.alarm_item_thursday);
|
||||
alarmDayFriday = (CheckedTextView) view.findViewById(R.id.alarm_item_friday);
|
||||
alarmDaySaturday = (CheckedTextView) view.findViewById(R.id.alarm_item_saturday);
|
||||
alarmDaySunday = (CheckedTextView) view.findViewById(R.id.alarm_item_sunday);
|
||||
container = view.findViewById(R.id.card_view);
|
||||
|
||||
alarmTime = view.findViewById(R.id.alarm_item_time);
|
||||
isEnabled = view.findViewById(R.id.alarm_item_toggle);
|
||||
isSmartWakeup = view.findViewById(R.id.alarm_smart_wakeup);
|
||||
|
||||
alarmDayMonday = view.findViewById(R.id.alarm_item_monday);
|
||||
alarmDayTuesday = view.findViewById(R.id.alarm_item_tuesday);
|
||||
alarmDayWednesday = view.findViewById(R.id.alarm_item_wednesday);
|
||||
alarmDayThursday = view.findViewById(R.id.alarm_item_thursday);
|
||||
alarmDayFriday = view.findViewById(R.id.alarm_item_friday);
|
||||
alarmDaySaturday = view.findViewById(R.id.alarm_item_saturday);
|
||||
alarmDaySunday = view.findViewById(R.id.alarm_item_sunday);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,6 +188,13 @@ public interface DeviceCoordinator {
|
||||
*/
|
||||
boolean supportsAlarmConfiguration();
|
||||
|
||||
/**
|
||||
* Returns the number of alarms this device/coordinator supports
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int getAlarmSlotCount();
|
||||
|
||||
/**
|
||||
* Returns true if this device/coordinator supports alarms with smart wakeup
|
||||
* @return
|
||||
|
@ -142,6 +142,11 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -148,6 +148,11 @@ public class HPlusCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -108,6 +108,11 @@ public abstract class HuamiCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsActivityDataFetching() {
|
||||
return true;
|
||||
|
@ -110,6 +110,11 @@ public class ID115Coordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -149,6 +149,11 @@ public class TeclastH30Coordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -84,6 +84,11 @@ public class LiveviewCoordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -136,6 +136,11 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return true;
|
||||
|
@ -112,6 +112,11 @@ public class No1F1Coordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -121,6 +121,11 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -88,6 +88,11 @@ public abstract class RoidmiCoordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -85,6 +85,11 @@ public class VibratissimoCoordinator extends AbstractDeviceCoordinator {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -117,6 +117,11 @@ public class Watch9DeviceCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -91,6 +91,11 @@ public class XWatchCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSmartWakeup(GBDevice device) {
|
||||
return false;
|
||||
|
@ -79,6 +79,11 @@ public class ZeTimeCoordinator extends AbstractDeviceCoordinator {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAlarmSlotCount() {
|
||||
return 3; // FIXME - check the real value
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsWeather() {
|
||||
return true;
|
||||
|
@ -21,15 +21,13 @@ import android.os.Parcel;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.Locale;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS;
|
||||
|
||||
|
||||
public class GBAlarm implements Alarm {
|
||||
@ -40,11 +38,10 @@ public class GBAlarm implements Alarm {
|
||||
private int repetition;
|
||||
private int hour;
|
||||
private int minute;
|
||||
private long deviceId;
|
||||
|
||||
public static final String[] DEFAULT_ALARMS = {"2,false,false,0,15,30", "1,false,false,96,8,0", "0,false,true,31,7,30"};
|
||||
|
||||
|
||||
public GBAlarm(int index, boolean enabled, boolean smartWakeup, int repetition, int hour, int minute) {
|
||||
public GBAlarm(Long deviceId, int index, boolean enabled, boolean smartWakeup, int repetition, int hour, int minute) {
|
||||
this.deviceId = deviceId;
|
||||
this.index = index;
|
||||
this.enabled = enabled;
|
||||
this.smartWakeup = smartWakeup;
|
||||
@ -53,29 +50,19 @@ public class GBAlarm implements Alarm {
|
||||
this.minute = minute;
|
||||
}
|
||||
|
||||
public GBAlarm(String fromPreferences) {
|
||||
String[] tokens = fromPreferences.split(",");
|
||||
//TODO: sanify the string!
|
||||
this.index = Integer.parseInt(tokens[0]);
|
||||
this.enabled = Boolean.parseBoolean(tokens[1]);
|
||||
this.smartWakeup = Boolean.parseBoolean(tokens[2]);
|
||||
this.repetition = Integer.parseInt(tokens[3]);
|
||||
this.hour = Integer.parseInt(tokens[4]);
|
||||
this.minute = Integer.parseInt(tokens[5]);
|
||||
}
|
||||
|
||||
public static GBAlarm createSingleShot(int index, boolean smartWakeup, Calendar calendar) {
|
||||
return new GBAlarm(index, true, smartWakeup, Alarm.ALARM_ONCE, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
|
||||
public static GBAlarm createSingleShot(long deviceId, int index, boolean smartWakeup, Calendar calendar) {
|
||||
return new GBAlarm(deviceId, index, true, smartWakeup, Alarm.ALARM_ONCE, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
|
||||
}
|
||||
|
||||
private static GBAlarm readFromParcel(Parcel pc) {
|
||||
long deviceId = pc.readLong();
|
||||
int index = pc.readInt();
|
||||
boolean enabled = Boolean.parseBoolean(pc.readString());
|
||||
boolean smartWakeup = Boolean.parseBoolean(pc.readString());
|
||||
int repetition = pc.readInt();
|
||||
int hour = pc.readInt();
|
||||
int minute = pc.readInt();
|
||||
return new GBAlarm(index, enabled, smartWakeup, repetition, hour, minute);
|
||||
return new GBAlarm(deviceId, index, enabled, smartWakeup, repetition, hour, minute);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -100,6 +87,7 @@ public class GBAlarm implements Alarm {
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeLong(deviceId);
|
||||
dest.writeInt(this.index);
|
||||
dest.writeString(String.valueOf(this.enabled));
|
||||
dest.writeString(String.valueOf(this.smartWakeup));
|
||||
@ -125,7 +113,7 @@ public class GBAlarm implements Alarm {
|
||||
|
||||
@Override
|
||||
public String getTime() {
|
||||
return String.format("%02d", this.hour) + ":" + String.format("%02d", this.minute);
|
||||
return String.format(Locale.US, "%02d", this.hour) + ":" + String.format(Locale.US, "%02d", this.minute);
|
||||
}
|
||||
|
||||
public int getHour() {
|
||||
@ -175,22 +163,12 @@ public class GBAlarm implements Alarm {
|
||||
return getRepetitionMask() != ALARM_ONCE;
|
||||
}
|
||||
|
||||
public String toPreferences() {
|
||||
return String.valueOf(this.index) + ',' +
|
||||
String.valueOf(this.enabled) + ',' +
|
||||
String.valueOf(this.smartWakeup) + ',' +
|
||||
String.valueOf(this.repetition) + ',' +
|
||||
String.valueOf(this.hour) + ',' +
|
||||
String.valueOf(this.minute);
|
||||
}
|
||||
|
||||
public void setSmartWakeup(boolean smartWakeup) {
|
||||
this.smartWakeup = smartWakeup;
|
||||
}
|
||||
|
||||
public void setRepetition(boolean mon, boolean tue, boolean wed, boolean thu, boolean fri, boolean sat, boolean sun) {
|
||||
this.repetition = ALARM_ONCE |
|
||||
(mon ? ALARM_MON : 0) |
|
||||
this.repetition = (mon ? ALARM_MON : 0) |
|
||||
(tue ? ALARM_TUE : 0) |
|
||||
(wed ? ALARM_WED : 0) |
|
||||
(thu ? ALARM_THU : 0) |
|
||||
@ -212,22 +190,14 @@ public class GBAlarm implements Alarm {
|
||||
}
|
||||
|
||||
public void store() {
|
||||
Prefs prefs = GBApplication.getPrefs();
|
||||
Set<String> preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
||||
//the old Set cannot be updated in place see http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29
|
||||
Set<String> newPrefs = new HashSet<>(preferencesAlarmListSet);
|
||||
|
||||
Iterator<String> iterator = newPrefs.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
String alarmString = iterator.next();
|
||||
if (this.equals(new GBAlarm(alarmString))) {
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
DaoSession daoSession = db.getDaoSession();
|
||||
Long userId = DBHelper.getUser(daoSession).getId();
|
||||
nodomain.freeyourgadget.gadgetbridge.entities.Alarm alarm = new nodomain.freeyourgadget.gadgetbridge.entities.Alarm(deviceId, userId, index, enabled, smartWakeup, repetition, hour, minute);
|
||||
daoSession.insertOrReplace(alarm);
|
||||
} catch (Exception e) {
|
||||
// LOG.error("Error acquiring database", e);
|
||||
}
|
||||
newPrefs.add(this.toPreferences());
|
||||
prefs.getPreferences().edit().putStringSet(PREF_MIBAND_ALARMS, newPrefs).apply();
|
||||
}
|
||||
|
||||
public static final Creator CREATOR = new Creator() {
|
||||
|
@ -1394,10 +1394,12 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
||||
private void queueAlarm(Alarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
|
||||
Calendar calendar = alarm.getAlarmCal();
|
||||
|
||||
int maxAlarms = 5; // arbitrary at the moment...
|
||||
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
||||
int maxAlarms = coordinator.getAlarmSlotCount();
|
||||
|
||||
if (alarm.getIndex() >= maxAlarms) {
|
||||
if (alarm.isEnabled()) {
|
||||
GB.toast(getContext(), "Only 5 alarms are currently supported.", Toast.LENGTH_LONG, GB.WARN);
|
||||
GB.toast(getContext(), "Only " + maxAlarms + " alarms are currently supported.", Toast.LENGTH_LONG, GB.WARN);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -1465,7 +1467,16 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
||||
CalendarEvents upcomingEvents = new CalendarEvents();
|
||||
List<CalendarEvents.CalendarEvent> mEvents = upcomingEvents.getCalendarEventList(getContext());
|
||||
|
||||
Long deviceId;
|
||||
try (DBHandler handler = GBApplication.acquireDB()) {
|
||||
DaoSession session = handler.getDaoSession();
|
||||
deviceId = DBHelper.getDevice(getDevice(), session).getId();
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not acquire DB", e);
|
||||
return this;
|
||||
}
|
||||
int iteration = 0;
|
||||
|
||||
for (CalendarEvents.CalendarEvent mEvt : mEvents) {
|
||||
if (iteration >= availableSlots || iteration > 2) {
|
||||
break;
|
||||
@ -1473,7 +1484,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
||||
int slotToUse = 2 - iteration;
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(mEvt.getBegin());
|
||||
Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
|
||||
Alarm alarm = GBAlarm.createSingleShot(deviceId, slotToUse, false, calendar);
|
||||
queueAlarm(alarm, builder, characteristic);
|
||||
iteration++;
|
||||
}
|
||||
|
@ -1225,6 +1225,15 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
CalendarEvents upcomingEvents = new CalendarEvents();
|
||||
List<CalendarEvents.CalendarEvent> mEvents = upcomingEvents.getCalendarEventList(getContext());
|
||||
|
||||
Long deviceId;
|
||||
try (DBHandler handler = GBApplication.acquireDB()) {
|
||||
DaoSession session = handler.getDaoSession();
|
||||
deviceId = DBHelper.getDevice(getDevice(), session).getId();
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not acquire DB", e);
|
||||
return;
|
||||
}
|
||||
|
||||
int iteration = 0;
|
||||
for (CalendarEvents.CalendarEvent mEvt : mEvents) {
|
||||
if (iteration >= availableSlots || iteration > 2) {
|
||||
@ -1233,7 +1242,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
int slotToUse = 2 - iteration;
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(mEvt.getBegin());
|
||||
Alarm alarm = GBAlarm.createSingleShot(slotToUse, false, calendar);
|
||||
Alarm alarm = GBAlarm.createSingleShot(deviceId, slotToUse, false, calendar);
|
||||
queueAlarm(alarm, builder, characteristic);
|
||||
iteration++;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user