[Android] Fix the stopForeground deprecation

Nicolas Pomepuy git at videolan.org
Mon Nov 14 09:54:16 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Nov  4 09:39:04 2022 +0100| [a9e1e7dc62fcbfd257c748dfc6c446f99f0a37de] | committer: Nicolas Pomepuy

Fix the stopForeground deprecation

> https://code.videolan.org/videolan/vlc-android/commit/a9e1e7dc62fcbfd257c748dfc6c446f99f0a37de
---

 .../src/main/java/org/videolan/resources/util/Extensions.kt   | 11 +++++++++++
 .../vlc-android/src/org/videolan/vlc/DebugLogService.kt       |  3 ++-
 .../vlc-android/src/org/videolan/vlc/MediaParsingService.kt   |  3 ++-
 .../src/org/videolan/vlc/gui/video/PopupManager.kt            |  3 ++-
 4 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
index 250d1cdd9..ba18bc49d 100644
--- a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+++ b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
@@ -1,5 +1,6 @@
 package org.videolan.resources.util
 
+import android.app.Service
 import android.content.Context
 import android.content.Intent
 import android.os.Build.VERSION.SDK_INT
@@ -171,4 +172,14 @@ inline fun <reified T : Parcelable> Intent.parcelableArray(key: String): Array<T
 inline fun <reified T : Parcelable> Bundle.parcelableArray(key: String): Array<T>? = when {
     SDK_INT >= 33 -> getParcelableArray(key, T::class.java)
     else -> @Suppress("DEPRECATION", "UNCHECKED_CAST") (getParcelableArray(key) as Array<T>)
+}
+
+/**
+ * Use the new API to stop the foreground state of a service
+ *
+ * @param removeNotification Removes the notification if true
+ */
+fun Service.stopForegroundCompat(removeNotification:Boolean = true) = when {
+    SDK_INT >= 24 -> stopForeground(if (removeNotification) Service.STOP_FOREGROUND_REMOVE else Service.STOP_FOREGROUND_DETACH)
+    else -> @Suppress("DEPRECATION") stopForeground(removeNotification)
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt b/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
index 6119c1d2c..d88484a41 100644
--- a/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/DebugLogService.kt
@@ -36,6 +36,7 @@ import org.videolan.resources.AndroidDevices
 import org.videolan.resources.AppContextProvider
 import org.videolan.resources.VLCOptions
 import org.videolan.resources.util.launchForeground
+import org.videolan.resources.util.stopForegroundCompat
 import org.videolan.tools.CloseableUtils
 import org.videolan.tools.Logcat
 import org.videolan.tools.getContextWithLocale
@@ -156,7 +157,7 @@ class DebugLogService : Service(), Logcat.Callback, Runnable {
         logcat!!.stop()
         logcat = null
         sendMessage(MSG_STOPPED, null)
-        stopForeground(true)
+        stopForegroundCompat()
         stopSelf()
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 2af2dffb7..0e50cef84 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -51,6 +51,7 @@ import org.videolan.medialibrary.stubs.StubMedialibrary
 import org.videolan.resources.*
 import org.videolan.resources.util.dbExists
 import org.videolan.resources.util.launchForeground
+import org.videolan.resources.util.stopForegroundCompat
 import org.videolan.tools.*
 import org.videolan.vlc.gui.SendCrashActivity
 import org.videolan.vlc.gui.helpers.NotificationHelper
@@ -386,7 +387,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
 
     private fun hideNotification() {
         lastNotificationTime = -1L
-        stopForeground(true)
+        stopForegroundCompat()
         showProgress(-1F, "")
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.kt
index c560e0ffd..cd6f6708f 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/PopupManager.kt
@@ -40,6 +40,7 @@ import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.ACTION_REMOTE_PLAYPAUSE
 import org.videolan.resources.ACTION_REMOTE_STOP
 import org.videolan.resources.ACTION_REMOTE_SWITCH_VIDEO
+import org.videolan.resources.util.stopForegroundCompat
 import org.videolan.tools.*
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
@@ -264,7 +265,7 @@ class PopupManager constructor(private val service: PlaybackService) : PlaybackS
     }
 
     private fun hideNotification() {
-        service.stopForeground(true)
+        service.stopForegroundCompat()
     }
 
     override fun onSurfacesCreated(vlcVout: IVLCVout) {



More information about the Android mailing list