Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tell media router to discover cast devices #780

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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