Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 20 | #include <poll.h> |
| 21 | #include <dlfcn.h> |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 23 | |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 24 | #include <memory> |
| 25 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 26 | #include <android_runtime/android_app_NativeActivity.h> |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 27 | #include <android_runtime/android_util_AssetManager.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 28 | #include <android_runtime/android_view_Surface.h> |
| 29 | #include <android_runtime/AndroidRuntime.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 30 | #include <input/InputTransport.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <gui/Surface.h> |
| 33 | |
| 34 | #include <system/window.h> |
| 35 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 36 | #include <utils/Looper.h> |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 37 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 38 | #include "JNIHelp.h" |
| 39 | #include "android_os_MessageQueue.h" |
| 40 | #include "android_view_InputChannel.h" |
| 41 | #include "android_view_KeyEvent.h" |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 42 | |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 43 | #include "nativebridge/native_bridge.h" |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 44 | #include "nativeloader/native_loader.h" |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 45 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 46 | #include "core_jni_helpers.h" |
| 47 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 48 | #include "ScopedUtfChars.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 49 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 50 | #define LOG_TRACE(...) |
Steve Block | 28d9f02 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 51 | //#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 52 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 53 | namespace android |
| 54 | { |
| 55 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 56 | static const bool kLogTrace = false; |
| 57 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 58 | static struct { |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 59 | jmethodID finish; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 60 | jmethodID setWindowFlags; |
| 61 | jmethodID setWindowFormat; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 62 | jmethodID showIme; |
| 63 | jmethodID hideIme; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 64 | } gNativeActivityClassInfo; |
| 65 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 66 | // ------------------------------------------------------------------------ |
| 67 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 68 | struct ActivityWork { |
| 69 | int32_t cmd; |
| 70 | int32_t arg1; |
| 71 | int32_t arg2; |
| 72 | }; |
| 73 | |
| 74 | enum { |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 75 | CMD_FINISH = 1, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 76 | CMD_SET_WINDOW_FORMAT, |
| 77 | CMD_SET_WINDOW_FLAGS, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 78 | CMD_SHOW_SOFT_INPUT, |
| 79 | CMD_HIDE_SOFT_INPUT, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static void write_work(int fd, int32_t cmd, int32_t arg1=0, int32_t arg2=0) { |
| 83 | ActivityWork work; |
| 84 | work.cmd = cmd; |
| 85 | work.arg1 = arg1; |
| 86 | work.arg2 = arg2; |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 87 | |
| 88 | if (kLogTrace) { |
| 89 | ALOGD("write_work: cmd=%d", cmd); |
| 90 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 91 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 92 | restart: |
| 93 | int res = write(fd, &work, sizeof(work)); |
| 94 | if (res < 0 && errno == EINTR) { |
| 95 | goto restart; |
| 96 | } |
| 97 | |
| 98 | if (res == sizeof(work)) return; |
| 99 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 100 | if (res < 0) ALOGW("Failed writing to work fd: %s", strerror(errno)); |
| 101 | else ALOGW("Truncated writing to work fd: %d", res); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static bool read_work(int fd, ActivityWork* outWork) { |
| 105 | int res = read(fd, outWork, sizeof(ActivityWork)); |
| 106 | // no need to worry about EINTR, poll loop will just come back again. |
| 107 | if (res == sizeof(ActivityWork)) return true; |
| 108 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 109 | if (res < 0) ALOGW("Failed reading work fd: %s", strerror(errno)); |
| 110 | else ALOGW("Truncated reading work fd: %d", res); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Native state for interacting with the NativeActivity class. |
| 116 | */ |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 117 | struct NativeCode : public ANativeActivity { |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 118 | NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 119 | memset((ANativeActivity*)this, 0, sizeof(ANativeActivity)); |
| 120 | memset(&callbacks, 0, sizeof(callbacks)); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 121 | dlhandle = _dlhandle; |
| 122 | createActivityFunc = _createFunc; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 123 | nativeWindow = NULL; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 124 | mainWorkRead = mainWorkWrite = -1; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | ~NativeCode() { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 128 | if (callbacks.onDestroy != NULL) { |
| 129 | callbacks.onDestroy(this); |
| 130 | } |
| 131 | if (env != NULL && clazz != NULL) { |
| 132 | env->DeleteGlobalRef(clazz); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 133 | } |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 134 | if (messageQueue != NULL && mainWorkRead >= 0) { |
| 135 | messageQueue->getLooper()->removeFd(mainWorkRead); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 136 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 137 | setSurface(NULL); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 138 | if (mainWorkRead >= 0) close(mainWorkRead); |
| 139 | if (mainWorkWrite >= 0) close(mainWorkWrite); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 140 | if (dlhandle != NULL) { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 141 | // for now don't unload... we probably should clean this |
| 142 | // up and only keep one open dlhandle per proc, since there |
| 143 | // is really no benefit to unloading the code. |
| 144 | //dlclose(dlhandle); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void setSurface(jobject _surface) { |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 149 | if (_surface != NULL) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 150 | nativeWindow = android_view_Surface_getNativeWindow(env, _surface); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 151 | } else { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 152 | nativeWindow = NULL; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 156 | ANativeActivityCallbacks callbacks; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 157 | |
| 158 | void* dlhandle; |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 159 | ANativeActivity_createFunc* createActivityFunc; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 160 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 161 | String8 internalDataPathObj; |
| 162 | String8 externalDataPathObj; |
| 163 | String8 obbPathObj; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 164 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 165 | sp<ANativeWindow> nativeWindow; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 166 | int32_t lastWindowWidth; |
| 167 | int32_t lastWindowHeight; |
| 168 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 169 | // These are used to wake up the main thread to process work. |
| 170 | int mainWorkRead; |
| 171 | int mainWorkWrite; |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 172 | sp<MessageQueue> messageQueue; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 175 | void android_NativeActivity_finish(ANativeActivity* activity) { |
| 176 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 177 | write_work(code->mainWorkWrite, CMD_FINISH, 0); |
| 178 | } |
| 179 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 180 | void android_NativeActivity_setWindowFormat( |
| 181 | ANativeActivity* activity, int32_t format) { |
| 182 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 183 | write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format); |
| 184 | } |
| 185 | |
| 186 | void android_NativeActivity_setWindowFlags( |
| 187 | ANativeActivity* activity, int32_t values, int32_t mask) { |
| 188 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 189 | write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask); |
| 190 | } |
| 191 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 192 | void android_NativeActivity_showSoftInput( |
| 193 | ANativeActivity* activity, int32_t flags) { |
| 194 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 195 | write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags); |
| 196 | } |
| 197 | |
| 198 | void android_NativeActivity_hideSoftInput( |
| 199 | ANativeActivity* activity, int32_t flags) { |
| 200 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 201 | write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags); |
| 202 | } |
| 203 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 204 | // ------------------------------------------------------------------------ |
| 205 | |
| 206 | /* |
| 207 | * Callback for handling native events on the application's main thread. |
| 208 | */ |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 209 | static int mainWorkCallback(int fd, int events, void* data) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 210 | NativeCode* code = (NativeCode*)data; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 211 | if ((events & POLLIN) == 0) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 212 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | ActivityWork work; |
| 216 | if (!read_work(code->mainWorkRead, &work)) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 217 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 218 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 219 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 220 | if (kLogTrace) { |
| 221 | ALOGD("mainWorkCallback: cmd=%d", work.cmd); |
| 222 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 223 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 224 | switch (work.cmd) { |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 225 | case CMD_FINISH: { |
| 226 | code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 227 | code->messageQueue->raiseAndClearException(code->env, "finish"); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 228 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 229 | case CMD_SET_WINDOW_FORMAT: { |
| 230 | code->env->CallVoidMethod(code->clazz, |
| 231 | gNativeActivityClassInfo.setWindowFormat, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 232 | code->messageQueue->raiseAndClearException(code->env, "setWindowFormat"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 233 | } break; |
| 234 | case CMD_SET_WINDOW_FLAGS: { |
| 235 | code->env->CallVoidMethod(code->clazz, |
| 236 | gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 237 | code->messageQueue->raiseAndClearException(code->env, "setWindowFlags"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 238 | } break; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 239 | case CMD_SHOW_SOFT_INPUT: { |
| 240 | code->env->CallVoidMethod(code->clazz, |
| 241 | gNativeActivityClassInfo.showIme, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 242 | code->messageQueue->raiseAndClearException(code->env, "showIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 243 | } break; |
| 244 | case CMD_HIDE_SOFT_INPUT: { |
| 245 | code->env->CallVoidMethod(code->clazz, |
| 246 | gNativeActivityClassInfo.hideIme, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 247 | code->messageQueue->raiseAndClearException(code->env, "hideIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 248 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 249 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 250 | ALOGW("Unknown work command: %d", work.cmd); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 251 | break; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 254 | return 1; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 257 | // ------------------------------------------------------------------------ |
| 258 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 259 | static jlong |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 260 | loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName, |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 261 | jobject messageQueue, jstring internalDataDir, jstring obbDir, |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 262 | jstring externalDataDir, jint sdkVersion, jobject jAssetMgr, |
Dimitry Ivanov | ea90281 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 263 | jbyteArray savedState, jobject classLoader, jstring libraryPath) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 264 | if (kLogTrace) { |
| 265 | ALOGD("loadNativeCode_native"); |
| 266 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 267 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 268 | ScopedUtfChars pathStr(env, path); |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 269 | std::unique_ptr<NativeCode> code; |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 270 | bool needs_native_bridge = false; |
| 271 | std::string error_msg; |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 272 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 273 | void* handle = OpenNativeLibrary(env, |
| 274 | sdkVersion, |
| 275 | pathStr.c_str(), |
| 276 | classLoader, |
| 277 | libraryPath, |
| 278 | &needs_native_bridge, |
| 279 | &error_msg); |
| 280 | |
| 281 | if (handle == nullptr) { |
| 282 | ALOGW("NativeActivity LoadNativeLibrary(\"%s\") failed: %s", |
| 283 | pathStr.c_str(), |
| 284 | error_msg.c_str()); |
| 285 | return 0; |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 286 | } |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 287 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 288 | void* funcPtr = NULL; |
| 289 | const char* funcStr = env->GetStringUTFChars(funcName, NULL); |
| 290 | if (needs_native_bridge) { |
| 291 | funcPtr = NativeBridgeGetTrampoline(handle, funcStr, NULL, 0); |
| 292 | } else { |
| 293 | funcPtr = dlsym(handle, funcStr); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 294 | } |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 295 | |
| 296 | code.reset(new NativeCode(handle, (ANativeActivity_createFunc*)funcPtr)); |
| 297 | env->ReleaseStringUTFChars(funcName, funcStr); |
| 298 | |
| 299 | if (code->createActivityFunc == NULL) { |
| 300 | ALOGW("ANativeActivity_onCreate not found"); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | code->messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueue); |
| 305 | if (code->messageQueue == NULL) { |
| 306 | ALOGW("Unable to retrieve native MessageQueue"); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | int msgpipe[2]; |
| 311 | if (pipe(msgpipe)) { |
| 312 | ALOGW("could not create pipe: %s", strerror(errno)); |
| 313 | return 0; |
| 314 | } |
| 315 | code->mainWorkRead = msgpipe[0]; |
| 316 | code->mainWorkWrite = msgpipe[1]; |
| 317 | int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK); |
| 318 | SLOGW_IF(result != 0, "Could not make main work read pipe " |
| 319 | "non-blocking: %s", strerror(errno)); |
| 320 | result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK); |
| 321 | SLOGW_IF(result != 0, "Could not make main work write pipe " |
| 322 | "non-blocking: %s", strerror(errno)); |
| 323 | code->messageQueue->getLooper()->addFd( |
| 324 | code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code.get()); |
| 325 | |
| 326 | code->ANativeActivity::callbacks = &code->callbacks; |
| 327 | if (env->GetJavaVM(&code->vm) < 0) { |
| 328 | ALOGW("NativeActivity GetJavaVM failed"); |
| 329 | return 0; |
| 330 | } |
| 331 | code->env = env; |
| 332 | code->clazz = env->NewGlobalRef(clazz); |
| 333 | |
| 334 | const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL); |
| 335 | code->internalDataPathObj = dirStr; |
| 336 | code->internalDataPath = code->internalDataPathObj.string(); |
| 337 | env->ReleaseStringUTFChars(internalDataDir, dirStr); |
| 338 | |
| 339 | if (externalDataDir != NULL) { |
| 340 | dirStr = env->GetStringUTFChars(externalDataDir, NULL); |
| 341 | code->externalDataPathObj = dirStr; |
| 342 | env->ReleaseStringUTFChars(externalDataDir, dirStr); |
| 343 | } |
| 344 | code->externalDataPath = code->externalDataPathObj.string(); |
| 345 | |
| 346 | code->sdkVersion = sdkVersion; |
| 347 | |
| 348 | code->assetManager = assetManagerForJavaObject(env, jAssetMgr); |
| 349 | |
| 350 | if (obbDir != NULL) { |
| 351 | dirStr = env->GetStringUTFChars(obbDir, NULL); |
| 352 | code->obbPathObj = dirStr; |
| 353 | env->ReleaseStringUTFChars(obbDir, dirStr); |
| 354 | } |
| 355 | code->obbPath = code->obbPathObj.string(); |
| 356 | |
| 357 | jbyte* rawSavedState = NULL; |
| 358 | jsize rawSavedSize = 0; |
| 359 | if (savedState != NULL) { |
| 360 | rawSavedState = env->GetByteArrayElements(savedState, NULL); |
| 361 | rawSavedSize = env->GetArrayLength(savedState); |
| 362 | } |
| 363 | |
| 364 | code->createActivityFunc(code.get(), rawSavedState, rawSavedSize); |
| 365 | |
| 366 | if (rawSavedState != NULL) { |
| 367 | env->ReleaseByteArrayElements(savedState, rawSavedState, 0); |
| 368 | } |
| 369 | |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 370 | return (jlong)code.release(); |
| 371 | } |
| 372 | |
| 373 | static jstring getDlError_native(JNIEnv* env, jobject clazz) { |
| 374 | return env->NewStringUTF(dlerror()); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 378 | unloadNativeCode_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 379 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 380 | if (kLogTrace) { |
| 381 | ALOGD("unloadNativeCode_native"); |
| 382 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 383 | if (handle != 0) { |
| 384 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 385 | delete code; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 390 | onStart_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 391 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 392 | if (kLogTrace) { |
| 393 | ALOGD("onStart_native"); |
| 394 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 395 | if (handle != 0) { |
| 396 | NativeCode* code = (NativeCode*)handle; |
| 397 | if (code->callbacks.onStart != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 398 | code->callbacks.onStart(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 404 | onResume_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 405 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 406 | if (kLogTrace) { |
| 407 | ALOGD("onResume_native"); |
| 408 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 409 | if (handle != 0) { |
| 410 | NativeCode* code = (NativeCode*)handle; |
| 411 | if (code->callbacks.onResume != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 412 | code->callbacks.onResume(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 417 | static jbyteArray |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 418 | onSaveInstanceState_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 419 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 420 | if (kLogTrace) { |
| 421 | ALOGD("onSaveInstanceState_native"); |
| 422 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 423 | |
| 424 | jbyteArray array = NULL; |
| 425 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 426 | if (handle != 0) { |
| 427 | NativeCode* code = (NativeCode*)handle; |
| 428 | if (code->callbacks.onSaveInstanceState != NULL) { |
| 429 | size_t len = 0; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 430 | jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len); |
| 431 | if (len > 0) { |
| 432 | array = env->NewByteArray(len); |
| 433 | if (array != NULL) { |
| 434 | env->SetByteArrayRegion(array, 0, len, state); |
| 435 | } |
| 436 | } |
| 437 | if (state != NULL) { |
| 438 | free(state); |
| 439 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 442 | |
| 443 | return array; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 447 | onPause_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 448 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 449 | if (kLogTrace) { |
| 450 | ALOGD("onPause_native"); |
| 451 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 452 | if (handle != 0) { |
| 453 | NativeCode* code = (NativeCode*)handle; |
| 454 | if (code->callbacks.onPause != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 455 | code->callbacks.onPause(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 461 | onStop_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 462 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 463 | if (kLogTrace) { |
| 464 | ALOGD("onStop_native"); |
| 465 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 466 | if (handle != 0) { |
| 467 | NativeCode* code = (NativeCode*)handle; |
| 468 | if (code->callbacks.onStop != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 469 | code->callbacks.onStop(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 475 | onConfigurationChanged_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 476 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 477 | if (kLogTrace) { |
| 478 | ALOGD("onConfigurationChanged_native"); |
| 479 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 480 | if (handle != 0) { |
| 481 | NativeCode* code = (NativeCode*)handle; |
| 482 | if (code->callbacks.onConfigurationChanged != NULL) { |
| 483 | code->callbacks.onConfigurationChanged(code); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 489 | onLowMemory_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 490 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 491 | if (kLogTrace) { |
| 492 | ALOGD("onLowMemory_native"); |
| 493 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 494 | if (handle != 0) { |
| 495 | NativeCode* code = (NativeCode*)handle; |
| 496 | if (code->callbacks.onLowMemory != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 497 | code->callbacks.onLowMemory(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 503 | onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jlong handle, jboolean focused) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 504 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 505 | if (kLogTrace) { |
| 506 | ALOGD("onWindowFocusChanged_native"); |
| 507 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 508 | if (handle != 0) { |
| 509 | NativeCode* code = (NativeCode*)handle; |
| 510 | if (code->callbacks.onWindowFocusChanged != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 511 | code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 516 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 517 | onSurfaceCreated_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 518 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 519 | if (kLogTrace) { |
| 520 | ALOGD("onSurfaceCreated_native"); |
| 521 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 522 | if (handle != 0) { |
| 523 | NativeCode* code = (NativeCode*)handle; |
| 524 | code->setSurface(surface); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 525 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 526 | code->callbacks.onNativeWindowCreated(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 527 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 532 | static int32_t getWindowProp(ANativeWindow* window, int what) { |
| 533 | int value; |
| 534 | int res = window->query(window, what, &value); |
| 535 | return res < 0 ? res : value; |
| 536 | } |
| 537 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 538 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 539 | onSurfaceChanged_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface, |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 540 | jint format, jint width, jint height) |
| 541 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 542 | if (kLogTrace) { |
| 543 | ALOGD("onSurfaceChanged_native"); |
| 544 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 545 | if (handle != 0) { |
| 546 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 547 | sp<ANativeWindow> oldNativeWindow = code->nativeWindow; |
| 548 | code->setSurface(surface); |
| 549 | if (oldNativeWindow != code->nativeWindow) { |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 550 | if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 551 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 552 | oldNativeWindow.get()); |
| 553 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 554 | if (code->nativeWindow != NULL) { |
| 555 | if (code->callbacks.onNativeWindowCreated != NULL) { |
| 556 | code->callbacks.onNativeWindowCreated(code, |
| 557 | code->nativeWindow.get()); |
| 558 | } |
| 559 | code->lastWindowWidth = getWindowProp(code->nativeWindow.get(), |
| 560 | NATIVE_WINDOW_WIDTH); |
| 561 | code->lastWindowHeight = getWindowProp(code->nativeWindow.get(), |
| 562 | NATIVE_WINDOW_HEIGHT); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 563 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 564 | } else { |
| 565 | // Maybe it resized? |
| 566 | int32_t newWidth = getWindowProp(code->nativeWindow.get(), |
| 567 | NATIVE_WINDOW_WIDTH); |
| 568 | int32_t newHeight = getWindowProp(code->nativeWindow.get(), |
| 569 | NATIVE_WINDOW_HEIGHT); |
| 570 | if (newWidth != code->lastWindowWidth |
| 571 | || newHeight != code->lastWindowHeight) { |
| 572 | if (code->callbacks.onNativeWindowResized != NULL) { |
| 573 | code->callbacks.onNativeWindowResized(code, |
| 574 | code->nativeWindow.get()); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 582 | onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 583 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 584 | if (kLogTrace) { |
| 585 | ALOGD("onSurfaceRedrawNeeded_native"); |
| 586 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 587 | if (handle != 0) { |
| 588 | NativeCode* code = (NativeCode*)handle; |
| 589 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) { |
| 590 | code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 596 | onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 597 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 598 | if (kLogTrace) { |
| 599 | ALOGD("onSurfaceDestroyed_native"); |
| 600 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 601 | if (handle != 0) { |
| 602 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 603 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 604 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 605 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 606 | } |
| 607 | code->setSurface(NULL); |
| 608 | } |
| 609 | } |
| 610 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 611 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 612 | onInputQueueCreated_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 613 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 614 | if (kLogTrace) { |
| 615 | ALOGD("onInputChannelCreated_native"); |
| 616 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 617 | if (handle != 0) { |
| 618 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 619 | if (code->callbacks.onInputQueueCreated != NULL) { |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 620 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 621 | code->callbacks.onInputQueueCreated(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 627 | onInputQueueDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 628 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 629 | if (kLogTrace) { |
| 630 | ALOGD("onInputChannelDestroyed_native"); |
| 631 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 632 | if (handle != 0) { |
| 633 | NativeCode* code = (NativeCode*)handle; |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 634 | if (code->callbacks.onInputQueueDestroyed != NULL) { |
| 635 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 636 | code->callbacks.onInputQueueDestroyed(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 637 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 641 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 642 | onContentRectChanged_native(JNIEnv* env, jobject clazz, jlong handle, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 643 | jint x, jint y, jint w, jint h) |
| 644 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 645 | if (kLogTrace) { |
| 646 | ALOGD("onContentRectChanged_native"); |
| 647 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 648 | if (handle != 0) { |
| 649 | NativeCode* code = (NativeCode*)handle; |
| 650 | if (code->callbacks.onContentRectChanged != NULL) { |
| 651 | ARect rect; |
| 652 | rect.left = x; |
| 653 | rect.top = y; |
| 654 | rect.right = x+w; |
| 655 | rect.bottom = y+h; |
| 656 | code->callbacks.onContentRectChanged(code, &rect); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 661 | static const JNINativeMethod g_methods[] = { |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 662 | { "loadNativeCode", |
Dimitry Ivanov | ea90281 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 663 | "(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 Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 664 | (void*)loadNativeCode_native }, |
| 665 | { "getDlError", "()Ljava/lang/String;", (void*) getDlError_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 666 | { "unloadNativeCode", "(J)V", (void*)unloadNativeCode_native }, |
| 667 | { "onStartNative", "(J)V", (void*)onStart_native }, |
| 668 | { "onResumeNative", "(J)V", (void*)onResume_native }, |
| 669 | { "onSaveInstanceStateNative", "(J)[B", (void*)onSaveInstanceState_native }, |
| 670 | { "onPauseNative", "(J)V", (void*)onPause_native }, |
| 671 | { "onStopNative", "(J)V", (void*)onStop_native }, |
| 672 | { "onConfigurationChangedNative", "(J)V", (void*)onConfigurationChanged_native }, |
| 673 | { "onLowMemoryNative", "(J)V", (void*)onLowMemory_native }, |
| 674 | { "onWindowFocusChangedNative", "(JZ)V", (void*)onWindowFocusChanged_native }, |
| 675 | { "onSurfaceCreatedNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceCreated_native }, |
| 676 | { "onSurfaceChangedNative", "(JLandroid/view/Surface;III)V", (void*)onSurfaceChanged_native }, |
| 677 | { "onSurfaceRedrawNeededNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native }, |
| 678 | { "onSurfaceDestroyedNative", "(J)V", (void*)onSurfaceDestroyed_native }, |
| 679 | { "onInputQueueCreatedNative", "(JJ)V", |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 680 | (void*)onInputQueueCreated_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 681 | { "onInputQueueDestroyedNative", "(JJ)V", |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 682 | (void*)onInputQueueDestroyed_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 683 | { "onContentRectChangedNative", "(JIIII)V", (void*)onContentRectChanged_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | static const char* const kNativeActivityPathName = "android/app/NativeActivity"; |
| 687 | |
| 688 | int register_android_app_NativeActivity(JNIEnv* env) |
| 689 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 690 | //ALOGD("register_android_app_NativeActivity"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 691 | jclass clazz = FindClassOrDie(env, kNativeActivityPathName); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 692 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 693 | gNativeActivityClassInfo.finish = GetMethodIDOrDie(env, clazz, "finish", "()V"); |
| 694 | gNativeActivityClassInfo.setWindowFlags = GetMethodIDOrDie(env, clazz, "setWindowFlags", |
| 695 | "(II)V"); |
| 696 | gNativeActivityClassInfo.setWindowFormat = GetMethodIDOrDie(env, clazz, "setWindowFormat", |
| 697 | "(I)V"); |
| 698 | gNativeActivityClassInfo.showIme = GetMethodIDOrDie(env, clazz, "showIme", "(I)V"); |
| 699 | gNativeActivityClassInfo.hideIme = GetMethodIDOrDie(env, clazz, "hideIme", "(I)V"); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 700 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 701 | return RegisterMethodsOrDie(env, kNativeActivityPathName, g_methods, NELEM(g_methods)); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 704 | } // namespace android |