Skip to content

Commit f300a0d

Browse files
committed
Set negative battery levels to null in mobile context (close #698)
1 parent 3c8f9fa commit f300a0d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/MockDeviceInfoMonitor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import com.snowplowanalytics.core.utils.DeviceInfoMonitor
2020
class MockDeviceInfoMonitor : DeviceInfoMonitor() {
2121
private val methodAccessCounts: MutableMap<String, Int> = HashMap()
2222
var customIdfa: String? = "XJKLJSALFKJ"
23+
var batteryLevel: Int = 20
24+
2325
override val osType: String
2426
get() {
2527
increaseMethodAccessCount("getOsType")
@@ -73,7 +75,7 @@ class MockDeviceInfoMonitor : DeviceInfoMonitor() {
7375

7476
override fun getBatteryStateAndLevel(context: Context): Pair<String?, Int>? {
7577
increaseMethodAccessCount("getBatteryStateAndLevel")
76-
return Pair("charging", 20)
78+
return Pair("charging", batteryLevel)
7779
}
7880

7981
override val availableStorage: Long

snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/internal/tracker/PlatformContextTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ class PlatformContextTest {
341341
Assert.assertEquals("r13", sdjData[Parameters.APP_SET_ID_SCOPE])
342342
}
343343

344+
@Test
345+
fun batteryLevelNotTrackedIfNegative() {
346+
val deviceInfoMonitor = MockDeviceInfoMonitor()
347+
deviceInfoMonitor.batteryLevel = -1
348+
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
349+
350+
val sdj = platformContext.getMobileContext(false)
351+
Assert.assertNotNull(sdj)
352+
val sdjData = sdj!!.map["data"] as Map<*, *>
353+
354+
Assert.assertEquals("charging", sdjData[Parameters.BATTERY_STATE])
355+
Assert.assertFalse(sdjData.containsKey(Parameters.BATTERY_LEVEL))
356+
}
357+
344358
// --- PRIVATE
345359
private val context: Context
346360
get() = InstrumentationRegistry.getInstrumentation().targetContext

snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/PlatformContext.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package com.snowplowanalytics.core.tracker
1414

1515
import android.content.Context
16+
import android.util.Pair
1617
import com.snowplowanalytics.core.constants.Parameters
1718
import com.snowplowanalytics.core.constants.TrackerConstants
1819
import com.snowplowanalytics.core.utils.DeviceInfoMonitor
@@ -147,7 +148,11 @@ class PlatformContext(
147148
if (trackBatState || trackBatLevel) {
148149
val batteryInfo = deviceInfoMonitor.getBatteryStateAndLevel(context)
149150
if (trackBatState) { addToMap(Parameters.BATTERY_STATE, fromRetrieverOr(retriever.batteryState) { batteryInfo?.first }, pairs) }
150-
if (trackBatLevel) { addToMap(Parameters.BATTERY_LEVEL, fromRetrieverOr(retriever.batteryLevel) { batteryInfo?.second }, pairs) }
151+
if (trackBatLevel) {
152+
val batteryLevel = fromRetrieverOr(retriever.batteryLevel) { batteryInfo?.second }
153+
val validBatteryLevel = if (batteryLevel != null && batteryLevel >= 0) batteryLevel else null
154+
addToMap(Parameters.BATTERY_LEVEL, validBatteryLevel, pairs)
155+
}
151156
}
152157
// Memory
153158
if (shouldTrack(PlatformContextProperty.SYSTEM_AVAILABLE_MEMORY)) {

0 commit comments

Comments
 (0)