diff --git a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/MockDeviceInfoMonitor.kt b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/MockDeviceInfoMonitor.kt index 7346702f7..1551fb913 100644 --- a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/MockDeviceInfoMonitor.kt +++ b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/MockDeviceInfoMonitor.kt @@ -20,6 +20,8 @@ import com.snowplowanalytics.core.utils.DeviceInfoMonitor class MockDeviceInfoMonitor : DeviceInfoMonitor() { private val methodAccessCounts: MutableMap = HashMap() var customIdfa: String? = "XJKLJSALFKJ" + var batteryLevel: Int = 20 + override val osType: String get() { increaseMethodAccessCount("getOsType") @@ -73,7 +75,7 @@ class MockDeviceInfoMonitor : DeviceInfoMonitor() { override fun getBatteryStateAndLevel(context: Context): Pair? { increaseMethodAccessCount("getBatteryStateAndLevel") - return Pair("charging", 20) + return Pair("charging", batteryLevel) } override val availableStorage: Long diff --git a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/PlatformContextTest.kt b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/PlatformContextTest.kt index f7e9ba6a4..ae7dcc9c2 100644 --- a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/PlatformContextTest.kt +++ b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/PlatformContextTest.kt @@ -341,6 +341,20 @@ class PlatformContextTest { Assert.assertEquals("r13", sdjData[Parameters.APP_SET_ID_SCOPE]) } + @Test + fun batteryLevelNotTrackedIfNegative() { + val deviceInfoMonitor = MockDeviceInfoMonitor() + deviceInfoMonitor.batteryLevel = -1 + val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context) + + val sdj = platformContext.getMobileContext(false) + Assert.assertNotNull(sdj) + val sdjData = sdj!!.map["data"] as Map<*, *> + + Assert.assertEquals("charging", sdjData[Parameters.BATTERY_STATE]) + Assert.assertFalse(sdjData.containsKey(Parameters.BATTERY_LEVEL)) + } + // --- PRIVATE private val context: Context get() = InstrumentationRegistry.getInstrumentation().targetContext diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/PlatformContext.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/PlatformContext.kt index 2419ed929..19f282168 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/PlatformContext.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/PlatformContext.kt @@ -13,6 +13,7 @@ package com.snowplowanalytics.core.tracker import android.content.Context +import android.util.Pair import com.snowplowanalytics.core.constants.Parameters import com.snowplowanalytics.core.constants.TrackerConstants import com.snowplowanalytics.core.utils.DeviceInfoMonitor @@ -147,7 +148,11 @@ class PlatformContext( if (trackBatState || trackBatLevel) { val batteryInfo = deviceInfoMonitor.getBatteryStateAndLevel(context) if (trackBatState) { addToMap(Parameters.BATTERY_STATE, fromRetrieverOr(retriever.batteryState) { batteryInfo?.first }, pairs) } - if (trackBatLevel) { addToMap(Parameters.BATTERY_LEVEL, fromRetrieverOr(retriever.batteryLevel) { batteryInfo?.second }, pairs) } + if (trackBatLevel) { + val batteryLevel = fromRetrieverOr(retriever.batteryLevel) { batteryInfo?.second } + val validBatteryLevel = if (batteryLevel != null && batteryLevel >= 0) batteryLevel else null + addToMap(Parameters.BATTERY_LEVEL, validBatteryLevel, pairs) + } } // Memory if (shouldTrack(PlatformContextProperty.SYSTEM_AVAILABLE_MEMORY)) {