From 1fefff6d1f99dbcf1a453424753ad5562fb675ef Mon Sep 17 00:00:00 2001 From: Jarkko Koivikko Date: Thu, 24 Mar 2022 06:12:38 +0200 Subject: [PATCH] Android: Keep the ParcelFileDescriptor open for content uris Detaching fd and closing the ParcelFileDescriptor prevents IO with the services like Google Drive on Android. Instead, the file system should keep the ParcelFileDescriptor open while the IO operations take place and close it manually. Also, prevent Qt from closing the handle for us. Pick-to: 6.2 6.3 Fixes: QTBUG-101996 Change-Id: Ie54c04ad5aa1e7ee5444a04c30ac1323f73047bb Reviewed-by: Assam Boudjelthia --- .../android/androidcontentfileengine.cpp | 28 +++++++++++++++---- .../android/androidcontentfileengine.h | 3 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/plugins/platforms/android/androidcontentfileengine.cpp b/src/plugins/platforms/android/androidcontentfileengine.cpp index b18c81e8964..c5ead186c90 100644 --- a/src/plugins/platforms/android/androidcontentfileengine.cpp +++ b/src/plugins/platforms/android/androidcontentfileengine.cpp @@ -71,17 +71,35 @@ bool AndroidContentFileEngine::open(QIODevice::OpenMode openMode, openModeStr += QLatin1Char('a'); } - const auto fd = QJniObject::callStaticMethod("org/qtproject/qt/android/QtNative", - "openFdForContentUrl", - "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)I", + m_pfd = QJniObject::callStaticObjectMethod("org/qtproject/qt/android/QtNative", + "openParcelFdForContentUrl", + "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", QAndroidApplication::context(), QJniObject::fromString(fileName(DefaultName)).object(), QJniObject::fromString(openModeStr).object()); - if (fd < 0) + if (!m_pfd.isValid()) return false; - return QFSFileEngine::open(openMode, fd, QFile::AutoCloseHandle); + const auto fd = m_pfd.callMethod("getFd", "()I"); + + if (fd < 0) { + m_pfd.callMethod("close", "()V"); + m_pfd = QJniObject(); + return false; + } + + return QFSFileEngine::open(openMode, fd, QFile::DontCloseHandle); +} + +bool AndroidContentFileEngine::close() +{ + if (m_pfd.isValid()) { + m_pfd.callMethod("close", "()V"); + m_pfd = QJniObject(); + } + + return QFSFileEngine::close(); } qint64 AndroidContentFileEngine::size() const diff --git a/src/plugins/platforms/android/androidcontentfileengine.h b/src/plugins/platforms/android/androidcontentfileengine.h index 531d0f80ff0..47730f56850 100644 --- a/src/plugins/platforms/android/androidcontentfileengine.h +++ b/src/plugins/platforms/android/androidcontentfileengine.h @@ -41,12 +41,14 @@ #define ANDROIDCONTENTFILEENGINE_H #include +#include class AndroidContentFileEngine : public QFSFileEngine { public: AndroidContentFileEngine(const QString &fileName); bool open(QIODevice::OpenMode openMode, std::optional permissions) override; + bool close() override; qint64 size() const override; FileFlags fileFlags(FileFlags type = FileInfoAll) const override; QString fileName(FileName file = DefaultName) const override; @@ -54,6 +56,7 @@ public: QAbstractFileEngine::Iterator *endEntryList() override; private: QString m_file; + QJniObject m_pfd; };