Huawei: fix send GPS and time data.

It is used to calculate the correct moon and sun data on the watch.
It can also be used to speed up the GPS.
This commit is contained in:
Me7c7
2025-10-01 21:31:21 +03:00
parent b37078d309
commit bf7094d616
8 changed files with 70 additions and 14 deletions
@@ -123,8 +123,8 @@ public class SettingsActivity extends AbstractSettingsActivityV2 {
index(R.xml.automations_settings, R.string.pref_header_automations);
setInputTypeFor("rtl_max_line_length", InputType.TYPE_CLASS_NUMBER);
setInputTypeFor("location_latitude", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
setInputTypeFor("location_longitude", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
setInputTypeFor("location_latitude", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
setInputTypeFor("location_longitude", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Prefs prefs = GBApplication.getPrefs();
Preference pref = findPreference("pref_category_activity_personal");
@@ -395,6 +395,8 @@ public class HuaweiCoordinator {
// Developer
final List<Integer> developer = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DEVELOPER);
developer.add(R.xml.devicesettings_huawei_debug);
if(supportsGpsAndTimeToDevice())
developer.add(R.xml.devicesettings_huawei_gps_and_time);
return deviceSpecificSettings;
}
@@ -652,6 +654,8 @@ public class HuaweiCoordinator {
return supportsCommandForService(0x18, 0x02);
}
public boolean supportsGpsAndTimeToDevice() { return supportsCommandForService(0x18, 0x06); }
public boolean supportsAccount() {
return supportsCommandForService(0x1A, 0x01);
}
@@ -229,8 +229,8 @@ public class GpsAndTime {
this.commandId = id;
this.tlv = new HuaweiTLV()
.put(0x01, timestamp)
.put(0x02, lon)
.put(0x03, lat);
.put(0x02, lat)
.put(0x03, lon);
this.isEncrypted = true;
this.complete = true;
}
@@ -16,14 +16,19 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei;
import android.location.Location;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
@@ -38,6 +43,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.Send
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWeatherSunMoonSupportRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWeatherSupportRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWeatherUnitRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.CurrentPosition;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@@ -169,6 +175,8 @@ public class HuaweiWeatherManager {
}
for (WeatherSpec.Daily point : weatherSpec.getForecasts()) {
if(point == null)
continue;
currentDay = nextDay;
nextDay = DateTimeUtils.shiftByDays(currentDay, 1);
@@ -191,6 +199,14 @@ public class HuaweiWeatherManager {
}
}
private BigDecimal toBigDecimal(double d2) {
try {
return new BigDecimal(d2);
} catch (NumberFormatException unused) {
return null;
}
}
public void sendWeather(WeatherSpec weatherSpec) {
// Initialize weather settings and send weather
if (!supportProvider.getHuaweiCoordinator().supportsWeather()) {
@@ -250,10 +266,23 @@ public class HuaweiWeatherManager {
lastRequest.nextRequest(sendWeatherCurrentRequest);
lastRequest = sendWeatherCurrentRequest;
SendGpsAndTimeToDeviceRequest sendGpsAndTimeToDeviceRequest = new SendGpsAndTimeToDeviceRequest(supportProvider);
sendGpsAndTimeToDeviceRequest.setFinalizeReq(errorHandler);
lastRequest.nextRequest(sendGpsAndTimeToDeviceRequest);
lastRequest = sendGpsAndTimeToDeviceRequest;
if (supportProvider.getHuaweiCoordinator().supportsGpsAndTimeToDevice() &&
GBApplication.getDevicePrefs(supportProvider.getDevice()).getBoolean("pref_huawei_gps_and_time", true)) {
Location location = new CurrentPosition().getLastKnownLocation();
BigDecimal latitude = toBigDecimal(location.getLatitude());
BigDecimal longitude = toBigDecimal(location.getLongitude());
if (latitude != null && longitude != null) {
double lat = latitude.setScale(7, RoundingMode.HALF_UP).doubleValue();
double lon = longitude.setScale(7, RoundingMode.HALF_UP).doubleValue();
//TODO: should be timestamp when location is set, set old enough to prevent override location determined by workout
int timestamp = (int) (Calendar.getInstance().getTime().getTime() / 1000L) - 86400;
SendGpsAndTimeToDeviceRequest sendGpsAndTimeToDeviceRequest = new SendGpsAndTimeToDeviceRequest(supportProvider, timestamp, lat, lon);
sendGpsAndTimeToDeviceRequest.setFinalizeReq(errorHandler);
lastRequest.nextRequest(sendGpsAndTimeToDeviceRequest);
lastRequest = sendGpsAndTimeToDeviceRequest;
}
}
if (supportProvider.getHuaweiCoordinator().supportsWeatherForecasts()) {
SendWeatherForecastRequest sendWeatherForecastRequest = new SendWeatherForecastRequest(supportProvider, weatherSettings, weatherSpec);
@@ -19,31 +19,41 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import android.location.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.GpsAndTime;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.p2p.HuaweiP2PContactsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.CurrentPosition;
public class SendGpsAndTimeToDeviceRequest extends Request {
private final int timestamp;
private final double lat;
private final double lon;
public SendGpsAndTimeToDeviceRequest(HuaweiSupportProvider support) {
public SendGpsAndTimeToDeviceRequest(HuaweiSupportProvider support, int timestamp, double lat, double lon) {
super(support);
this.serviceId = GpsAndTime.id;
this.commandId = GpsAndTime.CurrentGPSRequest.id;
this.timestamp = timestamp;
this.lat = lat;
this.lon = lon;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
Location location = new CurrentPosition().getLastKnownLocation();
return new GpsAndTime.CurrentGPSRequest(
this.paramsProvider,
(int) (Calendar.getInstance().getTime().getTime() / 1000L) - 60, // Backdating a bit seems to work better
location.getLatitude(),
location.getLongitude()
timestamp,
lat,
lon
).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
@@ -62,7 +62,7 @@ public class CurrentPosition {
lastKnownLocation.setLatitude(this.latitude);
lastKnownLocation.setLongitude(this.longitude);
LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);
LOG.info("got longitude/latitude from preferences: {}/{}", latitude, longitude);
this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old
+2
View File
@@ -4352,4 +4352,6 @@
<string name="rotate_screen">Rotate screen</string>
<string name="filter_charts">Filter charts</string>
<string name="recovery_heart_rate">Recovery Heart rate</string>
<string name="huawei_send_gps_and_time_title">Send GPS and Time to device</string>
<string name="huawei_send_gps_and_time_summary">Send GPS and Time to device for weather and search stars optimization. Location should be set in the settings</string>
</resources>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_header_location" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:icon="@drawable/ic_gps_location"
android:key="pref_huawei_gps_and_time"
android:layout="@layout/preference_checkbox"
android:title="@string/huawei_send_gps_and_time_title"
android:summary="@string/huawei_send_gps_and_time_summary" />
</androidx.preference.PreferenceScreen>