[Android] Fix ConflatedBroadcastChannel consumption as Flow

Geoffrey Métais git at videolan.org
Fri Mar 13 14:55:39 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar 13 14:45:36 2020 +0100| [0271c9ddeb45bed8189a3abb94d5beedaabdbe5a] | committer: Geoffrey Métais

Fix ConflatedBroadcastChannel consumption as Flow

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

 .../src/main/java/org/videolan/tools/NetworkMonitor.kt   |  4 ++--
 .../vlc-android/src/org/videolan/vlc/ExternalMonitor.kt  |  4 ++--
 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt  | 16 ++++++++++------
 .../org/videolan/vlc/gui/browser/FileBrowserFragment.kt  |  3 ++-
 4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/NetworkMonitor.kt b/application/tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
index 3ad31a61a..8505cff2d 100644
--- a/application/tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
+++ b/application/tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
@@ -15,7 +15,7 @@ import androidx.lifecycle.OnLifecycleEvent
 import androidx.lifecycle.ProcessLifecycleOwner
 import kotlinx.coroutines.channels.ConflatedBroadcastChannel
 import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.flow.consumeAsFlow
+import kotlinx.coroutines.flow.asFlow
 import java.net.NetworkInterface
 import java.net.SocketException
 
@@ -24,7 +24,7 @@ class NetworkMonitor(private val context: Context) : LifecycleObserver {
     private val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
     val connection = ConflatedBroadcastChannel(Connection(connected = false, mobile = true, vpn = false))
     val connectionFlow : Flow<Connection>
-        get() = connection.openSubscription().consumeAsFlow()
+        get() = connection.asFlow()
     val connected : Boolean
         get() = connection.value.connected
     val isLan : Boolean
diff --git a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
index b8c491d14..27f7bf50c 100644
--- a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
+++ b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
@@ -44,7 +44,7 @@ import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
 import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.flow.consumeAsFlow
+import kotlinx.coroutines.flow.asFlow
 import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.resources.ACTION_CHECK_STORAGES
 import org.videolan.resources.AppContextProvider
@@ -111,7 +111,7 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
 
     private val storageChannel = BroadcastChannel<DeviceAction>(BUFFERED)
     val storageEvents : Flow<DeviceAction>
-        get() = storageChannel.openSubscription().consumeAsFlow()
+        get() = storageChannel.asFlow()
     private var storageObserver: WeakReference<Activity>? = null
 
     var devices = LiveDataset<UsbDevice>()
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index c0777d04f..f6026b635 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -44,16 +44,20 @@ import androidx.annotation.RequiresApi
 import androidx.core.app.NotificationManagerCompat
 import androidx.core.app.ServiceCompat
 import androidx.core.content.ContextCompat
-import androidx.lifecycle.*
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.Observer
+import androidx.lifecycle.ServiceLifecycleDispatcher
+import androidx.lifecycle.lifecycleScope
 import androidx.media.MediaBrowserServiceCompat
 import androidx.media.session.MediaButtonReceiver
 import kotlinx.coroutines.*
-import kotlinx.coroutines.channels.*
-import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.channels.ConflatedBroadcastChannel
+import kotlinx.coroutines.channels.SendChannel
+import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.flow.consumeAsFlow
-import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.flow.asFlow
 import org.videolan.libvlc.MediaPlayer
 import org.videolan.libvlc.RendererItem
 import org.videolan.libvlc.interfaces.IMedia
@@ -1314,7 +1318,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
         val instance : PlaybackService?
             get() = instanceChannel.valueOrNull
         val serviceFlow : Flow<PlaybackService?>
-            get() = instanceChannel.openSubscription().consumeAsFlow()
+            get() = instanceChannel.asFlow()
 
         val renderer = RendererLiveData()
         val restartPlayer = LiveEvent<Boolean>()
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.kt
index f75c0fee0..096fc2bab 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.kt
@@ -34,6 +34,7 @@ import androidx.lifecycle.lifecycleScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.flow.asFlow
 import kotlinx.coroutines.flow.consumeAsFlow
 import kotlinx.coroutines.flow.filterNotNull
 import kotlinx.coroutines.flow.first
@@ -121,7 +122,7 @@ open class FileBrowserFragment : BaseBrowserFragment() {
                     browseOtgDevice(rootUri, title)
                 } else {
                     lifecycleScope.launchWhenStarted {
-                        val otgRoot = OtgAccess.otgRoot.openSubscription().consumeAsFlow()
+                        val otgRoot = OtgAccess.otgRoot.asFlow()
                         val uri = otgRoot.filterNotNull().first()
                         browseOtgDevice(uri, title)
                     }



More information about the Android mailing list