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 const bool hasEscapeButton = opt->escapeButton() != QPlatformDialogHelper::NoButton;
95 m_javaMessageDialog.callMethod<void>("show", "(JZ)V",
96 jlong(static_cast<QObject*>(this)),
97 jboolean(hasEscapeButton));
98 m_shown = true;
99 return true;
100}
101
102void QAndroidPlatformMessageDialogHelper::addButtons(QSharedPointer<QMessageDialogOptions> opt,
103 ButtonRole role)
104{
105 for (const QMessageDialogOptions::CustomButton &b : opt->customButtons()) {
106 if (b.role == role) {
107 QString label = b.label;
108 label.remove(QChar('&'));
109 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", b.id,
110 QJniObject::fromString(label).object());
111 }
112 }
113
114 for (int i = QPlatformDialogHelper::FirstButton; i < QPlatformDialogHelper::LastButton; i<<=1) {
115 StandardButton b = static_cast<StandardButton>(i);
116 if (buttonRole(b) == role && (opt->standardButtons() & i)) {
117 const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(b);
118 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i,
119 QJniObject::fromString(text).object());
120 }
121 }
122}
123
125{
126 m_javaMessageDialog.callMethod<void>("hide", "()V");
127 m_shown = false;
128}
129
130void QAndroidPlatformMessageDialogHelper::dialogResult(int buttonID)
131{
132 if (m_loop.isRunning())
133 m_loop.exit();
134 if (buttonID < 0) {
135 emit reject();
136 return;
137 }
138
139 const StandardButton standardButton = static_cast<StandardButton>(buttonID);
140 ButtonRole role = QPlatformDialogHelper::buttonRole(standardButton);
141 if (buttonID > QPlatformDialogHelper::LastButton) {
142 // In case of a custom button
143 const QMessageDialogOptions::CustomButton *custom = options()->customButton(buttonID);
144 Q_ASSERT(custom);
145 role = custom->role;
146 }
147
148 emit clicked(standardButton, role);
149}
150
151static void dialogResult(JNIEnv * /*env*/, jobject /*thiz*/, jlong handler, int buttonID)
152{
153 QObject *object = reinterpret_cast<QObject *>(handler);
154 QMetaObject::invokeMethod(object, "dialogResult", Qt::QueuedConnection, Q_ARG(int, buttonID));
155}
156
157static const JNINativeMethod methods[] = {
158 {"dialogResult", "(JI)V", (void *)dialogResult}
159};
160
161
162#define FIND_AND_CHECK_CLASS(CLASS_NAME)
163 clazz = env->FindClass(CLASS_NAME);
164 if (!clazz) {
165 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt(), CLASS_NAME);
166 return false;
167 }
168
169bool registerNatives(QJniEnvironment &env)
170{
171 const char QtMessageHandlerHelperClassName[] = "org/qtproject/qt/android/QtMessageDialogHelper";
172 jclass clazz = env.findClass(QtMessageHandlerHelperClassName);
173 if (!clazz) {
174 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt()
175 , QtMessageHandlerHelperClassName);
176 return false;
177 }
178 g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz));
179
180 if (!env.registerNativeMethods("org/qtproject/qt/android/QtNativeDialogHelper",
181 methods, sizeof(methods) / sizeof(methods[0]))) {
182 __android_log_print(ANDROID_LOG_FATAL, "Qt", "RegisterNatives failed");
183 return false;
184 }
185
186 return true;
187}
188}
189
190QT_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()