169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn/*
269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * Copyright (C) 2010 The Android Open Source Project
369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn *
469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * you may not use this file except in compliance with the License.
669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * You may obtain a copy of the License at
769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn *
869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn *
1069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * Unless required by applicable law or agreed to in writing, software
1169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
1269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * See the License for the specific language governing permissions and
1469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn * limitations under the License.
1569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn */
1669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
1769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn#define LOG_TAG "NativeActivity"
1869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn#include <utils/Log.h>
1969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
203c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include <poll.h>
213c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include <dlfcn.h>
22d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn#include <fcntl.h>
233c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
24289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn#include <android_runtime/android_app_NativeActivity.h>
256cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate#include <android_runtime/android_util_AssetManager.h>
268335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <android_runtime/android_view_Surface.h>
278335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <android_runtime/AndroidRuntime.h>
289d3b1a424c5c61e24e9659d15fb353026a00d925Jeff Brown#include <input/InputTransport.h>
298335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian
308335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/Surface.h>
318335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian
328335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <system/window.h>
338335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian
344fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown#include <utils/Looper.h>
3569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
363c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "JNIHelp.h"
373c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_os_MessageQueue.h"
383c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_view_InputChannel.h"
393c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_view_KeyEvent.h"
4069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
41abd76d0a091e283117b8aeb642833b8869ea81bfYong WU#include "nativebridge/native_bridge.h"
42abd76d0a091e283117b8aeb642833b8869ea81bfYong WU
43db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn#define LOG_TRACE(...)
4428d9f024e043817212b15f04128d0464330502eaSteve Block//#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
45d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
4669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornnamespace android
4769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
4869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
493c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackbornstatic struct {
50db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    jmethodID finish;
5154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    jmethodID setWindowFlags;
5254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    jmethodID setWindowFormat;
53d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    jmethodID showIme;
54d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    jmethodID hideIme;
553c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn} gNativeActivityClassInfo;
563c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
5754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
5854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
59289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstruct ActivityWork {
60289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t cmd;
61289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t arg1;
62289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t arg2;
63289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn};
64289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
65289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornenum {
66a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    CMD_FINISH = 1,
67289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    CMD_SET_WINDOW_FORMAT,
68289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    CMD_SET_WINDOW_FLAGS,
69d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    CMD_SHOW_SOFT_INPUT,
70d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    CMD_HIDE_SOFT_INPUT,
71289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn};
72289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
73289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstatic void write_work(int fd, int32_t cmd, int32_t arg1=0, int32_t arg2=0) {
74289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    ActivityWork work;
75289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    work.cmd = cmd;
76289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    work.arg1 = arg1;
77289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    work.arg2 = arg2;
78289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
79d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("write_work: cmd=%d", cmd);
80d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
81289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornrestart:
82289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int res = write(fd, &work, sizeof(work));
83289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (res < 0 && errno == EINTR) {
84289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        goto restart;
85289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    }
86289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
87289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (res == sizeof(work)) return;
88289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
898564c8da817a845353d213acd8636b76f567b234Steve Block    if (res < 0) ALOGW("Failed writing to work fd: %s", strerror(errno));
908564c8da817a845353d213acd8636b76f567b234Steve Block    else ALOGW("Truncated writing to work fd: %d", res);
91289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
92289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
93289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstatic bool read_work(int fd, ActivityWork* outWork) {
94289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int res = read(fd, outWork, sizeof(ActivityWork));
95289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    // no need to worry about EINTR, poll loop will just come back again.
96289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (res == sizeof(ActivityWork)) return true;
97289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
988564c8da817a845353d213acd8636b76f567b234Steve Block    if (res < 0) ALOGW("Failed reading work fd: %s", strerror(errno));
998564c8da817a845353d213acd8636b76f567b234Steve Block    else ALOGW("Truncated reading work fd: %d", res);
100289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    return false;
101289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
102289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
10354a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn/*
10454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn * Native state for interacting with the NativeActivity class.
10554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn */
106289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstruct NativeCode : public ANativeActivity {
1072e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) {
108d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        memset((ANativeActivity*)this, 0, sizeof(ANativeActivity));
109d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        memset(&callbacks, 0, sizeof(callbacks));
11074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        dlhandle = _dlhandle;
11174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        createActivityFunc = _createFunc;
11254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        nativeWindow = NULL;
1133c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        mainWorkRead = mainWorkWrite = -1;
11474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
11574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
11674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    ~NativeCode() {
117289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (callbacks.onDestroy != NULL) {
118289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            callbacks.onDestroy(this);
119289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        }
120289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (env != NULL && clazz != NULL) {
121289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            env->DeleteGlobalRef(clazz);
1223c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
123603b44589682db3ff33ade172facb0c5e309f1beJeff Brown        if (messageQueue != NULL && mainWorkRead >= 0) {
124603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            messageQueue->getLooper()->removeFd(mainWorkRead);
1253c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
126a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        setSurface(NULL);
1273c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (mainWorkRead >= 0) close(mainWorkRead);
1283c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (mainWorkWrite >= 0) close(mainWorkWrite);
12974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        if (dlhandle != NULL) {
13054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // for now don't unload...  we probably should clean this
13154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // up and only keep one open dlhandle per proc, since there
13254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // is really no benefit to unloading the code.
13354a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            //dlclose(dlhandle);
13474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
13574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
13674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
13774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    void setSurface(jobject _surface) {
13874323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        if (_surface != NULL) {
13964a55af0ac700baecb0877235eb42caac59a3560Jeff Brown            nativeWindow = android_view_Surface_getNativeWindow(env, _surface);
14074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        } else {
14154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            nativeWindow = NULL;
14274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
14374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
14474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
1452e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    ANativeActivityCallbacks callbacks;
14669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
14769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    void* dlhandle;
1482e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    ANativeActivity_createFunc* createActivityFunc;
14974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
150805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn    String8 internalDataPathObj;
151805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn    String8 externalDataPathObj;
152805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn    String8 obbPathObj;
153682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
15454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    sp<ANativeWindow> nativeWindow;
155d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int32_t lastWindowWidth;
156d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int32_t lastWindowHeight;
157d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
1583c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    // These are used to wake up the main thread to process work.
1593c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    int mainWorkRead;
1603c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    int mainWorkWrite;
161603b44589682db3ff33ade172facb0c5e309f1beJeff Brown    sp<MessageQueue> messageQueue;
16269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn};
16369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
164db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackbornvoid android_NativeActivity_finish(ANativeActivity* activity) {
165db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
166db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    write_work(code->mainWorkWrite, CMD_FINISH, 0);
167db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn}
168db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn
169289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornvoid android_NativeActivity_setWindowFormat(
170289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        ANativeActivity* activity, int32_t format) {
171289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
172289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format);
173289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
174289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
175289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornvoid android_NativeActivity_setWindowFlags(
176289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        ANativeActivity* activity, int32_t values, int32_t mask) {
177289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
178289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask);
179289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
180289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
181d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid android_NativeActivity_showSoftInput(
182d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        ANativeActivity* activity, int32_t flags) {
183d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
184d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags);
185d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
186d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
187d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid android_NativeActivity_hideSoftInput(
188d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        ANativeActivity* activity, int32_t flags) {
189d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
190d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags);
191d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
192d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
19354a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
19454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
19554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn/*
19654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn * Callback for handling native events on the application's main thread.
19754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn */
1984fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brownstatic int mainWorkCallback(int fd, int events, void* data) {
1993c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    NativeCode* code = (NativeCode*)data;
200289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if ((events & POLLIN) == 0) {
2014fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        return 1;
202289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    }
203289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
204289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    ActivityWork work;
205289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (!read_work(code->mainWorkRead, &work)) {
2064fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        return 1;
207289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    }
208d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
209d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("mainWorkCallback: cmd=%d", work.cmd);
210d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
211289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    switch (work.cmd) {
212db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn        case CMD_FINISH: {
213db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish);
214603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            code->messageQueue->raiseAndClearException(code->env, "finish");
215db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn        } break;
216289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        case CMD_SET_WINDOW_FORMAT: {
217289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->env->CallVoidMethod(code->clazz,
218289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                    gNativeActivityClassInfo.setWindowFormat, work.arg1);
219603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            code->messageQueue->raiseAndClearException(code->env, "setWindowFormat");
220289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        } break;
221289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        case CMD_SET_WINDOW_FLAGS: {
222289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->env->CallVoidMethod(code->clazz,
223289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                    gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2);
224603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            code->messageQueue->raiseAndClearException(code->env, "setWindowFlags");
225289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        } break;
226d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        case CMD_SHOW_SOFT_INPUT: {
227d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->env->CallVoidMethod(code->clazz,
228d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    gNativeActivityClassInfo.showIme, work.arg1);
229603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            code->messageQueue->raiseAndClearException(code->env, "showIme");
230d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } break;
231d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        case CMD_HIDE_SOFT_INPUT: {
232d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->env->CallVoidMethod(code->clazz,
233d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    gNativeActivityClassInfo.hideIme, work.arg1);
234603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            code->messageQueue->raiseAndClearException(code->env, "hideIme");
235d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } break;
236289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        default:
2378564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Unknown work command: %d", work.cmd);
238289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            break;
2393c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    }
2403c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
2414fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    return 1;
2423c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn}
2433c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
24454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
24554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
24658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhatstatic jlong
247e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne HackbornloadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName,
248805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        jobject messageQueue, jstring internalDataDir, jstring obbDir,
24958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat        jstring externalDataDir, jint sdkVersion,
25008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jobject jAssetMgr, jbyteArray savedState)
25169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
252d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("loadNativeCode_native");
253d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
25469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    const char* pathStr = env->GetStringUTFChars(path, NULL);
25569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    NativeCode* code = NULL;
256abd76d0a091e283117b8aeb642833b8869ea81bfYong WU    bool needNativeBridge = false;
257abd76d0a091e283117b8aeb642833b8869ea81bfYong WU
25869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    void* handle = dlopen(pathStr, RTLD_LAZY);
259abd76d0a091e283117b8aeb642833b8869ea81bfYong WU    if (handle == NULL) {
260abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        if (NativeBridgeIsSupported(pathStr)) {
261abd76d0a091e283117b8aeb642833b8869ea81bfYong WU            handle = NativeBridgeLoadLibrary(pathStr, RTLD_LAZY);
262abd76d0a091e283117b8aeb642833b8869ea81bfYong WU            needNativeBridge = true;
263abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        }
264abd76d0a091e283117b8aeb642833b8869ea81bfYong WU    }
26569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    env->ReleaseStringUTFChars(path, pathStr);
266abd76d0a091e283117b8aeb642833b8869ea81bfYong WU
26769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != NULL) {
268abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        void* funcPtr = NULL;
269e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn        const char* funcStr = env->GetStringUTFChars(funcName, NULL);
270abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        if (needNativeBridge) {
271abd76d0a091e283117b8aeb642833b8869ea81bfYong WU            funcPtr = NativeBridgeGetTrampoline(handle, funcStr, NULL, 0);
272abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        } else {
273abd76d0a091e283117b8aeb642833b8869ea81bfYong WU            funcPtr = dlsym(handle, funcStr);
274abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        }
275abd76d0a091e283117b8aeb642833b8869ea81bfYong WU
276abd76d0a091e283117b8aeb642833b8869ea81bfYong WU        code = new NativeCode(handle, (ANativeActivity_createFunc*)funcPtr);
277e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn        env->ReleaseStringUTFChars(funcName, funcStr);
278abd76d0a091e283117b8aeb642833b8869ea81bfYong WU
27969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->createActivityFunc == NULL) {
2808564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("ANativeActivity_onCreate not found");
28169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            delete code;
28269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            return 0;
28369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
2843c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
285603b44589682db3ff33ade172facb0c5e309f1beJeff Brown        code->messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueue);
286603b44589682db3ff33ade172facb0c5e309f1beJeff Brown        if (code->messageQueue == NULL) {
287603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            ALOGW("Unable to retrieve native MessageQueue");
2883c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            delete code;
2893c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            return 0;
2903c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
2913c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
2923c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        int msgpipe[2];
2933c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (pipe(msgpipe)) {
2948564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("could not create pipe: %s", strerror(errno));
2953c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            delete code;
2963c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            return 0;
2973c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
2983c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        code->mainWorkRead = msgpipe[0];
2993c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        code->mainWorkWrite = msgpipe[1];
300d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK);
301d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make main work read pipe "
302d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
303d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK);
304d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make main work write pipe "
305d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
306603b44589682db3ff33ade172facb0c5e309f1beJeff Brown        code->messageQueue->getLooper()->addFd(
307603b44589682db3ff33ade172facb0c5e309f1beJeff Brown                code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code);
3083c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
309289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->ANativeActivity::callbacks = &code->callbacks;
310289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (env->GetJavaVM(&code->vm) < 0) {
3118564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("NativeActivity GetJavaVM failed");
3122e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            delete code;
3132e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            return 0;
3142e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn        }
315289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->env = env;
316289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->clazz = env->NewGlobalRef(clazz);
3176cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
318682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL);
319805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        code->internalDataPathObj = dirStr;
320805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        code->internalDataPath = code->internalDataPathObj.string();
321805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        env->ReleaseStringUTFChars(internalDataDir, dirStr);
322682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
323e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey        if (externalDataDir != NULL) {
324e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            dirStr = env->GetStringUTFChars(externalDataDir, NULL);
325e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            code->externalDataPathObj = dirStr;
326e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            env->ReleaseStringUTFChars(externalDataDir, dirStr);
327e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey        }
328805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        code->externalDataPath = code->externalDataPathObj.string();
3296cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
330289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->sdkVersion = sdkVersion;
331682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
3326cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate        code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
3336cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
334e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey        if (obbDir != NULL) {
335e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            dirStr = env->GetStringUTFChars(obbDir, NULL);
336e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            code->obbPathObj = dirStr;
337e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey            env->ReleaseStringUTFChars(obbDir, dirStr);
338e0475c828773aae42bf6bf54422df996d6b0fcefJeff Sharkey        }
339805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn        code->obbPath = code->obbPathObj.string();
340805fd7ee0e5dc2939e85c84f78d9890a51982bc0Dianne Hackborn
34108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jbyte* rawSavedState = NULL;
34208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jsize rawSavedSize = 0;
34308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (savedState != NULL) {
34408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            rawSavedState = env->GetByteArrayElements(savedState, NULL);
34508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            rawSavedSize = env->GetArrayLength(savedState);
34608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
34708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
34808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        code->createActivityFunc(code, rawSavedState, rawSavedSize);
34908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
35008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (rawSavedState != NULL) {
35108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            env->ReleaseByteArrayElements(savedState, rawSavedState, 0);
35208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
35369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
35469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
35558b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    return (jlong)code;
35669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
35769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
35869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
35958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatunloadNativeCode_native(JNIEnv* env, jobject clazz, jlong handle)
36069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
361d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("unloadNativeCode_native");
36269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
36369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
36469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        delete code;
36569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
36669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
36769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
36869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
36958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonStart_native(JNIEnv* env, jobject clazz, jlong handle)
37069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
371d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onStart_native");
37269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
37369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
37469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onStart != NULL) {
375289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onStart(code);
37669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
37769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
37869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
37969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
38069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
38158b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonResume_native(JNIEnv* env, jobject clazz, jlong handle)
38269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
383d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onResume_native");
38469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
38569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
38669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onResume != NULL) {
387289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onResume(code);
38869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
38969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
39069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
39169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
39208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackbornstatic jbyteArray
39358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonSaveInstanceState_native(JNIEnv* env, jobject clazz, jlong handle)
39469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
395d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSaveInstanceState_native");
39608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
39708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    jbyteArray array = NULL;
39808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
39969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
40069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
40169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onSaveInstanceState != NULL) {
40269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            size_t len = 0;
40308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len);
40408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            if (len > 0) {
40508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                array = env->NewByteArray(len);
40608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                if (array != NULL) {
40708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                    env->SetByteArrayRegion(array, 0, len, state);
40808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                }
40908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            }
41008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            if (state != NULL) {
41108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                free(state);
41208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            }
41369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
41469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
41508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
41608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    return array;
41769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
41869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
41969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
42058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonPause_native(JNIEnv* env, jobject clazz, jlong handle)
42169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
422d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onPause_native");
42369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
42469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
42569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onPause != NULL) {
426289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onPause(code);
42769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
42869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
42969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
43069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
43169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
43258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonStop_native(JNIEnv* env, jobject clazz, jlong handle)
43369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
434d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onStop_native");
43569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
43669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
43769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onStop != NULL) {
438289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onStop(code);
43969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
44069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
44169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
44269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
44369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
44458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonConfigurationChanged_native(JNIEnv* env, jobject clazz, jlong handle)
44508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn{
44608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    LOG_TRACE("onConfigurationChanged_native");
44708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    if (handle != 0) {
44808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
44908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (code->callbacks.onConfigurationChanged != NULL) {
45008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            code->callbacks.onConfigurationChanged(code);
45108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
45208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    }
45308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn}
45408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
45508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackbornstatic void
45658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonLowMemory_native(JNIEnv* env, jobject clazz, jlong handle)
45769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
458d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onLowMemory_native");
45969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
46069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
46169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onLowMemory != NULL) {
462289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onLowMemory(code);
46369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
46469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
46569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
46669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
46769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
46858b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonWindowFocusChanged_native(JNIEnv* env, jobject clazz, jlong handle, jboolean focused)
46969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
470d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onWindowFocusChanged_native");
47169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
47269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
47369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onWindowFocusChanged != NULL) {
474289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0);
47569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
47669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
47769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
47869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
47974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
48058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonSurfaceCreated_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface)
48174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
482d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceCreated_native");
48374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
48474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
48574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        code->setSurface(surface);
48654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) {
487289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onNativeWindowCreated(code,
48854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn                    code->nativeWindow.get());
48974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
49074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
49174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
49274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
493d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic int32_t getWindowProp(ANativeWindow* window, int what) {
494d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int value;
495d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int res = window->query(window, what, &value);
496d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    return res < 0 ? res : value;
497d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
498d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
49974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
50058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonSurfaceChanged_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface,
50174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        jint format, jint width, jint height)
50274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
503d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceChanged_native");
50474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
50574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
50654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        sp<ANativeWindow> oldNativeWindow = code->nativeWindow;
50754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        code->setSurface(surface);
50854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (oldNativeWindow != code->nativeWindow) {
5098ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn            if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
510289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                code->callbacks.onNativeWindowDestroyed(code,
5118ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn                        oldNativeWindow.get());
5128ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn            }
513d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            if (code->nativeWindow != NULL) {
514d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                if (code->callbacks.onNativeWindowCreated != NULL) {
515d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    code->callbacks.onNativeWindowCreated(code,
516d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                            code->nativeWindow.get());
517d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                }
518d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                code->lastWindowWidth = getWindowProp(code->nativeWindow.get(),
519d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                        NATIVE_WINDOW_WIDTH);
520d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                code->lastWindowHeight = getWindowProp(code->nativeWindow.get(),
521d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                        NATIVE_WINDOW_HEIGHT);
522d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            }
523d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } else {
524d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            // Maybe it resized?
525d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            int32_t newWidth = getWindowProp(code->nativeWindow.get(),
526d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    NATIVE_WINDOW_WIDTH);
527d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            int32_t newHeight = getWindowProp(code->nativeWindow.get(),
528d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    NATIVE_WINDOW_HEIGHT);
529d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            if (newWidth != code->lastWindowWidth
530d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    || newHeight != code->lastWindowHeight) {
531d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                if (code->callbacks.onNativeWindowResized != NULL) {
532d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    code->callbacks.onNativeWindowResized(code,
533d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                            code->nativeWindow.get());
534d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                }
53554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            }
53674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
53774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
53874323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
53974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
54074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
54158b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jlong handle)
542d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn{
543d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceRedrawNeeded_native");
544d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (handle != 0) {
545d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
546d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) {
547d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get());
548d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
549d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
550d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
551d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
552d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic void
55358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jobject surface)
55474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
555d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceDestroyed_native");
55674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
55774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
55854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
559289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onNativeWindowDestroyed(code,
56054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn                    code->nativeWindow.get());
56174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
56274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        code->setSurface(NULL);
56374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
56474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
56574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
566a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackbornstatic void
56758b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonInputQueueCreated_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr)
568a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn{
569d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onInputChannelCreated_native");
570a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    if (handle != 0) {
571a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
572a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (code->callbacks.onInputQueueCreated != NULL) {
573a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright            AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr);
574a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright            code->callbacks.onInputQueueCreated(code, queue);
575a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
576a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    }
577a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn}
578a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn
579a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackbornstatic void
58058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonInputQueueDestroyed_native(JNIEnv* env, jobject clazz, jlong handle, jlong queuePtr)
581a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn{
582d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onInputChannelDestroyed_native");
583a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    if (handle != 0) {
584a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
585a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright        if (code->callbacks.onInputQueueDestroyed != NULL) {
586a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright            AInputQueue* queue = reinterpret_cast<AInputQueue*>(queuePtr);
587a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright            code->callbacks.onInputQueueDestroyed(code, queue);
588a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
589a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    }
590a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn}
591a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn
592d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic void
59358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatonContentRectChanged_native(JNIEnv* env, jobject clazz, jlong handle,
594d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        jint x, jint y, jint w, jint h)
595d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn{
596d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onContentRectChanged_native");
597d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (handle != 0) {
598d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
599d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (code->callbacks.onContentRectChanged != NULL) {
600d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            ARect rect;
601d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.left = x;
602d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.top = y;
603d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.right = x+w;
604d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.bottom = y+h;
605d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->callbacks.onContentRectChanged(code, &rect);
606d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
607d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
608d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
609d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
61069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic const JNINativeMethod g_methods[] = {
61158b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "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",
612682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn            (void*)loadNativeCode_native },
61358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "unloadNativeCode", "(J)V", (void*)unloadNativeCode_native },
61458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onStartNative", "(J)V", (void*)onStart_native },
61558b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onResumeNative", "(J)V", (void*)onResume_native },
61658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onSaveInstanceStateNative", "(J)[B", (void*)onSaveInstanceState_native },
61758b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onPauseNative", "(J)V", (void*)onPause_native },
61858b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onStopNative", "(J)V", (void*)onStop_native },
61958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onConfigurationChangedNative", "(J)V", (void*)onConfigurationChanged_native },
62058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onLowMemoryNative", "(J)V", (void*)onLowMemory_native },
62158b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onWindowFocusChangedNative", "(JZ)V", (void*)onWindowFocusChanged_native },
62258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onSurfaceCreatedNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceCreated_native },
62358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onSurfaceChangedNative", "(JLandroid/view/Surface;III)V", (void*)onSurfaceChanged_native },
62458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onSurfaceRedrawNeededNative", "(JLandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native },
62558b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onSurfaceDestroyedNative", "(J)V", (void*)onSurfaceDestroyed_native },
62658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onInputQueueCreatedNative", "(JJ)V",
627a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright        (void*)onInputQueueCreated_native },
62858b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onInputQueueDestroyedNative", "(JJ)V",
629a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright        (void*)onInputQueueDestroyed_native },
63058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "onContentRectChangedNative", "(JIIII)V", (void*)onContentRectChanged_native },
63169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn};
63269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
63369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic const char* const kNativeActivityPathName = "android/app/NativeActivity";
63469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
6353c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#define FIND_CLASS(var, className) \
6363c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        var = env->FindClass(className); \
63717cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro        LOG_FATAL_IF(! var, "Unable to find class %s", className);
6383c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
6393c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
6403c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
6413c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        LOG_FATAL_IF(! var, "Unable to find method" methodName);
6423c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
64369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornint register_android_app_NativeActivity(JNIEnv* env)
64469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
6455baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    //ALOGD("register_android_app_NativeActivity");
64617cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    jclass clazz;
64717cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    FIND_CLASS(clazz, kNativeActivityPathName);
64869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
649db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.finish,
65017cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro            clazz,
651db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            "finish", "()V");
65254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFlags,
65317cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro            clazz,
65454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            "setWindowFlags", "(II)V");
65554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFormat,
65617cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro            clazz,
65754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            "setWindowFormat", "(I)V");
658d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.showIme,
65917cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro            clazz,
660d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            "showIme", "(I)V");
661d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.hideIme,
66217cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro            clazz,
663d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            "hideIme", "(I)V");
66454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
66569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    return AndroidRuntime::registerNativeMethods(
66669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        env, kNativeActivityPathName,
66769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        g_methods, NELEM(g_methods));
66869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
66969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
670a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn} // namespace android
671