diff options
author | Jarkko Koivikko <jarkko.koivikko@code-q.fi> | 2022-03-24 06:12:38 +0200 |
---|---|---|
committer | Jarkko Koivikko <jarkko.koivikko@code-q.fi> | 2022-03-25 19:06:53 +0200 |
commit | 1fefff6d1f99dbcf1a453424753ad5562fb675ef (patch) | |
tree | 5cbc1eca4fdcdac8a35417a680161784d6f0956c /src/plugins/platforms/android | |
parent | eda8b7b4dad6fada6c7afa43b7df0f531d2504d1 (diff) |
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 <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/plugins/platforms/android')
-rw-r--r-- | src/plugins/platforms/android/androidcontentfileengine.cpp | 28 | ||||
-rw-r--r-- | src/plugins/platforms/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 b18c81e896..c5ead186c9 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<jint>("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<jint>("getFd", "()I"); + + if (fd < 0) { + m_pfd.callMethod<void>("close", "()V"); + m_pfd = QJniObject(); + return false; + } + + return QFSFileEngine::open(openMode, fd, QFile::DontCloseHandle); +} + +bool AndroidContentFileEngine::close() +{ + if (m_pfd.isValid()) { + m_pfd.callMethod<void>("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 531d0f80ff..47730f5685 100644 --- a/src/plugins/platforms/android/androidcontentfileengine.h +++ b/src/plugins/platforms/android/androidcontentfileengine.h @@ -41,12 +41,14 @@ #define ANDROIDCONTENTFILEENGINE_H #include <private/qfsfileengine_p.h> +#include <QtCore/qjniobject.h> class AndroidContentFileEngine : public QFSFileEngine { public: AndroidContentFileEngine(const QString &fileName); bool open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> 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; }; |