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, |
| 262 | jbyteArray savedState, jobject classLoader, jstring libraryPath, |
| 263 | jstring isolationPath) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 264 | if (kLogTrace) { |
| 265 | ALOGD("loadNativeCode_native"); |
| 266 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 267 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 268 | const char* pathStr = env->GetStringUTFChars(path, NULL); |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 269 | std::unique_ptr<NativeCode> code; |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 270 | bool needNativeBridge = false; |
| 271 | |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 272 | void* handle = OpenNativeLibrary(env, sdkVersion, pathStr, classLoader, |
Dimitry Ivanov | 75b10ec | 2015-12-11 17:34:21 -0800 | [diff] [blame] | 273 | false, libraryPath, isolationPath); |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 274 | if (handle == NULL) { |
| 275 | if (NativeBridgeIsSupported(pathStr)) { |
| 276 | handle = NativeBridgeLoadLibrary(pathStr, RTLD_LAZY); |
| 277 | needNativeBridge = true; |
| 278 | } |
| 279 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 280 | env->ReleaseStringUTFChars(path, pathStr); |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 281 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 282 | if (handle != NULL) { |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 283 | void* funcPtr = NULL; |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 284 | const char* funcStr = env->GetStringUTFChars(funcName, NULL); |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 285 | if (needNativeBridge) { |
| 286 | funcPtr = NativeBridgeGetTrampoline(handle, funcStr, NULL, 0); |
| 287 | } else { |
| 288 | funcPtr = dlsym(handle, funcStr); |
| 289 | } |
| 290 | |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 291 | code.reset(new NativeCode(handle, (ANativeActivity_createFunc*)funcPtr)); |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 292 | env->ReleaseStringUTFChars(funcName, funcStr); |
Yong WU | 03c8660 | 2014-07-28 21:20:18 +0800 | [diff] [blame] | 293 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 294 | if (code->createActivityFunc == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 295 | ALOGW("ANativeActivity_onCreate not found"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 298 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 299 | code->messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueue); |
| 300 | if (code->messageQueue == NULL) { |
| 301 | ALOGW("Unable to retrieve native MessageQueue"); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int msgpipe[2]; |
| 306 | if (pipe(msgpipe)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 307 | ALOGW("could not create pipe: %s", strerror(errno)); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | code->mainWorkRead = msgpipe[0]; |
| 311 | code->mainWorkWrite = msgpipe[1]; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 312 | int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK); |
| 313 | SLOGW_IF(result != 0, "Could not make main work read pipe " |
| 314 | "non-blocking: %s", strerror(errno)); |
| 315 | result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK); |
| 316 | SLOGW_IF(result != 0, "Could not make main work write pipe " |
| 317 | "non-blocking: %s", strerror(errno)); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 318 | code->messageQueue->getLooper()->addFd( |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 319 | code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code.get()); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 320 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 321 | code->ANativeActivity::callbacks = &code->callbacks; |
| 322 | if (env->GetJavaVM(&code->vm) < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 323 | ALOGW("NativeActivity GetJavaVM failed"); |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 324 | return 0; |
| 325 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 326 | code->env = env; |
| 327 | code->clazz = env->NewGlobalRef(clazz); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 328 | |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 329 | const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 330 | code->internalDataPathObj = dirStr; |
| 331 | code->internalDataPath = code->internalDataPathObj.string(); |
| 332 | env->ReleaseStringUTFChars(internalDataDir, dirStr); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 333 | |
Jeff Sharkey | e0475c8 | 2013-08-15 11:50:02 -0700 | [diff] [blame] | 334 | if (externalDataDir != NULL) { |
| 335 | dirStr = env->GetStringUTFChars(externalDataDir, NULL); |
| 336 | code->externalDataPathObj = dirStr; |
| 337 | env->ReleaseStringUTFChars(externalDataDir, dirStr); |
| 338 | } |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 339 | code->externalDataPath = code->externalDataPathObj.string(); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 340 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 341 | code->sdkVersion = sdkVersion; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 342 | |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 343 | code->assetManager = assetManagerForJavaObject(env, jAssetMgr); |
| 344 | |
Jeff Sharkey | e0475c8 | 2013-08-15 11:50:02 -0700 | [diff] [blame] | 345 | if (obbDir != NULL) { |
| 346 | dirStr = env->GetStringUTFChars(obbDir, NULL); |
| 347 | code->obbPathObj = dirStr; |
| 348 | env->ReleaseStringUTFChars(obbDir, dirStr); |
| 349 | } |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 350 | code->obbPath = code->obbPathObj.string(); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 351 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 352 | jbyte* rawSavedState = NULL; |
| 353 | jsize rawSavedSize = 0; |
| 354 | if (savedState != NULL) { |
| 355 | rawSavedState = env->GetByteArrayElements(savedState, NULL); |
| 356 | rawSavedSize = env->GetArrayLength(savedState); |
| 357 | } |
| 358 | |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 359 | code->createActivityFunc(code.get(), rawSavedState, rawSavedSize); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 360 | |
| 361 | if (rawSavedState != NULL) { |
| 362 | env->ReleaseByteArrayElements(savedState, rawSavedState, 0); |
| 363 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 366 | return (jlong)code.release(); |
| 367 | } |
| 368 | |
| 369 | static jstring getDlError_native(JNIEnv* env, jobject clazz) { |
| 370 | return env->NewStringUTF(dlerror()); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 374 | unloadNativeCode_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 375 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 376 | if (kLogTrace) { |
| 377 | ALOGD("unloadNativeCode_native"); |
| 378 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 379 | if (handle != 0) { |
| 380 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 381 | delete code; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 386 | onStart_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 387 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 388 | if (kLogTrace) { |
| 389 | ALOGD("onStart_native"); |
| 390 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 391 | if (handle != 0) { |
| 392 | NativeCode* code = (NativeCode*)handle; |
| 393 | if (code->callbacks.onStart != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 394 | code->callbacks.onStart(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 400 | onResume_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 401 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 402 | if (kLogTrace) { |
| 403 | ALOGD("onResume_native"); |
| 404 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 405 | if (handle != 0) { |
| 406 | NativeCode* code = (NativeCode*)handle; |
| 407 | if (code->callbacks.onResume != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 408 | code->callbacks.onResume(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 413 | static jbyteArray |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 414 | onSaveInstanceState_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 415 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 416 | if (kLogTrace) { |
| 417 | ALOGD("onSaveInstanceState_native"); |
| 418 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 419 | |
| 420 | jbyteArray array = NULL; |
| 421 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 422 | if (handle != 0) { |
| 423 | NativeCode* code = (NativeCode*)handle; |
| 424 | if (code->callbacks.onSaveInstanceState != NULL) { |
| 425 | size_t len = 0; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 426 | jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len); |
| 427 | if (len > 0) { |
| 428 | array = env->NewByteArray(len); |
| 429 | if (array != NULL) { |
| 430 | env->SetByteArrayRegion(array, 0, len, state); |
| 431 | } |
| 432 | } |
| 433 | if (state != NULL) { |
| 434 | free(state); |
| 435 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 438 | |
| 439 | return array; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 443 | onPause_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 444 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 445 | if (kLogTrace) { |
| 446 | ALOGD("onPause_native"); |
| 447 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 448 | if (handle != 0) { |
| 449 | NativeCode* code = (NativeCode*)handle; |
| 450 | if (code->callbacks.onPause != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 451 | code->callbacks.onPause(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 457 | onStop_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 458 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 459 | if (kLogTrace) { |
| 460 | ALOGD("onStop_native"); |
| 461 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 462 | if (handle != 0) { |
| 463 | NativeCode* code = (NativeCode*)handle; |
| 464 | if (code->callbacks.onStop != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 465 | code->callbacks.onStop(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 471 | onConfigurationChanged_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 472 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 473 | if (kLogTrace) { |
| 474 | ALOGD("onConfigurationChanged_native"); |
| 475 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 476 | if (handle != 0) { |
| 477 | NativeCode* code = (NativeCode*)handle; |
| 478 | if (code->callbacks.onConfigurationChanged != NULL) { |
| 479 | code->callbacks.onConfigurationChanged(code); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 485 | onLowMemory_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 486 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 487 | if (kLogTrace) { |
| 488 | ALOGD("onLowMemory_native"); |
| 489 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 490 | if (handle != 0) { |
| 491 | NativeCode* code = (NativeCode*)handle; |
| 492 | if (code->callbacks.onLowMemory != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 493 | code->callbacks.onLowMemory(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 499 | onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jlong handle, jboolean focused) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 500 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 501 | if (kLogTrace) { |
| 502 | ALOGD("onWindowFocusChanged_native"); |
| 503 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 504 | if (handle != 0) { |
| 505 | NativeCode* code = (NativeCode*)handle; |
| 506 | if (code->callbacks.onWindowFocusChanged != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 507 | code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 512 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 513 | onSurfaceCreated_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 514 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 515 | if (kLogTrace) { |
| 516 | ALOGD("onSurfaceCreated_native"); |
| 517 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 518 | if (handle != 0) { |
| 519 | NativeCode* code = (NativeCode*)handle; |
| 520 | code->setSurface(surface); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 521 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 522 | code->callbacks.onNativeWindowCreated(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 523 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 528 | static int32_t getWindowProp(ANativeWindow* window, int what) { |
| 529 | int value; |
| 530 | int res = window->query(window, what, &value); |
| 531 | return res < 0 ? res : value; |
| 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 | onSurfaceChanged_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface, |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 536 | jint format, jint width, jint height) |
| 537 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 538 | if (kLogTrace) { |
| 539 | ALOGD("onSurfaceChanged_native"); |
| 540 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 541 | if (handle != 0) { |
| 542 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 543 | sp<ANativeWindow> oldNativeWindow = code->nativeWindow; |
| 544 | code->setSurface(surface); |
| 545 | if (oldNativeWindow != code->nativeWindow) { |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 546 | if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 547 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 548 | oldNativeWindow.get()); |
| 549 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 550 | if (code->nativeWindow != NULL) { |
| 551 | if (code->callbacks.onNativeWindowCreated != NULL) { |
| 552 | code->callbacks.onNativeWindowCreated(code, |
| 553 | code->nativeWindow.get()); |
| 554 | } |
| 555 | code->lastWindowWidth = getWindowProp(code->nativeWindow.get(), |
| 556 | NATIVE_WINDOW_WIDTH); |
| 557 | code->lastWindowHeight = getWindowProp(code->nativeWindow.get(), |
| 558 | NATIVE_WINDOW_HEIGHT); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 559 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 560 | } else { |
| 561 | // Maybe it resized? |
| 562 | int32_t newWidth = getWindowProp(code->nativeWindow.get(), |
| 563 | NATIVE_WINDOW_WIDTH); |
| 564 | int32_t newHeight = getWindowProp(code->nativeWindow.get(), |
| 565 | NATIVE_WINDOW_HEIGHT); |
| 566 | if (newWidth != code->lastWindowWidth |
| 567 | || newHeight != code->lastWindowHeight) { |
| 568 | if (code->callbacks.onNativeWindowResized != NULL) { |
| 569 | code->callbacks.onNativeWindowResized(code, |
| 570 | code->nativeWindow.get()); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 578 | onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jlong handle) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 579 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 580 | if (kLogTrace) { |
| 581 | ALOGD("onSurfaceRedrawNeeded_native"); |
| 582 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 583 | if (handle != 0) { |
| 584 | NativeCode* code = (NativeCode*)handle; |
| 585 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) { |
| 586 | code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 592 | onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface) |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 593 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 594 | if (kLogTrace) { |
| 595 | ALOGD("onSurfaceDestroyed_native"); |
| 596 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 597 | if (handle != 0) { |
| 598 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 599 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 600 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 601 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 602 | } |
| 603 | code->setSurface(NULL); |
| 604 | } |
| 605 | } |
| 606 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 607 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 608 | onInputQueueCreated_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 609 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 610 | if (kLogTrace) { |
| 611 | ALOGD("onInputChannelCreated_native"); |
| 612 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 613 | if (handle != 0) { |
| 614 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 615 | if (code->callbacks.onInputQueueCreated != NULL) { |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 616 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 617 | code->callbacks.onInputQueueCreated(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 623 | onInputQueueDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr) |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 624 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 625 | if (kLogTrace) { |
| 626 | ALOGD("onInputChannelDestroyed_native"); |
| 627 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 628 | if (handle != 0) { |
| 629 | NativeCode* code = (NativeCode*)handle; |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 630 | if (code->callbacks.onInputQueueDestroyed != NULL) { |
| 631 | AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr); |
| 632 | code->callbacks.onInputQueueDestroyed(code, queue); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 633 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 637 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 638 | onContentRectChanged_native(JNIEnv* env, jobject clazz, jlong handle, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 639 | jint x, jint y, jint w, jint h) |
| 640 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 641 | if (kLogTrace) { |
| 642 | ALOGD("onContentRectChanged_native"); |
| 643 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 644 | if (handle != 0) { |
| 645 | NativeCode* code = (NativeCode*)handle; |
| 646 | if (code->callbacks.onContentRectChanged != NULL) { |
| 647 | ARect rect; |
| 648 | rect.left = x; |
| 649 | rect.top = y; |
| 650 | rect.right = x+w; |
| 651 | rect.bottom = y+h; |
| 652 | code->callbacks.onContentRectChanged(code, &rect); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 657 | static const JNINativeMethod g_methods[] = { |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 658 | { "loadNativeCode", |
| 659 | "(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;Ljava/lang/String;)J", |
| 660 | (void*)loadNativeCode_native }, |
| 661 | { "getDlError", "()Ljava/lang/String;", (void*) getDlError_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 662 | { "unloadNativeCode", "(J)V", (void*)unloadNativeCode_native }, |
| 663 | { "onStartNative", "(J)V", (void*)onStart_native }, |
| 664 | { "onResumeNative", "(J)V", (void*)onResume_native }, |
| 665 | { "onSaveInstanceStateNative", "(J)[B", (void*)onSaveInstanceState_native }, |
| 666 | { "onPauseNative", "(J)V", (void*)onPause_native }, |
| 667 | { "onStopNative", "(J)V", (void*)onStop_native }, |
| 668 | { "onConfigurationChangedNative", "(J)V", (void*)onConfigurationChanged_native }, |
| 669 | { "onLowMemoryNative", "(J)V", (void*)onLowMemory_native }, |
| 670 | { "onWindowFocusChangedNative", "(JZ)V", (void*)onWindowFocusChanged_native }, |
| 671 | { "onSurfaceCreatedNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceCreated_native }, |
| 672 | { "onSurfaceChangedNative", "(JLandroid/view/Surface;III)V", (void*)onSurfaceChanged_native }, |
| 673 | { "onSurfaceRedrawNeededNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native }, |
| 674 | { "onSurfaceDestroyedNative", "(J)V", (void*)onSurfaceDestroyed_native }, |
| 675 | { "onInputQueueCreatedNative", "(JJ)V", |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 676 | (void*)onInputQueueCreated_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 677 | { "onInputQueueDestroyedNative", "(JJ)V", |
Michael Wright | a44dd26 | 2013-04-10 21:12:00 -0700 | [diff] [blame] | 678 | (void*)onInputQueueDestroyed_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 679 | { "onContentRectChangedNative", "(JIIII)V", (void*)onContentRectChanged_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | static const char* const kNativeActivityPathName = "android/app/NativeActivity"; |
| 683 | |
| 684 | int register_android_app_NativeActivity(JNIEnv* env) |
| 685 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 686 | //ALOGD("register_android_app_NativeActivity"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 687 | jclass clazz = FindClassOrDie(env, kNativeActivityPathName); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 688 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 689 | gNativeActivityClassInfo.finish = GetMethodIDOrDie(env, clazz, "finish", "()V"); |
| 690 | gNativeActivityClassInfo.setWindowFlags = GetMethodIDOrDie(env, clazz, "setWindowFlags", |
| 691 | "(II)V"); |
| 692 | gNativeActivityClassInfo.setWindowFormat = GetMethodIDOrDie(env, clazz, "setWindowFormat", |
| 693 | "(I)V"); |
| 694 | gNativeActivityClassInfo.showIme = GetMethodIDOrDie(env, clazz, "showIme", "(I)V"); |
| 695 | gNativeActivityClassInfo.hideIme = GetMethodIDOrDie(env, clazz, "hideIme", "(I)V"); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 696 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 697 | return RegisterMethodsOrDie(env, kNativeActivityPathName, g_methods, NELEM(g_methods)); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 700 | } // namespace android |