[Android] Fix storages channel

Geoffrey Métais git at videolan.org
Fri Feb 21 14:34:14 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 18 22:35:58 2020 +0100| [bb72dc3428c25d4e2eec30324023adb15407fedc] | committer: Geoffrey Métais

Fix storages channel

ConflatedBroadcastChannel is not suited as it would persist the last event

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

 .../videolan/television/viewmodel/MainTvModel.kt   |  2 +-
 .../src/org/videolan/vlc/ExternalMonitor.kt        | 28 ++++++++++------------
 2 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt b/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
index 5c40cd696..cc3052242 100644
--- a/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
+++ b/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
@@ -112,7 +112,7 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
         medialibrary.addOnDeviceChangeListener(this)
         favorites.observeForever(favObserver)
         networkMonitor.connectionFlow.onEach { updateActor.offer(Unit) }.launchIn(viewModelScope)
-        ExternalMonitor.storageEvent.onEach { updateActor.offer(Unit) }.launchIn(viewModelScope)
+        ExternalMonitor.storageEvents.onEach { updateActor.offer(Unit) }.launchIn(viewModelScope)
         PlaylistManager.showAudioPlayer.observeForever(playerObserver)
         mediaMetadataRepository.getAllLive().observeForever(videoObserver)
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
index 99f12b643..a1057efe7 100644
--- a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
+++ b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
@@ -24,7 +24,6 @@
 package org.videolan.vlc
 
 import android.annotation.SuppressLint
-import android.annotation.TargetApi
 import android.app.Activity
 import android.content.BroadcastReceiver
 import android.content.Context
@@ -32,32 +31,31 @@ import android.content.Intent
 import android.content.IntentFilter
 import android.hardware.usb.UsbDevice
 import android.hardware.usb.UsbManager
-import android.net.ConnectivityManager
-import android.net.NetworkCapabilities
 import android.net.Uri
-import android.os.Build
 import android.text.TextUtils
 import androidx.core.content.ContextCompat
-import androidx.lifecycle.*
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleObserver
+import androidx.lifecycle.OnLifecycleEvent
+import androidx.lifecycle.ProcessLifecycleOwner
 import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.BroadcastChannel
 import kotlinx.coroutines.channels.Channel
-import kotlinx.coroutines.channels.ConflatedBroadcastChannel
+import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
+import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
 import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.consumeAsFlow
-import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.resources.ACTION_CHECK_STORAGES
 import org.videolan.resources.AppContextProvider
+import org.videolan.resources.util.getFromMl
 import org.videolan.tools.*
 import org.videolan.tools.livedata.LiveDataset
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.helpers.hf.OtgAccess
-import org.videolan.resources.util.getFromMl
 import videolan.org.commontools.LiveEvent
 import java.lang.ref.WeakReference
-import java.net.NetworkInterface
-import java.net.SocketException
 
 private const val TAG = "VLC/ExternalMonitor"
 
@@ -80,7 +78,7 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
             is MediaUnmounted -> {
                 delay(100L)
                 Medialibrary.getInstance().removeDevice(action.uuid, action.path)
-                storageActions.safeOffer(action)
+                storageChannel.safeOffer(action)
             }
         }
     }
@@ -113,9 +111,9 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
         }
     }
 
-    private val storageActions = ConflatedBroadcastChannel<DeviceAction>()
-    val storageEvent : Flow<DeviceAction>
-        get() = storageActions.openSubscription().consumeAsFlow()
+    private val storageChannel = BroadcastChannel<DeviceAction>(BUFFERED)
+    val storageEvents : Flow<DeviceAction>
+        get() = storageChannel.openSubscription().consumeAsFlow()
     private var storageObserver: WeakReference<Activity>? = null
 
     var devices = LiveDataset<UsbDevice>()
@@ -163,7 +161,7 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
     private fun notifyNewStorage(mediaMounted: MediaMounted) {
         val activity = storageObserver?.get() ?: return
         UiTools.newStorageDetected(activity, mediaMounted.path)
-        storageActions.safeOffer(mediaMounted)
+        storageChannel.safeOffer(mediaMounted)
     }
 
     @Synchronized



More information about the Android mailing list