Skip to content
Snippets Groups Projects
Commit 05adcfd6 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Add a callback for ext devices (un)mount

parent 2cf0936b
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
}
......@@ -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);
}
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment