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>
This commit is contained in:
parent
eda8b7b4da
commit
1fefff6d1f
@ -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
|
||||
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user