android_app_NativeActivity.cpp revision e21d91c694e828e6285cc50e79b2a6be6e8c692b
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
2469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn#include <android_runtime/AndroidRuntime.h>
25289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn#include <android_runtime/android_view_Surface.h>
26289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn#include <android_runtime/android_app_NativeActivity.h>
276cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate#include <android_runtime/android_util_AssetManager.h>
2854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn#include <surfaceflinger/Surface.h>
2954a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn#include <ui/egl/android_natives.h>
30a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn#include <ui/InputTransport.h>
314fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown#include <utils/Looper.h>
3269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
333c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "JNIHelp.h"
343c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_os_MessageQueue.h"
353c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_view_InputChannel.h"
363c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#include "android_view_KeyEvent.h"
3769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
38db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn#define LOG_TRACE(...)
39db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn//#define LOG_TRACE(...) LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
40d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
4169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornnamespace android
4269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
4369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
443c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackbornstatic struct {
453c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    jclass clazz;
463c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
473c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    jmethodID dispatchUnhandledKeyEvent;
482c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    jmethodID preDispatchKeyEvent;
49db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    jmethodID finish;
5054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    jmethodID setWindowFlags;
5154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    jmethodID setWindowFormat;
52d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    jmethodID showIme;
53d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    jmethodID hideIme;
543c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn} gNativeActivityClassInfo;
553c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
5654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
5754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
58289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstruct ActivityWork {
59289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t cmd;
60289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t arg1;
61289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    int32_t arg2;
62289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn};
63289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
64289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornenum {
65289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    CMD_DEF_KEY = 1,
66db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    CMD_FINISH,
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
89289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (res < 0) LOGW("Failed writing to work fd: %s", strerror(errno));
90289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    else LOGW("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
98289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (res < 0) LOGW("Failed reading work fd: %s", strerror(errno));
99289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    else LOGW("Truncated reading work fd: %d", res);
100289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    return false;
101289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
102289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
103289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn// ------------------------------------------------------------------------
104289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
105d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn} // namespace android
106d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
107d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornusing namespace android;
108d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
109d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackbornAInputQueue::AInputQueue(const sp<InputChannel>& channel, int workWrite) :
1102c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        mWorkWrite(workWrite), mConsumer(channel), mSeq(0) {
111d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int msgpipe[2];
112d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (pipe(msgpipe)) {
113d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        LOGW("could not create pipe: %s", strerror(errno));
114d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        mDispatchKeyRead = mDispatchKeyWrite = -1;
115d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    } else {
116d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        mDispatchKeyRead = msgpipe[0];
117d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        mDispatchKeyWrite = msgpipe[1];
118d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        int result = fcntl(mDispatchKeyRead, F_SETFL, O_NONBLOCK);
119d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make AInputQueue read pipe "
120d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
121d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        result = fcntl(mDispatchKeyWrite, F_SETFL, O_NONBLOCK);
122d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make AInputQueue write pipe "
123d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
1243c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    }
125d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
126d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
127d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackbornAInputQueue::~AInputQueue() {
128d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    close(mDispatchKeyRead);
129d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    close(mDispatchKeyWrite);
130d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
131d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
13242c03e579aade011b451e2a13ea3f44a2ef0056aDianne Hackbornvoid AInputQueue::attachLooper(ALooper* looper, int ident,
1334fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        ALooper_callbackFunc callback, void* data) {
1344fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    mLooper = static_cast<android::Looper*>(looper);
1354fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    mLooper->addFd(mConsumer.getChannel()->getReceivePipeFd(),
1364fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown            ident, ALOOPER_EVENT_INPUT, callback, data);
1374fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    mLooper->addFd(mDispatchKeyRead,
1384fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown            ident, ALOOPER_EVENT_INPUT, callback, data);
139d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
140d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
141d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid AInputQueue::detachLooper() {
1424fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    mLooper->removeFd(mConsumer.getChannel()->getReceivePipeFd());
1434fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    mLooper->removeFd(mDispatchKeyRead);
144d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
145d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
146d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornint32_t AInputQueue::hasEvents() {
147d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    struct pollfd pfd[2];
148d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
149d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[0].fd = mConsumer.getChannel()->getReceivePipeFd();
150d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[0].events = POLLIN;
151d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[0].revents = 0;
152d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[1].fd = mDispatchKeyRead;
153d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[0].events = POLLIN;
154d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    pfd[0].revents = 0;
1553c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
156d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int nfd = poll(pfd, 2, 0);
157d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (nfd <= 0) return 0;
158d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    return (pfd[0].revents == POLLIN || pfd[1].revents == POLLIN) ? 1 : -1;
159d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
160d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
161d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornint32_t AInputQueue::getEvent(AInputEvent** outEvent) {
162d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    *outEvent = NULL;
163d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
1642c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    bool finishNow = false;
1652c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
166d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    char byteread;
167d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    ssize_t nRead = read(mDispatchKeyRead, &byteread, 1);
168d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (nRead == 1) {
1693c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        mLock.lock();
170d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (mDispatchingKeys.size() > 0) {
171d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            KeyEvent* kevent = mDispatchingKeys[0];
172d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            *outEvent = kevent;
173d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            mDispatchingKeys.removeAt(0);
1742c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            in_flight_event inflight;
1752c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            inflight.event = kevent;
1762c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            inflight.seq = -1;
1772c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            inflight.doFinish = false;
1782c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mInFlightEvents.push(inflight);
1792c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        }
1802c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        if (mFinishPreDispatches.size() > 0) {
1812c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            finish_pre_dispatch finish(mFinishPreDispatches[0]);
1822c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mFinishPreDispatches.removeAt(0);
1832c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            const size_t N = mInFlightEvents.size();
1842c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            for (size_t i=0; i<N; i++) {
1852c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                const in_flight_event& inflight(mInFlightEvents[i]);
1862c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                if (inflight.seq == finish.seq) {
1872c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                    *outEvent = inflight.event;
1882c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                    finishNow = finish.handled;
1892c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                }
1902c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
1912c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (*outEvent == NULL) {
1922c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                LOGW("getEvent couldn't find inflight for seq %d", finish.seq);
1932c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
1943c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
1953c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        mLock.unlock();
1962c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
1972c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        if (finishNow) {
1982c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            finishEvent(*outEvent, true);
1992c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            *outEvent = NULL;
2002c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            return -1;
2012c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        } else if (*outEvent != NULL) {
202d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            return 0;
203d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
2043c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    }
2053c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
206d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int32_t res = mConsumer.receiveDispatchSignal();
207d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (res != android::OK) {
208d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        LOGE("channel '%s' ~ Failed to receive dispatch signal.  status=%d",
209d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                mConsumer.getChannel()->getName().string(), res);
210d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        return -1;
211d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
212d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
213d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    InputEvent* myEvent = NULL;
2142c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    res = mConsumer.consume(this, &myEvent);
215d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (res != android::OK) {
216d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        LOGW("channel '%s' ~ Failed to consume input event.  status=%d",
217d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                mConsumer.getChannel()->getName().string(), res);
218d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        mConsumer.sendFinishedSignal();
219d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        return -1;
220d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
221d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
2222c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    in_flight_event inflight;
2232c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    inflight.event = myEvent;
2242c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    inflight.seq = -1;
2252c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    inflight.doFinish = true;
2262c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mInFlightEvents.push(inflight);
2272c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
228d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    *outEvent = myEvent;
229d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    return 0;
230d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
231d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
2322c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornbool AInputQueue::preDispatchEvent(AInputEvent* event) {
2332c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (((InputEvent*)event)->getType() != AINPUT_EVENT_TYPE_KEY) {
2342c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        // The IME only cares about key events.
2352c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        return false;
2362c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
2372c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
2382c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    // For now we only send system keys to the IME...  this avoids having
2392c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    // critical keys like DPAD go through this path.  We really need to have
2402c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    // the IME report which keys it wants.
2412c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (!((KeyEvent*)event)->isSystemKey()) {
2422c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        return false;
2432c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
2442c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
2452c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    return preDispatchKey((KeyEvent*)event);
2462c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
2472c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
248d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid AInputQueue::finishEvent(AInputEvent* event, bool handled) {
2492c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("finishEvent: %p handled=%d", event, handled ? 1 : 0);
250d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
251c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown    if (!handled && ((InputEvent*)event)->getType() == AINPUT_EVENT_TYPE_KEY
252d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            && ((KeyEvent*)event)->hasDefaultAction()) {
253d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        // The app didn't handle this, but it may have a default action
254d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        // associated with it.  We need to hand this back to Java to be
255d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        // executed.
2562c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        doUnhandledKey((KeyEvent*)event);
2572c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        return;
258d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
259d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
2602c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
2612c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    const size_t N = mInFlightEvents.size();
262d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    for (size_t i=0; i<N; i++) {
2632c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        const in_flight_event& inflight(mInFlightEvents[i]);
2642c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        if (inflight.event == event) {
2652c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (inflight.doFinish) {
2662c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                int32_t res = mConsumer.sendFinishedSignal();
2672c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                if (res != android::OK) {
2682c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                    LOGW("Failed to send finished signal on channel '%s'.  status=%d",
2692c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                            mConsumer.getChannel()->getName().string(), res);
2702c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                }
2712c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
2722c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (static_cast<InputEvent*>(event)->getType() == AINPUT_EVENT_TYPE_KEY) {
2732c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                mAvailKeyEvents.push(static_cast<KeyEvent*>(event));
2742c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            } else {
2752c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                mAvailMotionEvents.push(static_cast<MotionEvent*>(event));
2762c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
2772c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mInFlightEvents.removeAt(i);
2782c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mLock.unlock();
2792c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            return;
2803c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
2813c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    }
2822c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.unlock();
2833c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
2842c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOGW("finishEvent called for unknown event: %p", event);
285d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
286d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
287d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid AInputQueue::dispatchEvent(android::KeyEvent* event) {
288d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.lock();
289d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("dispatchEvent: dispatching=%d write=%d\n", mDispatchingKeys.size(),
290d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            mDispatchKeyWrite);
291d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mDispatchingKeys.add(event);
2922c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    wakeupDispatch();
293d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.unlock();
2942c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
295d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
2962c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornvoid AInputQueue::finishPreDispatch(int seq, bool handled) {
2972c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
2982c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("finishPreDispatch: seq=%d handled=%d\n", seq, handled ? 1 : 0);
2992c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    finish_pre_dispatch finish;
3002c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    finish.seq = seq;
3012c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    finish.handled = handled;
3022c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mFinishPreDispatches.add(finish);
3032c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    wakeupDispatch();
3042c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.unlock();
305d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
306d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
307d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackbornKeyEvent* AInputQueue::consumeUnhandledEvent() {
308d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    KeyEvent* event = NULL;
309d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
310d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.lock();
3112c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (mUnhandledKeys.size() > 0) {
3122c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = mUnhandledKeys[0];
3132c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        mUnhandledKeys.removeAt(0);
314d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
315d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.unlock();
316d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
317d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("consumeUnhandledEvent: KeyEvent=%p", event);
318d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
319d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    return event;
320d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
321d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
3222c6081ce3593712f30dacd990a97209c791d6cedDianne HackbornKeyEvent* AInputQueue::consumePreDispatchingEvent(int* outSeq) {
3232c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    KeyEvent* event = NULL;
3242c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
325d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.lock();
3262c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (mPreDispatchingKeys.size() > 0) {
3272c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        const in_flight_event& inflight(mPreDispatchingKeys[0]);
3282c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = static_cast<KeyEvent*>(inflight.event);
3292c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        *outSeq = inflight.seq;
3302c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        mPreDispatchingKeys.removeAt(0);
3312c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
3322c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.unlock();
3332c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3342c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("consumePreDispatchingEvent: KeyEvent=%p", event);
3352c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3362c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    return event;
3372c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
3382c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3392c6081ce3593712f30dacd990a97209c791d6cedDianne HackbornKeyEvent* AInputQueue::createKeyEvent() {
3402c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
3412c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    KeyEvent* event;
3422c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (mAvailKeyEvents.size() <= 0) {
3432c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = new KeyEvent();
3442c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    } else {
3452c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = mAvailKeyEvents.top();
3462c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        mAvailKeyEvents.pop();
3472c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
3482c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.unlock();
3492c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    return event;
3502c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
3512c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3522c6081ce3593712f30dacd990a97209c791d6cedDianne HackbornMotionEvent* AInputQueue::createMotionEvent() {
3532c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
3542c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    MotionEvent* event;
3552c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (mAvailMotionEvents.size() <= 0) {
3562c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = new MotionEvent();
3572c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    } else {
3582c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        event = mAvailMotionEvents.top();
3592c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        mAvailMotionEvents.pop();
3602c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
3612c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.unlock();
3622c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    return event;
3632c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
3642c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3652c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornvoid AInputQueue::doUnhandledKey(KeyEvent* keyEvent) {
3662c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
3672c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("Unhandled key: pending=%d write=%d\n", mUnhandledKeys.size(), mWorkWrite);
3682c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (mUnhandledKeys.size() <= 0 && mWorkWrite >= 0) {
369d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        write_work(mWorkWrite, CMD_DEF_KEY);
370d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
3712c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mUnhandledKeys.add(keyEvent);
372d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    mLock.unlock();
373d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
374d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
3752c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornbool AInputQueue::preDispatchKey(KeyEvent* keyEvent) {
3762c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    mLock.lock();
3772c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("preDispatch key: pending=%d write=%d\n", mPreDispatchingKeys.size(), mWorkWrite);
3782c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    const size_t N = mInFlightEvents.size();
3792c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    for (size_t i=0; i<N; i++) {
3802c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        in_flight_event& inflight(mInFlightEvents.editItemAt(i));
3812c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        if (inflight.event == keyEvent) {
3822c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (inflight.seq >= 0) {
3832c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                // This event has already been pre-dispatched!
3842c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                LOG_TRACE("Event already pre-dispatched!");
3852c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                mLock.unlock();
3862c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                return false;
3872c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
3882c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mSeq++;
3892c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (mSeq < 0) mSeq = 1;
3902c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            inflight.seq = mSeq;
3912c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
3922c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            if (mPreDispatchingKeys.size() <= 0 && mWorkWrite >= 0) {
3932c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                write_work(mWorkWrite, CMD_DEF_KEY);
3942c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
3952c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mPreDispatchingKeys.add(inflight);
3962c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            mLock.unlock();
3972c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            return true;
3982c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        }
3992c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
4002c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
4012c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOGW("preDispatchKey called for unknown event: %p", keyEvent);
4022c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    return false;
4032c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
4042c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
4052c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornvoid AInputQueue::wakeupDispatch() {
4062c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornrestart:
4072c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    char dummy = 0;
4082c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    int res = write(mDispatchKeyWrite, &dummy, sizeof(dummy));
4092c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (res < 0 && errno == EINTR) {
4102c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        goto restart;
4112c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
4122c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
4132c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (res == sizeof(dummy)) return;
4142c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
4152c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (res < 0) LOGW("Failed writing to dispatch fd: %s", strerror(errno));
4162c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    else LOGW("Truncated writing to dispatch fd: %d", res);
4172c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
4182c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
419d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornnamespace android {
4203c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
42154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
42254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
42354a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn/*
42454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn * Native state for interacting with the NativeActivity class.
42554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn */
426289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornstruct NativeCode : public ANativeActivity {
4272e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) {
428d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        memset((ANativeActivity*)this, 0, sizeof(ANativeActivity));
429d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        memset(&callbacks, 0, sizeof(callbacks));
43074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        dlhandle = _dlhandle;
43174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        createActivityFunc = _createFunc;
43254a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        nativeWindow = NULL;
433a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        inputChannel = NULL;
434a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        nativeInputQueue = NULL;
4353c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        mainWorkRead = mainWorkWrite = -1;
43674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
43774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
43874323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    ~NativeCode() {
439289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (callbacks.onDestroy != NULL) {
440289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            callbacks.onDestroy(this);
441289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        }
442289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (env != NULL && clazz != NULL) {
443289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            env->DeleteGlobalRef(clazz);
4443c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
4454fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        if (looper != NULL && mainWorkRead >= 0) {
4464fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown            looper->removeFd(mainWorkRead);
4473c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
4483c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (nativeInputQueue != NULL) {
4493c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            nativeInputQueue->mWorkWrite = -1;
4503c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
451a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        setSurface(NULL);
452a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        setInputChannel(NULL);
4533c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (mainWorkRead >= 0) close(mainWorkRead);
4543c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (mainWorkWrite >= 0) close(mainWorkWrite);
45574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        if (dlhandle != NULL) {
45654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // for now don't unload...  we probably should clean this
45754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // up and only keep one open dlhandle per proc, since there
45854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            // is really no benefit to unloading the code.
45954a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            //dlclose(dlhandle);
46074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
46174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
46274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
46374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    void setSurface(jobject _surface) {
46474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        if (_surface != NULL) {
465289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            nativeWindow = android_Surface_getNativeWindow(env, _surface);
46674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        } else {
46754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            nativeWindow = NULL;
46874323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
46974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
47074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
471a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    status_t setInputChannel(jobject _channel) {
472a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (inputChannel != NULL) {
473a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            delete nativeInputQueue;
474289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            env->DeleteGlobalRef(inputChannel);
475a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
476a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        inputChannel = NULL;
477a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        nativeInputQueue = NULL;
478a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (_channel != NULL) {
479289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            inputChannel = env->NewGlobalRef(_channel);
480a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            sp<InputChannel> ic =
481289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                    android_view_InputChannel_getInputChannel(env, _channel);
482a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            if (ic != NULL) {
483d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                nativeInputQueue = new AInputQueue(ic, mainWorkWrite);
484a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                if (nativeInputQueue->getConsumer().initialize() != android::OK) {
485a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    delete nativeInputQueue;
486a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    nativeInputQueue = NULL;
487a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    return UNKNOWN_ERROR;
488a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                }
489a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            } else {
490a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                return UNKNOWN_ERROR;
491a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            }
492a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
493a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        return OK;
494a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    }
495a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn
4962e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    ANativeActivityCallbacks callbacks;
49769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
49869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    void* dlhandle;
4992e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn    ANativeActivity_createFunc* createActivityFunc;
50074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
501682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn    String8 internalDataPath;
502682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn    String8 externalDataPath;
503682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
50454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    sp<ANativeWindow> nativeWindow;
505d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int32_t lastWindowWidth;
506d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int32_t lastWindowHeight;
507d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
508a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    jobject inputChannel;
509d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    struct AInputQueue* nativeInputQueue;
5103c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
5113c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    // These are used to wake up the main thread to process work.
5123c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    int mainWorkRead;
5133c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    int mainWorkWrite;
5144fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    sp<Looper> looper;
51569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn};
51669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
517db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackbornvoid android_NativeActivity_finish(ANativeActivity* activity) {
518db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
519db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    write_work(code->mainWorkWrite, CMD_FINISH, 0);
520db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn}
521db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn
522289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornvoid android_NativeActivity_setWindowFormat(
523289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        ANativeActivity* activity, int32_t format) {
524289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
525289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format);
526289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
527289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
528289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackbornvoid android_NativeActivity_setWindowFlags(
529289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        ANativeActivity* activity, int32_t values, int32_t mask) {
530289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
531289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask);
532289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn}
533289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
534d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid android_NativeActivity_showSoftInput(
535d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        ANativeActivity* activity, int32_t flags) {
536d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
537d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags);
538d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
539d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
540d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornvoid android_NativeActivity_hideSoftInput(
541d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        ANativeActivity* activity, int32_t flags) {
542d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    NativeCode* code = static_cast<NativeCode*>(activity);
543d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags);
544d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
545d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
54654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
54754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
548db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackbornstatic bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
549db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn   if (env->ExceptionCheck()) {
550db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn       LOGE("An exception was thrown by callback '%s'.", methodName);
551db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn       LOGE_EX(env);
552db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn       env->ExceptionClear();
553db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn       return true;
554db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn   }
555db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn   return false;
556db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn}
557db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn
55854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn/*
55954a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn * Callback for handling native events on the application's main thread.
56054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn */
5614fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brownstatic int mainWorkCallback(int fd, int events, void* data) {
5623c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    NativeCode* code = (NativeCode*)data;
563289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if ((events & POLLIN) == 0) {
5644fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        return 1;
565289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    }
566289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn
567289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    ActivityWork work;
568289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    if (!read_work(code->mainWorkRead, &work)) {
5694fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        return 1;
570289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    }
571d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
572d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("mainWorkCallback: cmd=%d", work.cmd);
573d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
574289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn    switch (work.cmd) {
575289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        case CMD_DEF_KEY: {
576289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            KeyEvent* keyEvent;
577d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            while ((keyEvent=code->nativeInputQueue->consumeUnhandledEvent()) != NULL) {
578289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                jobject inputEventObj = android_view_KeyEvent_fromNative(
579289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                        code->env, keyEvent);
580289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                code->env->CallVoidMethod(code->clazz,
581289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                        gNativeActivityClassInfo.dispatchUnhandledKeyEvent, inputEventObj);
582db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn                checkAndClearExceptionFromCallback(code->env, "dispatchUnhandledKeyEvent");
5832c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                code->nativeInputQueue->finishEvent(keyEvent, true);
5842c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            }
5852c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            int seq;
5862c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            while ((keyEvent=code->nativeInputQueue->consumePreDispatchingEvent(&seq)) != NULL) {
5872c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                jobject inputEventObj = android_view_KeyEvent_fromNative(
5882c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                        code->env, keyEvent);
5892c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                code->env->CallVoidMethod(code->clazz,
5902c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn                        gNativeActivityClassInfo.preDispatchKeyEvent, inputEventObj, seq);
591db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn                checkAndClearExceptionFromCallback(code->env, "preDispatchKeyEvent");
5923c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            }
593289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        } break;
594db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn        case CMD_FINISH: {
595db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish);
596db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            checkAndClearExceptionFromCallback(code->env, "finish");
597db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn        } break;
598289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        case CMD_SET_WINDOW_FORMAT: {
599289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->env->CallVoidMethod(code->clazz,
600289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                    gNativeActivityClassInfo.setWindowFormat, work.arg1);
601db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            checkAndClearExceptionFromCallback(code->env, "setWindowFormat");
602289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        } break;
603289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        case CMD_SET_WINDOW_FLAGS: {
604289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->env->CallVoidMethod(code->clazz,
605289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                    gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2);
606db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            checkAndClearExceptionFromCallback(code->env, "setWindowFlags");
607289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        } break;
608d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        case CMD_SHOW_SOFT_INPUT: {
609d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->env->CallVoidMethod(code->clazz,
610d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    gNativeActivityClassInfo.showIme, work.arg1);
611db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            checkAndClearExceptionFromCallback(code->env, "showIme");
612d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } break;
613d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        case CMD_HIDE_SOFT_INPUT: {
614d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->env->CallVoidMethod(code->clazz,
615d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    gNativeActivityClassInfo.hideIme, work.arg1);
616db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            checkAndClearExceptionFromCallback(code->env, "hideIme");
617d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } break;
618289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        default:
619289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            LOGW("Unknown work command: %d", work.cmd);
620289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            break;
6213c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    }
6223c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
6234fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown    return 1;
6243c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn}
6253c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
62654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn// ------------------------------------------------------------------------
62754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
62869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic jint
629e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne HackbornloadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName,
630e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn        jobject messageQueue,
6316cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate        jstring internalDataDir, jstring externalDataDir, int sdkVersion,
63208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jobject jAssetMgr, jbyteArray savedState)
63369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
634d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("loadNativeCode_native");
635d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
63669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    const char* pathStr = env->GetStringUTFChars(path, NULL);
63769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    NativeCode* code = NULL;
63869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
63969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    void* handle = dlopen(pathStr, RTLD_LAZY);
64069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
64169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    env->ReleaseStringUTFChars(path, pathStr);
64269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
64369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != NULL) {
644e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn        const char* funcStr = env->GetStringUTFChars(funcName, NULL);
6452e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn        code = new NativeCode(handle, (ANativeActivity_createFunc*)
646e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn                dlsym(handle, funcStr));
647e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn        env->ReleaseStringUTFChars(funcName, funcStr);
648e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn
64969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->createActivityFunc == NULL) {
6502e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            LOGW("ANativeActivity_onCreate not found");
65169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            delete code;
65269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            return 0;
65369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
6543c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
6554fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        code->looper = android_os_MessageQueue_getLooper(env, messageQueue);
6564fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        if (code->looper == NULL) {
6574fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown            LOGW("Unable to retrieve MessageQueue's Looper");
6583c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            delete code;
6593c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            return 0;
6603c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
6613c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
6623c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        int msgpipe[2];
6633c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        if (pipe(msgpipe)) {
6643c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            LOGW("could not create pipe: %s", strerror(errno));
6653c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            delete code;
6663c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            return 0;
6673c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        }
6683c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        code->mainWorkRead = msgpipe[0];
6693c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        code->mainWorkWrite = msgpipe[1];
670d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK);
671d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make main work read pipe "
672d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
673d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK);
674d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        SLOGW_IF(result != 0, "Could not make main work write pipe "
675d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                "non-blocking: %s", strerror(errno));
6764fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        code->looper->addFd(code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code);
6773c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
678289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->ANativeActivity::callbacks = &code->callbacks;
679289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        if (env->GetJavaVM(&code->vm) < 0) {
6802e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            LOGW("NativeActivity GetJavaVM failed");
6812e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            delete code;
6822e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn            return 0;
6832e9f93e8db509d5236229dc8540e0904c5dbb9f5Dianne Hackborn        }
684289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->env = env;
685289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->clazz = env->NewGlobalRef(clazz);
6866cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
687682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL);
688682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        code->internalDataPath = dirStr;
689289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->internalDataPath = code->internalDataPath.string();
690682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        env->ReleaseStringUTFChars(path, dirStr);
691682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
692682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        dirStr = env->GetStringUTFChars(externalDataDir, NULL);
693682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        code->externalDataPath = dirStr;
694289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->externalDataPath = code->externalDataPath.string();
695682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn        env->ReleaseStringUTFChars(path, dirStr);
6966cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
697289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn        code->sdkVersion = sdkVersion;
698682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn
6996cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate        code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
7006cce32b6adbb3a9725fc730ba0e0068a74657e60Christopher Tate
70108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jbyte* rawSavedState = NULL;
70208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        jsize rawSavedSize = 0;
70308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (savedState != NULL) {
70408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            rawSavedState = env->GetByteArrayElements(savedState, NULL);
70508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            rawSavedSize = env->GetArrayLength(savedState);
70608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
70708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
70808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        code->createActivityFunc(code, rawSavedState, rawSavedSize);
70908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
71008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (rawSavedState != NULL) {
71108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            env->ReleaseByteArrayElements(savedState, rawSavedState, 0);
71208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
71369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
71469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
71569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    return (jint)code;
71669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
71769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
71869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
71969969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornunloadNativeCode_native(JNIEnv* env, jobject clazz, jint handle)
72069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
721d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("unloadNativeCode_native");
72269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
72369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
72469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        delete code;
72569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
72669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
72769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
72869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
72969969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonStart_native(JNIEnv* env, jobject clazz, jint handle)
73069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
731d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onStart_native");
73269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
73369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
73469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onStart != NULL) {
735289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onStart(code);
73669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
73769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
73869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
73969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
74069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
74169969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonResume_native(JNIEnv* env, jobject clazz, jint handle)
74269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
743d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onResume_native");
74469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
74569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
74669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onResume != NULL) {
747289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onResume(code);
74869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
74969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
75069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
75169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
75208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackbornstatic jbyteArray
75369969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonSaveInstanceState_native(JNIEnv* env, jobject clazz, jint handle)
75469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
755d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSaveInstanceState_native");
75608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
75708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    jbyteArray array = NULL;
75808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
75969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
76069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
76169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onSaveInstanceState != NULL) {
76269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn            size_t len = 0;
76308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len);
76408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            if (len > 0) {
76508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                array = env->NewByteArray(len);
76608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                if (array != NULL) {
76708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                    env->SetByteArrayRegion(array, 0, len, state);
76808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                }
76908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            }
77008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            if (state != NULL) {
77108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn                free(state);
77208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            }
77369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
77469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
77508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
77608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    return array;
77769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
77869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
77969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
78069969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonPause_native(JNIEnv* env, jobject clazz, jint handle)
78169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
782d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onPause_native");
78369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
78469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
78569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onPause != NULL) {
786289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onPause(code);
78769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
78869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
78969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
79069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
79169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
79269969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonStop_native(JNIEnv* env, jobject clazz, jint handle)
79369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
794d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onStop_native");
79569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
79669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
79769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onStop != NULL) {
798289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onStop(code);
79969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
80069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
80169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
80269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
80369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
80408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne HackbornonConfigurationChanged_native(JNIEnv* env, jobject clazz, jint handle)
80508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn{
80608d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    LOG_TRACE("onConfigurationChanged_native");
80708d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    if (handle != 0) {
80808d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
80908d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        if (code->callbacks.onConfigurationChanged != NULL) {
81008d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn            code->callbacks.onConfigurationChanged(code);
81108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn        }
81208d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    }
81308d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn}
81408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn
81508d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackbornstatic void
81669969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonLowMemory_native(JNIEnv* env, jobject clazz, jint handle)
81769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
818d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onLowMemory_native");
81969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
82069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
82169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onLowMemory != NULL) {
822289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onLowMemory(code);
82369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
82469969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
82569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
82669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
82769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic void
82869969e48f2bca9339662dddfacff0bbf6374ed7fDianne HackbornonWindowFocusChanged_native(JNIEnv* env, jobject clazz, jint handle, jboolean focused)
82969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
830d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onWindowFocusChanged_native");
83169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    if (handle != 0) {
83269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        NativeCode* code = (NativeCode*)handle;
83369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        if (code->callbacks.onWindowFocusChanged != NULL) {
834289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0);
83569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        }
83669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    }
83769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
83869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
83974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
84074323fd1ab8eb11beea286d5c213c63e4b803141Dianne HackbornonSurfaceCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject surface)
84174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
842d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceCreated_native");
84374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
84474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
84574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        code->setSurface(surface);
84654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) {
847289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onNativeWindowCreated(code,
84854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn                    code->nativeWindow.get());
84974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
85074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
85174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
85274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
853d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic int32_t getWindowProp(ANativeWindow* window, int what) {
854d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int value;
855d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    int res = window->query(window, what, &value);
856d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    return res < 0 ? res : value;
857d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
858d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
85974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
86074323fd1ab8eb11beea286d5c213c63e4b803141Dianne HackbornonSurfaceChanged_native(JNIEnv* env, jobject clazz, jint handle, jobject surface,
86174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        jint format, jint width, jint height)
86274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
863d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceChanged_native");
86474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
86574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
86654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        sp<ANativeWindow> oldNativeWindow = code->nativeWindow;
86754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        code->setSurface(surface);
86854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (oldNativeWindow != code->nativeWindow) {
8698ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn            if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
870289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn                code->callbacks.onNativeWindowDestroyed(code,
8718ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn                        oldNativeWindow.get());
8728ae5a8e7c04c7b204b739dfcd5da9e2e0f83e1ebDianne Hackborn            }
873d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            if (code->nativeWindow != NULL) {
874d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                if (code->callbacks.onNativeWindowCreated != NULL) {
875d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    code->callbacks.onNativeWindowCreated(code,
876d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                            code->nativeWindow.get());
877d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                }
878d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                code->lastWindowWidth = getWindowProp(code->nativeWindow.get(),
879d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                        NATIVE_WINDOW_WIDTH);
880d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                code->lastWindowHeight = getWindowProp(code->nativeWindow.get(),
881d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                        NATIVE_WINDOW_HEIGHT);
882d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            }
883d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        } else {
884d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            // Maybe it resized?
885d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            int32_t newWidth = getWindowProp(code->nativeWindow.get(),
886d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    NATIVE_WINDOW_WIDTH);
887d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            int32_t newHeight = getWindowProp(code->nativeWindow.get(),
888d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    NATIVE_WINDOW_HEIGHT);
889d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            if (newWidth != code->lastWindowWidth
890d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    || newHeight != code->lastWindowHeight) {
891d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                if (code->callbacks.onNativeWindowResized != NULL) {
892d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                    code->callbacks.onNativeWindowResized(code,
893d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                            code->nativeWindow.get());
894d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn                }
89554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            }
89674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
89774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
89874323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
89974323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
90074323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackbornstatic void
901d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackbornonSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jint handle)
902d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn{
903d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceRedrawNeeded_native");
904d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (handle != 0) {
905d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
906d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) {
907d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get());
908d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
909d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
910d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
911d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
912d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic void
91374323fd1ab8eb11beea286d5c213c63e4b803141Dianne HackbornonSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject surface)
91474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn{
915d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onSurfaceDestroyed_native");
91674323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    if (handle != 0) {
91774323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
91854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
919289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onNativeWindowDestroyed(code,
92054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn                    code->nativeWindow.get());
92174323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        }
92274323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn        code->setSurface(NULL);
92374323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn    }
92474323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn}
92574323fd1ab8eb11beea286d5c213c63e4b803141Dianne Hackborn
926a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackbornstatic void
927a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne HackbornonInputChannelCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject channel)
928a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn{
929d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onInputChannelCreated_native");
930a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    if (handle != 0) {
931a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
932a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        status_t err = code->setInputChannel(channel);
933a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (err != OK) {
934a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            jniThrowException(env, "java/lang/IllegalStateException",
935a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    "Error setting input channel");
936a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn            return;
937a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
938a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (code->callbacks.onInputQueueCreated != NULL) {
939289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onInputQueueCreated(code,
940a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    code->nativeInputQueue);
941a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
942a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    }
943a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn}
944a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn
945a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackbornstatic void
946a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne HackbornonInputChannelDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject channel)
947a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn{
948d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onInputChannelDestroyed_native");
949a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    if (handle != 0) {
950a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
951a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        if (code->nativeInputQueue != NULL
952a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                && code->callbacks.onInputQueueDestroyed != NULL) {
953289b9b62372ef52a06113b83dfb870e2c2fb325aDianne Hackborn            code->callbacks.onInputQueueDestroyed(code,
954a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn                    code->nativeInputQueue);
955a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        }
956a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn        code->setInputChannel(NULL);
957a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    }
958a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn}
959a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn
960d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic void
961d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackbornonContentRectChanged_native(JNIEnv* env, jobject clazz, jint handle,
962d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        jint x, jint y, jint w, jint h)
963d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn{
964d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("onContentRectChanged_native");
965d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (handle != 0) {
966d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
967d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (code->callbacks.onContentRectChanged != NULL) {
968d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            ARect rect;
969d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.left = x;
970d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.top = y;
971d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.right = x+w;
972d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            rect.bottom = y+h;
973d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->callbacks.onContentRectChanged(code, &rect);
974d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
975d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
976d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
977d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
978d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackbornstatic void
979d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne HackborndispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle, jobject eventObj)
980d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn{
981d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    LOG_TRACE("dispatchKeyEvent_native");
982d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    if (handle != 0) {
983d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        NativeCode* code = (NativeCode*)handle;
984d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        if (code->nativeInputQueue != NULL) {
9852c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            KeyEvent* event = code->nativeInputQueue->createKeyEvent();
986c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown            android_view_KeyEvent_toNative(env, eventObj, event);
987d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            code->nativeInputQueue->dispatchEvent(event);
988d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn        }
989d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    }
990d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn}
991d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn
9922c6081ce3593712f30dacd990a97209c791d6cedDianne Hackbornstatic void
9932c6081ce3593712f30dacd990a97209c791d6cedDianne HackbornfinishPreDispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle,
9942c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        jint seq, jboolean handled)
9952c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn{
9962c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    LOG_TRACE("finishPreDispatchKeyEvent_native");
9972c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    if (handle != 0) {
9982c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        NativeCode* code = (NativeCode*)handle;
9992c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        if (code->nativeInputQueue != NULL) {
10002c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            code->nativeInputQueue->finishPreDispatch(seq, handled ? true : false);
10012c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn        }
10022c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    }
10032c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn}
10042c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn
100569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic const JNINativeMethod g_methods[] = {
1006e21d91c694e828e6285cc50e79b2a6be6e8c692bDianne Hackborn    { "loadNativeCode", "(Ljava/lang/String;Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;ILandroid/content/res/AssetManager;[B)I",
1007682674154e3fe88f6061245145f934f25f1a2eb8Dianne Hackborn            (void*)loadNativeCode_native },
100869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "unloadNativeCode", "(I)V", (void*)unloadNativeCode_native },
100969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onStartNative", "(I)V", (void*)onStart_native },
101069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onResumeNative", "(I)V", (void*)onResume_native },
101108d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    { "onSaveInstanceStateNative", "(I)[B", (void*)onSaveInstanceState_native },
101269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onPauseNative", "(I)V", (void*)onPause_native },
101369969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onStopNative", "(I)V", (void*)onStop_native },
101408d5b8fad8d46ccb64db2fdcb4d66972ec87bf48Dianne Hackborn    { "onConfigurationChangedNative", "(I)V", (void*)onConfigurationChanged_native },
101569969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onLowMemoryNative", "(I)V", (void*)onLowMemory_native },
101669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    { "onWindowFocusChangedNative", "(IZ)V", (void*)onWindowFocusChanged_native },
101754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    { "onSurfaceCreatedNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceCreated_native },
101854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    { "onSurfaceChangedNative", "(ILandroid/view/Surface;III)V", (void*)onSurfaceChanged_native },
1019d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    { "onSurfaceRedrawNeededNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native },
102054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    { "onSurfaceDestroyedNative", "(I)V", (void*)onSurfaceDestroyed_native },
1021a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    { "onInputChannelCreatedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelCreated_native },
1022a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn    { "onInputChannelDestroyedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelDestroyed_native },
1023d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    { "onContentRectChangedNative", "(IIIII)V", (void*)onContentRectChanged_native },
1024d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    { "dispatchKeyEventNative", "(ILandroid/view/KeyEvent;)V", (void*)dispatchKeyEvent_native },
10252c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    { "finishPreDispatchKeyEventNative", "(IIZ)V", (void*)finishPreDispatchKeyEvent_native },
102669969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn};
102769969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
102869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornstatic const char* const kNativeActivityPathName = "android/app/NativeActivity";
102969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
10303c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#define FIND_CLASS(var, className) \
10313c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        var = env->FindClass(className); \
10323c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        LOG_FATAL_IF(! var, "Unable to find class " className); \
10333c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        var = jclass(env->NewGlobalRef(var));
10343c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
10353c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
10363c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
10373c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn        LOG_FATAL_IF(! var, "Unable to find method" methodName);
10383c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn
103969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackbornint register_android_app_NativeActivity(JNIEnv* env)
104069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn{
104169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    //LOGD("register_android_app_NativeActivity");
104269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
10433c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    FIND_CLASS(gNativeActivityClassInfo.clazz, kNativeActivityPathName);
104454a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
10453c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.dispatchUnhandledKeyEvent,
10463c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            gNativeActivityClassInfo.clazz,
10473c80a4a044865bdf1289c7896baffa1c082d835cDianne Hackborn            "dispatchUnhandledKeyEvent", "(Landroid/view/KeyEvent;)V");
10482c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.preDispatchKeyEvent,
10492c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            gNativeActivityClassInfo.clazz,
10502c6081ce3593712f30dacd990a97209c791d6cedDianne Hackborn            "preDispatchKeyEvent", "(Landroid/view/KeyEvent;I)V");
105154a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
1052db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.finish,
1053db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            gNativeActivityClassInfo.clazz,
1054db28a94d499f995b467b07cee5c9b9119f538b1cDianne Hackborn            "finish", "()V");
105554a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFlags,
105654a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            gNativeActivityClassInfo.clazz,
105754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            "setWindowFlags", "(II)V");
105854a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFormat,
105954a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            gNativeActivityClassInfo.clazz,
106054a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn            "setWindowFormat", "(I)V");
1061d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.showIme,
1062d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            gNativeActivityClassInfo.clazz,
1063d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            "showIme", "(I)V");
1064d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn    GET_METHOD_ID(gNativeActivityClassInfo.hideIme,
1065d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            gNativeActivityClassInfo.clazz,
1066d76b67c340d1564abf8d14d976fdaf83bf2b3320Dianne Hackborn            "hideIme", "(I)V");
106754a181b1a2b1517a9479b21fbf7705a688232fafDianne Hackborn
106869969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn    return AndroidRuntime::registerNativeMethods(
106969969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        env, kNativeActivityPathName,
107069969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn        g_methods, NELEM(g_methods));
107169969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn}
107269969e48f2bca9339662dddfacff0bbf6374ed7fDianne Hackborn
1073a95e4cb62f3642cb190d032dbf7dc40d9ecc6973Dianne Hackborn} // namespace android
1074