diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index a251abde..787784e7 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -858,7 +858,7 @@ class MainActivity : AppCompatActivity(), Logging, return true } R.id.connectStatusImage -> { - Toast.makeText(this, item.title, Toast.LENGTH_SHORT).show() + Toast.makeText(applicationContext, item.title, Toast.LENGTH_SHORT).show() return true } else -> super.onOptionsItemSelected(item) @@ -869,7 +869,7 @@ class MainActivity : AppCompatActivity(), Logging, try { val packageInfo: PackageInfo = packageManager.getPackageInfo(packageName, 0) val versionName = packageInfo.versionName - Toast.makeText(this, versionName, Toast.LENGTH_LONG).show() + Toast.makeText(applicationContext, versionName, Toast.LENGTH_LONG).show() } catch (e: PackageManager.NameNotFoundException) { errormsg("Can not find the version: ${e.message}") } diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 819f9b59..5a046942 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -866,9 +866,10 @@ class MeshService : Service(), Logging { } /// Update our DB of users based on someone sending out a Position subpacket - private fun handleReceivedPosition(fromNum: Int, p: MeshProtos.Position) { + private fun handleReceivedPosition(fromNum: Int, p: MeshProtos.Position, defaultTime: Int = Position.currentTime()) { updateNodeInfo(fromNum) { - it.position = Position(p, it.position?.time ?: 0) + it.position = Position(p) + updateNodeInfoTime(it, defaultTime) } } @@ -942,22 +943,22 @@ class MeshService : Service(), Logging { val p = packet.decoded // If the rxTime was not set by the device (because device software was old), guess at a time - val rxTime = if (packet.rxTime == 0) packet.rxTime else currentSecond() + val rxTime = if (packet.rxTime != 0) packet.rxTime else currentSecond() // Update last seen for the node that sent the packet, but also for _our node_ because anytime a packet passes // through our node on the way to the phone that means that local node is also alive in the mesh - updateNodeInfo(fromNum) { - // Update our last seen based on any valid timestamps. If the device didn't provide a timestamp make one - val lastSeen = rxTime - it.position = it.position?.copy(time = lastSeen) - } updateNodeInfo(myNodeNum) { it.position = it.position?.copy(time = currentSecond()) } if (p.hasPosition()) - handleReceivedPosition(fromNum, p.position) + handleReceivedPosition(fromNum, p.position, rxTime) + else + updateNodeInfo(fromNum) { + // Update our last seen based on any valid timestamps. If the device didn't provide a timestamp make one + updateNodeInfoTime(it, rxTime) + } if (p.hasData()) handleReceivedData(packet) @@ -1620,3 +1621,8 @@ class MeshService : Service(), Logging { } } } + +public fun updateNodeInfoTime(it: NodeInfo, rxTime: Int) { + if (it.position?.time == null || it.position?.time!! < rxTime) + it.position = it.position?.copy(time = rxTime) +} \ No newline at end of file diff --git a/app/src/test/java/com/geeksville/mesh/PositionTest.kt b/app/src/test/java/com/geeksville/mesh/PositionTest.kt index f2053928..4f7f4e17 100644 --- a/app/src/test/java/com/geeksville/mesh/PositionTest.kt +++ b/app/src/test/java/com/geeksville/mesh/PositionTest.kt @@ -14,5 +14,10 @@ class PositionTest { Assert.assertEquals(Position.degD(Position.degI(-89.0)), -89.0, 0.01) } + @Test + fun givenPositionCreatedWithoutTime_thenTimeIsSet() { + val position = Position(37.1, 121.1, 35) + Assert.assertTrue(position.time != 0) + } } diff --git a/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt new file mode 100644 index 00000000..f8db6328 --- /dev/null +++ b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt @@ -0,0 +1,31 @@ +package com.geeksville.mesh.service + +import com.geeksville.mesh.MeshUser +import com.geeksville.mesh.NodeInfo +import com.geeksville.mesh.Position +import org.junit.Assert +import org.junit.Test + + +class MeshServiceTest { + + val nodeInfo = NodeInfo(4, MeshUser("+one", "User One", "U1"), Position(37.1, 121.1, 35, 10)) + + @Test + fun givenNodeInfo_whenUpdatingWithNewTime_thenPositionTimeIsUpdated() { + + val newerTime = 20 + updateNodeInfoTime(nodeInfo, newerTime) + Assert.assertEquals(newerTime, nodeInfo.position?.time) + } + + @Test + fun givenNodeInfo_whenUpdatingWithOldTime_thenPositionTimeIsNotUpdated() { + val olderTime = 5 + val timeBeforeTryingToUpdate = nodeInfo.position?.time + updateNodeInfoTime(nodeInfo, olderTime) + Assert.assertEquals(timeBeforeTryingToUpdate, nodeInfo.position?.time) + } +} + +