mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Merge pull request #4718 from Bnyro/master
fix: properly kill offline player notification
This commit is contained in:
commit
1f96e88c88
@ -350,11 +350,6 @@
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
|
||||
<service
|
||||
android:name=".services.ClosingService"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
|
||||
<service
|
||||
android:name=".services.OnlinePlayerService"
|
||||
android:enabled="true"
|
||||
|
@ -49,10 +49,10 @@ object BackgroundHelper {
|
||||
/**
|
||||
* Stop the [OnlinePlayerService] service if it is running.
|
||||
*/
|
||||
fun stopBackgroundPlay(context: Context) {
|
||||
if (isBackgroundServiceRunning(context)) {
|
||||
fun stopBackgroundPlay(context: Context, serviceClass: Class<*> = OnlinePlayerService::class.java) {
|
||||
if (isBackgroundServiceRunning(context, serviceClass)) {
|
||||
// Intent to stop background mode service
|
||||
val intent = Intent(context, OnlinePlayerService::class.java)
|
||||
val intent = Intent(context, serviceClass)
|
||||
context.stopService(intent)
|
||||
}
|
||||
}
|
||||
@ -60,9 +60,9 @@ object BackgroundHelper {
|
||||
/**
|
||||
* Check if the [OnlinePlayerService] service is currently running.
|
||||
*/
|
||||
fun isBackgroundServiceRunning(context: Context): Boolean {
|
||||
fun isBackgroundServiceRunning(context: Context, serviceClass: Class<*> = OnlinePlayerService::class.java): Boolean {
|
||||
@Suppress("DEPRECATION")
|
||||
return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
|
||||
.any { OnlinePlayerService::class.java.name == it.service.className }
|
||||
.any { serviceClass.name == it.service.className }
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
package com.github.libretube.services
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import androidx.core.content.getSystemService
|
||||
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
||||
|
||||
class ClosingService : Service() {
|
||||
|
||||
override fun onBind(intent: Intent?) = null
|
||||
|
||||
// Handle application closing
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
super.onTaskRemoved(rootIntent)
|
||||
|
||||
// destroy the player notification when the app gets destroyed
|
||||
getSystemService<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
|
||||
|
||||
// Destroy the service
|
||||
stopSelf()
|
||||
}
|
||||
}
|
@ -118,4 +118,12 @@ class OfflinePlayerService : LifecycleService() {
|
||||
super.onBind(intent)
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the service when app is removed from the task manager.
|
||||
*/
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
super.onTaskRemoved(rootIntent)
|
||||
onDestroy()
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,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.ClosingService
|
||||
import com.github.libretube.ui.base.BaseActivity
|
||||
import com.github.libretube.ui.dialogs.ErrorDialog
|
||||
import com.github.libretube.ui.fragments.AudioPlayerFragment
|
||||
@ -65,13 +64,6 @@ class MainActivity : BaseActivity() {
|
||||
// enable auto rotation if turned on
|
||||
requestOrientationChange()
|
||||
|
||||
// start service that gets called on closure
|
||||
try {
|
||||
startService(Intent(this, ClosingService::class.java))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
// show noInternet Activity if no internet available on app startup
|
||||
if (!NetworkHelper.isNetworkAvailable(this)) {
|
||||
val noInternetIntent = Intent(this, NoInternetActivity::class.java)
|
||||
|
Loading…
x
Reference in New Issue
Block a user