2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Damien
|
|
|
|
Gaignon, Daniel Dakhno, Daniele Gobbetti, Dmitry Markin, José Rebelo,
|
|
|
|
Lem Dulfo, Petr Vaněk
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-06-24 20:14:08 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
2019-12-29 11:07:42 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2015-06-24 20:14:08 +02:00
|
|
|
import android.content.Intent;
|
2019-12-29 11:07:42 +01:00
|
|
|
import android.content.IntentFilter;
|
2015-06-24 20:14:08 +02:00
|
|
|
import android.os.Bundle;
|
2015-06-26 17:22:42 +02:00
|
|
|
import android.view.MenuItem;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2019-12-24 01:27:57 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2019-12-29 11:07:42 +01:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2019-12-24 01:27:57 +01:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2018-11-24 12:16:47 +01:00
|
|
|
import java.util.List;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
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;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Alarm;
|
2019-01-07 01:10:57 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
2018-11-24 12:16:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
2019-01-07 01:10:57 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
2017-01-26 20:55:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2019-12-29 11:07:42 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
2019-01-07 01:10:57 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
|
2018-11-24 12:16:47 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public class ConfigureAlarms extends AbstractGBActivity {
|
2019-01-07 01:10:57 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ConfigureAlarms.class);
|
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);
|
|
|
|
|
2019-12-29 11:07:42 +01:00
|
|
|
IntentFilter filterLocal = new IntentFilter();
|
|
|
|
filterLocal.addAction(DeviceService.ACTION_SAVE_ALARMS);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
|
|
|
|
|
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) {
|
2019-10-12 19:44:45 +02:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2015-10-18 23:52:59 +02:00
|
|
|
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
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
/**
|
|
|
|
* Reads the available alarms from the database and updates the view afterwards.
|
|
|
|
*/
|
2018-11-24 12:16:47 +01:00
|
|
|
private void updateAlarmsFromDB() {
|
2019-01-07 01:10:57 +01:00
|
|
|
List<Alarm> alarms = DBHelper.getAlarms(getGbDevice());
|
|
|
|
if (alarms.isEmpty()) {
|
|
|
|
alarms = AlarmUtils.readAlarmsFromPrefs(getGbDevice());
|
|
|
|
storeMigratedAlarms(alarms);
|
2018-11-24 12:16:47 +01:00
|
|
|
}
|
2019-01-07 01:10:57 +01:00
|
|
|
addMissingAlarms(alarms);
|
2018-11-24 12:16:47 +01:00
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
mGBAlarmListAdapter.setAlarmList(alarms);
|
|
|
|
mGBAlarmListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void storeMigratedAlarms(List<Alarm> alarms) {
|
|
|
|
for (Alarm alarm : alarms) {
|
|
|
|
DBHelper.store(alarm);
|
2018-11-24 12:16:47 +01:00
|
|
|
}
|
2019-01-07 01:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addMissingAlarms(List<Alarm> alarms) {
|
2023-09-27 23:11:02 +02:00
|
|
|
DeviceCoordinator coordinator = getGbDevice().getDeviceCoordinator();
|
2023-06-13 23:02:26 +02:00
|
|
|
int supportedNumAlarms = coordinator.getAlarmSlotCount(getGbDevice());
|
2019-01-07 01:10:57 +01:00
|
|
|
if (supportedNumAlarms > alarms.size()) {
|
|
|
|
try (DBHandler db = GBApplication.acquireDB()) {
|
|
|
|
DaoSession daoSession = db.getDaoSession();
|
|
|
|
Device device = DBHelper.getDevice(getGbDevice(), daoSession);
|
|
|
|
User user = DBHelper.getUser(daoSession);
|
2019-02-09 00:03:50 +01:00
|
|
|
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));
|
|
|
|
}
|
2019-01-07 01:10:57 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("Error accessing database", e);
|
|
|
|
}
|
2018-11-24 12:16:47 +01:00
|
|
|
}
|
2019-01-07 01:10:57 +01:00
|
|
|
}
|
2018-11-24 12:16:47 +01:00
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
private Alarm createDefaultAlarm(@NonNull Device device, @NonNull User user, int position) {
|
2020-04-24 00:31:04 +02:00
|
|
|
return new Alarm(device.getId(), user.getId(), position, false, false, false, 0, 6, 30, false, null, null);
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
public void configureAlarm(Alarm 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);
|
2019-01-07 01:10:57 +01:00
|
|
|
startIntent.putExtra(Alarm.EXTRA_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() {
|
2022-08-01 23:04:41 +02:00
|
|
|
GBApplication.deviceService(gbDevice).onSetAlarms(mGBAlarmListAdapter.getAlarmList());
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
2019-12-29 11:07:42 +01:00
|
|
|
|
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case DeviceService.ACTION_SAVE_ALARMS: {
|
|
|
|
updateAlarmsFromDB();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|