Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidplatformdialoghelpers.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2013 BogDan Vatra <bogdan@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
8
9#include <QTextDocument>
10
11#include <private/qguiapplication_p.h>
12#include <qpa/qplatformtheme.h>
13
15
16using namespace Qt::StringLiterals;
17
20
21QAndroidPlatformMessageDialogHelper::QAndroidPlatformMessageDialogHelper()
22 : m_javaMessageDialog(g_messageDialogHelperClass, "(Landroid/app/Activity;)V",
23 QtAndroidPrivate::activity().object())
24{
25}
26
31
33{
34 if (!m_shown)
35 show(Qt::Dialog, Qt::ApplicationModal, 0);
36 m_loop.exec();
37}
38
39static QString htmlText(QString text)
40{
41 if (Qt::mightBeRichText(text))
42 return text;
43 text.remove(u'\r');
44 return text.toHtmlEscaped().replace(u'\n', "<br />"_L1);
45}
46
47bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags,
48 Qt::WindowModality windowModality,
49 QWindow *parent)
50{
51 Q_UNUSED(windowFlags);
52 Q_UNUSED(windowModality);
53 Q_UNUSED(parent);
54 QSharedPointer<QMessageDialogOptions> opt = options();
55 if (!opt.data())
56 return false;
57
58 if (!opt->checkBoxLabel().isNull())
59 return false; // Can't support
60
61 m_javaMessageDialog.callMethod<void>("setStandardIcon", "(I)V", opt->standardIcon());
62
63 QString str = htmlText(opt->windowTitle());
64 if (!str.isEmpty()) {
65 m_javaMessageDialog.callMethod<void>("setTile", "(Ljava/lang/String;)V",
66 QJniObject::fromString(str).object());
67 }
68
69 str = htmlText(opt->text());
70 if (!str.isEmpty()) {
71 m_javaMessageDialog.callMethod<void>("setText", "(Ljava/lang/String;)V",
72 QJniObject::fromString(str).object());
73 }
74
75 str = htmlText(opt->informativeText());
76 if (!str.isEmpty()) {
77 m_javaMessageDialog.callMethod<void>("setInformativeText", "(Ljava/lang/String;)V",
78 QJniObject::fromString(str).object());
79 }
80
81 str = htmlText(opt->detailedText());
82 if (!str.isEmpty()) {
83 m_javaMessageDialog.callMethod<void>("setDetailedText", "(Ljava/lang/String;)V",
84 QJniObject::fromString(str).object());
85 }
86
87 const int *currentLayout = buttonLayout(Qt::Horizontal, AndroidLayout);
88 while (*currentLayout != QPlatformDialogHelper::EOL) {
89 const int role = (*currentLayout & ~QPlatformDialogHelper::Reverse);
90 addButtons(opt, static_cast<ButtonRole>(role));
91 ++currentLayout;
92 }
93
94 m_javaMessageDialog.callMethod<void>("show", "(J)V", jlong(static_cast<QObject*>(this)));
95 m_shown = true;
96 return true;
97}
98
99void QAndroidPlatformMessageDialogHelper::addButtons(QSharedPointer<QMessageDialogOptions> opt,
100 ButtonRole role)
101{
102 for (const QMessageDialogOptions::CustomButton &b : opt->customButtons()) {
103 if (b.role == role) {
104 QString label = b.label;
105 label.remove(QChar('&'));
106 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", b.id,
107 QJniObject::fromString(label).object());
108 }
109 }
110
111 for (int i = QPlatformDialogHelper::FirstButton; i < QPlatformDialogHelper::LastButton; i<<=1) {
112 StandardButton b = static_cast<StandardButton>(i);
113 if (buttonRole(b) == role && (opt->standardButtons() & i)) {
114 const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(b);
115 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i,
116 QJniObject::fromString(text).object());
117 }
118 }
119}
120
122{
123 m_javaMessageDialog.callMethod<void>("hide", "()V");
124 m_shown = false;
125}
126
127void QAndroidPlatformMessageDialogHelper::dialogResult(int buttonID)
128{
129 if (m_loop.isRunning())
130 m_loop.exit();
131 if (buttonID < 0) {
132 emit reject();
133 return;
134 }
135
136 const StandardButton standardButton = static_cast<StandardButton>(buttonID);
137 ButtonRole role = QPlatformDialogHelper::buttonRole(standardButton);
138 if (buttonID > QPlatformDialogHelper::LastButton) {
139 // In case of a custom button
140 const QMessageDialogOptions::CustomButton *custom = options()->customButton(buttonID);
141 Q_ASSERT(custom);
142 role = custom->role;
143 }
144
145 emit clicked(standardButton, role);
146}
147
148static void dialogResult(JNIEnv * /*env*/, jobject /*thiz*/, jlong handler, int buttonID)
149{
150 QObject *object = reinterpret_cast<QObject *>(handler);
151 QMetaObject::invokeMethod(object, "dialogResult", Qt::QueuedConnection, Q_ARG(int, buttonID));
152}
153
154static const JNINativeMethod methods[] = {
155 {"dialogResult", "(JI)V", (void *)dialogResult}
156};
157
158
159#define FIND_AND_CHECK_CLASS(CLASS_NAME)
160 clazz = env->FindClass(CLASS_NAME);
161 if (!clazz) {
162 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt(), CLASS_NAME);
163 return false;
164 }
165
166bool registerNatives(QJniEnvironment &env)
167{
168 const char QtMessageHandlerHelperClassName[] = "org/qtproject/qt/android/QtMessageDialogHelper";
169 jclass clazz = env.findClass(QtMessageHandlerHelperClassName);
170 if (!clazz) {
171 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt()
172 , QtMessageHandlerHelperClassName);
173 return false;
174 }
175 g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz));
176
177 if (!env.registerNativeMethods("org/qtproject/qt/android/QtNativeDialogHelper",
178 methods, sizeof(methods) / sizeof(methods[0]))) {
179 __android_log_print(ANDROID_LOG_FATAL, "Qt", "RegisterNatives failed");
180 return false;
181 }
182
183 return true;
184}
185}
186
187QT_END_NAMESPACE
Combined button and popup list for selecting options.
bool registerNatives(QJniEnvironment &env)
static QString htmlText(QString text)
static void dialogResult(JNIEnv *, jobject, jlong handler, int buttonID)
static const JNINativeMethod methods[]
const char * classErrorMsgFmt()
const char * qtTagText()