From 15e16fcae6d6ab7f2ffa98c2710ba5434472560d Mon Sep 17 00:00:00 2001 From: Krunal Patel Date: Tue, 29 Nov 2022 19:59:26 +0530 Subject: [PATCH] Fix running service lead to sticky notification Stop service started using `BackgroundMode` before new video is played in foreground. --- .../libretube/ui/fragments/PlayerFragment.kt | 19 ++++-------- .../github/libretube/util/BackgroundHelper.kt | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 0ad746643..8004da35c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt index 475a21abf..39ce8e869 100644 --- a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt @@ -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 + } }