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

@@ -23,7 +23,13 @@ The intent you use to reach the service should look like this:
Once you have bound to the service you should register your broadcast receivers per https://developer.android.com/guide/components/broadcasts#context-registered-receivers
// com.geeksville.mesh.x broadcast intents, where x is:
// RECEIVED_DATA for data received from other nodes. payload will contain a DataPacket
// RECEIVED_DATA for data received from other nodes. payload will contain a DataPacket, this action is DEPRECATED (because it sends all received data)
// far better to instead use RECEIVED.<portnumm>
// RECEIVED.<portnumm> - will **only** deliver packets for the specified port number. If a wellknown portnums.proto name for portnum is known it will be used
// (i.e. com.geeksville.mesh.RECEIVED.TEXT_MESSAGE_APP) else the numeric portnum will be included as a base 10 integer (com.geeksville.mesh.RECEIVED.4403 etc...)
// NODE_CHANGE for new IDs appearing or disappearing
// CONNECTION_CHANGED for losing/gaining connection to the packet radio
// MESSAGE_STATUS_CHANGED for any message status changes (for sent messages only, other messages come via RECEIVED_DATA. payload will contain a message ID and a MessageStatus)

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) {