From 1d41a48274b334df7bfb995be9326f92f5113e9c Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 11 Jan 2021 17:15:19 +0800 Subject: [PATCH] Conditionalize message receive broadcast, so apps can subscribe to JUST their messages --- .../aidl/com/geeksville/mesh/IMeshService.aidl | 8 +++++++- .../java/com/geeksville/mesh/MainActivity.kt | 17 +++++------------ .../com/geeksville/mesh/service/MeshService.kt | 13 +++++++++++++ .../mesh/service/MeshServiceBroadcasts.kt | 12 +++++++++--- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl index b07374ca7..fcb44874a 100644 --- a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl +++ b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl @@ -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. + + // RECEIVED. - 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) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 4f5a1fce3..7222cfc41 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -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(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 -> { 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 d8ef31baf..c8ba1f98c 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -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" diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshServiceBroadcasts.kt b/app/src/main/java/com/geeksville/mesh/service/MeshServiceBroadcasts.kt index eca04d91b..831ab1bd2 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshServiceBroadcasts.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshServiceBroadcasts.kt @@ -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) {