mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-08-05 13:32:14 +02:00
b77f3ad3bf
This also moved the canned replies setting to pebble settings (we will change that if we have another device supporting this)
315 lines
8.0 KiB
Java
315 lines
8.0 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.EnumSet;
|
|
import java.util.UUID;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
/**
|
|
* Wraps another device support instance and supports busy-checking and throttling of events.
|
|
*/
|
|
public class ServiceDeviceSupport implements DeviceSupport {
|
|
|
|
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 lastNotificationTime = 0;
|
|
private String lastNotificationKind;
|
|
private final EnumSet<Flags> flags;
|
|
|
|
public ServiceDeviceSupport(DeviceSupport delegate, EnumSet<Flags> flags) {
|
|
this.delegate = delegate;
|
|
this.flags = flags;
|
|
}
|
|
|
|
@Override
|
|
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
|
|
delegate.setContext(gbDevice, btAdapter, context);
|
|
}
|
|
|
|
@Override
|
|
public boolean isConnected() {
|
|
return delegate.isConnected();
|
|
}
|
|
|
|
@Override
|
|
public boolean connect() {
|
|
return delegate.connect();
|
|
}
|
|
|
|
@Override
|
|
public void setAutoReconnect(boolean enable) {
|
|
delegate.setAutoReconnect(enable);
|
|
}
|
|
|
|
@Override
|
|
public boolean getAutoReconnect() {
|
|
return delegate.getAutoReconnect();
|
|
}
|
|
|
|
@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) {
|
|
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 - lastNotificationTime) < THROTTLING_THRESHOLD) {
|
|
if (notificationKind != null && notificationKind.equals(lastNotificationKind)) {
|
|
LOG.info("Ignoring " + notificationKind + " because of throttling threshold reached");
|
|
return true;
|
|
}
|
|
}
|
|
lastNotificationTime = currentTime;
|
|
lastNotificationKind = notificationKind;
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
|
if (checkBusy("generic notification") || checkThrottle("generic notification")) {
|
|
return;
|
|
}
|
|
delegate.onNotification(notificationSpec);
|
|
}
|
|
|
|
@Override
|
|
public void onSetTime() {
|
|
if (checkBusy("set time") || checkThrottle("set time")) {
|
|
return;
|
|
}
|
|
delegate.onSetTime();
|
|
}
|
|
|
|
// No throttling for the other events
|
|
|
|
@Override
|
|
public void onSetCallState(CallSpec callSpec) {
|
|
if (checkBusy("set call state")) {
|
|
return;
|
|
}
|
|
delegate.onSetCallState(callSpec);
|
|
}
|
|
|
|
@Override
|
|
public void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) {
|
|
if (checkBusy("set canned messages")) {
|
|
return;
|
|
}
|
|
delegate.onSetCannedMessages(cannedMessagesSpec);
|
|
}
|
|
|
|
@Override
|
|
public void onSetMusicState(MusicStateSpec stateSpec) {
|
|
if (checkBusy("set music state")) {
|
|
return;
|
|
}
|
|
delegate.onSetMusicState(stateSpec);
|
|
}
|
|
|
|
@Override
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
|
if (checkBusy("set music info")) {
|
|
return;
|
|
}
|
|
delegate.onSetMusicInfo(musicSpec);
|
|
}
|
|
|
|
@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
|
|
public void onAppStart(UUID uuid, boolean start) {
|
|
if (checkBusy("app start")) {
|
|
return;
|
|
}
|
|
delegate.onAppStart(uuid, start);
|
|
}
|
|
|
|
@Override
|
|
public void onAppDelete(UUID uuid) {
|
|
if (checkBusy("app delete")) {
|
|
return;
|
|
}
|
|
delegate.onAppDelete(uuid);
|
|
}
|
|
|
|
@Override
|
|
public void onAppConfiguration(UUID uuid, String config) {
|
|
if (checkBusy("app configuration")) {
|
|
return;
|
|
}
|
|
delegate.onAppConfiguration(uuid, config);
|
|
}
|
|
|
|
@Override
|
|
public void onAppReorder(UUID[] uuids) {
|
|
if (checkBusy("app reorder")) {
|
|
return;
|
|
}
|
|
delegate.onAppReorder(uuids);
|
|
}
|
|
|
|
@Override
|
|
public void onFetchActivityData() {
|
|
if (checkBusy("fetch activity data")) {
|
|
return;
|
|
}
|
|
delegate.onFetchActivityData();
|
|
}
|
|
|
|
@Override
|
|
public void onReboot() {
|
|
if (checkBusy("reboot")) {
|
|
return;
|
|
}
|
|
delegate.onReboot();
|
|
}
|
|
|
|
@Override
|
|
public void onHeartRateTest() {
|
|
if (checkBusy("heartrate")) {
|
|
return;
|
|
}
|
|
delegate.onHeartRateTest();
|
|
}
|
|
|
|
@Override
|
|
public void onFindDevice(boolean start) {
|
|
if (checkBusy("find device")) {
|
|
return;
|
|
}
|
|
delegate.onFindDevice(start);
|
|
}
|
|
|
|
@Override
|
|
public void onScreenshotReq() {
|
|
if (checkBusy("request screenshot")) {
|
|
return;
|
|
}
|
|
delegate.onScreenshotReq();
|
|
}
|
|
|
|
@Override
|
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
|
if (checkBusy("set alarms")) {
|
|
return;
|
|
}
|
|
delegate.onSetAlarms(alarms);
|
|
}
|
|
|
|
@Override
|
|
public void onEnableRealtimeSteps(boolean enable) {
|
|
if (checkBusy("enable realtime steps: " + enable)) {
|
|
return;
|
|
}
|
|
delegate.onEnableRealtimeSteps(enable);
|
|
}
|
|
|
|
@Override
|
|
public void onEnableHeartRateSleepSupport(boolean enable) {
|
|
if (checkBusy("enable heartrate sleep support: " + enable)) {
|
|
return;
|
|
}
|
|
delegate.onEnableHeartRateSleepSupport(enable);
|
|
}
|
|
|
|
@Override
|
|
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
|
|
if (checkBusy("enable realtime heart rate measurement: " + enable)) {
|
|
return;
|
|
}
|
|
delegate.onEnableRealtimeHeartRateMeasurement(enable);
|
|
}
|
|
|
|
@Override
|
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
|
if (checkBusy("add calendar event")) {
|
|
return;
|
|
}
|
|
delegate.onAddCalendarEvent(calendarEventSpec);
|
|
}
|
|
|
|
@Override
|
|
public void onDeleteCalendarEvent(byte type, long id) {
|
|
if (checkBusy("delete calendar event")) {
|
|
return;
|
|
}
|
|
delegate.onDeleteCalendarEvent(type, id);
|
|
}
|
|
}
|