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 CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
This commit is contained in:
parent
8a0bf7e0e6
commit
b43405c8e9
@ -128,6 +128,22 @@ void QAndroidPlatformFileDialogHelper::setInitialFileName(const QString &title)
|
||||
extraTitle.object(), QJNIObjectPrivate::fromString(title).object());
|
||||
}
|
||||
|
||||
void QAndroidPlatformFileDialogHelper::setInitialDirectoryUri(const QString &directory)
|
||||
{
|
||||
if (directory.isEmpty())
|
||||
return;
|
||||
|
||||
if (QtAndroidPrivate::androidSdkVersion() < 26)
|
||||
return;
|
||||
|
||||
const auto extraInitialUri = QJNIObjectPrivate::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(),
|
||||
QJNIObjectPrivate::fromString(directory).object());
|
||||
}
|
||||
|
||||
void QAndroidPlatformFileDialogHelper::setOpenableCategory()
|
||||
{
|
||||
const QJNIObjectPrivate CATEGORY_OPENABLE = QJNIObjectPrivate::getStaticObjectField(
|
||||
@ -210,11 +226,8 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win
|
||||
if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
|
||||
m_intent = getFileDialogIntent("ACTION_CREATE_DOCUMENT");
|
||||
const QList<QUrl> selectedFiles = options()->initiallySelectedFiles();
|
||||
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.
|
||||
if (selectedFiles.size() > 0)
|
||||
setInitialFileName(selectedFiles.first().fileName());
|
||||
}
|
||||
} else if (options()->acceptMode() == QFileDialogOptions::AcceptOpen) {
|
||||
switch (options()->fileMode()) {
|
||||
case QFileDialogOptions::FileMode::DirectoryOnly:
|
||||
@ -238,6 +251,8 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win
|
||||
setMimeTypes();
|
||||
}
|
||||
|
||||
setInitialDirectoryUri(m_directory.toString());
|
||||
|
||||
QtAndroidPrivate::registerActivityResultListener(this);
|
||||
m_activity.callMethod<void>("startActivityForResult", "(Landroid/content/Intent;I)V",
|
||||
m_intent.object(), REQUEST_CODE);
|
||||
@ -251,6 +266,11 @@ void QAndroidPlatformFileDialogHelper::hide()
|
||||
QtAndroidPrivate::unregisterActivityResultListener(this);
|
||||
}
|
||||
|
||||
void QAndroidPlatformFileDialogHelper::setDirectory(const QUrl &directory)
|
||||
{
|
||||
m_directory = directory;
|
||||
}
|
||||
|
||||
void QAndroidPlatformFileDialogHelper::exec()
|
||||
{
|
||||
m_eventLoop.exec(QEventLoop::DialogExec);
|
||||
|
@ -67,8 +67,8 @@ public:
|
||||
void setFilter() override {};
|
||||
QList<QUrl> selectedFiles() const override { return m_selectedFile; };
|
||||
void selectFile(const QUrl &file) override { Q_UNUSED(file) };
|
||||
QUrl directory() const override { return QUrl(); };
|
||||
void setDirectory(const QUrl &directory) override { Q_UNUSED(directory) };
|
||||
QUrl directory() const override { return m_directory; }
|
||||
void setDirectory(const QUrl &directory) override;
|
||||
bool defaultNameFilterDisables() const override { return false; };
|
||||
bool handleActivityResult(jint requestCode, jint resultCode, jobject data) override;
|
||||
|
||||
@ -76,12 +76,14 @@ private:
|
||||
QJNIObjectPrivate getFileDialogIntent(const QString &intentType);
|
||||
void takePersistableUriPermission(const QJNIObjectPrivate &uri);
|
||||
void setInitialFileName(const QString &title);
|
||||
void setInitialDirectoryUri(const QString &directory);
|
||||
void setOpenableCategory();
|
||||
void setAllowMultipleSelections(bool allowMultiple);
|
||||
void setMimeTypes();
|
||||
|
||||
QEventLoop m_eventLoop;
|
||||
QList<QUrl> m_selectedFile;
|
||||
QUrl m_directory;
|
||||
QJNIObjectPrivate m_intent;
|
||||
const QJNIObjectPrivate m_activity;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user