2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-07-23 19:11:57 +05:30
|
|
|
|
2022-11-29 19:59:26 +05:30
|
|
|
import android.app.ActivityManager
|
2022-07-23 19:11:57 +05:30
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2023-01-18 09:36:50 +05:30
|
|
|
import androidx.core.content.ContextCompat
|
2023-03-31 08:57:53 +05:30
|
|
|
import androidx.core.content.getSystemService
|
2023-03-03 21:55:46 +05:30
|
|
|
import androidx.fragment.app.commit
|
2022-09-08 23:49:44 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
2023-06-17 07:57:56 +05:30
|
|
|
import com.github.libretube.parcelable.PlayerData
|
2023-04-10 19:24:09 +05:30
|
|
|
import com.github.libretube.services.OnlinePlayerService
|
2023-03-03 21:55:46 +05:30
|
|
|
import com.github.libretube.ui.fragments.PlayerFragment
|
2022-07-23 19:11:57 +05:30
|
|
|
|
2022-08-08 14:13:46 +05:30
|
|
|
/**
|
2023-04-10 19:24:09 +05:30
|
|
|
* Helper for starting a new Instance of the [OnlinePlayerService]
|
2022-08-08 14:13:46 +05:30
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
object BackgroundHelper {
|
2022-11-29 19:59:26 +05:30
|
|
|
|
|
|
|
/**
|
2023-04-10 19:24:09 +05:30
|
|
|
* Start the foreground service [OnlinePlayerService] to play in background. [position]
|
2022-11-29 19:59:26 +05:30
|
|
|
* is seek to position specified in milliseconds in the current [videoId].
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
fun playOnBackground(
|
|
|
|
context: Context,
|
|
|
|
videoId: String,
|
2023-06-17 07:57:56 +05:30
|
|
|
position: Long = 0,
|
2022-12-02 19:15:52 +05:30
|
|
|
playlistId: String? = null,
|
2023-01-14 16:31:14 +05:30
|
|
|
channelId: String? = null,
|
2023-06-17 07:57:56 +05:30
|
|
|
keepQueue: Boolean = false,
|
2023-06-24 23:27:00 +05:30
|
|
|
keepVideoPlayerAlive: Boolean = false
|
2022-07-23 19:11:57 +05:30
|
|
|
) {
|
2023-03-03 21:55:46 +05:30
|
|
|
// close the previous video player if open
|
|
|
|
if (!keepVideoPlayerAlive) {
|
2023-06-01 21:46:17 +05:30
|
|
|
val fragmentManager = ContextHelper.unwrapActivity(context).supportFragmentManager
|
|
|
|
fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
|
|
|
|
fragmentManager.commit { remove(it) }
|
2023-03-03 21:55:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 14:13:46 +05:30
|
|
|
// create an intent for the background mode service
|
2023-06-17 07:57:56 +05:30
|
|
|
val playerData = PlayerData(videoId, playlistId, channelId, keepQueue, position)
|
2023-04-10 19:24:09 +05:30
|
|
|
val intent = Intent(context, OnlinePlayerService::class.java)
|
2023-06-17 07:57:56 +05:30
|
|
|
.putExtra(IntentData.playerData, playerData)
|
2022-08-08 14:13:46 +05:30
|
|
|
|
|
|
|
// start the background mode as foreground service
|
2023-01-18 09:36:50 +05:30
|
|
|
ContextCompat.startForegroundService(context, intent)
|
2022-07-23 19:11:57 +05:30
|
|
|
}
|
2022-11-29 19:59:26 +05:30
|
|
|
|
|
|
|
/**
|
2023-04-10 19:24:09 +05:30
|
|
|
* Stop the [OnlinePlayerService] service if it is running.
|
2022-11-29 19:59:26 +05:30
|
|
|
*/
|
|
|
|
fun stopBackgroundPlay(context: Context) {
|
2023-03-31 08:57:53 +05:30
|
|
|
if (isBackgroundServiceRunning(context)) {
|
|
|
|
// Intent to stop background mode service
|
2023-04-10 19:24:09 +05:30
|
|
|
val intent = Intent(context, OnlinePlayerService::class.java)
|
2023-03-31 08:57:53 +05:30
|
|
|
context.stopService(intent)
|
|
|
|
}
|
2022-11-29 19:59:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-10 19:24:09 +05:30
|
|
|
* Check if the [OnlinePlayerService] service is currently running.
|
2022-11-29 19:59:26 +05:30
|
|
|
*/
|
2023-03-31 08:57:53 +05:30
|
|
|
fun isBackgroundServiceRunning(context: Context): Boolean {
|
2022-11-29 19:59:26 +05:30
|
|
|
@Suppress("DEPRECATION")
|
2023-03-31 08:57:53 +05:30
|
|
|
return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
|
2023-04-10 19:24:09 +05:30
|
|
|
.any { OnlinePlayerService::class.java.name == it.service.className }
|
2022-11-29 19:59:26 +05:30
|
|
|
}
|
2022-07-23 19:11:57 +05:30
|
|
|
}
|