• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ui/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 : public android::InputEventFactoryInterface {
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 
getConsumerAInputQueue90     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     virtual android::KeyEvent* createKeyEvent();
100     virtual android::MotionEvent* createMotionEvent();
101 
102     int mWorkWrite;
103 
104 private:
105     void doUnhandledKey(android::KeyEvent* keyEvent);
106     bool preDispatchKey(android::KeyEvent* keyEvent);
107     void wakeupDispatch();
108 
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;
118         bool doFinish;
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     // Cache of previously allocated key events.
131     android::Vector<android::KeyEvent*> mAvailKeyEvents;
132     // Cache of previously allocated motion events.
133     android::Vector<android::MotionEvent*> mAvailMotionEvents;
134 
135     // All input events that are actively being processed.
136     android::Vector<in_flight_event> mInFlightEvents;
137 
138     // Key events that the app didn't handle, and are pending for
139     // delivery to the activity's default key handling.
140     android::Vector<android::KeyEvent*> mUnhandledKeys;
141 
142     // Keys that arrived in the Java framework and need to be
143     // dispatched to the app.
144     android::Vector<android::KeyEvent*> mDispatchingKeys;
145 
146     // Key events that are pending to be pre-dispatched to the IME.
147     android::Vector<in_flight_event> mPreDispatchingKeys;
148 
149     // Event sequence numbers that we have finished pre-dispatching.
150     android::Vector<finish_pre_dispatch> mFinishPreDispatches;
151 };
152 
153 #endif // _ANDROID_APP_NATIVEACTIVITY_H
154