From ed18509377697406d6c208a155907fd03ac21d5b Mon Sep 17 00:00:00 2001 From: jakepurple13 Date: Tue, 20 Apr 2021 08:50:06 -0400 Subject: [PATCH] - made casting better and easier to use --- .../programmersbox/animeworld/MainActivity.kt | 37 ++++++++++-------- .../animeworld/cast/CastOptions.kt | 38 +++++++++---------- .../ic_baseline_cast_connected_24.xml | 10 +++++ 3 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 animeworld/src/main/res/drawable/ic_baseline_cast_connected_24.xml diff --git a/animeworld/src/main/java/com/programmersbox/animeworld/MainActivity.kt b/animeworld/src/main/java/com/programmersbox/animeworld/MainActivity.kt index 201177174..4bf03bde1 100644 --- a/animeworld/src/main/java/com/programmersbox/animeworld/MainActivity.kt +++ b/animeworld/src/main/java/com/programmersbox/animeworld/MainActivity.kt @@ -6,9 +6,11 @@ import android.content.Intent import android.net.Uri import android.widget.Toast import androidx.core.content.ContextCompat +import androidx.mediarouter.app.MediaRouteDialogFactory import androidx.preference.Preference import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.google.android.gms.cast.framework.CastContext import com.obsez.android.lib.filechooser.ChooserDialog import com.programmersbox.anime_sources.Sources import com.programmersbox.anime_sources.anime.Movies @@ -45,19 +47,7 @@ class MainActivity : BaseMainActivity() { activity = this - /* - startActivity(Intent(this, ExpandedControlsActivity::class.java)) - */ - - cast.init( - this, - onSessionDisconnected = { _, _ -> - - }, - onSessionConnected = { - - } - ) + cast.init(this) Notifications.setup(this) @@ -85,6 +75,10 @@ class MainActivity : BaseMainActivity() { override fun createLayoutManager(context: Context): RecyclerView.LayoutManager = LinearLayoutManager(context) override fun downloadChapter(chapterModel: ChapterModel, title: String) { + if (chapterModel.source == Yts) { + Toast.makeText(this, "Yts cannot stream at the moment", Toast.LENGTH_SHORT).show() + return + } GlobalScope.launch { val link = chapterModel.getChapterInfo().blockingGet().firstOrNull()?.link runOnUiThread { @@ -197,18 +191,29 @@ class MainActivity : BaseMainActivity() { ) val casting = Preference(it.context).apply { - title = "Cast Controls" + title = "Cast" icon = ContextCompat.getDrawable(it.context, R.drawable.ic_baseline_cast_24) setOnPreferenceClickListener { - startActivity(Intent(this@MainActivity, ExpandedControlsActivity::class.java)) + if (cast.isCastActive()) { + startActivity(Intent(this@MainActivity, ExpandedControlsActivity::class.java)) + } else { + MediaRouteDialogFactory.getDefault().onCreateChooserDialogFragment() + .also { it.routeSelector = CastContext.getSharedInstance(applicationContext)?.mergedSelector } + .show(activity.supportFragmentManager, "media_chooser") + } true } } - cast.sessionStatus() + cast.sessionConnected() .subscribe(casting::setVisible) .addTo(disposable) + cast.sessionStatus() + .map { if (it) R.drawable.ic_baseline_cast_connected_24 else R.drawable.ic_baseline_cast_24 } + .subscribe(casting::setIcon) + .addTo(disposable) + it.addPreference(casting) it.addPreference( diff --git a/animeworld/src/main/java/com/programmersbox/animeworld/cast/CastOptions.kt b/animeworld/src/main/java/com/programmersbox/animeworld/cast/CastOptions.kt index e595ac03f..d55e5d852 100644 --- a/animeworld/src/main/java/com/programmersbox/animeworld/cast/CastOptions.kt +++ b/animeworld/src/main/java/com/programmersbox/animeworld/cast/CastOptions.kt @@ -13,6 +13,7 @@ import android.os.Handler import android.util.Log import android.view.Menu import android.view.MenuItem +import androidx.mediarouter.app.MediaRouteButton import com.google.android.gms.cast.* import com.google.android.gms.cast.framework.* import com.google.android.gms.cast.framework.CastOptions @@ -106,7 +107,11 @@ class CastHelper { private val sessionConnected = PublishSubject.create() - fun sessionStatus(): Observable = sessionConnected + fun sessionConnected(): Observable = sessionConnected + + private val sessionStatus = PublishSubject.create() + + fun sessionStatus(): Observable = sessionStatus fun isCastActive() = mCastContext.castState == CastState.CONNECTED @@ -121,9 +126,7 @@ class CastHelper { mCastContext = CastContext.getSharedInstance(mApplicationContext) setUpCastListener() - mCastContext.sessionManager.addSessionManagerListener( - mSessionManagerListener, CastSession::class.java - ) + mCastContext.sessionManager.addSessionManagerListener(mSessionManagerListener, CastSession::class.java) } fun String?.toFile(): File? { @@ -160,36 +163,33 @@ class CastHelper { /** Destroy the session callbacks to avoid memory leaks */ fun unInit() { mCastContext.removeCastStateListener(castListener) - mCastContext.sessionManager.removeSessionManagerListener( - mSessionManagerListener, CastSession::class.java - ) + mCastContext.sessionManager.removeSessionManagerListener(mSessionManagerListener, CastSession::class.java) onSessionDisconnected = null onNeedToShowIntroductoryOverlay = null } private val castListener: (Int) -> Unit = { state -> - if (state != CastState.NO_DEVICES_AVAILABLE) - this.onNeedToShowIntroductoryOverlay?.invoke() + sessionConnected.onNext(state != CastState.NO_DEVICES_AVAILABLE) + if (state != CastState.NO_DEVICES_AVAILABLE) this.onNeedToShowIntroductoryOverlay?.invoke() if (state == CastState.NOT_CONNECTED) { /** When casting is disconnected we post updateLastModel */ postUpdateLastModel() SimpleWebServer.stopServer() - sessionConnected.onNext(false) + //sessionConnected.onNext(false) } if (state == CastState.CONNECTED) { onSessionConnected() - sessionConnected.onNext(true) + //sessionConnected.onNext(true) } + sessionStatus.onNext(state == CastState.CONNECTED) } /** Separate UI logic to avoid memory, use context from view */ fun setMediaRouteMenu(context: Context, menu: Menu): MenuItem? = - CastButtonFactory.setUpMediaRouteButton( - context, - menu, - com.programmersbox.animeworld.R.id.media_route_menu_item - ) + CastButtonFactory.setUpMediaRouteButton(context, menu, com.programmersbox.animeworld.R.id.media_route_menu_item) + fun setMediaRouteMenu(context: Context, button: MediaRouteButton) = + CastButtonFactory.setUpMediaRouteButton(context, button) /** * Should be used only in [LibraryFragment] @@ -218,8 +218,7 @@ class CastHelper { val remoteFileName = Utils.getRemoteFileName(deviceIpAddress, mediaFile) ?.replace(" ", "%20") val remoteImageFileName = - if (bannerImage != null) - Utils.getRemoteFileName(deviceIpAddress, bannerImage)?.replace(" ", "%20") + if (bannerImage != null) Utils.getRemoteFileName(deviceIpAddress, bannerImage)?.replace(" ", "%20") else ""//APP_IMAGE_URL /** Generate media metadata */ @@ -286,8 +285,7 @@ class CastHelper { val remoteFileName = Utils.getRemoteFileName(deviceIp, mediaFile) ?.replace(" ", "%20") val remoteImageFileName = - if (bannerFile != null) - Utils.getRemoteFileName(deviceIp, bannerFile)?.replace(" ", "%20") + if (bannerFile != null) Utils.getRemoteFileName(deviceIp, bannerFile)?.replace(" ", "%20") else ""//APP_IMAGE_URL /** Generate movie meta data */ diff --git a/animeworld/src/main/res/drawable/ic_baseline_cast_connected_24.xml b/animeworld/src/main/res/drawable/ic_baseline_cast_connected_24.xml new file mode 100644 index 000000000..9d6df9905 --- /dev/null +++ b/animeworld/src/main/res/drawable/ic_baseline_cast_connected_24.xml @@ -0,0 +1,10 @@ + + +