Fix running service lead to sticky notification

Stop service started using `BackgroundMode` before new video is played in foreground.
This commit is contained in:
Krunal Patel 2022-11-29 19:59:26 +05:30
parent 34211eee4c
commit 15e16fcae6
2 changed files with 37 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.PictureInPictureParams import android.app.PictureInPictureParams
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
@ -195,6 +194,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
context?.hideKeyboard(view) context?.hideKeyboard(view)
// Stop [BackgroundMode] service if it is running.
BackgroundHelper.stopBackgroundPlay(requireContext())
// clear the playing queue // clear the playing queue
PlayingQueue.resetToDefaults() PlayingQueue.resetToDefaults()
@ -315,6 +317,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
binding.playImageView.setOnClickListener { binding.playImageView.setOnClickListener {
// Stop [BackgroundMode] service if it is running.
BackgroundHelper.stopBackgroundPlay(requireContext())
if (!exoPlayer.isPlaying) { if (!exoPlayer.isPlaying) {
// start or go on playing // start or go on playing
binding.playImageView.setImageResource(R.drawable.ic_pause) binding.playImageView.setImageResource(R.drawable.ic_pause)
@ -1398,22 +1402,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
return false return false
} }
val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java) val backgroundModeRunning = BackgroundHelper.isServiceRunning(requireContext(), BackgroundMode::class.java)
return exoPlayer.isPlaying && !backgroundModeRunning 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) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)

View File

@ -1,5 +1,6 @@
package com.github.libretube.util package com.github.libretube.util
import android.app.ActivityManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
@ -10,6 +11,11 @@ import com.github.libretube.services.BackgroundMode
* Helper for starting a new Instance of the [BackgroundMode] * Helper for starting a new Instance of the [BackgroundMode]
*/ */
object BackgroundHelper { 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( fun playOnBackground(
context: Context, context: Context,
videoId: String, videoId: String,
@ -29,4 +35,29 @@ object BackgroundHelper {
context.startService(intent) 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
}
} }