2019-02-13 20:43:30 +01:00
|
|
|
/* Copyright (C) 2015-2019 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, José Rebelo, Julien Pivotto, Kasha, Steffen Liebergeld
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
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;
|
2016-05-16 17:30:11 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
2016-06-24 10:25:08 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-08 20:27:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2016-12-31 15:56:05 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
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();
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:33:19 +02:00
|
|
|
@Override
|
|
|
|
public boolean connectFirstTime() {
|
|
|
|
return delegate.connectFirstTime();
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
@Override
|
|
|
|
public boolean connect() {
|
|
|
|
return delegate.connect();
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:17:13 +02:00
|
|
|
@Override
|
|
|
|
public void setAutoReconnect(boolean enable) {
|
|
|
|
delegate.setAutoReconnect(enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean getAutoReconnect() {
|
|
|
|
return delegate.getAutoReconnect();
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
@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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-09 16:33:00 +01:00
|
|
|
@Override
|
|
|
|
public void onDeleteNotification(int id) {
|
|
|
|
delegate.onDeleteNotification(id);
|
|
|
|
}
|
|
|
|
|
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
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetCallState(CallSpec callSpec) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("set call state")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-04 20:08:34 +02:00
|
|
|
delegate.onSetCallState(callSpec);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:25:08 +02:00
|
|
|
@Override
|
|
|
|
public void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) {
|
|
|
|
if (checkBusy("set canned messages")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetCannedMessages(cannedMessagesSpec);
|
|
|
|
}
|
|
|
|
|
2016-06-08 20:27:25 +02:00
|
|
|
@Override
|
|
|
|
public void onSetMusicState(MusicStateSpec stateSpec) {
|
|
|
|
if (checkBusy("set music state")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetMusicState(stateSpec);
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("set music info")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-04 20:08:34 +02:00
|
|
|
delegate.onSetMusicInfo(musicSpec);
|
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
|
2017-08-01 00:03:28 +02:00
|
|
|
public void onAppConfiguration(UUID uuid, String config, Integer id) {
|
2016-03-03 14:23:17 +01:00
|
|
|
if (checkBusy("app configuration")) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-01 00:03:28 +02:00
|
|
|
delegate.onAppConfiguration(uuid, config, id);
|
2016-03-03 14:23:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 01:20:12 +02:00
|
|
|
@Override
|
|
|
|
public void onAppReorder(UUID[] uuids) {
|
|
|
|
if (checkBusy("app reorder")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onAppReorder(uuids);
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:54:34 +02:00
|
|
|
@Override
|
2018-03-31 16:21:25 +02:00
|
|
|
public void onFetchRecordedData(int dataTypes) {
|
2015-06-06 23:54:34 +02:00
|
|
|
if (checkBusy("fetch activity data")) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-31 16:21:25 +02:00
|
|
|
delegate.onFetchRecordedData(dataTypes);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-16 16:05:13 +01:00
|
|
|
public void onReset(int flags) {
|
|
|
|
if (checkBusy("reset")) {
|
2015-06-06 23:54:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-16 16:05:13 +01:00
|
|
|
delegate.onReset(flags);
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|
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
|
|
|
|
2016-09-20 20:28:52 +02:00
|
|
|
@Override
|
|
|
|
public void onSetConstantVibration(int intensity) {
|
|
|
|
if (checkBusy("set constant vibration")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetConstantVibration(intensity);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2016-04-03 22:38:06 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableHeartRateSleepSupport(boolean enable) {
|
2017-11-11 00:04:51 +01:00
|
|
|
if (checkBusy("enable heart rate sleep support: " + enable)) {
|
2016-04-03 22:38:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onEnableHeartRateSleepSupport(enable);
|
|
|
|
}
|
2016-04-12 23:12:15 +02:00
|
|
|
|
2017-11-11 00:04:51 +01:00
|
|
|
@Override
|
|
|
|
public void onSetHeartRateMeasurementInterval(int seconds) {
|
|
|
|
if (checkBusy("set heart rate measurement interval: " + seconds + "s")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetHeartRateMeasurementInterval(seconds);
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
@Override
|
|
|
|
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
|
|
|
|
if (checkBusy("enable realtime heart rate measurement: " + enable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onEnableRealtimeHeartRateMeasurement(enable);
|
|
|
|
}
|
2016-05-16 17:30:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
|
|
|
if (checkBusy("add calendar event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onAddCalendarEvent(calendarEventSpec);
|
|
|
|
}
|
2016-05-16 23:37:40 +02:00
|
|
|
|
|
|
|
@Override
|
2016-05-24 13:11:57 +02:00
|
|
|
public void onDeleteCalendarEvent(byte type, long id) {
|
2016-05-16 23:37:40 +02:00
|
|
|
if (checkBusy("delete calendar event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onDeleteCalendarEvent(type, id);
|
|
|
|
}
|
2016-10-11 23:06:59 +02:00
|
|
|
|
2016-11-13 20:47:24 +01:00
|
|
|
@Override
|
|
|
|
public void onSendConfiguration(String config) {
|
|
|
|
if (checkBusy("send configuration: " + config)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSendConfiguration(config);
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:09:21 +01:00
|
|
|
@Override
|
|
|
|
public void onReadConfiguration(String config) {
|
|
|
|
if (checkBusy("read configuration: " + config)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onReadConfiguration(config);
|
|
|
|
}
|
|
|
|
|
2016-10-11 23:06:59 +02:00
|
|
|
@Override
|
|
|
|
public void onTestNewFunction() {
|
|
|
|
if (checkBusy("test new function event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onTestNewFunction();
|
|
|
|
}
|
2016-12-31 15:56:05 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSendWeather(WeatherSpec weatherSpec) {
|
|
|
|
if (checkBusy("send weather event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSendWeather(weatherSpec);
|
|
|
|
}
|
2018-07-28 17:23:58 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetFmFrequency(float frequency) {
|
|
|
|
if (checkBusy("set frequency event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetFmFrequency(frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetLedColor(int color) {
|
|
|
|
if (checkBusy("set led color event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delegate.onSetLedColor(color);
|
|
|
|
}
|
2015-06-06 23:54:34 +02:00
|
|
|
}
|