From 7ee20348dba94038520e490df3cd058b9d47f3f8 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Wed, 19 Apr 2017 21:51:23 +0200 Subject: [PATCH] Only sync Calendar and Sunrise/Sunset on devices that support it --- .../devices/DeviceCoordinator.java | 7 ++++ .../devices/UnknownDeviceCoordinator.java | 5 +++ .../devices/hplus/HPlusCoordinator.java | 5 +++ .../devices/liveview/LiveviewCoordinator.java | 5 +++ .../devices/miband/MiBandCoordinator.java | 5 +++ .../devices/pebble/PebbleCoordinator.java | 9 ++++- .../vibratissimo/VibratissimoCoordinator.java | 5 +++ .../service/DeviceCommunicationService.java | 38 ++++++++++--------- 8 files changed, 60 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java index 515a6e23..465895ac 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java @@ -227,4 +227,11 @@ public interface DeviceCoordinator { * @param device */ int getBondingStyle(GBDevice device); + + /** + * Indicates whether the device has some kind of calender we can sync to. + * Also used for generated sunrise/sunset events + */ + boolean supportsCalendarEvents(); + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java index 108423d0..e89198fa 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java @@ -176,4 +176,9 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { public Class getAppsManagementActivity() { return null; } + + @Override + public boolean supportsCalendarEvents() { + return false; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java index 0be66cd6..f2340dac 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java @@ -85,6 +85,11 @@ public class HPlusCoordinator extends AbstractDeviceCoordinator { return BONDING_STYLE_NONE; } + @Override + public boolean supportsCalendarEvents() { + return false; + } + @Override public DeviceType getDeviceType() { return DeviceType.HPLUS; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/liveview/LiveviewCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/liveview/LiveviewCoordinator.java index 4065ee76..5b5c19a7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/liveview/LiveviewCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/liveview/LiveviewCoordinator.java @@ -120,6 +120,11 @@ public class LiveviewCoordinator extends AbstractDeviceCoordinator { return null; } + @Override + public boolean supportsCalendarEvents() { + return false; + } + @Override protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { // nothing to delete, yet diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java index e9cd706c..6afc943b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java @@ -171,6 +171,11 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator { return null; } + @Override + public boolean supportsCalendarEvents() { + return false; + } + public static boolean hasValidUserInfo() { String dummyMacAddress = MiBandService.MAC_ADDRESS_FILTER_1_1A + ":00:00:00"; try { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java index da1358af..2e4da852 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java @@ -20,6 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble; import android.app.Activity; import android.content.Context; import android.net.Uri; +import android.support.annotation.NonNull; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -46,6 +47,7 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { public PebbleCoordinator() { } + @NonNull @Override public DeviceType getSupportedType(GBDeviceCandidate candidate) { String name = candidate.getDevice().getName(); @@ -71,7 +73,7 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { } @Override - protected void deleteDevice(GBDevice gbDevice, Device device, DaoSession session) throws GBException { + protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { Long deviceId = device.getId(); QueryBuilder qb = session.getPebbleHealthActivitySampleDao().queryBuilder(); qb.where(PebbleHealthActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); @@ -154,4 +156,9 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { public Class getAppsManagementActivity() { return AppManagerActivity.class; } + + @Override + public boolean supportsCalendarEvents() { + return true; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java index f8641760..ecc10de1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java @@ -120,6 +120,11 @@ public class VibratissimoCoordinator extends AbstractDeviceCoordinator { return null; } + @Override + public boolean supportsCalendarEvents() { + return false; + } + @Override protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { // nothing to delete, yet 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 79de077e..63d442f5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -41,6 +41,8 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; @@ -206,7 +208,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere if (mGBDevice.equals(device)) { mGBDevice = device; boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized()); - setReceiversEnableState(enableReceivers, mGBDevice.isInitialized()); + setReceiversEnableState(enableReceivers, mGBDevice.isInitialized(), DeviceHelper.getInstance().getCoordinator(device)); GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context); } else { LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice); @@ -397,7 +399,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, false); + setReceiversEnableState(false, false, null); mGBDevice.setState(GBDevice.State.NOT_CONNECTED); mGBDevice.sendDeviceUpdateIntent(this); } @@ -575,10 +577,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere } - private void setReceiversEnableState(boolean enable, boolean initialized) { + private void setReceiversEnableState(boolean enable, boolean initialized, DeviceCoordinator coordinator) { LOG.info("Setting broadcast receivers to: " + enable); - if (enable && initialized) { + if (enable && initialized && coordinator != null && coordinator.supportsCalendarEvents()) { if (mCalendarReceiver == null) { IntentFilter calendarIntentFilter = new IntentFilter(); calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED"); @@ -587,9 +589,19 @@ public class DeviceCommunicationService extends Service implements SharedPrefere mCalendarReceiver = new CalendarReceiver(mGBDevice); registerReceiver(mCalendarReceiver, calendarIntentFilter); } - } else if (mCalendarReceiver != null) { - unregisterReceiver(mCalendarReceiver); - mCalendarReceiver = null; + if (mAlarmReceiver == null) { + mAlarmReceiver = new AlarmReceiver(); + registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM")); + } + } else { + if (mCalendarReceiver != null) { + unregisterReceiver(mCalendarReceiver); + mCalendarReceiver = null; + } + if (mAlarmReceiver != null) { + unregisterReceiver(mAlarmReceiver); + mAlarmReceiver = null; + } } if (enable) { @@ -631,11 +643,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere mBlueToothPairingRequestReceiver = new BluetoothPairingRequestReceiver(this); registerReceiver(mBlueToothPairingRequestReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST)); } - - if (mAlarmReceiver == null) { - mAlarmReceiver = new AlarmReceiver(); - registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM")); - } if (mAlarmClockReceiver == null) { mAlarmClockReceiver = new AlarmClockReceiver(); IntentFilter filter = new IntentFilter(); @@ -673,11 +680,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere unregisterReceiver(mBlueToothPairingRequestReceiver); mBlueToothPairingRequestReceiver = null; } - - if (mAlarmReceiver != null) { - unregisterReceiver(mAlarmReceiver); - mAlarmReceiver = null; - } if (mAlarmClockReceiver != null) { unregisterReceiver(mAlarmClockReceiver); mAlarmClockReceiver = null; @@ -695,7 +697,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere super.onDestroy(); LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); - setReceiversEnableState(false, false); // disable BroadcastReceivers + setReceiversEnableState(false, false, null); // disable BroadcastReceivers setDeviceSupport(null); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);