Garmin: experimental settings for wake / sleep times (#4331)

Experiment to support setting typical wake / sleep times for devices without realtime settings.

Settings / Developer options / Experimental settings
-> enable

Device specific setting / Experimental settings
-> wake time
-> sleep time
-> send
This commit is contained in:
Thomas Kuehne
2026-05-02 21:27:55 +00:00
parent e5bd136385
commit 72dcc1db2c
2 changed files with 96 additions and 1 deletions
@@ -1,3 +1,19 @@
/* Copyright (C) 2024-2026 kuhy, José Rebelo, Thomas Kuehne
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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.garmin;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.toast;
@@ -7,6 +23,7 @@ import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.widget.Toast;
@@ -25,6 +42,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@@ -36,18 +54,26 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.FileType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.agps.GarminAgpsStatus;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.FitAsyncProcessor;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.FitFile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordData;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.messages.FitFileId;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.messages.FitUserProfile;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.notifications.GBProgressNotification;
import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs;
public class GarminSettingsCustomizer implements DeviceSpecificSettingsCustomizer {
private static final Logger LOG = LoggerFactory.getLogger(GarminSettingsCustomizer.class);
@@ -247,6 +273,60 @@ public class GarminSettingsCustomizer implements DeviceSpecificSettingsCustomize
url2.setSummary("dciwx.com");
blacklistedDomains.addPreference(url2);
}
final Preference prefSleepSend = handler.findPreference("garmin_experimental_sleep_send");
if (prefSleepSend != null) {
prefSleepSend.setOnPreferenceClickListener(dummy -> sendSleep(handler));
}
}
private boolean sendSleep(DeviceSpecificSettingsHandler handler) {
final GBDevice device = handler.getDevice();
if (device == null || !device.isInitialized()) {
LOG.warn("SleepTest device: {}", (device == null) ? "null" : device.getState());
toast(handler.getContext(), R.string.device_not_connected, Toast.LENGTH_LONG, GB.ERROR);
return false;
}
final DevicePrefs prefs = GBApplication.getDevicePrefs(device);
final LocalTime wakeTime = prefs.getLocalTime("garmin_experimental_sleep_WakeTime", "07:00");
LOG.debug("SleepTest wakeTime: {}", wakeTime);
final long wake = wakeTime.getHour() * 3600L + wakeTime.getMinute() * 60L + wakeTime.getSecond();
LOG.debug("SleepTest wake: {}", wake);
final LocalTime sleepTime = prefs.getLocalTime("garmin_experimental_sleep_SleepTime", "20:00");
LOG.debug("SleepTest sleepTime {}", sleepTime);
final long sleep = sleepTime.getHour() * 3600L + sleepTime.getMinute() * 60L + sleepTime.getSecond();
LOG.debug("SleepTest sleep: {}", sleep);
final List<RecordData> recordData = new ArrayList<>(10);
final long now = System.currentTimeMillis() / 1000L;
final FitFileId.Builder fb = new FitFileId.Builder();
fb.setSerialNumber(1L);
fb.setTimeCreated(now);
fb.setManufacturer(1);
fb.setProduct(65534);
fb.setNumber(1);
fb.setType(FileType.FILETYPE.SETTINGS);
fb.setProductName("GBSleepTest");
recordData.add(fb.build());
final FitUserProfile.Builder ub = new FitUserProfile.Builder();
ub.setWakeTime(wake);
ub.setSleepTime(sleep);
recordData.add(ub.build());
final FitFile fitFile = new FitFile(recordData);
final byte[] fitBytes = fitFile.getOutgoingMessage();
final Uri uri = Uri.parse("fake://SleepTest");
final Bundle options = new Bundle();
options.putByteArray(AbstractDeviceSupport.BUNDLE_EXTRA_INSTALL_BYTES, fitBytes);
options.putString(AbstractDeviceSupport.BUNDLE_EXTRA_INSTALL_TASK_NAME, "configure SleepTest times");
LOG.debug("send SleepTest to device");
GBApplication.deviceService(device).onInstallApp(uri, options);
return true;
}
private void selectAgpsFile(final DeviceSpecificSettingsHandler handler, final Prefs prefs, final String url, final Preference prefLocalFile) {
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_settings_applications"
@@ -7,4 +8,18 @@
android:layout="@layout/preference_checkbox"
android:summary="Allow installed app management from Gadgetbridge. Not heavily tested. Uninstalling system apps might be dangerous."
android:title="App management" />
<nodomain.freeyourgadget.gadgetbridge.util.XTimePreference
android:key="garmin_experimental_sleep_WakeTime"
app:useSimpleSummaryProvider="true"
app:defaultValue="06:00"
android:title="SleepTest: wake time" />
<nodomain.freeyourgadget.gadgetbridge.util.XTimePreference
android:key="garmin_experimental_sleep_SleepTime"
android:title="SleepTest: sleep time"
app:useSimpleSummaryProvider="true"
app:defaultValue="22:00" />
<Preference
android:key="garmin_experimental_sleep_send"
android:summary="Click this to try to send the above typical wake and sleep times to the gadget."
android:title="SleepTest: Send" />
</androidx.preference.PreferenceScreen>