From b7f0bd7f5f2d241ecbda1abe79c8975f381f73bf Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 31 Mar 2023 08:57:53 +0530 Subject: [PATCH] Simplify isServiceRunning() method. --- .../libretube/helpers/BackgroundHelper.kt | 24 ++++++++----------- .../libretube/ui/activities/MainActivity.kt | 5 ++-- .../libretube/ui/fragments/PlayerFragment.kt | 8 +------ 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt index 93f77bcca..010a1b164 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt @@ -4,6 +4,7 @@ import android.app.ActivityManager import android.content.Context import android.content.Intent import androidx.core.content.ContextCompat +import androidx.core.content.getSystemService import androidx.fragment.app.commit import com.github.libretube.constants.IntentData import com.github.libretube.services.BackgroundMode @@ -53,24 +54,19 @@ object BackgroundHelper { * 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) + if (isBackgroundServiceRunning(context)) { + // 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. + * Check if the [BackgroundMode] service is currently running. */ - fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean { - val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + fun isBackgroundServiceRunning(context: Context): Boolean { @Suppress("DEPRECATION") - for (service in manager.getRunningServices(Int.MAX_VALUE)) { - if (serviceClass.name == service.service.className) { - return true - } - } - return false + return context.getSystemService()!!.getRunningServices(Int.MAX_VALUE) + .any { BackgroundMode::class.java.name == it.service.className } } } diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 0669de1a9..a39eb5593 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -35,7 +35,6 @@ import com.github.libretube.helpers.NetworkHelper import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.WindowHelper -import com.github.libretube.services.BackgroundMode import com.github.libretube.services.ClosingService import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.dialogs.ErrorDialog @@ -256,8 +255,8 @@ class MainActivity : BaseActivity() { } override fun onPrepareOptionsMenu(menu: Menu?): Boolean { - menu?.findItem(R.id.action_audio)?.isVisible = BackgroundHelper - .isServiceRunning(this, BackgroundMode::class.java) + menu?.findItem(R.id.action_audio)?.isVisible = + BackgroundHelper.isBackgroundServiceRunning(this) return super.onPrepareOptionsMenu(menu) } 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 c16555d4c..faaf89f1b 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 @@ -73,7 +73,6 @@ import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ProxyHelper import com.github.libretube.obj.ShareData import com.github.libretube.obj.VideoResolution -import com.github.libretube.services.BackgroundMode import com.github.libretube.services.DownloadService import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.adapters.ChaptersAdapter @@ -1547,12 +1546,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { return false } - val backgroundModeRunning = BackgroundHelper.isServiceRunning( - requireContext(), - BackgroundMode::class.java - ) - - return exoPlayer.isPlaying && !backgroundModeRunning + return exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext()) } private fun killPlayerFragment() {