2022-07-21 16:40:27 +05:30
|
|
|
package com.github.libretube.services
|
2022-05-08 16:11:08 +05:30
|
|
|
|
2022-07-23 19:11:57 +05:30
|
|
|
import android.app.Notification
|
|
|
|
import android.app.NotificationChannel
|
2022-06-30 19:32:55 +05:30
|
|
|
import android.app.NotificationManager
|
2022-07-21 16:40:27 +05:30
|
|
|
import android.app.Service
|
|
|
|
import android.content.Intent
|
2022-07-23 19:11:57 +05:30
|
|
|
import android.os.Build
|
2022-08-02 15:04:42 +05:30
|
|
|
import android.os.Handler
|
2022-07-21 16:40:27 +05:30
|
|
|
import android.os.IBinder
|
2022-08-02 15:04:42 +05:30
|
|
|
import android.os.Looper
|
2022-10-17 22:48:34 +05:30
|
|
|
import android.util.Log
|
2022-08-08 15:31:25 +05:30
|
|
|
import android.widget.Toast
|
2022-08-02 15:04:42 +05:30
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
2022-07-23 19:11:57 +05:30
|
|
|
import com.github.libretube.R
|
2022-08-14 13:25:28 +05:30
|
|
|
import com.github.libretube.api.RetrofitInstance
|
2022-10-18 23:10:28 +05:30
|
|
|
import com.github.libretube.api.obj.Segment
|
2022-10-17 22:39:08 +05:30
|
|
|
import com.github.libretube.api.obj.Segments
|
|
|
|
import com.github.libretube.api.obj.Streams
|
2022-09-08 22:12:52 +05:30
|
|
|
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
2022-09-08 23:49:44 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
2022-09-08 22:12:52 +05:30
|
|
|
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-10-17 22:39:08 +05:30
|
|
|
import com.github.libretube.db.DatabaseHelper
|
2022-10-17 22:48:34 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder
|
|
|
|
import com.github.libretube.extensions.awaitQuery
|
2022-10-23 17:09:15 +05:30
|
|
|
import com.github.libretube.extensions.toStreamItem
|
2022-09-22 21:31:03 +05:30
|
|
|
import com.github.libretube.util.NowPlayingNotification
|
|
|
|
import com.github.libretube.util.PlayerHelper
|
|
|
|
import com.github.libretube.util.PlayingQueue
|
|
|
|
import com.github.libretube.util.PreferenceHelper
|
2022-05-27 22:49:08 +05:30
|
|
|
import com.google.android.exoplayer2.C
|
2022-05-08 16:11:08 +05:30
|
|
|
import com.google.android.exoplayer2.ExoPlayer
|
|
|
|
import com.google.android.exoplayer2.MediaItem
|
2022-09-26 22:46:13 +05:30
|
|
|
import com.google.android.exoplayer2.PlaybackException
|
2022-06-30 19:32:55 +05:30
|
|
|
import com.google.android.exoplayer2.Player
|
2022-05-27 22:49:08 +05:30
|
|
|
import com.google.android.exoplayer2.audio.AudioAttributes
|
2022-08-02 15:04:42 +05:30
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
2022-05-08 16:11:08 +05:30
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
/**
|
2022-07-21 16:40:27 +05:30
|
|
|
* Loads the selected videos audio in background mode with a notification area.
|
2022-05-08 16:11:08 +05:30
|
|
|
*/
|
2022-07-21 16:40:27 +05:30
|
|
|
class BackgroundMode : Service() {
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* VideoId of the video
|
|
|
|
*/
|
|
|
|
private lateinit var videoId: String
|
|
|
|
|
2022-08-08 14:13:46 +05:30
|
|
|
/**
|
|
|
|
*PlaylistId for autoplay
|
|
|
|
*/
|
|
|
|
private var playlistId: String? = null
|
|
|
|
|
2022-05-08 16:11:08 +05:30
|
|
|
/**
|
|
|
|
* The response that gets when called the Api.
|
|
|
|
*/
|
2022-10-17 22:39:08 +05:30
|
|
|
private var streams: Streams? = null
|
2022-05-08 16:11:08 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* The [ExoPlayer] player. Followed tutorial [here](https://developer.android.com/codelabs/exoplayer-intro)
|
|
|
|
*/
|
|
|
|
private var player: ExoPlayer? = null
|
|
|
|
private var playWhenReadyPlayer = true
|
|
|
|
|
2022-06-14 22:38:02 +05:30
|
|
|
/**
|
|
|
|
* The [AudioAttributes] handle the audio focus of the [player]
|
|
|
|
*/
|
|
|
|
private lateinit var audioAttributes: AudioAttributes
|
|
|
|
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* SponsorBlock Segment data
|
|
|
|
*/
|
2022-10-17 22:39:08 +05:30
|
|
|
private var segmentData: Segments? = null
|
2022-08-02 15:04:42 +05:30
|
|
|
|
2022-08-07 21:52:40 +05:30
|
|
|
/**
|
2022-08-08 14:13:46 +05:30
|
|
|
* [Notification] for the player
|
2022-08-07 21:52:40 +05:30
|
|
|
*/
|
|
|
|
private lateinit var nowPlayingNotification: NowPlayingNotification
|
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
/**
|
2022-08-10 19:53:57 +05:30
|
|
|
* Autoplay Preference
|
|
|
|
*/
|
2022-09-15 19:24:02 +05:30
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
|
|
|
|
2022-08-10 19:53:57 +05:30
|
|
|
/**
|
|
|
|
* Setting the required [Notification] for running as a foreground service
|
2022-08-07 22:45:23 +05:30
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
2022-10-21 22:05:35 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2022-07-23 19:11:57 +05:30
|
|
|
val channel = NotificationChannel(
|
2022-10-21 22:05:35 +05:30
|
|
|
BACKGROUND_CHANNEL_ID,
|
2022-08-01 12:25:38 +05:30
|
|
|
"Background Service",
|
2022-07-23 19:11:57 +05:30
|
|
|
NotificationManager.IMPORTANCE_DEFAULT
|
|
|
|
)
|
|
|
|
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
2022-10-21 22:05:35 +05:30
|
|
|
|
|
|
|
// see https://developer.android.com/reference/android/app/Service#startForeground(int,%20android.app.Notification)
|
|
|
|
val notification: Notification = Notification.Builder(this, BACKGROUND_CHANNEL_ID)
|
2022-07-23 19:11:57 +05:30
|
|
|
.setContentTitle(getString(R.string.app_name))
|
2022-10-21 22:05:35 +05:30
|
|
|
.setContentText(getString(R.string.playingOnBackground))
|
|
|
|
.build()
|
|
|
|
|
2022-08-01 12:25:38 +05:30
|
|
|
startForeground(PLAYER_NOTIFICATION_ID, notification)
|
2022-07-23 19:11:57 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:08:25 +05:30
|
|
|
/**
|
|
|
|
* Initializes the [player] with the [MediaItem].
|
2022-05-08 16:11:08 +05:30
|
|
|
*/
|
2022-07-21 16:40:27 +05:30
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2022-08-07 22:31:03 +05:30
|
|
|
try {
|
2022-08-10 19:23:34 +05:30
|
|
|
// clear the playing queue
|
2022-09-19 23:37:55 +05:30
|
|
|
PlayingQueue.clear()
|
2022-08-10 19:23:34 +05:30
|
|
|
|
2022-08-07 22:31:03 +05:30
|
|
|
// get the intent arguments
|
2022-09-08 23:49:44 +05:30
|
|
|
videoId = intent?.getStringExtra(IntentData.videoId)!!
|
|
|
|
playlistId = intent.getStringExtra(IntentData.playlistId)
|
|
|
|
val position = intent.getLongExtra(IntentData.position, 0L)
|
2022-08-07 22:31:03 +05:30
|
|
|
|
|
|
|
// play the audio in the background
|
2022-09-15 19:24:02 +05:30
|
|
|
loadAudio(videoId, position)
|
2022-10-17 22:39:08 +05:30
|
|
|
|
|
|
|
updateWatchPosition()
|
2022-08-07 22:31:03 +05:30
|
|
|
} catch (e: Exception) {
|
2022-08-27 18:43:24 +05:30
|
|
|
onDestroy()
|
2022-08-07 22:31:03 +05:30
|
|
|
}
|
2022-07-21 16:40:27 +05:30
|
|
|
return super.onStartCommand(intent, flags, startId)
|
|
|
|
}
|
|
|
|
|
2022-10-17 22:39:08 +05:30
|
|
|
private fun updateWatchPosition() {
|
2022-10-23 17:09:15 +05:30
|
|
|
player?.currentPosition?.let {
|
|
|
|
DatabaseHelper.saveWatchPosition(videoId, it)
|
|
|
|
}
|
2022-10-17 22:39:08 +05:30
|
|
|
handler.postDelayed(this::updateWatchPosition, 500)
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:40:27 +05:30
|
|
|
/**
|
|
|
|
* Gets the video data and prepares the [player].
|
|
|
|
*/
|
2022-09-15 19:24:02 +05:30
|
|
|
private fun loadAudio(
|
2022-07-21 16:40:27 +05:30
|
|
|
videoId: String,
|
|
|
|
seekToPosition: Long = 0
|
|
|
|
) {
|
2022-09-15 19:24:02 +05:30
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
try {
|
2022-08-08 14:13:46 +05:30
|
|
|
streams = RetrofitInstance.api.getStreams(videoId)
|
2022-09-15 19:24:02 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
return@launch
|
2022-07-21 16:40:27 +05:30
|
|
|
}
|
2022-08-07 21:52:40 +05:30
|
|
|
|
2022-10-23 18:13:47 +05:30
|
|
|
// add the playlist video to the queue
|
|
|
|
if (playlistId != null && PlayingQueue.isEmpty()) {
|
2022-10-23 19:34:15 +05:30
|
|
|
streams?.toStreamItem(videoId)
|
|
|
|
?.let { PlayingQueue.insertPlaylist(playlistId!!, it) }
|
2022-10-23 18:13:47 +05:30
|
|
|
} else {
|
2022-10-23 19:34:15 +05:30
|
|
|
streams?.toStreamItem(videoId)?.let { PlayingQueue.updateCurrent(it) }
|
|
|
|
streams?.relatedStreams?.toTypedArray()?.let {
|
|
|
|
PlayingQueue.add(*it)
|
|
|
|
}
|
2022-10-23 18:13:47 +05:30
|
|
|
}
|
2022-10-23 17:09:15 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
handler.post {
|
|
|
|
playAudio(seekToPosition)
|
2022-08-07 22:31:03 +05:30
|
|
|
}
|
2022-09-15 19:24:02 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-21 16:40:27 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
private fun playAudio(
|
|
|
|
seekToPosition: Long
|
|
|
|
) {
|
|
|
|
initializePlayer()
|
|
|
|
setMediaItem()
|
2022-07-21 16:40:27 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
// create the notification
|
|
|
|
if (!this@BackgroundMode::nowPlayingNotification.isInitialized) {
|
2022-10-17 22:39:08 +05:30
|
|
|
nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!, true)
|
2022-09-15 19:24:02 +05:30
|
|
|
}
|
2022-10-17 22:39:08 +05:30
|
|
|
nowPlayingNotification.updatePlayerNotification(videoId, streams!!)
|
2022-08-02 15:04:42 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
player?.apply {
|
|
|
|
playWhenReady = playWhenReadyPlayer
|
|
|
|
prepare()
|
|
|
|
}
|
2022-08-09 18:37:12 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
// seek to the previous position if available
|
2022-10-17 22:48:34 +05:30
|
|
|
if (seekToPosition != 0L) {
|
|
|
|
player?.seekTo(seekToPosition)
|
|
|
|
} else if (PlayerHelper.watchPositionsEnabled) {
|
|
|
|
try {
|
|
|
|
val watchPosition = awaitQuery {
|
|
|
|
DatabaseHolder.Database.watchPositionDao().findById(videoId)
|
|
|
|
}
|
|
|
|
Log.e("position", watchPosition.toString())
|
|
|
|
streams?.duration?.let {
|
|
|
|
if (watchPosition != null && watchPosition.position < it * 1000 * 0.9) {
|
|
|
|
player?.seekTo(watchPosition.position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
2022-08-08 14:38:44 +05:30
|
|
|
|
2022-09-15 19:24:02 +05:30
|
|
|
// set the playback speed
|
|
|
|
val playbackSpeed = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.BACKGROUND_PLAYBACK_SPEED,
|
|
|
|
"1"
|
|
|
|
).toFloat()
|
|
|
|
player?.setPlaybackSpeed(playbackSpeed)
|
|
|
|
|
|
|
|
fetchSponsorBlockSegments()
|
2022-07-21 16:40:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the player
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun initializePlayer() {
|
2022-08-08 14:13:46 +05:30
|
|
|
if (player != null) return
|
|
|
|
|
2022-06-14 22:38:02 +05:30
|
|
|
audioAttributes = AudioAttributes.Builder()
|
2022-05-27 22:49:08 +05:30
|
|
|
.setUsage(C.USAGE_MEDIA)
|
2022-09-09 21:44:23 +05:30
|
|
|
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
2022-05-27 22:49:08 +05:30
|
|
|
.build()
|
2022-08-08 14:13:46 +05:30
|
|
|
player = ExoPlayer.Builder(this)
|
2022-09-09 21:44:23 +05:30
|
|
|
.setHandleAudioBecomingNoisy(true)
|
2022-08-08 14:13:46 +05:30
|
|
|
.setAudioAttributes(audioAttributes, true)
|
|
|
|
.build()
|
2022-06-30 19:32:55 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens for changed playbackStates (e.g. pause, end)
|
|
|
|
* Plays the next video when the current one ended
|
|
|
|
*/
|
2022-09-15 19:11:01 +05:30
|
|
|
player?.addListener(object : Player.Listener {
|
2022-09-26 22:46:13 +05:30
|
|
|
override fun onPlaybackStateChanged(state: Int) {
|
2022-07-27 14:59:16 +05:30
|
|
|
when (state) {
|
|
|
|
Player.STATE_ENDED -> {
|
2022-10-07 23:09:41 +05:30
|
|
|
if (PlayerHelper.autoPlayEnabled) playNextVideo()
|
2022-07-27 14:59:16 +05:30
|
|
|
}
|
|
|
|
Player.STATE_IDLE -> {
|
2022-08-07 22:45:23 +05:30
|
|
|
onDestroy()
|
2022-07-27 14:59:16 +05:30
|
|
|
}
|
2022-09-26 22:46:13 +05:30
|
|
|
Player.STATE_BUFFERING -> {}
|
|
|
|
Player.STATE_READY -> {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPlayerError(error: PlaybackException) {
|
|
|
|
// show a toast on errors
|
|
|
|
Handler(Looper.getMainLooper()).post {
|
|
|
|
Toast.makeText(
|
|
|
|
this@BackgroundMode.applicationContext,
|
|
|
|
error.localizedMessage,
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2022-06-30 19:32:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|
|
|
|
|
2022-06-30 19:32:55 +05:30
|
|
|
/**
|
|
|
|
* Plays the first related video to the current (used when the playback of the current video ended)
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun playNextVideo() {
|
2022-10-23 18:13:47 +05:30
|
|
|
val nextVideo = PlayingQueue.getNext()
|
2022-08-08 14:13:46 +05:30
|
|
|
|
|
|
|
// play new video on background
|
2022-10-23 18:13:47 +05:30
|
|
|
if (nextVideo != null) {
|
|
|
|
this.videoId = nextVideo
|
|
|
|
}
|
2022-08-08 14:13:46 +05:30
|
|
|
this.segmentData = null
|
2022-09-15 19:24:02 +05:30
|
|
|
loadAudio(videoId)
|
2022-06-30 19:32:55 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 01:43:42 +05:30
|
|
|
/**
|
2022-08-10 19:53:57 +05:30
|
|
|
* Sets the [MediaItem] with the [streams] into the [player]
|
2022-05-20 01:43:42 +05:30
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun setMediaItem() {
|
2022-08-08 14:13:46 +05:30
|
|
|
streams?.let {
|
2022-09-15 19:24:02 +05:30
|
|
|
val uri = if (streams!!.hls != null) {
|
|
|
|
streams!!.hls
|
|
|
|
} else if (streams!!.audioStreams!!.isNotEmpty()) {
|
|
|
|
PlayerHelper.getAudioSource(
|
|
|
|
this,
|
|
|
|
streams!!.audioStreams!!
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val mediaItem = MediaItem.Builder()
|
|
|
|
.setUri(uri)
|
|
|
|
.build()
|
2022-05-20 01:43:42 +05:30
|
|
|
player?.setMediaItem(mediaItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* fetch the segments for SponsorBlock
|
|
|
|
*/
|
|
|
|
private fun fetchSponsorBlockSegments() {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
kotlin.runCatching {
|
|
|
|
val categories = PlayerHelper.getSponsorBlockCategories()
|
|
|
|
if (categories.size > 0) {
|
|
|
|
segmentData =
|
|
|
|
RetrofitInstance.api.getSegments(
|
|
|
|
videoId,
|
|
|
|
ObjectMapper().writeValueAsString(categories)
|
|
|
|
)
|
|
|
|
checkForSegments()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check for SponsorBlock segments
|
|
|
|
*/
|
|
|
|
private fun checkForSegments() {
|
|
|
|
Handler(Looper.getMainLooper()).postDelayed(this::checkForSegments, 100)
|
|
|
|
|
|
|
|
if (segmentData == null || segmentData!!.segments.isEmpty()) return
|
|
|
|
|
2022-10-18 23:10:28 +05:30
|
|
|
segmentData!!.segments.forEach { segment: Segment ->
|
2022-08-02 15:04:42 +05:30
|
|
|
val segmentStart = (segment.segment!![0] * 1000f).toLong()
|
|
|
|
val segmentEnd = (segment.segment[1] * 1000f).toLong()
|
|
|
|
val currentPosition = player?.currentPosition
|
|
|
|
if (currentPosition in segmentStart until segmentEnd) {
|
2022-10-07 23:09:41 +05:30
|
|
|
if (PlayerHelper.sponsorBlockNotifications) {
|
2022-08-08 15:31:25 +05:30
|
|
|
try {
|
|
|
|
Toast.makeText(this, R.string.segment_skipped, Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
} catch (e: Exception) {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 15:04:42 +05:30
|
|
|
player?.seekTo(segmentEnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
/**
|
|
|
|
* destroy the [BackgroundMode] foreground service
|
|
|
|
*/
|
|
|
|
override fun onDestroy() {
|
2022-09-20 01:13:13 +05:30
|
|
|
// clear the playing queue
|
|
|
|
PlayingQueue.clear()
|
|
|
|
|
2022-10-18 23:10:28 +05:30
|
|
|
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer()
|
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
// called when the user pressed stop in the notification
|
|
|
|
// stop the service from being in the foreground and remove the notification
|
2022-08-27 18:43:24 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
|
|
} else {
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
stopForeground(true)
|
|
|
|
}
|
2022-08-07 22:45:23 +05:30
|
|
|
// destroy the service
|
|
|
|
stopSelf()
|
2022-10-18 23:10:28 +05:30
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:40:27 +05:30
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
2022-08-09 18:37:12 +05:30
|
|
|
return null
|
2022-05-20 01:43:42 +05:30
|
|
|
}
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|