android_app_NativeActivity.cpp revision 3915bb845b032dc184dba5e60970b803390ca3ed
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "NativeActivity"
18#include <utils/Log.h>
19
20#include <poll.h>
21#include <dlfcn.h>
22#include <fcntl.h>
23
24#include <android_runtime/AndroidRuntime.h>
25#include <android_runtime/android_view_Surface.h>
26#include <android_runtime/android_app_NativeActivity.h>
27#include <android_runtime/android_util_AssetManager.h>
28#include <surfaceflinger/Surface.h>
29#include <ui/egl/android_natives.h>
30#include <ui/InputTransport.h>
31#include <utils/Looper.h>
32
33#include "JNIHelp.h"
34#include "android_os_MessageQueue.h"
35#include "android_view_InputChannel.h"
36#include "android_view_KeyEvent.h"
37
38#define LOG_TRACE(...)
39//#define LOG_TRACE(...) LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
40
41namespace android
42{
43
44static struct {
45    jclass clazz;
46
47    jmethodID dispatchUnhandledKeyEvent;
48    jmethodID preDispatchKeyEvent;
49    jmethodID finish;
50    jmethodID setWindowFlags;
51    jmethodID setWindowFormat;
52    jmethodID showIme;
53    jmethodID hideIme;
54} gNativeActivityClassInfo;
55
56// ------------------------------------------------------------------------
57
58struct ActivityWork {
59    int32_t cmd;
60    int32_t arg1;
61    int32_t arg2;
62};
63
64enum {
65    CMD_DEF_KEY = 1,
66    CMD_FINISH,
67    CMD_SET_WINDOW_FORMAT,
68    CMD_SET_WINDOW_FLAGS,
69    CMD_SHOW_SOFT_INPUT,
70    CMD_HIDE_SOFT_INPUT,
71};
72
73static void write_work(int fd, int32_t cmd, int32_t arg1=0, int32_t arg2=0) {
74    ActivityWork work;
75    work.cmd = cmd;
76    work.arg1 = arg1;
77    work.arg2 = arg2;
78
79    LOG_TRACE("write_work: cmd=%d", cmd);
80
81restart:
82    int res = write(fd, &work, sizeof(work));
83    if (res < 0 && errno == EINTR) {
84        goto restart;
85    }
86
87    if (res == sizeof(work)) return;
88
89    if (res < 0) LOGW("Failed writing to work fd: %s", strerror(errno));
90    else LOGW("Truncated writing to work fd: %d", res);
91}
92
93static bool read_work(int fd, ActivityWork* outWork) {
94    int res = read(fd, outWork, sizeof(ActivityWork));
95    // no need to worry about EINTR, poll loop will just come back again.
96    if (res == sizeof(ActivityWork)) return true;
97
98    if (res < 0) LOGW("Failed reading work fd: %s", strerror(errno));
99    else LOGW("Truncated reading work fd: %d", res);
100    return false;
101}
102
103// ------------------------------------------------------------------------
104
105} // namespace android
106
107using namespace android;
108
109AInputQueue::AInputQueue(const sp<InputChannel>& channel, int workWrite) :
110        mWorkWrite(workWrite), mConsumer(channel), mSeq(0) {
111    int msgpipe[2];
112    if (pipe(msgpipe)) {
113        LOGW("could not create pipe: %s", strerror(errno));
114        mDispatchKeyRead = mDispatchKeyWrite = -1;
115    } else {
116        mDispatchKeyRead = msgpipe[0];
117        mDispatchKeyWrite = msgpipe[1];
118        int result = fcntl(mDispatchKeyRead, F_SETFL, O_NONBLOCK);
119        SLOGW_IF(result != 0, "Could not make AInputQueue read pipe "
120                "non-blocking: %s", strerror(errno));
121        result = fcntl(mDispatchKeyWrite, F_SETFL, O_NONBLOCK);
122        SLOGW_IF(result != 0, "Could not make AInputQueue write pipe "
123                "non-blocking: %s", strerror(errno));
124    }
125}
126
127AInputQueue::~AInputQueue() {
128    close(mDispatchKeyRead);
129    close(mDispatchKeyWrite);
130}
131
132void AInputQueue::attachLooper(ALooper* looper, int ident,
133        ALooper_callbackFunc callback, void* data) {
134    mLooper = static_cast<android::Looper*>(looper);
135    mLooper->addFd(mConsumer.getChannel()->getReceivePipeFd(),
136            ident, ALOOPER_EVENT_INPUT, callback, data);
137    mLooper->addFd(mDispatchKeyRead,
138            ident, ALOOPER_EVENT_INPUT, callback, data);
139}
140
141void AInputQueue::detachLooper() {
142    mLooper->removeFd(mConsumer.getChannel()->getReceivePipeFd());
143    mLooper->removeFd(mDispatchKeyRead);
144}
145
146int32_t AInputQueue::hasEvents() {
147    struct pollfd pfd[2];
148
149    pfd[0].fd = mConsumer.getChannel()->getReceivePipeFd();
150    pfd[0].events = POLLIN;
151    pfd[0].revents = 0;
152    pfd[1].fd = mDispatchKeyRead;
153    pfd[0].events = POLLIN;
154    pfd[0].revents = 0;
155
156    int nfd = poll(pfd, 2, 0);
157    if (nfd <= 0) return 0;
158    return (pfd[0].revents == POLLIN || pfd[1].revents == POLLIN) ? 1 : -1;
159}
160
161int32_t AInputQueue::getEvent(AInputEvent** outEvent) {
162    *outEvent = NULL;
163
164    bool finishNow = false;
165
166    char byteread;
167    ssize_t nRead = read(mDispatchKeyRead, &byteread, 1);
168    if (nRead == 1) {
169        mLock.lock();
170        if (mDispatchingKeys.size() > 0) {
171            KeyEvent* kevent = mDispatchingKeys[0];
172            *outEvent = kevent;
173            mDispatchingKeys.removeAt(0);
174            in_flight_event inflight;
175            inflight.event = kevent;
176            inflight.seq = -1;
177            inflight.doFinish = false;
178            mInFlightEvents.push(inflight);
179        }
180        if (mFinishPreDispatches.size() > 0) {
181            finish_pre_dispatch finish(mFinishPreDispatches[0]);
182            mFinishPreDispatches.removeAt(0);
183            const size_t N = mInFlightEvents.size();
184            for (size_t i=0; i<N; i++) {
185                const in_flight_event& inflight(mInFlightEvents[i]);
186                if (inflight.seq == finish.seq) {
187                    *outEvent = inflight.event;
188                    finishNow = finish.handled;
189                }
190            }
191            if (*outEvent == NULL) {
192                LOGW("getEvent couldn't find inflight for seq %d", finish.seq);
193            }
194        }
195        mLock.unlock();
196
197        if (finishNow) {
198            finishEvent(*outEvent, true, false);
199            *outEvent = NULL;
200            return -1;
201        } else if (*outEvent != NULL) {
202            return 0;
203        }
204    }
205
206    int32_t res = mConsumer.receiveDispatchSignal();
207    if (res != android::OK) {
208        LOGE("channel '%s' ~ Failed to receive dispatch signal.  status=%d",
209                mConsumer.getChannel()->getName().string(), res);
210        return -1;
211    }
212
213    InputEvent* myEvent = NULL;
214    res = mConsumer.consume(this, &myEvent);
215    if (res != android::OK) {
216        LOGW("channel '%s' ~ Failed to consume input event.  status=%d",
217                mConsumer.getChannel()->getName().string(), res);
218        mConsumer.sendFinishedSignal(false);
219        return -1;
220    }
221
222    in_flight_event inflight;
223    inflight.event = myEvent;
224    inflight.seq = -1;
225    inflight.doFinish = true;
226    mInFlightEvents.push(inflight);
227
228    *outEvent = myEvent;
229    return 0;
230}
231
232bool AInputQueue::preDispatchEvent(AInputEvent* event) {
233    if (((InputEvent*)event)->getType() != AINPUT_EVENT_TYPE_KEY) {
234        // The IME only cares about key events.
235        return false;
236    }
237
238    // For now we only send system keys to the IME...  this avoids having
239    // critical keys like DPAD go through this path.  We really need to have
240    // the IME report which keys it wants.
241    if (!((KeyEvent*)event)->isSystemKey()) {
242        return false;
243    }
244
245    return preDispatchKey((KeyEvent*)event);
246}
247
248void AInputQueue::finishEvent(AInputEvent* event, bool handled, bool didDefaultHandling) {
249    LOG_TRACE("finishEvent: %p handled=%d, didDefaultHandling=%d", event,
250            handled ? 1 : 0, didDefaultHandling ? 1 : 0);
251
252    if (!handled && !didDefaultHandling
253            && ((InputEvent*)event)->getType() == AINPUT_EVENT_TYPE_KEY
254            && ((KeyEvent*)event)->hasDefaultAction()) {
255        // The app didn't handle this, but it may have a default action
256        // associated with it.  We need to hand this back to Java to be
257        // executed.
258        doUnhandledKey((KeyEvent*)event);
259        return;
260    }
261
262    mLock.lock();
263    const size_t N = mInFlightEvents.size();
264    for (size_t i=0; i<N; i++) {
265        const in_flight_event& inflight(mInFlightEvents[i]);
266        if (inflight.event == event) {
267            if (inflight.doFinish) {
268                int32_t res = mConsumer.sendFinishedSignal(handled);
269                if (res != android::OK) {
270                    LOGW("Failed to send finished signal on channel '%s'.  status=%d",
271                            mConsumer.getChannel()->getName().string(), res);
272                }
273            }
274            if (static_cast<InputEvent*>(event)->getType() == AINPUT_EVENT_TYPE_KEY) {
275                mAvailKeyEvents.push(static_cast<KeyEvent*>(event));
276            } else {
277                mAvailMotionEvents.push(static_cast<MotionEvent*>(event));
278            }
279            mInFlightEvents.removeAt(i);
280            mLock.unlock();
281            return;
282        }
283    }
284    mLock.unlock();
285
286    LOGW("finishEvent called for unknown event: %p", event);
287}
288
289void AInputQueue::dispatchEvent(android::KeyEvent* event) {
290    mLock.lock();
291    LOG_TRACE("dispatchEvent: dispatching=%d write=%d\n", mDispatchingKeys.size(),
292            mDispatchKeyWrite);
293    mDispatchingKeys.add(event);
294    wakeupDispatch();
295    mLock.unlock();
296}
297
298void AInputQueue::finishPreDispatch(int seq, bool handled) {
299    mLock.lock();
300    LOG_TRACE("finishPreDispatch: seq=%d handled=%d\n", seq, handled ? 1 : 0);
301    finish_pre_dispatch finish;
302    finish.seq = seq;
303    finish.handled = handled;
304    mFinishPreDispatches.add(finish);
305    wakeupDispatch();
306    mLock.unlock();
307}
308
309KeyEvent* AInputQueue::consumeUnhandledEvent() {
310    KeyEvent* event = NULL;
311
312    mLock.lock();
313    if (mUnhandledKeys.size() > 0) {
314        event = mUnhandledKeys[0];
315        mUnhandledKeys.removeAt(0);
316    }
317    mLock.unlock();
318
319    LOG_TRACE("consumeUnhandledEvent: KeyEvent=%p", event);
320
321    return event;
322}
323
324KeyEvent* AInputQueue::consumePreDispatchingEvent(int* outSeq) {
325    KeyEvent* event = NULL;
326
327    mLock.lock();
328    if (mPreDispatchingKeys.size() > 0) {
329        const in_flight_event& inflight(mPreDispatchingKeys[0]);
330        event = static_cast<KeyEvent*>(inflight.event);
331        *outSeq = inflight.seq;
332        mPreDispatchingKeys.removeAt(0);
333    }
334    mLock.unlock();
335
336    LOG_TRACE("consumePreDispatchingEvent: KeyEvent=%p", event);
337
338    return event;
339}
340
341KeyEvent* AInputQueue::createKeyEvent() {
342    mLock.lock();
343    KeyEvent* event;
344    if (mAvailKeyEvents.size() <= 0) {
345        event = new KeyEvent();
346    } else {
347        event = mAvailKeyEvents.top();
348        mAvailKeyEvents.pop();
349    }
350    mLock.unlock();
351    return event;
352}
353
354MotionEvent* AInputQueue::createMotionEvent() {
355    mLock.lock();
356    MotionEvent* event;
357    if (mAvailMotionEvents.size() <= 0) {
358        event = new MotionEvent();
359    } else {
360        event = mAvailMotionEvents.top();
361        mAvailMotionEvents.pop();
362    }
363    mLock.unlock();
364    return event;
365}
366
367void AInputQueue::doUnhandledKey(KeyEvent* keyEvent) {
368    mLock.lock();
369    LOG_TRACE("Unhandled key: pending=%d write=%d\n", mUnhandledKeys.size(), mWorkWrite);
370    if (mUnhandledKeys.size() <= 0 && mWorkWrite >= 0) {
371        write_work(mWorkWrite, CMD_DEF_KEY);
372    }
373    mUnhandledKeys.add(keyEvent);
374    mLock.unlock();
375}
376
377bool AInputQueue::preDispatchKey(KeyEvent* keyEvent) {
378    mLock.lock();
379    LOG_TRACE("preDispatch key: pending=%d write=%d\n", mPreDispatchingKeys.size(), mWorkWrite);
380    const size_t N = mInFlightEvents.size();
381    for (size_t i=0; i<N; i++) {
382        in_flight_event& inflight(mInFlightEvents.editItemAt(i));
383        if (inflight.event == keyEvent) {
384            if (inflight.seq >= 0) {
385                // This event has already been pre-dispatched!
386                LOG_TRACE("Event already pre-dispatched!");
387                mLock.unlock();
388                return false;
389            }
390            mSeq++;
391            if (mSeq < 0) mSeq = 1;
392            inflight.seq = mSeq;
393
394            if (mPreDispatchingKeys.size() <= 0 && mWorkWrite >= 0) {
395                write_work(mWorkWrite, CMD_DEF_KEY);
396            }
397            mPreDispatchingKeys.add(inflight);
398            mLock.unlock();
399            return true;
400        }
401    }
402
403    LOGW("preDispatchKey called for unknown event: %p", keyEvent);
404    return false;
405}
406
407void AInputQueue::wakeupDispatch() {
408restart:
409    char dummy = 0;
410    int res = write(mDispatchKeyWrite, &dummy, sizeof(dummy));
411    if (res < 0 && errno == EINTR) {
412        goto restart;
413    }
414
415    if (res == sizeof(dummy)) return;
416
417    if (res < 0) LOGW("Failed writing to dispatch fd: %s", strerror(errno));
418    else LOGW("Truncated writing to dispatch fd: %d", res);
419}
420
421namespace android {
422
423// ------------------------------------------------------------------------
424
425/*
426 * Native state for interacting with the NativeActivity class.
427 */
428struct NativeCode : public ANativeActivity {
429    NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) {
430        memset((ANativeActivity*)this, 0, sizeof(ANativeActivity));
431        memset(&callbacks, 0, sizeof(callbacks));
432        dlhandle = _dlhandle;
433        createActivityFunc = _createFunc;
434        nativeWindow = NULL;
435        inputChannel = NULL;
436        nativeInputQueue = NULL;
437        mainWorkRead = mainWorkWrite = -1;
438    }
439
440    ~NativeCode() {
441        if (callbacks.onDestroy != NULL) {
442            callbacks.onDestroy(this);
443        }
444        if (env != NULL && clazz != NULL) {
445            env->DeleteGlobalRef(clazz);
446        }
447        if (looper != NULL && mainWorkRead >= 0) {
448            looper->removeFd(mainWorkRead);
449        }
450        if (nativeInputQueue != NULL) {
451            nativeInputQueue->mWorkWrite = -1;
452        }
453        setSurface(NULL);
454        setInputChannel(NULL);
455        if (mainWorkRead >= 0) close(mainWorkRead);
456        if (mainWorkWrite >= 0) close(mainWorkWrite);
457        if (dlhandle != NULL) {
458            // for now don't unload...  we probably should clean this
459            // up and only keep one open dlhandle per proc, since there
460            // is really no benefit to unloading the code.
461            //dlclose(dlhandle);
462        }
463    }
464
465    void setSurface(jobject _surface) {
466        if (_surface != NULL) {
467            nativeWindow = android_Surface_getNativeWindow(env, _surface);
468        } else {
469            nativeWindow = NULL;
470        }
471    }
472
473    status_t setInputChannel(jobject _channel) {
474        if (inputChannel != NULL) {
475            delete nativeInputQueue;
476            env->DeleteGlobalRef(inputChannel);
477        }
478        inputChannel = NULL;
479        nativeInputQueue = NULL;
480        if (_channel != NULL) {
481            inputChannel = env->NewGlobalRef(_channel);
482            sp<InputChannel> ic =
483                    android_view_InputChannel_getInputChannel(env, _channel);
484            if (ic != NULL) {
485                nativeInputQueue = new AInputQueue(ic, mainWorkWrite);
486                if (nativeInputQueue->getConsumer().initialize() != android::OK) {
487                    delete nativeInputQueue;
488                    nativeInputQueue = NULL;
489                    return UNKNOWN_ERROR;
490                }
491            } else {
492                return UNKNOWN_ERROR;
493            }
494        }
495        return OK;
496    }
497
498    ANativeActivityCallbacks callbacks;
499
500    void* dlhandle;
501    ANativeActivity_createFunc* createActivityFunc;
502
503    String8 internalDataPath;
504    String8 externalDataPath;
505
506    sp<ANativeWindow> nativeWindow;
507    int32_t lastWindowWidth;
508    int32_t lastWindowHeight;
509
510    jobject inputChannel;
511    struct AInputQueue* nativeInputQueue;
512
513    // These are used to wake up the main thread to process work.
514    int mainWorkRead;
515    int mainWorkWrite;
516    sp<Looper> looper;
517};
518
519void android_NativeActivity_finish(ANativeActivity* activity) {
520    NativeCode* code = static_cast<NativeCode*>(activity);
521    write_work(code->mainWorkWrite, CMD_FINISH, 0);
522}
523
524void android_NativeActivity_setWindowFormat(
525        ANativeActivity* activity, int32_t format) {
526    NativeCode* code = static_cast<NativeCode*>(activity);
527    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format);
528}
529
530void android_NativeActivity_setWindowFlags(
531        ANativeActivity* activity, int32_t values, int32_t mask) {
532    NativeCode* code = static_cast<NativeCode*>(activity);
533    write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask);
534}
535
536void android_NativeActivity_showSoftInput(
537        ANativeActivity* activity, int32_t flags) {
538    NativeCode* code = static_cast<NativeCode*>(activity);
539    write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags);
540}
541
542void android_NativeActivity_hideSoftInput(
543        ANativeActivity* activity, int32_t flags) {
544    NativeCode* code = static_cast<NativeCode*>(activity);
545    write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags);
546}
547
548// ------------------------------------------------------------------------
549
550static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
551   if (env->ExceptionCheck()) {
552       LOGE("An exception was thrown by callback '%s'.", methodName);
553       LOGE_EX(env);
554       env->ExceptionClear();
555       return true;
556   }
557   return false;
558}
559
560/*
561 * Callback for handling native events on the application's main thread.
562 */
563static int mainWorkCallback(int fd, int events, void* data) {
564    NativeCode* code = (NativeCode*)data;
565    if ((events & POLLIN) == 0) {
566        return 1;
567    }
568
569    ActivityWork work;
570    if (!read_work(code->mainWorkRead, &work)) {
571        return 1;
572    }
573
574    LOG_TRACE("mainWorkCallback: cmd=%d", work.cmd);
575
576    switch (work.cmd) {
577        case CMD_DEF_KEY: {
578            KeyEvent* keyEvent;
579            while ((keyEvent=code->nativeInputQueue->consumeUnhandledEvent()) != NULL) {
580                jobject inputEventObj = android_view_KeyEvent_fromNative(
581                        code->env, keyEvent);
582                jboolean handled = code->env->CallBooleanMethod(code->clazz,
583                        gNativeActivityClassInfo.dispatchUnhandledKeyEvent, inputEventObj);
584                checkAndClearExceptionFromCallback(code->env, "dispatchUnhandledKeyEvent");
585                code->env->DeleteLocalRef(inputEventObj);
586                code->nativeInputQueue->finishEvent(keyEvent, handled, true);
587            }
588            int seq;
589            while ((keyEvent=code->nativeInputQueue->consumePreDispatchingEvent(&seq)) != NULL) {
590                jobject inputEventObj = android_view_KeyEvent_fromNative(
591                        code->env, keyEvent);
592                code->env->CallVoidMethod(code->clazz,
593                        gNativeActivityClassInfo.preDispatchKeyEvent, inputEventObj, seq);
594                checkAndClearExceptionFromCallback(code->env, "preDispatchKeyEvent");
595                code->env->DeleteLocalRef(inputEventObj);
596            }
597        } break;
598        case CMD_FINISH: {
599            code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish);
600            checkAndClearExceptionFromCallback(code->env, "finish");
601        } break;
602        case CMD_SET_WINDOW_FORMAT: {
603            code->env->CallVoidMethod(code->clazz,
604                    gNativeActivityClassInfo.setWindowFormat, work.arg1);
605            checkAndClearExceptionFromCallback(code->env, "setWindowFormat");
606        } break;
607        case CMD_SET_WINDOW_FLAGS: {
608            code->env->CallVoidMethod(code->clazz,
609                    gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2);
610            checkAndClearExceptionFromCallback(code->env, "setWindowFlags");
611        } break;
612        case CMD_SHOW_SOFT_INPUT: {
613            code->env->CallVoidMethod(code->clazz,
614                    gNativeActivityClassInfo.showIme, work.arg1);
615            checkAndClearExceptionFromCallback(code->env, "showIme");
616        } break;
617        case CMD_HIDE_SOFT_INPUT: {
618            code->env->CallVoidMethod(code->clazz,
619                    gNativeActivityClassInfo.hideIme, work.arg1);
620            checkAndClearExceptionFromCallback(code->env, "hideIme");
621        } break;
622        default:
623            LOGW("Unknown work command: %d", work.cmd);
624            break;
625    }
626
627    return 1;
628}
629
630// ------------------------------------------------------------------------
631
632static jint
633loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName,
634        jobject messageQueue,
635        jstring internalDataDir, jstring externalDataDir, int sdkVersion,
636        jobject jAssetMgr, jbyteArray savedState)
637{
638    LOG_TRACE("loadNativeCode_native");
639
640    const char* pathStr = env->GetStringUTFChars(path, NULL);
641    NativeCode* code = NULL;
642
643    void* handle = dlopen(pathStr, RTLD_LAZY);
644
645    env->ReleaseStringUTFChars(path, pathStr);
646
647    if (handle != NULL) {
648        const char* funcStr = env->GetStringUTFChars(funcName, NULL);
649        code = new NativeCode(handle, (ANativeActivity_createFunc*)
650                dlsym(handle, funcStr));
651        env->ReleaseStringUTFChars(funcName, funcStr);
652
653        if (code->createActivityFunc == NULL) {
654            LOGW("ANativeActivity_onCreate not found");
655            delete code;
656            return 0;
657        }
658
659        code->looper = android_os_MessageQueue_getLooper(env, messageQueue);
660        if (code->looper == NULL) {
661            LOGW("Unable to retrieve MessageQueue's Looper");
662            delete code;
663            return 0;
664        }
665
666        int msgpipe[2];
667        if (pipe(msgpipe)) {
668            LOGW("could not create pipe: %s", strerror(errno));
669            delete code;
670            return 0;
671        }
672        code->mainWorkRead = msgpipe[0];
673        code->mainWorkWrite = msgpipe[1];
674        int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK);
675        SLOGW_IF(result != 0, "Could not make main work read pipe "
676                "non-blocking: %s", strerror(errno));
677        result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK);
678        SLOGW_IF(result != 0, "Could not make main work write pipe "
679                "non-blocking: %s", strerror(errno));
680        code->looper->addFd(code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code);
681
682        code->ANativeActivity::callbacks = &code->callbacks;
683        if (env->GetJavaVM(&code->vm) < 0) {
684            LOGW("NativeActivity GetJavaVM failed");
685            delete code;
686            return 0;
687        }
688        code->env = env;
689        code->clazz = env->NewGlobalRef(clazz);
690
691        const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL);
692        code->internalDataPath = dirStr;
693        code->internalDataPath = code->internalDataPath.string();
694        env->ReleaseStringUTFChars(path, dirStr);
695
696        dirStr = env->GetStringUTFChars(externalDataDir, NULL);
697        code->externalDataPath = dirStr;
698        code->externalDataPath = code->externalDataPath.string();
699        env->ReleaseStringUTFChars(path, dirStr);
700
701        code->sdkVersion = sdkVersion;
702
703        code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
704
705        jbyte* rawSavedState = NULL;
706        jsize rawSavedSize = 0;
707        if (savedState != NULL) {
708            rawSavedState = env->GetByteArrayElements(savedState, NULL);
709            rawSavedSize = env->GetArrayLength(savedState);
710        }
711
712        code->createActivityFunc(code, rawSavedState, rawSavedSize);
713
714        if (rawSavedState != NULL) {
715            env->ReleaseByteArrayElements(savedState, rawSavedState, 0);
716        }
717    }
718
719    return (jint)code;
720}
721
722static void
723unloadNativeCode_native(JNIEnv* env, jobject clazz, jint handle)
724{
725    LOG_TRACE("unloadNativeCode_native");
726    if (handle != 0) {
727        NativeCode* code = (NativeCode*)handle;
728        delete code;
729    }
730}
731
732static void
733onStart_native(JNIEnv* env, jobject clazz, jint handle)
734{
735    LOG_TRACE("onStart_native");
736    if (handle != 0) {
737        NativeCode* code = (NativeCode*)handle;
738        if (code->callbacks.onStart != NULL) {
739            code->callbacks.onStart(code);
740        }
741    }
742}
743
744static void
745onResume_native(JNIEnv* env, jobject clazz, jint handle)
746{
747    LOG_TRACE("onResume_native");
748    if (handle != 0) {
749        NativeCode* code = (NativeCode*)handle;
750        if (code->callbacks.onResume != NULL) {
751            code->callbacks.onResume(code);
752        }
753    }
754}
755
756static jbyteArray
757onSaveInstanceState_native(JNIEnv* env, jobject clazz, jint handle)
758{
759    LOG_TRACE("onSaveInstanceState_native");
760
761    jbyteArray array = NULL;
762
763    if (handle != 0) {
764        NativeCode* code = (NativeCode*)handle;
765        if (code->callbacks.onSaveInstanceState != NULL) {
766            size_t len = 0;
767            jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len);
768            if (len > 0) {
769                array = env->NewByteArray(len);
770                if (array != NULL) {
771                    env->SetByteArrayRegion(array, 0, len, state);
772                }
773            }
774            if (state != NULL) {
775                free(state);
776            }
777        }
778    }
779
780    return array;
781}
782
783static void
784onPause_native(JNIEnv* env, jobject clazz, jint handle)
785{
786    LOG_TRACE("onPause_native");
787    if (handle != 0) {
788        NativeCode* code = (NativeCode*)handle;
789        if (code->callbacks.onPause != NULL) {
790            code->callbacks.onPause(code);
791        }
792    }
793}
794
795static void
796onStop_native(JNIEnv* env, jobject clazz, jint handle)
797{
798    LOG_TRACE("onStop_native");
799    if (handle != 0) {
800        NativeCode* code = (NativeCode*)handle;
801        if (code->callbacks.onStop != NULL) {
802            code->callbacks.onStop(code);
803        }
804    }
805}
806
807static void
808onConfigurationChanged_native(JNIEnv* env, jobject clazz, jint handle)
809{
810    LOG_TRACE("onConfigurationChanged_native");
811    if (handle != 0) {
812        NativeCode* code = (NativeCode*)handle;
813        if (code->callbacks.onConfigurationChanged != NULL) {
814            code->callbacks.onConfigurationChanged(code);
815        }
816    }
817}
818
819static void
820onLowMemory_native(JNIEnv* env, jobject clazz, jint handle)
821{
822    LOG_TRACE("onLowMemory_native");
823    if (handle != 0) {
824        NativeCode* code = (NativeCode*)handle;
825        if (code->callbacks.onLowMemory != NULL) {
826            code->callbacks.onLowMemory(code);
827        }
828    }
829}
830
831static void
832onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jint handle, jboolean focused)
833{
834    LOG_TRACE("onWindowFocusChanged_native");
835    if (handle != 0) {
836        NativeCode* code = (NativeCode*)handle;
837        if (code->callbacks.onWindowFocusChanged != NULL) {
838            code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0);
839        }
840    }
841}
842
843static void
844onSurfaceCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject surface)
845{
846    LOG_TRACE("onSurfaceCreated_native");
847    if (handle != 0) {
848        NativeCode* code = (NativeCode*)handle;
849        code->setSurface(surface);
850        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) {
851            code->callbacks.onNativeWindowCreated(code,
852                    code->nativeWindow.get());
853        }
854    }
855}
856
857static int32_t getWindowProp(ANativeWindow* window, int what) {
858    int value;
859    int res = window->query(window, what, &value);
860    return res < 0 ? res : value;
861}
862
863static void
864onSurfaceChanged_native(JNIEnv* env, jobject clazz, jint handle, jobject surface,
865        jint format, jint width, jint height)
866{
867    LOG_TRACE("onSurfaceChanged_native");
868    if (handle != 0) {
869        NativeCode* code = (NativeCode*)handle;
870        sp<ANativeWindow> oldNativeWindow = code->nativeWindow;
871        code->setSurface(surface);
872        if (oldNativeWindow != code->nativeWindow) {
873            if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
874                code->callbacks.onNativeWindowDestroyed(code,
875                        oldNativeWindow.get());
876            }
877            if (code->nativeWindow != NULL) {
878                if (code->callbacks.onNativeWindowCreated != NULL) {
879                    code->callbacks.onNativeWindowCreated(code,
880                            code->nativeWindow.get());
881                }
882                code->lastWindowWidth = getWindowProp(code->nativeWindow.get(),
883                        NATIVE_WINDOW_WIDTH);
884                code->lastWindowHeight = getWindowProp(code->nativeWindow.get(),
885                        NATIVE_WINDOW_HEIGHT);
886            }
887        } else {
888            // Maybe it resized?
889            int32_t newWidth = getWindowProp(code->nativeWindow.get(),
890                    NATIVE_WINDOW_WIDTH);
891            int32_t newHeight = getWindowProp(code->nativeWindow.get(),
892                    NATIVE_WINDOW_HEIGHT);
893            if (newWidth != code->lastWindowWidth
894                    || newHeight != code->lastWindowHeight) {
895                if (code->callbacks.onNativeWindowResized != NULL) {
896                    code->callbacks.onNativeWindowResized(code,
897                            code->nativeWindow.get());
898                }
899            }
900        }
901    }
902}
903
904static void
905onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jint handle)
906{
907    LOG_TRACE("onSurfaceRedrawNeeded_native");
908    if (handle != 0) {
909        NativeCode* code = (NativeCode*)handle;
910        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) {
911            code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get());
912        }
913    }
914}
915
916static void
917onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject surface)
918{
919    LOG_TRACE("onSurfaceDestroyed_native");
920    if (handle != 0) {
921        NativeCode* code = (NativeCode*)handle;
922        if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) {
923            code->callbacks.onNativeWindowDestroyed(code,
924                    code->nativeWindow.get());
925        }
926        code->setSurface(NULL);
927    }
928}
929
930static void
931onInputChannelCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject channel)
932{
933    LOG_TRACE("onInputChannelCreated_native");
934    if (handle != 0) {
935        NativeCode* code = (NativeCode*)handle;
936        status_t err = code->setInputChannel(channel);
937        if (err != OK) {
938            jniThrowException(env, "java/lang/IllegalStateException",
939                    "Error setting input channel");
940            return;
941        }
942        if (code->callbacks.onInputQueueCreated != NULL) {
943            code->callbacks.onInputQueueCreated(code,
944                    code->nativeInputQueue);
945        }
946    }
947}
948
949static void
950onInputChannelDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject channel)
951{
952    LOG_TRACE("onInputChannelDestroyed_native");
953    if (handle != 0) {
954        NativeCode* code = (NativeCode*)handle;
955        if (code->nativeInputQueue != NULL
956                && code->callbacks.onInputQueueDestroyed != NULL) {
957            code->callbacks.onInputQueueDestroyed(code,
958                    code->nativeInputQueue);
959        }
960        code->setInputChannel(NULL);
961    }
962}
963
964static void
965onContentRectChanged_native(JNIEnv* env, jobject clazz, jint handle,
966        jint x, jint y, jint w, jint h)
967{
968    LOG_TRACE("onContentRectChanged_native");
969    if (handle != 0) {
970        NativeCode* code = (NativeCode*)handle;
971        if (code->callbacks.onContentRectChanged != NULL) {
972            ARect rect;
973            rect.left = x;
974            rect.top = y;
975            rect.right = x+w;
976            rect.bottom = y+h;
977            code->callbacks.onContentRectChanged(code, &rect);
978        }
979    }
980}
981
982static void
983dispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle, jobject eventObj)
984{
985    LOG_TRACE("dispatchKeyEvent_native");
986    if (handle != 0) {
987        NativeCode* code = (NativeCode*)handle;
988        if (code->nativeInputQueue != NULL) {
989            KeyEvent* event = code->nativeInputQueue->createKeyEvent();
990            android_view_KeyEvent_toNative(env, eventObj, event);
991            code->nativeInputQueue->dispatchEvent(event);
992        }
993    }
994}
995
996static void
997finishPreDispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle,
998        jint seq, jboolean handled)
999{
1000    LOG_TRACE("finishPreDispatchKeyEvent_native");
1001    if (handle != 0) {
1002        NativeCode* code = (NativeCode*)handle;
1003        if (code->nativeInputQueue != NULL) {
1004            code->nativeInputQueue->finishPreDispatch(seq, handled ? true : false);
1005        }
1006    }
1007}
1008
1009static const JNINativeMethod g_methods[] = {
1010    { "loadNativeCode", "(Ljava/lang/String;Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;ILandroid/content/res/AssetManager;[B)I",
1011            (void*)loadNativeCode_native },
1012    { "unloadNativeCode", "(I)V", (void*)unloadNativeCode_native },
1013    { "onStartNative", "(I)V", (void*)onStart_native },
1014    { "onResumeNative", "(I)V", (void*)onResume_native },
1015    { "onSaveInstanceStateNative", "(I)[B", (void*)onSaveInstanceState_native },
1016    { "onPauseNative", "(I)V", (void*)onPause_native },
1017    { "onStopNative", "(I)V", (void*)onStop_native },
1018    { "onConfigurationChangedNative", "(I)V", (void*)onConfigurationChanged_native },
1019    { "onLowMemoryNative", "(I)V", (void*)onLowMemory_native },
1020    { "onWindowFocusChangedNative", "(IZ)V", (void*)onWindowFocusChanged_native },
1021    { "onSurfaceCreatedNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceCreated_native },
1022    { "onSurfaceChangedNative", "(ILandroid/view/Surface;III)V", (void*)onSurfaceChanged_native },
1023    { "onSurfaceRedrawNeededNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native },
1024    { "onSurfaceDestroyedNative", "(I)V", (void*)onSurfaceDestroyed_native },
1025    { "onInputChannelCreatedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelCreated_native },
1026    { "onInputChannelDestroyedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelDestroyed_native },
1027    { "onContentRectChangedNative", "(IIIII)V", (void*)onContentRectChanged_native },
1028    { "dispatchKeyEventNative", "(ILandroid/view/KeyEvent;)V", (void*)dispatchKeyEvent_native },
1029    { "finishPreDispatchKeyEventNative", "(IIZ)V", (void*)finishPreDispatchKeyEvent_native },
1030};
1031
1032static const char* const kNativeActivityPathName = "android/app/NativeActivity";
1033
1034#define FIND_CLASS(var, className) \
1035        var = env->FindClass(className); \
1036        LOG_FATAL_IF(! var, "Unable to find class %s", className); \
1037        var = jclass(env->NewGlobalRef(var));
1038
1039#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
1040        var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
1041        LOG_FATAL_IF(! var, "Unable to find method" methodName);
1042
1043int register_android_app_NativeActivity(JNIEnv* env)
1044{
1045    //LOGD("register_android_app_NativeActivity");
1046
1047    FIND_CLASS(gNativeActivityClassInfo.clazz, kNativeActivityPathName);
1048
1049    GET_METHOD_ID(gNativeActivityClassInfo.dispatchUnhandledKeyEvent,
1050            gNativeActivityClassInfo.clazz,
1051            "dispatchUnhandledKeyEvent", "(Landroid/view/KeyEvent;)Z");
1052    GET_METHOD_ID(gNativeActivityClassInfo.preDispatchKeyEvent,
1053            gNativeActivityClassInfo.clazz,
1054            "preDispatchKeyEvent", "(Landroid/view/KeyEvent;I)V");
1055
1056    GET_METHOD_ID(gNativeActivityClassInfo.finish,
1057            gNativeActivityClassInfo.clazz,
1058            "finish", "()V");
1059    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFlags,
1060            gNativeActivityClassInfo.clazz,
1061            "setWindowFlags", "(II)V");
1062    GET_METHOD_ID(gNativeActivityClassInfo.setWindowFormat,
1063            gNativeActivityClassInfo.clazz,
1064            "setWindowFormat", "(I)V");
1065    GET_METHOD_ID(gNativeActivityClassInfo.showIme,
1066            gNativeActivityClassInfo.clazz,
1067            "showIme", "(I)V");
1068    GET_METHOD_ID(gNativeActivityClassInfo.hideIme,
1069            gNativeActivityClassInfo.clazz,
1070            "hideIme", "(I)V");
1071
1072    return AndroidRuntime::registerNativeMethods(
1073        env, kNativeActivityPathName,
1074        g_methods, NELEM(g_methods));
1075}
1076
1077} // namespace android
1078