[Android] Browse OTG device directly if already set

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jul 31 18:49:14 2018 +0200| [6d49723f32d71fc09e2fb883befcc22c521e1387] | committer: Geoffrey Métais

Browse OTG device directly if already set

In case we have 1 single USB device plugged

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

 .../java/videolan/org/commontools/LiveEvent.kt     |  2 +-
 .../src/org/videolan/vlc/ExternalMonitor.java      |  8 +++-
 .../vlc/gui/browser/FileBrowserFragment.java       | 44 ++++++++++++++--------
 .../videolan/vlc/providers/FileBrowserProvider.kt  |  2 +-
 4 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
index a4d7eaa48..9ae7e6e89 100644
--- a/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
+++ b/tools/src/main/java/videolan/org/commontools/LiveEvent.kt
@@ -27,6 +27,6 @@ class LiveEvent<T> : MutableLiveData<T>() {
 
     @MainThread
     fun call() {
-        value = null
+        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 29b2f7f9c..f063408b8 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
@@ -49,6 +49,7 @@ import android.text.TextUtils;
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.Medialibrary;
 import org.videolan.vlc.gui.helpers.UiTools;
+import org.videolan.vlc.gui.helpers.hf.OtgAccess;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.LiveDataset;
 import org.videolan.vlc.util.Strings;
@@ -60,6 +61,8 @@ import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.Enumeration;
 
+import videolan.org.commontools.LiveEvent;
+
 public class ExternalMonitor extends BroadcastReceiver implements LifecycleObserver {
     public final static String TAG = "VLC/ExternalMonitor";
     public static volatile MutableLiveData<Boolean> connected = new MutableLiveData<>();
@@ -68,7 +71,7 @@ public class ExternalMonitor extends BroadcastReceiver implements LifecycleObser
     private static final ExternalMonitor instance = new ExternalMonitor();
     private static WeakReference<Activity> storageObserver = null;
 
-    public static LiveDataset<UsbDevice> devices;
+    public static LiveDataset<UsbDevice> devices = new LiveDataset<>();
 
     public ExternalMonitor() {
         ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
@@ -101,7 +104,6 @@ public class ExternalMonitor extends BroadcastReceiver implements LifecycleObser
                 }
             });
         }
-        devices = new LiveDataset<>();
         final UsbManager usbManager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
         devices.add(new ArrayList<>(usbManager.getDeviceList().values()));
     }
@@ -111,6 +113,7 @@ public class ExternalMonitor extends BroadcastReceiver implements LifecycleObser
         final Context ctx = VLCApplication.getAppContext();
         ctx.unregisterReceiver(instance);
         connected.setValue(false);
+        devices.clear();
     }
 
     public static ExternalMonitor getInstance() {
@@ -150,6 +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();
                     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 16a7088da..2d553112c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -23,6 +23,7 @@
 
 package org.videolan.vlc.gui.browser;
 
+import android.arch.lifecycle.LiveData;
 import android.arch.lifecycle.Observer;
 import android.arch.lifecycle.ViewModelProviders;
 import android.net.Uri;
@@ -35,6 +36,7 @@ import android.view.View;
 import org.jetbrains.annotations.NotNull;
 import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.medialibrary.media.MediaWrapper;
+import org.videolan.vlc.ExternalMonitor;
 import org.videolan.vlc.R;
 import org.videolan.vlc.gui.helpers.hf.OtgAccess;
 import org.videolan.vlc.util.AndroidDevices;
@@ -92,25 +94,37 @@ public class FileBrowserFragment extends BaseBrowserFragment {
             final MediaWrapper mw = (MediaWrapper) item;
             if ("otg://".equals(mw.getLocation())) {
                 final String title = getString(R.string.otg_device_title);
-                OtgAccess.Companion.getOtgRoot().observeForever(new Observer<Uri>() {
-                    @Override
-                    public void onChanged(@Nullable Uri uri) {
-                        OtgAccess.Companion.getOtgRoot().removeObserver(this);
-                        final MediaWrapper mw = new MediaWrapper(uri);
-                        mw.setType(MediaWrapper.TYPE_DIR);
-                        mw.setTitle(title);
-                        getHandler().post(new Runnable() {
-                            @Override
-                            public void run() {
-                                browse(mw, true);
+                final LiveData<Uri> otgRoot = OtgAccess.Companion.getOtgRoot();
+                final Uri rootUri = otgRoot.getValue();
+                if (rootUri != null && ExternalMonitor.devices.getValue().size() == 1) {
+                    browseOtgDevice(rootUri, title);
+                } else {
+                    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.requestOtgRoot(requireActivity());
+                        }
+                    });
+                    OtgAccess.Companion.requestOtgRoot(requireActivity());
+                }
                 return;
             }
         }
         super.onClick(v, position, item);
     }
+
+    private void browseOtgDevice(@NotNull Uri uri, @NotNull String title) {
+        final MediaWrapper mw = new MediaWrapper(uri);
+        mw.setType(MediaWrapper.TYPE_DIR);
+        mw.setTitle(title);
+        getHandler().post(new Runnable() {
+            @Override
+            public void run() {
+                browse(mw, true);
+            }
+        });
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index a8a868886..c2beccd92 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -119,7 +119,7 @@ open class FileBrowserProvider(dataset: LiveDataset<MediaLibraryItem>, url: Stri
     override fun refresh() = true
 
     override fun release(): Job {
-        ExternalMonitor.devices.removeObserver(this)
+        if (url == null) ExternalMonitor.devices.removeObserver(this)
         return super.release()
     }
 



More information about the Android mailing list