blob: 145f71314408a3e5090361f254f8f6691d318660 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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 "InputManager-JNI"
18
Jeff Brown9c3cda02010-06-15 01:31:58 -070019//#define LOG_NDEBUG 0
20
21// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070023
24// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070026
Jeff Brown83c09682010-12-23 17:50:18 -080027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "JNIHelp.h"
29#include "jni.h"
Jeff Brown349703e2010-06-22 01:27:15 -070030#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include <android_runtime/AndroidRuntime.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080034#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080036
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037#include <input/InputManager.h>
38#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070039#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040
Jeff Brown05dc66a2011-03-02 14:41:58 -080041#include <android_os_MessageQueue.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include <android_view_KeyEvent.h>
43#include <android_view_MotionEvent.h>
44#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070045#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080046#include <android/graphics/GraphicsJNI.h>
47
Jeff Brown00fa7bd2010-07-02 15:37:36 -070048#include "com_android_server_PowerManagerService.h"
Jeff Brown928e0542011-01-10 11:17:36 -080049#include "com_android_server_InputApplicationHandle.h"
Jeff Brown928e0542011-01-10 11:17:36 -080050#include "com_android_server_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
52namespace android {
53
Jeff Brown1a84fd12011-06-02 01:26:32 -070054// The exponent used to calculate the pointer speed scaling factor.
55// The scaling factor is calculated as 2 ^ (speed * exponent),
56// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070057static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070058
Jeff Brown46b9ac02010-04-22 18:58:52 -070059static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070060 jmethodID notifyConfigurationChanged;
61 jmethodID notifyLidSwitchChanged;
Jeff Brown7fbdc842010-06-17 20:52:56 -070062 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070063 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070064 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070065 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080066 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070067 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070068 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070069 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080070 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070071 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080072 jmethodID getKeyRepeatTimeout;
73 jmethodID getKeyRepeatDelay;
Jeff Brownae9fc032010-08-18 15:51:08 -070074 jmethodID getMaxEventsPerSecond;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070075 jmethodID getHoverTapTimeout;
76 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070077 jmethodID getDoubleTapTimeout;
78 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080079 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080 jmethodID getPointerIcon;
Jeff Brown46b9ac02010-04-22 18:58:52 -070081} gCallbacksClassInfo;
82
83static struct {
84 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070085} gKeyEventClassInfo;
86
87static struct {
88 jclass clazz;
89} gMotionEventClassInfo;
90
Jeff Brown8d608662010-08-30 03:02:23 -070091static struct {
92 jclass clazz;
93
94 jmethodID ctor;
95 jmethodID addMotionRange;
96
97 jfieldID mId;
98 jfieldID mName;
99 jfieldID mSources;
100 jfieldID mKeyboardType;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800101 jfieldID mKeyCharacterMapFile;
Jeff Brown8d608662010-08-30 03:02:23 -0700102} gInputDeviceClassInfo;
103
Jeff Brown57c59372010-09-21 18:22:55 -0700104static struct {
Jeff Brown57c59372010-09-21 18:22:55 -0700105 jfieldID touchscreen;
106 jfieldID keyboard;
107 jfieldID navigation;
108} gConfigurationClassInfo;
109
Jeff Brown928e0542011-01-10 11:17:36 -0800110
111// --- Global functions ---
112
Jeff Brown214eaf42011-05-26 19:17:02 -0700113template<typename T>
114inline static T min(const T& a, const T& b) {
115 return a < b ? a : b;
116}
117
118template<typename T>
119inline static T max(const T& a, const T& b) {
120 return a > b ? a : b;
121}
122
Jeff Brown928e0542011-01-10 11:17:36 -0800123static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
124 const sp<InputApplicationHandle>& inputApplicationHandle) {
125 if (inputApplicationHandle == NULL) {
126 return NULL;
127 }
128 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
129 getInputApplicationHandleObjLocalRef(env);
130}
131
132static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
133 const sp<InputWindowHandle>& inputWindowHandle) {
134 if (inputWindowHandle == NULL) {
135 return NULL;
136 }
137 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
138 getInputWindowHandleObjLocalRef(env);
139}
140
Jeff Brown2352b972011-04-12 22:39:53 -0700141static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
142 SpriteIcon* outSpriteIcon) {
143 PointerIcon pointerIcon;
144 status_t status = android_view_PointerIcon_loadSystemIcon(env,
145 contextObj, style, &pointerIcon);
146 if (!status) {
147 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
148 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
149 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
150 }
151}
152
Jeff Brown905805a2011-10-12 13:57:59 -0700153enum {
154 WM_ACTION_PASS_TO_USER = 1,
155 WM_ACTION_POKE_USER_ACTIVITY = 2,
156 WM_ACTION_GO_TO_SLEEP = 4,
157};
158
Jeff Brown928e0542011-01-10 11:17:36 -0800159
160// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800161
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162class NativeInputManager : public virtual RefBase,
163 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700164 public virtual InputDispatcherPolicyInterface,
165 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166protected:
167 virtual ~NativeInputManager();
168
169public:
Jeff Brown2352b972011-04-12 22:39:53 -0700170 NativeInputManager(jobject contextObj, jobject callbacksObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700171
172 inline sp<InputManager> getInputManager() const { return mInputManager; }
173
Jeff Brownb88102f2010-09-08 11:49:43 -0700174 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700175
Jeff Brownbc68a592011-07-25 12:58:12 -0700176 void setDisplaySize(int32_t displayId, int32_t width, int32_t height,
177 int32_t externalWidth, int32_t externalHeight);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178 void setDisplayOrientation(int32_t displayId, int32_t orientation);
179
Jeff Brown7fbdc842010-06-17 20:52:56 -0700180 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800181 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700182 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
183
Jeff Brown9302c872011-07-13 22:51:29 -0700184 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
185 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700186 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800187 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700188 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700189 void setShowTouches(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700190
Jeff Brown9c3cda02010-06-15 01:31:58 -0700191 /* --- InputReaderPolicyInterface implementation --- */
192
Jeff Brown214eaf42011-05-26 19:17:02 -0700193 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800194 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700195
196 /* --- InputDispatcherPolicyInterface implementation --- */
197
Jeff Browne20c9e02010-10-11 14:20:19 -0700198 virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
199 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700201 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800202 const sp<InputWindowHandle>& inputWindowHandle);
203 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700204 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700205 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
206 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800207 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800208 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700209 virtual nsecs_t interceptKeyBeforeDispatching(
210 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700211 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800212 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800213 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700214 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700215 virtual bool checkInjectEventsPermissionNonReentrant(
216 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700217
Jeff Brown2352b972011-04-12 22:39:53 -0700218 /* --- PointerControllerPolicyInterface implementation --- */
219
220 virtual void loadPointerResources(PointerResources* outResources);
221
Jeff Brown9c3cda02010-06-15 01:31:58 -0700222private:
223 sp<InputManager> mInputManager;
224
Jeff Brown2352b972011-04-12 22:39:53 -0700225 jobject mContextObj;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226 jobject mCallbacksObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800227 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228
Jeff Brown83c09682010-12-23 17:50:18 -0800229 Mutex mLock;
230 struct Locked {
231 // Display size information.
Jeff Brownbc68a592011-07-25 12:58:12 -0700232 int32_t displayWidth, displayHeight; // -1 when not initialized
233 int32_t displayExternalWidth, displayExternalHeight; // -1 when not initialized
Jeff Brown83c09682010-12-23 17:50:18 -0800234 int32_t displayOrientation;
235
Jeff Brown05dc66a2011-03-02 14:41:58 -0800236 // System UI visibility.
237 int32_t systemUiVisibility;
238
Jeff Brown1a84fd12011-06-02 01:26:32 -0700239 // Pointer speed.
240 int32_t pointerSpeed;
241
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 // True if pointer gestures are enabled.
243 bool pointerGesturesEnabled;
244
Jeff Browndaf4a122011-08-26 17:14:14 -0700245 // Show touches feature enable/disable.
246 bool showTouches;
247
Jeff Brown5541de92011-04-11 11:54:25 -0700248 // Sprite controller singleton, created on first use.
249 sp<SpriteController> spriteController;
250
Jeff Brown83c09682010-12-23 17:50:18 -0800251 // Pointer controller singleton, created and destroyed as needed.
252 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800253 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700254
Jeff Brown2352b972011-04-12 22:39:53 -0700255 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800256 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700257 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800258
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700259 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260 bool isScreenOn();
261 bool isScreenBright();
262
Jeff Brownb88102f2010-09-08 11:49:43 -0700263 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700264
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265 static inline JNIEnv* jniEnv() {
266 return AndroidRuntime::getJNIEnv();
267 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268};
269
Jeff Brown928e0542011-01-10 11:17:36 -0800270
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271
Jeff Brown2352b972011-04-12 22:39:53 -0700272NativeInputManager::NativeInputManager(jobject contextObj,
273 jobject callbacksObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700274 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275 JNIEnv* env = jniEnv();
276
Jeff Brown2352b972011-04-12 22:39:53 -0700277 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700278 mCallbacksObj = env->NewGlobalRef(callbacksObj);
279
Jeff Brown83c09682010-12-23 17:50:18 -0800280 {
281 AutoMutex _l(mLock);
282 mLocked.displayWidth = -1;
283 mLocked.displayHeight = -1;
Jeff Brownbc68a592011-07-25 12:58:12 -0700284 mLocked.displayExternalWidth = -1;
285 mLocked.displayExternalHeight = -1;
Jeff Brown65fd2512011-08-18 11:20:58 -0700286 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800287
288 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700289 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700290 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700291 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800292 }
293
Jeff Brown9c3cda02010-06-15 01:31:58 -0700294 sp<EventHub> eventHub = new EventHub();
295 mInputManager = new InputManager(eventHub, this, this);
296}
297
298NativeInputManager::~NativeInputManager() {
299 JNIEnv* env = jniEnv();
300
Jeff Brown2352b972011-04-12 22:39:53 -0700301 env->DeleteGlobalRef(mContextObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700302 env->DeleteGlobalRef(mCallbacksObj);
303}
304
Jeff Brownb88102f2010-09-08 11:49:43 -0700305void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700306 mInputManager->getReader()->dump(dump);
307 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308
Jeff Brownb88102f2010-09-08 11:49:43 -0700309 mInputManager->getDispatcher()->dump(dump);
310 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700311}
312
Jeff Brown7fbdc842010-06-17 20:52:56 -0700313bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314 if (env->ExceptionCheck()) {
315 LOGE("An exception was thrown by callback '%s'.", methodName);
316 LOGE_EX(env);
317 env->ExceptionClear();
318 return true;
319 }
320 return false;
321}
322
Jeff Brownbc68a592011-07-25 12:58:12 -0700323void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height,
324 int32_t externalWidth, int32_t externalHeight) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700325 bool changed = false;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700326 if (displayId == 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700327 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700328
Jeff Brown65fd2512011-08-18 11:20:58 -0700329 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
330 changed = true;
331 mLocked.displayWidth = width;
332 mLocked.displayHeight = height;
Jeff Brownbc68a592011-07-25 12:58:12 -0700333
Jeff Brown65fd2512011-08-18 11:20:58 -0700334 sp<PointerController> controller = mLocked.pointerController.promote();
335 if (controller != NULL) {
336 controller->setDisplaySize(width, height);
Jeff Brown2352b972011-04-12 22:39:53 -0700337 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700338 }
Jeff Brown2352b972011-04-12 22:39:53 -0700339
Jeff Brown65fd2512011-08-18 11:20:58 -0700340 if (mLocked.displayExternalWidth != externalWidth
341 || mLocked.displayExternalHeight != externalHeight) {
342 changed = true;
Jeff Brownbc68a592011-07-25 12:58:12 -0700343 mLocked.displayExternalWidth = externalWidth;
344 mLocked.displayExternalHeight = externalHeight;
Jeff Brown65fd2512011-08-18 11:20:58 -0700345 }
346 }
347
348 if (changed) {
349 mInputManager->getReader()->requestRefreshConfiguration(
350 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351 }
352}
353
354void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700355 bool changed = false;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700356 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800357 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700358
Jeff Brown83c09682010-12-23 17:50:18 -0800359 if (mLocked.displayOrientation != orientation) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700360 changed = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800361 mLocked.displayOrientation = orientation;
362
363 sp<PointerController> controller = mLocked.pointerController.promote();
364 if (controller != NULL) {
365 controller->setDisplayOrientation(orientation);
366 }
367 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700368 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700369
370 if (changed) {
371 mInputManager->getReader()->requestRefreshConfiguration(
372 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
373 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700374}
375
Jeff Brown7fbdc842010-06-17 20:52:56 -0700376status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800377 const sp<InputChannel>& inputChannel,
378 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
379 return mInputManager->getDispatcher()->registerInputChannel(
380 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700381}
382
383status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
384 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700385 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700386}
387
Jeff Brown214eaf42011-05-26 19:17:02 -0700388void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700389 JNIEnv* env = jniEnv();
390
Jeff Brown214eaf42011-05-26 19:17:02 -0700391 jint virtualKeyQuietTime = env->CallIntMethod(mCallbacksObj,
392 gCallbacksClassInfo.getVirtualKeyQuietTimeMillis);
393 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
394 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
395 }
396
397 outConfig->excludedDeviceNames.clear();
398 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mCallbacksObj,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700399 gCallbacksClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700400 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
401 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700402 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700403 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700404 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700405 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700406 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700407 env->DeleteLocalRef(item);
408 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700409 env->DeleteLocalRef(excludedDeviceNames);
410 }
411
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700412 jint hoverTapTimeout = env->CallIntMethod(mCallbacksObj,
413 gCallbacksClassInfo.getHoverTapTimeout);
414 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700415 jint doubleTapTimeout = env->CallIntMethod(mCallbacksObj,
416 gCallbacksClassInfo.getDoubleTapTimeout);
417 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
418 jint longPressTimeout = env->CallIntMethod(mCallbacksObj,
419 gCallbacksClassInfo.getLongPressTimeout);
420 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700421 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700422
423 // We must ensure that the tap-drag interval is significantly shorter than
424 // the long-press timeout because the tap is held down for the entire duration
425 // of the double-tap timeout.
426 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700427 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700428 outConfig->pointerGestureTapDragInterval =
429 milliseconds_to_nanoseconds(tapDragInterval);
430 }
431 }
432 }
433
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700434 jint hoverTapSlop = env->CallIntMethod(mCallbacksObj,
435 gCallbacksClassInfo.getHoverTapSlop);
436 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
437 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700438 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700439
440 { // acquire lock
441 AutoMutex _l(mLock);
442
443 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
444 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700445 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700446
Jeff Browndaf4a122011-08-26 17:14:14 -0700447 outConfig->showTouches = mLocked.showTouches;
448
Jeff Brown65fd2512011-08-18 11:20:58 -0700449 outConfig->setDisplayInfo(0, false /*external*/,
450 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);
451 outConfig->setDisplayInfo(0, true /*external*/,
452 mLocked.displayExternalWidth, mLocked.displayExternalHeight,
453 mLocked.displayOrientation);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700454 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700455}
456
Jeff Brown83c09682010-12-23 17:50:18 -0800457sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
458 AutoMutex _l(mLock);
459
460 sp<PointerController> controller = mLocked.pointerController.promote();
461 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700462 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800463
Jeff Brown2352b972011-04-12 22:39:53 -0700464 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800465 mLocked.pointerController = controller;
466
467 controller->setDisplaySize(mLocked.displayWidth, mLocked.displayHeight);
468 controller->setDisplayOrientation(mLocked.displayOrientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800469
Jeff Brown5541de92011-04-11 11:54:25 -0700470 JNIEnv* env = jniEnv();
Jeff Brown2352b972011-04-12 22:39:53 -0700471 jobject pointerIconObj = env->CallObjectMethod(mCallbacksObj,
472 gCallbacksClassInfo.getPointerIcon);
473 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
474 PointerIcon pointerIcon;
475 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
476 mContextObj, &pointerIcon);
477 if (!status && !pointerIcon.isNullIcon()) {
478 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
479 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
480 } else {
481 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800482 }
Jeff Brown2352b972011-04-12 22:39:53 -0700483 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800484 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800485
Jeff Brown2352b972011-04-12 22:39:53 -0700486 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800487 }
488 return controller;
489}
490
Jeff Brown5541de92011-04-11 11:54:25 -0700491void NativeInputManager::ensureSpriteControllerLocked() {
492 if (mLocked.spriteController == NULL) {
493 JNIEnv* env = jniEnv();
494 jint layer = env->CallIntMethod(mCallbacksObj, gCallbacksClassInfo.getPointerLayer);
495 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
496 layer = -1;
497 }
498 mLocked.spriteController = new SpriteController(mLooper, layer);
499 }
500}
501
Jeff Browne20c9e02010-10-11 14:20:19 -0700502void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
503 int32_t switchValue, uint32_t policyFlags) {
504#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000505 ALOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
Jeff Browne20c9e02010-10-11 14:20:19 -0700506 when, switchCode, switchValue, policyFlags);
507#endif
508
509 JNIEnv* env = jniEnv();
510
511 switch (switchCode) {
512 case SW_LID:
513 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyLidSwitchChanged,
514 when, switchValue == 0);
515 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
516 break;
517 }
518}
519
Jeff Brown9c3cda02010-06-15 01:31:58 -0700520void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
521#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000522 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700523#endif
524
525 JNIEnv* env = jniEnv();
526
Jeff Brown57c59372010-09-21 18:22:55 -0700527 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700528 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700529}
530
Jeff Brown519e0242010-09-15 15:18:56 -0700531nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800532 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700533#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000534 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700535#endif
536
537 JNIEnv* env = jniEnv();
538
Jeff Brown928e0542011-01-10 11:17:36 -0800539 jobject inputApplicationHandleObj =
540 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
541 jobject inputWindowHandleObj =
542 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700543
Jeff Brown519e0242010-09-15 15:18:56 -0700544 jlong newTimeout = env->CallLongMethod(mCallbacksObj,
Jeff Brown928e0542011-01-10 11:17:36 -0800545 gCallbacksClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700546 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
547 newTimeout = 0; // abort dispatch
548 } else {
549 assert(newTimeout >= 0);
550 }
551
Jeff Brown928e0542011-01-10 11:17:36 -0800552 env->DeleteLocalRef(inputWindowHandleObj);
553 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700554 return newTimeout;
555}
556
Jeff Brown928e0542011-01-10 11:17:36 -0800557void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700558#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000559 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700560#endif
561
Jeff Brown7fbdc842010-06-17 20:52:56 -0700562 JNIEnv* env = jniEnv();
563
Jeff Brown928e0542011-01-10 11:17:36 -0800564 jobject inputWindowHandleObj =
565 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
566 if (inputWindowHandleObj) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700567 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800568 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700569 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
570
Jeff Brown928e0542011-01-10 11:17:36 -0800571 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700572 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573}
574
Jeff Brown214eaf42011-05-26 19:17:02 -0700575void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
576 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800577
Jeff Brown214eaf42011-05-26 19:17:02 -0700578 jint keyRepeatTimeout = env->CallIntMethod(mCallbacksObj,
579 gCallbacksClassInfo.getKeyRepeatTimeout);
580 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
581 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
582 }
Jeff Browna4547672011-03-02 21:38:11 -0800583
Jeff Brown214eaf42011-05-26 19:17:02 -0700584 jint keyRepeatDelay = env->CallIntMethod(mCallbacksObj,
585 gCallbacksClassInfo.getKeyRepeatDelay);
586 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
587 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
588 }
589
590 jint maxEventsPerSecond = env->CallIntMethod(mCallbacksObj,
591 gCallbacksClassInfo.getMaxEventsPerSecond);
592 if (!checkAndClearExceptionFromCallback(env, "getMaxEventsPerSecond")) {
593 outConfig->maxEventsPerSecond = maxEventsPerSecond;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700594 }
595}
596
Jeff Brown214eaf42011-05-26 19:17:02 -0700597bool NativeInputManager::isKeyRepeatEnabled() {
598 // Only enable automatic key repeating when the screen is on.
599 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700600}
601
Jeff Brown9302c872011-07-13 22:51:29 -0700602void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
603 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700604
Jeff Brown9302c872011-07-13 22:51:29 -0700605 if (windowHandleObjArray) {
606 jsize length = env->GetArrayLength(windowHandleObjArray);
607 for (jsize i = 0; i < length; i++) {
608 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
609 if (! windowHandleObj) {
610 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700611 }
Jeff Brown9302c872011-07-13 22:51:29 -0700612
613 sp<InputWindowHandle> windowHandle =
614 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
615 if (windowHandle != NULL) {
616 windowHandles.push(windowHandle);
617 }
618 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700619 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700620 }
Jeff Brown349703e2010-06-22 01:27:15 -0700621
Jeff Brown9302c872011-07-13 22:51:29 -0700622 mInputManager->getDispatcher()->setInputWindows(windowHandles);
623
624 // Do this after the dispatcher has updated the window handle state.
625 bool newPointerGesturesEnabled = true;
626 size_t numWindows = windowHandles.size();
627 for (size_t i = 0; i < numWindows; i++) {
628 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700629 const InputWindowInfo* windowInfo = windowHandle->getInfo();
630 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
631 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700632 newPointerGesturesEnabled = false;
633 }
634 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700635
636 uint32_t changes = 0;
637 { // acquire lock
638 AutoMutex _l(mLock);
639
640 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
641 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
642 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
643 }
644 } // release lock
645
646 if (changes) {
647 mInputManager->getReader()->requestRefreshConfiguration(changes);
648 }
Jeff Brown349703e2010-06-22 01:27:15 -0700649}
650
Jeff Brown9302c872011-07-13 22:51:29 -0700651void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
652 sp<InputApplicationHandle> applicationHandle =
653 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
654 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700655}
656
657void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700658 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700659}
660
Jeff Brown05dc66a2011-03-02 14:41:58 -0800661void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
662 AutoMutex _l(mLock);
663
664 if (mLocked.systemUiVisibility != visibility) {
665 mLocked.systemUiVisibility = visibility;
666
667 sp<PointerController> controller = mLocked.pointerController.promote();
668 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700669 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800670 }
671 }
672}
673
Jeff Brown2352b972011-04-12 22:39:53 -0700674void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800675 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700676 controller->setInactivityTimeout(lightsOut
677 ? PointerController::INACTIVITY_TIMEOUT_SHORT
678 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800679}
680
Jeff Brown1a84fd12011-06-02 01:26:32 -0700681void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700682 { // acquire lock
683 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700684
Jeff Brown474dcb52011-06-14 20:22:50 -0700685 if (mLocked.pointerSpeed == speed) {
686 return;
687 }
688
Jeff Brown1a84fd12011-06-02 01:26:32 -0700689 LOGI("Setting pointer speed to %d.", speed);
690 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700691 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700692
Jeff Brown474dcb52011-06-14 20:22:50 -0700693 mInputManager->getReader()->requestRefreshConfiguration(
694 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700695}
696
Jeff Browndaf4a122011-08-26 17:14:14 -0700697void NativeInputManager::setShowTouches(bool enabled) {
698 { // acquire lock
699 AutoMutex _l(mLock);
700
701 if (mLocked.showTouches == enabled) {
702 return;
703 }
704
705 LOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
706 mLocked.showTouches = enabled;
707 } // release lock
708
709 mInputManager->getReader()->requestRefreshConfiguration(
710 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
711}
712
Jeff Browne20c9e02010-10-11 14:20:19 -0700713bool NativeInputManager::isScreenOn() {
714 return android_server_PowerManagerService_isScreenOn();
715}
716
717bool NativeInputManager::isScreenBright() {
718 return android_server_PowerManagerService_isScreenBright();
719}
720
Jeff Brown0029c662011-03-30 02:25:18 -0700721bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
722 jobject inputEventObj;
723
724 JNIEnv* env = jniEnv();
725 switch (inputEvent->getType()) {
726 case AINPUT_EVENT_TYPE_KEY:
727 inputEventObj = android_view_KeyEvent_fromNative(env,
728 static_cast<const KeyEvent*>(inputEvent));
729 break;
730 case AINPUT_EVENT_TYPE_MOTION:
731 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
732 static_cast<const MotionEvent*>(inputEvent));
733 break;
734 default:
735 return true; // dispatch the event normally
736 }
737
738 if (!inputEventObj) {
739 LOGE("Failed to obtain input event object for filterInputEvent.");
740 return true; // dispatch the event normally
741 }
742
743 // The callee is responsible for recycling the event.
744 jboolean pass = env->CallBooleanMethod(mCallbacksObj, gCallbacksClassInfo.filterInputEvent,
745 inputEventObj, policyFlags);
746 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
747 pass = true;
748 }
749 env->DeleteLocalRef(inputEventObj);
750 return pass;
751}
752
Jeff Brown1f245102010-11-18 20:53:46 -0800753void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
754 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700755 // Policy:
756 // - Ignore untrusted events and pass them along.
757 // - Ask the window manager what to do with normal events and trusted injected events.
758 // - For normal events wake and brighten the screen if currently off or dim.
759 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800760 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700761 bool isScreenOn = this->isScreenOn();
762 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700763
Jeff Brown3122e442010-10-11 23:32:49 -0700764 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800765 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
766 jint wmActions;
767 if (keyEventObj) {
768 wmActions = env->CallIntMethod(mCallbacksObj,
769 gCallbacksClassInfo.interceptKeyBeforeQueueing,
770 keyEventObj, policyFlags, isScreenOn);
771 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
772 wmActions = 0;
773 }
774 android_view_KeyEvent_recycle(env, keyEventObj);
775 env->DeleteLocalRef(keyEventObj);
776 } else {
777 LOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700778 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700779 }
780
Jeff Brown1f245102010-11-18 20:53:46 -0800781 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700782 if (!isScreenOn) {
783 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700784 }
785
786 if (!isScreenBright) {
787 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
788 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700789 }
790
Jeff Brown56194eb2011-03-02 19:23:13 -0800791 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700792 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700793 policyFlags |= POLICY_FLAG_PASS_TO_USER;
794 }
795}
796
Jeff Brown56194eb2011-03-02 19:23:13 -0800797void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700798 // Policy:
799 // - Ignore untrusted events and pass them along.
800 // - No special filtering for injected events required at this time.
801 // - Filter normal events based on screen state.
802 // - For normal events brighten (but do not wake) the screen if currently dim.
803 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
804 if (isScreenOn()) {
805 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700806
Jeff Brown3122e442010-10-11 23:32:49 -0700807 if (!isScreenBright()) {
808 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
809 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800810 } else {
811 JNIEnv* env = jniEnv();
812 jint wmActions = env->CallIntMethod(mCallbacksObj,
813 gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
814 policyFlags);
815 if (checkAndClearExceptionFromCallback(env,
816 "interceptMotionBeforeQueueingWhenScreenOff")) {
817 wmActions = 0;
818 }
819
820 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
821 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700822 }
Jeff Brown3122e442010-10-11 23:32:49 -0700823 } else {
824 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700825 }
826}
827
Jeff Brown56194eb2011-03-02 19:23:13 -0800828void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
829 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800830 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800831#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000832 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800833#endif
834 android_server_PowerManagerService_goToSleep(when);
835 }
836
837 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800838#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000839 ALOGD("handleInterceptActions: Poking user activity.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800840#endif
841 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
842 }
843
844 if (wmActions & WM_ACTION_PASS_TO_USER) {
845 policyFlags |= POLICY_FLAG_PASS_TO_USER;
846 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800847#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000848 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800849#endif
850 }
851}
852
Jeff Brown905805a2011-10-12 13:57:59 -0700853nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800854 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700855 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700856 // Policy:
857 // - Ignore untrusted events and pass them along.
858 // - Filter normal events and trusted injected events through the window manager policy to
859 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700860 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700861 if (policyFlags & POLICY_FLAG_TRUSTED) {
862 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700863
Jeff Brown928e0542011-01-10 11:17:36 -0800864 // Note: inputWindowHandle may be null.
865 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800866 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
867 if (keyEventObj) {
Jeff Brown905805a2011-10-12 13:57:59 -0700868 jlong delayMillis = env->CallLongMethod(mCallbacksObj,
Jeff Brown1f245102010-11-18 20:53:46 -0800869 gCallbacksClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800870 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800871 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
872 android_view_KeyEvent_recycle(env, keyEventObj);
873 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700874 if (!error) {
875 if (delayMillis < 0) {
876 result = -1;
877 } else if (delayMillis > 0) {
878 result = milliseconds_to_nanoseconds(delayMillis);
879 }
880 }
Jeff Brown1f245102010-11-18 20:53:46 -0800881 } else {
882 LOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800883 }
Jeff Brown928e0542011-01-10 11:17:36 -0800884 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700885 }
Jeff Brown1f245102010-11-18 20:53:46 -0800886 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700887}
888
Jeff Brown928e0542011-01-10 11:17:36 -0800889bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800890 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700891 // Policy:
892 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800893 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700894 if (policyFlags & POLICY_FLAG_TRUSTED) {
895 JNIEnv* env = jniEnv();
896
Jeff Brown928e0542011-01-10 11:17:36 -0800897 // Note: inputWindowHandle may be null.
898 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800899 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
900 if (keyEventObj) {
Jeff Brown49ed71d2010-12-06 17:13:33 -0800901 jobject fallbackKeyEventObj = env->CallObjectMethod(mCallbacksObj,
Jeff Brown1f245102010-11-18 20:53:46 -0800902 gCallbacksClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800903 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700904 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
905 fallbackKeyEventObj = NULL;
906 }
Jeff Brown1f245102010-11-18 20:53:46 -0800907 android_view_KeyEvent_recycle(env, keyEventObj);
908 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800909
910 if (fallbackKeyEventObj) {
911 // Note: outFallbackKeyEvent may be the same object as keyEvent.
912 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
913 outFallbackKeyEvent)) {
914 result = true;
915 }
916 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
917 env->DeleteLocalRef(fallbackKeyEventObj);
918 }
Jeff Brown1f245102010-11-18 20:53:46 -0800919 } else {
920 LOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800921 }
Jeff Brown928e0542011-01-10 11:17:36 -0800922 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700923 }
Jeff Brown1f245102010-11-18 20:53:46 -0800924 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700925}
926
Jeff Brown01ce2e92010-09-26 22:20:12 -0700927void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
928 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700929}
930
Jeff Brown349703e2010-06-22 01:27:15 -0700931
Jeff Brownb88102f2010-09-08 11:49:43 -0700932bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
933 int32_t injectorPid, int32_t injectorUid) {
934 JNIEnv* env = jniEnv();
935 jboolean result = env->CallBooleanMethod(mCallbacksObj,
936 gCallbacksClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700937 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
938 result = false;
939 }
Jeff Brown349703e2010-06-22 01:27:15 -0700940 return result;
941}
942
Jeff Brown2352b972011-04-12 22:39:53 -0700943void NativeInputManager::loadPointerResources(PointerResources* outResources) {
944 JNIEnv* env = jniEnv();
945
946 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
947 &outResources->spotHover);
948 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
949 &outResources->spotTouch);
950 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
951 &outResources->spotAnchor);
952}
953
Jeff Brown83c09682010-12-23 17:50:18 -0800954
Jeff Brown9c3cda02010-06-15 01:31:58 -0700955// ----------------------------------------------------------------------------
956
957static sp<NativeInputManager> gNativeInputManager;
958
Jeff Brown46b9ac02010-04-22 18:58:52 -0700959static bool checkInputManagerUnitialized(JNIEnv* env) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700960 if (gNativeInputManager == NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700961 LOGE("Input manager not initialized.");
962 jniThrowRuntimeException(env, "Input manager not initialized.");
963 return true;
964 }
965 return false;
966}
967
968static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700969 jobject contextObj, jobject callbacksObj, jobject messageQueueObj) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700970 if (gNativeInputManager == NULL) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800971 sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);
Jeff Brown2352b972011-04-12 22:39:53 -0700972 gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700973 } else {
974 LOGE("Input manager already initialized.");
975 jniThrowRuntimeException(env, "Input manager already initialized.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700976 }
977}
978
979static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {
980 if (checkInputManagerUnitialized(env)) {
981 return;
982 }
983
Jeff Brown9c3cda02010-06-15 01:31:58 -0700984 status_t result = gNativeInputManager->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700985 if (result) {
986 jniThrowRuntimeException(env, "Input manager could not be started.");
987 }
988}
989
990static void android_server_InputManager_nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Jeff Brownbc68a592011-07-25 12:58:12 -0700991 jint displayId, jint width, jint height, jint externalWidth, jint externalHeight) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700992 if (checkInputManagerUnitialized(env)) {
993 return;
994 }
995
996 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
997 // to be passed in like this, not sure which is better but leaving it like this
998 // keeps the window manager in direct control of when display transitions propagate down
999 // to the input dispatcher
Jeff Brownbc68a592011-07-25 12:58:12 -07001000 gNativeInputManager->setDisplaySize(displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001001}
1002
1003static void android_server_InputManager_nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
1004 jint displayId, jint orientation) {
1005 if (checkInputManagerUnitialized(env)) {
1006 return;
1007 }
1008
Jeff Brown9c3cda02010-06-15 01:31:58 -07001009 gNativeInputManager->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001010}
1011
1012static jint android_server_InputManager_nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001013 jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001014 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001015 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001016 }
1017
Jeff Brownb88102f2010-09-08 11:49:43 -07001018 return gNativeInputManager->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001020}
1021
1022static jint android_server_InputManager_nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001023 jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001024 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001025 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001026 }
1027
Jeff Brownb88102f2010-09-08 11:49:43 -07001028 return gNativeInputManager->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001029 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001030}
1031
1032static jint android_server_InputManager_nativeGetSwitchState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001033 jint deviceId, jint sourceMask, jint sw) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001034 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001035 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001036 }
1037
Jeff Brownb88102f2010-09-08 11:49:43 -07001038 return gNativeInputManager->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001039 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001040}
1041
1042static jboolean android_server_InputManager_nativeHasKeys(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001043 jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001044 if (checkInputManagerUnitialized(env)) {
1045 return JNI_FALSE;
1046 }
1047
1048 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1049 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1050 jsize numCodes = env->GetArrayLength(keyCodes);
1051 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001052 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001053 result = gNativeInputManager->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001054 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001055 } else {
1056 result = JNI_FALSE;
1057 }
1058
1059 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1060 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1061 return result;
1062}
1063
1064static void throwInputChannelNotInitialized(JNIEnv* env) {
1065 jniThrowException(env, "java/lang/IllegalStateException",
1066 "inputChannel is not initialized");
1067}
1068
1069static void android_server_InputManager_handleInputChannelDisposed(JNIEnv* env,
1070 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
1071 LOGW("Input channel object '%s' was disposed without first being unregistered with "
1072 "the input manager!", inputChannel->getName().string());
1073
Jeff Brown9c3cda02010-06-15 01:31:58 -07001074 if (gNativeInputManager != NULL) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001075 gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001076 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001077}
1078
1079static void android_server_InputManager_nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001080 jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001081 if (checkInputManagerUnitialized(env)) {
1082 return;
1083 }
1084
1085 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1086 inputChannelObj);
1087 if (inputChannel == NULL) {
1088 throwInputChannelNotInitialized(env);
1089 return;
1090 }
1091
Jeff Brown928e0542011-01-10 11:17:36 -08001092 sp<InputWindowHandle> inputWindowHandle =
1093 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001094
1095 status_t status = gNativeInputManager->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001096 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001097 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001098 String8 message;
1099 message.appendFormat("Failed to register input channel. status=%d", status);
1100 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101 return;
1102 }
1103
Jeff Browna41ca772010-08-11 14:46:32 -07001104 if (! monitor) {
1105 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
1106 android_server_InputManager_handleInputChannelDisposed, NULL);
1107 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001108}
1109
1110static void android_server_InputManager_nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1111 jobject inputChannelObj) {
1112 if (checkInputManagerUnitialized(env)) {
1113 return;
1114 }
1115
1116 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1117 inputChannelObj);
1118 if (inputChannel == NULL) {
1119 throwInputChannelNotInitialized(env);
1120 return;
1121 }
1122
1123 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1124
Jeff Brown7fbdc842010-06-17 20:52:56 -07001125 status_t status = gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001126 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1127 String8 message;
1128 message.appendFormat("Failed to unregister input channel. status=%d", status);
1129 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001130 }
1131}
1132
Jeff Brown0029c662011-03-30 02:25:18 -07001133static void android_server_InputManager_nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
1134 jboolean enabled) {
1135 if (checkInputManagerUnitialized(env)) {
1136 return;
1137 }
1138
1139 gNativeInputManager->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
1140}
1141
Jeff Brown6ec402b2010-07-28 15:48:59 -07001142static jint android_server_InputManager_nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1143 jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001144 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001145 if (checkInputManagerUnitialized(env)) {
1146 return INPUT_EVENT_INJECTION_FAILED;
1147 }
1148
Jeff Brown6ec402b2010-07-28 15:48:59 -07001149 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1150 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001151 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1152 if (status) {
1153 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1154 return INPUT_EVENT_INJECTION_FAILED;
1155 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001156
Jeff Brownb88102f2010-09-08 11:49:43 -07001157 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001158 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1159 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001160 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001161 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1162 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001163 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1164 return INPUT_EVENT_INJECTION_FAILED;
1165 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001166
Jeff Brownb88102f2010-09-08 11:49:43 -07001167 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001168 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1169 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001170 } else {
1171 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001172 return INPUT_EVENT_INJECTION_FAILED;
1173 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001174}
1175
Jeff Brown349703e2010-06-22 01:27:15 -07001176static void android_server_InputManager_nativeSetInputWindows(JNIEnv* env, jclass clazz,
Jeff Brown9302c872011-07-13 22:51:29 -07001177 jobjectArray windowHandleObjArray) {
Jeff Brown349703e2010-06-22 01:27:15 -07001178 if (checkInputManagerUnitialized(env)) {
1179 return;
1180 }
1181
Jeff Brown9302c872011-07-13 22:51:29 -07001182 gNativeInputManager->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001183}
1184
1185static void android_server_InputManager_nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Jeff Brown9302c872011-07-13 22:51:29 -07001186 jobject applicationHandleObj) {
Jeff Brown349703e2010-06-22 01:27:15 -07001187 if (checkInputManagerUnitialized(env)) {
1188 return;
1189 }
1190
Jeff Brown9302c872011-07-13 22:51:29 -07001191 gNativeInputManager->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001192}
1193
1194static void android_server_InputManager_nativeSetInputDispatchMode(JNIEnv* env,
1195 jclass clazz, jboolean enabled, jboolean frozen) {
1196 if (checkInputManagerUnitialized(env)) {
1197 return;
1198 }
1199
1200 gNativeInputManager->setInputDispatchMode(enabled, frozen);
1201}
1202
Jeff Brown05dc66a2011-03-02 14:41:58 -08001203static void android_server_InputManager_nativeSetSystemUiVisibility(JNIEnv* env,
1204 jclass clazz, jint visibility) {
1205 if (checkInputManagerUnitialized(env)) {
1206 return;
1207 }
1208
1209 gNativeInputManager->setSystemUiVisibility(visibility);
1210}
1211
Jeff Brown8d608662010-08-30 03:02:23 -07001212static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env,
1213 jclass clazz, jint deviceId) {
1214 if (checkInputManagerUnitialized(env)) {
1215 return NULL;
1216 }
1217
1218 InputDeviceInfo deviceInfo;
Jeff Brownb88102f2010-09-08 11:49:43 -07001219 status_t status = gNativeInputManager->getInputManager()->getReader()->getInputDeviceInfo(
Jeff Brown8d608662010-08-30 03:02:23 -07001220 deviceId, & deviceInfo);
1221 if (status) {
1222 return NULL;
1223 }
1224
1225 jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor);
1226 if (! deviceObj) {
1227 return NULL;
1228 }
1229
1230 jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string());
1231 if (! deviceNameObj) {
1232 return NULL;
1233 }
1234
Jeff Brown1e08fe92011-11-15 17:48:10 -08001235 jstring fileStr = env->NewStringUTF(deviceInfo.getKeyCharacterMapFile());
1236 if (!fileStr) {
1237 return NULL;
1238 }
1239
Jeff Brown8d608662010-08-30 03:02:23 -07001240 env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
1241 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
1242 env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
1243 env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
Jeff Brown1e08fe92011-11-15 17:48:10 -08001244 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mKeyCharacterMapFile, fileStr);
Jeff Brown8d608662010-08-30 03:02:23 -07001245
Jeff Brownefd32662011-03-08 15:13:06 -08001246 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Brown8d608662010-08-30 03:02:23 -07001247 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -08001248 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
Jeff Brown8d608662010-08-30 03:02:23 -07001249 env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange,
Jeff Brownefd32662011-03-08 15:13:06 -08001250 range.axis, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Brown8d608662010-08-30 03:02:23 -07001251 if (env->ExceptionCheck()) {
1252 return NULL;
1253 }
1254 }
1255
1256 return deviceObj;
1257}
1258
1259static jintArray android_server_InputManager_nativeGetInputDeviceIds(JNIEnv* env,
1260 jclass clazz) {
1261 if (checkInputManagerUnitialized(env)) {
1262 return NULL;
1263 }
1264
1265 Vector<int> deviceIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001266 gNativeInputManager->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
Jeff Brown8d608662010-08-30 03:02:23 -07001267
1268 jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
1269 if (! deviceIdsObj) {
1270 return NULL;
1271 }
1272
1273 env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
1274 return deviceIdsObj;
1275}
1276
Jeff Brown57c59372010-09-21 18:22:55 -07001277static void android_server_InputManager_nativeGetInputConfiguration(JNIEnv* env,
1278 jclass clazz, jobject configObj) {
1279 if (checkInputManagerUnitialized(env)) {
1280 return;
1281 }
1282
1283 InputConfiguration config;
1284 gNativeInputManager->getInputManager()->getReader()->getInputConfiguration(& config);
1285
1286 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1287 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1288 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1289}
1290
Jeff Browne6504122010-09-27 14:52:15 -07001291static jboolean android_server_InputManager_nativeTransferTouchFocus(JNIEnv* env,
1292 jclass clazz, jobject fromChannelObj, jobject toChannelObj) {
1293 if (checkInputManagerUnitialized(env)) {
1294 return false;
1295 }
1296
1297 sp<InputChannel> fromChannel =
1298 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1299 sp<InputChannel> toChannel =
1300 android_view_InputChannel_getInputChannel(env, toChannelObj);
1301
1302 if (fromChannel == NULL || toChannel == NULL) {
1303 return false;
1304 }
1305
1306 return gNativeInputManager->getInputManager()->getDispatcher()->
1307 transferTouchFocus(fromChannel, toChannel);
1308}
1309
Jeff Brown1a84fd12011-06-02 01:26:32 -07001310static void android_server_InputManager_nativeSetPointerSpeed(JNIEnv* env,
1311 jclass clazz, jint speed) {
1312 if (checkInputManagerUnitialized(env)) {
1313 return;
1314 }
1315
1316 gNativeInputManager->setPointerSpeed(speed);
1317}
1318
Jeff Browndaf4a122011-08-26 17:14:14 -07001319static void android_server_InputManager_nativeSetShowTouches(JNIEnv* env,
1320 jclass clazz, jboolean enabled) {
1321 if (checkInputManagerUnitialized(env)) {
1322 return;
1323 }
1324
1325 gNativeInputManager->setShowTouches(enabled);
1326}
1327
Jeff Browne33348b2010-07-15 23:54:05 -07001328static jstring android_server_InputManager_nativeDump(JNIEnv* env, jclass clazz) {
1329 if (checkInputManagerUnitialized(env)) {
1330 return NULL;
1331 }
1332
Jeff Brownb88102f2010-09-08 11:49:43 -07001333 String8 dump;
1334 gNativeInputManager->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001335 return env->NewStringUTF(dump.string());
1336}
1337
Jeff Brown89ef0722011-08-10 16:25:21 -07001338static void android_server_InputManager_nativeMonitor(JNIEnv* env, jclass clazz) {
1339 if (checkInputManagerUnitialized(env)) {
1340 return;
1341 }
1342
1343 gNativeInputManager->getInputManager()->getReader()->monitor();
1344 gNativeInputManager->getInputManager()->getDispatcher()->monitor();
1345}
1346
Jeff Brown9c3cda02010-06-15 01:31:58 -07001347// ----------------------------------------------------------------------------
1348
Jeff Brown46b9ac02010-04-22 18:58:52 -07001349static JNINativeMethod gInputManagerMethods[] = {
1350 /* name, signature, funcPtr */
Jeff Brown2352b972011-04-12 22:39:53 -07001351 { "nativeInit", "(Landroid/content/Context;"
1352 "Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001353 (void*) android_server_InputManager_nativeInit },
1354 { "nativeStart", "()V",
1355 (void*) android_server_InputManager_nativeStart },
Jeff Brownbc68a592011-07-25 12:58:12 -07001356 { "nativeSetDisplaySize", "(IIIII)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001357 (void*) android_server_InputManager_nativeSetDisplaySize },
1358 { "nativeSetDisplayOrientation", "(II)V",
1359 (void*) android_server_InputManager_nativeSetDisplayOrientation },
1360 { "nativeGetScanCodeState", "(III)I",
1361 (void*) android_server_InputManager_nativeGetScanCodeState },
1362 { "nativeGetKeyCodeState", "(III)I",
1363 (void*) android_server_InputManager_nativeGetKeyCodeState },
1364 { "nativeGetSwitchState", "(III)I",
1365 (void*) android_server_InputManager_nativeGetSwitchState },
Jeff Brown6d0fec22010-07-23 21:28:06 -07001366 { "nativeHasKeys", "(II[I[Z)Z",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001367 (void*) android_server_InputManager_nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001368 { "nativeRegisterInputChannel",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001369 "(Landroid/view/InputChannel;Lcom/android/server/wm/InputWindowHandle;Z)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001370 (void*) android_server_InputManager_nativeRegisterInputChannel },
1371 { "nativeUnregisterInputChannel", "(Landroid/view/InputChannel;)V",
Jeff Brown7fbdc842010-06-17 20:52:56 -07001372 (void*) android_server_InputManager_nativeUnregisterInputChannel },
Jeff Brown0029c662011-03-30 02:25:18 -07001373 { "nativeSetInputFilterEnabled", "(Z)V",
1374 (void*) android_server_InputManager_nativeSetInputFilterEnabled },
1375 { "nativeInjectInputEvent", "(Landroid/view/InputEvent;IIIII)I",
Jeff Brown6ec402b2010-07-28 15:48:59 -07001376 (void*) android_server_InputManager_nativeInjectInputEvent },
Jeff Brown9302c872011-07-13 22:51:29 -07001377 { "nativeSetInputWindows", "([Lcom/android/server/wm/InputWindowHandle;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001378 (void*) android_server_InputManager_nativeSetInputWindows },
Jeff Brown9302c872011-07-13 22:51:29 -07001379 { "nativeSetFocusedApplication", "(Lcom/android/server/wm/InputApplicationHandle;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001380 (void*) android_server_InputManager_nativeSetFocusedApplication },
1381 { "nativeSetInputDispatchMode", "(ZZ)V",
1382 (void*) android_server_InputManager_nativeSetInputDispatchMode },
Jeff Brown05dc66a2011-03-02 14:41:58 -08001383 { "nativeSetSystemUiVisibility", "(I)V",
1384 (void*) android_server_InputManager_nativeSetSystemUiVisibility },
Jeff Brown8d608662010-08-30 03:02:23 -07001385 { "nativeGetInputDevice", "(I)Landroid/view/InputDevice;",
1386 (void*) android_server_InputManager_nativeGetInputDevice },
1387 { "nativeGetInputDeviceIds", "()[I",
1388 (void*) android_server_InputManager_nativeGetInputDeviceIds },
Jeff Brown57c59372010-09-21 18:22:55 -07001389 { "nativeGetInputConfiguration", "(Landroid/content/res/Configuration;)V",
1390 (void*) android_server_InputManager_nativeGetInputConfiguration },
Jeff Browne6504122010-09-27 14:52:15 -07001391 { "nativeTransferTouchFocus", "(Landroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1392 (void*) android_server_InputManager_nativeTransferTouchFocus },
Jeff Brown1a84fd12011-06-02 01:26:32 -07001393 { "nativeSetPointerSpeed", "(I)V",
1394 (void*) android_server_InputManager_nativeSetPointerSpeed },
Jeff Browndaf4a122011-08-26 17:14:14 -07001395 { "nativeSetShowTouches", "(Z)V",
1396 (void*) android_server_InputManager_nativeSetShowTouches },
Jeff Browne33348b2010-07-15 23:54:05 -07001397 { "nativeDump", "()Ljava/lang/String;",
1398 (void*) android_server_InputManager_nativeDump },
Jeff Brown89ef0722011-08-10 16:25:21 -07001399 { "nativeMonitor", "()V",
1400 (void*) android_server_InputManager_nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001401};
1402
1403#define FIND_CLASS(var, className) \
1404 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001405 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001406
1407#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1408 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1409 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1410
1411#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1412 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1413 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1414
1415int register_android_server_InputManager(JNIEnv* env) {
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001416 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputManager",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001417 gInputManagerMethods, NELEM(gInputManagerMethods));
1418 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1419
Jeff Brown9c3cda02010-06-15 01:31:58 -07001420 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001421
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001422 jclass clazz;
1423 FIND_CLASS(clazz, "com/android/server/wm/InputManager$Callbacks");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001424
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001425 GET_METHOD_ID(gCallbacksClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001426 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001427
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001428 GET_METHOD_ID(gCallbacksClassInfo.notifyLidSwitchChanged, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001429 "notifyLidSwitchChanged", "(JZ)V");
1430
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001431 GET_METHOD_ID(gCallbacksClassInfo.notifyInputChannelBroken, clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001432 "notifyInputChannelBroken", "(Lcom/android/server/wm/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001433
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001434 GET_METHOD_ID(gCallbacksClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001435 "notifyANR",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001436 "(Lcom/android/server/wm/InputApplicationHandle;Lcom/android/server/wm/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001437
Jeff Brown0029c662011-03-30 02:25:18 -07001438 GET_METHOD_ID(gCallbacksClassInfo.filterInputEvent, clazz,
1439 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1440
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001441 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001442 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001443
Jeff Brown56194eb2011-03-02 19:23:13 -08001444 GET_METHOD_ID(gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001445 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001446 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1447
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001448 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001449 "interceptKeyBeforeDispatching",
Jeff Brown905805a2011-10-12 13:57:59 -07001450 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001451
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001452 GET_METHOD_ID(gCallbacksClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001453 "dispatchUnhandledKey",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001454 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001455
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001456 GET_METHOD_ID(gCallbacksClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001457 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001458
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001459 GET_METHOD_ID(gCallbacksClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001460 "getVirtualKeyQuietTimeMillis", "()I");
1461
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001462 GET_METHOD_ID(gCallbacksClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001463 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1464
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001465 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001466 "getKeyRepeatTimeout", "()I");
1467
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001468 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001469 "getKeyRepeatDelay", "()I");
1470
Jeff Brown774ed9d2011-06-07 17:48:39 -07001471 GET_METHOD_ID(gCallbacksClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001472 "getHoverTapTimeout", "()I");
1473
Jeff Brown774ed9d2011-06-07 17:48:39 -07001474 GET_METHOD_ID(gCallbacksClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001475 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001476
Dianne Hackbornf3b57de2011-06-03 12:13:24 -07001477 GET_METHOD_ID(gCallbacksClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001478 "getDoubleTapTimeout", "()I");
1479
Dianne Hackbornf3b57de2011-06-03 12:13:24 -07001480 GET_METHOD_ID(gCallbacksClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001481 "getLongPressTimeout", "()I");
1482
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001483 GET_METHOD_ID(gCallbacksClassInfo.getMaxEventsPerSecond, clazz,
Jeff Brownae9fc032010-08-18 15:51:08 -07001484 "getMaxEventsPerSecond", "()I");
1485
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001486 GET_METHOD_ID(gCallbacksClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001487 "getPointerLayer", "()I");
1488
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001489 GET_METHOD_ID(gCallbacksClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001490 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001491
Jeff Brown6ec402b2010-07-28 15:48:59 -07001492 // KeyEvent
1493
1494 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001495 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1496
Jeff Brown6ec402b2010-07-28 15:48:59 -07001497
Jeff Brown8d608662010-08-30 03:02:23 -07001498 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001499
1500 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001501 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001502
Jeff Brown8d608662010-08-30 03:02:23 -07001503 // InputDevice
1504
1505 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001506 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
Jeff Brown8d608662010-08-30 03:02:23 -07001507
1508 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
1509 "<init>", "()V");
1510
1511 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
Jeff Brownefd32662011-03-08 15:13:06 -08001512 "addMotionRange", "(IIFFFF)V");
Jeff Brown8d608662010-08-30 03:02:23 -07001513
1514 GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz,
1515 "mId", "I");
1516
1517 GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz,
1518 "mName", "Ljava/lang/String;");
1519
1520 GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz,
1521 "mSources", "I");
1522
1523 GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
1524 "mKeyboardType", "I");
1525
Jeff Brown1e08fe92011-11-15 17:48:10 -08001526 GET_FIELD_ID(gInputDeviceClassInfo.mKeyCharacterMapFile, gInputDeviceClassInfo.clazz,
1527 "mKeyCharacterMapFile", "Ljava/lang/String;");
1528
Jeff Brown57c59372010-09-21 18:22:55 -07001529 // Configuration
1530
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001531 FIND_CLASS(clazz, "android/content/res/Configuration");
Jeff Brown57c59372010-09-21 18:22:55 -07001532
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001533 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001534 "touchscreen", "I");
1535
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001536 GET_FIELD_ID(gConfigurationClassInfo.keyboard, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001537 "keyboard", "I");
1538
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001539 GET_FIELD_ID(gConfigurationClassInfo.navigation, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001540 "navigation", "I");
1541
Jeff Brown46b9ac02010-04-22 18:58:52 -07001542 return 0;
1543}
1544
Jeff Brown46b9ac02010-04-22 18:58:52 -07001545} /* namespace android */