Skip to content

Commit

Permalink
- made casting better and easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
jakepurple13 committed Apr 20, 2021
1 parent 057fed0 commit ed18509
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +107,11 @@ class CastHelper {

private val sessionConnected = PublishSubject.create<Boolean>()

fun sessionStatus(): Observable<Boolean> = sessionConnected
fun sessionConnected(): Observable<Boolean> = sessionConnected

private val sessionStatus = PublishSubject.create<Boolean>()

fun sessionStatus(): Observable<Boolean> = sessionStatus

fun isCastActive() =
mCastContext.castState == CastState.CONNECTED
Expand All @@ -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? {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
10 changes: 10 additions & 0 deletions animeworld/src/main/res/drawable/ic_baseline_cast_connected_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M1,18v3h3c0,-1.66 -1.34,-3 -3,-3zM1,14v2c2.76,0 5,2.24 5,5h2c0,-3.87 -3.13,-7 -7,-7zM19,7L5,7v1.63c3.96,1.28 7.09,4.41 8.37,8.37L19,17L19,7zM1,10v2c4.97,0 9,4.03 9,9h2c0,-6.08 -4.93,-11 -11,-11zM21,3L3,3c-1.1,0 -2,0.9 -2,2v3h2L3,5h18v14h-7v2h7c1.1,0 2,-0.9 2,-2L23,5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>

0 comments on commit ed18509

Please sign in to comment.