Skip to content

Commit aaa6f96

Browse files
authored
Merge pull request libre-tube#7266 from Bnyro/master
fix: properly open current video from media notification
2 parents a0a6694 + dd84281 commit aaa6f96

5 files changed

Lines changed: 39 additions & 22 deletions

File tree

app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import com.google.common.util.concurrent.ListenableFuture
4141
import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.Dispatchers
4343
import kotlinx.coroutines.launch
44+
import kotlinx.coroutines.withContext
4445

4546
@UnstableApi
4647
abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySession.Callback {
@@ -116,7 +117,9 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
116117
START_SERVICE_ACTION -> {
117118
CoroutineScope(Dispatchers.IO).launch {
118119
onServiceCreated(args)
119-
notificationProvider?.intentActivity = getIntentActivity()
120+
withContext(Dispatchers.Main) {
121+
updateNotification()
122+
}
120123

121124
if (::videoId.isInitialized) startPlayback()
122125
}
@@ -185,6 +188,7 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
185188
trackSelector?.updateParameters {
186189
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, isAudioOnlyPlayer)
187190
}
191+
updateNotification()
188192
}
189193
}
190194
}
@@ -239,9 +243,24 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
239243
}
240244
}
241245

246+
/**
247+
* Trigger a notification update with an updated PendingIntent.
248+
*/
249+
private fun updateNotification() {
250+
val notificationIntent = Intent(this, getIntentActivity()).apply {
251+
putExtra(IntentData.maximizePlayer, true)
252+
putExtra(IntentData.offlinePlayer, isOfflinePlayer)
253+
putExtra(IntentData.audioOnly, isAudioOnlyPlayer)
254+
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
255+
}
256+
notificationProvider?.notificationIntent = notificationIntent
257+
mediaLibrarySession?.let {
258+
onUpdateNotification(it, true)
259+
}
260+
}
261+
242262
abstract val isOfflinePlayer: Boolean
243-
abstract var isAudioOnlyPlayer: Boolean
244-
open val maximizePlayer: Boolean = true
263+
var isAudioOnlyPlayer: Boolean = false
245264

246265
val watchPositionsEnabled get() =
247266
(PlayerHelper.watchPositionsAudio && isAudioOnlyPlayer) || (PlayerHelper.watchPositionsVideo && !isAudioOnlyPlayer)
@@ -252,10 +271,7 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
252271
override fun onCreate() {
253272
super.onCreate()
254273

255-
notificationProvider = NowPlayingNotification(
256-
this,
257-
offlinePlayer = isOfflinePlayer,
258-
)
274+
notificationProvider = NowPlayingNotification(this)
259275
setMediaNotificationProvider(notificationProvider!!)
260276

261277
createPlayerAndMediaSession()

app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.github.libretube.extensions.updateParameters
2626
import com.github.libretube.helpers.PlayerHelper
2727
import com.github.libretube.ui.activities.MainActivity
2828
import com.github.libretube.ui.activities.NoInternetActivity
29+
import com.github.libretube.ui.activities.OfflinePlayerActivity
2930
import com.github.libretube.ui.fragments.DownloadTab
3031
import com.github.libretube.util.PlayingQueue
3132
import kotlinx.coroutines.CoroutineScope
@@ -41,7 +42,6 @@ import kotlin.io.path.exists
4142
@OptIn(UnstableApi::class)
4243
open class OfflinePlayerService : AbstractPlayerService() {
4344
override val isOfflinePlayer: Boolean = true
44-
override var isAudioOnlyPlayer: Boolean = true
4545
private var noInternetService: Boolean = false
4646

4747
private var downloadWithItems: DownloadWithItems? = null
@@ -94,7 +94,11 @@ open class OfflinePlayerService : AbstractPlayerService() {
9494
}
9595

9696
override fun getIntentActivity(): Class<*> {
97-
return if (noInternetService) NoInternetActivity::class.java else MainActivity::class.java
97+
return when {
98+
!noInternetService && isAudioOnlyPlayer -> MainActivity::class.java
99+
noInternetService && isAudioOnlyPlayer -> NoInternetActivity::class.java
100+
else -> OfflinePlayerActivity::class.java
101+
}
98102
}
99103

100104
/**

app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import java.io.IOException
4848
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
4949
open class OnlinePlayerService : AbstractPlayerService() {
5050
override val isOfflinePlayer: Boolean = false
51-
override var isAudioOnlyPlayer: Boolean = false
5251

5352
// PlaylistId/ChannelId for autoplay
5453
private var playlistId: String? = null

app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,13 @@ class MainActivity : BaseActivity() {
434434
}
435435

436436
if (intent?.getBooleanExtra(IntentData.maximizePlayer, false) == true) {
437-
// attempt to open the current player fragment first before creating a new one
438-
// TODO: handle this differently
439-
if (runOnPlayerFragment { binding.playerMotionLayout.transitionToStart(); true }) return
437+
// attempt to open the current video player fragment
438+
if (intent?.getBooleanExtra(IntentData.audioOnly, false) == false) {
439+
runOnPlayerFragment { binding.playerMotionLayout.transitionToStart(); true }
440+
return
441+
}
442+
443+
// if it's an audio only session, attempt to maximize the audio player or create a new one
440444
if (runOnAudioPlayerFragment { binding.playerMotionLayout.transitionToStart(); true }) return
441445

442446
val offlinePlayer = intent!!.getBooleanExtra(IntentData.offlinePlayer, false)

app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import com.google.common.collect.ImmutableList
2121
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
2222
class NowPlayingNotification(
2323
private val context: Context,
24-
private val offlinePlayer: Boolean = false
24+
var notificationIntent: Intent = Intent(),
2525
): MediaNotification.Provider {
26-
var intentActivity: Class<*> = MainActivity::class.java
27-
2826
private val nProvider = DefaultMediaNotificationProvider.Builder(context)
2927
.setNotificationId(NotificationId.PLAYER_PLAYBACK.id)
3028
.setChannelId(PLAYER_CHANNEL_NAME)
@@ -36,14 +34,10 @@ class NowPlayingNotification(
3634
// it doesn't start a completely new MainActivity because the MainActivity's launchMode
3735
// is set to "singleTop" in the AndroidManifest (important!!!)
3836
// that's the only way to launch back into the previous activity (e.g. the player view)
39-
val intent = Intent(context, intentActivity).apply {
40-
putExtra(IntentData.maximizePlayer, true)
41-
putExtra(IntentData.offlinePlayer, offlinePlayer)
42-
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
43-
}
37+
4438

4539
return PendingIntentCompat
46-
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false)
40+
.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, false)
4741
}
4842

4943
/**

0 commit comments

Comments
 (0)