From af80d3fb9a3978281f24495e50bd9ec279b6d43a Mon Sep 17 00:00:00 2001 From: Sebastian Kranz Date: Wed, 17 Jul 2019 14:19:37 +0200 Subject: [PATCH] NEW FEATURE: Add preference to turn your clock forward or backward, up to 10 minutes ahead or behind of your local time. --- .../activities/SettingsActivity.java | 18 +++++++++++++ .../casiogb6900/CasioGB6900DeviceSupport.java | 3 ++- .../service/devices/hplus/HPlusSupport.java | 6 +++-- .../service/devices/huami/HuamiSupport.java | 3 ++- .../service/devices/id115/ID115Support.java | 3 ++- .../devices/jyou/BFH16DeviceSupport.java | 4 ++- .../devices/jyou/TeclastH30Support.java | 4 ++- .../service/devices/miband/MiBandSupport.java | 2 +- .../service/devices/no1f1/No1F1Support.java | 3 ++- .../devices/watch9/Watch9DeviceSupport.java | 6 +++-- .../service/devices/xwatch/XWatchSupport.java | 3 ++- .../devices/zetime/ZeTimeDeviceSupport.java | 4 ++- .../gadgetbridge/util/DateTimeUtils.java | 25 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences.xml | 6 +++++ 15 files changed, 78 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index 90cbcbfed..1ca79dbff 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -198,6 +198,24 @@ public class SettingsActivity extends AbstractSettingsActivity { }); + pref = findPreference("datetime_turn_clocks_forward"); + pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + try { + GBApplication.deviceService().onSetTime(); + } catch (Exception ex) { + GB.toast(getApplicationContext(), + "Error setting new time: " + ex.getLocalizedMessage(), + Toast.LENGTH_LONG, + GB.ERROR, + ex); + } + return true; + } + + }); + final Preference unit = findPreference(PREF_MEASUREMENT_SYSTEM); unit.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java index d42233a0f..09a82d863 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java @@ -54,6 +54,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.ServerTransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.devices.casiogb6900.operations.InitOperation; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -225,7 +226,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport { private void writeCasioCurrentTime(TransactionBuilder builder) { byte[] arr = new byte[10]; - Calendar cal = Calendar.getInstance(); + Calendar cal = DateTimeUtils.getCalendarTurnClockForward(); int year = cal.get(Calendar.YEAR); arr[0] = (byte)((year >>> 0) & 0xff); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java index 2c0f43101..cc5042f3a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java @@ -59,6 +59,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -191,7 +192,8 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { } private HPlusSupport setCurrentDate(TransactionBuilder transaction) { - Calendar c = GregorianCalendar.getInstance(); + Calendar c = DateTimeUtils.getCalendarTurnClockForward(); + int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); @@ -208,7 +210,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { } private HPlusSupport setCurrentTime(TransactionBuilder transaction) { - Calendar c = GregorianCalendar.getInstance(); + Calendar c = DateTimeUtils.getCalendarTurnClockForward(); transaction.write(ctrlCharacteristic, new byte[]{ HPlusConstants.CMD_SET_TIME, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index e7b7cd0a1..6b3384ed0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -120,6 +120,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationS import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.RealtimeSamplesSupport; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; @@ -256,7 +257,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } public HuamiSupport setCurrentTimeWithService(TransactionBuilder builder) { - GregorianCalendar now = BLETypeConversions.createCalendar(); + Calendar now = DateTimeUtils.getCalendarTurnClockForward(); byte[] bytes = getTimeBytes(now, TimeUnit.SECONDS); builder.write(getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_CURRENT_TIME), bytes); return this; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/id115/ID115Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/id115/ID115Support.java index 77363230d..33c33f760 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/id115/ID115Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/id115/ID115Support.java @@ -45,6 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; public class ID115Support extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(ID115Support.class); @@ -269,7 +270,7 @@ public class ID115Support extends AbstractBTLEDeviceSupport { } ID115Support setTime(TransactionBuilder builder) { - Calendar c = Calendar.getInstance(TimeZone.getDefault()); + Calendar c = DateTimeUtils.getCalendarTurnClockForward(); int day = c.get(Calendar.DAY_OF_WEEK); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java index 923f99412..6d23b2df1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/BFH16DeviceSupport.java @@ -81,6 +81,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -546,7 +547,8 @@ public class BFH16DeviceSupport extends AbstractBTLEDeviceSupport { //working private void syncDateAndTime(TransactionBuilder builder) { - Calendar cal = Calendar.getInstance(); + Calendar cal = DateTimeUtils.getCalendarTurnClockForward(); + String strYear = String.valueOf(cal.get(Calendar.YEAR)); byte year1 = (byte)Integer.parseInt(strYear.substring(0, 2)); byte year2 = (byte)Integer.parseInt(strYear.substring(2, 4)); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/TeclastH30Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/TeclastH30Support.java index 368966e15..ce299f145 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/TeclastH30Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/jyou/TeclastH30Support.java @@ -47,6 +47,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -126,7 +127,8 @@ public class TeclastH30Support extends AbstractBTLEDeviceSupport { } private void syncDateAndTime(TransactionBuilder builder) { - Calendar cal = Calendar.getInstance(); + Calendar cal = DateTimeUtils.getCalendarTurnClockForward(); + String strYear = String.valueOf(cal.get(Calendar.YEAR)); byte year1 = (byte)Integer.parseInt(strYear.substring(0, 2)); byte year2 = (byte)Integer.parseInt(strYear.substring(2, 4)); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java index 72dcffb8d..baae9474d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java @@ -622,7 +622,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { * @param builder */ private MiBandSupport setCurrentTime(TransactionBuilder builder) { - Calendar now = GregorianCalendar.getInstance(); + Calendar now = DateTimeUtils.getCalendarTurnClockForward(); Date date = now.getTime(); LOG.info("Sending current time to Mi Band: " + DateTimeUtils.formatDate(date) + " (" + date.toGMTString() + ")"); byte[] nowBytes = MiBandDateConverter.calendarToRawBytes(now, gbDevice.getAddress()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java index 6639aa334..86837144e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java @@ -59,6 +59,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import static org.apache.commons.lang3.math.NumberUtils.min; @@ -395,7 +396,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport { } private void setTime(TransactionBuilder transaction) { - Calendar c = GregorianCalendar.getInstance(); + Calendar c = DateTimeUtils.getCalendarTurnClockForward(); byte[] datetimeBytes = new byte[]{ No1F1Constants.CMD_DATETIME, (byte) ((c.get(Calendar.YEAR) / 256) & 0xff), diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java index a99a07ad8..1eb5cb72c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java @@ -38,6 +38,7 @@ import java.util.UUID; import androidx.annotation.IntRange; import androidx.localbroadcastmanager.content.LocalBroadcastManager; + import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.devices.watch9.Watch9Constants; @@ -60,6 +61,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA import nodomain.freeyourgadget.gadgetbridge.service.devices.watch9.operations.InitOperation; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { @@ -236,7 +238,7 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { } private void handleTime(byte[] time) { - GregorianCalendar now = BLETypeConversions.createCalendar(); + Calendar now = DateTimeUtils.getCalendarTurnClockForward(); GregorianCalendar nowDevice = BLETypeConversions.createCalendar(); int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + Conversion.fromBcd8(time[8]); nowDevice.set(year, @@ -250,7 +252,7 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000; if (10 < timeDiff && timeDiff < 120) { enableCalibration(true); - setTime(BLETypeConversions.createCalendar()); + setTime(DateTimeUtils.getCalendarTurnClockForward()); enableCalibration(false); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java index c3bc3a04c..e1c74d8da 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java @@ -59,6 +59,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSuppo import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.DeviceInfo; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; public class XWatchSupport extends AbstractBTLEDeviceSupport { @@ -134,7 +135,7 @@ public class XWatchSupport extends AbstractBTLEDeviceSupport { LOG.debug("Sending current date to the XWatch"); BluetoothGattCharacteristic deviceData = getCharacteristic(XWatchService.UUID_WRITE); - String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); + String time = new SimpleDateFormat("yyyyMMddHHmmss").format(DateTimeUtils.todayTurnClockForward()); String y = time.substring(2, 4); String M = time.substring(4, 6); String d = time.substring(6, 8); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java index ce493fb22..17cc52c0d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java @@ -64,6 +64,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; @@ -1439,7 +1440,8 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { private void synchronizeTime(TransactionBuilder builder) { - Calendar now = GregorianCalendar.getInstance(); + Calendar now = DateTimeUtils.getCalendarTurnClockForward(); + byte[] timeSync = new byte[]{ZeTimeConstants.CMD_PREAMBLE, ZeTimeConstants.CMD_DATE_TIME, ZeTimeConstants.CMD_SEND, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DateTimeUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DateTimeUtils.java index c234a4a39..16a6fafd9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DateTimeUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DateTimeUtils.java @@ -153,4 +153,29 @@ public class DateTimeUtils { public static String minutesToHHMM(int minutes) { return String.format(Locale.US, "%d:%02d", minutes / 60, minutes % 60); // no I do not want to use durationformatter :P } + + public static Date todayTurnClockForward() { + Calendar now = getCalendarTurnClockForward(); + return now.getTime(); + } + + public static Calendar getCalendarTurnClockForward() { + Calendar now = GregorianCalendar.getInstance(); + // we will now check if the user likes to turn theirs clock forward or backward + // get the time entered on the preferences + int minutesToTurnClockForward = GBApplication.getPrefs().getInt("datetime_turn_clocks_forward", 0); + if(minutesToTurnClockForward > 10) + { // check if the time is greater than 10 minutes forward; this could be changed to any value + minutesToTurnClockForward = 10; + } else if(minutesToTurnClockForward < -10) + { // check if the time is greater than 10 minutes backward; this could be changed to any value + minutesToTurnClockForward = -10; + } + if(minutesToTurnClockForward != 0) { // if a value is given turn the actual clock time forwards or backwards + long timenow = now.getTimeInMillis(); // get the actual time in milli seconds + timenow += minutesToTurnClockForward * 60000; // convert the given minutes to milli seconds and add this to the actual time in milli seconds + now.setTimeInMillis(timenow); // set the "new time" + } + return now; + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b9adc3bc0..4383546aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -74,6 +74,7 @@ Date and Time Sync time Sync time to Gadgetbridge device when connecting, and when time or time zone changes on Android device + Turn your clock forward Theme Light Dark diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 03f4e8552..1bd34d3b8 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -190,6 +190,12 @@ android:key="datetime_synconconnect" android:summary="@string/pref_summary_datetime_syctimeonconnect" android:title="@string/pref_title_datetime_syctimeonconnect" /> +