[Android] External devices code cleanup

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 18 19:39:49 2020 +0100| [25a5b1b7f9abc8a2102257b28d92a10ad900c534] | committer: Geoffrey Métais

External devices code cleanup

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

 .../videolan/television/viewmodel/MainTvModel.kt   |  7 +-----
 .../src/org/videolan/vlc/ExternalMonitor.kt        | 28 ++++++++++++----------
 .../vlc/gui/browser/FileBrowserFragment.kt         |  2 +-
 .../videolan/vlc/providers/FileBrowserProvider.kt  |  2 +-
 4 files changed, 19 insertions(+), 20 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 afa3e3294..5c40cd696 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
@@ -103,8 +103,6 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
         if (!updateActor.isClosedForSend) updateActor.offer(Unit)
     }
 
-    private val monitorObserver = Observer<Any> { updateActor.offer(Unit) }
-
     private val playerObserver = Observer<Boolean> { updateAudioCategories() }
 
     private val videoObserver = Observer<Any> { updateVideos() }
@@ -114,8 +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.storageUnplugged.observeForever(monitorObserver)
-        ExternalMonitor.storagePlugged.observeForever(monitorObserver)
+        ExternalMonitor.storageEvent.onEach { updateActor.offer(Unit) }.launchIn(viewModelScope)
         PlaylistManager.showAudioPlayer.observeForever(playerObserver)
         mediaMetadataRepository.getAllLive().observeForever(videoObserver)
     }
@@ -246,8 +243,6 @@ class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedi
         medialibrary.removeOnMedialibraryReadyListener(this)
         medialibrary.removeOnDeviceChangeListener(this)
         favorites.removeObserver(favObserver)
-        ExternalMonitor.storageUnplugged.removeObserver(monitorObserver)
-        ExternalMonitor.storagePlugged.removeObserver(monitorObserver)
         PlaylistManager.showAudioPlayer.removeObserver(playerObserver)
         nowPlayingDelegate.onClear()
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
index 073fd32a9..99f12b643 100644
--- a/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
+++ b/application/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
@@ -41,7 +41,10 @@ import androidx.core.content.ContextCompat
 import androidx.lifecycle.*
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.channels.ConflatedBroadcastChannel
 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
@@ -71,13 +74,13 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
             is MediaMounted -> {
                 if (TextUtils.isEmpty(action.uuid)) return at actor
                 if (ctx.getFromMl { addDevice(action.uuid, action.path, true) }) {
-                    notifyNewStorage(action.uri, action.path)
+                    notifyNewStorage(action)
                 }
             }
             is MediaUnmounted -> {
                 delay(100L)
                 Medialibrary.getInstance().removeDevice(action.uuid, action.path)
-                if (storageUnplugged.hasActiveObservers()) storageUnplugged.postValue(action.uri)
+                storageActions.safeOffer(action)
             }
         }
     }
@@ -110,8 +113,9 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
         }
     }
 
-    val storageUnplugged = LiveEvent<Uri>()
-    val storagePlugged = LiveEvent<Uri>()
+    private val storageActions = ConflatedBroadcastChannel<DeviceAction>()
+    val storageEvent : Flow<DeviceAction>
+        get() = storageActions.openSubscription().consumeAsFlow()
     private var storageObserver: WeakReference<Activity>? = null
 
     var devices = LiveDataset<UsbDevice>()
@@ -156,10 +160,10 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
     }
 
     @Synchronized
-    private fun notifyNewStorage(uri: Uri, path: String) {
+    private fun notifyNewStorage(mediaMounted: MediaMounted) {
         val activity = storageObserver?.get() ?: return
-        UiTools.newStorageDetected(activity, path)
-        if (storagePlugged.hasActiveObservers()) storagePlugged.postValue(uri)
+        UiTools.newStorageDetected(activity, mediaMounted.path)
+        storageActions.safeOffer(mediaMounted)
     }
 
     @Synchronized
@@ -169,8 +173,8 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
 
     @Synchronized
     fun unsubscribeStorageCb(observer: Activity) {
-        if (storageObserver != null && storageObserver!!.get() === observer) {
-            storageObserver!!.clear()
+        if (storageObserver?.get() === observer) {
+            storageObserver?.clear()
             storageObserver = null
         }
     }
@@ -182,6 +186,6 @@ fun containsDevice(devices: Array<String>, device: String): Boolean {
     return false
 }
 
-private sealed class DeviceAction
-private class MediaMounted(val uri : Uri, val path : String = uri.path!!, val uuid : String = uri.lastPathSegment!!) : DeviceAction()
-private class MediaUnmounted(val uri : Uri, val path : String = uri.path!!, val uuid : String = uri.lastPathSegment!!) : DeviceAction()
+sealed class DeviceAction
+class MediaMounted(val uri : Uri, val path : String = uri.path!!, val uuid : String = uri.lastPathSegment!!) : DeviceAction()
+class MediaUnmounted(val uri : Uri, val path : String = uri.path!!, val uuid : String = uri.lastPathSegment!!) : DeviceAction()
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 f52ee9019..af34cc1e6 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
@@ -117,7 +117,7 @@ open class FileBrowserFragment : BaseBrowserFragment() {
                 val title = getString(R.string.otg_device_title)
                 val otgRoot = OtgAccess.otgRoot
                 val rootUri = otgRoot.value
-                if (rootUri != null && ExternalMonitor.devices.value.size == 1) {
+                if (rootUri != null && ExternalMonitor.devices.size == 1) {
                     browseOtgDevice(rootUri, title)
                 } else {
                     otgRoot.observeForever(object : Observer<Uri> {
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index 0d3daaf8c..997ac66c1 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -179,7 +179,7 @@ open class FileBrowserProvider(
     }
 
     override fun onChanged(list: MutableList<UsbDevice>?) {
-        if (list?.isEmpty() != false) {
+        if (!list.isNullOrEmpty()) {
             if (otgPosition != -1) {
                 dataset.remove(otgPosition)
                 otgPosition = -1



More information about the Android mailing list