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 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 38 | #include <nativehelper/JNIHelp.h> |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 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 | |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 43 | #include "android-base/stringprintf.h" |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 44 | #include "nativebridge/native_bridge.h" |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 45 | #include "nativeloader/native_loader.h" |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 46 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 47 | #include "core_jni_helpers.h" |
| 48 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 49 | #include <nativehelper/ScopedUtfChars.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 50 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 51 | #define LOG_TRACE(...) |
Steve Block | 28d9f02 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 52 | //#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 53 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 54 | namespace android |
| 55 | { |
| 56 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 57 | static const bool kLogTrace = false; |
| 58 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 59 | static struct { |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 60 | jmethodID finish; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 61 | jmethodID setWindowFlags; |
| 62 | jmethodID setWindowFormat; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 63 | jmethodID showIme; |
| 64 | jmethodID hideIme; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 65 | } gNativeActivityClassInfo; |
| 66 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 67 | // ------------------------------------------------------------------------ |
| 68 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 69 | struct ActivityWork { |
| 70 | int32_t cmd; |
| 71 | int32_t arg1; |
| 72 | int32_t arg2; |
| 73 | }; |
| 74 | |
| 75 | enum { |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 76 | CMD_FINISH = 1, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 77 | CMD_SET_WINDOW_FORMAT, |
| 78 | CMD_SET_WINDOW_FLAGS, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 79 | CMD_SHOW_SOFT_INPUT, |
| 80 | CMD_HIDE_SOFT_INPUT, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static 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 Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 88 | |
| 89 | if (kLogTrace) { |
| 90 | ALOGD("write_work: cmd=%d", cmd); |
| 91 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 92 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 93 | restart: |
| 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 101 | if (res < 0) ALOGW("Failed writing to work fd: %s", strerror(errno)); |
| 102 | else ALOGW("Truncated writing to work fd: %d", res); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 110 | if (res < 0) ALOGW("Failed reading work fd: %s", strerror(errno)); |
| 111 | else ALOGW("Truncated reading work fd: %d", res); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 115 | /* |
| 116 | * Native state for interacting with the NativeActivity class. |
| 117 | */ |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 118 | struct NativeCode : public ANativeActivity { |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 119 | NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 120 | memset((ANativeActivity*)this, 0, sizeof(ANativeActivity)); |
| 121 | memset(&callbacks, 0, sizeof(callbacks)); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 122 | dlhandle = _dlhandle; |
| 123 | createActivityFunc = _createFunc; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 124 | nativeWindow = NULL; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 125 | mainWorkRead = mainWorkWrite = -1; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | ~NativeCode() { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 129 | if (callbacks.onDestroy != NULL) { |
| 130 | callbacks.onDestroy(this); |
| 131 | } |
Adam Lesinski | 5c690d5 | 2017-03-17 15:17:12 -0700 | [diff] [blame] | 132 | if (env != NULL) { |
| 133 | if (clazz != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 134 | env->DeleteGlobalRef(clazz); |
Adam Lesinski | 5c690d5 | 2017-03-17 15:17:12 -0700 | [diff] [blame] | 135 | } |
| 136 | if (javaAssetManager != NULL) { |
| 137 | env->DeleteGlobalRef(javaAssetManager); |
| 138 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 139 | } |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 140 | if (messageQueue != NULL && mainWorkRead >= 0) { |
| 141 | messageQueue->getLooper()->removeFd(mainWorkRead); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 142 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 143 | setSurface(NULL); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 144 | if (mainWorkRead >= 0) close(mainWorkRead); |
| 145 | if (mainWorkWrite >= 0) close(mainWorkWrite); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 146 | if (dlhandle != NULL) { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 147 | // 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 Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | void setSurface(jobject _surface) { |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 155 | if (_surface != NULL) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 156 | nativeWindow = android_view_Surface_getNativeWindow(env, _surface); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 157 | } else { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 158 | nativeWindow = NULL; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 162 | ANativeActivityCallbacks callbacks; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 163 | |
| 164 | void* dlhandle; |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 165 | ANativeActivity_createFunc* createActivityFunc; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 166 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 167 | String8 internalDataPathObj; |
| 168 | String8 externalDataPathObj; |
| 169 | String8 obbPathObj; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 170 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 171 | sp<ANativeWindow> nativeWindow; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 172 | int32_t lastWindowWidth; |
| 173 | int32_t lastWindowHeight; |
| 174 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 175 | // These are used to wake up the main thread to process work. |
| 176 | int mainWorkRead; |
| 177 | int mainWorkWrite; |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 178 | sp<MessageQueue> messageQueue; |
Adam Lesinski | 5c690d5 | 2017-03-17 15:17:12 -0700 | [diff] [blame] | 179 | |
| 180 | // Need to hold on to a reference here in case the upper layers destroy our |
| 181 | // AssetManager. |
| 182 | jobject javaAssetManager; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 185 | void android_NativeActivity_finish(ANativeActivity* activity) { |
| 186 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 187 | write_work(code->mainWorkWrite, CMD_FINISH, 0); |
| 188 | } |
| 189 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 190 | void 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 | |
| 196 | void 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 Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 202 | void 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 | |
| 208 | void 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 Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 214 | // ------------------------------------------------------------------------ |
| 215 | |
| 216 | /* |
| 217 | * Callback for handling native events on the application's main thread. |
| 218 | */ |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 219 | static int mainWorkCallback(int fd, int events, void* data) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 220 | NativeCode* code = (NativeCode*)data; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 221 | if ((events & POLLIN) == 0) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 222 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | ActivityWork work; |
| 226 | if (!read_work(code->mainWorkRead, &work)) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 227 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 228 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 229 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 230 | if (kLogTrace) { |
| 231 | ALOGD("mainWorkCallback: cmd=%d", work.cmd); |
| 232 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 233 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 234 | switch (work.cmd) { |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 235 | case CMD_FINISH: { |
| 236 | code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 237 | code->messageQueue->raiseAndClearException(code->env, "finish"); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 238 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 239 | case CMD_SET_WINDOW_FORMAT: { |
| 240 | code->env->CallVoidMethod(code->clazz, |
| 241 | gNativeActivityClassInfo.setWindowFormat, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 242 | code->messageQueue->raiseAndClearException(code->env, "setWindowFormat"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 243 | } break; |
| 244 | case CMD_SET_WINDOW_FLAGS: { |
| 245 | code->env->CallVoidMethod(code->clazz, |
| 246 | gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 247 | code->messageQueue->raiseAndClearException(code->env, "setWindowFlags"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 248 | } break; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 249 | case CMD_SHOW_SOFT_INPUT: { |
| 250 | code->env->CallVoidMethod(code->clazz, |
| 251 | gNativeActivityClassInfo.showIme, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 252 | code->messageQueue->raiseAndClearException(code->env, "showIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 253 | } break; |
| 254 | case CMD_HIDE_SOFT_INPUT: { |
| 255 | code->env->CallVoidMethod(code->clazz, |
| 256 | gNativeActivityClassInfo.hideIme, work.arg1); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 257 | code->messageQueue->raiseAndClearException(code->env, "hideIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 258 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 259 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 260 | ALOGW("Unknown work command: %d", work.cmd); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 261 | break; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 264 | return 1; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 267 | // ------------------------------------------------------------------------ |
| 268 | |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 269 | static thread_local std::string g_error_msg; |
| 270 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 271 | static jlong |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 272 | loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName, |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 273 | jobject messageQueue, jstring internalDataDir, jstring obbDir, |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 274 | jstring externalDataDir, jint sdkVersion, jobject jAssetMgr, |
Dimitry Ivanov | ea90281 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 275 | jbyteArray savedState, jobject classLoader, jstring libraryPath) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 276 | if (kLogTrace) { |
| 277 | ALOGD("loadNativeCode_native"); |
| 278 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 279 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 280 | ScopedUtfChars pathStr(env, path); |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 281 | std::unique_ptr<NativeCode> code; |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 282 | bool needs_native_bridge = false; |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 283 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 284 | void* handle = OpenNativeLibrary(env, |
| 285 | sdkVersion, |
| 286 | pathStr.c_str(), |
| 287 | classLoader, |
| 288 | libraryPath, |
| 289 | &needs_native_bridge, |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 290 | &g_error_msg); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 291 | |
| 292 | if (handle == nullptr) { |
| 293 | ALOGW("NativeActivity LoadNativeLibrary(\"%s\") failed: %s", |
| 294 | pathStr.c_str(), |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 295 | g_error_msg.c_str()); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 296 | return 0; |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 297 | } |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 298 | |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 299 | 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 Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 305 | } |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 306 | |
| 307 | code.reset(new NativeCode(handle, (ANativeActivity_createFunc*)funcPtr)); |
| 308 | env->ReleaseStringUTFChars(funcName, funcStr); |
| 309 | |
| 310 | if (code->createActivityFunc == NULL) { |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 311 | g_error_msg = needs_native_bridge ? NativeBridgeGetError() : dlerror(); |
| 312 | ALOGW("ANativeActivity_onCreate not found: %s", g_error_msg.c_str()); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | code->messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueue); |
| 317 | if (code->messageQueue == NULL) { |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 318 | g_error_msg = "Unable to retrieve native MessageQueue"; |
| 319 | ALOGW("%s", g_error_msg.c_str()); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | int msgpipe[2]; |
| 324 | if (pipe(msgpipe)) { |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 325 | g_error_msg = android::base::StringPrintf("could not create pipe: %s", strerror(errno)); |
| 326 | ALOGW("%s", g_error_msg.c_str()); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 327 | 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) { |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 342 | g_error_msg = "NativeActivity GetJavaVM failed"; |
| 343 | ALOGW("%s", g_error_msg.c_str()); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 344 | 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 Lesinski | 5c690d5 | 2017-03-17 15:17:12 -0700 | [diff] [blame] | 363 | code->javaAssetManager = env->NewGlobalRef(jAssetMgr); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 364 | code->assetManager = NdkAssetManagerForJavaObject(env, jAssetMgr); |
Dimitry Ivanov | 569834d | 2016-07-06 14:17:52 -0700 | [diff] [blame] | 365 | |
| 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 Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 386 | return (jlong)code.release(); |
| 387 | } |
| 388 | |
| 389 | static jstring getDlError_native(JNIEnv* env, jobject clazz) { |
dimitry | f66a223 | 2017-10-04 19:44:56 +0200 | [diff] [blame] | 390 | jstring result = env->NewStringUTF(g_error_msg.c_str()); |
| 391 | g_error_msg.clear(); |
| 392 | return result; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 396 | unloadNativeCode_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 397 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 398 | if (kLogTrace) { |
| 399 | ALOGD("unloadNativeCode_native"); |
| 400 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 401 | if (handle != 0) { |
| 402 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 403 | delete code; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 408 | onStart_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 409 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 410 | if (kLogTrace) { |
| 411 | ALOGD("onStart_native"); |
| 412 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 413 | if (handle != 0) { |
| 414 | NativeCode* code = (NativeCode*)handle; |
| 415 | if (code->callbacks.onStart != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 416 | code->callbacks.onStart(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 422 | onResume_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 423 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 424 | if (kLogTrace) { |
| 425 | ALOGD("onResume_native"); |
| 426 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 427 | if (handle != 0) { |
| 428 | NativeCode* code = (NativeCode*)handle; |
| 429 | if (code->callbacks.onResume != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 430 | code->callbacks.onResume(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 435 | static jbyteArray |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 436 | onSaveInstanceState_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 437 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 438 | if (kLogTrace) { |
| 439 | ALOGD("onSaveInstanceState_native"); |
| 440 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 441 | |
| 442 | jbyteArray array = NULL; |
| 443 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 444 | if (handle != 0) { |
| 445 | NativeCode* code = (NativeCode*)handle; |
| 446 | if (code->callbacks.onSaveInstanceState != NULL) { |
| 447 | size_t len = 0; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 448 | 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 Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 460 | |
| 461 | return array; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 465 | onPause_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 466 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 467 | if (kLogTrace) { |
| 468 | ALOGD("onPause_native"); |
| 469 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 470 | if (handle != 0) { |
| 471 | NativeCode* code = (NativeCode*)handle; |
| 472 | if (code->callbacks.onPause != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 473 | code->callbacks.onPause(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 479 | onStop_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 480 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 481 | if (kLogTrace) { |
| 482 | ALOGD("onStop_native"); |
| 483 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 484 | if (handle != 0) { |
| 485 | NativeCode* code = (NativeCode*)handle; |
| 486 | if (code->callbacks.onStop != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 487 | code->callbacks.onStop(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 493 | onConfigurationChanged_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 494 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 495 | if (kLogTrace) { |
| 496 | ALOGD("onConfigurationChanged_native"); |
| 497 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 498 | if (handle != 0) { |
| 499 | NativeCode* code = (NativeCode*)handle; |
| 500 | if (code->callbacks.onConfigurationChanged != NULL) { |
| 501 | code->callbacks.onConfigurationChanged(code); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 507 | onLowMemory_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 508 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 509 | if (kLogTrace) { |
| 510 | ALOGD("onLowMemory_native"); |
| 511 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 512 | if (handle != 0) { |
| 513 | NativeCode* code = (NativeCode*)handle; |
| 514 | if (code->callbacks.onLowMemory != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 515 | code->callbacks.onLowMemory(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 521 | onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jlong handle, jboolean focused) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 522 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 523 | if (kLogTrace) { |
| 524 | ALOGD("onWindowFocusChanged_native"); |
| 525 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 526 | if (handle != 0) { |
| 527 | NativeCode* code = (NativeCode*)handle; |
| 528 | if (code->callbacks.onWindowFocusChanged != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 529 | code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 534 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 535 | onSurfaceCreated_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 536 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 537 | if (kLogTrace) { |
| 538 | ALOGD("onSurfaceCreated_native"); |
| 539 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 540 | if (handle != 0) { |
| 541 | NativeCode* code = (NativeCode*)handle; |
| 542 | code->setSurface(surface); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 543 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 544 | code->callbacks.onNativeWindowCreated(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 545 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 550 | static 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 Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 556 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 557 | onSurfaceChanged_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface, |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 558 | jint format, jint width, jint height) |
| 559 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 560 | if (kLogTrace) { |
| 561 | ALOGD("onSurfaceChanged_native"); |
| 562 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 563 | if (handle != 0) { |
| 564 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 565 | sp<ANativeWindow> oldNativeWindow = code->nativeWindow; |
| 566 | code->setSurface(surface); |
| 567 | if (oldNativeWindow != code->nativeWindow) { |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 568 | if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 569 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 570 | oldNativeWindow.get()); |
| 571 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 572 | 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 Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 581 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 582 | } 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 | |
| 599 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 600 | onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 601 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 602 | if (kLogTrace) { |
| 603 | ALOGD("onSurfaceRedrawNeeded_native"); |
| 604 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 605 | 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 Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 614 | onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 615 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 616 | if (kLogTrace) { |
| 617 | ALOGD("onSurfaceDestroyed_native"); |
| 618 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 619 | if (handle != 0) { |
| 620 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 621 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 622 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 623 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 624 | } |
| 625 | code->setSurface(NULL); |
| 626 | } |
| 627 | } |
| 628 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 629 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 630 | onInputQueueCreated_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 631 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 632 | if (kLogTrace) { |
| 633 | ALOGD("onInputChannelCreated_native"); |
| 634 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 635 | if (handle != 0) { |
| 636 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 637 | if (code->callbacks.onInputQueueCreated != NULL) { |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 638 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 639 | code->callbacks.onInputQueueCreated(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 645 | onInputQueueDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 646 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 647 | if (kLogTrace) { |
| 648 | ALOGD("onInputChannelDestroyed_native"); |
| 649 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 650 | if (handle != 0) { |
| 651 | NativeCode* code = (NativeCode*)handle; |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 652 | if (code->callbacks.onInputQueueDestroyed != NULL) { |
| 653 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 654 | code->callbacks.onInputQueueDestroyed(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 655 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 659 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 660 | onContentRectChanged_native(JNIEnv* env, jobject clazz, jlong handle, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 661 | jint x, jint y, jint w, jint h) |
| 662 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 663 | if (kLogTrace) { |
| 664 | ALOGD("onContentRectChanged_native"); |
| 665 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 666 | 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 Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 679 | static const JNINativeMethod g_methods[] = { |
Dmitriy Ivanov | 6f06ad7 | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 680 | { "loadNativeCode", |
Dimitry Ivanov | ea90281 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 681 | "(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] | 682 | (void*)loadNativeCode_native }, |
| 683 | { "getDlError", "()Ljava/lang/String;", (void*) getDlError_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 684 | { "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 Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 698 | (void*)onInputQueueCreated_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 699 | { "onInputQueueDestroyedNative", "(JJ)V", |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 700 | (void*)onInputQueueDestroyed_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 701 | { "onContentRectChangedNative", "(JIIII)V", (void*)onContentRectChanged_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | static const char* const kNativeActivityPathName = "android/app/NativeActivity"; |
| 705 | |
| 706 | int register_android_app_NativeActivity(JNIEnv* env) |
| 707 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 708 | //ALOGD("register_android_app_NativeActivity"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 709 | jclass clazz = FindClassOrDie(env, kNativeActivityPathName); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 710 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 711 | 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 Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 718 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 719 | return RegisterMethodsOrDie(env, kNativeActivityPathName, g_methods, NELEM(g_methods)); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 722 | } // namespace android |