Home | History | Annotate | Download | only in android_runtime
      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 #ifndef _ANDROID_APP_NATIVEACTIVITY_H
     18 #define _ANDROID_APP_NATIVEACTIVITY_H
     19 
     20 #include <androidfw/InputTransport.h>
     21 #include <utils/Looper.h>
     22 
     23 #include <android/native_activity.h>
     24 
     25 #include "jni.h"
     26 
     27 namespace android {
     28 
     29 extern void android_NativeActivity_finish(
     30         ANativeActivity* activity);
     31 
     32 extern void android_NativeActivity_setWindowFormat(
     33         ANativeActivity* activity, int32_t format);
     34 
     35 extern void android_NativeActivity_setWindowFlags(
     36         ANativeActivity* activity, int32_t values, int32_t mask);
     37 
     38 extern void android_NativeActivity_showSoftInput(
     39         ANativeActivity* activity, int32_t flags);
     40 
     41 extern void android_NativeActivity_hideSoftInput(
     42         ANativeActivity* activity, int32_t flags);
     43 
     44 } // namespace android
     45 
     46 
     47 /*
     48  * NDK input queue API.
     49  *
     50  * Here is the event flow:
     51  * 1. Event arrives in input consumer, and is returned by getEvent().
     52  * 2. Application calls preDispatchEvent():
     53  *    a. Event is assigned a sequence ID and enqueued in mPreDispatchingKeys.
     54  *    b. Main thread picks up event, hands to input method.
     55  *    c. Input method eventually returns sequence # and whether it was handled.
     56  *    d. finishPreDispatch() is called to enqueue the information.
     57  *    e. next getEvent() call will:
     58  *       - finish any pre-dispatch events that the input method handled
     59  *       - return the next pre-dispatched event that the input method didn't handle.
     60  *    f. (A preDispatchEvent() call on this event will now return false).
     61  * 3. Application calls finishEvent() with whether it was handled.
     62  *    - If handled is true, the event is finished.
     63  *    - If handled is false, the event is put on mUnhandledKeys, and:
     64  *      a. Main thread receives event from consumeUnhandledEvent().
     65  *      b. Java sends event through default key handler.
     66  *      c. event is finished.
     67  */
     68 struct AInputQueue {
     69 public:
     70     /* Creates a consumer associated with an input channel. */
     71     explicit AInputQueue(const android::sp<android::InputChannel>& channel, int workWrite);
     72 
     73     /* Destroys the consumer and releases its input channel. */
     74     ~AInputQueue();
     75 
     76     void attachLooper(ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
     77 
     78     void detachLooper();
     79 
     80     int32_t hasEvents();
     81 
     82     int32_t getEvent(AInputEvent** outEvent);
     83 
     84     bool preDispatchEvent(AInputEvent* event);
     85 
     86     void finishEvent(AInputEvent* event, bool handled, bool didDefaultHandling);
     87 
     88     // ----------------------------------------------------------
     89 
     90     inline android::InputConsumer& getConsumer() { return mConsumer; }
     91 
     92     void dispatchEvent(android::KeyEvent* event);
     93 
     94     void finishPreDispatch(int seq, bool handled);
     95 
     96     android::KeyEvent* consumeUnhandledEvent();
     97     android::KeyEvent* consumePreDispatchingEvent(int* outSeq);
     98 
     99     android::KeyEvent* createKeyEvent();
    100 
    101     int mWorkWrite;
    102 
    103 private:
    104     void doUnhandledKey(android::KeyEvent* keyEvent);
    105     bool preDispatchKey(android::KeyEvent* keyEvent);
    106     void wakeupDispatchLocked();
    107 
    108     android::PooledInputEventFactory mPooledInputEventFactory;
    109     android::InputConsumer mConsumer;
    110     android::sp<android::Looper> mLooper;
    111 
    112     int mDispatchKeyRead;
    113     int mDispatchKeyWrite;
    114 
    115     struct in_flight_event {
    116         android::InputEvent* event;
    117         int seq; // internal sequence number for synthetic pre-dispatch events
    118         uint32_t finishSeq; // sequence number for sendFinishedSignal, or 0 if finish not required
    119     };
    120 
    121     struct finish_pre_dispatch {
    122         int seq;
    123         bool handled;
    124     };
    125 
    126     android::Mutex mLock;
    127 
    128     int mSeq;
    129 
    130     // All input events that are actively being processed.
    131     android::Vector<in_flight_event> mInFlightEvents;
    132 
    133     // Key events that the app didn't handle, and are pending for
    134     // delivery to the activity's default key handling.
    135     android::Vector<android::KeyEvent*> mUnhandledKeys;
    136 
    137     // Keys that arrived in the Java framework and need to be
    138     // dispatched to the app.
    139     android::Vector<android::KeyEvent*> mDispatchingKeys;
    140 
    141     // Key events that are pending to be pre-dispatched to the IME.
    142     android::Vector<in_flight_event> mPreDispatchingKeys;
    143 
    144     // Event sequence numbers that we have finished pre-dispatching.
    145     android::Vector<finish_pre_dispatch> mFinishPreDispatches;
    146 };
    147 
    148 #endif // _ANDROID_APP_NATIVEACTIVITY_H
    149