1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org>
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "qandroidplatformdialoghelpers.h"
42 #include "androidjnimain.h"
43 
44 #include <QTextDocument>
45 
46 #include <private/qguiapplication_p.h>
47 #include <qpa/qplatformtheme.h>
48 
49 QT_BEGIN_NAMESPACE
50 
51 namespace QtAndroidDialogHelpers {
52 static jclass g_messageDialogHelperClass = 0;
53 
54 static const char QtMessageHandlerHelperClassName[] = "org/qtproject/qt5/android/QtMessageDialogHelper";
55 
QAndroidPlatformMessageDialogHelper()56 QAndroidPlatformMessageDialogHelper::QAndroidPlatformMessageDialogHelper()
57     :m_buttonId(-1)
58     ,m_javaMessageDialog(g_messageDialogHelperClass, "(Landroid/app/Activity;)V", QtAndroid::activity())
59     ,m_shown(false)
60 {
61 }
62 
exec()63 void QAndroidPlatformMessageDialogHelper::exec()
64 {
65     if (!m_shown)
66         show(Qt::Dialog, Qt::ApplicationModal, 0);
67     m_loop.exec();
68 }
69 
htmlText(QString text)70 static QString htmlText(QString text)
71 {
72     if (Qt::mightBeRichText(text))
73         return text;
74     text.remove(QLatin1Char('\r'));
75     return text.toHtmlEscaped().replace(QLatin1Char('\n'), QLatin1String("<br />"));
76 }
77 
show(Qt::WindowFlags windowFlags,Qt::WindowModality windowModality,QWindow * parent)78 bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags
79                                          , Qt::WindowModality windowModality
80                                          , QWindow *parent)
81 {
82     Q_UNUSED(windowFlags)
83     Q_UNUSED(windowModality)
84     Q_UNUSED(parent)
85     QSharedPointer<QMessageDialogOptions> opt = options();
86     if (!opt.data())
87         return false;
88 
89     m_javaMessageDialog.callMethod<void>("setIcon", "(I)V", opt->icon());
90 
91     QString str = htmlText(opt->windowTitle());
92     if (!str.isEmpty())
93         m_javaMessageDialog.callMethod<void>("setTile", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object());
94 
95     str = htmlText(opt->text());
96     if (!str.isEmpty())
97         m_javaMessageDialog.callMethod<void>("setText", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object());
98 
99     str = htmlText(opt->informativeText());
100     if (!str.isEmpty())
101         m_javaMessageDialog.callMethod<void>("setInformativeText", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object());
102 
103     str = htmlText(opt->detailedText());
104     if (!str.isEmpty())
105         m_javaMessageDialog.callMethod<void>("setDetailedText", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object());
106 
107     const int * currentLayout = buttonLayout(Qt::Horizontal, AndroidLayout);
108     while (*currentLayout != QPlatformDialogHelper::EOL) {
109         int role = (*currentLayout & ~QPlatformDialogHelper::Reverse);
110         addButtons(opt, static_cast<ButtonRole>(role));
111         ++currentLayout;
112     }
113 
114     m_javaMessageDialog.callMethod<void>("show", "(J)V", jlong(static_cast<QObject*>(this)));
115     m_shown = true;
116     return true;
117 }
118 
addButtons(QSharedPointer<QMessageDialogOptions> opt,ButtonRole role)119 void QAndroidPlatformMessageDialogHelper::addButtons(QSharedPointer<QMessageDialogOptions> opt, ButtonRole role)
120 {
121     for (const QMessageDialogOptions::CustomButton &b : opt->customButtons()) {
122         if (b.role == role) {
123             QString label = b.label;
124             label.remove(QChar('&'));
125             m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", b.id,
126                                                  QJNIObjectPrivate::fromString(label).object());
127         }
128     }
129 
130     for (int i = QPlatformDialogHelper::FirstButton; i < QPlatformDialogHelper::LastButton; i<<=1) {
131         StandardButton b = static_cast<StandardButton>(i);
132         if (buttonRole(b) == role && (opt->standardButtons() & i)) {
133             const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(b);
134             m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i, QJNIObjectPrivate::fromString(text).object());
135         }
136     }
137 }
138 
hide()139 void QAndroidPlatformMessageDialogHelper::hide()
140 {
141     m_javaMessageDialog.callMethod<void>("hide", "()V");
142     m_shown = false;
143 }
144 
dialogResult(int buttonID)145 void QAndroidPlatformMessageDialogHelper::dialogResult(int buttonID)
146 {
147     m_buttonId = buttonID;
148     if (m_loop.isRunning())
149         m_loop.exit();
150     if (m_buttonId < 0) {
151         emit reject();
152         return;
153     }
154 
155     QPlatformDialogHelper::StandardButton standardButton = static_cast<QPlatformDialogHelper::StandardButton>(buttonID);
156     QPlatformDialogHelper::ButtonRole role = QPlatformDialogHelper::buttonRole(standardButton);
157     if (buttonID > QPlatformDialogHelper::LastButton) {
158         const QMessageDialogOptions::CustomButton *custom = options()->customButton(buttonID);
159         Q_ASSERT(custom);
160         role = custom->role;
161     }
162 
163     emit clicked(standardButton, role);
164 }
165 
dialogResult(JNIEnv *,jobject,jlong handler,int buttonID)166 static void dialogResult(JNIEnv * /*env*/, jobject /*thiz*/, jlong handler, int buttonID)
167 {
168     QObject *object = reinterpret_cast<QObject *>(handler);
169     QMetaObject::invokeMethod(object, "dialogResult", Qt::QueuedConnection, Q_ARG(int, buttonID));
170 }
171 
172 static JNINativeMethod methods[] = {
173     {"dialogResult", "(JI)V", (void *)dialogResult}
174 };
175 
176 
177 #define FIND_AND_CHECK_CLASS(CLASS_NAME) \
178     clazz = env->FindClass(CLASS_NAME); \
179     if (!clazz) { \
180         __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt(), CLASS_NAME); \
181         return false; \
182     }
183 
registerNatives(JNIEnv * env)184 bool registerNatives(JNIEnv *env)
185 {
186     jclass clazz = QJNIEnvironmentPrivate::findClass(QtMessageHandlerHelperClassName, env);
187     if (!clazz) {
188         __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt()
189                             , QtMessageHandlerHelperClassName);
190         return false;
191     }
192     g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz));
193     FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/QtNativeDialogHelper");
194     jclass appClass = static_cast<jclass>(env->NewGlobalRef(clazz));
195 
196     if (env->RegisterNatives(appClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
197         __android_log_print(ANDROID_LOG_FATAL, "Qt", "RegisterNatives failed");
198         return false;
199     }
200 
201     return true;
202 }
203 }
204 
205 QT_END_NAMESPACE
206