[Android] Fix otg root setting event

Geoffrey Métais git at videolan.org
Wed Aug 1 13:12:06 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Aug  1 13:11:09 2018 +0200| [93e09f3d45061da326395ed65ab1c19aaffd1a70] | committer: Geoffrey Métais

Fix otg root setting event

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

 tools/src/main/java/videolan/org/commontools/LiveEvent.kt  | 12 ++++++++----
 vlc-android/src/org/videolan/vlc/ExternalMonitor.java      |  2 +-
 .../org/videolan/vlc/gui/browser/FileBrowserFragment.java  |  6 ++----
 .../src/org/videolan/vlc/providers/FileBrowserProvider.kt  | 14 ++++++--------
 4 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
index 9ae7e6e89..e4e15b838 100644
--- a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
+++ b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
@@ -16,7 +16,13 @@ class LiveEvent<T> : MutableLiveData<T>() {
     override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
         if (hasActiveObservers()) Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
         // Observe the internal MutableLiveData
-        super.observe(owner, Observer<T> { t -> if (pending.compareAndSet(true, false)) observer.onChanged(t) })
+        super.observe(owner, Observer<T> { t ->
+            if (pending.compareAndSet(true, false)) observer.onChanged(t)
+        })
+    }
+
+    override fun observeForever(observer: Observer<T>) {
+        super.observeForever { if (pending.compareAndSet(true, false)) observer.onChanged(it) }
     }
 
     @MainThread
@@ -26,7 +32,5 @@ class LiveEvent<T> : MutableLiveData<T>() {
     }
 
     @MainThread
-    fun call() {
-        super.setValue(null)
-    }
+    fun clear() = super.setValue(null)
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
index f063408b8..9f29c664a 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
@@ -153,7 +153,7 @@ public class ExternalMonitor extends BroadcastReceiver implements LifecycleObser
                 break;
             case UsbManager.ACTION_USB_DEVICE_DETACHED:
                 if (intent.hasExtra(UsbManager.EXTRA_DEVICE)) {
-                    ((LiveEvent<Uri>)OtgAccess.Companion.getOtgRoot()).call();
+                    ((LiveEvent<Uri>)OtgAccess.Companion.getOtgRoot()).clear();
                     final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                     devices.remove(device);
                 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 2d553112c..9e4fcab4b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -102,10 +102,8 @@ public class FileBrowserFragment extends BaseBrowserFragment {
                     otgRoot.observeForever(new Observer<Uri>() {
                         @Override
                         public void onChanged(@Nullable Uri uri) {
-                            if (uri != null) {
-                                OtgAccess.Companion.getOtgRoot().removeObserver(this);
-                                browseOtgDevice(uri, title);
-                            }
+                            OtgAccess.Companion.getOtgRoot().removeObserver(this);
+                            if (uri != null) browseOtgDevice(uri, title);
                         }
                     });
                     OtgAccess.Companion.requestOtgRoot(requireActivity());
diff --git a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index c2beccd92..f90ac10d3 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -129,15 +129,13 @@ open class FileBrowserProvider(dataset: LiveDataset<MediaLibraryItem>, url: Stri
                 dataset.remove(otgPosition)
                 otgPosition = -1
             }
-        } else {
-            if (otgPosition == -1) {
-                val otg = MediaWrapper(Uri.parse("otg://")).apply {
-                    title = VLCApplication.getAppResources().getString(R.string.otg_device_title)
-                    type = MediaWrapper.TYPE_DIR
-                }
-                otgPosition = storagePosition+1
-                dataset.add(otgPosition, otg)
+        } else if (otgPosition == -1) {
+            val otg = MediaWrapper(Uri.parse("otg://")).apply {
+                title = VLCApplication.getAppResources().getString(R.string.otg_device_title)
+                type = MediaWrapper.TYPE_DIR
             }
+            otgPosition = storagePosition+1
+            dataset.add(otgPosition, otg)
         }
     }
 



More information about the Android mailing list