mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 14:50: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.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.content.getSystemService
|
||||||
import androidx.fragment.app.commit
|
import androidx.fragment.app.commit
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.services.BackgroundMode
|
import com.github.libretube.services.BackgroundMode
|
||||||
@ -53,24 +54,19 @@ object BackgroundHelper {
|
|||||||
* Stop the [BackgroundMode] service if it is running.
|
* Stop the [BackgroundMode] service if it is running.
|
||||||
*/
|
*/
|
||||||
fun stopBackgroundPlay(context: Context) {
|
fun stopBackgroundPlay(context: Context) {
|
||||||
if (!isServiceRunning(context, BackgroundMode::class.java)) return
|
if (isBackgroundServiceRunning(context)) {
|
||||||
|
// Intent to stop background mode service
|
||||||
// Intent to stop background mode service
|
val intent = Intent(context, BackgroundMode::class.java)
|
||||||
val intent = Intent(context, BackgroundMode::class.java)
|
context.stopService(intent)
|
||||||
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 {
|
fun isBackgroundServiceRunning(context: Context): Boolean {
|
||||||
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
|
return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
|
||||||
if (serviceClass.name == service.service.className) {
|
.any { BackgroundMode::class.java.name == it.service.className }
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,6 @@ import com.github.libretube.helpers.NetworkHelper
|
|||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import com.github.libretube.helpers.ThemeHelper
|
import com.github.libretube.helpers.ThemeHelper
|
||||||
import com.github.libretube.helpers.WindowHelper
|
import com.github.libretube.helpers.WindowHelper
|
||||||
import com.github.libretube.services.BackgroundMode
|
|
||||||
import com.github.libretube.services.ClosingService
|
import com.github.libretube.services.ClosingService
|
||||||
import com.github.libretube.ui.base.BaseActivity
|
import com.github.libretube.ui.base.BaseActivity
|
||||||
import com.github.libretube.ui.dialogs.ErrorDialog
|
import com.github.libretube.ui.dialogs.ErrorDialog
|
||||||
@ -256,8 +255,8 @@ class MainActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
|
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
|
||||||
menu?.findItem(R.id.action_audio)?.isVisible = BackgroundHelper
|
menu?.findItem(R.id.action_audio)?.isVisible =
|
||||||
.isServiceRunning(this, BackgroundMode::class.java)
|
BackgroundHelper.isBackgroundServiceRunning(this)
|
||||||
return super.onPrepareOptionsMenu(menu)
|
return super.onPrepareOptionsMenu(menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,6 @@ import com.github.libretube.helpers.PreferenceHelper
|
|||||||
import com.github.libretube.helpers.ProxyHelper
|
import com.github.libretube.helpers.ProxyHelper
|
||||||
import com.github.libretube.obj.ShareData
|
import com.github.libretube.obj.ShareData
|
||||||
import com.github.libretube.obj.VideoResolution
|
import com.github.libretube.obj.VideoResolution
|
||||||
import com.github.libretube.services.BackgroundMode
|
|
||||||
import com.github.libretube.services.DownloadService
|
import com.github.libretube.services.DownloadService
|
||||||
import com.github.libretube.ui.activities.MainActivity
|
import com.github.libretube.ui.activities.MainActivity
|
||||||
import com.github.libretube.ui.adapters.ChaptersAdapter
|
import com.github.libretube.ui.adapters.ChaptersAdapter
|
||||||
@ -1547,12 +1546,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val backgroundModeRunning = BackgroundHelper.isServiceRunning(
|
return exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext())
|
||||||
requireContext(),
|
|
||||||
BackgroundMode::class.java
|
|
||||||
)
|
|
||||||
|
|
||||||
return exoPlayer.isPlaying && !backgroundModeRunning
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun killPlayerFragment() {
|
private fun killPlayerFragment() {
|
||||||
|
Loading…
Reference in New Issue
Block a user