2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
2015-06-06 23:54:34 +02:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-06-25 14:34:21 +02:00
|
|
|
import java.util.ArrayList;
|
2015-08-17 21:30:37 +02:00
|
|
|
import java.util.EnumSet;
|
2015-06-06 23:54:34 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
|
2015-08-03 23:09:49 +02:00
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
/**
|
|
|
|
* Wraps another device support instance and supports busy-checking and throttling of events.
|
|
|
|
*/
|
|
|
|
public class ServiceDeviceSupport implements DeviceSupport {
|
2015-08-17 21:30:37 +02:00
|
|
|
|
2015-11-23 22:46:12 +01:00
|
|
|
enum Flags {
|
2015-08-17 21:30:37 +02:00
|
|
|
THROTTLING,
|
|
|
|
BUSY_CHECKING,
|
|
|
|
}
|
2015-09-24 14:45:21 +02:00
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ServiceDeviceSupport.class);
|
|
|
|
|
2015-08-17 21:30:37 +02:00
|
|
|
private static final long THROTTLING_THRESHOLD = 1000; // throttle multiple events in between one second
|
2015-06-06 23:54:34 +02:00
|
|
|
private final DeviceSupport delegate;
|
2015-08-17 21:30:37 +02:00
|
|
|
|
|
|
|
private long lastNotificationTime = 0;
|
2015-06-06 23:59:53 +02:00
|
|
|
private String lastNotificationKind;
|
2015-08-17 21:30:37 +02:00
|
|
|
private final EnumSet<Flags> flags;
|
2015-06-06 23:54:34 +02:00
|
|
|
|
2015-08-17 21:30:37 +02:00
|
|
|
public ServiceDeviceSupport(DeviceSupport delegate, EnumSet<Flags> flags) {
|
2015-06-06 23:54:34 +02:00
|
|
|
this.delegate = delegate;
|
2015-08-17 21:30:37 +02:00
|
|
|
this.flags = flags;
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-02 00:12:21 +02:00
|
|
|
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
|
|
|
|
delegate.setContext(gbDevice, btAdapter, context);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isConnected() {
|
|
|
|
return delegate.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean connect() {
|
|
|
|
return delegate.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dispose() {
|
|
|
|
delegate.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GBDevice getDevice() {
|
|
|
|
return delegate.getDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BluetoothAdapter getBluetoothAdapter() {
|
|
|
|
return delegate.getBluetoothAdapter();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Context getContext() {
|
|
|
|
return delegate.getContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean useAutoConnect() {
|
|
|
|
return delegate.useAutoConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void pair() {
|
|
|
|
delegate.pair();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkBusy(String notificationKind) {
|
2015-08-17 21:30:37 +02:00
|
|
|
if (!flags.contains(Flags.BUSY_CHECKING)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-06 23:54:34 +02:00
|
|
|
if (getDevice().isBusy()) {
|
|
|
|
LOG.info("Ignoring " + notificationKind + " because we're busy with " + getDevice().getBusyTask());
|
2015-08-17 21:30:37 +02:00
|
|
|
return true;
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkThrottle(String notificationKind) {
|
2015-08-17 21:30:37 +02:00
|
|
|
if (!flags.contains(Flags.THROTTLING)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-06 23:54:34 +02:00
|
|
|
long currentTime = System.currentTimeMillis();
|
2015-08-17 21:30:37 +02:00
|
|
|
if ((currentTime - lastNotificationTime) < THROTTLING_THRESHOLD) {
|
2015-06-06 23:59:53 +02:00
|
|
|
if (notificationKind != null && notificationKind.equals(lastNotificationKind)) {
|
2015-06-06 23:54:34 +02:00
|
|
|
LOG.info("Ignoring " + notificationKind + " because of throttling threshold reached");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 21:30:37 +02:00
|
|
|
lastNotificationTime = currentTime;
|
2015-06-06 23:59:53 +02:00
|
|
|
lastNotificationKind = notificationKind;
|
2015-06-06 23:54:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-24 14:45:21 +02:00
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("generic notification") || checkThrottle("generic notification")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-24 14:45:21 +02:00
|
|
|
delegate.onNotification(notificationSpec);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-21 00:58:18 +02:00
|
|
|
public void onSetTime() {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("set time") || checkThrottle("set time")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 00:58:18 +02:00
|
|
|
delegate.onSetTime();
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No throttling for the other events
|
|
|
|
|
|
|
|
@Override
|
2015-08-03 23:09:49 +02:00
|
|
|
public void onSetCallState(String number, String name, ServiceCommand command) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("set call state")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetCallState(number, name, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-27 17:44:20 +02:00
|
|
|
public void onSetMusicInfo(String artist, String album, String track, int duration, int trackCount, int trackNr) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("set music info")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-27 17:44:20 +02:00
|
|
|
delegate.onSetMusicInfo(artist, album, track, duration, trackCount, trackNr);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInstallApp(Uri uri) {
|
|
|
|
if (checkBusy("install app")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onInstallApp(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppInfoReq() {
|
|
|
|
if (checkBusy("app info request")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onAppInfoReq();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-13 21:44:26 +02:00
|
|
|
public void onAppStart(UUID uuid, boolean start) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("app start")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-13 21:44:26 +02:00
|
|
|
delegate.onAppStart(uuid, start);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppDelete(UUID uuid) {
|
|
|
|
if (checkBusy("app delete")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onAppDelete(uuid);
|
|
|
|
}
|
|
|
|
|
2016-03-03 14:23:17 +01:00
|
|
|
@Override
|
|
|
|
public void onAppConfiguration(UUID uuid, String config) {
|
|
|
|
if (checkBusy("app configuration")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onAppConfiguration(uuid, config);
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
@Override
|
|
|
|
public void onFetchActivityData() {
|
|
|
|
if (checkBusy("fetch activity data")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onFetchActivityData();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReboot() {
|
|
|
|
if (checkBusy("reboot")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onReboot();
|
|
|
|
}
|
2015-06-21 00:34:05 +02:00
|
|
|
|
2015-12-28 14:38:56 +01:00
|
|
|
@Override
|
2016-02-29 22:05:29 +01:00
|
|
|
public void onHeartRateTest() {
|
2015-12-28 14:38:56 +01:00
|
|
|
if (checkBusy("heartrate")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-29 22:05:29 +01:00
|
|
|
delegate.onHeartRateTest();
|
2015-12-28 14:38:56 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 00:34:05 +02:00
|
|
|
@Override
|
|
|
|
public void onFindDevice(boolean start) {
|
|
|
|
if (checkBusy("find device")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onFindDevice(start);
|
|
|
|
}
|
2015-06-24 00:23:38 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScreenshotReq() {
|
|
|
|
if (checkBusy("request screenshot")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onScreenshotReq();
|
|
|
|
}
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
@Override
|
2015-08-21 00:58:18 +02:00
|
|
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
2015-06-24 20:14:08 +02:00
|
|
|
if (checkBusy("set alarms")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-25 14:34:21 +02:00
|
|
|
delegate.onSetAlarms(alarms);
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableRealtimeSteps(boolean enable) {
|
|
|
|
if (checkBusy("enable realtime steps: " + enable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onEnableRealtimeSteps(enable);
|
|
|
|
}
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|