2018-02-26 14:27:32 +01:00
|
|
|
/* Copyright (C) 2015-2018 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2017-03-10 14:53:19 +01:00
|
|
|
Gobbetti, Lem Dulfo
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-06-24 20:14:08 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2017-04-08 18:44:15 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2015-06-26 17:22:42 +02:00
|
|
|
import android.view.MenuItem;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-06-24 20:14:08 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBAlarmListAdapter;
|
2018-11-24 12:16:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-12-17 17:02:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
2018-11-24 12:16:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Alarm;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AlarmDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
2017-01-26 20:55:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2018-11-24 12:16:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public class ConfigureAlarms extends AbstractGBActivity {
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2015-10-18 23:52:59 +02:00
|
|
|
private static final int REQ_CONFIGURE_ALARM = 1;
|
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
private GBAlarmListAdapter mGBAlarmListAdapter;
|
2015-10-18 23:52:59 +02:00
|
|
|
private boolean avoidSendAlarmsToDevice;
|
2018-11-24 12:16:47 +01:00
|
|
|
private GBDevice gbDevice;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
setContentView(R.layout.activity_configure_alarms);
|
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
gbDevice = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
2017-01-26 20:55:00 +01:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
mGBAlarmListAdapter = new GBAlarmListAdapter(this);
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
RecyclerView alarmsRecyclerView = findViewById(R.id.alarm_list);
|
2017-04-08 18:44:15 +02:00
|
|
|
alarmsRecyclerView.setHasFixedSize(true);
|
|
|
|
alarmsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
|
|
|
alarmsRecyclerView.setAdapter(mGBAlarmListAdapter);
|
2018-11-24 12:16:47 +01:00
|
|
|
updateAlarmsFromDB();
|
2015-10-18 23:52:59 +02:00
|
|
|
}
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-10-18 23:52:59 +02:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
2018-11-24 12:16:47 +01:00
|
|
|
if (!avoidSendAlarmsToDevice && gbDevice.isInitialized()) {
|
2015-10-18 23:52:59 +02:00
|
|
|
sendAlarmsToDevice();
|
|
|
|
}
|
|
|
|
super.onPause();
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-10-18 23:52:59 +02:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (requestCode == REQ_CONFIGURE_ALARM) {
|
|
|
|
avoidSendAlarmsToDevice = false;
|
2018-11-24 12:16:47 +01:00
|
|
|
updateAlarmsFromDB();
|
2015-10-18 23:52:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
private void updateAlarmsFromDB() {
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2016-04-25 23:39:03 +02:00
|
|
|
int reservedSlots = prefs.getInt(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, 0);
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
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);
|
2015-06-26 17:22:42 +02:00
|
|
|
mGBAlarmListAdapter.notifyDataSetChanged();
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-26 17:22:42 +02:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
// back button
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void configureAlarm(GBAlarm alarm) {
|
2015-10-18 23:52:59 +02:00
|
|
|
avoidSendAlarmsToDevice = true;
|
2017-01-26 20:55:00 +01:00
|
|
|
Intent startIntent = new Intent(getApplicationContext(), AlarmDetails.class);
|
2015-06-26 17:22:42 +02:00
|
|
|
startIntent.putExtra("alarm", alarm);
|
2018-11-24 12:16:47 +01:00
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, getGbDevice());
|
2015-10-18 23:52:59 +02:00
|
|
|
startActivityForResult(startIntent, REQ_CONFIGURE_ALARM);
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
private GBDevice getGbDevice() {
|
|
|
|
return gbDevice;
|
2017-01-26 20:55:00 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 17:22:42 +02:00
|
|
|
private void sendAlarmsToDevice() {
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().onSetAlarms(mGBAlarmListAdapter.getAlarmList());
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
|
|
|
}
|