mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Simplify isServiceRunning() method.
This commit is contained in:
parent
269ef10001
commit
b7f0bd7f5f
@ -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<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
|
||||
.any { BackgroundMode::class.java.name == it.service.className }
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user