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 <andy.shaw@qt.io>
(cherry picked from commit cbc29dc16c4e08d6e399c7c26a38736bff80d6e6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Assam Boudjelthia 2021-10-21 13:25:02 +03:00 committed by Qt Cherry-pick Bot
parent 98edfa18c1
commit cae5e3c348
2 changed files with 9 additions and 4 deletions

View File

@ -46,6 +46,7 @@
#include <QMimeDatabase>
#include <QMimeType>
#include <QRegularExpression>
#include <QUrl>
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<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.
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<void>("startActivityForResult", "(Landroid/content/Intent;I)V",
m_intent.object(), REQUEST_CODE);

View File

@ -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();