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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#include "SkTypes.h"
#include "SkImageDecoder.h"
#define LOG_TAG "DoCoMoEmojiFactory_jni"
#include <utils/Log.h>
#include <utils/String8.h>
#include "EmojiFactory.h"
#include <nativehelper/JNIHelp.h>
#include <dlfcn.h>
// #include <pthread.h>
namespace android {
// Note: This class is originally developed so that libandroid_runtime does
// not have to depend on libemoji which is optional library. However, we
// cannot use this class, since current (2009-02-16) bionic libc does not allow
// dlopen()-ing inside dlopen(), while not only this class but also libemoji
// uses dlopen().
class EmojiFactoryCaller {
public:
EmojiFactoryCaller();
virtual ~EmojiFactoryCaller();
EmojiFactory *TryCallGetImplementation(const char* name);
EmojiFactory *TryCallGetAvailableImplementation();
private:
void *m_handle;
EmojiFactory *(*m_get_implementation)(const char*);
EmojiFactory *(*m_get_available_implementation)();
};
EmojiFactoryCaller::EmojiFactoryCaller() {
m_handle = dlopen("libemoji.so", RTLD_LAZY | RTLD_LOCAL);
const char* error_str = dlerror();
if (error_str) {
LOGI("Failed to load libemoji.so: %s", error_str);
return;
}
m_get_implementation =
reinterpret_cast<EmojiFactory *(*)(const char*)>(
dlsym(m_handle, "GetImplementation"));
error_str = dlerror();
if (error_str) {
LOGE("Failed to get symbol of GetImplementation: %s", error_str);
dlclose(m_handle);
m_handle = NULL;
return;
}
m_get_available_implementation =
reinterpret_cast<EmojiFactory *(*)()>(
dlsym(m_handle,"GetAvailableImplementation"));
error_str = dlerror();
if (error_str) {
LOGE("Failed to get symbol of GetAvailableImplementation: %s", error_str);
dlclose(m_handle);
m_handle = NULL;
return;
}
}
EmojiFactoryCaller::~EmojiFactoryCaller() {
if (m_handle) {
dlclose(m_handle);
}
}
EmojiFactory *EmojiFactoryCaller::TryCallGetImplementation(
const char* name) {
if (NULL == m_handle) {
return NULL;
}
return m_get_implementation(name);
}
EmojiFactory *EmojiFactoryCaller::TryCallGetAvailableImplementation() {
if (NULL == m_handle) {
return NULL;
}
return m_get_available_implementation();
}
// Note: bionic libc's dlopen() does not allow recursive dlopen(). So currently
// we cannot use EmojiFactoryCaller here.
// static EmojiFactoryCaller* gCaller;
// static pthread_once_t g_once = PTHREAD_ONCE_INIT;
static jclass gString_class;
static jclass gBitmap_class;
static jmethodID gBitmap_constructorMethodID;
static jclass gEmojiFactory_class;
static jmethodID gEmojiFactory_constructorMethodID;
// static void InitializeCaller() {
// gCaller = new EmojiFactoryCaller();
// }
static jobject create_java_EmojiFactory(
JNIEnv* env, EmojiFactory* factory, jstring name) {
jobject obj = env->AllocObject(gEmojiFactory_class);
if (obj) {
env->CallVoidMethod(obj, gEmojiFactory_constructorMethodID,
(jint)factory, name);
if (env->ExceptionCheck() != 0) {
LOGE("*** Uncaught exception returned from Java call!\n");
env->ExceptionDescribe();
obj = NULL;
}
}
return obj;
}
static jobject android_emoji_EmojiFactory_newInstance(
JNIEnv* env, jobject clazz, jstring name) {
// pthread_once(&g_once, InitializeCaller);
if (NULL == name) {
return NULL;
}
const jchar* jchars = env->GetStringChars(name, NULL);
jsize len = env->GetStringLength(name);
String8 str(String16(jchars, len));
// EmojiFactory *factory = gCaller->TryCallGetImplementation(str.string());
EmojiFactory *factory = EmojiFactory::GetImplementation(str.string());
env->ReleaseStringChars(name, jchars);
return create_java_EmojiFactory(env, factory, name);
}
static jobject android_emoji_EmojiFactory_newAvailableInstance(
JNIEnv* env, jobject clazz) {
// pthread_once(&g_once, InitializeCaller);
// EmojiFactory *factory = gCaller->TryCallGetAvailableImplementation();
EmojiFactory *factory = EmojiFactory::GetAvailableImplementation();
if (NULL == factory) {
return NULL;
}
String16 name_16(String8(factory->Name()));
jstring jname = env->NewString(name_16.string(), name_16.size());
if (NULL == jname) {
return NULL;
}
return create_java_EmojiFactory(env, factory, jname);
}
static jobject android_emoji_EmojiFactory_getBitmapFromAndroidPua(
JNIEnv* env, jobject clazz, jint nativeEmojiFactory, jint pua) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
int size;
const char *bytes = factory->GetImageBinaryFromAndroidPua(pua, &size);
if (bytes == NULL) {
return NULL;
}
SkBitmap *bitmap = new SkBitmap;
if (!SkImageDecoder::DecodeMemory(bytes, size, bitmap)) {
LOGE("SkImageDecoder::DecodeMemory() failed.");
return NULL;
}
jobject obj = env->AllocObject(gBitmap_class);
if (obj) {
env->CallVoidMethod(obj, gBitmap_constructorMethodID,
reinterpret_cast<jint>(bitmap), false, NULL);
if (env->ExceptionCheck() != 0) {
LOGE("*** Uncaught exception returned from Java call!\n");
env->ExceptionDescribe();
return NULL;
}
}
return obj;
}
static void android_emoji_EmojiFactory_destructor(
JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
delete factory;
}
static jint android_emoji_EmojiFactory_getAndroidPuaFromVendorSpecificSjis(
JNIEnv* env, jobject obj, jint nativeEmojiFactory, jchar sjis) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetAndroidPuaFromVendorSpecificSjis(sjis);
}
static jint android_emoji_EmojiFactory_getVendorSpecificSjisFromAndroidPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory, jint pua) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetVendorSpecificSjisFromAndroidPua(pua);
}
static jint android_emoji_EmojiFactory_getAndroidPuaFromVendorSpecificPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory, jint vsu) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetAndroidPuaFromVendorSpecificPua(vsu);
}
static jint android_emoji_EmojiFactory_getVendorSpecificPuaFromAndroidPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory, jint pua) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetVendorSpecificPuaFromAndroidPua(pua);
}
static jint android_emoji_EmojiFactory_getMaximumVendorSpecificPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetMaximumVendorSpecificPua();
}
static jint android_emoji_EmojiFactory_getMinimumVendorSpecificPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetMinimumVendorSpecificPua();
}
static jint android_emoji_EmojiFactory_getMaximumAndroidPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetMaximumAndroidPua();
}
static jint android_emoji_EmojiFactory_getMinimumAndroidPua(
JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
return factory->GetMinimumAndroidPua();
}
static JNINativeMethod gMethods[] = {
{ "newInstance", "(Ljava/lang/String;)Landroid/emoji/EmojiFactory;",
(void*)android_emoji_EmojiFactory_newInstance},
{ "newAvailableInstance", "()Landroid/emoji/EmojiFactory;",
(void*)android_emoji_EmojiFactory_newAvailableInstance},
{ "nativeDestructor", "(I)V",
(void*)android_emoji_EmojiFactory_destructor},
{ "nativeGetBitmapFromAndroidPua", "(II)Landroid/graphics/Bitmap;",
(void*)android_emoji_EmojiFactory_getBitmapFromAndroidPua},
{ "nativeGetAndroidPuaFromVendorSpecificSjis", "(IC)I",
(void*)android_emoji_EmojiFactory_getAndroidPuaFromVendorSpecificSjis},
{ "nativeGetVendorSpecificSjisFromAndroidPua", "(II)I",
(void*)android_emoji_EmojiFactory_getVendorSpecificSjisFromAndroidPua},
{ "nativeGetAndroidPuaFromVendorSpecificPua", "(II)I",
(void*)android_emoji_EmojiFactory_getAndroidPuaFromVendorSpecificPua},
{ "nativeGetVendorSpecificPuaFromAndroidPua", "(II)I",
(void*)android_emoji_EmojiFactory_getVendorSpecificPuaFromAndroidPua},
{ "nativeGetMaximumVendorSpecificPua", "(I)I",
(void*)android_emoji_EmojiFactory_getMaximumVendorSpecificPua},
{ "nativeGetMinimumVendorSpecificPua", "(I)I",
(void*)android_emoji_EmojiFactory_getMinimumVendorSpecificPua},
{ "nativeGetMaximumAndroidPua", "(I)I",
(void*)android_emoji_EmojiFactory_getMaximumAndroidPua},
{ "nativeGetMinimumAndroidPua", "(I)I",
(void*)android_emoji_EmojiFactory_getMinimumAndroidPua}
};
static jclass make_globalref(JNIEnv* env, const char classname[])
{
jclass c = env->FindClass(classname);
SkASSERT(c);
return (jclass)env->NewGlobalRef(c);
}
static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz,
const char fieldname[], const char type[])
{
jfieldID id = env->GetFieldID(clazz, fieldname, type);
SkASSERT(id);
return id;
}
int register_android_emoji_EmojiFactory(JNIEnv* env) {
gBitmap_class = make_globalref(env, "android/graphics/Bitmap");
gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>",
"(IZ[B)V");
gEmojiFactory_class = make_globalref(env, "android/emoji/EmojiFactory");
gEmojiFactory_constructorMethodID = env->GetMethodID(
gEmojiFactory_class, "<init>", "(ILjava/lang/String;)V");
return jniRegisterNativeMethods(env, "android/emoji/EmojiFactory",
gMethods, NELEM(gMethods));
}
} // namespace android
|