[Android] Add a callback for ext devices (un)mount

Geoffrey Métais git at videolan.org
Wed Dec 5 11:29:56 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Dec  4 18:22:57 2018 +0100| [05adcfd6780fb17b9f2a2b1769aceb91c04a6153] | committer: Geoffrey Métais

Add a callback for ext devices (un)mount

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

 .../org/videolan/medialibrary/Medialibrary.java    | 32 ++++++++++++++++++++--
 .../src/org/videolan/medialibrary/Tools.java       |  9 ++++--
 .../videolan/vlc/viewmodels/MedialibraryModel.kt   | 12 +++++++-
 3 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index a09464277..02fa11152 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -83,6 +83,7 @@ public class Medialibrary {
     private final List<GenresCb> mGenreCbs = new ArrayList<>();
     private final List<PlaylistsCb> mPlaylistCbs = new ArrayList<>();
     private final List<OnMedialibraryReadyListener> onMedialibraryReadyListeners = new ArrayList<>();
+    private final List<OnDeviceChangeListener> onDeviceChangeListeners = new ArrayList<>();
     private volatile boolean isMedialibraryStarted = false;
     private final List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
     private final List<EntryPointsEventsCb> entryPointsEventsCbList = new ArrayList<>();
@@ -145,7 +146,12 @@ public class Medialibrary {
     }
 
     public boolean addDevice(@NonNull String uuid, @NonNull String path, boolean removable) {
-        return nativeAddDevice(Tools.encodeVLCMrl(uuid), Tools.encodeVLCMrl(path), removable);
+        if (!mIsInitiated) return false;
+        final boolean added = nativeAddDevice(Tools.encodeVLCString(uuid), Tools.encodeVLCMrl(path), removable);
+        synchronized (onDeviceChangeListeners) {
+            for (OnDeviceChangeListener listener : onDeviceChangeListeners) listener.onDeviceChange();
+        }
+        return added;
     }
 
     public void discover(@NonNull String path) {
@@ -168,7 +174,12 @@ public class Medialibrary {
     }
 
     public boolean removeDevice(String uuid, String path) {
-        return mIsInitiated && !TextUtils.isEmpty(uuid) && !TextUtils.isEmpty(path) && nativeRemoveDevice(Tools.encodeVLCMrl(uuid), Tools.encodeVLCMrl(path));
+        if (!mIsInitiated) return false;
+        final boolean removed = !TextUtils.isEmpty(uuid) && !TextUtils.isEmpty(path) && nativeRemoveDevice(Tools.encodeVLCString(uuid), Tools.encodeVLCMrl(path));
+        synchronized (onDeviceChangeListeners) {
+            for (OnDeviceChangeListener listener : onDeviceChangeListeners) listener.onDeviceChange();
+        }
+        return removed;
     }
 
     @Override
@@ -856,6 +867,19 @@ public class Medialibrary {
         }
     }
 
+    public void addOnDeviceChangeListener(OnDeviceChangeListener listener) {
+        synchronized (onDeviceChangeListeners) {
+            if (!onDeviceChangeListeners.contains(listener))
+                onDeviceChangeListeners.add(listener);
+        }
+    }
+
+    public void removeOnDeviceChangeListener(OnDeviceChangeListener listener) {
+        synchronized (onDeviceChangeListeners) {
+            onDeviceChangeListeners.remove(listener);
+        }
+    }
+
     public static String[] getBlackList() {
         return new String[] {
                 "/Android/data/",
@@ -1011,4 +1035,8 @@ public class Medialibrary {
         void onMedialibraryReady();
         void onMedialibraryIdle();
     }
+
+    public interface OnDeviceChangeListener {
+        void onDeviceChange();
+    }
 }
diff --git a/medialibrary/src/org/videolan/medialibrary/Tools.java b/medialibrary/src/org/videolan/medialibrary/Tools.java
index 15aa611a2..dffd8f9b0 100644
--- a/medialibrary/src/org/videolan/medialibrary/Tools.java
+++ b/medialibrary/src/org/videolan/medialibrary/Tools.java
@@ -3,7 +3,6 @@ package org.videolan.medialibrary;
 
 import android.net.Uri;
 import android.os.Environment;
-import androidx.annotation.Nullable;
 import android.text.TextUtils;
 
 import org.videolan.medialibrary.media.MediaLibraryItem;
@@ -13,6 +12,8 @@ import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.util.Locale;
 
+import androidx.annotation.Nullable;
+
 public class Tools {
 
     private static final String TAG = "VLC/Tools";
@@ -121,8 +122,12 @@ public class Tools {
         return sb.toString();
     }
 
+    public static String encodeVLCString(String mrl) {
+        return Uri.encode(Uri.decode(mrl), ".-_~/()&!$*+,;='@:");
+    }
+
     public static String encodeVLCMrl(String mrl) {
         if (mrl.startsWith("/")) mrl = "file://"+mrl;
-        return Uri.encode(Uri.decode(mrl), ".-_~/()&!$*+,;='@:");
+        return encodeVLCString(mrl);
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
index 4f8d3cf94..2a355e880 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
@@ -21,17 +21,19 @@
 package org.videolan.vlc.viewmodels
 
 import android.content.Context
+import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 
 
-abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), Medialibrary.OnMedialibraryReadyListener {
+abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
 
     val medialibrary = Medialibrary.getInstance()
 
     override fun fetch() {
         medialibrary.addOnMedialibraryReadyListener(this)
+        medialibrary.addOnDeviceChangeListener(this)
         if (medialibrary.isStarted) onMedialibraryReady()
     }
 
@@ -43,8 +45,16 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
         launch { refresh() }
     }
 
+    override fun onDeviceChange() {
+        launch {
+            delay(1000L)
+            refresh()
+        }
+    }
+
     override fun onCleared() {
         medialibrary.removeOnMedialibraryReadyListener(this)
+        medialibrary.removeOnDeviceChangeListener(this)
         super.onCleared()
     }
 }
\ No newline at end of file



More information about the Android mailing list