[Android] DeviceLister refactoring for new API

Geoffrey Métais git at videolan.org
Thu Mar 26 18:07:52 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar 18 16:15:02 2020 +0100| [ff41347b509b372756352be5a92869284e105f76] | committer: Geoffrey Métais

DeviceLister refactoring for new API

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

 medialibrary/jni/AndroidDeviceLister.cpp | 36 +++++++++++++++++++++++++++-----
 medialibrary/jni/AndroidDeviceLister.h   |  8 +++++--
 medialibrary/jni/AndroidMediaLibrary.cpp | 12 ++++-------
 medialibrary/jni/AndroidMediaLibrary.h   |  1 -
 4 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/medialibrary/jni/AndroidDeviceLister.cpp b/medialibrary/jni/AndroidDeviceLister.cpp
index 71855bfe6..9f7500ad4 100644
--- a/medialibrary/jni/AndroidDeviceLister.cpp
+++ b/medialibrary/jni/AndroidDeviceLister.cpp
@@ -6,21 +6,47 @@ AndroidDeviceLister::devices() const
     std::lock_guard<std::mutex> guard(m_mutex);
     std::vector<std::tuple<std::string, std::string, bool>> devices;
     devices.reserve( m_devices.size() );
-    for(auto kv : m_devices)
-        devices.push_back(kv.second);
+    for(auto kv : m_devices) devices.push_back(kv.second);
     return devices;
 }
 
-void
+bool
 AndroidDeviceLister::addDevice(std::string uuid, std::string path, bool removable)
 {
     std::lock_guard<std::mutex> guard(m_mutex);
     m_devices.insert(std::make_pair(uuid, std::make_tuple(uuid, path, removable)));
+    return p_DeviceListerCb != nullptr && p_DeviceListerCb->onDeviceMounted(uuid, path, removable);
 }
 
 bool
-AndroidDeviceLister::removeDevice(std::string uuidToRemove)
+AndroidDeviceLister::removeDevice(std::string uuidToRemove, const std::string& path)
 {
     std::lock_guard<std::mutex> guard(m_mutex);
-    return m_devices.erase(uuidToRemove);
+    auto iterator = m_devices.erase(uuidToRemove);
+    auto erased = iterator > 0;
+    if (erased && p_DeviceListerCb != nullptr) p_DeviceListerCb->onDeviceUnmounted(uuidToRemove, path);
+    return erased;
+}
+
+bool AndroidDeviceLister::start( medialibrary::IDeviceListerCb* cb )
+{
+    p_DeviceListerCb = cb;
+    return true;
+}
+
+void AndroidDeviceLister::stop()
+{
+
+}
+
+void AndroidDeviceLister::refresh()
+{
+    if (p_DeviceListerCb == nullptr) return;
+    std::string uuid;
+    std::string path;
+    bool removable;
+    for (auto d : m_devices ) {
+        std::tie(uuid, path, removable) = d.second;
+        p_DeviceListerCb->onDeviceMounted(uuid, path, removable);
+    }
 }
diff --git a/medialibrary/jni/AndroidDeviceLister.h b/medialibrary/jni/AndroidDeviceLister.h
index df979be92..b7561c0d7 100644
--- a/medialibrary/jni/AndroidDeviceLister.h
+++ b/medialibrary/jni/AndroidDeviceLister.h
@@ -11,14 +11,18 @@ class AndroidDeviceLister : public medialibrary::IDeviceLister
 {
 public:
     std::vector<std::tuple<std::string, std::string, bool>> devices() const;
-    void addDevice(std::string, std::string, bool);
-    bool removeDevice(std::string uuidToRemove);
+    bool addDevice(std::string, std::string, bool);
+    bool removeDevice(std::string uuidToRemove, const std::string& path);
+    void refresh();
+    bool start( medialibrary::IDeviceListerCb* cb );
+    void stop();
 
 private:
     std::unordered_map<std::string, std::tuple<std::string, std::string, bool>> m_devices;
 
 private:
     mutable std::mutex m_mutex;
+    medialibrary::IDeviceListerCb* p_DeviceListerCb = nullptr;
 };
 
 #endif // ANDROIDDEVICELISTER_H
diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 4c28d7a2f..97a152615 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -49,14 +49,14 @@ AndroidMediaLibrary::~AndroidMediaLibrary()
 medialibrary::InitializeResult
 AndroidMediaLibrary::initML(const std::string& dbPath, const std::string& thumbsPath)
 {
-    p_DeviceListerCb = p_ml->setDeviceLister(p_lister);
+    p_ml->registerDeviceLister(p_lister, "file://");
     return p_ml->initialize(dbPath, thumbsPath, this);
 }
 
 void
 AndroidMediaLibrary::start()
 {
-    p_ml->start();
+//    p_ml->start();
     m_started = true;
 }
 
@@ -69,8 +69,7 @@ AndroidMediaLibrary::clearDatabase(bool restorePlaylists) {
 bool
 AndroidMediaLibrary::addDevice(const std::string& uuid, const std::string& path, bool removable)
 {
-    p_lister->addDevice(uuid, path, removable);
-    return p_DeviceListerCb != nullptr && (p_DeviceListerCb->onDeviceMounted(uuid, path));
+    return p_lister->addDevice(uuid, path, removable);
 }
 
 std::vector<std::tuple<std::string, std::string, bool>>
@@ -82,10 +81,7 @@ AndroidMediaLibrary::devices()
 bool
 AndroidMediaLibrary::removeDevice(const std::string& uuid, const std::string& path)
 {
-    bool removed = p_lister->removeDevice(uuid);
-    if (removed && p_DeviceListerCb != nullptr)
-        p_DeviceListerCb->onDeviceUnmounted(uuid, path);
-    return removed;
+    return p_lister->removeDevice(uuid, path);
 }
 
 void
diff --git a/medialibrary/jni/AndroidMediaLibrary.h b/medialibrary/jni/AndroidMediaLibrary.h
index b1302837c..5e71dd483 100644
--- a/medialibrary/jni/AndroidMediaLibrary.h
+++ b/medialibrary/jni/AndroidMediaLibrary.h
@@ -169,7 +169,6 @@ private:
     fields *p_fields;
     medialibrary::IMediaLibrary* p_ml;
     std::shared_ptr<AndroidDeviceLister> p_lister;
-    medialibrary::IDeviceListerCb* p_DeviceListerCb = nullptr;
     bool m_paused = false;
     uint32_t m_nbDiscovery = 0, m_progress = 0, m_mediaAddedType = 0, m_mediaUpdatedType = 0;
 };



More information about the Android mailing list