android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Intent filter extension

Open IVIanuu opened this issue 7 years ago • 9 comments

What about a function like this?

fun intentFilterOf(vararg actions: String) = IntentFilter().apply { actions.forEach { addAction(it) } }

IVIanuu avatar Feb 06 '18 19:02 IVIanuu

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?

JakeWharton avatar Feb 06 '18 20:02 JakeWharton

I usually use IntenFilter with Boardcast receiver to observe network state changes, but I rarely need to add multiple actions to a single IntentFilter

Edward608 avatar Feb 07 '18 03:02 Edward608

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.

IVIanuu avatar Feb 07 '18 10:02 IVIanuu

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.

Edward608 avatar Feb 14 '18 08:02 Edward608

Do you have an example we can look at somewhere?

JakeWharton avatar Feb 14 '18 19:02 JakeWharton

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.

Edward608 avatar Feb 15 '18 03:02 Edward608

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.

JakeWharton avatar Mar 02 '18 19:03 JakeWharton

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.

Edward608 avatar Mar 06 '18 08:03 Edward608

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.

JakeWharton avatar Mar 07 '18 22:03 JakeWharton