From cae5e3c348ac724ef4ded36dd79fb26f2ce0fde1 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Thu, 21 Oct 2021 13:25:02 +0300 Subject: [PATCH] Android: set EXTRA_TITLE to the initially selected file in save dialog The extra data EXTRA_TITLE is only documented to be used to provide the initially selected file name in the context of file dialog [1]. So, let's stick to setting it only in save mode. This also now allows the save dialog to set an initial file name which wasn't possible before. [1] https://developer.android.com/reference/kotlin/android/content/ Intent#action_create_document Change-Id: Ib55191a7269bfad28af4928f4e74d87981bdd574 Reviewed-by: Andy Shaw (cherry picked from commit cbc29dc16c4e08d6e399c7c26a38736bff80d6e6) Reviewed-by: Qt Cherry-pick Bot --- .../android/qandroidplatformfiledialoghelper.cpp | 11 ++++++++--- .../android/qandroidplatformfiledialoghelper.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp index 7741fc755d7..1ec5867a38d 100644 --- a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp +++ b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp @@ -46,6 +46,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -119,7 +120,7 @@ void QAndroidPlatformFileDialogHelper::takePersistableUriPermission(const QJniOb uri.object(), modeFlags); } -void QAndroidPlatformFileDialogHelper::setIntentTitle(const QString &title) +void QAndroidPlatformFileDialogHelper::setInitialFileName(const QString &title) { const QJniObject extraTitle = QJniObject::getStaticObjectField( JniIntentClass, "EXTRA_TITLE", "Ljava/lang/String;"); @@ -209,6 +210,12 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win if (options()->acceptMode() == QFileDialogOptions::AcceptSave) { m_intent = getFileDialogIntent("ACTION_CREATE_DOCUMENT"); + const QList 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. + setInitialFileName(selectedFiles.first().fileName()); + } } else if (options()->acceptMode() == QFileDialogOptions::AcceptOpen) { switch (options()->fileMode()) { case QFileDialogOptions::FileMode::DirectoryOnly: @@ -232,8 +239,6 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win setMimeTypes(); } - setIntentTitle(options()->windowTitle()); - QtAndroidPrivate::registerActivityResultListener(this); m_activity.callMethod("startActivityForResult", "(Landroid/content/Intent;I)V", m_intent.object(), REQUEST_CODE); diff --git a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h index 2d3cb49a96f..f573e72fef5 100644 --- a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h +++ b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h @@ -76,7 +76,7 @@ public: private: QJniObject getFileDialogIntent(const QString &intentType); void takePersistableUriPermission(const QJniObject &uri); - void setIntentTitle(const QString &title); + void setInitialFileName(const QString &title); void setOpenableCategory(); void setAllowMultipleSelections(bool allowMultiple); void setMimeTypes();