Skip to content

Commit

Permalink
Merge pull request #4025 from Isira-Seneviratne/PlayerData
Browse files Browse the repository at this point in the history
Add PlayerData class
  • Loading branch information
Isira-Seneviratne authored Jun 18, 2023
2 parents 955fbfc + edd97ed commit 07a1193
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package com.github.libretube.constants

object IntentData {
const val downloadData = "downloadData"
const val playerData = "playerData"
const val videoId = "videoId"
const val channelId = "channelId"
const val channelName = "channelName"
const val playlistId = "playlistId"
const val timeStamp = "timeStamp"
const val position = "position"
const val fileName = "fileName"
const val keepQueue = "keepQueue"
const val playlistType = "playlistType"
const val downloading = "downloading"
const val openAudioPlayer = "openAudioPlayer"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/github/libretube/extensions/Bundle.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.libretube.extensions

import android.os.Bundle
import android.os.Parcelable
import androidx.core.os.BundleCompat

inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.fragment.app.commit
import com.github.libretube.constants.IntentData
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.services.OnlinePlayerService
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.fragments.PlayerFragment

/**
Expand All @@ -23,10 +23,10 @@ object BackgroundHelper {
fun playOnBackground(
context: Context,
videoId: String,
position: Long? = null,
position: Long = 0,
playlistId: String? = null,
channelId: String? = null,
keepQueue: Boolean? = null,
keepQueue: Boolean = false,
keepVideoPlayerAlive: Boolean = false,
) {
// close the previous video player if open
Expand All @@ -38,12 +38,9 @@ object BackgroundHelper {
}

// create an intent for the background mode service
val playerData = PlayerData(videoId, playlistId, channelId, keepQueue, position)
val intent = Intent(context, OnlinePlayerService::class.java)
.putExtra(IntentData.videoId, videoId)
.putExtra(IntentData.playlistId, playlistId)
.putExtra(IntentData.channelId, channelId)
.putExtra(IntentData.position, position)
.putExtra(IntentData.keepQueue, keepQueue)
.putExtra(IntentData.playerData, playerData)

// start the background mode as foreground service
ContextCompat.startForegroundService(context, intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.core.content.ContextCompat
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.services.DownloadData
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.services.DownloadService
import java.nio.file.Path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.toID
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.ui.fragments.AudioPlayerFragment
import com.github.libretube.ui.fragments.PlayerFragment
import com.github.libretube.ui.views.SingleViewTouchableMotionLayout
Expand Down Expand Up @@ -53,7 +54,7 @@ object NavigationHelper {
playlistId: String? = null,
channelId: String? = null,
keepQueue: Boolean = false,
timeStamp: Long? = null,
timestamp: Long = 0,
forceVideo: Boolean = false,
) {
if (videoId == null) return
Expand All @@ -63,7 +64,7 @@ object NavigationHelper {
BackgroundHelper.playOnBackground(
context,
videoId.toID(),
timeStamp,
timestamp,
playlistId,
channelId,
keepQueue,
Expand All @@ -74,13 +75,8 @@ object NavigationHelper {
return
}

val bundle = bundleOf(
IntentData.videoId to videoId.toID(),
IntentData.playlistId to playlistId,
IntentData.channelId to channelId,
IntentData.keepQueue to keepQueue,
IntentData.timeStamp to timeStamp,
)
val playerData = PlayerData(videoId.toID(), playlistId, channelId, keepQueue, timestamp)
val bundle = bundleOf(IntentData.playerData to playerData)

val activity = ContextHelper.unwrapActivity(context)
activity.supportFragmentManager.commitNow {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.libretube.services
package com.github.libretube.parcelable

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/github/libretube/parcelable/PlayerData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.libretube.parcelable

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class PlayerData(
val videoId: String,
val playlistId: String? = null,
val channelId: String? = null,
val keepQueue: Boolean = false,
val timestamp: Long = 0
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.DownloadHelper.getNotificationId
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.obj.DownloadStatus
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.receivers.NotificationReceiver
import com.github.libretube.receivers.NotificationReceiver.Companion.ACTION_DOWNLOAD_PAUSE
import com.github.libretube.receivers.NotificationReceiver.Companion.ACTION_DOWNLOAD_RESUME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.os.Binder
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
Expand All @@ -28,14 +27,15 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.parcelableExtra
import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.checkForSegments
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.obj.PlayerNotificationData
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -117,27 +117,24 @@ class OnlinePlayerService : LifecycleService() {
* Initializes the [player] with the [MediaItem].
*/
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
// reset the playing queue listeners
PlayingQueue.resetToDefaults()
// reset the playing queue listeners
PlayingQueue.resetToDefaults()

intent?.parcelableExtra<PlayerData>(IntentData.playerData)?.let { playerData ->
// get the intent arguments
videoId = intent?.getStringExtra(IntentData.videoId)!!
playlistId = intent.getStringExtra(IntentData.playlistId)
val position = intent.getLongExtra(IntentData.position, 0L)
val keepQueue = intent.getBooleanExtra(IntentData.keepQueue, false)
videoId = playerData.videoId
playlistId = playerData.playlistId

// play the audio in the background
loadAudio(videoId, position, keepQueue)
loadAudio(playerData)

PlayingQueue.setOnQueueTapListener { streamItem ->
streamItem.url?.toID()?.let { playNextVideo(it) }
}

if (PlayerHelper.watchPositionsAudio) updateWatchPosition()
} catch (e: Exception) {
Log.e(TAG(), e.toString())
onDestroy()
if (PlayerHelper.watchPositionsAudio) {
updateWatchPosition()
}
}
return super.onStartCommand(intent, flags, startId)
}
Expand All @@ -158,15 +155,10 @@ class OnlinePlayerService : LifecycleService() {

/**
* Gets the video data and prepares the [player].
* @param videoId The id of the video to play
* @param seekToPosition The position of the video to seek to
* @param keepQueue Whether to keep the queue or clear it instead
*/
private fun loadAudio(
videoId: String,
seekToPosition: Long = 0,
keepQueue: Boolean = false,
) {
private fun loadAudio(playerData: PlayerData) {
val (videoId, _, _, keepQueue, timestamp) = playerData

lifecycleScope.launch(Dispatchers.IO) {
streams = runCatching {
RetrofitInstance.api.getStreams(videoId)
Expand All @@ -183,7 +175,7 @@ class OnlinePlayerService : LifecycleService() {
}

withContext(Dispatchers.Main) {
playAudio(seekToPosition)
playAudio(timestamp)
}
}
}
Expand Down Expand Up @@ -292,7 +284,7 @@ class OnlinePlayerService : LifecycleService() {
this.videoId = nextVideo
this.streams = null
this.segments = emptyList()
loadAudio(videoId, keepQueue = true)
loadAudio(PlayerData(videoId, keepQueue = true))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.NetworkHelper
Expand Down Expand Up @@ -421,26 +420,26 @@ class MainActivity : BaseActivity() {
intent?.getStringExtra(IntentData.channelId)?.let {
navController.navigate(
R.id.channelFragment,
bundleOf(IntentData.channelId to it),
bundleOf(IntentData.channelId to it)
)
}
intent?.getStringExtra(IntentData.channelName)?.let {
navController.navigate(
R.id.channelFragment,
bundleOf(IntentData.channelName to it),
bundleOf(IntentData.channelName to it)
)
}
intent?.getStringExtra(IntentData.playlistId)?.let {
navController.navigate(
R.id.playlistFragment,
bundleOf(IntentData.playlistId to it),
bundleOf(IntentData.playlistId to it)
)
}
intent?.getStringExtra(IntentData.videoId)?.let {
NavigationHelper.navigateVideo(
context = this,
videoId = it,
timeStamp = intent?.getLongExtra(IntentData.timeStamp, 0L),
timestamp = intent.getLongExtra(IntentData.timeStamp, 0L)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.getWhileDigit
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.services.DownloadData
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.util.TextUtils
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
NavigationHelper.navigateVideo(
context = requireContext(),
videoId = PlayingQueue.getCurrent()?.url?.toID(),
timeStamp = playerService?.player?.currentPosition?.div(1000),
timestamp = playerService?.player?.currentPosition?.div(1000) ?: 0,
keepQueue = true,
forceVideo = true,
)
Expand Down
Loading

0 comments on commit 07a1193

Please sign in to comment.