2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Sebastian
|
|
|
|
Kranz, Yukai Li
|
2020-10-05 05:00:56 +02: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/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.lefun;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
2020-10-05 14:00:59 +02:00
|
|
|
import android.content.Intent;
|
2020-10-05 22:20:20 +02:00
|
|
|
import android.content.SharedPreferences;
|
2020-10-05 05:00:56 +02:00
|
|
|
import android.net.Uri;
|
2020-10-06 17:22:04 +02:00
|
|
|
import android.text.format.DateFormat;
|
2020-10-05 05:00:56 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-10-05 14:00:59 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
2020-10-05 05:00:56 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2020-10-05 11:13:22 +02:00
|
|
|
import java.util.Calendar;
|
2020-10-05 05:00:56 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-10-05 11:13:22 +02:00
|
|
|
import java.util.Queue;
|
2020-10-05 05:00:56 +02:00
|
|
|
import java.util.UUID;
|
2020-10-05 11:13:22 +02:00
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 11:13:22 +02:00
|
|
|
import de.greenrobot.dao.query.Query;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2020-10-05 14:17:42 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.LefunConstants;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.FeaturesCommand;
|
2020-10-05 12:04:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.FindPhoneCommand;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.GetActivityDataCommand;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.GetPpgDataCommand;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.GetSleepDataCommand;
|
2020-10-05 14:00:59 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.GetStepsDataCommand;
|
2020-10-05 11:31:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.PpgResultCommand;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.lefun.commands.SettingsCommand;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.LefunActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.LefunActivitySampleDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.LefunBiometricSample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.LefunSleepSample;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
2020-10-05 14:00:59 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
|
2020-10-05 05:19:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.FindDeviceRequest;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetActivityDataRequest;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetBatteryLevelRequest;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetEnabledFeaturesRequest;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetFirmwareInfoRequest;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetGeneralSettingsRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetHydrationReminderIntervalRequest;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetPpgDataRequest;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetSedentaryReminderIntervalRequest;
|
2020-10-05 11:13:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetSleepDataRequest;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.Request;
|
2020-10-05 13:30:07 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SendCallNotificationRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SendNotificationRequest;
|
2020-10-05 11:51:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetAlarmRequest;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetEnabledFeaturesRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetGeneralSettingsRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetHydrationReminderIntervalRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetLanguageRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetProfileRequest;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetSedentaryReminderIntervalRequest;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetTimeRequest;
|
2020-10-05 11:31:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.StartPpgRequest;
|
2020-10-05 05:00:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2020-10-05 22:20:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Device support class for Lefun devices
|
|
|
|
*/
|
2020-10-05 05:00:56 +02:00
|
|
|
public class LefunDeviceSupport extends AbstractBTLEDeviceSupport {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(LefunDeviceSupport.class);
|
|
|
|
|
|
|
|
private final List<Request> inProgressRequests = Collections.synchronizedList(new ArrayList<Request>());
|
2020-10-05 11:13:22 +02:00
|
|
|
private final Queue<Request> queuedRequests = new ConcurrentLinkedQueue<>();
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 14:00:59 +02:00
|
|
|
private int lastStepsCount = -1;
|
|
|
|
private int lastStepsTimestamp;
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Instantiates a new instance of LefunDeviceSupport
|
|
|
|
*/
|
2020-10-05 05:00:56 +02:00
|
|
|
public LefunDeviceSupport() {
|
|
|
|
super(LOG);
|
|
|
|
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
|
|
|
|
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
|
|
|
|
addSupportedService(GattService.UUID_SERVICE_DEVICE_INFORMATION);
|
|
|
|
addSupportedService(LefunConstants.UUID_SERVICE_LEFUN);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
|
|
|
|
builder.setGattCallback(this);
|
|
|
|
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
|
|
|
|
|
|
|
|
// Enable notification
|
|
|
|
builder.notify(getCharacteristic(LefunConstants.UUID_CHARACTERISTIC_LEFUN_NOTIFY), true);
|
|
|
|
|
2020-10-05 05:19:38 +02:00
|
|
|
// Init device (get version info, battery level, and set time)
|
2020-10-05 05:00:56 +02:00
|
|
|
try {
|
|
|
|
GetFirmwareInfoRequest firmwareReq = new GetFirmwareInfoRequest(this, builder);
|
|
|
|
firmwareReq.perform();
|
|
|
|
inProgressRequests.add(firmwareReq);
|
|
|
|
|
|
|
|
SetTimeRequest timeReq = new SetTimeRequest(this, builder);
|
|
|
|
timeReq.perform();
|
|
|
|
inProgressRequests.add(timeReq);
|
|
|
|
|
|
|
|
GetBatteryLevelRequest batReq = new GetBatteryLevelRequest(this, builder);
|
|
|
|
batReq.perform();
|
|
|
|
inProgressRequests.add(batReq);
|
2020-10-05 22:20:20 +02:00
|
|
|
|
2020-10-06 17:22:04 +02:00
|
|
|
sendAmPmSettingIfNecessary(builder);
|
2020-10-05 22:20:20 +02:00
|
|
|
sendUnitsSetting(builder);
|
|
|
|
sendUserProfile(builder);
|
2020-10-05 05:00:56 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to initialize Lefun device", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean useAutoConnect() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
2020-10-05 13:30:07 +02:00
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetTimeRequest.class.getSimpleName());
|
|
|
|
SendNotificationRequest request = new SendNotificationRequest(this, builder);
|
|
|
|
request.setNotification(notificationSpec);
|
|
|
|
request.perform();
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to send notification", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDeleteNotification(int id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetTime() {
|
2020-10-05 05:19:38 +02:00
|
|
|
try {
|
2020-10-05 11:13:22 +02:00
|
|
|
TransactionBuilder builder = performInitialized(SetTimeRequest.class.getSimpleName());
|
2020-10-05 05:19:38 +02:00
|
|
|
SetTimeRequest request = new SetTimeRequest(this, builder);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
2020-10-05 05:47:48 +02:00
|
|
|
performConnected(builder.getTransaction());
|
2020-10-05 05:19:38 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set time", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
2020-10-05 11:51:41 +02:00
|
|
|
int i = 0;
|
|
|
|
for (Alarm alarm : alarms) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetAlarmRequest.class.getSimpleName());
|
|
|
|
SetAlarmRequest request = new SetAlarmRequest(this, builder);
|
|
|
|
request.setIndex(i);
|
|
|
|
request.setEnabled(alarm.getEnabled());
|
|
|
|
request.setDayOfWeek(alarm.getRepetition());
|
|
|
|
request.setHour(alarm.getHour());
|
|
|
|
request.setMinute(alarm.getMinute());
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set alarm", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetCallState(CallSpec callSpec) {
|
2020-10-05 13:30:07 +02:00
|
|
|
switch (callSpec.command) {
|
|
|
|
case CallSpec.CALL_INCOMING:
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetTimeRequest.class.getSimpleName());
|
|
|
|
SendCallNotificationRequest request = new SendCallNotificationRequest(this, builder);
|
|
|
|
request.setCallNotification(callSpec);
|
|
|
|
request.perform();
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to send call notification", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetMusicState(MusicStateSpec stateSpec) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableRealtimeSteps(boolean enable) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInstallApp(Uri uri) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppInfoReq() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppStart(UUID uuid, boolean start) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppDelete(UUID uuid) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppConfiguration(UUID appUuid, String config, Integer id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppReorder(UUID[] uuids) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchRecordedData(int dataTypes) {
|
2020-10-05 11:13:22 +02:00
|
|
|
if ((dataTypes & RecordedDataTypes.TYPE_ACTIVITY) != 0) {
|
|
|
|
for (int i = 0; i < 7; ++i) {
|
|
|
|
GetActivityDataRequest req = new GetActivityDataRequest(this);
|
|
|
|
req.setDaysAgo(i);
|
|
|
|
queuedRequests.add(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < LefunConstants.PPG_TYPE_COUNT; ++i) {
|
|
|
|
GetPpgDataRequest req = new GetPpgDataRequest(this);
|
|
|
|
req.setPpgType(i);
|
|
|
|
queuedRequests.add(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 7; ++i) {
|
|
|
|
GetSleepDataRequest req = new GetSleepDataRequest(this);
|
|
|
|
req.setDaysAgo(i);
|
|
|
|
queuedRequests.add(req);
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 11:13:22 +02:00
|
|
|
runNextQueuedRequest();
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReset(int flags) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onHeartRateTest() {
|
2020-10-05 11:31:30 +02:00
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(StartPpgRequest.class.getSimpleName());
|
|
|
|
StartPpgRequest request = new StartPpgRequest(this, builder);
|
|
|
|
request.setPpgType(LefunConstants.PPG_TYPE_HEART_RATE);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to start heart rate test", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFindDevice(boolean start) {
|
2020-10-05 05:19:38 +02:00
|
|
|
if (start) {
|
|
|
|
try {
|
2020-10-05 11:13:22 +02:00
|
|
|
TransactionBuilder builder = performInitialized(FindDeviceRequest.class.getSimpleName());
|
2020-10-05 05:19:38 +02:00
|
|
|
FindDeviceRequest request = new FindDeviceRequest(this, builder);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
2020-10-05 05:47:48 +02:00
|
|
|
performConnected(builder.getTransaction());
|
2020-10-05 05:19:38 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to initiate find device", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetConstantVibration(int integer) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScreenshotReq() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableHeartRateSleepSupport(boolean enable) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetHeartRateMeasurementInterval(int seconds) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDeleteCalendarEvent(byte type, long id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSendConfiguration(String config) {
|
2020-10-05 22:20:20 +02:00
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
switch (config) {
|
2020-10-06 17:22:04 +02:00
|
|
|
case DeviceSettingsPreferenceConst.PREF_TIMEFORMAT: {
|
|
|
|
sendAmPmSetting(null);
|
2020-10-05 22:20:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceSettingsPreferenceConst.PREF_LIFTWRIST_NOSHED: {
|
|
|
|
boolean enabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_LIFTWRIST_NOSHED, true);
|
|
|
|
FeaturesCommand features = getCurrentEnabledFeatures();
|
|
|
|
features.setFeature(FeaturesCommand.FEATURE_RAISE_TO_WAKE, enabled);
|
|
|
|
sendEnabledFeaturesSetting(features);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceSettingsPreferenceConst.PREF_ANTILOST_ENABLED: {
|
|
|
|
boolean enabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_ANTILOST_ENABLED, true);
|
|
|
|
FeaturesCommand features = getCurrentEnabledFeatures();
|
|
|
|
features.setFeature(FeaturesCommand.FEATURE_ANTI_LOST, enabled);
|
|
|
|
sendEnabledFeaturesSetting(features);
|
|
|
|
break;
|
|
|
|
}
|
2022-03-07 15:13:23 +01:00
|
|
|
case DeviceSettingsPreferenceConst.PREF_INACTIVITY_ENABLE: {
|
|
|
|
boolean enabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_INACTIVITY_ENABLE, false);
|
2020-10-05 22:20:20 +02:00
|
|
|
FeaturesCommand features = getCurrentEnabledFeatures();
|
|
|
|
features.setFeature(FeaturesCommand.FEATURE_SEDENTARY_REMINDER, enabled);
|
|
|
|
sendEnabledFeaturesSetting(features);
|
|
|
|
break;
|
|
|
|
}
|
2022-03-07 15:13:23 +01:00
|
|
|
case DeviceSettingsPreferenceConst.PREF_INACTIVITY_THRESHOLD: {
|
|
|
|
String periodStr = prefs.getString(DeviceSettingsPreferenceConst.PREF_INACTIVITY_THRESHOLD, "60");
|
2020-10-05 22:20:20 +02:00
|
|
|
try {
|
|
|
|
int period = Integer.parseInt(periodStr);
|
|
|
|
sendSedentaryReminderIntervalSetting(period);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
GB.toast(getContext(), "Invalid sedentary reminder interval value", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceSettingsPreferenceConst.PREF_HYDRATION_SWITCH: {
|
|
|
|
boolean enabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_HYDRATION_SWITCH, false);
|
|
|
|
FeaturesCommand features = getCurrentEnabledFeatures();
|
|
|
|
features.setFeature(FeaturesCommand.FEATURE_HYDRATION_REMINDER, enabled);
|
|
|
|
sendEnabledFeaturesSetting(features);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceSettingsPreferenceConst.PREF_HYDRATION_PERIOD: {
|
|
|
|
String periodStr = prefs.getString(DeviceSettingsPreferenceConst.PREF_HYDRATION_PERIOD, "60");
|
|
|
|
try {
|
|
|
|
int period = Integer.parseInt(periodStr);
|
|
|
|
sendHydrationReminderIntervalSetting(period);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
GB.toast(getContext(), "Invalid sedentary reminder interval value", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SettingsActivity.PREF_MEASUREMENT_SYSTEM: {
|
|
|
|
sendUnitsSetting(null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceSettingsPreferenceConst.PREF_LEFUN_INTERFACE_LANGUAGE: {
|
|
|
|
String value = prefs.getString(DeviceSettingsPreferenceConst.PREF_LEFUN_INTERFACE_LANGUAGE, "0");
|
|
|
|
int intValue = Integer.parseInt(value);
|
|
|
|
sendLanguageSetting((byte) intValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends unit of measurement to the device
|
|
|
|
*
|
|
|
|
* @param builder the transaction builder to append to
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendUnitsSetting(TransactionBuilder builder) {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
String units = prefs.getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM,
|
|
|
|
getContext().getString(R.string.p_unit_metric));
|
|
|
|
|
|
|
|
byte lefunUnits;
|
|
|
|
if (getContext().getString(R.string.p_unit_metric).equals(units)) {
|
|
|
|
lefunUnits = SettingsCommand.MEASUREMENT_UNIT_METRIC;
|
|
|
|
} else {
|
|
|
|
lefunUnits = SettingsCommand.MEASUREMENT_UNIT_IMPERIAL;
|
|
|
|
}
|
|
|
|
sendGeneralSettings(builder, (byte) 0xff, lefunUnits);
|
|
|
|
}
|
|
|
|
|
2020-10-06 17:22:04 +02:00
|
|
|
/**
|
|
|
|
* Send AM/PM indicator setting based on time format pref
|
|
|
|
*
|
|
|
|
* @param builder the transaction builder to append to
|
|
|
|
*/
|
|
|
|
private void sendAmPmSetting(TransactionBuilder builder) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
String ampmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
|
|
|
|
getContext().getString(R.string.p_timeformat_auto));
|
|
|
|
|
|
|
|
byte ampmDeviceSetting = (byte) 0xff;
|
|
|
|
if (getContext().getString(R.string.p_timeformat_auto).equals(ampmSetting)) {
|
|
|
|
if (DateFormat.is24HourFormat(getContext())) {
|
|
|
|
ampmDeviceSetting = SettingsCommand.AM_PM_24_HOUR;
|
|
|
|
} else {
|
|
|
|
ampmDeviceSetting = SettingsCommand.AM_PM_12_HOUR;
|
|
|
|
}
|
|
|
|
} else if (getContext().getString(R.string.p_timeformat_24h).equals(ampmSetting)) {
|
|
|
|
ampmDeviceSetting = SettingsCommand.AM_PM_24_HOUR;
|
|
|
|
} else if (getContext().getString(R.string.p_timeformat_am_pm).equals(ampmSetting)) {
|
|
|
|
ampmDeviceSetting = SettingsCommand.AM_PM_12_HOUR;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendGeneralSettings(builder, ampmDeviceSetting, (byte) 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send AM/PM indicator setting only if time format pref is set to auto
|
|
|
|
*
|
|
|
|
* @param builder the transaction builder to append to
|
|
|
|
*/
|
|
|
|
private void sendAmPmSettingIfNecessary(TransactionBuilder builder) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
String ampmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
|
|
|
|
getContext().getString(R.string.p_timeformat_auto));
|
|
|
|
|
|
|
|
if (getContext().getString(R.string.p_timeformat_auto).equals(ampmSetting)) {
|
|
|
|
sendAmPmSetting(builder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Gets a features command with the currently enabled features set
|
|
|
|
*
|
|
|
|
* @return the features command
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private FeaturesCommand getCurrentEnabledFeatures() {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
boolean raiseToWakeEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_LIFTWRIST_NOSHED, true);
|
|
|
|
boolean antilostEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_ANTILOST_ENABLED, true);
|
2022-03-07 15:13:23 +01:00
|
|
|
boolean sedentaryEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_INACTIVITY_ENABLE, false);
|
2020-10-05 22:20:20 +02:00
|
|
|
boolean hydrationEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_HYDRATION_SWITCH, false);
|
|
|
|
|
|
|
|
FeaturesCommand cmd = new FeaturesCommand();
|
|
|
|
cmd.setFeature(FeaturesCommand.FEATURE_RAISE_TO_WAKE, raiseToWakeEnabled);
|
|
|
|
cmd.setFeature(FeaturesCommand.FEATURE_ANTI_LOST, antilostEnabled);
|
|
|
|
cmd.setFeature(FeaturesCommand.FEATURE_SEDENTARY_REMINDER, sedentaryEnabled);
|
|
|
|
cmd.setFeature(FeaturesCommand.FEATURE_HYDRATION_REMINDER, hydrationEnabled);
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends general settings to the device
|
|
|
|
*
|
|
|
|
* @param builder the transaction builder to append to
|
|
|
|
* @param amPm AM/PM indicator setting
|
|
|
|
* @param units units of measurement setting
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendGeneralSettings(TransactionBuilder builder, byte amPm, byte units) {
|
|
|
|
boolean givenBuilder = builder != null;
|
|
|
|
try {
|
|
|
|
if (!givenBuilder)
|
|
|
|
builder = performInitialized(SetGeneralSettingsRequest.class.getSimpleName());
|
|
|
|
SetGeneralSettingsRequest request = new SetGeneralSettingsRequest(this, builder);
|
|
|
|
request.setAmPm(amPm);
|
|
|
|
request.setUnits(units);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
if (!givenBuilder)
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set settings", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends the user profile to the device
|
|
|
|
*
|
|
|
|
* @param builder the transaction builder to append to
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendUserProfile(TransactionBuilder builder) {
|
|
|
|
boolean givenBuilder = builder != null;
|
|
|
|
try {
|
|
|
|
if (!givenBuilder)
|
|
|
|
builder = performInitialized(SetProfileRequest.class.getSimpleName());
|
|
|
|
SetProfileRequest request = new SetProfileRequest(this, builder);
|
|
|
|
ActivityUser user = new ActivityUser();
|
|
|
|
request.setUser(user);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
if (!givenBuilder)
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to send profile", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends enabled features settings to the device
|
|
|
|
*
|
|
|
|
* @param cmd the features command to send
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendEnabledFeaturesSetting(FeaturesCommand cmd) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetEnabledFeaturesRequest.class.getSimpleName());
|
|
|
|
SetEnabledFeaturesRequest request = new SetEnabledFeaturesRequest(this, builder);
|
|
|
|
request.setCmd(cmd);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set enabled features", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends the sedentary reminder interval setting to the device
|
|
|
|
*
|
|
|
|
* @param period the reminder interval
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendSedentaryReminderIntervalSetting(int period) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetSedentaryReminderIntervalRequest.class.getSimpleName());
|
|
|
|
SetSedentaryReminderIntervalRequest request = new SetSedentaryReminderIntervalRequest(this, builder);
|
|
|
|
request.setInterval(period);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set sedentary reminder interval", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends the hydration reminder interval setting to the device
|
|
|
|
*
|
|
|
|
* @param period the reminder interval
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendHydrationReminderIntervalSetting(int period) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetHydrationReminderIntervalRequest.class.getSimpleName());
|
|
|
|
SetHydrationReminderIntervalRequest request = new SetHydrationReminderIntervalRequest(this, builder);
|
|
|
|
request.setInterval(period);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set hydration reminder interval", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Sends the language selection to the device
|
|
|
|
*
|
|
|
|
* @param language the language selection
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
private void sendLanguageSetting(byte language) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized(SetLanguageRequest.class.getSimpleName());
|
|
|
|
SetLanguageRequest request = new SetLanguageRequest(this, builder);
|
|
|
|
request.setLanguage(language);
|
|
|
|
request.perform();
|
|
|
|
inProgressRequests.add(request);
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to set language", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Stores received general settings to prefs
|
|
|
|
*
|
|
|
|
* @param amPm AM/PM indicator setting
|
|
|
|
* @param units units of measurement setting
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
public void receiveGeneralSettings(int amPm, int units) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
boolean ampmEnabled = amPm == SettingsCommand.AM_PM_12_HOUR;
|
2020-10-06 17:22:04 +02:00
|
|
|
String currAmpmSetting = prefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT,
|
|
|
|
getContext().getString(R.string.p_timeformat_auto));
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
|
|
|
|
// Only update AM/PM indicator setting if it is not currently set to auto
|
|
|
|
if (!getContext().getString(R.string.p_timeformat_auto).equals(currAmpmSetting)) {
|
|
|
|
String ampmValue = getContext().getString(ampmEnabled ? R.string.p_timeformat_am_pm
|
|
|
|
: R.string.p_timeformat_24h);
|
|
|
|
editor.putString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT, ampmValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.apply();
|
2020-10-05 22:20:20 +02:00
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Stores received enabled features settings to prefs
|
|
|
|
*
|
|
|
|
* @param cmd the features command
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
public void receiveEnabledFeaturesSetting(FeaturesCommand cmd) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
prefs.edit()
|
|
|
|
.putBoolean(DeviceSettingsPreferenceConst.PREF_LIFTWRIST_NOSHED,
|
|
|
|
cmd.getFeature(FeaturesCommand.FEATURE_RAISE_TO_WAKE))
|
2022-03-07 15:13:23 +01:00
|
|
|
.putBoolean(DeviceSettingsPreferenceConst.PREF_INACTIVITY_ENABLE,
|
2020-10-05 22:20:20 +02:00
|
|
|
cmd.getFeature(FeaturesCommand.FEATURE_SEDENTARY_REMINDER))
|
|
|
|
.putBoolean(DeviceSettingsPreferenceConst.PREF_HYDRATION_SWITCH,
|
|
|
|
cmd.getFeature(FeaturesCommand.FEATURE_HYDRATION_REMINDER))
|
|
|
|
.putBoolean(DeviceSettingsPreferenceConst.PREF_ANTILOST_ENABLED,
|
|
|
|
cmd.getFeature(FeaturesCommand.FEATURE_ANTI_LOST))
|
|
|
|
.apply();
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Stores received sedentary reminder interval setting to prefs
|
|
|
|
*
|
|
|
|
* @param period the interval
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
public void receiveSedentaryReminderIntervalSetting(int period) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
prefs.edit()
|
2022-03-07 15:13:23 +01:00
|
|
|
.putString(DeviceSettingsPreferenceConst.PREF_INACTIVITY_THRESHOLD, String.valueOf(period))
|
2020-10-05 22:20:20 +02:00
|
|
|
.apply();
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Stores received hydration reminder interval setting to prefs
|
|
|
|
*
|
|
|
|
* @param period the interval
|
|
|
|
*/
|
2020-10-05 22:20:20 +02:00
|
|
|
public void receiveHydrationReminderIntervalSetting(int period) {
|
|
|
|
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
|
|
|
|
prefs.edit()
|
|
|
|
.putString(DeviceSettingsPreferenceConst.PREF_HYDRATION_PERIOD, String.valueOf(period))
|
|
|
|
.apply();
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReadConfiguration(String config) {
|
2020-10-05 22:20:20 +02:00
|
|
|
// Just going to read all the settings
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized("Read settings");
|
|
|
|
|
|
|
|
GetGeneralSettingsRequest getGeneralSettingsRequest
|
|
|
|
= new GetGeneralSettingsRequest(this, builder);
|
|
|
|
getGeneralSettingsRequest.perform();
|
|
|
|
inProgressRequests.add(getGeneralSettingsRequest);
|
|
|
|
|
|
|
|
GetEnabledFeaturesRequest getEnabledFeaturesRequest
|
|
|
|
= new GetEnabledFeaturesRequest(this, builder);
|
|
|
|
getEnabledFeaturesRequest.perform();
|
|
|
|
inProgressRequests.add(getEnabledFeaturesRequest);
|
2020-10-05 05:00:56 +02:00
|
|
|
|
2020-10-05 22:20:20 +02:00
|
|
|
GetSedentaryReminderIntervalRequest getSedentaryReminderIntervalRequest
|
|
|
|
= new GetSedentaryReminderIntervalRequest(this, builder);
|
|
|
|
getSedentaryReminderIntervalRequest.perform();
|
|
|
|
inProgressRequests.add(getSedentaryReminderIntervalRequest);
|
|
|
|
|
|
|
|
GetHydrationReminderIntervalRequest getHydrationReminderIntervalRequest
|
|
|
|
= new GetHydrationReminderIntervalRequest(this, builder);
|
|
|
|
getHydrationReminderIntervalRequest.perform();
|
|
|
|
inProgressRequests.add(getHydrationReminderIntervalRequest);
|
|
|
|
|
|
|
|
performConnected(builder.getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to retrieve settings", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTestNewFunction() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSendWeather(WeatherSpec weatherSpec) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
|
|
if (characteristic.getUuid().equals(LefunConstants.UUID_CHARACTERISTIC_LEFUN_NOTIFY)) {
|
|
|
|
byte[] data = characteristic.getValue();
|
|
|
|
// Parse response
|
|
|
|
if (data.length >= LefunConstants.CMD_HEADER_LENGTH && data[0] == LefunConstants.CMD_RESPONSE_ID) {
|
|
|
|
// Note: full validation is done within the request
|
|
|
|
byte commandId = data[2];
|
|
|
|
synchronized (inProgressRequests) {
|
|
|
|
for (Request req : inProgressRequests) {
|
|
|
|
if (req.expectsResponse() && req.getCommandId() == commandId) {
|
|
|
|
try {
|
|
|
|
req.handleResponse(data);
|
2020-10-05 05:47:27 +02:00
|
|
|
if (req.shouldRemoveAfterHandling())
|
|
|
|
inProgressRequests.remove(req);
|
2020-10-05 05:00:56 +02:00
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
LOG.error("Failed to handle response", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:31:30 +02:00
|
|
|
if (handleAsynchronousResponse(commandId, data))
|
2020-10-05 05:00:56 +02:00
|
|
|
return true;
|
|
|
|
|
2020-10-05 11:13:22 +02:00
|
|
|
logMessageContent(data);
|
2020-10-05 05:00:56 +02:00
|
|
|
LOG.error(String.format("No handler for response 0x%02x", commandId));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:13:22 +02:00
|
|
|
logMessageContent(data);
|
2020-10-05 05:00:56 +02:00
|
|
|
LOG.error("Invalid response received");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onCharacteristicChanged(gatt, characteristic);
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Handles commands from the device that are not typically associated with a request
|
|
|
|
*
|
|
|
|
* @param commandId the command ID
|
|
|
|
* @param data the entire response
|
|
|
|
* @return whether the response has been handled
|
|
|
|
*/
|
2020-10-05 11:31:30 +02:00
|
|
|
private boolean handleAsynchronousResponse(byte commandId, byte[] data) {
|
2020-10-05 05:00:56 +02:00
|
|
|
// Assume data already checked for correct response code and length
|
2020-10-05 11:31:30 +02:00
|
|
|
switch (commandId) {
|
|
|
|
case LefunConstants.CMD_PPG_RESULT:
|
|
|
|
return handleAsynchronousPpgResult(data);
|
2020-10-05 12:04:28 +02:00
|
|
|
case LefunConstants.CMD_FIND_PHONE:
|
|
|
|
return handleAntiLoss(data);
|
2020-10-05 14:00:59 +02:00
|
|
|
case LefunConstants.CMD_STEPS_DATA:
|
|
|
|
return handleAsynchronousActivity(data);
|
2020-10-05 11:31:30 +02:00
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Handles live steps data
|
|
|
|
*
|
|
|
|
* @param data the response
|
|
|
|
* @return whether the response has been handled
|
|
|
|
*/
|
2020-10-05 14:00:59 +02:00
|
|
|
private boolean handleAsynchronousActivity(byte[] data) {
|
|
|
|
try {
|
|
|
|
GetStepsDataCommand cmd = new GetStepsDataCommand();
|
|
|
|
cmd.deserialize(data);
|
|
|
|
broadcastSample(cmd);
|
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
LOG.error("Failed to handle live activity update", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adapted from nodomain.freeyourgadget.gadgetbridge.service.devices.makibeshr3.MakibesHR3DeviceSupport.broadcastSample
|
2020-10-05 23:08:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcasts live sample
|
|
|
|
*
|
|
|
|
* @param command the steps data
|
|
|
|
*/
|
2020-10-05 14:00:59 +02:00
|
|
|
private void broadcastSample(GetStepsDataCommand command) {
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
int timestamp = (int) (now.getTimeInMillis() / 1000);
|
|
|
|
// Workaround for a world where sub-second time resolution is not a thing
|
|
|
|
if (lastStepsTimestamp == timestamp) return;
|
|
|
|
lastStepsTimestamp = timestamp;
|
|
|
|
LefunActivitySample sample = new LefunActivitySample();
|
|
|
|
sample.setTimestamp(timestamp);
|
|
|
|
if (lastStepsCount == -1 || command.getSteps() < lastStepsCount) {
|
|
|
|
lastStepsCount = command.getSteps();
|
|
|
|
}
|
|
|
|
int diff = command.getSteps() - lastStepsCount;
|
|
|
|
sample.setSteps(diff);
|
|
|
|
lastStepsCount = command.getSteps();
|
|
|
|
Intent intent = new Intent(DeviceService.ACTION_REALTIME_SAMPLES)
|
|
|
|
.putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample)
|
|
|
|
.putExtra(DeviceService.EXTRA_TIMESTAMP, sample.getTimestamp());
|
|
|
|
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Handles PPG result from earlier request
|
|
|
|
*
|
|
|
|
* @param data the response
|
|
|
|
* @return whether the response has been handled
|
|
|
|
*/
|
2020-10-05 11:31:30 +02:00
|
|
|
private boolean handleAsynchronousPpgResult(byte[] data) {
|
|
|
|
try {
|
|
|
|
PpgResultCommand cmd = new PpgResultCommand();
|
|
|
|
cmd.deserialize(data);
|
|
|
|
handlePpgData(cmd);
|
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException e) {
|
2020-10-05 14:00:59 +02:00
|
|
|
LOG.error("Failed to PPG result", e);
|
2020-10-05 11:31:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Handles find phone request
|
|
|
|
*
|
|
|
|
* @param data the response
|
|
|
|
* @return whether the response has been handled
|
|
|
|
*/
|
2020-10-05 12:04:28 +02:00
|
|
|
private boolean handleAntiLoss(byte[] data) {
|
|
|
|
try {
|
|
|
|
FindPhoneCommand cmd = new FindPhoneCommand();
|
|
|
|
cmd.deserialize(data);
|
2020-10-05 14:17:42 +02:00
|
|
|
GBDeviceEventFindPhone event = new GBDeviceEventFindPhone();
|
|
|
|
event.event = GBDeviceEventFindPhone.Event.START;
|
|
|
|
evaluateGBDeviceEvent(event);
|
2020-10-05 12:04:28 +02:00
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
LOG.error("Failed to handle anti-loss", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Callback when device info has been obtained
|
|
|
|
*/
|
2020-10-05 05:00:56 +02:00
|
|
|
public void completeInitialization() {
|
|
|
|
gbDevice.setState(GBDevice.State.INITIALIZED);
|
|
|
|
gbDevice.sendDeviceUpdateIntent(getContext());
|
2020-10-05 22:20:20 +02:00
|
|
|
onReadConfiguration("");
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|
2020-10-05 11:13:22 +02:00
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Converts Lefun datetime format to Unix timestamp
|
|
|
|
*
|
|
|
|
* @param year the year (2 digits based on 2000)
|
|
|
|
* @param month the month
|
|
|
|
* @param day the day
|
|
|
|
* @param hour the hour
|
|
|
|
* @param minute the minute
|
|
|
|
* @param second the second
|
|
|
|
* @return Unix timestamp of the datetime
|
|
|
|
*/
|
2020-10-05 11:13:22 +02:00
|
|
|
private int dateToTimestamp(byte year, byte month, byte day, byte hour, byte minute, byte second) {
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.set(
|
|
|
|
((int) year & 0xff) + 2000,
|
|
|
|
((int) month & 0xff) - 1,
|
|
|
|
(int) day,
|
|
|
|
(int) hour,
|
|
|
|
(int) minute,
|
|
|
|
(int) second
|
|
|
|
);
|
|
|
|
return (int) (calendar.getTimeInMillis() / 1000);
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Fetches an activity sample given the timestamp
|
|
|
|
*
|
|
|
|
* @param session DAO session
|
|
|
|
* @param timestamp the timestamp
|
|
|
|
* @return fetched activity or null if none exists
|
|
|
|
*/
|
2020-10-05 11:13:22 +02:00
|
|
|
private LefunActivitySample getActivitySample(DaoSession session, int timestamp) {
|
|
|
|
LefunActivitySampleDao dao = session.getLefunActivitySampleDao();
|
|
|
|
Long userId = DBHelper.getUser(session).getId();
|
|
|
|
Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
|
|
|
|
Query<LefunActivitySample> q = dao.queryBuilder()
|
|
|
|
.where(LefunActivitySampleDao.Properties.Timestamp.eq(timestamp))
|
|
|
|
.where(LefunActivitySampleDao.Properties.DeviceId.eq(deviceId))
|
|
|
|
.where(LefunActivitySampleDao.Properties.UserId.eq(userId))
|
|
|
|
.build();
|
|
|
|
return q.unique();
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Processes activity data and stores it
|
|
|
|
*
|
|
|
|
* @param command the activity data
|
|
|
|
*/
|
2020-10-05 11:13:22 +02:00
|
|
|
public void handleActivityData(GetActivityDataCommand command) {
|
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
DaoSession session = handler.getDaoSession();
|
|
|
|
int timestamp = dateToTimestamp(command.getYear(), command.getMonth(), command.getDay(),
|
|
|
|
command.getHour(), command.getMinute(), (byte) 0);
|
|
|
|
// For the most part I'm ignoring the sample provider, because it doesn't really help
|
|
|
|
// when I need to combine sample data instead of replacing
|
|
|
|
LefunActivitySample sample = getActivitySample(session, timestamp);
|
|
|
|
if (sample == null) {
|
|
|
|
sample = new LefunActivitySample(timestamp,
|
|
|
|
DBHelper.getDevice(getDevice(), session).getId());
|
|
|
|
sample.setUserId(DBHelper.getUser(session).getId());
|
|
|
|
sample.setRawKind(LefunConstants.DB_ACTIVITY_KIND_ACTIVITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
sample.setSteps(command.getSteps());
|
|
|
|
sample.setDistance(command.getDistance());
|
|
|
|
sample.setCalories(command.getCalories());
|
|
|
|
sample.setRawIntensity(LefunConstants.INTENSITY_AWAKE);
|
|
|
|
|
|
|
|
session.getLefunActivitySampleDao().insertOrReplace(sample);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("Error handling activity data", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Processes PPG data and stores it
|
|
|
|
*
|
|
|
|
* @param timestamp the timestamp
|
|
|
|
* @param ppgType the PPG type
|
|
|
|
* @param ppgData the data from the PPG operation
|
|
|
|
*/
|
2020-10-05 11:31:30 +02:00
|
|
|
private void handlePpgData(int timestamp, int ppgType, byte[] ppgData) {
|
2020-10-05 11:13:22 +02:00
|
|
|
int ppgData0 = ppgData[0] & 0xff;
|
|
|
|
int ppgData1 = ppgData.length > 1 ? ppgData[1] & 0xff : 0;
|
|
|
|
|
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
DaoSession session = handler.getDaoSession();
|
|
|
|
|
2020-10-05 11:31:30 +02:00
|
|
|
if (ppgType == LefunConstants.PPG_TYPE_HEART_RATE) {
|
2020-10-05 11:13:22 +02:00
|
|
|
LefunActivitySample sample = getActivitySample(session, timestamp);
|
|
|
|
if (sample == null) {
|
|
|
|
sample = new LefunActivitySample(timestamp,
|
|
|
|
DBHelper.getDevice(getDevice(), session).getId());
|
|
|
|
sample.setUserId(DBHelper.getUser(session).getId());
|
|
|
|
sample.setRawKind(LefunConstants.DB_ACTIVITY_KIND_HEART_RATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
sample.setHeartRate(ppgData0);
|
|
|
|
|
|
|
|
session.getLefunActivitySampleDao().insertOrReplace(sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
LefunBiometricSample bioSample = new LefunBiometricSample(timestamp,
|
|
|
|
DBHelper.getDevice(getDevice(), session).getId());
|
|
|
|
bioSample.setUserId(DBHelper.getUser(session).getId());
|
2020-10-05 11:31:30 +02:00
|
|
|
bioSample.setType(ppgType);
|
2020-10-05 11:13:22 +02:00
|
|
|
bioSample.setValue1(ppgData0);
|
|
|
|
bioSample.setValue2(ppgData1);
|
|
|
|
session.getLefunBiometricSampleDao().insertOrReplace(bioSample);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("Error handling PPG data", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Processes PPG data from bulk get operation
|
|
|
|
*
|
|
|
|
* @param command the PPG data
|
|
|
|
*/
|
2020-10-05 11:31:30 +02:00
|
|
|
public void handlePpgData(GetPpgDataCommand command) {
|
|
|
|
int timestamp = dateToTimestamp(command.getYear(), command.getMonth(), command.getDay(),
|
|
|
|
command.getHour(), command.getMinute(), command.getSecond());
|
|
|
|
int ppgType = command.getPpgType();
|
|
|
|
byte[] ppgData = command.getPpgData();
|
|
|
|
handlePpgData(timestamp, ppgType, ppgData);
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Processes PPG result received as a result of requesting PPG operation
|
|
|
|
*
|
|
|
|
* @param command the PPG result
|
|
|
|
*/
|
2020-10-05 11:31:30 +02:00
|
|
|
public void handlePpgData(PpgResultCommand command) {
|
|
|
|
int timestamp = (int) (Calendar.getInstance().getTimeInMillis() / 1000);
|
|
|
|
int ppgType = command.getPpgType();
|
|
|
|
byte[] ppgData = command.getPpgData();
|
|
|
|
handlePpgData(timestamp, ppgType, ppgData);
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Processes bulk sleep data
|
|
|
|
*
|
|
|
|
* @param command the sleep data
|
|
|
|
*/
|
2020-10-05 11:13:22 +02:00
|
|
|
public void handleSleepData(GetSleepDataCommand command) {
|
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
DaoSession session = handler.getDaoSession();
|
|
|
|
int timestamp = dateToTimestamp(command.getYear(), command.getMonth(), command.getDay(),
|
|
|
|
command.getHour(), command.getMinute(), (byte) 0);
|
|
|
|
|
|
|
|
LefunActivitySample sample = getActivitySample(session, timestamp);
|
|
|
|
if (sample == null) {
|
|
|
|
sample = new LefunActivitySample(timestamp,
|
|
|
|
DBHelper.getDevice(getDevice(), session).getId());
|
|
|
|
sample.setUserId(DBHelper.getUser(session).getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
int rawKind;
|
|
|
|
int intensity;
|
|
|
|
switch (command.getSleepType()) {
|
|
|
|
case GetSleepDataCommand.SLEEP_TYPE_AWAKE:
|
|
|
|
rawKind = LefunConstants.DB_ACTIVITY_KIND_ACTIVITY;
|
|
|
|
intensity = LefunConstants.INTENSITY_AWAKE;
|
|
|
|
break;
|
|
|
|
case GetSleepDataCommand.SLEEP_TYPE_LIGHT_SLEEP:
|
|
|
|
rawKind = LefunConstants.DB_ACTIVITY_KIND_LIGHT_SLEEP;
|
|
|
|
intensity = LefunConstants.INTENSITY_LIGHT_SLEEP;
|
|
|
|
break;
|
|
|
|
case GetSleepDataCommand.SLEEP_TYPE_DEEP_SLEEP:
|
|
|
|
rawKind = LefunConstants.DB_ACTIVITY_KIND_DEEP_SLEEP;
|
|
|
|
intensity = LefunConstants.INTENSITY_DEEP_SLEEP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rawKind = LefunConstants.DB_ACTIVITY_KIND_UNKNOWN;
|
|
|
|
intensity = LefunConstants.INTENSITY_AWAKE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sample.setRawKind(rawKind);
|
|
|
|
sample.setRawIntensity(intensity);
|
|
|
|
|
|
|
|
session.getLefunActivitySampleDao().insertOrReplace(sample);
|
|
|
|
|
|
|
|
LefunSleepSample sleepSample = new LefunSleepSample(timestamp,
|
|
|
|
DBHelper.getDevice(getDevice(), session).getId());
|
|
|
|
sleepSample.setUserId(DBHelper.getUser(session).getId());
|
|
|
|
sleepSample.setType(command.getSleepType());
|
|
|
|
session.getLefunSleepSampleDao().insertOrReplace(sleepSample);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("Error handling sleep data", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:08:11 +02:00
|
|
|
/**
|
|
|
|
* Runs the next queued request
|
|
|
|
*/
|
2020-10-05 11:13:22 +02:00
|
|
|
public void runNextQueuedRequest() {
|
|
|
|
Request request = queuedRequests.poll();
|
|
|
|
if (request != null) {
|
|
|
|
try {
|
|
|
|
request.perform();
|
|
|
|
if (!request.isSelfQueue())
|
|
|
|
performConnected(request.getTransactionBuilder().getTransaction());
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getContext(), "Failed to run next queued request", Toast.LENGTH_SHORT,
|
|
|
|
GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 05:00:56 +02:00
|
|
|
}
|