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.
master
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) { public CalendarReceiver(GBDevice gbDevice) {
LOG.info("Created calendar receiver."); LOG.info("Created calendar receiver.");
mGBDevice = gbDevice; mGBDevice = gbDevice;
syncCalendar();
} }
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
LOG.info("got calendar changed broadcast");
syncCalendar(); syncCalendar();
} }

View File

@ -204,7 +204,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
if (mGBDevice.equals(device)) { if (mGBDevice.equals(device)) {
mGBDevice = device; mGBDevice = device;
boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized()); boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized());
setReceiversEnableState(enableReceivers); setReceiversEnableState(enableReceivers, mGBDevice.isInitialized());
GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context); GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context);
} else { } else {
LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice); LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice);
@ -395,7 +395,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
case ACTION_DISCONNECT: { case ACTION_DISCONNECT: {
mDeviceSupport.dispose(); mDeviceSupport.dispose();
if (mGBDevice != null && mGBDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) { if (mGBDevice != null && mGBDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
setReceiversEnableState(false); setReceiversEnableState(false, false);
mGBDevice.setState(GBDevice.State.NOT_CONNECTED); mGBDevice.setState(GBDevice.State.NOT_CONNECTED);
mGBDevice.sendDeviceUpdateIntent(this); 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); 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 (enable) {
if (mPhoneCallReceiver == null) { if (mPhoneCallReceiver == null) {
mPhoneCallReceiver = new PhoneCallReceiver(); mPhoneCallReceiver = new PhoneCallReceiver();
@ -615,14 +629,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
mAlarmReceiver = new AlarmReceiver(); mAlarmReceiver = new AlarmReceiver();
registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM")); 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) { if (mAlarmClockReceiver == null) {
mAlarmClockReceiver = new AlarmClockReceiver(); mAlarmClockReceiver = new AlarmClockReceiver();
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
@ -659,10 +665,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
unregisterReceiver(mAlarmReceiver); unregisterReceiver(mAlarmReceiver);
mAlarmReceiver = null; mAlarmReceiver = null;
} }
if (mCalendarReceiver != null) {
unregisterReceiver(mCalendarReceiver);
mCalendarReceiver = null;
}
if (mAlarmClockReceiver != null) { if (mAlarmClockReceiver != null) {
unregisterReceiver(mAlarmClockReceiver); unregisterReceiver(mAlarmClockReceiver);
mAlarmClockReceiver = null; mAlarmClockReceiver = null;
@ -680,7 +682,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
super.onDestroy(); super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
setReceiversEnableState(false); // disable BroadcastReceivers setReceiversEnableState(false, false); // disable BroadcastReceivers
setDeviceSupport(null); setDeviceSupport(null);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);