From 50defa1eb6e86eb5a39e4bb065bb759a6a416b7b Mon Sep 17 00:00:00 2001 From: vanous Date: Sun, 29 Dec 2019 11:07:42 +0100 Subject: [PATCH] Add broadcast sender/receiver to ConfigureAlarms/HuamiSupport This is to prevent discrepancies if user edits alarms in Gadgetbridge and also on the device at the same time. --- .../activities/ConfigureAlarms.java | 30 +++++++++++++++++++ .../gadgetbridge/model/DeviceService.java | 1 + .../service/devices/huami/HuamiSupport.java | 3 ++ 3 files changed, 34 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java index de8f4c31a..fd427af72 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java @@ -17,11 +17,15 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.activities; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.view.MenuItem; import androidx.annotation.NonNull; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -41,6 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; @@ -60,6 +65,10 @@ public class ConfigureAlarms extends AbstractGBActivity { setContentView(R.layout.activity_configure_alarms); + IntentFilter filterLocal = new IntentFilter(); + filterLocal.addAction(DeviceService.ACTION_SAVE_ALARMS); + LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); + gbDevice = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE); mGBAlarmListAdapter = new GBAlarmListAdapter(this); @@ -166,4 +175,25 @@ public class ConfigureAlarms extends AbstractGBActivity { private void sendAlarmsToDevice() { GBApplication.deviceService().onSetAlarms(mGBAlarmListAdapter.getAlarmList()); } + + 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(); + } + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index fb48ccf8b..d0e7c0241 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -53,6 +53,7 @@ public interface DeviceService extends EventHandler { String ACTION_FIND_DEVICE = PREFIX + ".action.find_device"; String ACTION_SET_CONSTANT_VIBRATION = PREFIX + ".action.set_constant_vibration"; String ACTION_SET_ALARMS = PREFIX + ".action.set_alarms"; + String ACTION_SAVE_ALARMS = PREFIX + ".action.save_alarms"; String ACTION_ENABLE_REALTIME_STEPS = PREFIX + ".action.enable_realtime_steps"; String ACTION_REALTIME_SAMPLES = PREFIX + ".action.realtime_samples"; String ACTION_ENABLE_REALTIME_HEARTRATE_MEASUREMENT = PREFIX + ".action.realtime_hr_measurement"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 1adac3fc6..b664d11bc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -143,6 +143,7 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.ge import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefStringValue; import static nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL; + public class HuamiSupport extends AbstractBTLEDeviceSupport { // We introduce key press counter for notification purposes @@ -1466,6 +1467,8 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { alarm.setEnabled(enabled); alarm.setUnused(unused); DBHelper.store(alarm); + Intent intent = new Intent(DeviceService.ACTION_SAVE_ALARMS); + LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); } } }