[Android] PlaybackService: Implement LifeCycleOwner

Geoffrey Métais git at videolan.org
Tue Nov 13 10:38:45 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Nov  2 16:12:26 2018 +0100| [b16dafcdbc7abeb2b0dae1f67dfdc51c1e665406] | committer: Geoffrey Métais

PlaybackService: Implement LifeCycleOwner

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

 vlc-android/src/org/videolan/vlc/PlaybackService.kt | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 1457d44f5..e44103e41 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -41,6 +41,9 @@ import androidx.annotation.MainThread
 import androidx.core.app.NotificationManagerCompat
 import androidx.core.app.ServiceCompat
 import androidx.core.content.ContextCompat
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.ServiceLifecycleDispatcher
+import androidx.media.MediaBrowserServiceCompat
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.channels.actor
@@ -67,8 +70,9 @@ import java.util.*
 
 private const val TAG = "VLC/PlaybackService"
 
-class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineScope {
+class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOwner {
     override val coroutineContext = Dispatchers.Main.immediate
+    private val dispatcher = ServiceLifecycleDispatcher(this)
 
     lateinit var playlistManager: PlaylistManager
         private set
@@ -444,6 +448,7 @@ class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineSco
     }
 
     override fun onCreate() {
+        dispatcher.onServicePreSuperOnCreate()
         super.onCreate()
         settings = Settings.getInstance(this)
         playlistManager = PlaylistManager(this)
@@ -486,6 +491,11 @@ class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineSco
         keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
     }
 
+    override fun onStart(intent: Intent?, startId: Int) {
+        dispatcher.onServicePreSuperOnStart()
+        super.onStart(intent, startId)
+    }
+
     private fun updateHasWidget() {
         val manager = AppWidgetManager.getInstance(this)
         widget = when {
@@ -522,6 +532,7 @@ class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineSco
     }
 
     override fun onDestroy() {
+        dispatcher.onServicePreSuperOnDestroy()
         super.onDestroy()
         handler.removeCallbacksAndMessages(null)
         if (this::mediaSession.isInitialized) mediaSession.release()
@@ -533,6 +544,7 @@ class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineSco
     }
 
     override fun onBind(intent: Intent): IBinder? {
+        dispatcher.onServicePreSuperOnBind()
         return if (androidx.media.MediaBrowserServiceCompat.SERVICE_INTERFACE == intent.action) super.onBind(intent) else mBinder
     }
 
@@ -1195,6 +1207,8 @@ class PlaybackService : androidx.media.MediaBrowserServiceCompat(), CoroutineSco
     @MainThread
     fun setVideoAspectRatio(aspect: String?) = playlistManager.player.setVideoAspectRatio(aspect)
 
+    override fun getLifecycle() = dispatcher.lifecycle!!
+
     class Client(private val mContext: Context?, private val mCallback: Callback?) {
 
         private var mBound = false



More information about the Android mailing list