[Android] Medialibrary: Remove obsolete broadcasting

Geoffrey Métais git at videolan.org
Mon Jul 1 15:34:55 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun 28 14:24:40 2019 +0200| [a2a5f2ad4e0d46d24ffedf074ed027b001ce17d1] | committer: Geoffrey Métais

Medialibrary: Remove obsolete broadcasting

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

 .../org/videolan/medialibrary/Medialibrary.java    |  3 ---
 .../interfaces/AbstractMedialibrary.java           |  4 ----
 .../src/org/videolan/vlc/MediaParsingService.kt    | 24 ++++++++++++++--------
 .../src/org/videolan/vlc/PlaybackService.kt        |  7 ++-----
 4 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 80d397b23..2d66f6451 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -29,9 +29,6 @@ import android.util.Log;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.annotation.WorkerThread;
-import androidx.core.content.ContextCompat;
-import androidx.lifecycle.MutableLiveData;
-import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
 import org.videolan.libvlc.LibVLC;
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary;
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
index 3e02dcf93..93bac0c4b 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
@@ -61,9 +61,6 @@ abstract public class AbstractMedialibrary {
     public static final int ML_INIT_FAILED = 2;
     public static final int ML_INIT_DB_RESET = 3;
 
-    public static final String ACTION_IDLE = "action_idle";
-    public static final String STATE_IDLE = "state_idle";
-
     public static final AbstractMediaWrapper[] EMPTY_COLLECTION = {};
     public static final String VLC_MEDIA_DB_NAME = "/vlc_media.db";
     public static final String THUMBS_FOLDER_NAME = "/thumbs";
@@ -384,7 +381,6 @@ abstract public class AbstractMedialibrary {
     public void onBackgroundTasksIdleChanged(boolean isIdle) {
         mIsWorking = !isIdle;
         sRunning.postValue(mIsWorking);
-        LocalBroadcastManager.getInstance(sContext).sendBroadcast(new Intent(ACTION_IDLE).putExtra(STATE_IDLE, isIdle));
         if (isIdle) {
             synchronized (onMedialibraryReadyListeners) {
                 for (OnMedialibraryReadyListener listener : onMedialibraryReadyListeners) listener.onMedialibraryIdle();
diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index ed00f6036..223ef34e8 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -38,7 +38,7 @@ import android.text.TextUtils
 import android.util.Log
 import androidx.core.app.NotificationManagerCompat
 import androidx.core.content.ContextCompat
-import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.*
 import androidx.localbroadcastmanager.content.LocalBroadcastManager
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.Channel
@@ -56,8 +56,10 @@ private const val NOTIFICATION_DELAY = 1000L
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
-class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
+class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, LifecycleOwner {
+
     override val coroutineContext = Dispatchers.Main.immediate
+    private val dispatcher = ServiceLifecycleDispatcher(this)
     private lateinit var wakeLock: PowerManager.WakeLock
     private lateinit var localBroadcastManager: LocalBroadcastManager
 
@@ -89,6 +91,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
 
     @SuppressLint("WakelockTimeout")
     override fun onCreate() {
+        dispatcher.onServicePreSuperOnCreate()
         super.onCreate()
         localBroadcastManager = LocalBroadcastManager.getInstance(this)
         medialibrary = AbstractMedialibrary.getInstance()
@@ -97,15 +100,20 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
         filter.addAction(ACTION_PAUSE_SCAN)
         filter.addAction(ACTION_RESUME_SCAN)
         registerReceiver(receiver, filter)
-        localBroadcastManager.registerReceiver(receiver, IntentFilter(AbstractMedialibrary.ACTION_IDLE))
         val pm = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
         wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VLC:MediaParsigService")
         wakeLock.acquire()
 
         if (lastNotificationTime == 5L) stopSelf()
+        AbstractMedialibrary.getState().observe(this, Observer<Boolean> { running ->
+            if (!running && !scanPaused) {
+                exitCommand()
+            }
+        })
     }
 
     override fun onBind(intent: Intent): IBinder? {
+        dispatcher.onServicePreSuperOnBind()
         return binder
     }
 
@@ -119,6 +127,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
 
         if (AndroidUtil.isOOrLater) forceForeground()
         else if (lastNotificationTime <= 0L) lastNotificationTime = System.currentTimeMillis()
+        dispatcher.onServicePreSuperOnStart()
         when (intent.action) {
             ACTION_INIT -> {
                 val upgrade = intent.getBooleanExtra(EXTRA_UPGRADE, false)
@@ -341,6 +350,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
     }
 
     override fun onDestroy() {
+        dispatcher.onServicePreSuperOnDestroy()
         notificationActor.offer(Hide)
         medialibrary.removeDeviceDiscoveryCb(this)
         unregisterReceiver(receiver)
@@ -403,12 +413,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
                     medialibrary.resumeBackgroundOperations()
                     scanPaused = false
                 }
-                AbstractMedialibrary.ACTION_IDLE -> if (intent.getBooleanExtra(AbstractMedialibrary.STATE_IDLE, true)) {
-                    if (!scanPaused) {
-                        exitCommand()
-                        return
-                    }
-                }
             }
         }
     }
@@ -421,6 +425,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
         return false
     }
 
+    override fun getLifecycle(): Lifecycle = dispatcher.lifecycle
+
     companion object {
         val progress = MutableLiveData<ScanProgress>()
         val newStorages = MutableLiveData<MutableList<String>>()
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index ad181276b..dde462c34 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -41,11 +41,8 @@ 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.LiveData
-import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.*
 import androidx.lifecycle.Observer
-import androidx.lifecycle.ServiceLifecycleDispatcher
 import androidx.media.MediaBrowserServiceCompat
 import androidx.media.session.MediaButtonReceiver
 import kotlinx.coroutines.*
@@ -1241,7 +1238,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     @MainThread
     fun setVideoAspectRatio(aspect: String?) = playlistManager.player.setVideoAspectRatio(aspect)
 
-    override fun getLifecycle() = dispatcher.lifecycle!!
+    override fun getLifecycle(): Lifecycle = dispatcher.lifecycle
 
     /*
      * Browsing



More information about the Android mailing list