blob: 75c20f3db5ab8784728bdc2c274c3a47f872a7b9 [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 Brown4532e612012-04-05 14:27:12 -070049#include "com_android_server_input_InputApplicationHandle.h"
50#include "com_android_server_input_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 Brownbb3fcba0c2011-06-06 19:23:05 -070074 jmethodID getHoverTapTimeout;
75 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070076 jmethodID getDoubleTapTimeout;
77 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080078 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080079 jmethodID getPointerIcon;
Jeff Brown4532e612012-04-05 14:27:12 -070080} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070081
82static struct {
83 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070084} gKeyEventClassInfo;
85
86static struct {
87 jclass clazz;
88} gMotionEventClassInfo;
89
Jeff Brown8d608662010-08-30 03:02:23 -070090static struct {
91 jclass clazz;
92
93 jmethodID ctor;
94 jmethodID addMotionRange;
95
96 jfieldID mId;
97 jfieldID mName;
Jeff Browne38fdfa2012-04-06 14:51:01 -070098 jfieldID mDescriptor;
Jeff Brown8d608662010-08-30 03:02:23 -070099 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 Brown4532e612012-04-05 14:27:12 -0700170 NativeInputManager(jobject contextObj, jobject serviceObj, 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 Brown4532e612012-04-05 14:27:12 -0700226 jobject mServiceObj;
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,
Jeff Brown4532e612012-04-05 14:27:12 -0700273 jobject serviceObj, 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 Brown4532e612012-04-05 14:27:12 -0700278 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700279
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 Brown4532e612012-04-05 14:27:12 -0700302 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700303}
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()) {
Steve Block3762c312012-01-06 19:20:56 +0000315 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316 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 Brown4532e612012-04-05 14:27:12 -0700391 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
392 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700393 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
394 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
395 }
396
397 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700398 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
399 gServiceClassInfo.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 Brown4532e612012-04-05 14:27:12 -0700412 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
413 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700414 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700415 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
416 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700417 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700418 jint longPressTimeout = env->CallIntMethod(mServiceObj,
419 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700420 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 Brown4532e612012-04-05 14:27:12 -0700434 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
435 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700436 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 Brown4532e612012-04-05 14:27:12 -0700471 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
472 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700473 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();
Jeff Brown4532e612012-04-05 14:27:12 -0700494 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700495 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:
Jeff Brown4532e612012-04-05 14:27:12 -0700513 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyLidSwitchChanged,
Jeff Browne20c9e02010-10-11 14:20:19 -0700514 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 Brown4532e612012-04-05 14:27:12 -0700527 env->CallVoidMethod(mServiceObj, gServiceClassInfo.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 Brown4532e612012-04-05 14:27:12 -0700544 jlong newTimeout = env->CallLongMethod(mServiceObj,
545 gServiceClassInfo.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 Brown4532e612012-04-05 14:27:12 -0700567 env->CallVoidMethod(mServiceObj, gServiceClassInfo.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 Brown4532e612012-04-05 14:27:12 -0700578 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
579 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700580 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
581 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
582 }
Jeff Browna4547672011-03-02 21:38:11 -0800583
Jeff Brown4532e612012-04-05 14:27:12 -0700584 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
585 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700586 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
587 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
588 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700589}
590
Jeff Brown214eaf42011-05-26 19:17:02 -0700591bool NativeInputManager::isKeyRepeatEnabled() {
592 // Only enable automatic key repeating when the screen is on.
593 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700594}
595
Jeff Brown9302c872011-07-13 22:51:29 -0700596void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
597 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700598
Jeff Brown9302c872011-07-13 22:51:29 -0700599 if (windowHandleObjArray) {
600 jsize length = env->GetArrayLength(windowHandleObjArray);
601 for (jsize i = 0; i < length; i++) {
602 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
603 if (! windowHandleObj) {
604 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700605 }
Jeff Brown9302c872011-07-13 22:51:29 -0700606
607 sp<InputWindowHandle> windowHandle =
608 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
609 if (windowHandle != NULL) {
610 windowHandles.push(windowHandle);
611 }
612 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700613 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700614 }
Jeff Brown349703e2010-06-22 01:27:15 -0700615
Jeff Brown9302c872011-07-13 22:51:29 -0700616 mInputManager->getDispatcher()->setInputWindows(windowHandles);
617
618 // Do this after the dispatcher has updated the window handle state.
619 bool newPointerGesturesEnabled = true;
620 size_t numWindows = windowHandles.size();
621 for (size_t i = 0; i < numWindows; i++) {
622 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700623 const InputWindowInfo* windowInfo = windowHandle->getInfo();
624 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
625 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700626 newPointerGesturesEnabled = false;
627 }
628 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700629
630 uint32_t changes = 0;
631 { // acquire lock
632 AutoMutex _l(mLock);
633
634 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
635 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
636 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
637 }
638 } // release lock
639
640 if (changes) {
641 mInputManager->getReader()->requestRefreshConfiguration(changes);
642 }
Jeff Brown349703e2010-06-22 01:27:15 -0700643}
644
Jeff Brown9302c872011-07-13 22:51:29 -0700645void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
646 sp<InputApplicationHandle> applicationHandle =
647 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
648 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700649}
650
651void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700652 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700653}
654
Jeff Brown05dc66a2011-03-02 14:41:58 -0800655void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
656 AutoMutex _l(mLock);
657
658 if (mLocked.systemUiVisibility != visibility) {
659 mLocked.systemUiVisibility = visibility;
660
661 sp<PointerController> controller = mLocked.pointerController.promote();
662 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700663 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800664 }
665 }
666}
667
Jeff Brown2352b972011-04-12 22:39:53 -0700668void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800669 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700670 controller->setInactivityTimeout(lightsOut
671 ? PointerController::INACTIVITY_TIMEOUT_SHORT
672 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800673}
674
Jeff Brown1a84fd12011-06-02 01:26:32 -0700675void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700676 { // acquire lock
677 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700678
Jeff Brown474dcb52011-06-14 20:22:50 -0700679 if (mLocked.pointerSpeed == speed) {
680 return;
681 }
682
Steve Block6215d3f2012-01-04 20:05:49 +0000683 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700684 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700685 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700686
Jeff Brown474dcb52011-06-14 20:22:50 -0700687 mInputManager->getReader()->requestRefreshConfiguration(
688 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700689}
690
Jeff Browndaf4a122011-08-26 17:14:14 -0700691void NativeInputManager::setShowTouches(bool enabled) {
692 { // acquire lock
693 AutoMutex _l(mLock);
694
695 if (mLocked.showTouches == enabled) {
696 return;
697 }
698
Steve Block6215d3f2012-01-04 20:05:49 +0000699 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700700 mLocked.showTouches = enabled;
701 } // release lock
702
703 mInputManager->getReader()->requestRefreshConfiguration(
704 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
705}
706
Jeff Browne20c9e02010-10-11 14:20:19 -0700707bool NativeInputManager::isScreenOn() {
708 return android_server_PowerManagerService_isScreenOn();
709}
710
711bool NativeInputManager::isScreenBright() {
712 return android_server_PowerManagerService_isScreenBright();
713}
714
Jeff Brown0029c662011-03-30 02:25:18 -0700715bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
716 jobject inputEventObj;
717
718 JNIEnv* env = jniEnv();
719 switch (inputEvent->getType()) {
720 case AINPUT_EVENT_TYPE_KEY:
721 inputEventObj = android_view_KeyEvent_fromNative(env,
722 static_cast<const KeyEvent*>(inputEvent));
723 break;
724 case AINPUT_EVENT_TYPE_MOTION:
725 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
726 static_cast<const MotionEvent*>(inputEvent));
727 break;
728 default:
729 return true; // dispatch the event normally
730 }
731
732 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000733 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700734 return true; // dispatch the event normally
735 }
736
737 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700738 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700739 inputEventObj, policyFlags);
740 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
741 pass = true;
742 }
743 env->DeleteLocalRef(inputEventObj);
744 return pass;
745}
746
Jeff Brown1f245102010-11-18 20:53:46 -0800747void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
748 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700749 // Policy:
750 // - Ignore untrusted events and pass them along.
751 // - Ask the window manager what to do with normal events and trusted injected events.
752 // - For normal events wake and brighten the screen if currently off or dim.
753 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800754 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700755 bool isScreenOn = this->isScreenOn();
756 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700757
Jeff Brown3122e442010-10-11 23:32:49 -0700758 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800759 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
760 jint wmActions;
761 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700762 wmActions = env->CallIntMethod(mServiceObj,
763 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800764 keyEventObj, policyFlags, isScreenOn);
765 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
766 wmActions = 0;
767 }
768 android_view_KeyEvent_recycle(env, keyEventObj);
769 env->DeleteLocalRef(keyEventObj);
770 } else {
Steve Block3762c312012-01-06 19:20:56 +0000771 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700772 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700773 }
774
Jeff Brown1f245102010-11-18 20:53:46 -0800775 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700776 if (!isScreenOn) {
777 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700778 }
779
780 if (!isScreenBright) {
781 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
782 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700783 }
784
Jeff Brown56194eb2011-03-02 19:23:13 -0800785 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700786 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700787 policyFlags |= POLICY_FLAG_PASS_TO_USER;
788 }
789}
790
Jeff Brown56194eb2011-03-02 19:23:13 -0800791void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700792 // Policy:
793 // - Ignore untrusted events and pass them along.
794 // - No special filtering for injected events required at this time.
795 // - Filter normal events based on screen state.
796 // - For normal events brighten (but do not wake) the screen if currently dim.
797 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
798 if (isScreenOn()) {
799 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700800
Jeff Brown3122e442010-10-11 23:32:49 -0700801 if (!isScreenBright()) {
802 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
803 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800804 } else {
805 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700806 jint wmActions = env->CallIntMethod(mServiceObj,
807 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown56194eb2011-03-02 19:23:13 -0800808 policyFlags);
809 if (checkAndClearExceptionFromCallback(env,
810 "interceptMotionBeforeQueueingWhenScreenOff")) {
811 wmActions = 0;
812 }
813
814 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
815 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700816 }
Jeff Brown3122e442010-10-11 23:32:49 -0700817 } else {
818 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700819 }
820}
821
Jeff Brown56194eb2011-03-02 19:23:13 -0800822void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
823 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800824 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800825#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000826 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800827#endif
828 android_server_PowerManagerService_goToSleep(when);
829 }
830
831 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800832#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000833 ALOGD("handleInterceptActions: Poking user activity.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800834#endif
835 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
836 }
837
838 if (wmActions & WM_ACTION_PASS_TO_USER) {
839 policyFlags |= POLICY_FLAG_PASS_TO_USER;
840 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800841#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000842 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800843#endif
844 }
845}
846
Jeff Brown905805a2011-10-12 13:57:59 -0700847nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800848 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700849 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700850 // Policy:
851 // - Ignore untrusted events and pass them along.
852 // - Filter normal events and trusted injected events through the window manager policy to
853 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700854 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700855 if (policyFlags & POLICY_FLAG_TRUSTED) {
856 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700857
Jeff Brown928e0542011-01-10 11:17:36 -0800858 // Note: inputWindowHandle may be null.
859 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800860 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
861 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700862 jlong delayMillis = env->CallLongMethod(mServiceObj,
863 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800864 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800865 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
866 android_view_KeyEvent_recycle(env, keyEventObj);
867 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700868 if (!error) {
869 if (delayMillis < 0) {
870 result = -1;
871 } else if (delayMillis > 0) {
872 result = milliseconds_to_nanoseconds(delayMillis);
873 }
874 }
Jeff Brown1f245102010-11-18 20:53:46 -0800875 } else {
Steve Block3762c312012-01-06 19:20:56 +0000876 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800877 }
Jeff Brown928e0542011-01-10 11:17:36 -0800878 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700879 }
Jeff Brown1f245102010-11-18 20:53:46 -0800880 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700881}
882
Jeff Brown928e0542011-01-10 11:17:36 -0800883bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800884 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700885 // Policy:
886 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800887 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700888 if (policyFlags & POLICY_FLAG_TRUSTED) {
889 JNIEnv* env = jniEnv();
890
Jeff Brown928e0542011-01-10 11:17:36 -0800891 // Note: inputWindowHandle may be null.
892 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800893 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
894 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700895 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
896 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800897 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700898 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
899 fallbackKeyEventObj = NULL;
900 }
Jeff Brown1f245102010-11-18 20:53:46 -0800901 android_view_KeyEvent_recycle(env, keyEventObj);
902 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800903
904 if (fallbackKeyEventObj) {
905 // Note: outFallbackKeyEvent may be the same object as keyEvent.
906 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
907 outFallbackKeyEvent)) {
908 result = true;
909 }
910 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
911 env->DeleteLocalRef(fallbackKeyEventObj);
912 }
Jeff Brown1f245102010-11-18 20:53:46 -0800913 } else {
Steve Block3762c312012-01-06 19:20:56 +0000914 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800915 }
Jeff Brown928e0542011-01-10 11:17:36 -0800916 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700917 }
Jeff Brown1f245102010-11-18 20:53:46 -0800918 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700919}
920
Jeff Brown01ce2e92010-09-26 22:20:12 -0700921void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
922 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700923}
924
Jeff Brown349703e2010-06-22 01:27:15 -0700925
Jeff Brownb88102f2010-09-08 11:49:43 -0700926bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
927 int32_t injectorPid, int32_t injectorUid) {
928 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700929 jboolean result = env->CallBooleanMethod(mServiceObj,
930 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700931 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
932 result = false;
933 }
Jeff Brown349703e2010-06-22 01:27:15 -0700934 return result;
935}
936
Jeff Brown2352b972011-04-12 22:39:53 -0700937void NativeInputManager::loadPointerResources(PointerResources* outResources) {
938 JNIEnv* env = jniEnv();
939
940 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
941 &outResources->spotHover);
942 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
943 &outResources->spotTouch);
944 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
945 &outResources->spotAnchor);
946}
947
Jeff Brown83c09682010-12-23 17:50:18 -0800948
Jeff Brown9c3cda02010-06-15 01:31:58 -0700949// ----------------------------------------------------------------------------
950
Jeff Brown4532e612012-04-05 14:27:12 -0700951static jint nativeInit(JNIEnv* env, jclass clazz,
952 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -0700953 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
954 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
955 messageQueue->getLooper());
Jeff Brown4532e612012-04-05 14:27:12 -0700956 im->incStrong(serviceObj);
957 return reinterpret_cast<jint>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700958}
959
Jeff Brown4532e612012-04-05 14:27:12 -0700960static void nativeStart(JNIEnv* env, jclass clazz, jint ptr) {
961 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700962
Jeff Brown4532e612012-04-05 14:27:12 -0700963 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700964 if (result) {
965 jniThrowRuntimeException(env, "Input manager could not be started.");
966 }
967}
968
Jeff Brown4532e612012-04-05 14:27:12 -0700969static void nativeSetDisplaySize(JNIEnv* env, jclass clazz, jint ptr,
Jeff Brownbc68a592011-07-25 12:58:12 -0700970 jint displayId, jint width, jint height, jint externalWidth, jint externalHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -0700971 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700972
973 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
974 // to be passed in like this, not sure which is better but leaving it like this
975 // keeps the window manager in direct control of when display transitions propagate down
976 // to the input dispatcher
Jeff Brown4532e612012-04-05 14:27:12 -0700977 im->setDisplaySize(displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700978}
979
Jeff Brown4532e612012-04-05 14:27:12 -0700980static void nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
981 jint ptr, jint displayId, jint orientation) {
982 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700983
Jeff Brown4532e612012-04-05 14:27:12 -0700984 im->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700985}
986
Jeff Brown4532e612012-04-05 14:27:12 -0700987static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
988 jint ptr, jint deviceId, jint sourceMask, jint scanCode) {
989 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700990
Jeff Brown4532e612012-04-05 14:27:12 -0700991 return im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700992 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700993}
994
Jeff Brown4532e612012-04-05 14:27:12 -0700995static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
996 jint ptr, jint deviceId, jint sourceMask, jint keyCode) {
997 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700998
Jeff Brown4532e612012-04-05 14:27:12 -0700999 return im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001000 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001001}
1002
Jeff Brown4532e612012-04-05 14:27:12 -07001003static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
1004 jint ptr, jint deviceId, jint sourceMask, jint sw) {
1005 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001006
Jeff Brown4532e612012-04-05 14:27:12 -07001007 return im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001008 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001009}
1010
Jeff Brown4532e612012-04-05 14:27:12 -07001011static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
1012 jint ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
1013 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001014
1015 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1016 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1017 jsize numCodes = env->GetArrayLength(keyCodes);
1018 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brown4532e612012-04-05 14:27:12 -07001020 result = im->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001021 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001022 } else {
1023 result = JNI_FALSE;
1024 }
1025
1026 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1027 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1028 return result;
1029}
1030
1031static void throwInputChannelNotInitialized(JNIEnv* env) {
1032 jniThrowException(env, "java/lang/IllegalStateException",
1033 "inputChannel is not initialized");
1034}
1035
Jeff Brown4532e612012-04-05 14:27:12 -07001036static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001038 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1039
Steve Block8564c8d2012-01-05 23:22:43 +00001040 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001041 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001042 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001043}
1044
Jeff Brown4532e612012-04-05 14:27:12 -07001045static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
1046 jint ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
1047 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001048
1049 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1050 inputChannelObj);
1051 if (inputChannel == NULL) {
1052 throwInputChannelNotInitialized(env);
1053 return;
1054 }
1055
Jeff Brown928e0542011-01-10 11:17:36 -08001056 sp<InputWindowHandle> inputWindowHandle =
1057 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001058
Jeff Brown4532e612012-04-05 14:27:12 -07001059 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001060 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001061 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001062 String8 message;
1063 message.appendFormat("Failed to register input channel. status=%d", status);
1064 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001065 return;
1066 }
1067
Jeff Browna41ca772010-08-11 14:46:32 -07001068 if (! monitor) {
1069 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001070 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001071 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001072}
1073
Jeff Brown4532e612012-04-05 14:27:12 -07001074static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1075 jint ptr, jobject inputChannelObj) {
1076 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001077
1078 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1079 inputChannelObj);
1080 if (inputChannel == NULL) {
1081 throwInputChannelNotInitialized(env);
1082 return;
1083 }
1084
1085 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1086
Jeff Brown4532e612012-04-05 14:27:12 -07001087 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001088 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1089 String8 message;
1090 message.appendFormat("Failed to unregister input channel. status=%d", status);
1091 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001092 }
1093}
1094
Jeff Brown4532e612012-04-05 14:27:12 -07001095static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
1096 jint ptr, jboolean enabled) {
1097 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001098
Jeff Brown4532e612012-04-05 14:27:12 -07001099 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001100}
1101
Jeff Brown4532e612012-04-05 14:27:12 -07001102static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1103 jint ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001104 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001105 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001106
Jeff Brown6ec402b2010-07-28 15:48:59 -07001107 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1108 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001109 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1110 if (status) {
1111 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1112 return INPUT_EVENT_INJECTION_FAILED;
1113 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001114
Jeff Brown4532e612012-04-05 14:27:12 -07001115 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001116 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1117 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001118 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001119 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1120 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001121 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1122 return INPUT_EVENT_INJECTION_FAILED;
1123 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001124
Jeff Brown4532e612012-04-05 14:27:12 -07001125 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001126 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1127 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001128 } else {
1129 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001130 return INPUT_EVENT_INJECTION_FAILED;
1131 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001132}
1133
Jeff Brown4532e612012-04-05 14:27:12 -07001134static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
1135 jint ptr, jobjectArray windowHandleObjArray) {
1136 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001137
Jeff Brown4532e612012-04-05 14:27:12 -07001138 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001139}
1140
Jeff Brown4532e612012-04-05 14:27:12 -07001141static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1142 jint ptr, jobject applicationHandleObj) {
1143 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001144
Jeff Brown4532e612012-04-05 14:27:12 -07001145 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001146}
1147
Jeff Brown4532e612012-04-05 14:27:12 -07001148static void nativeSetInputDispatchMode(JNIEnv* env,
1149 jclass clazz, jint ptr, jboolean enabled, jboolean frozen) {
1150 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001151
Jeff Brown4532e612012-04-05 14:27:12 -07001152 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001153}
1154
Jeff Brown4532e612012-04-05 14:27:12 -07001155static void nativeSetSystemUiVisibility(JNIEnv* env,
1156 jclass clazz, jint ptr, jint visibility) {
1157 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001158
Jeff Brown4532e612012-04-05 14:27:12 -07001159 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001160}
1161
Jeff Brown4532e612012-04-05 14:27:12 -07001162static jobject nativeGetInputDevice(JNIEnv* env,
1163 jclass clazz, jint ptr, jint deviceId) {
1164 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown8d608662010-08-30 03:02:23 -07001165
1166 InputDeviceInfo deviceInfo;
Jeff Brown4532e612012-04-05 14:27:12 -07001167 status_t status = im->getInputManager()->getReader()->getInputDeviceInfo(
Jeff Brown8d608662010-08-30 03:02:23 -07001168 deviceId, & deviceInfo);
1169 if (status) {
1170 return NULL;
1171 }
1172
1173 jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001174 if (!deviceObj) {
Jeff Brown8d608662010-08-30 03:02:23 -07001175 return NULL;
1176 }
1177
1178 jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001179 if (!deviceNameObj) {
1180 return NULL;
1181 }
1182
1183 jstring deviceDescriptorObj = env->NewStringUTF(deviceInfo.getDescriptor().string());
1184 if (!deviceDescriptorObj) {
Jeff Brown8d608662010-08-30 03:02:23 -07001185 return NULL;
1186 }
1187
Jeff Brown1e08fe92011-11-15 17:48:10 -08001188 jstring fileStr = env->NewStringUTF(deviceInfo.getKeyCharacterMapFile());
1189 if (!fileStr) {
1190 return NULL;
1191 }
1192
Jeff Brown8d608662010-08-30 03:02:23 -07001193 env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
1194 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001195 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mDescriptor, deviceDescriptorObj);
Jeff Brown8d608662010-08-30 03:02:23 -07001196 env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
1197 env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
Jeff Brown1e08fe92011-11-15 17:48:10 -08001198 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mKeyCharacterMapFile, fileStr);
Jeff Brown8d608662010-08-30 03:02:23 -07001199
Jeff Brownefd32662011-03-08 15:13:06 -08001200 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Brown8d608662010-08-30 03:02:23 -07001201 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -08001202 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
Jeff Brown8d608662010-08-30 03:02:23 -07001203 env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange,
Jeff Brownefd32662011-03-08 15:13:06 -08001204 range.axis, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Brown8d608662010-08-30 03:02:23 -07001205 if (env->ExceptionCheck()) {
1206 return NULL;
1207 }
1208 }
1209
1210 return deviceObj;
1211}
1212
Jeff Brown4532e612012-04-05 14:27:12 -07001213static jintArray nativeGetInputDeviceIds(JNIEnv* env,
1214 jclass clazz, jint ptr) {
1215 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown8d608662010-08-30 03:02:23 -07001216
1217 Vector<int> deviceIds;
Jeff Brown4532e612012-04-05 14:27:12 -07001218 im->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
Jeff Brown8d608662010-08-30 03:02:23 -07001219
1220 jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
1221 if (! deviceIdsObj) {
1222 return NULL;
1223 }
1224
1225 env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
1226 return deviceIdsObj;
1227}
1228
Jeff Brown4532e612012-04-05 14:27:12 -07001229static void nativeGetInputConfiguration(JNIEnv* env,
1230 jclass clazz, jint ptr, jobject configObj) {
1231 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown57c59372010-09-21 18:22:55 -07001232
1233 InputConfiguration config;
Jeff Brown4532e612012-04-05 14:27:12 -07001234 im->getInputManager()->getReader()->getInputConfiguration(& config);
Jeff Brown57c59372010-09-21 18:22:55 -07001235
1236 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1237 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1238 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1239}
1240
Jeff Brown4532e612012-04-05 14:27:12 -07001241static jboolean nativeTransferTouchFocus(JNIEnv* env,
1242 jclass clazz, jint ptr, jobject fromChannelObj, jobject toChannelObj) {
1243 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001244
1245 sp<InputChannel> fromChannel =
1246 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1247 sp<InputChannel> toChannel =
1248 android_view_InputChannel_getInputChannel(env, toChannelObj);
1249
1250 if (fromChannel == NULL || toChannel == NULL) {
1251 return false;
1252 }
1253
Jeff Brown4532e612012-04-05 14:27:12 -07001254 return im->getInputManager()->getDispatcher()->
Jeff Browne6504122010-09-27 14:52:15 -07001255 transferTouchFocus(fromChannel, toChannel);
1256}
1257
Jeff Brown4532e612012-04-05 14:27:12 -07001258static void nativeSetPointerSpeed(JNIEnv* env,
1259 jclass clazz, jint ptr, jint speed) {
1260 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001261
Jeff Brown4532e612012-04-05 14:27:12 -07001262 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001263}
1264
Jeff Brown4532e612012-04-05 14:27:12 -07001265static void nativeSetShowTouches(JNIEnv* env,
1266 jclass clazz, jint ptr, jboolean enabled) {
1267 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001268
Jeff Brown4532e612012-04-05 14:27:12 -07001269 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001270}
1271
Jeff Brown4532e612012-04-05 14:27:12 -07001272static jstring nativeDump(JNIEnv* env, jclass clazz, jint ptr) {
1273 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001274
Jeff Brownb88102f2010-09-08 11:49:43 -07001275 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001276 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001277 return env->NewStringUTF(dump.string());
1278}
1279
Jeff Brown4532e612012-04-05 14:27:12 -07001280static void nativeMonitor(JNIEnv* env, jclass clazz, jint ptr) {
1281 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001282
Jeff Brown4532e612012-04-05 14:27:12 -07001283 im->getInputManager()->getReader()->monitor();
1284 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001285}
1286
Jeff Brown9c3cda02010-06-15 01:31:58 -07001287// ----------------------------------------------------------------------------
1288
Jeff Brown46b9ac02010-04-22 18:58:52 -07001289static JNINativeMethod gInputManagerMethods[] = {
1290 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001291 { "nativeInit",
1292 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)I",
1293 (void*) nativeInit },
1294 { "nativeStart", "(I)V",
1295 (void*) nativeStart },
1296 { "nativeSetDisplaySize", "(IIIIII)V",
1297 (void*) nativeSetDisplaySize },
1298 { "nativeSetDisplayOrientation", "(III)V",
1299 (void*) nativeSetDisplayOrientation },
1300 { "nativeGetScanCodeState", "(IIII)I",
1301 (void*) nativeGetScanCodeState },
1302 { "nativeGetKeyCodeState", "(IIII)I",
1303 (void*) nativeGetKeyCodeState },
1304 { "nativeGetSwitchState", "(IIII)I",
1305 (void*) nativeGetSwitchState },
1306 { "nativeHasKeys", "(III[I[Z)Z",
1307 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001308 { "nativeRegisterInputChannel",
Jeff Brown4532e612012-04-05 14:27:12 -07001309 "(ILandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
1310 (void*) nativeRegisterInputChannel },
1311 { "nativeUnregisterInputChannel", "(ILandroid/view/InputChannel;)V",
1312 (void*) nativeUnregisterInputChannel },
1313 { "nativeSetInputFilterEnabled", "(IZ)V",
1314 (void*) nativeSetInputFilterEnabled },
1315 { "nativeInjectInputEvent", "(ILandroid/view/InputEvent;IIIII)I",
1316 (void*) nativeInjectInputEvent },
1317 { "nativeSetInputWindows", "(I[Lcom/android/server/input/InputWindowHandle;)V",
1318 (void*) nativeSetInputWindows },
1319 { "nativeSetFocusedApplication", "(ILcom/android/server/input/InputApplicationHandle;)V",
1320 (void*) nativeSetFocusedApplication },
1321 { "nativeSetInputDispatchMode", "(IZZ)V",
1322 (void*) nativeSetInputDispatchMode },
1323 { "nativeSetSystemUiVisibility", "(II)V",
1324 (void*) nativeSetSystemUiVisibility },
1325 { "nativeGetInputDevice", "(II)Landroid/view/InputDevice;",
1326 (void*) nativeGetInputDevice },
1327 { "nativeGetInputDeviceIds", "(I)[I",
1328 (void*) nativeGetInputDeviceIds },
1329 { "nativeGetInputConfiguration", "(ILandroid/content/res/Configuration;)V",
1330 (void*) nativeGetInputConfiguration },
1331 { "nativeTransferTouchFocus", "(ILandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1332 (void*) nativeTransferTouchFocus },
1333 { "nativeSetPointerSpeed", "(II)V",
1334 (void*) nativeSetPointerSpeed },
1335 { "nativeSetShowTouches", "(IZ)V",
1336 (void*) nativeSetShowTouches },
1337 { "nativeDump", "(I)Ljava/lang/String;",
1338 (void*) nativeDump },
1339 { "nativeMonitor", "(I)V",
1340 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001341};
1342
1343#define FIND_CLASS(var, className) \
1344 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001345 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001346
1347#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1348 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1349 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1350
1351#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1352 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1353 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1354
1355int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001356 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001357 gInputManagerMethods, NELEM(gInputManagerMethods));
1358 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1359
Jeff Brown9c3cda02010-06-15 01:31:58 -07001360 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001361
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001362 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001363 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001364
Jeff Brown4532e612012-04-05 14:27:12 -07001365 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001366 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001367
Jeff Brown4532e612012-04-05 14:27:12 -07001368 GET_METHOD_ID(gServiceClassInfo.notifyLidSwitchChanged, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001369 "notifyLidSwitchChanged", "(JZ)V");
1370
Jeff Brown4532e612012-04-05 14:27:12 -07001371 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1372 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001373
Jeff Brown4532e612012-04-05 14:27:12 -07001374 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001375 "notifyANR",
Jeff Brown4532e612012-04-05 14:27:12 -07001376 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001377
Jeff Brown4532e612012-04-05 14:27:12 -07001378 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001379 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1380
Jeff Brown4532e612012-04-05 14:27:12 -07001381 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001382 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001383
Jeff Brown4532e612012-04-05 14:27:12 -07001384 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001385 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001386 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1387
Jeff Brown4532e612012-04-05 14:27:12 -07001388 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001389 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001390 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001391
Jeff Brown4532e612012-04-05 14:27:12 -07001392 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001393 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001394 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001395
Jeff Brown4532e612012-04-05 14:27:12 -07001396 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001397 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001398
Jeff Brown4532e612012-04-05 14:27:12 -07001399 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001400 "getVirtualKeyQuietTimeMillis", "()I");
1401
Jeff Brown4532e612012-04-05 14:27:12 -07001402 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001403 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1404
Jeff Brown4532e612012-04-05 14:27:12 -07001405 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001406 "getKeyRepeatTimeout", "()I");
1407
Jeff Brown4532e612012-04-05 14:27:12 -07001408 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001409 "getKeyRepeatDelay", "()I");
1410
Jeff Brown4532e612012-04-05 14:27:12 -07001411 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001412 "getHoverTapTimeout", "()I");
1413
Jeff Brown4532e612012-04-05 14:27:12 -07001414 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001415 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001416
Jeff Brown4532e612012-04-05 14:27:12 -07001417 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001418 "getDoubleTapTimeout", "()I");
1419
Jeff Brown4532e612012-04-05 14:27:12 -07001420 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001421 "getLongPressTimeout", "()I");
1422
Jeff Brown4532e612012-04-05 14:27:12 -07001423 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001424 "getPointerLayer", "()I");
1425
Jeff Brown4532e612012-04-05 14:27:12 -07001426 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001427 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001428
Jeff Brown6ec402b2010-07-28 15:48:59 -07001429 // KeyEvent
1430
1431 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001432 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1433
Jeff Brown8d608662010-08-30 03:02:23 -07001434 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001435
1436 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001437 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001438
Jeff Brown8d608662010-08-30 03:02:23 -07001439 // InputDevice
1440
1441 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001442 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
Jeff Brown8d608662010-08-30 03:02:23 -07001443
1444 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
1445 "<init>", "()V");
1446
1447 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
Jeff Brownefd32662011-03-08 15:13:06 -08001448 "addMotionRange", "(IIFFFF)V");
Jeff Brown8d608662010-08-30 03:02:23 -07001449
1450 GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz,
1451 "mId", "I");
1452
1453 GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz,
1454 "mName", "Ljava/lang/String;");
1455
Jeff Browne38fdfa2012-04-06 14:51:01 -07001456 GET_FIELD_ID(gInputDeviceClassInfo.mDescriptor, gInputDeviceClassInfo.clazz,
1457 "mDescriptor", "Ljava/lang/String;");
1458
Jeff Brown8d608662010-08-30 03:02:23 -07001459 GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz,
1460 "mSources", "I");
1461
1462 GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
1463 "mKeyboardType", "I");
1464
Jeff Brown1e08fe92011-11-15 17:48:10 -08001465 GET_FIELD_ID(gInputDeviceClassInfo.mKeyCharacterMapFile, gInputDeviceClassInfo.clazz,
1466 "mKeyCharacterMapFile", "Ljava/lang/String;");
1467
Jeff Brown57c59372010-09-21 18:22:55 -07001468 // Configuration
1469
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001470 FIND_CLASS(clazz, "android/content/res/Configuration");
Jeff Brown57c59372010-09-21 18:22:55 -07001471
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001472 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001473 "touchscreen", "I");
1474
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001475 GET_FIELD_ID(gConfigurationClassInfo.keyboard, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001476 "keyboard", "I");
1477
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001478 GET_FIELD_ID(gConfigurationClassInfo.navigation, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001479 "navigation", "I");
1480
Jeff Brown46b9ac02010-04-22 18:58:52 -07001481 return 0;
1482}
1483
Jeff Brown46b9ac02010-04-22 18:58:52 -07001484} /* namespace android */