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