Skip to content

Commit

Permalink
Set negative battery levels to null in mobile context (close #698)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Sep 11, 2024
1 parent 3c8f9fa commit f300a0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.snowplowanalytics.core.utils.DeviceInfoMonitor
class MockDeviceInfoMonitor : DeviceInfoMonitor() {
private val methodAccessCounts: MutableMap<String, Int> = HashMap()
var customIdfa: String? = "XJKLJSALFKJ"
var batteryLevel: Int = 20

override val osType: String
get() {
increaseMethodAccessCount("getOsType")
Expand Down Expand Up @@ -73,7 +75,7 @@ class MockDeviceInfoMonitor : DeviceInfoMonitor() {

override fun getBatteryStateAndLevel(context: Context): Pair<String?, Int>? {
increaseMethodAccessCount("getBatteryStateAndLevel")
return Pair("charging", 20)
return Pair("charging", batteryLevel)
}

override val availableStorage: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit f300a0d

Please sign in to comment.