From 7ea2261ba3f76137b862060063065736bf8d19e7 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 24 May 2024 09:26:37 +0100 Subject: [PATCH] Bangle.js: honour the enable_calendar_sync setting Allow handleCalendarSync to send a single `calendar-` command with all IDs before it starts adding new items (faster) --- .../banglejs/BangleJSDeviceSupport.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java index 4ca9a0e71..6ef224d0f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java @@ -877,10 +877,11 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { * Handle "force_calendar_sync" packet */ private void handleCalendarSync(JSONObject json) throws JSONException { - //if(!GBApplication.getPrefs().getBoolean("enable_calendar_sync", false)) return; + if(!GBApplication.getPrefs().getBoolean("enable_calendar_sync", false)) return; //pretty much like the updateEvents in CalendarReceiver, but would need a lot of libraries here JSONArray ids = json.getJSONArray("ids"); ArrayList idsList = new ArrayList<>(ids.length()); + ArrayList idsDeletedList = new ArrayList<>(ids.length()); try (DBHandler dbHandler = GBApplication.acquireDB()) { DaoSession session = dbHandler.getDaoSession(); Long deviceId = DBHelper.getDevice(gbDevice, session).getId(); @@ -897,6 +898,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { qb.and(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId), CalendarSyncStateDao.Properties.CalendarEntryId.eq(id))).build().unique(); if(calendarSyncState == null) { + idsDeletedList.add(id); onDeleteCalendarEvent((byte)0, id); LOG.info("event id="+ id +" is on device id="+ deviceId +", removing it there"); } else { @@ -904,6 +906,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { idsList.add(id); } } + // Now issue the command to delete from the Bangle + if (idsDeletedList.size() > 0) + deleteCalendarEvents(idsDeletedList); //remove all elements not in ids from database (we don't have them) for(CalendarSyncState calendarSyncState : states) { @@ -1622,6 +1627,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { @Override public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) { + if(!GBApplication.getPrefs().getBoolean("enable_calendar_sync", false)) return; String description = calendarEventSpec.description; if (description != null) { // remove any HTML formatting @@ -1655,6 +1661,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { @Override public void onDeleteCalendarEvent(byte type, long id) { + // FIXME: CalenderReceiver will call this directly - can we somehow batch up delete calls and use deleteCalendarEvents? + if(!GBApplication.getPrefs().getBoolean("enable_calendar_sync", false)) return; try { JSONObject o = new JSONObject(); o.put("t", "calendar-"); @@ -1665,6 +1673,26 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { } } + /* Called when we need to get rid of multiple calendar events */ + public void deleteCalendarEvents(ArrayList ids) { + if(!GBApplication.getPrefs().getBoolean("enable_calendar_sync", false)) return; + if (ids.size() > 0) + try { + JSONObject o = new JSONObject(); + o.put("t", "calendar-"); + if (ids.size() == 1) { + o.put("id", ids.get(0)); + } else { + JSONArray a = new JSONArray(); + for (long id : ids) a.put(id); + o.put("id", a); + } + uartTxJSON("onDeleteCalendarEvent", o); + } catch (JSONException e) { + LOG.info("JSONException: " + e.getLocalizedMessage()); + } + } + @Override public void onSendWeather(ArrayList weatherSpecs) { WeatherSpec weatherSpec = weatherSpecs.get(0);