Conditionalize message receive broadcast, so apps can subscribe to JUST their messages

This commit is contained in:
Kevin Hester
2021-01-11 17:15:19 +08:00
parent de80afe5d7
commit 1d41a48274
4 changed files with 34 additions and 16 deletions

View File

@@ -569,7 +569,7 @@ class MainActivity : AppCompatActivity(), Logging,
val filter = IntentFilter()
filter.addAction(MeshService.ACTION_MESH_CONNECTED)
filter.addAction(MeshService.ACTION_NODE_CHANGE)
filter.addAction(MeshService.ACTION_RECEIVED_DATA)
filter.addAction(MeshService.actionReceived(Portnums.PortNum.TEXT_MESSAGE_APP_VALUE))
filter.addAction((MeshService.ACTION_MESSAGE_STATUS))
registerReceiver(meshServiceReceiver, filter)
receiverRegistered = true;
@@ -641,8 +641,7 @@ class MainActivity : AppCompatActivity(), Logging,
model.isConnected.value = oldConnection
}
}
}
else {
} else {
// For other connection states, just slam them in
model.isConnected.value = connected
}
@@ -717,18 +716,12 @@ class MainActivity : AppCompatActivity(), Logging,
}
}
MeshService.ACTION_RECEIVED_DATA -> {
debug("received new data from service")
MeshService.actionReceived(Portnums.PortNum.TEXT_MESSAGE_APP_VALUE) -> {
debug("received new message from service")
val payload =
intent.getParcelableExtra<DataPacket>(EXTRA_PAYLOAD)!!
when (payload.dataType) {
Portnums.PortNum.TEXT_MESSAGE_APP_VALUE -> {
model.messagesState.addMessage(payload)
}
else ->
debug("activity only cares about text messages, ignoring dataType ${payload.dataType}")
}
model.messagesState.addMessage(payload)
}
MeshService.ACTION_MESSAGE_STATUS -> {

View File

@@ -53,7 +53,20 @@ class MeshService : Service(), Logging {
const val NODENUM_BROADCAST = (0xffffffff).toInt()
/// Intents broadcast by MeshService
@Deprecated(message = "Does not filter by port number. For legacy reasons only broadcast for UNKNOWN_APP, switch to ACTION_RECEIVED")
const val ACTION_RECEIVED_DATA = "$prefix.RECEIVED_DATA"
fun actionReceived(portNum: String) = "$prefix.RECEIVED.$portNum"
/// generate a RECEIVED action filter string that includes either the portnumber as an int, or preferably a symbolic name from portnums.proto
fun actionReceived(portNum: Int): String {
val portType = Portnums.PortNum.forNumber(portNum)
val portStr = portType?.toString() ?: portNum.toString()
return actionReceived(portStr)
}
const val ACTION_NODE_CHANGE = "$prefix.NODE_CHANGE"
const val ACTION_MESH_CONNECTED = "$prefix.MESH_CONNECTED"
const val ACTION_MESSAGE_STATUS = "$prefix.MESSAGE_STATUS"

View File

@@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Parcelable
import com.geeksville.mesh.DataPacket
import com.geeksville.mesh.NodeInfo
import com.geeksville.mesh.Portnums
class MeshServiceBroadcasts(
private val context: Context,
@@ -12,12 +13,17 @@ class MeshServiceBroadcasts(
private val getConnectionState: () -> MeshService.ConnectionState
) {
/**
* The RECEIVED_OPAQUE:
* Broadcast some received data
* Payload will be a DataPacket
*/
fun broadcastReceivedData(payload: DataPacket) {
val intent = Intent(MeshService.ACTION_RECEIVED_DATA).putExtra(EXTRA_PAYLOAD, payload)
explicitBroadcast(intent)
explicitBroadcast(Intent(MeshService.actionReceived(payload.dataType)).putExtra(EXTRA_PAYLOAD, payload))
// For the time being we ALSO broadcast using old ACTION_RECEIVED_DATA field for any oldschool opaque packets
// newer packets (that have a non zero portnum) are only broadcast using the standard mechanism.
if(payload.dataType == Portnums.PortNum.UNKNOWN_APP_VALUE)
explicitBroadcast(Intent(MeshService.ACTION_RECEIVED_DATA).putExtra(EXTRA_PAYLOAD, payload))
}
fun broadcastNodeChange(info: NodeInfo) {