Merge pull request #4718 from Bnyro/master

fix: properly kill offline player notification
This commit is contained in:
Bnyro 2023-09-08 16:24:01 +02:00 committed by GitHub
commit 1f96e88c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 41 deletions

View File

@ -350,11 +350,6 @@
android:enabled="true" android:enabled="true"
android:exported="false" /> android:exported="false" />
<service
android:name=".services.ClosingService"
android:enabled="true"
android:exported="false" />
<service <service
android:name=".services.OnlinePlayerService" android:name=".services.OnlinePlayerService"
android:enabled="true" android:enabled="true"

View File

@ -49,10 +49,10 @@ object BackgroundHelper {
/** /**
* Stop the [OnlinePlayerService] service if it is running. * Stop the [OnlinePlayerService] service if it is running.
*/ */
fun stopBackgroundPlay(context: Context) { fun stopBackgroundPlay(context: Context, serviceClass: Class<*> = OnlinePlayerService::class.java) {
if (isBackgroundServiceRunning(context)) { if (isBackgroundServiceRunning(context, serviceClass)) {
// Intent to stop background mode service // Intent to stop background mode service
val intent = Intent(context, OnlinePlayerService::class.java) val intent = Intent(context, serviceClass)
context.stopService(intent) context.stopService(intent)
} }
} }
@ -60,9 +60,9 @@ object BackgroundHelper {
/** /**
* Check if the [OnlinePlayerService] service is currently running. * 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") @Suppress("DEPRECATION")
return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE) return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
.any { OnlinePlayerService::class.java.name == it.service.className } .any { serviceClass.name == it.service.className }
} }
} }

View File

@ -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()
}
}

View File

@ -118,4 +118,12 @@ class OfflinePlayerService : LifecycleService() {
super.onBind(intent) super.onBind(intent)
return null return null
} }
/**
* Stop the service when app is removed from the task manager.
*/
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
onDestroy()
}
} }

View File

@ -33,7 +33,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.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
import com.github.libretube.ui.fragments.AudioPlayerFragment import com.github.libretube.ui.fragments.AudioPlayerFragment
@ -65,13 +64,6 @@ class MainActivity : BaseActivity() {
// enable auto rotation if turned on // enable auto rotation if turned on
requestOrientationChange() 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 // show noInternet Activity if no internet available on app startup
if (!NetworkHelper.isNetworkAvailable(this)) { if (!NetworkHelper.isNetworkAvailable(this)) {
val noInternetIntent = Intent(this, NoInternetActivity::class.java) val noInternetIntent = Intent(this, NoInternetActivity::class.java)