diff --git a/CHANGELOG.md b/CHANGELOG.md index ecaca03debf..8c9be27bc5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/app/src/main/java/au/com/shiftyjelly/pocketcasts/ui/MainActivity.kt b/app/src/main/java/au/com/shiftyjelly/pocketcasts/ui/MainActivity.kt index c84c04e8edd..d9d6a30ab30 100644 --- a/app/src/main/java/au/com/shiftyjelly/pocketcasts/ui/MainActivity.kt +++ b/app/src/main/java/au/com/shiftyjelly/pocketcasts/ui/MainActivity.kt @@ -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 @@ -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 get() = supportFragmentManager.fragments.filterIsInstance() @@ -359,6 +368,8 @@ class MainActivity : handleIntent(intent, savedInstanceState) updateSystemColors() + + mediaRouter = MediaRouter.getInstance(this) } override fun openOnboardingFlow(onboardingFlow: OnboardingFlow) { @@ -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() { @@ -450,8 +472,8 @@ class MainActivity : override fun onDestroy() { super.onDestroy() - disposables.clear() + mediaRouter?.removeCallback(mediaRouterCallback) } @Suppress("DEPRECATION")