[Android] TV: Leave browser when browsed device is unplugged

Geoffrey Métais git at videolan.org
Thu Dec 6 15:21:10 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Dec  6 15:20:20 2018 +0100| [3b473997731a43f31598653ee19199b95d0c09c9] | committer: Geoffrey Métais

TV: Leave browser when browsed device is unplugged

Fix #462

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

 .../src/org/videolan/vlc/ExternalMonitor.kt        |  9 ++++++---
 .../gui/tv/browser/DirectoryBrowserFragment.java   | 23 +++++++++++++++++++---
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt b/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
index 4445f3835..775398b94 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
@@ -129,7 +129,10 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
             Intent.ACTION_MEDIA_UNMOUNTED,
             Intent.ACTION_MEDIA_EJECT -> {
                 if (AndroidDevices.watchDevices || storageObserver != null && storageObserver!!.get() != null)
-                    intent.data?.let { actor.offer(MediaUnmounted(it)) }
+                    intent.data?.let {
+                        actor.offer(MediaUnmounted(it))
+                        storageUnplugged.postValue(it)
+                    }
             }
             UsbManager.ACTION_USB_DEVICE_ATTACHED -> {
                 if (intent.hasExtra(UsbManager.EXTRA_DEVICE)) {
@@ -167,8 +170,8 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
         }
     }
 
-    @Volatile
-    var connected = MutableLiveData<Boolean>()
+    val connected = MutableLiveData<Boolean>()
+    val storageUnplugged = LiveEvent<Uri>()
     @Volatile
     var isMobile = true
         private set
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/DirectoryBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/DirectoryBrowserFragment.java
index b71836b5e..a25d23788 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/DirectoryBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/DirectoryBrowserFragment.java
@@ -24,19 +24,23 @@
 package org.videolan.vlc.gui.tv.browser;
 
 import android.annotation.TargetApi;
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProviders;
+import android.app.Activity;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
-import androidx.annotation.Nullable;
 
 import org.videolan.medialibrary.media.MediaLibraryItem;
+import org.videolan.vlc.ExternalMonitor;
 import org.videolan.vlc.viewmodels.browser.BrowserModel;
 import org.videolan.vlc.viewmodels.browser.BrowserModelKt;
 
 import java.util.List;
 import java.util.Map;
 
+import androidx.annotation.Nullable;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProviders;
+
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 public class DirectoryBrowserFragment extends MediaSortedFragment<BrowserModel> {
 
@@ -52,5 +56,18 @@ public class DirectoryBrowserFragment extends MediaSortedFragment<BrowserModel>
                 if (stringListMap != null) update(stringListMap);
             }
         });
+        ExternalMonitor.INSTANCE.getStorageUnplugged().observe(this, new Observer<Uri>() {
+            @Override
+            public void onChanged(Uri uri) {
+                if (mUri != null && "file".equals(mUri.getScheme())) {
+                    final String currentPath = mUri.getPath();
+                    final String unpluggedPath = uri.getPath();
+                    if (currentPath != null && unpluggedPath != null && currentPath.startsWith(unpluggedPath)) {
+                        final Activity activity = getActivity();
+                        if (activity != null) activity.finish();
+                    }
+                }
+            }
+        });
     }
 }



More information about the Android mailing list