mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
Fix running service lead to sticky notification
Stop service started using `BackgroundMode` before new video is played in foreground.
This commit is contained in:
parent
34211eee4c
commit
15e16fcae6
@ -1,7 +1,6 @@
|
||||
package com.github.libretube.ui.fragments
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.ActivityManager
|
||||
import android.app.PictureInPictureParams
|
||||
import android.content.Context
|
||||
import android.content.pm.ActivityInfo
|
||||
@ -195,6 +194,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
context?.hideKeyboard(view)
|
||||
|
||||
// Stop [BackgroundMode] service if it is running.
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
|
||||
// clear the playing queue
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
@ -315,6 +317,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
}
|
||||
|
||||
binding.playImageView.setOnClickListener {
|
||||
// Stop [BackgroundMode] service if it is running.
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
if (!exoPlayer.isPlaying) {
|
||||
// start or go on playing
|
||||
binding.playImageView.setImageResource(R.drawable.ic_pause)
|
||||
@ -1398,22 +1402,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
return false
|
||||
}
|
||||
|
||||
val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java)
|
||||
val backgroundModeRunning = BackgroundHelper.isServiceRunning(requireContext(), BackgroundMode::class.java)
|
||||
|
||||
return exoPlayer.isPlaying && !backgroundModeRunning
|
||||
}
|
||||
|
||||
private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
|
||||
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
@Suppress("DEPRECATION")
|
||||
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
|
||||
if (serviceClass.name == service.service.className) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
@ -10,6 +11,11 @@ import com.github.libretube.services.BackgroundMode
|
||||
* Helper for starting a new Instance of the [BackgroundMode]
|
||||
*/
|
||||
object BackgroundHelper {
|
||||
|
||||
/**
|
||||
* Start the foreground service [BackgroundMode] to play in background. [position]
|
||||
* is seek to position specified in milliseconds in the current [videoId].
|
||||
*/
|
||||
fun playOnBackground(
|
||||
context: Context,
|
||||
videoId: String,
|
||||
@ -29,4 +35,29 @@ object BackgroundHelper {
|
||||
context.startService(intent)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the [BackgroundMode] service if it is running.
|
||||
*/
|
||||
fun stopBackgroundPlay(context: Context) {
|
||||
if (!isServiceRunning(context, BackgroundMode::class.java)) return
|
||||
|
||||
// Intent to stop background mode service
|
||||
val intent = Intent(context, BackgroundMode::class.java)
|
||||
context.stopService(intent)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given service as [serviceClass] is currently running.
|
||||
*/
|
||||
fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
|
||||
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
@Suppress("DEPRECATION")
|
||||
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
|
||||
if (serviceClass.name == service.service.className) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user