1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 06:22:55 +01:00

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.
This commit is contained in:
Andreas Shimokawa 2017-04-17 21:09:29 +02:00
parent 1e231e6129
commit 35efa30c4b
2 changed files with 20 additions and 16 deletions

View File

@ -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();
}

View File

@ -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);