1
1
#if defined(DM_PLATFORM_ANDROID)
2
2
3
- #include < jni .h>
3
+ #include < dmsdk/dlib/android .h>
4
4
#include < stdlib.h>
5
5
#include < unistd.h>
6
6
@@ -37,18 +37,6 @@ struct WebViewCommand
37
37
const char * m_Url;
38
38
};
39
39
40
- static JNIEnv* Attach ()
41
- {
42
- JNIEnv* env = 0 ;
43
- g_AndroidApp->activity ->vm ->AttachCurrentThread (&env, NULL );
44
- return env;
45
- }
46
-
47
- static void Detach ()
48
- {
49
- g_AndroidApp->activity ->vm ->DetachCurrentThread ();
50
- }
51
-
52
40
struct WebViewExtensionState
53
41
{
54
42
WebViewExtensionState ()
@@ -111,19 +99,19 @@ int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
111
99
g_WebView.m_Used [webview_id] = true ;
112
100
g_WebView.m_Info [webview_id] = *_info;
113
101
114
- JNIEnv* env = Attach ();
102
+ dmAndroid::ThreadAttacher threadAttacher;
103
+ JNIEnv* env = threadAttacher.GetEnv ();
115
104
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_Create , webview_id);
116
- Detach ();
117
105
118
106
return webview_id;
119
107
}
120
108
121
109
static int DestroyWebView (int webview_id)
122
110
{
123
111
CHECK_WEBVIEW_AND_RETURN ();
124
- JNIEnv* env = Attach ();
112
+ dmAndroid::ThreadAttacher threadAttacher;
113
+ JNIEnv* env = threadAttacher.GetEnv ();
125
114
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_Destroy , webview_id);
126
- Detach ();
127
115
ClearWebViewInfo (&g_WebView.m_Info [webview_id]);
128
116
g_WebView.m_Used [webview_id] = false ;
129
117
return 0 ;
@@ -140,23 +128,23 @@ int Platform_Open(lua_State* L, int webview_id, const char* url, dmWebView::Requ
140
128
CHECK_WEBVIEW_AND_RETURN ();
141
129
int request_id = ++g_WebView.m_RequestIds [webview_id];
142
130
143
- JNIEnv* env = Attach ();
131
+ dmAndroid::ThreadAttacher threadAttacher;
132
+ JNIEnv* env = threadAttacher.GetEnv ();
144
133
jstring jurl = env->NewStringUTF (url);
145
134
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_Load , jurl, webview_id, request_id, options->m_Hidden );
146
135
env->DeleteLocalRef (jurl);
147
- Detach ();
148
136
return request_id;
149
137
}
150
138
151
139
int Platform_ContinueOpen (lua_State* L, int webview_id, int request_id, const char * url)
152
140
{
153
141
CHECK_WEBVIEW_AND_RETURN ();
154
142
155
- JNIEnv* env = Attach ();
143
+ dmAndroid::ThreadAttacher threadAttacher;
144
+ JNIEnv* env = threadAttacher.GetEnv ();
156
145
jstring jurl = env->NewStringUTF (url);
157
146
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_ContinueLoading , jurl, webview_id, request_id);
158
147
env->DeleteLocalRef (jurl);
159
- Detach ();
160
148
return request_id;
161
149
}
162
150
@@ -171,11 +159,11 @@ int Platform_OpenRaw(lua_State* L, int webview_id, const char* html, dmWebView::
171
159
CHECK_WEBVIEW_AND_RETURN ();
172
160
int request_id = ++g_WebView.m_RequestIds [webview_id];
173
161
174
- JNIEnv* env = Attach ();
162
+ dmAndroid::ThreadAttacher threadAttacher;
163
+ JNIEnv* env = threadAttacher.GetEnv ();
175
164
jstring jhtml = env->NewStringUTF (html);
176
165
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_LoadRaw , jhtml, webview_id, request_id, options->m_Hidden );
177
166
env->DeleteLocalRef (jhtml);
178
- Detach ();
179
167
return request_id;
180
168
}
181
169
@@ -184,37 +172,37 @@ int Platform_Eval(lua_State* L, int webview_id, const char* code)
184
172
CHECK_WEBVIEW_AND_RETURN ();
185
173
int request_id = ++g_WebView.m_RequestIds [webview_id];
186
174
187
- JNIEnv* env = Attach ();
175
+ dmAndroid::ThreadAttacher threadAttacher;
176
+ JNIEnv* env = threadAttacher.GetEnv ();
188
177
jstring jcode = env->NewStringUTF (code);
189
178
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_Eval , jcode, webview_id, request_id);
190
- Detach ();
191
179
return request_id;
192
180
}
193
181
194
182
int Platform_SetVisible (lua_State* L, int webview_id, int visible)
195
183
{
196
184
CHECK_WEBVIEW_AND_RETURN ();
197
- JNIEnv* env = Attach ();
185
+ dmAndroid::ThreadAttacher threadAttacher;
186
+ JNIEnv* env = threadAttacher.GetEnv ();
198
187
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_SetVisible , webview_id, visible);
199
- Detach ();
200
188
return 0 ;
201
189
}
202
190
203
191
int Platform_IsVisible (lua_State* L, int webview_id)
204
192
{
205
193
CHECK_WEBVIEW_AND_RETURN ();
206
- JNIEnv* env = Attach ();
194
+ dmAndroid::ThreadAttacher threadAttacher;
195
+ JNIEnv* env = threadAttacher.GetEnv ();
207
196
int visible = env->CallIntMethod (g_WebView.m_WebViewJNI , g_WebView.m_IsVisible , webview_id);
208
- Detach ();
209
197
return visible;
210
198
}
211
199
212
200
int Platform_SetPosition (lua_State* L, int webview_id, int x, int y, int width, int height)
213
201
{
214
202
CHECK_WEBVIEW_AND_RETURN ();
215
- JNIEnv* env = Attach ();
203
+ dmAndroid::ThreadAttacher threadAttacher;
204
+ JNIEnv* env = threadAttacher.GetEnv ();
216
205
env->CallVoidMethod (g_WebView.m_WebViewJNI , g_WebView.m_SetPosition , webview_id, x, y, width, height);
217
- Detach ();
218
206
return 0 ;
219
207
}
220
208
@@ -303,12 +291,19 @@ JNIEXPORT void JNICALL Java_com_defold_webview_WebViewJNI_onPageLoading(JNIEnv*
303
291
dmExtension::Result Platform_Update (dmExtension::Params* params)
304
292
{
305
293
if (g_WebView.m_CmdQueue .Empty ())
294
+ {
306
295
return dmExtension::RESULT_OK; // avoid a lock (~300us on iPhone 4s)
296
+ }
307
297
308
- DM_MUTEX_SCOPED_LOCK (g_WebView.m_Mutex );
309
- for (uint32_t i=0 ; i != g_WebView.m_CmdQueue .Size (); ++i)
298
+ dmArray<WebViewCommand> tmp;
310
299
{
311
- const WebViewCommand& cmd = g_WebView.m_CmdQueue [i];
300
+ DM_MUTEX_SCOPED_LOCK (g_WebView.m_Mutex );
301
+ tmp.Swap (g_WebView.m_CmdQueue );
302
+ }
303
+
304
+ for (uint32_t i=0 ; i != tmp.Size (); ++i)
305
+ {
306
+ const WebViewCommand& cmd = tmp[i];
312
307
313
308
dmWebView::CallbackInfo cbinfo;
314
309
switch (cmd.m_Type )
@@ -373,7 +368,6 @@ dmExtension::Result Platform_Update(dmExtension::Params* params)
373
368
free (cmd.m_Data );
374
369
}
375
370
}
376
- g_WebView.m_CmdQueue .SetSize (0 );
377
371
return dmExtension::RESULT_OK;
378
372
}
379
373
@@ -382,16 +376,9 @@ dmExtension::Result Platform_AppInitialize(dmExtension::AppParams* params)
382
376
g_WebView.m_Mutex = dmMutex::New ();
383
377
g_WebView.m_CmdQueue .SetCapacity (8 );
384
378
385
- JNIEnv* env = Attach ();
386
-
387
- jclass activity_class = env->FindClass (" android/app/NativeActivity" );
388
- jmethodID get_class_loader = env->GetMethodID (activity_class," getClassLoader" , " ()Ljava/lang/ClassLoader;" );
389
- jobject cls = env->CallObjectMethod (g_AndroidApp->activity ->clazz , get_class_loader);
390
- jclass class_loader = env->FindClass (" java/lang/ClassLoader" );
391
- jmethodID find_class = env->GetMethodID (class_loader, " loadClass" , " (Ljava/lang/String;)Ljava/lang/Class;" );
392
- jstring str_class_name = env->NewStringUTF (" com.defold.webview.WebViewJNI" );
393
- jclass webview_class = (jclass)env->CallObjectMethod (cls, find_class, str_class_name);
394
- env->DeleteLocalRef (str_class_name);
379
+ dmAndroid::ThreadAttacher threadAttacher;
380
+ JNIEnv* env = threadAttacher.GetEnv ();
381
+ jclass webview_class = dmAndroid::LoadClass (env, " com.defold.webview.WebViewJNI" );
395
382
396
383
g_WebView.m_Create = env->GetMethodID (webview_class, " create" , " (I)V" );
397
384
g_WebView.m_Destroy = env->GetMethodID (webview_class, " destroy" , " (I)V" );
@@ -404,9 +391,7 @@ dmExtension::Result Platform_AppInitialize(dmExtension::AppParams* params)
404
391
g_WebView.m_SetPosition = env->GetMethodID (webview_class, " setPosition" , " (IIIII)V" );
405
392
406
393
jmethodID jni_constructor = env->GetMethodID (webview_class, " <init>" , " (Landroid/app/Activity;I)V" );
407
- g_WebView.m_WebViewJNI = env->NewGlobalRef (env->NewObject (webview_class, jni_constructor, g_AndroidApp->activity ->clazz , dmWebView::MAX_NUM_WEBVIEWS));
408
-
409
- Detach ();
394
+ g_WebView.m_WebViewJNI = env->NewGlobalRef (env->NewObject (webview_class, jni_constructor, threadAttacher.GetActivity ()->clazz , dmWebView::MAX_NUM_WEBVIEWS));
410
395
411
396
return dmExtension::RESULT_OK;
412
397
}
@@ -418,9 +403,9 @@ dmExtension::Result Platform_Initialize(dmExtension::Params* params)
418
403
419
404
dmExtension::Result Platform_AppFinalize (dmExtension::AppParams* params)
420
405
{
421
- JNIEnv* env = Attach ();
406
+ dmAndroid::ThreadAttacher threadAttacher;
407
+ JNIEnv* env = threadAttacher.GetEnv ();
422
408
env->DeleteGlobalRef (g_WebView.m_WebViewJNI );
423
- Detach ();
424
409
g_WebView.m_WebViewJNI = NULL ;
425
410
426
411
dmMutex::Delete (g_WebView.m_Mutex );
0 commit comments