From 35efa30c4b5d684794ff5381815e38af89eb73c2 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 17 Apr 2017 21:09:29 +0200 Subject: [PATCH] Calendar Sync: Only enable calendar broadcast receiver when device is initialized This excludes it from any auto connect logic. We are now save to sync the calendar once in contructor. --- .../externalevents/CalendarReceiver.java | 2 ++ .../service/DeviceCommunicationService.java | 34 ++++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CalendarReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CalendarReceiver.java index d4f22439a..4918e6955 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CalendarReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CalendarReceiver.java @@ -83,10 +83,12 @@ public class CalendarReceiver extends BroadcastReceiver { public CalendarReceiver(GBDevice gbDevice) { LOG.info("Created calendar receiver."); mGBDevice = gbDevice; + syncCalendar(); } @Override public void onReceive(Context context, Intent intent) { + LOG.info("got calendar changed broadcast"); syncCalendar(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index a1a4ab19e..a99fc6551 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -204,7 +204,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere if (mGBDevice.equals(device)) { mGBDevice = device; boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized()); - setReceiversEnableState(enableReceivers); + setReceiversEnableState(enableReceivers, mGBDevice.isInitialized()); GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context); } else { LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice); @@ -395,7 +395,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere case ACTION_DISCONNECT: { mDeviceSupport.dispose(); if (mGBDevice != null && mGBDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) { - setReceiversEnableState(false); + setReceiversEnableState(false, false); mGBDevice.setState(GBDevice.State.NOT_CONNECTED); mGBDevice.sendDeviceUpdateIntent(this); } @@ -573,9 +573,23 @@ public class DeviceCommunicationService extends Service implements SharedPrefere } - private void setReceiversEnableState(boolean enable) { + private void setReceiversEnableState(boolean enable, boolean initialized) { LOG.info("Setting broadcast receivers to: " + enable); + if (enable && initialized) { + if (mCalendarReceiver == null) { + IntentFilter calendarIntentFilter = new IntentFilter(); + calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED"); + calendarIntentFilter.addDataScheme("content"); + calendarIntentFilter.addDataAuthority("com.android.calendar", null); + mCalendarReceiver = new CalendarReceiver(mGBDevice); + registerReceiver(mCalendarReceiver, calendarIntentFilter); + } + } else if (mCalendarReceiver != null) { + unregisterReceiver(mCalendarReceiver); + mCalendarReceiver = null; + } + if (enable) { if (mPhoneCallReceiver == null) { mPhoneCallReceiver = new PhoneCallReceiver(); @@ -615,14 +629,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere mAlarmReceiver = new AlarmReceiver(); registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM")); } - if (mCalendarReceiver == null) { - IntentFilter calendarIntentFilter = new IntentFilter(); - calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED"); - calendarIntentFilter.addDataScheme("content"); - calendarIntentFilter.addDataAuthority("com.android.calendar", null); - mCalendarReceiver = new CalendarReceiver(mGBDevice); - registerReceiver(mCalendarReceiver, calendarIntentFilter); - } if (mAlarmClockReceiver == null) { mAlarmClockReceiver = new AlarmClockReceiver(); IntentFilter filter = new IntentFilter(); @@ -659,10 +665,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere unregisterReceiver(mAlarmReceiver); mAlarmReceiver = null; } - if (mCalendarReceiver != null) { - unregisterReceiver(mCalendarReceiver); - mCalendarReceiver = null; - } if (mAlarmClockReceiver != null) { unregisterReceiver(mAlarmClockReceiver); mAlarmClockReceiver = null; @@ -680,7 +682,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere super.onDestroy(); LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); - setReceiversEnableState(false); // disable BroadcastReceivers + setReceiversEnableState(false, false); // disable BroadcastReceivers setDeviceSupport(null); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);