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