Android: pass EXTRA_INITIAL_URI to native FileDialog
Allow setting the initial directory where the file dialog will be opened. Change-Id: I1395b367c74d28fb2890ac53a90456c3ac4c1b05 Reviewed-by: Andy Shaw <andy.shaw@qt.io> (cherry picked from commit 609e14724edfd8d8cef23c5f30ad7812a359ed8d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
3e3f508f6d
commit
1e7ec8620b
@ -97,6 +97,22 @@ void QAndroidPlatformFileDialogHelper::setInitialFileName(const QString &title)
|
|||||||
extraTitle.object(), QJniObject::fromString(title).object());
|
extraTitle.object(), QJniObject::fromString(title).object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QAndroidPlatformFileDialogHelper::setInitialDirectoryUri(const QString &directory)
|
||||||
|
{
|
||||||
|
if (directory.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (QNativeInterface::QAndroidApplication::sdkVersion() < 26)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto extraInitialUri = QJniObject::getStaticObjectField(
|
||||||
|
"android/provider/DocumentsContract", "EXTRA_INITIAL_URI", "Ljava/lang/String;");
|
||||||
|
m_intent.callObjectMethod("putExtra",
|
||||||
|
"(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
|
||||||
|
extraInitialUri.object(),
|
||||||
|
QJniObject::fromString(directory).object());
|
||||||
|
}
|
||||||
|
|
||||||
void QAndroidPlatformFileDialogHelper::setOpenableCategory()
|
void QAndroidPlatformFileDialogHelper::setOpenableCategory()
|
||||||
{
|
{
|
||||||
const QJniObject CATEGORY_OPENABLE = QJniObject::getStaticObjectField(
|
const QJniObject CATEGORY_OPENABLE = QJniObject::getStaticObjectField(
|
||||||
@ -179,11 +195,8 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win
|
|||||||
if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
|
if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
|
||||||
m_intent = getFileDialogIntent("ACTION_CREATE_DOCUMENT");
|
m_intent = getFileDialogIntent("ACTION_CREATE_DOCUMENT");
|
||||||
const QList<QUrl> selectedFiles = options()->initiallySelectedFiles();
|
const QList<QUrl> selectedFiles = options()->initiallySelectedFiles();
|
||||||
if (selectedFiles.size() > 0) {
|
if (selectedFiles.size() > 0)
|
||||||
// TODO: The initial folder to show at the start should be handled by EXTRA_INITIAL_URI
|
|
||||||
// Take only the file name.
|
|
||||||
setInitialFileName(selectedFiles.first().fileName());
|
setInitialFileName(selectedFiles.first().fileName());
|
||||||
}
|
|
||||||
} else if (options()->acceptMode() == QFileDialogOptions::AcceptOpen) {
|
} else if (options()->acceptMode() == QFileDialogOptions::AcceptOpen) {
|
||||||
switch (options()->fileMode()) {
|
switch (options()->fileMode()) {
|
||||||
case QFileDialogOptions::FileMode::DirectoryOnly:
|
case QFileDialogOptions::FileMode::DirectoryOnly:
|
||||||
@ -207,6 +220,8 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win
|
|||||||
setMimeTypes();
|
setMimeTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setInitialDirectoryUri(m_directory.toString());
|
||||||
|
|
||||||
QtAndroidPrivate::registerActivityResultListener(this);
|
QtAndroidPrivate::registerActivityResultListener(this);
|
||||||
m_activity.callMethod<void>("startActivityForResult", "(Landroid/content/Intent;I)V",
|
m_activity.callMethod<void>("startActivityForResult", "(Landroid/content/Intent;I)V",
|
||||||
m_intent.object(), REQUEST_CODE);
|
m_intent.object(), REQUEST_CODE);
|
||||||
@ -220,6 +235,11 @@ void QAndroidPlatformFileDialogHelper::hide()
|
|||||||
QtAndroidPrivate::unregisterActivityResultListener(this);
|
QtAndroidPrivate::unregisterActivityResultListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QAndroidPlatformFileDialogHelper::setDirectory(const QUrl &directory)
|
||||||
|
{
|
||||||
|
m_directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
void QAndroidPlatformFileDialogHelper::exec()
|
void QAndroidPlatformFileDialogHelper::exec()
|
||||||
{
|
{
|
||||||
m_eventLoop.exec(QEventLoop::DialogExec);
|
m_eventLoop.exec(QEventLoop::DialogExec);
|
||||||
|
@ -32,8 +32,8 @@ public:
|
|||||||
void setFilter() override {}
|
void setFilter() override {}
|
||||||
QList<QUrl> selectedFiles() const override { return m_selectedFile; }
|
QList<QUrl> selectedFiles() const override { return m_selectedFile; }
|
||||||
void selectFile(const QUrl &) override {}
|
void selectFile(const QUrl &) override {}
|
||||||
QUrl directory() const override { return QUrl(); }
|
QUrl directory() const override { return m_directory; }
|
||||||
void setDirectory(const QUrl &) override {}
|
void setDirectory(const QUrl &directory) override;
|
||||||
bool defaultNameFilterDisables() const override { return false; }
|
bool defaultNameFilterDisables() const override { return false; }
|
||||||
bool handleActivityResult(jint requestCode, jint resultCode, jobject data) override;
|
bool handleActivityResult(jint requestCode, jint resultCode, jobject data) override;
|
||||||
|
|
||||||
@ -41,12 +41,14 @@ private:
|
|||||||
QJniObject getFileDialogIntent(const QString &intentType);
|
QJniObject getFileDialogIntent(const QString &intentType);
|
||||||
void takePersistableUriPermission(const QJniObject &uri);
|
void takePersistableUriPermission(const QJniObject &uri);
|
||||||
void setInitialFileName(const QString &title);
|
void setInitialFileName(const QString &title);
|
||||||
|
void setInitialDirectoryUri(const QString &directory);
|
||||||
void setOpenableCategory();
|
void setOpenableCategory();
|
||||||
void setAllowMultipleSelections(bool allowMultiple);
|
void setAllowMultipleSelections(bool allowMultiple);
|
||||||
void setMimeTypes();
|
void setMimeTypes();
|
||||||
|
|
||||||
QEventLoop m_eventLoop;
|
QEventLoop m_eventLoop;
|
||||||
QList<QUrl> m_selectedFile;
|
QList<QUrl> m_selectedFile;
|
||||||
|
QUrl m_directory;
|
||||||
QJniObject m_intent;
|
QJniObject m_intent;
|
||||||
const QJniObject m_activity;
|
const QJniObject m_activity;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user