Skip to content

Commit

Permalink
Merge pull request #780 from Automattic/fix/chromecast-discovery
Browse files Browse the repository at this point in the history
Tell media router to discover cast devices
  • Loading branch information
ashiagr authored Feb 17, 2023
2 parents 0c1ed84 + e816504 commit ad0770e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
-----
* Bug Fixes:
* App no longer crashes when the device browser has been disabled
([#762](https://github.com/Automattic/pocket-casts-android/issues/762)).
([#762](https://github.com/Automattic/pocket-casts-android/issues/762)).
* Improve discovery of chromecast devices
([#780](https://github.com/Automattic/pocket-casts-android/issues/780)).

7.32
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.LiveDataReactiveStreams
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.mediarouter.media.MediaControlIntent
import androidx.mediarouter.media.MediaRouteSelector
import androidx.mediarouter.media.MediaRouter
import androidx.transition.Slide
import au.com.shiftyjelly.pocketcasts.R
import au.com.shiftyjelly.pocketcasts.account.AccountActivity
Expand Down Expand Up @@ -184,6 +187,12 @@ class MainActivity :
private var videoPlayerShown: Boolean = false
private var overrideNextRefreshTimer: Boolean = false

private var mediaRouter: MediaRouter? = null
private val mediaRouterCallback = object : MediaRouter.Callback() {}
private val mediaRouteSelector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()

private val childrenWithBackStack: List<HasBackstack>
get() = supportFragmentManager.fragments.filterIsInstance<HasBackstack>()

Expand Down Expand Up @@ -359,6 +368,8 @@ class MainActivity :
handleIntent(intent, savedInstanceState)

updateSystemColors()

mediaRouter = MediaRouter.getInstance(this)
}

override fun openOnboardingFlow(onboardingFlow: OnboardingFlow) {
Expand All @@ -374,6 +385,17 @@ class MainActivity :
videoPlayerShown = false
}
}

// Tell media router to discover routes
mediaRouter?.addCallback(mediaRouteSelector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY)
}

override fun onStop() {
super.onStop()
// Remove the callback flag CALLBACK_FLAG_REQUEST_DISCOVERY on stop by calling
// addCallback() again in order to tell the media router that it no longer
// needs to invest effort trying to discover routes of these kinds for now.
mediaRouter?.addCallback(mediaRouteSelector, mediaRouterCallback, 0)
}

private fun openFullscreenViewPlayer() {
Expand Down Expand Up @@ -450,8 +472,8 @@ class MainActivity :

override fun onDestroy() {
super.onDestroy()

disposables.clear()
mediaRouter?.removeCallback(mediaRouterCallback)
}

@Suppress("DEPRECATION")
Expand Down

0 comments on commit ad0770e

Please sign in to comment.