Don't throttle events for Pebble

And actually do busy-filtering for Mi and Pebble
master
cpfeiffer 2015-08-17 21:30:37 +02:00
parent 6fede31bdf
commit 9dc945a406
2 changed files with 26 additions and 9 deletions

View File

@ -5,6 +5,8 @@ import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.widget.Toast;
import java.util.EnumSet;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
@ -51,13 +53,12 @@ public class DeviceSupportFactory {
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(deviceAddress);
if (btDevice.getName() == null || btDevice.getName().equals("MI")) { //FIXME: workaround for Miband not being paired
gbDevice = new GBDevice(deviceAddress, "MI", DeviceType.MIBAND);
deviceSupport = new MiBandSupport();
deviceSupport = new ServiceDeviceSupport(new MiBandSupport(), EnumSet.of(ServiceDeviceSupport.Flags.THROTTLING, ServiceDeviceSupport.Flags.BUSY_CHECKING));
} else if (btDevice.getName().indexOf("Pebble") == 0) {
gbDevice = new GBDevice(deviceAddress, btDevice.getName(), DeviceType.PEBBLE);
deviceSupport = new PebbleSupport();
deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
}
if (deviceSupport != null) {
deviceSupport = new ServiceDeviceSupport(deviceSupport);
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
return deviceSupport;
}

View File

@ -8,6 +8,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
@ -18,15 +19,23 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
* Wraps another device support instance and supports busy-checking and throttling of events.
*/
public class ServiceDeviceSupport implements DeviceSupport {
static enum Flags {
THROTTLING,
BUSY_CHECKING,
}
private static final Logger LOG = LoggerFactory.getLogger(ServiceDeviceSupport.class);
private static final long THROTTLING_THRESHOLD = 1000; // throttle multiple events in between one second
private final DeviceSupport delegate;
private long lastNoficationTime = 0;
private String lastNotificationKind;
public ServiceDeviceSupport(DeviceSupport delegate) {
private long lastNotificationTime = 0;
private String lastNotificationKind;
private final EnumSet<Flags> flags;
public ServiceDeviceSupport(DeviceSupport delegate, EnumSet<Flags> flags) {
this.delegate = delegate;
this.flags = flags;
}
@Override
@ -75,21 +84,28 @@ public class ServiceDeviceSupport implements DeviceSupport {
}
private boolean checkBusy(String notificationKind) {
if (!flags.contains(Flags.BUSY_CHECKING)) {
return false;
}
if (getDevice().isBusy()) {
LOG.info("Ignoring " + notificationKind + " because we're busy with " + getDevice().getBusyTask());
return true;
}
return false;
}
private boolean checkThrottle(String notificationKind) {
if (!flags.contains(Flags.THROTTLING)) {
return false;
}
long currentTime = System.currentTimeMillis();
if ((currentTime - lastNoficationTime) < THROTTLING_THRESHOLD) {
if ((currentTime - lastNotificationTime) < THROTTLING_THRESHOLD) {
if (notificationKind != null && notificationKind.equals(lastNotificationKind)) {
LOG.info("Ignoring " + notificationKind + " because of throttling threshold reached");
return true;
}
}
lastNoficationTime = currentTime;
lastNotificationTime = currentTime;
lastNotificationKind = notificationKind;
return false;
}