From d352f8cd7bb8ec6f5bd0ef1662412a6e91c417e7 Mon Sep 17 00:00:00 2001 From: Andrew Rowson Date: Wed, 26 Jul 2023 17:33:38 +0100 Subject: [PATCH] Don't include batt in location message if extended data is not set Fixes #741 --- CHANGELOG.md | 1 + .../owntracks/android/model/messages/MessageLocation.kt | 2 +- .../owntracks/android/support/DeviceMetricsProvider.kt | 8 ++++---- project/app/src/main/res/values/strings.xml | 2 +- .../test/java/org/owntracks/android/support/ParserTest.kt | 5 ++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d384e48a96..ba4b62b698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Both cloud and local backup should work now - Config export actually exports to a local file now, rather than just a somewhat useless "share" - `conn` value correctly filled out as `o` (offline) when there's no network connection (#1442) +- `batt` is ommitted from locations if extended data is disabled (#741) ## Version 2.4.10 diff --git a/project/app/src/main/java/org/owntracks/android/model/messages/MessageLocation.kt b/project/app/src/main/java/org/owntracks/android/model/messages/MessageLocation.kt index ec91d6c6e6..c1f2243303 100644 --- a/project/app/src/main/java/org/owntracks/android/model/messages/MessageLocation.kt +++ b/project/app/src/main/java/org/owntracks/android/model/messages/MessageLocation.kt @@ -33,7 +33,7 @@ open class MessageLocation(private val dep: MessageWithCreatedAt = MessageCreate var trigger: String? = null @JsonProperty("batt") - var battery = 0 + var battery: Int? = null @JsonProperty("bs") var batteryStatus: BatteryStatus? = null diff --git a/project/app/src/main/java/org/owntracks/android/support/DeviceMetricsProvider.kt b/project/app/src/main/java/org/owntracks/android/support/DeviceMetricsProvider.kt index d7c88de8c4..09e94a8ad7 100644 --- a/project/app/src/main/java/org/owntracks/android/support/DeviceMetricsProvider.kt +++ b/project/app/src/main/java/org/owntracks/android/support/DeviceMetricsProvider.kt @@ -15,11 +15,11 @@ import timber.log.Timber @Singleton class DeviceMetricsProvider @Inject internal constructor(@ApplicationContext private val context: Context) { - val batteryLevel: Int + val batteryLevel: Int? get() { - val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) - val batteryStatus = context.registerReceiver(null, intentFilter) - return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) ?: 0 + return context + .registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) + ?.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) } val batteryStatus: BatteryStatus get() { diff --git a/project/app/src/main/res/values/strings.xml b/project/app/src/main/res/values/strings.xml index 0d6504e1ab..79572d9f6e 100644 --- a/project/app/src/main/res/values/strings.xml +++ b/project/app/src/main/res/values/strings.xml @@ -215,7 +215,7 @@ To allow this, please enable Location in the device settings." "Port must be a number between 1 and 65535" "Mode" "Extended data" - "Include extended data in location reports" + "Include battery status, connection type and monitoring mode in location reports" "Remote commands" "Enable remote commands" "Remote configuration" diff --git a/project/app/src/test/java/org/owntracks/android/support/ParserTest.kt b/project/app/src/test/java/org/owntracks/android/support/ParserTest.kt index 51408d47e5..fca412f252 100644 --- a/project/app/src/test/java/org/owntracks/android/support/ParserTest.kt +++ b/project/app/src/test/java/org/owntracks/android/support/ParserTest.kt @@ -110,7 +110,6 @@ class ParserTest { "_type" : "location", "acc" : 10, "alt" : 20, - "batt" : 0, "created_at" : 25, "inregions" : [ "Testregion1", "Testregion2" ], "lat" : 50.1, @@ -157,7 +156,7 @@ class ParserTest { assertEquals("s5", message.trackerId) assertEquals(1600, message.accuracy.toLong()) assertEquals(0.0, message.altitude.toDouble(), 0.0) - assertEquals(99, message.battery.toLong()) + assertEquals(99, message.battery) assertEquals(BatteryStatus.FULL, message.batteryStatus) assertEquals("w", message.conn) assertEquals(52.3153748, message.latitude, 0.0) @@ -422,7 +421,7 @@ class ParserTest { assertEquals("s5", messageLocation.trackerId) assertEquals(1600, messageLocation.accuracy.toLong()) assertEquals(0.0, messageLocation.altitude.toDouble(), 0.0) - assertEquals(99, messageLocation.battery.toLong()) + assertEquals(99, messageLocation.battery) assertEquals("w", messageLocation.conn) assertEquals(52.3153748, messageLocation.latitude, 0.0) assertEquals(5.0408462, messageLocation.longitude, 0.0)