Skip to content

Instantly share code, notes, and snippets.

@dzolnai
Last active April 23, 2020 09:26
Show Gist options
  • Save dzolnai/b2d2a3e3ca9317035bec2294340518ae to your computer and use it in GitHub Desktop.
Save dzolnai/b2d2a3e3ca9317035bec2294340518ae to your computer and use it in GitHub Desktop.
Custom Media Route button which hides when there are no Cast devices on the network
@file:Suppress("PackageDirectoryMismatch")
package androidx.mediarouter.app
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import androidx.mediarouter.media.MediaRouter
class CustomMediaRouteButton : MediaRouteButton {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private var mediaRouter : MediaRouter? = null
init {
if (!isInEditMode) {
mediaRouter = MediaRouter.getInstance(context)
}
}
override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
// When Cast enables / disables the button, it is because the routes have changed. We also
// Want to update the visibility in this case.
refreshVisibility()
}
override fun refreshVisibility() {
if (mediaRouter?.isRouteAvailable(routeSelector, MediaRouter.AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE) == false) {
// We can't set visibility here, because it would call refreshVisibility again, so we just change alpha to 0 and disable clicks
alpha = 0f
isClickable = false
} else {
alpha = 1f
isClickable = true
super.refreshVisibility()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment