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