Intent filter extension
What about a function like this?
fun intentFilterOf(vararg actions: String) = IntentFilter().apply { actions.forEach { addAction(it) } }
I don't think I've ever used an IntentFilter in my life. I think we'd need to find out common usage to determine whether this was something likely to be used or whether you rarely just specify only actions.
Any ideas?
I usually use IntenFilter with Boardcast receiver to observe network state changes, but I rarely need to add multiple actions to a single IntentFilter
I also use it only for broadcasts. I think we can close this because most of the time more than one action is not needed.
Recently found one use case of multiple action in one IntentFilter, which is when you need to use the WifiP2P features. Seems like this extension is kind of useful.
Do you have an example we can look at somewhere?
val receiver = WifiP2PBoardcastReceiver(...)
fun onResume() {
super.onResume()
val filter = IntentFilter().apply {
addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
}
registerReceiver(p2pReceiver, filter)
}
I don't know if there is a better way to do this, since its the first time I use the WifiP2P feature, but according to the offical tutorial, this is how you receive infos from the system about Wifi P2P connections and infos.
I guess my concern here is that this is only for actions and doesn't cover any of the other things you can add to a filter.
Then a set of builder-like extension function could be better? For example in Anko, you can have val intent = Intent().newTask().clearTask(). Although something similar for IntentFilter could be too much extensions.
It probably is, yes, but we could consider adding it to the support libraries. I just have never used IntentFilter so I honestly have no idea whether it's worth our time or not.