1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QJNIHELPERS_H
41 #define QJNIHELPERS_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <jni.h>
55 #include <functional>
56 #include <QtCore/private/qglobal_p.h>
57 #include <QHash>
58 #include <QMetaType>
59 
60 QT_BEGIN_NAMESPACE
61 
62 class QRunnable;
63 class QStringList;
64 
65 namespace QtAndroidPrivate
66 {
67     class Q_CORE_EXPORT ActivityResultListener
68     {
69     public:
70         virtual ~ActivityResultListener();
71         virtual bool handleActivityResult(jint requestCode, jint resultCode, jobject data) = 0;
72     };
73 
74     class Q_CORE_EXPORT NewIntentListener
75     {
76     public:
77         virtual ~NewIntentListener();
78         virtual bool handleNewIntent(JNIEnv *env, jobject intent) = 0;
79     };
80 
81     class Q_CORE_EXPORT ResumePauseListener
82     {
83     public:
84         virtual ~ResumePauseListener();
85         virtual void handlePause();
86         virtual void handleResume();
87     };
88 
89     class Q_CORE_EXPORT GenericMotionEventListener
90     {
91     public:
92         virtual ~GenericMotionEventListener();
93         virtual bool handleGenericMotionEvent(jobject event) = 0;
94     };
95 
96     class Q_CORE_EXPORT KeyEventListener
97     {
98     public:
99         virtual ~KeyEventListener();
100         virtual bool handleKeyEvent(jobject event) = 0;
101     };
102 
103     class Q_CORE_EXPORT OnBindListener
104     {
105     public:
~OnBindListener()106         virtual ~OnBindListener() {}
107         virtual jobject onBind(jobject intent) = 0;
108     };
109 
110     enum class PermissionsResult {
111         Granted,
112         Denied
113     };
114     typedef QHash<QString,  QtAndroidPrivate::PermissionsResult> PermissionsHash;
115     typedef std::function<void()> Runnable;
116     typedef std::function<void(const PermissionsHash &)> PermissionsResultFunc;
117 
118     Q_CORE_EXPORT jobject activity();
119     Q_CORE_EXPORT jobject service();
120     Q_CORE_EXPORT jobject context();
121     Q_CORE_EXPORT JavaVM *javaVM();
122     Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env);
123     jobject classLoader();
124     Q_CORE_EXPORT jint androidSdkVersion();
125     Q_CORE_EXPORT void runOnAndroidThread(const Runnable &runnable, JNIEnv *env);
126     Q_CORE_EXPORT void runOnAndroidThreadSync(const Runnable &runnable, JNIEnv *env, int timeoutMs = INT_MAX);
127     Q_CORE_EXPORT void runOnUiThread(QRunnable *runnable, JNIEnv *env);
128     Q_CORE_EXPORT void requestPermissions(JNIEnv *env, const QStringList &permissions, const PermissionsResultFunc &callbackFunc, bool directCall = false);
129     Q_CORE_EXPORT PermissionsHash requestPermissionsSync(JNIEnv *env, const QStringList &permissions, int timeoutMs = INT_MAX);
130     Q_CORE_EXPORT PermissionsResult checkPermission(const QString &permission);
131     Q_CORE_EXPORT bool shouldShowRequestPermissionRationale(const QString &permission);
132 
133     Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data);
134     Q_CORE_EXPORT void registerActivityResultListener(ActivityResultListener *listener);
135     Q_CORE_EXPORT void unregisterActivityResultListener(ActivityResultListener *listener);
136 
137     Q_CORE_EXPORT void handleNewIntent(JNIEnv *env, jobject intent);
138     Q_CORE_EXPORT void registerNewIntentListener(NewIntentListener *listener);
139     Q_CORE_EXPORT void unregisterNewIntentListener(NewIntentListener *listener);
140 
141     Q_CORE_EXPORT void handlePause();
142     Q_CORE_EXPORT void handleResume();
143     Q_CORE_EXPORT void registerResumePauseListener(ResumePauseListener *listener);
144     Q_CORE_EXPORT void unregisterResumePauseListener(ResumePauseListener *listener);
145 
146     Q_CORE_EXPORT void registerGenericMotionEventListener(GenericMotionEventListener *listener);
147     Q_CORE_EXPORT void unregisterGenericMotionEventListener(GenericMotionEventListener *listener);
148 
149     Q_CORE_EXPORT void registerKeyEventListener(KeyEventListener *listener);
150     Q_CORE_EXPORT void unregisterKeyEventListener(KeyEventListener *listener);
151 
152     Q_CORE_EXPORT void hideSplashScreen(JNIEnv *env, int duration = 0);
153 
154 
155     Q_CORE_EXPORT void waitForServiceSetup();
156     Q_CORE_EXPORT int acuqireServiceSetup(int flags);
157     Q_CORE_EXPORT void setOnBindListener(OnBindListener *listener);
158     Q_CORE_EXPORT jobject callOnBindListener(jobject intent);
159 
160 }
161 
162 QT_END_NAMESPACE
163 
164 Q_DECLARE_METATYPE(QtAndroidPrivate::PermissionsHash)
165 
166 #endif // QJNIHELPERS_H
167