@@ -10,11 +10,16 @@ import androidx.activity.compose.setContent
1010import androidx.activity.OnBackPressedCallback
1111import androidx.activity.viewModels
1212import androidx.fragment.app.FragmentActivity
13+ import androidx.lifecycle.Lifecycle
14+ import androidx.lifecycle.lifecycleScope
15+ import androidx.lifecycle.repeatOnLifecycle
16+ import com.foreverjukebox.app.data.AppMode
1317import com.foreverjukebox.app.ui.ForeverJukeboxApp
1418import com.foreverjukebox.app.ui.MainViewModel
1519import com.google.android.gms.cast.framework.CastContext
1620import com.google.android.gms.cast.framework.CastSession
1721import com.google.android.gms.cast.framework.SessionManagerListener
22+ import kotlinx.coroutines.launch
1823
1924class MainActivity : FragmentActivity () {
2025 private val viewModel: MainViewModel by viewModels()
@@ -26,34 +31,12 @@ class MainActivity : FragmentActivity() {
2631
2732 override fun onCreate (savedInstanceState : Bundle ? ) {
2833 super .onCreate(savedInstanceState)
29- try {
30- val castContext = CastContext .getSharedInstance(this )
31- val listener = object : SessionManagerListener <CastSession > {
32- override fun onSessionStarted (session : CastSession , sessionId : String ) {
33- viewModel.setCastingConnected(true , session.castDevice?.friendlyName)
34- viewModel.requestCastStatus()
34+ lifecycleScope.launch {
35+ repeatOnLifecycle(Lifecycle .State .STARTED ) {
36+ viewModel.state.collect { state ->
37+ syncCastSessionListener(state.appMode == AppMode .Server )
3538 }
36-
37- override fun onSessionResumed (session : CastSession , wasSuspended : Boolean ) {
38- viewModel.setCastingConnected(true , session.castDevice?.friendlyName)
39- viewModel.requestCastStatus()
40- }
41-
42- override fun onSessionEnded (session : CastSession , error : Int ) {
43- viewModel.setCastingConnected(false )
44- }
45-
46- override fun onSessionStarting (session : CastSession ) = Unit
47- override fun onSessionStartFailed (session : CastSession , error : Int ) = Unit
48- override fun onSessionEnding (session : CastSession ) = Unit
49- override fun onSessionResuming (session : CastSession , sessionId : String ) = Unit
50- override fun onSessionResumeFailed (session : CastSession , error : Int ) = Unit
51- override fun onSessionSuspended (session : CastSession , reason : Int ) = Unit
5239 }
53- castContext.sessionManager.addSessionManagerListener(listener, CastSession ::class .java)
54- sessionListener = listener
55- } catch (_: Exception ) {
56- // Ignore cast init failures; app still works without it.
5740 }
5841 viewModel.handleDeepLink(intent?.data)
5942 if (intent.getBooleanExtra(EXTRA_OPEN_LISTEN_TAB , false )) {
@@ -97,15 +80,59 @@ class MainActivity : FragmentActivity() {
9780 }
9881
9982 override fun onDestroy () {
100- sessionListener?.let { listener ->
101- runCatching {
102- CastContext .getSharedInstance(this )
103- .sessionManager
104- .removeSessionManagerListener(listener, CastSession ::class .java)
83+ syncCastSessionListener(enable = false )
84+ super .onDestroy()
85+ }
86+
87+ private fun syncCastSessionListener (enable : Boolean ) {
88+ if (! enable) {
89+ sessionListener?.let { listener ->
90+ runCatching {
91+ CastContext .getSharedInstance(this )
92+ .sessionManager
93+ .removeSessionManagerListener(listener, CastSession ::class .java)
94+ }
10595 }
96+ sessionListener = null
97+ viewModel.setCastingConnected(false )
98+ return
99+ }
100+
101+ if (sessionListener != null ) {
102+ return
103+ }
104+
105+ val castContext = runCatching { CastContext .getSharedInstance(this ) }.getOrNull()
106+ ? : return
107+ val listener = object : SessionManagerListener <CastSession > {
108+ override fun onSessionStarted (session : CastSession , sessionId : String ) {
109+ viewModel.setCastingConnected(true , session.castDevice?.friendlyName)
110+ viewModel.requestCastStatus()
111+ }
112+
113+ override fun onSessionResumed (session : CastSession , wasSuspended : Boolean ) {
114+ viewModel.setCastingConnected(true , session.castDevice?.friendlyName)
115+ viewModel.requestCastStatus()
116+ }
117+
118+ override fun onSessionEnded (session : CastSession , error : Int ) {
119+ viewModel.setCastingConnected(false )
120+ }
121+
122+ override fun onSessionStarting (session : CastSession ) = Unit
123+ override fun onSessionStartFailed (session : CastSession , error : Int ) = Unit
124+ override fun onSessionEnding (session : CastSession ) = Unit
125+ override fun onSessionResuming (session : CastSession , sessionId : String ) = Unit
126+ override fun onSessionResumeFailed (session : CastSession , error : Int ) = Unit
127+ override fun onSessionSuspended (session : CastSession , reason : Int ) = Unit
128+ }
129+ castContext.sessionManager.addSessionManagerListener(listener, CastSession ::class .java)
130+ sessionListener = listener
131+
132+ castContext.sessionManager.currentCastSession?.let { session ->
133+ viewModel.setCastingConnected(true , session.castDevice?.friendlyName)
134+ viewModel.requestCastStatus()
106135 }
107- sessionListener = null
108- super .onDestroy()
109136 }
110137
111138 companion object {
0 commit comments