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