blob: 49a24a30f77edd31e129121f21bfefdf2caac7f5 [file] [log] [blame]
Dianne Hackborn69969e42010-05-04 11:40:40 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "NativeActivity"
18#include <utils/Log.h>
19
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070020#include <poll.h>
21#include <dlfcn.h>
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070022#include <fcntl.h>
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070023
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -080024#include <memory>
25
Dianne Hackborn289b9b62010-07-09 11:44:11 -070026#include <android_runtime/android_app_NativeActivity.h>
Christopher Tate6cce32b2010-07-12 18:21:36 -070027#include <android_runtime/android_util_AssetManager.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080028#include <android_runtime/android_view_Surface.h>
29#include <android_runtime/AndroidRuntime.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070030#include <input/InputTransport.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080031
32#include <gui/Surface.h>
33
34#include <system/window.h>
35
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070036#include <utils/Looper.h>
Dianne Hackborn69969e42010-05-04 11:40:40 -070037
Steven Moreland2279b252017-07-19 09:50:45 -070038#include <nativehelper/JNIHelp.h>
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070039#include "android_os_MessageQueue.h"
40#include "android_view_InputChannel.h"
41#include "android_view_KeyEvent.h"
Dianne Hackborn69969e42010-05-04 11:40:40 -070042
dimitryf66a2232017-10-04 19:44:56 +020043#include "android-base/stringprintf.h"
Yong WU03c86602014-07-28 21:20:18 +080044#include "nativebridge/native_bridge.h"
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -080045#include "nativeloader/native_loader.h"
Yong WU03c86602014-07-28 21:20:18 +080046
Andreas Gampe987f79f2014-11-18 17:29:46 -080047#include "core_jni_helpers.h"
48
Steven Moreland2279b252017-07-19 09:50:45 -070049#include <nativehelper/ScopedUtfChars.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080050
Dianne Hackborndb28a942010-10-21 17:22:30 -070051#define LOG_TRACE(...)
Steve Block28d9f022011-10-12 17:27:03 +010052//#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070053
Dianne Hackborn69969e42010-05-04 11:40:40 -070054namespace android
55{
56
Andreas Gampeed6b9df2014-11-20 22:02:20 -080057static const bool kLogTrace = false;
58
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070059static struct {
Dianne Hackborndb28a942010-10-21 17:22:30 -070060 jmethodID finish;
Dianne Hackborn54a181b2010-06-30 18:35:14 -070061 jmethodID setWindowFlags;
62 jmethodID setWindowFormat;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070063 jmethodID showIme;
64 jmethodID hideIme;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070065} gNativeActivityClassInfo;
66
Dianne Hackborn54a181b2010-06-30 18:35:14 -070067// ------------------------------------------------------------------------
68
Dianne Hackborn289b9b62010-07-09 11:44:11 -070069struct ActivityWork {
70 int32_t cmd;
71 int32_t arg1;
72 int32_t arg2;
73};
74
75enum {
Michael Wrighta44dd262013-04-10 21:12:00 -070076 CMD_FINISH = 1,
Dianne Hackborn289b9b62010-07-09 11:44:11 -070077 CMD_SET_WINDOW_FORMAT,
78 CMD_SET_WINDOW_FLAGS,
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070079 CMD_SHOW_SOFT_INPUT,
80 CMD_HIDE_SOFT_INPUT,
Dianne Hackborn289b9b62010-07-09 11:44:11 -070081};
82
83static void write_work(int fd, int32_t cmd, int32_t arg1=0, int32_t arg2=0) {
84 ActivityWork work;
85 work.cmd = cmd;
86 work.arg1 = arg1;
87 work.arg2 = arg2;
Andreas Gampeed6b9df2014-11-20 22:02:20 -080088
89 if (kLogTrace) {
90 ALOGD("write_work: cmd=%d", cmd);
91 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070092
Dianne Hackborn289b9b62010-07-09 11:44:11 -070093restart:
94 int res = write(fd, &work, sizeof(work));
95 if (res < 0 && errno == EINTR) {
96 goto restart;
97 }
98
99 if (res == sizeof(work)) return;
100
Steve Block8564c8d2012-01-05 23:22:43 +0000101 if (res < 0) ALOGW("Failed writing to work fd: %s", strerror(errno));
102 else ALOGW("Truncated writing to work fd: %d", res);
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700103}
104
105static bool read_work(int fd, ActivityWork* outWork) {
106 int res = read(fd, outWork, sizeof(ActivityWork));
107 // no need to worry about EINTR, poll loop will just come back again.
108 if (res == sizeof(ActivityWork)) return true;
109
Steve Block8564c8d2012-01-05 23:22:43 +0000110 if (res < 0) ALOGW("Failed reading work fd: %s", strerror(errno));
111 else ALOGW("Truncated reading work fd: %d", res);
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700112 return false;
113}
114
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700115/*
116 * Native state for interacting with the NativeActivity class.
117 */
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700118struct NativeCode : public ANativeActivity {
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700119 NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700120 memset((ANativeActivity*)this, 0, sizeof(ANativeActivity));
121 memset(&callbacks, 0, sizeof(callbacks));
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700122 dlhandle = _dlhandle;
123 createActivityFunc = _createFunc;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700124 nativeWindow = NULL;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700125 mainWorkRead = mainWorkWrite = -1;
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700126 }
127
128 ~NativeCode() {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700129 if (callbacks.onDestroy != NULL) {
130 callbacks.onDestroy(this);
131 }
Adam Lesinski5c690d52017-03-17 15:17:12 -0700132 if (env != NULL) {
133 if (clazz != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700134 env->DeleteGlobalRef(clazz);
Adam Lesinski5c690d52017-03-17 15:17:12 -0700135 }
136 if (javaAssetManager != NULL) {
137 env->DeleteGlobalRef(javaAssetManager);
138 }
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700139 }
Jeff Brown603b4452012-04-06 17:39:41 -0700140 if (messageQueue != NULL && mainWorkRead >= 0) {
141 messageQueue->getLooper()->removeFd(mainWorkRead);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700142 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700143 setSurface(NULL);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700144 if (mainWorkRead >= 0) close(mainWorkRead);
145 if (mainWorkWrite >= 0) close(mainWorkWrite);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700146 if (dlhandle != NULL) {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700147 // for now don't unload... we probably should clean this
148 // up and only keep one open dlhandle per proc, since there
149 // is really no benefit to unloading the code.
150 //dlclose(dlhandle);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700151 }
152 }
153
154 void setSurface(jobject _surface) {
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700155 if (_surface != NULL) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700156 nativeWindow = android_view_Surface_getNativeWindow(env, _surface);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700157 } else {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700158 nativeWindow = NULL;
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700159 }
160 }
161
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700162 ANativeActivityCallbacks callbacks;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700163
164 void* dlhandle;
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700165 ANativeActivity_createFunc* createActivityFunc;
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700166
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800167 String8 internalDataPathObj;
168 String8 externalDataPathObj;
169 String8 obbPathObj;
Dianne Hackborn68267412010-07-02 18:52:01 -0700170
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700171 sp<ANativeWindow> nativeWindow;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700172 int32_t lastWindowWidth;
173 int32_t lastWindowHeight;
174
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700175 // These are used to wake up the main thread to process work.
176 int mainWorkRead;
177 int mainWorkWrite;
Jeff Brown603b4452012-04-06 17:39:41 -0700178 sp<MessageQueue> messageQueue;
Adam Lesinski5c690d52017-03-17 15:17:12 -0700179
180 // Need to hold on to a reference here in case the upper layers destroy our
181 // AssetManager.
182 jobject javaAssetManager;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700183};
184
Dianne Hackborndb28a942010-10-21 17:22:30 -0700185void android_NativeActivity_finish(ANativeActivity* activity) {
186 NativeCode* code = static_cast<NativeCode*>(activity);
187 write_work(code->mainWorkWrite, CMD_FINISH, 0);
188}
189
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700190void android_NativeActivity_setWindowFormat(
191 ANativeActivity* activity, int32_t format) {
192 NativeCode* code = static_cast<NativeCode*>(activity);
193 write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format);
194}
195
196void android_NativeActivity_setWindowFlags(
197 ANativeActivity* activity, int32_t values, int32_t mask) {
198 NativeCode* code = static_cast<NativeCode*>(activity);
199 write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask);
200}
201
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700202void android_NativeActivity_showSoftInput(
203 ANativeActivity* activity, int32_t flags) {
204 NativeCode* code = static_cast<NativeCode*>(activity);
205 write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags);
206}
207
208void android_NativeActivity_hideSoftInput(
209 ANativeActivity* activity, int32_t flags) {
210 NativeCode* code = static_cast<NativeCode*>(activity);
211 write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags);
212}
213
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700214// ------------------------------------------------------------------------
215
216/*
217 * Callback for handling native events on the application's main thread.
218 */
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700219static int mainWorkCallback(int fd, int events, void* data) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700220 NativeCode* code = (NativeCode*)data;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700221 if ((events & POLLIN) == 0) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700222 return 1;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700223 }
224
225 ActivityWork work;
226 if (!read_work(code->mainWorkRead, &work)) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700227 return 1;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700228 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700229
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800230 if (kLogTrace) {
231 ALOGD("mainWorkCallback: cmd=%d", work.cmd);
232 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700233
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700234 switch (work.cmd) {
Dianne Hackborndb28a942010-10-21 17:22:30 -0700235 case CMD_FINISH: {
236 code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish);
Jeff Brown603b4452012-04-06 17:39:41 -0700237 code->messageQueue->raiseAndClearException(code->env, "finish");
Dianne Hackborndb28a942010-10-21 17:22:30 -0700238 } break;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700239 case CMD_SET_WINDOW_FORMAT: {
240 code->env->CallVoidMethod(code->clazz,
241 gNativeActivityClassInfo.setWindowFormat, work.arg1);
Jeff Brown603b4452012-04-06 17:39:41 -0700242 code->messageQueue->raiseAndClearException(code->env, "setWindowFormat");
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700243 } break;
244 case CMD_SET_WINDOW_FLAGS: {
245 code->env->CallVoidMethod(code->clazz,
246 gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2);
Jeff Brown603b4452012-04-06 17:39:41 -0700247 code->messageQueue->raiseAndClearException(code->env, "setWindowFlags");
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700248 } break;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700249 case CMD_SHOW_SOFT_INPUT: {
250 code->env->CallVoidMethod(code->clazz,
251 gNativeActivityClassInfo.showIme, work.arg1);
Jeff Brown603b4452012-04-06 17:39:41 -0700252 code->messageQueue->raiseAndClearException(code->env, "showIme");
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700253 } break;
254 case CMD_HIDE_SOFT_INPUT: {
255 code->env->CallVoidMethod(code->clazz,
256 gNativeActivityClassInfo.hideIme, work.arg1);
Jeff Brown603b4452012-04-06 17:39:41 -0700257 code->messageQueue->raiseAndClearException(code->env, "hideIme");
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700258 } break;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700259 default:
Steve Block8564c8d2012-01-05 23:22:43 +0000260 ALOGW("Unknown work command: %d", work.cmd);
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700261 break;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700262 }
263
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700264 return 1;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700265}
266
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700267// ------------------------------------------------------------------------
268
dimitryf66a2232017-10-04 19:44:56 +0200269static thread_local std::string g_error_msg;
270
Ashok Bhat58b8b242014-01-02 16:52:41 +0000271static jlong
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700272loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName,
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800273 jobject messageQueue, jstring internalDataDir, jstring obbDir,
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -0800274 jstring externalDataDir, jint sdkVersion, jobject jAssetMgr,
Dimitry Ivanovea902812016-02-23 14:25:50 -0800275 jbyteArray savedState, jobject classLoader, jstring libraryPath) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800276 if (kLogTrace) {
277 ALOGD("loadNativeCode_native");
278 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700279
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700280 ScopedUtfChars pathStr(env, path);
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -0800281 std::unique_ptr<NativeCode> code;
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700282 bool needs_native_bridge = false;
Yong WU03c86602014-07-28 21:20:18 +0800283
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700284 void* handle = OpenNativeLibrary(env,
285 sdkVersion,
286 pathStr.c_str(),
287 classLoader,
288 libraryPath,
289 &needs_native_bridge,
dimitryf66a2232017-10-04 19:44:56 +0200290 &g_error_msg);
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700291
292 if (handle == nullptr) {
293 ALOGW("NativeActivity LoadNativeLibrary(\"%s\") failed: %s",
294 pathStr.c_str(),
dimitryf66a2232017-10-04 19:44:56 +0200295 g_error_msg.c_str());
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700296 return 0;
Yong WU03c86602014-07-28 21:20:18 +0800297 }
Yong WU03c86602014-07-28 21:20:18 +0800298
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700299 void* funcPtr = NULL;
300 const char* funcStr = env->GetStringUTFChars(funcName, NULL);
301 if (needs_native_bridge) {
302 funcPtr = NativeBridgeGetTrampoline(handle, funcStr, NULL, 0);
303 } else {
304 funcPtr = dlsym(handle, funcStr);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700305 }
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700306
307 code.reset(new NativeCode(handle, (ANativeActivity_createFunc*)funcPtr));
308 env->ReleaseStringUTFChars(funcName, funcStr);
309
310 if (code->createActivityFunc == NULL) {
dimitryf66a2232017-10-04 19:44:56 +0200311 g_error_msg = needs_native_bridge ? NativeBridgeGetError() : dlerror();
312 ALOGW("ANativeActivity_onCreate not found: %s", g_error_msg.c_str());
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700313 return 0;
314 }
315
316 code->messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueue);
317 if (code->messageQueue == NULL) {
dimitryf66a2232017-10-04 19:44:56 +0200318 g_error_msg = "Unable to retrieve native MessageQueue";
319 ALOGW("%s", g_error_msg.c_str());
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700320 return 0;
321 }
322
323 int msgpipe[2];
324 if (pipe(msgpipe)) {
dimitryf66a2232017-10-04 19:44:56 +0200325 g_error_msg = android::base::StringPrintf("could not create pipe: %s", strerror(errno));
326 ALOGW("%s", g_error_msg.c_str());
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700327 return 0;
328 }
329 code->mainWorkRead = msgpipe[0];
330 code->mainWorkWrite = msgpipe[1];
331 int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK);
332 SLOGW_IF(result != 0, "Could not make main work read pipe "
333 "non-blocking: %s", strerror(errno));
334 result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK);
335 SLOGW_IF(result != 0, "Could not make main work write pipe "
336 "non-blocking: %s", strerror(errno));
337 code->messageQueue->getLooper()->addFd(
338 code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code.get());
339
340 code->ANativeActivity::callbacks = &code->callbacks;
341 if (env->GetJavaVM(&code->vm) < 0) {
dimitryf66a2232017-10-04 19:44:56 +0200342 g_error_msg = "NativeActivity GetJavaVM failed";
343 ALOGW("%s", g_error_msg.c_str());
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700344 return 0;
345 }
346 code->env = env;
347 code->clazz = env->NewGlobalRef(clazz);
348
349 const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL);
350 code->internalDataPathObj = dirStr;
351 code->internalDataPath = code->internalDataPathObj.string();
352 env->ReleaseStringUTFChars(internalDataDir, dirStr);
353
354 if (externalDataDir != NULL) {
355 dirStr = env->GetStringUTFChars(externalDataDir, NULL);
356 code->externalDataPathObj = dirStr;
357 env->ReleaseStringUTFChars(externalDataDir, dirStr);
358 }
359 code->externalDataPath = code->externalDataPathObj.string();
360
361 code->sdkVersion = sdkVersion;
362
Adam Lesinski5c690d52017-03-17 15:17:12 -0700363 code->javaAssetManager = env->NewGlobalRef(jAssetMgr);
Adam Lesinskidcb3c652017-01-23 12:58:11 -0800364 code->assetManager = NdkAssetManagerForJavaObject(env, jAssetMgr);
Dimitry Ivanov569834d2016-07-06 14:17:52 -0700365
366 if (obbDir != NULL) {
367 dirStr = env->GetStringUTFChars(obbDir, NULL);
368 code->obbPathObj = dirStr;
369 env->ReleaseStringUTFChars(obbDir, dirStr);
370 }
371 code->obbPath = code->obbPathObj.string();
372
373 jbyte* rawSavedState = NULL;
374 jsize rawSavedSize = 0;
375 if (savedState != NULL) {
376 rawSavedState = env->GetByteArrayElements(savedState, NULL);
377 rawSavedSize = env->GetArrayLength(savedState);
378 }
379
380 code->createActivityFunc(code.get(), rawSavedState, rawSavedSize);
381
382 if (rawSavedState != NULL) {
383 env->ReleaseByteArrayElements(savedState, rawSavedState, 0);
384 }
385
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -0800386 return (jlong)code.release();
387}
388
389static jstring getDlError_native(JNIEnv* env, jobject clazz) {
dimitryf66a2232017-10-04 19:44:56 +0200390 jstring result = env->NewStringUTF(g_error_msg.c_str());
391 g_error_msg.clear();
392 return result;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700393}
394
395static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000396unloadNativeCode_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700397{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800398 if (kLogTrace) {
399 ALOGD("unloadNativeCode_native");
400 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700401 if (handle != 0) {
402 NativeCode* code = (NativeCode*)handle;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700403 delete code;
404 }
405}
406
407static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000408onStart_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700409{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800410 if (kLogTrace) {
411 ALOGD("onStart_native");
412 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700413 if (handle != 0) {
414 NativeCode* code = (NativeCode*)handle;
415 if (code->callbacks.onStart != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700416 code->callbacks.onStart(code);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700417 }
418 }
419}
420
421static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000422onResume_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700423{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800424 if (kLogTrace) {
425 ALOGD("onResume_native");
426 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700427 if (handle != 0) {
428 NativeCode* code = (NativeCode*)handle;
429 if (code->callbacks.onResume != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700430 code->callbacks.onResume(code);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700431 }
432 }
433}
434
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700435static jbyteArray
Ashok Bhat58b8b242014-01-02 16:52:41 +0000436onSaveInstanceState_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700437{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800438 if (kLogTrace) {
439 ALOGD("onSaveInstanceState_native");
440 }
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700441
442 jbyteArray array = NULL;
443
Dianne Hackborn69969e42010-05-04 11:40:40 -0700444 if (handle != 0) {
445 NativeCode* code = (NativeCode*)handle;
446 if (code->callbacks.onSaveInstanceState != NULL) {
447 size_t len = 0;
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700448 jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len);
449 if (len > 0) {
450 array = env->NewByteArray(len);
451 if (array != NULL) {
452 env->SetByteArrayRegion(array, 0, len, state);
453 }
454 }
455 if (state != NULL) {
456 free(state);
457 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700458 }
459 }
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700460
461 return array;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700462}
463
464static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000465onPause_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700466{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800467 if (kLogTrace) {
468 ALOGD("onPause_native");
469 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700470 if (handle != 0) {
471 NativeCode* code = (NativeCode*)handle;
472 if (code->callbacks.onPause != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700473 code->callbacks.onPause(code);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700474 }
475 }
476}
477
478static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000479onStop_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700480{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800481 if (kLogTrace) {
482 ALOGD("onStop_native");
483 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700484 if (handle != 0) {
485 NativeCode* code = (NativeCode*)handle;
486 if (code->callbacks.onStop != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700487 code->callbacks.onStop(code);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700488 }
489 }
490}
491
492static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000493onConfigurationChanged_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700494{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800495 if (kLogTrace) {
496 ALOGD("onConfigurationChanged_native");
497 }
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700498 if (handle != 0) {
499 NativeCode* code = (NativeCode*)handle;
500 if (code->callbacks.onConfigurationChanged != NULL) {
501 code->callbacks.onConfigurationChanged(code);
502 }
503 }
504}
505
506static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000507onLowMemory_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700508{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800509 if (kLogTrace) {
510 ALOGD("onLowMemory_native");
511 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700512 if (handle != 0) {
513 NativeCode* code = (NativeCode*)handle;
514 if (code->callbacks.onLowMemory != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700515 code->callbacks.onLowMemory(code);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700516 }
517 }
518}
519
520static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000521onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jlong handle, jboolean focused)
Dianne Hackborn69969e42010-05-04 11:40:40 -0700522{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800523 if (kLogTrace) {
524 ALOGD("onWindowFocusChanged_native");
525 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700526 if (handle != 0) {
527 NativeCode* code = (NativeCode*)handle;
528 if (code->callbacks.onWindowFocusChanged != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700529 code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700530 }
531 }
532}
533
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700534static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000535onSurfaceCreated_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface)
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700536{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800537 if (kLogTrace) {
538 ALOGD("onSurfaceCreated_native");
539 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700540 if (handle != 0) {
541 NativeCode* code = (NativeCode*)handle;
542 code->setSurface(surface);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700543 if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700544 code->callbacks.onNativeWindowCreated(code,
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700545 code->nativeWindow.get());
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700546 }
547 }
548}
549
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700550static int32_t getWindowProp(ANativeWindow* window, int what) {
551 int value;
552 int res = window->query(window, what, &value);
553 return res < 0 ? res : value;
554}
555
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700556static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000557onSurfaceChanged_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface,
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700558 jint format, jint width, jint height)
559{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800560 if (kLogTrace) {
561 ALOGD("onSurfaceChanged_native");
562 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700563 if (handle != 0) {
564 NativeCode* code = (NativeCode*)handle;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700565 sp<ANativeWindow> oldNativeWindow = code->nativeWindow;
566 code->setSurface(surface);
567 if (oldNativeWindow != code->nativeWindow) {
Dianne Hackborn8ae5a8e2010-07-01 18:44:46 -0700568 if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700569 code->callbacks.onNativeWindowDestroyed(code,
Dianne Hackborn8ae5a8e2010-07-01 18:44:46 -0700570 oldNativeWindow.get());
571 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700572 if (code->nativeWindow != NULL) {
573 if (code->callbacks.onNativeWindowCreated != NULL) {
574 code->callbacks.onNativeWindowCreated(code,
575 code->nativeWindow.get());
576 }
577 code->lastWindowWidth = getWindowProp(code->nativeWindow.get(),
578 NATIVE_WINDOW_WIDTH);
579 code->lastWindowHeight = getWindowProp(code->nativeWindow.get(),
580 NATIVE_WINDOW_HEIGHT);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700581 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700582 } else {
583 // Maybe it resized?
584 int32_t newWidth = getWindowProp(code->nativeWindow.get(),
585 NATIVE_WINDOW_WIDTH);
586 int32_t newHeight = getWindowProp(code->nativeWindow.get(),
587 NATIVE_WINDOW_HEIGHT);
588 if (newWidth != code->lastWindowWidth
589 || newHeight != code->lastWindowHeight) {
590 if (code->callbacks.onNativeWindowResized != NULL) {
591 code->callbacks.onNativeWindowResized(code,
592 code->nativeWindow.get());
593 }
594 }
595 }
596 }
597}
598
599static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000600onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jlong handle)
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700601{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800602 if (kLogTrace) {
603 ALOGD("onSurfaceRedrawNeeded_native");
604 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700605 if (handle != 0) {
606 NativeCode* code = (NativeCode*)handle;
607 if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) {
608 code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get());
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700609 }
610 }
611}
612
613static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000614onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface)
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700615{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800616 if (kLogTrace) {
617 ALOGD("onSurfaceDestroyed_native");
618 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700619 if (handle != 0) {
620 NativeCode* code = (NativeCode*)handle;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700621 if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700622 code->callbacks.onNativeWindowDestroyed(code,
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700623 code->nativeWindow.get());
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700624 }
625 code->setSurface(NULL);
626 }
627}
628
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700629static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000630onInputQueueCreated_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr)
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700631{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800632 if (kLogTrace) {
633 ALOGD("onInputChannelCreated_native");
634 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700635 if (handle != 0) {
636 NativeCode* code = (NativeCode*)handle;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700637 if (code->callbacks.onInputQueueCreated != NULL) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700638 AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr);
639 code->callbacks.onInputQueueCreated(code, queue);
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700640 }
641 }
642}
643
644static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000645onInputQueueDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr)
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700646{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800647 if (kLogTrace) {
648 ALOGD("onInputChannelDestroyed_native");
649 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700650 if (handle != 0) {
651 NativeCode* code = (NativeCode*)handle;
Michael Wrighta44dd262013-04-10 21:12:00 -0700652 if (code->callbacks.onInputQueueDestroyed != NULL) {
653 AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr);
654 code->callbacks.onInputQueueDestroyed(code, queue);
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700655 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700656 }
657}
658
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700659static void
Ashok Bhat58b8b242014-01-02 16:52:41 +0000660onContentRectChanged_native(JNIEnv* env, jobject clazz, jlong handle,
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700661 jint x, jint y, jint w, jint h)
662{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800663 if (kLogTrace) {
664 ALOGD("onContentRectChanged_native");
665 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700666 if (handle != 0) {
667 NativeCode* code = (NativeCode*)handle;
668 if (code->callbacks.onContentRectChanged != NULL) {
669 ARect rect;
670 rect.left = x;
671 rect.top = y;
672 rect.right = x+w;
673 rect.bottom = y+h;
674 code->callbacks.onContentRectChanged(code, &rect);
675 }
676 }
677}
678
Dianne Hackborn69969e42010-05-04 11:40:40 -0700679static const JNINativeMethod g_methods[] = {
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -0800680 { "loadNativeCode",
Dimitry Ivanovea902812016-02-23 14:25:50 -0800681 "(Ljava/lang/String;Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILandroid/content/res/AssetManager;[BLjava/lang/ClassLoader;Ljava/lang/String;)J",
Dmitriy Ivanov6f06ad72015-11-15 14:58:36 -0800682 (void*)loadNativeCode_native },
683 { "getDlError", "()Ljava/lang/String;", (void*) getDlError_native },
Ashok Bhat58b8b242014-01-02 16:52:41 +0000684 { "unloadNativeCode", "(J)V", (void*)unloadNativeCode_native },
685 { "onStartNative", "(J)V", (void*)onStart_native },
686 { "onResumeNative", "(J)V", (void*)onResume_native },
687 { "onSaveInstanceStateNative", "(J)[B", (void*)onSaveInstanceState_native },
688 { "onPauseNative", "(J)V", (void*)onPause_native },
689 { "onStopNative", "(J)V", (void*)onStop_native },
690 { "onConfigurationChangedNative", "(J)V", (void*)onConfigurationChanged_native },
691 { "onLowMemoryNative", "(J)V", (void*)onLowMemory_native },
692 { "onWindowFocusChangedNative", "(JZ)V", (void*)onWindowFocusChanged_native },
693 { "onSurfaceCreatedNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceCreated_native },
694 { "onSurfaceChangedNative", "(JLandroid/view/Surface;III)V", (void*)onSurfaceChanged_native },
695 { "onSurfaceRedrawNeededNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native },
696 { "onSurfaceDestroyedNative", "(J)V", (void*)onSurfaceDestroyed_native },
697 { "onInputQueueCreatedNative", "(JJ)V",
Michael Wrighta44dd262013-04-10 21:12:00 -0700698 (void*)onInputQueueCreated_native },
Ashok Bhat58b8b242014-01-02 16:52:41 +0000699 { "onInputQueueDestroyedNative", "(JJ)V",
Michael Wrighta44dd262013-04-10 21:12:00 -0700700 (void*)onInputQueueDestroyed_native },
Ashok Bhat58b8b242014-01-02 16:52:41 +0000701 { "onContentRectChangedNative", "(JIIII)V", (void*)onContentRectChanged_native },
Dianne Hackborn69969e42010-05-04 11:40:40 -0700702};
703
704static const char* const kNativeActivityPathName = "android/app/NativeActivity";
705
706int register_android_app_NativeActivity(JNIEnv* env)
707{
Steve Block5baa3a62011-12-20 16:23:08 +0000708 //ALOGD("register_android_app_NativeActivity");
Andreas Gampe987f79f2014-11-18 17:29:46 -0800709 jclass clazz = FindClassOrDie(env, kNativeActivityPathName);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700710
Andreas Gampe987f79f2014-11-18 17:29:46 -0800711 gNativeActivityClassInfo.finish = GetMethodIDOrDie(env, clazz, "finish", "()V");
712 gNativeActivityClassInfo.setWindowFlags = GetMethodIDOrDie(env, clazz, "setWindowFlags",
713 "(II)V");
714 gNativeActivityClassInfo.setWindowFormat = GetMethodIDOrDie(env, clazz, "setWindowFormat",
715 "(I)V");
716 gNativeActivityClassInfo.showIme = GetMethodIDOrDie(env, clazz, "showIme", "(I)V");
717 gNativeActivityClassInfo.hideIme = GetMethodIDOrDie(env, clazz, "hideIme", "(I)V");
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700718
Andreas Gampe987f79f2014-11-18 17:29:46 -0800719 return RegisterMethodsOrDie(env, kNativeActivityPathName, g_methods, NELEM(g_methods));
Dianne Hackborn69969e42010-05-04 11:40:40 -0700720}
721
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700722} // namespace android