-
Notifications
You must be signed in to change notification settings - Fork 1
/
android.cpp
188 lines (151 loc) · 5.39 KB
/
android.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <QDebug>
#include "android.hpp"
using namespace SMSDB;
static JavaVM *s_javaVM = NULL;
static jclass s_androidClassID = NULL;
static jmethodID s_androidConstructorMethodID = NULL;
static jmethodID s_androidCopyToClipboardMethodID = NULL;
static jmethodID s_androidSendTextMethodID = NULL;
static jmethodID s_androidNotifyMethodID = NULL;
static jmethodID s_androidReleaseMethodID = NULL;
// This method is called immediately after the module is loaded.
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *)
{
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
qCritical() << " * Could not get the enviroument !!";
return -1;
}
s_javaVM = vm;
// Search for our class
jclass clazz = env->FindClass("smsdb/Android");
if (!clazz) {
qCritical() << " * Could not find the Android class !!";
return -1;
}
// Keep a global reference to it.
s_androidClassID = (jclass)env->NewGlobalRef(clazz);
// Search for its contructor.
s_androidConstructorMethodID = env->GetMethodID(
s_androidClassID, "<init>", "()V");
if (!s_androidConstructorMethodID) {
qCritical() << " * Could not find the Android class contructor !!";
return -1;
}
// Search for Release method
s_androidReleaseMethodID = env->GetMethodID(
s_androidClassID, "Release", "()Z");
if (!s_androidReleaseMethodID) {
qCritical() << " * Could not find Release method !!";
return -1;
}
// Search for CopyToClipboard method.
s_androidCopyToClipboardMethodID = env->GetMethodID(
s_androidClassID, "CopyToClipboard", "(Ljava/lang/String;)Z");
if (!s_androidCopyToClipboardMethodID) {
qCritical() << " * Could not find CopyToClipboard method !!";
return -1;
}
// Search for SendText method.
s_androidSendTextMethodID = env->GetMethodID(
s_androidClassID, "SendText", "(Ljava/lang/String;)Z");
if (!s_androidSendTextMethodID) {
qCritical() << " * Could not find SendText method !!";
return -1;
}
// Search for Notify method.
s_androidNotifyMethodID = env->GetMethodID(
s_androidClassID, "Notify", "(Ljava/lang/String;I)Z");
if (!s_androidNotifyMethodID) {
qCritical() << " * Could not find Notify method !!";
return -1;
}
qDebug() << " - JNI_OnLoad was executed successfully!";
return JNI_VERSION_1_6;
}
Android::Android()
{
JNIEnv *env;
// Qt is running in a different thread than Java UI.
// So, Java VM *MUST* always stay attached to the current thread.
if (s_javaVM->AttachCurrentThread(&env, NULL) < 0) {
qCritical() << " * AttachCurrentThread failed !!";
return;
}
// Create a new instance of Android
m_androidObject = env->NewGlobalRef(
env->NewObject(s_androidClassID,
s_androidConstructorMethodID));
if (!m_androidObject) {
qCritical() << " * Could not create the Android class instance !!";
return;
}
// Don't forget to detach from current thread.
s_javaVM->DetachCurrentThread();
}
Android::~Android()
{
if (m_androidObject) {
JNIEnv *env;
if (s_javaVM->AttachCurrentThread(&env, NULL) < 0 ) {
qCritical() << " * AttachCurrentThread failed !!";
return;
}
if (!env->CallBooleanMethod(m_androidObject,
s_androidReleaseMethodID)) {
qCritical() << " * Releasing Android object failed !!";
}
s_javaVM->DetachCurrentThread();
}
}
bool Android::CopyToClipboard(const QString &text)
{
if (!m_androidObject)
return false;
JNIEnv *env;
if (s_javaVM->AttachCurrentThread(&env, NULL) < 0) {
qCritical() << "AttachCurrentThread failed !!";
return false;
}
jstring str = env->NewString(reinterpret_cast<const jchar *>(
text.constData()), text.length());
jboolean res = env->CallBooleanMethod(
m_androidObject, s_androidCopyToClipboardMethodID, str);
env->DeleteLocalRef(str);
s_javaVM->DetachCurrentThread();
return res;
}
bool Android::SendText(const QString &text)
{
if (!m_androidObject)
return false;
JNIEnv *env;
if (s_javaVM->AttachCurrentThread(&env, NULL) < 0) {
qCritical() << "AttachCurrentThread failed !!";
return false;
}
jstring str = env->NewString(reinterpret_cast<const jchar *>(
text.constData()), text.length());
jboolean res = env->CallBooleanMethod(
m_androidObject, s_androidSendTextMethodID, str);
env->DeleteLocalRef(str);
s_javaVM->DetachCurrentThread();
return res;
}
bool Android::Notify(const QString &text, int duration)
{
if (!m_androidObject)
return false;
JNIEnv *env;
if (s_javaVM->AttachCurrentThread(&env, NULL) < 0) {
qCritical() << "AttachCurrentThread failed !!";
return false;
}
jstring str = env->NewString(reinterpret_cast<const jchar *>(
text.constData()), text.length());
jboolean res = env->CallBooleanMethod(
m_androidObject, s_androidNotifyMethodID, str, duration);
env->DeleteLocalRef(str);
s_javaVM->DetachCurrentThread();
return res;
}