Free as in Freedom: Codeberg.org. Create your repos!
Browse Source

Move fake device offset to Mi Band 1 preferences, remove support for other devices

Reasons for removal:
- I doubt we honored the offset correctly for new features anyway that are available on newer devices
- Newer devices have a display always displaying the wrong time
tags/0.34.0
Andreas Shimokawa 1 month ago
parent
commit
16090f0e21

+ 1
- 1
app/build.gradle View File

@@ -68,7 +68,7 @@ dependencies {
68 68
 
69 69
     implementation fileTree(dir: "libs", include: ["*.jar"])
70 70
     implementation "androidx.appcompat:appcompat:1.0.2"
71
-    implementation "androidx.preference:preference:1.0.0"
71
+    implementation "androidx.preference:preference:1.1.0-alpha05"
72 72
     implementation "androidx.cardview:cardview:1.0.0"
73 73
     implementation "androidx.recyclerview:recyclerview:1.0.0"
74 74
     implementation "androidx.legacy:legacy-support-v4:1.0.0"

+ 2
- 0
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java View File

@@ -671,6 +671,7 @@ public static String packageNameToPebbleMsgSender(String packageName) {
671 671
                         switch (deviceType) {
672 672
                             case MIBAND:
673 673
                                 deviceSharedPrefsEdit.putBoolean("low_latency_fw_update", prefs.getBoolean("mi_low_latency_fw_update", true));
674
+                                deviceSharedPrefsEdit.putInt("device_time_offset_hours", prefs.getInt("mi_device_time_offset_hours", 0));
674 675
                                 break;
675 676
                             case AMAZFITCOR:
676 677
                                 displayItems = prefs.getStringSet("cor_display_items", null);
@@ -709,6 +710,7 @@ public static String packageNameToPebbleMsgSender(String packageName) {
709 710
                 editor.remove("disconnect_notification_start");
710 711
                 editor.remove("disconnect_notification_end");
711 712
                 editor.remove("mi_low_latency_fw_update");
713
+                editor.remove("mi_device_time_offset_hours");
712 714
                 editor.remove("mi2_do_not_disturb");
713 715
                 editor.remove("mi2_do_not_disturb_start");
714 716
                 editor.remove("mi2_do_not_disturb_end");

+ 13
- 0
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java View File

@@ -1,9 +1,12 @@
1 1
 package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings;
2 2
 
3 3
 import android.os.Bundle;
4
+import android.text.InputType;
5
+import android.widget.EditText;
4 6
 
5 7
 import androidx.annotation.NonNull;
6 8
 import androidx.fragment.app.DialogFragment;
9
+import androidx.preference.EditTextPreference;
7 10
 import androidx.preference.Preference;
8 11
 import androidx.preference.PreferenceFragmentCompat;
9 12
 
@@ -269,6 +272,16 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat {
269 272
         addPreferenceHandlerFor(PREF_SWIPE_UNLOCK);
270 273
         addPreferenceHandlerFor(PREF_MI2_DATEFORMAT);
271 274
         addPreferenceHandlerFor(HuamiConst.PREF_DISPLAY_ITEMS);
275
+
276
+        EditTextPreference pref = findPreference(MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS);
277
+        if (pref != null) {
278
+            pref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
279
+                @Override
280
+                public void onBindEditText(@NonNull EditText editText) {
281
+                    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
282
+                }
283
+            });
284
+        }
272 285
     }
273 286
 
274 287
     static DeviceSpecificSettingsFragment newInstance(String settingsFileSuffix, @NonNull int[] supportedSettings) {

+ 1
- 1
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java View File

@@ -39,7 +39,7 @@ public final class MiBandConst {
39 39
     public static final String PREF_MIBAND_BUTTON_ACTION_DELAY = "mi_button_press_count_match_delay";
40 40
     public static final String PREF_MIBAND_BUTTON_PRESS_BROADCAST = "mi_button_press_broadcast";
41 41
     public static final String PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION = "mi_hr_sleep_detection";
42
-	public static final String PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS = "mi_device_time_offset_hours";
42
+    public static final String PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS = "device_time_offset_hours";
43 43
 	public static final String PREF_MI2_DATEFORMAT = "mi2_dateformat";
44 44
     public static final String PREF_MI2_GOAL_NOTIFICATION = "mi2_goal_notification";
45 45
 	public static final String PREF_MI2_DISPLAY_ITEM_CLOCK = "clock";

+ 5
- 3
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java View File

@@ -22,6 +22,7 @@ import android.app.Activity;
22 22
 import android.bluetooth.BluetoothDevice;
23 23
 import android.bluetooth.le.ScanFilter;
24 24
 import android.content.Context;
25
+import android.content.SharedPreferences;
25 26
 import android.net.Uri;
26 27
 import android.os.Build;
27 28
 import android.os.ParcelUuid;
@@ -236,8 +237,8 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
236 237
         return location;
237 238
     }
238 239
 
239
-	public static int getDeviceTimeOffsetHours() throws IllegalArgumentException {
240
-		Prefs prefs = GBApplication.getPrefs();
240
+    public static int getDeviceTimeOffsetHours(String deviceAddress) throws IllegalArgumentException {
241
+        Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
241 242
 		return prefs.getInt(MiBandConst.PREF_MIBAND_DEVICE_TIME_OFFSET_HOURS, 0);
242 243
 	}
243 244
 
@@ -260,7 +261,8 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
260 261
     @Override
261 262
     public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
262 263
         return new int[]{
263
-                R.xml.devicesettings_lowlatency_fwupdate
264
+                R.xml.devicesettings_lowlatency_fwupdate,
265
+                R.xml.devicesettings_fake_timeoffset
264 266
         };
265 267
     }
266 268
 

+ 6
- 6
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandDateConverter.java View File

@@ -34,9 +34,9 @@ public class MiBandDateConverter {
34 34
      * @param value
35 35
      * @return
36 36
      */
37
-    public static GregorianCalendar rawBytesToCalendar(byte[] value) {
37
+    public static GregorianCalendar rawBytesToCalendar(byte[] value, String deviceAddress) {
38 38
         if (value.length == 6) {
39
-            return rawBytesToCalendar(value, 0);
39
+            return rawBytesToCalendar(value, 0, deviceAddress);
40 40
         }
41 41
         return createCalendar();
42 42
     }
@@ -47,7 +47,7 @@ public class MiBandDateConverter {
47 47
      * @param value
48 48
      * @return
49 49
      */
50
-    public static GregorianCalendar rawBytesToCalendar(byte[] value, int offset) {
50
+    public static GregorianCalendar rawBytesToCalendar(byte[] value, int offset, String deviceAddress) {
51 51
         if (value.length - offset >= 6) {
52 52
             GregorianCalendar timestamp = new GregorianCalendar(
53 53
                     value[offset] + 2000,
@@ -57,7 +57,7 @@ public class MiBandDateConverter {
57 57
                     value[offset + 4],
58 58
                     value[offset + 5]);
59 59
 
60
-	        int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours();
60
+            int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours(deviceAddress);
61 61
 	        if(offsetInHours != 0)
62 62
 		        timestamp.add(Calendar.HOUR_OF_DAY,-offsetInHours);
63 63
 
@@ -73,7 +73,7 @@ public class MiBandDateConverter {
73 73
      * @param timestamp
74 74
      * @return
75 75
      */
76
-    public static byte[] calendarToRawBytes(Calendar timestamp) {
76
+    public static byte[] calendarToRawBytes(Calendar timestamp, String deviceAddress) {
77 77
 
78 78
 	    // The mi-band device currently records sleep
79 79
 	    // only if it happens after 10pm and before 7am.
@@ -82,7 +82,7 @@ public class MiBandDateConverter {
82 82
 	    // If you usually sleep, say, from 6am to 2pm, set the
83 83
 	    // shift to -8, so at 6am the device thinks it's still 10pm
84 84
 	    // of the day before.
85
-	    int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours();
85
+        int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours(deviceAddress);
86 86
 	    if(offsetInHours != 0)
87 87
 			timestamp.add(Calendar.HOUR_OF_DAY,offsetInHours);
88 88
 

+ 3
- 42
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLETypeConversions.java View File

@@ -40,22 +40,7 @@ public class BLETypeConversions {
40 40
      * @return
41 41
      * @see GattCharacteristic#UUID_CHARACTERISTIC_CURRENT_TIME
42 42
      */
43
-    public static byte[] calendarToRawBytes(Calendar timestamp, boolean honorDeviceTimeOffset) {
44
-
45
-        // The mi-band device currently records sleep
46
-        // only if it happens after 10pm and before 7am.
47
-        // The offset is used to trick the device to record sleep
48
-        // in non-standard hours.
49
-        // If you usually sleep, say, from 6am to 2pm, set the
50
-        // shift to -8, so at 6am the device thinks it's still 10pm
51
-        // of the day before.
52
-        if (honorDeviceTimeOffset) {
53
-            int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours();
54
-            if (offsetInHours != 0) {
55
-                timestamp.add(Calendar.HOUR_OF_DAY, offsetInHours);
56
-            }
57
-        }
58
-
43
+    public static byte[] calendarToRawBytes(Calendar timestamp) {
59 44
         // MiBand2:
60 45
         // year,year,month,dayofmonth,hour,minute,second,dayofweek,0,0,tz
61 46
 
@@ -78,25 +63,9 @@ public class BLETypeConversions {
78 63
     /**
79 64
      * Similar to calendarToRawBytes, but only up to (and including) the MINUTES.
80 65
      * @param timestamp
81
-     * @param honorDeviceTimeOffset
82 66
      * @return
83 67
      */
84
-    public static byte[] shortCalendarToRawBytes(Calendar timestamp, boolean honorDeviceTimeOffset) {
85
-
86
-        // The mi-band device currently records sleep
87
-        // only if it happens after 10pm and before 7am.
88
-        // The offset is used to trick the device to record sleep
89
-        // in non-standard hours.
90
-        // If you usually sleep, say, from 6am to 2pm, set the
91
-        // shift to -8, so at 6am the device thinks it's still 10pm
92
-        // of the day before.
93
-        if (honorDeviceTimeOffset) {
94
-            int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours();
95
-            if (offsetInHours != 0) {
96
-                timestamp.add(Calendar.HOUR_OF_DAY, offsetInHours);
97
-            }
98
-        }
99
-
68
+    public static byte[] shortCalendarToRawBytes(Calendar timestamp) {
100 69
         // MiBand2:
101 70
         // year,year,month,dayofmonth,hour,minute
102 71
 
@@ -136,7 +105,7 @@ public class BLETypeConversions {
136 105
      * @param value
137 106
      * @return
138 107
      */
139
-    public static GregorianCalendar rawBytesToCalendar(byte[] value, boolean honorDeviceTimeOffset) {
108
+    public static GregorianCalendar rawBytesToCalendar(byte[] value) {
140 109
         if (value.length >= 7) {
141 110
             int year = toUint16(value[0], value[1]);
142 111
             GregorianCalendar timestamp = new GregorianCalendar(
@@ -153,14 +122,6 @@ public class BLETypeConversions {
153 122
                 timeZone.setRawOffset(value[7] * 15 * 60 * 1000);
154 123
                 timestamp.setTimeZone(timeZone);
155 124
             }
156
-
157
-            if (honorDeviceTimeOffset) {
158
-                int offsetInHours = MiBandCoordinator.getDeviceTimeOffsetHours();
159
-                if (offsetInHours != 0) {
160
-                    timestamp.add(Calendar.HOUR_OF_DAY,-offsetInHours);
161
-                }
162
-            }
163
-
164 125
             return timestamp;
165 126
         }
166 127
 

+ 1
- 1
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiBatteryInfo.java View File

@@ -95,7 +95,7 @@ public class HuamiBatteryInfo extends AbstractInfo {
95 95
         if (mData.length >= 18) {
96 96
             lastCharge = BLETypeConversions.rawBytesToCalendar(new byte[]{
97 97
                     mData[10], mData[11], mData[12], mData[13], mData[14], mData[15], mData[16], mData[17]
98
-            }, true);
98
+            });
99 99
         }
100 100
 
101 101
         return lastCharge;

+ 3
- 3
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java View File

@@ -237,9 +237,9 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
237 237
     public byte[] getTimeBytes(Calendar calendar, TimeUnit precision) {
238 238
         byte[] bytes;
239 239
         if (precision == TimeUnit.MINUTES) {
240
-            bytes = BLETypeConversions.shortCalendarToRawBytes(calendar, true);
240
+            bytes = BLETypeConversions.shortCalendarToRawBytes(calendar);
241 241
         } else if (precision == TimeUnit.SECONDS) {
242
-            bytes = BLETypeConversions.calendarToRawBytes(calendar, true);
242
+            bytes = BLETypeConversions.calendarToRawBytes(calendar);
243 243
         } else {
244 244
             throw new IllegalArgumentException("Unsupported precision, only MINUTES and SECONDS are supported till now");
245 245
         }
@@ -251,7 +251,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
251 251
     }
252 252
 
253 253
     public Calendar fromTimeBytes(byte[] bytes) {
254
-        GregorianCalendar timestamp = BLETypeConversions.rawBytesToCalendar(bytes, true);
254
+        GregorianCalendar timestamp = BLETypeConversions.rawBytesToCalendar(bytes);
255 255
         return timestamp;
256 256
     }
257 257
 

+ 4
- 3
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/BatteryInfo.java View File

@@ -59,13 +59,14 @@ public class BatteryInfo extends AbstractInfo {
59 59
         return BatteryState.UNKNOWN;
60 60
     }
61 61
 
62
-    public GregorianCalendar getLastChargeTime() {
62
+    public GregorianCalendar getLastChargeTime(String deviceAddress) {
63 63
         GregorianCalendar lastCharge = MiBandDateConverter.createCalendar();
64 64
 
65 65
         if (mData.length >= 10) {
66 66
             lastCharge = MiBandDateConverter.rawBytesToCalendar(new byte[]{
67
-                    mData[1], mData[2], mData[3], mData[4], mData[5], mData[6]
68
-            });
67
+                            mData[1], mData[2], mData[3], mData[4], mData[5], mData[6]},
68
+                    deviceAddress
69
+            );
69 70
         }
70 71
 
71 72
         return lastCharge;

+ 4
- 4
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java View File

@@ -625,7 +625,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
625 625
         Calendar now = GregorianCalendar.getInstance();
626 626
         Date date = now.getTime();
627 627
         LOG.info("Sending current time to Mi Band: " + DateTimeUtils.formatDate(date) + " (" + date.toGMTString() + ")");
628
-        byte[] nowBytes = MiBandDateConverter.calendarToRawBytes(now);
628
+        byte[] nowBytes = MiBandDateConverter.calendarToRawBytes(now, gbDevice.getAddress());
629 629
         byte[] time = new byte[]{
630 630
                 nowBytes[0],
631 631
                 nowBytes[1],
@@ -943,7 +943,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
943 943
 
944 944
     public void logDate(byte[] value, int status) {
945 945
         if (status == BluetoothGatt.GATT_SUCCESS) {
946
-            GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value);
946
+            GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value, gbDevice.getAddress());
947 947
             LOG.info("Got Mi Band Date: " + DateTimeUtils.formatDateTime(calendar.getTime()));
948 948
         } else {
949 949
             logMessageContent(value);
@@ -1134,7 +1134,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
1134 1134
      * @param characteristic
1135 1135
      */
1136 1136
     private void queueAlarm(Alarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
1137
-        byte[] alarmCalBytes = MiBandDateConverter.calendarToRawBytes(AlarmUtils.toCalendar(alarm));
1137
+        byte[] alarmCalBytes = MiBandDateConverter.calendarToRawBytes(AlarmUtils.toCalendar(alarm), gbDevice.getAddress());
1138 1138
 
1139 1139
         byte[] alarmMessage = new byte[]{
1140 1140
                 MiBandService.COMMAND_SET_TIMER,
@@ -1172,7 +1172,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
1172 1172
             BatteryInfo info = new BatteryInfo(value);
1173 1173
             batteryCmd.level = ((short) info.getLevelInPercent());
1174 1174
             batteryCmd.state = info.getState();
1175
-            batteryCmd.lastChargeTime = info.getLastChargeTime();
1175
+            batteryCmd.lastChargeTime = info.getLastChargeTime(gbDevice.getAddress());
1176 1176
             batteryCmd.numCharges = info.getNumCharges();
1177 1177
             handleGBDeviceEvent(batteryCmd);
1178 1178
         }

+ 2
- 2
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java View File

@@ -254,7 +254,7 @@ public class FetchActivityOperation extends AbstractMiBand1Operation {
254 254
         // byte 0 is the data type: 1 means that each minute is represented by a triplet of bytes
255 255
         int dataType = value[0];
256 256
         // byte 1 to 6 represent a timestamp
257
-        GregorianCalendar timestamp = MiBandDateConverter.rawBytesToCalendar(value, 1);
257
+        GregorianCalendar timestamp = MiBandDateConverter.rawBytesToCalendar(value, 1, getDevice().getAddress());
258 258
 
259 259
         // counter of all data held by the band
260 260
         int totalDataToRead = (value[7] & 0xff) | ((value[8] & 0xff) << 8);
@@ -405,7 +405,7 @@ public class FetchActivityOperation extends AbstractMiBand1Operation {
405 405
      * @param bytesTransferred
406 406
      */
407 407
     private void sendAckDataTransfer(Calendar time, int bytesTransferred) {
408
-        byte[] ackTime = MiBandDateConverter.calendarToRawBytes(time);
408
+        byte[] ackTime = MiBandDateConverter.calendarToRawBytes(time, getDevice().getAddress());
409 409
         Prefs prefs = GBApplication.getPrefs();
410 410
 
411 411
         byte[] ackChecksum = new byte[]{

+ 8
- 0
app/src/main/res/xml/devicesettings_fake_timeoffset.xml View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3
+    <EditTextPreference
4
+        android:defaultValue="0"
5
+        android:key="device_time_offset_hours"
6
+        android:maxLength="2"
7
+        android:title="@string/miband_prefs_device_time_offset_hours" />
8
+</androidx.preference.PreferenceScreen>

+ 0
- 7
app/src/main/res/xml/miband_preferences.xml View File

@@ -106,13 +106,6 @@
106 106
             android:summary="%s"
107 107
             android:title="@string/prefs_title_heartrate_measurement_interval" />
108 108
 
109
-        <EditTextPreference
110
-			android:defaultValue="0"
111
-			android:inputType="numberSigned"
112
-			android:key="mi_device_time_offset_hours"
113
-			android:maxLength="2"
114
-			android:title="@string/miband_prefs_device_time_offset_hours" />
115
-
116 109
         <PreferenceScreen
117 110
             android:persistent="false"
118 111
             android:title="@string/mi2_prefs_activate_display_on_lift">

+ 4
- 4
app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/BLETypeConversionsTest.java View File

@@ -17,8 +17,8 @@ public class BLETypeConversionsTest extends TestBase {
17 17
         byte[] received = new byte[] {
18 18
                 (byte) 0xe1, 0x07, 0x0a, 0x1c, 0x17, 0x1c, 0x00, 0x04
19 19
         };
20
-        GregorianCalendar calRequested = BLETypeConversions.rawBytesToCalendar(requested, false);
21
-        GregorianCalendar calReceived = BLETypeConversions.rawBytesToCalendar(received, false);
20
+        GregorianCalendar calRequested = BLETypeConversions.rawBytesToCalendar(requested);
21
+        GregorianCalendar calReceived = BLETypeConversions.rawBytesToCalendar(received);
22 22
 
23 23
         assertTrue(calRequested.getTime().equals(calReceived.getTime()));
24 24
     }
@@ -31,8 +31,8 @@ public class BLETypeConversionsTest extends TestBase {
31 31
         byte[] received = new byte[] {
32 32
                 (byte) 0xe1,0x07,0x0a,0x09,0x10,0x23,0x00,0x04
33 33
         };
34
-        GregorianCalendar calRequested = BLETypeConversions.rawBytesToCalendar(requested, false);
35
-        GregorianCalendar calReceived = BLETypeConversions.rawBytesToCalendar(received, false);
34
+        GregorianCalendar calRequested = BLETypeConversions.rawBytesToCalendar(requested);
35
+        GregorianCalendar calReceived = BLETypeConversions.rawBytesToCalendar(received);
36 36
 
37 37
         assertTrue(calRequested.getTime().equals(calReceived.getTime()));
38 38
     }

Loading…
Cancel
Save