blob: b2a24292c02b9b7797ffde0003b642e8e207bdc7 [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 Brown9f25b7f2012-04-10 14:30:49 -070042#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080043#include <android_view_KeyEvent.h>
44#include <android_view_MotionEvent.h>
45#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070046#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <android/graphics/GraphicsJNI.h>
48
Jeff Brown6ec6f792012-04-17 16:52:41 -070049#include <ScopedLocalRef.h>
50#include <ScopedUtfChars.h>
51
Jeff Brown00fa7bd2010-07-02 15:37:36 -070052#include "com_android_server_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070053#include "com_android_server_input_InputApplicationHandle.h"
54#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070055
56namespace android {
57
Jeff Brown1a84fd12011-06-02 01:26:32 -070058// The exponent used to calculate the pointer speed scaling factor.
59// The scaling factor is calculated as 2 ^ (speed * exponent),
60// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070061static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070062
Jeff Brown46b9ac02010-04-22 18:58:52 -070063static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070064 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070065 jmethodID notifyInputDevicesChanged;
Jeff Brown46b9ac02010-04-22 18:58:52 -070066 jmethodID notifyLidSwitchChanged;
Jeff Brown7fbdc842010-06-17 20:52:56 -070067 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070068 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070069 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070070 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080071 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070072 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070073 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070074 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080075 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070076 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080077 jmethodID getKeyRepeatTimeout;
78 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070079 jmethodID getHoverTapTimeout;
80 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070081 jmethodID getDoubleTapTimeout;
82 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080083 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080084 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070085 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070086 jmethodID getDeviceAlias;
Jeff Brown4532e612012-04-05 14:27:12 -070087} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070088
89static struct {
90 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070091} gInputDeviceClassInfo;
92
93static struct {
94 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070095} gKeyEventClassInfo;
96
97static struct {
98 jclass clazz;
99} gMotionEventClassInfo;
100
Jeff Brown8d608662010-08-30 03:02:23 -0700101static struct {
Jeff Brown57c59372010-09-21 18:22:55 -0700102 jfieldID touchscreen;
103 jfieldID keyboard;
104 jfieldID navigation;
105} gConfigurationClassInfo;
106
Jeff Brown928e0542011-01-10 11:17:36 -0800107
108// --- Global functions ---
109
Jeff Brown214eaf42011-05-26 19:17:02 -0700110template<typename T>
111inline static T min(const T& a, const T& b) {
112 return a < b ? a : b;
113}
114
115template<typename T>
116inline static T max(const T& a, const T& b) {
117 return a > b ? a : b;
118}
119
Jeff Brown928e0542011-01-10 11:17:36 -0800120static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
121 const sp<InputApplicationHandle>& inputApplicationHandle) {
122 if (inputApplicationHandle == NULL) {
123 return NULL;
124 }
125 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
126 getInputApplicationHandleObjLocalRef(env);
127}
128
129static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
130 const sp<InputWindowHandle>& inputWindowHandle) {
131 if (inputWindowHandle == NULL) {
132 return NULL;
133 }
134 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
135 getInputWindowHandleObjLocalRef(env);
136}
137
Jeff Brown2352b972011-04-12 22:39:53 -0700138static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
139 SpriteIcon* outSpriteIcon) {
140 PointerIcon pointerIcon;
141 status_t status = android_view_PointerIcon_loadSystemIcon(env,
142 contextObj, style, &pointerIcon);
143 if (!status) {
144 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
145 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
146 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
147 }
148}
149
Jeff Brown905805a2011-10-12 13:57:59 -0700150enum {
151 WM_ACTION_PASS_TO_USER = 1,
152 WM_ACTION_POKE_USER_ACTIVITY = 2,
153 WM_ACTION_GO_TO_SLEEP = 4,
154};
155
Jeff Brown928e0542011-01-10 11:17:36 -0800156
157// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800158
Jeff Brown9c3cda02010-06-15 01:31:58 -0700159class NativeInputManager : public virtual RefBase,
160 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700161 public virtual InputDispatcherPolicyInterface,
162 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700163protected:
164 virtual ~NativeInputManager();
165
166public:
Jeff Brown4532e612012-04-05 14:27:12 -0700167 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700168
169 inline sp<InputManager> getInputManager() const { return mInputManager; }
170
Jeff Brownb88102f2010-09-08 11:49:43 -0700171 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700172
Jeff Brownbc68a592011-07-25 12:58:12 -0700173 void setDisplaySize(int32_t displayId, int32_t width, int32_t height,
174 int32_t externalWidth, int32_t externalHeight);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700175 void setDisplayOrientation(int32_t displayId, int32_t orientation);
176
Jeff Brown7fbdc842010-06-17 20:52:56 -0700177 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800178 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700179 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
180
Jeff Brown9302c872011-07-13 22:51:29 -0700181 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
182 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700183 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800184 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700185 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700186 void setShowTouches(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700187
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188 /* --- InputReaderPolicyInterface implementation --- */
189
Jeff Brown214eaf42011-05-26 19:17:02 -0700190 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800191 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700192 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700193 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700194 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
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 Brownaf9e8d32012-04-12 17:32:48 -0700502void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
503 JNIEnv* env = jniEnv();
504
505 size_t count = inputDevices.size();
506 jobjectArray inputDevicesObjArray = env->NewObjectArray(
507 count, gInputDeviceClassInfo.clazz, NULL);
508 if (inputDevicesObjArray) {
509 bool error = false;
510 for (size_t i = 0; i < count; i++) {
511 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
512 if (!inputDeviceObj) {
513 error = true;
514 break;
515 }
516
517 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
518 env->DeleteLocalRef(inputDeviceObj);
519 }
520
521 if (!error) {
522 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
523 inputDevicesObjArray);
524 }
525
526 env->DeleteLocalRef(inputDevicesObjArray);
527 }
528
529 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
530}
531
Jeff Brown6ec6f792012-04-17 16:52:41 -0700532sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
533 const String8& inputDeviceDescriptor) {
534 JNIEnv* env = jniEnv();
535
536 sp<KeyCharacterMap> result;
537 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
538 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
539 gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get())));
540 if (arrayObj.get()) {
541 ScopedLocalRef<jstring> filenameObj(env,
542 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
543 ScopedLocalRef<jstring> contentsObj(env,
544 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
545 ScopedUtfChars filenameChars(env, filenameObj.get());
546 ScopedUtfChars contentsChars(env, contentsObj.get());
547
548 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
549 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
550 }
551 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
552 return result;
553}
554
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700555String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
556 JNIEnv* env = jniEnv();
557
558 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
559 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
560 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
561 String8 result;
562 if (aliasObj.get()) {
563 ScopedUtfChars aliasChars(env, aliasObj.get());
564 result.setTo(aliasChars.c_str());
565 }
566 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
567 return result;
568}
569
Jeff Browne20c9e02010-10-11 14:20:19 -0700570void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
571 int32_t switchValue, uint32_t policyFlags) {
572#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000573 ALOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
Jeff Browne20c9e02010-10-11 14:20:19 -0700574 when, switchCode, switchValue, policyFlags);
575#endif
576
577 JNIEnv* env = jniEnv();
578
579 switch (switchCode) {
580 case SW_LID:
Jeff Brown27fd3422012-04-09 11:05:16 -0700581 // When switch value is set indicates lid is closed.
Jeff Brown4532e612012-04-05 14:27:12 -0700582 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyLidSwitchChanged,
Jeff Brown27fd3422012-04-09 11:05:16 -0700583 when, switchValue == 0 /*lidOpen*/);
Jeff Browne20c9e02010-10-11 14:20:19 -0700584 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
585 break;
586 }
587}
588
Jeff Brown9c3cda02010-06-15 01:31:58 -0700589void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
590#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000591 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700592#endif
593
594 JNIEnv* env = jniEnv();
595
Jeff Brown4532e612012-04-05 14:27:12 -0700596 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700597 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700598}
599
Jeff Brown519e0242010-09-15 15:18:56 -0700600nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800601 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700602#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000603 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700604#endif
605
606 JNIEnv* env = jniEnv();
607
Jeff Brown928e0542011-01-10 11:17:36 -0800608 jobject inputApplicationHandleObj =
609 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
610 jobject inputWindowHandleObj =
611 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700612
Jeff Brown4532e612012-04-05 14:27:12 -0700613 jlong newTimeout = env->CallLongMethod(mServiceObj,
614 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700615 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
616 newTimeout = 0; // abort dispatch
617 } else {
618 assert(newTimeout >= 0);
619 }
620
Jeff Brown928e0542011-01-10 11:17:36 -0800621 env->DeleteLocalRef(inputWindowHandleObj);
622 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700623 return newTimeout;
624}
625
Jeff Brown928e0542011-01-10 11:17:36 -0800626void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700627#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000628 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700629#endif
630
Jeff Brown7fbdc842010-06-17 20:52:56 -0700631 JNIEnv* env = jniEnv();
632
Jeff Brown928e0542011-01-10 11:17:36 -0800633 jobject inputWindowHandleObj =
634 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
635 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700636 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800637 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700638 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
639
Jeff Brown928e0542011-01-10 11:17:36 -0800640 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700641 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700642}
643
Jeff Brown214eaf42011-05-26 19:17:02 -0700644void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
645 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800646
Jeff Brown4532e612012-04-05 14:27:12 -0700647 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
648 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700649 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
650 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
651 }
Jeff Browna4547672011-03-02 21:38:11 -0800652
Jeff Brown4532e612012-04-05 14:27:12 -0700653 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
654 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700655 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
656 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
657 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700658}
659
Jeff Brown214eaf42011-05-26 19:17:02 -0700660bool NativeInputManager::isKeyRepeatEnabled() {
661 // Only enable automatic key repeating when the screen is on.
662 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700663}
664
Jeff Brown9302c872011-07-13 22:51:29 -0700665void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
666 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700667
Jeff Brown9302c872011-07-13 22:51:29 -0700668 if (windowHandleObjArray) {
669 jsize length = env->GetArrayLength(windowHandleObjArray);
670 for (jsize i = 0; i < length; i++) {
671 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
672 if (! windowHandleObj) {
673 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700674 }
Jeff Brown9302c872011-07-13 22:51:29 -0700675
676 sp<InputWindowHandle> windowHandle =
677 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
678 if (windowHandle != NULL) {
679 windowHandles.push(windowHandle);
680 }
681 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700682 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700683 }
Jeff Brown349703e2010-06-22 01:27:15 -0700684
Jeff Brown9302c872011-07-13 22:51:29 -0700685 mInputManager->getDispatcher()->setInputWindows(windowHandles);
686
687 // Do this after the dispatcher has updated the window handle state.
688 bool newPointerGesturesEnabled = true;
689 size_t numWindows = windowHandles.size();
690 for (size_t i = 0; i < numWindows; i++) {
691 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700692 const InputWindowInfo* windowInfo = windowHandle->getInfo();
693 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
694 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700695 newPointerGesturesEnabled = false;
696 }
697 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700698
699 uint32_t changes = 0;
700 { // acquire lock
701 AutoMutex _l(mLock);
702
703 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
704 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
705 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
706 }
707 } // release lock
708
709 if (changes) {
710 mInputManager->getReader()->requestRefreshConfiguration(changes);
711 }
Jeff Brown349703e2010-06-22 01:27:15 -0700712}
713
Jeff Brown9302c872011-07-13 22:51:29 -0700714void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
715 sp<InputApplicationHandle> applicationHandle =
716 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
717 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700718}
719
720void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700721 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700722}
723
Jeff Brown05dc66a2011-03-02 14:41:58 -0800724void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
725 AutoMutex _l(mLock);
726
727 if (mLocked.systemUiVisibility != visibility) {
728 mLocked.systemUiVisibility = visibility;
729
730 sp<PointerController> controller = mLocked.pointerController.promote();
731 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700732 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800733 }
734 }
735}
736
Jeff Brown2352b972011-04-12 22:39:53 -0700737void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800738 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700739 controller->setInactivityTimeout(lightsOut
740 ? PointerController::INACTIVITY_TIMEOUT_SHORT
741 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800742}
743
Jeff Brown1a84fd12011-06-02 01:26:32 -0700744void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700745 { // acquire lock
746 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700747
Jeff Brown474dcb52011-06-14 20:22:50 -0700748 if (mLocked.pointerSpeed == speed) {
749 return;
750 }
751
Steve Block6215d3f2012-01-04 20:05:49 +0000752 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700753 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700754 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700755
Jeff Brown474dcb52011-06-14 20:22:50 -0700756 mInputManager->getReader()->requestRefreshConfiguration(
757 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700758}
759
Jeff Browndaf4a122011-08-26 17:14:14 -0700760void NativeInputManager::setShowTouches(bool enabled) {
761 { // acquire lock
762 AutoMutex _l(mLock);
763
764 if (mLocked.showTouches == enabled) {
765 return;
766 }
767
Steve Block6215d3f2012-01-04 20:05:49 +0000768 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700769 mLocked.showTouches = enabled;
770 } // release lock
771
772 mInputManager->getReader()->requestRefreshConfiguration(
773 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
774}
775
Jeff Browne20c9e02010-10-11 14:20:19 -0700776bool NativeInputManager::isScreenOn() {
777 return android_server_PowerManagerService_isScreenOn();
778}
779
780bool NativeInputManager::isScreenBright() {
781 return android_server_PowerManagerService_isScreenBright();
782}
783
Jeff Brown0029c662011-03-30 02:25:18 -0700784bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
785 jobject inputEventObj;
786
787 JNIEnv* env = jniEnv();
788 switch (inputEvent->getType()) {
789 case AINPUT_EVENT_TYPE_KEY:
790 inputEventObj = android_view_KeyEvent_fromNative(env,
791 static_cast<const KeyEvent*>(inputEvent));
792 break;
793 case AINPUT_EVENT_TYPE_MOTION:
794 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
795 static_cast<const MotionEvent*>(inputEvent));
796 break;
797 default:
798 return true; // dispatch the event normally
799 }
800
801 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000802 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700803 return true; // dispatch the event normally
804 }
805
806 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700807 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700808 inputEventObj, policyFlags);
809 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
810 pass = true;
811 }
812 env->DeleteLocalRef(inputEventObj);
813 return pass;
814}
815
Jeff Brown1f245102010-11-18 20:53:46 -0800816void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
817 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700818 // Policy:
819 // - Ignore untrusted events and pass them along.
820 // - Ask the window manager what to do with normal events and trusted injected events.
821 // - For normal events wake and brighten the screen if currently off or dim.
822 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800823 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700824 bool isScreenOn = this->isScreenOn();
825 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700826
Jeff Brown3122e442010-10-11 23:32:49 -0700827 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800828 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
829 jint wmActions;
830 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700831 wmActions = env->CallIntMethod(mServiceObj,
832 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800833 keyEventObj, policyFlags, isScreenOn);
834 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
835 wmActions = 0;
836 }
837 android_view_KeyEvent_recycle(env, keyEventObj);
838 env->DeleteLocalRef(keyEventObj);
839 } else {
Steve Block3762c312012-01-06 19:20:56 +0000840 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700841 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700842 }
843
Jeff Brown1f245102010-11-18 20:53:46 -0800844 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700845 if (!isScreenOn) {
846 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700847 }
848
849 if (!isScreenBright) {
850 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
851 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700852 }
853
Jeff Brown56194eb2011-03-02 19:23:13 -0800854 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700855 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700856 policyFlags |= POLICY_FLAG_PASS_TO_USER;
857 }
858}
859
Jeff Brown56194eb2011-03-02 19:23:13 -0800860void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700861 // Policy:
862 // - Ignore untrusted events and pass them along.
863 // - No special filtering for injected events required at this time.
864 // - Filter normal events based on screen state.
865 // - For normal events brighten (but do not wake) the screen if currently dim.
866 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
867 if (isScreenOn()) {
868 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700869
Jeff Brown3122e442010-10-11 23:32:49 -0700870 if (!isScreenBright()) {
871 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
872 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800873 } else {
874 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700875 jint wmActions = env->CallIntMethod(mServiceObj,
876 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown56194eb2011-03-02 19:23:13 -0800877 policyFlags);
878 if (checkAndClearExceptionFromCallback(env,
879 "interceptMotionBeforeQueueingWhenScreenOff")) {
880 wmActions = 0;
881 }
882
883 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
884 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700885 }
Jeff Brown3122e442010-10-11 23:32:49 -0700886 } else {
887 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700888 }
889}
890
Jeff Brown56194eb2011-03-02 19:23:13 -0800891void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
892 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800893 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800894#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000895 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800896#endif
897 android_server_PowerManagerService_goToSleep(when);
898 }
899
900 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800901#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000902 ALOGD("handleInterceptActions: Poking user activity.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800903#endif
904 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
905 }
906
907 if (wmActions & WM_ACTION_PASS_TO_USER) {
908 policyFlags |= POLICY_FLAG_PASS_TO_USER;
909 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800910#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000911 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800912#endif
913 }
914}
915
Jeff Brown905805a2011-10-12 13:57:59 -0700916nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800917 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700918 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700919 // Policy:
920 // - Ignore untrusted events and pass them along.
921 // - Filter normal events and trusted injected events through the window manager policy to
922 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700923 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700924 if (policyFlags & POLICY_FLAG_TRUSTED) {
925 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700926
Jeff Brown928e0542011-01-10 11:17:36 -0800927 // Note: inputWindowHandle may be null.
928 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800929 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
930 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700931 jlong delayMillis = env->CallLongMethod(mServiceObj,
932 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800933 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800934 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
935 android_view_KeyEvent_recycle(env, keyEventObj);
936 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700937 if (!error) {
938 if (delayMillis < 0) {
939 result = -1;
940 } else if (delayMillis > 0) {
941 result = milliseconds_to_nanoseconds(delayMillis);
942 }
943 }
Jeff Brown1f245102010-11-18 20:53:46 -0800944 } else {
Steve Block3762c312012-01-06 19:20:56 +0000945 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800946 }
Jeff Brown928e0542011-01-10 11:17:36 -0800947 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700948 }
Jeff Brown1f245102010-11-18 20:53:46 -0800949 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700950}
951
Jeff Brown928e0542011-01-10 11:17:36 -0800952bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800953 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700954 // Policy:
955 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800956 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700957 if (policyFlags & POLICY_FLAG_TRUSTED) {
958 JNIEnv* env = jniEnv();
959
Jeff Brown928e0542011-01-10 11:17:36 -0800960 // Note: inputWindowHandle may be null.
961 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800962 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
963 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700964 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
965 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800966 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700967 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
968 fallbackKeyEventObj = NULL;
969 }
Jeff Brown1f245102010-11-18 20:53:46 -0800970 android_view_KeyEvent_recycle(env, keyEventObj);
971 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800972
973 if (fallbackKeyEventObj) {
974 // Note: outFallbackKeyEvent may be the same object as keyEvent.
975 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
976 outFallbackKeyEvent)) {
977 result = true;
978 }
979 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
980 env->DeleteLocalRef(fallbackKeyEventObj);
981 }
Jeff Brown1f245102010-11-18 20:53:46 -0800982 } else {
Steve Block3762c312012-01-06 19:20:56 +0000983 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800984 }
Jeff Brown928e0542011-01-10 11:17:36 -0800985 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700986 }
Jeff Brown1f245102010-11-18 20:53:46 -0800987 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700988}
989
Jeff Brown01ce2e92010-09-26 22:20:12 -0700990void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
991 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700992}
993
Jeff Brown349703e2010-06-22 01:27:15 -0700994
Jeff Brownb88102f2010-09-08 11:49:43 -0700995bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
996 int32_t injectorPid, int32_t injectorUid) {
997 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700998 jboolean result = env->CallBooleanMethod(mServiceObj,
999 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001000 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1001 result = false;
1002 }
Jeff Brown349703e2010-06-22 01:27:15 -07001003 return result;
1004}
1005
Jeff Brown2352b972011-04-12 22:39:53 -07001006void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1007 JNIEnv* env = jniEnv();
1008
1009 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1010 &outResources->spotHover);
1011 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1012 &outResources->spotTouch);
1013 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1014 &outResources->spotAnchor);
1015}
1016
Jeff Brown83c09682010-12-23 17:50:18 -08001017
Jeff Brown9c3cda02010-06-15 01:31:58 -07001018// ----------------------------------------------------------------------------
1019
Jeff Brown4532e612012-04-05 14:27:12 -07001020static jint nativeInit(JNIEnv* env, jclass clazz,
1021 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001022 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
1023 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1024 messageQueue->getLooper());
Jeff Brown4532e612012-04-05 14:27:12 -07001025 im->incStrong(serviceObj);
1026 return reinterpret_cast<jint>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001027}
1028
Jeff Brown4532e612012-04-05 14:27:12 -07001029static void nativeStart(JNIEnv* env, jclass clazz, jint ptr) {
1030 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001031
Jeff Brown4532e612012-04-05 14:27:12 -07001032 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001033 if (result) {
1034 jniThrowRuntimeException(env, "Input manager could not be started.");
1035 }
1036}
1037
Jeff Brown4532e612012-04-05 14:27:12 -07001038static void nativeSetDisplaySize(JNIEnv* env, jclass clazz, jint ptr,
Jeff Brownbc68a592011-07-25 12:58:12 -07001039 jint displayId, jint width, jint height, jint externalWidth, jint externalHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001040 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001041
1042 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
1043 // to be passed in like this, not sure which is better but leaving it like this
1044 // keeps the window manager in direct control of when display transitions propagate down
1045 // to the input dispatcher
Jeff Brown4532e612012-04-05 14:27:12 -07001046 im->setDisplaySize(displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001047}
1048
Jeff Brown4532e612012-04-05 14:27:12 -07001049static void nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
1050 jint ptr, jint displayId, jint orientation) {
1051 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001052
Jeff Brown4532e612012-04-05 14:27:12 -07001053 im->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001054}
1055
Jeff Brown4532e612012-04-05 14:27:12 -07001056static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
1057 jint ptr, jint deviceId, jint sourceMask, jint scanCode) {
1058 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001059
Jeff Brown4532e612012-04-05 14:27:12 -07001060 return im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001061 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001062}
1063
Jeff Brown4532e612012-04-05 14:27:12 -07001064static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
1065 jint ptr, jint deviceId, jint sourceMask, jint keyCode) {
1066 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001067
Jeff Brown4532e612012-04-05 14:27:12 -07001068 return im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001069 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001070}
1071
Jeff Brown4532e612012-04-05 14:27:12 -07001072static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
1073 jint ptr, jint deviceId, jint sourceMask, jint sw) {
1074 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001075
Jeff Brown4532e612012-04-05 14:27:12 -07001076 return im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001077 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001078}
1079
Jeff Brown4532e612012-04-05 14:27:12 -07001080static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
1081 jint ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
1082 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001083
1084 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1085 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1086 jsize numCodes = env->GetArrayLength(keyCodes);
1087 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001088 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brown4532e612012-04-05 14:27:12 -07001089 result = im->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001090 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001091 } else {
1092 result = JNI_FALSE;
1093 }
1094
1095 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1096 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1097 return result;
1098}
1099
1100static void throwInputChannelNotInitialized(JNIEnv* env) {
1101 jniThrowException(env, "java/lang/IllegalStateException",
1102 "inputChannel is not initialized");
1103}
1104
Jeff Brown4532e612012-04-05 14:27:12 -07001105static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001106 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001107 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1108
Steve Block8564c8d2012-01-05 23:22:43 +00001109 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001110 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001111 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001112}
1113
Jeff Brown4532e612012-04-05 14:27:12 -07001114static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
1115 jint ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
1116 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001117
1118 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1119 inputChannelObj);
1120 if (inputChannel == NULL) {
1121 throwInputChannelNotInitialized(env);
1122 return;
1123 }
1124
Jeff Brown928e0542011-01-10 11:17:36 -08001125 sp<InputWindowHandle> inputWindowHandle =
1126 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001127
Jeff Brown4532e612012-04-05 14:27:12 -07001128 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001129 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001130 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001131 String8 message;
1132 message.appendFormat("Failed to register input channel. status=%d", status);
1133 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001134 return;
1135 }
1136
Jeff Browna41ca772010-08-11 14:46:32 -07001137 if (! monitor) {
1138 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001139 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001140 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001141}
1142
Jeff Brown4532e612012-04-05 14:27:12 -07001143static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1144 jint ptr, jobject inputChannelObj) {
1145 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001146
1147 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1148 inputChannelObj);
1149 if (inputChannel == NULL) {
1150 throwInputChannelNotInitialized(env);
1151 return;
1152 }
1153
1154 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1155
Jeff Brown4532e612012-04-05 14:27:12 -07001156 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001157 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1158 String8 message;
1159 message.appendFormat("Failed to unregister input channel. status=%d", status);
1160 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001161 }
1162}
1163
Jeff Brown4532e612012-04-05 14:27:12 -07001164static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
1165 jint ptr, jboolean enabled) {
1166 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001167
Jeff Brown4532e612012-04-05 14:27:12 -07001168 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001169}
1170
Jeff Brown4532e612012-04-05 14:27:12 -07001171static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1172 jint ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001173 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001174 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001175
Jeff Brown6ec402b2010-07-28 15:48:59 -07001176 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1177 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001178 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1179 if (status) {
1180 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1181 return INPUT_EVENT_INJECTION_FAILED;
1182 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001183
Jeff Brown4532e612012-04-05 14:27:12 -07001184 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001185 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1186 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001187 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001188 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1189 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001190 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1191 return INPUT_EVENT_INJECTION_FAILED;
1192 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001193
Jeff Brown4532e612012-04-05 14:27:12 -07001194 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001195 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1196 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001197 } else {
1198 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001199 return INPUT_EVENT_INJECTION_FAILED;
1200 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001201}
1202
Jeff Brown4532e612012-04-05 14:27:12 -07001203static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
1204 jint ptr, jobjectArray windowHandleObjArray) {
1205 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001206
Jeff Brown4532e612012-04-05 14:27:12 -07001207 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001208}
1209
Jeff Brown4532e612012-04-05 14:27:12 -07001210static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1211 jint ptr, jobject applicationHandleObj) {
1212 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001213
Jeff Brown4532e612012-04-05 14:27:12 -07001214 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001215}
1216
Jeff Brown4532e612012-04-05 14:27:12 -07001217static void nativeSetInputDispatchMode(JNIEnv* env,
1218 jclass clazz, jint ptr, jboolean enabled, jboolean frozen) {
1219 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001220
Jeff Brown4532e612012-04-05 14:27:12 -07001221 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001222}
1223
Jeff Brown4532e612012-04-05 14:27:12 -07001224static void nativeSetSystemUiVisibility(JNIEnv* env,
1225 jclass clazz, jint ptr, jint visibility) {
1226 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001227
Jeff Brown4532e612012-04-05 14:27:12 -07001228 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001229}
1230
Jeff Brown4532e612012-04-05 14:27:12 -07001231static void nativeGetInputConfiguration(JNIEnv* env,
1232 jclass clazz, jint ptr, jobject configObj) {
1233 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown57c59372010-09-21 18:22:55 -07001234
1235 InputConfiguration config;
Jeff Brown4532e612012-04-05 14:27:12 -07001236 im->getInputManager()->getReader()->getInputConfiguration(& config);
Jeff Brown57c59372010-09-21 18:22:55 -07001237
1238 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1239 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1240 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1241}
1242
Jeff Brown4532e612012-04-05 14:27:12 -07001243static jboolean nativeTransferTouchFocus(JNIEnv* env,
1244 jclass clazz, jint ptr, jobject fromChannelObj, jobject toChannelObj) {
1245 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001246
1247 sp<InputChannel> fromChannel =
1248 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1249 sp<InputChannel> toChannel =
1250 android_view_InputChannel_getInputChannel(env, toChannelObj);
1251
1252 if (fromChannel == NULL || toChannel == NULL) {
1253 return false;
1254 }
1255
Jeff Brown4532e612012-04-05 14:27:12 -07001256 return im->getInputManager()->getDispatcher()->
Jeff Browne6504122010-09-27 14:52:15 -07001257 transferTouchFocus(fromChannel, toChannel);
1258}
1259
Jeff Brown4532e612012-04-05 14:27:12 -07001260static void nativeSetPointerSpeed(JNIEnv* env,
1261 jclass clazz, jint ptr, jint speed) {
1262 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001263
Jeff Brown4532e612012-04-05 14:27:12 -07001264 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001265}
1266
Jeff Brown4532e612012-04-05 14:27:12 -07001267static void nativeSetShowTouches(JNIEnv* env,
1268 jclass clazz, jint ptr, jboolean enabled) {
1269 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001270
Jeff Brown4532e612012-04-05 14:27:12 -07001271 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001272}
1273
Jeff Browna47425a2012-04-13 04:09:27 -07001274static void nativeVibrate(JNIEnv* env,
1275 jclass clazz, jint ptr, jint deviceId, jlongArray patternObj,
1276 jint repeat, jint token) {
1277 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1278
1279 size_t patternSize = env->GetArrayLength(patternObj);
1280 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1281 ALOGI("Skipped requested vibration because the pattern size is %d "
1282 "which is more than the maximum supported size of %d.",
1283 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1284 return; // limit to reasonable size
1285 }
1286
1287 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1288 patternObj, NULL));
1289 nsecs_t pattern[patternSize];
1290 for (size_t i = 0; i < patternSize; i++) {
1291 pattern[i] = max(jlong(0), min(patternMillis[i],
1292 MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL)) * 1000000LL;
1293 }
1294 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1295
1296 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1297}
1298
1299static void nativeCancelVibrate(JNIEnv* env,
1300 jclass clazz, jint ptr, jint deviceId, jint token) {
1301 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1302
1303 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1304}
1305
Jeff Brown6ec6f792012-04-17 16:52:41 -07001306static void nativeReloadKeyboardLayouts(JNIEnv* env,
1307 jclass clazz, jint ptr) {
1308 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1309
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001310 im->getInputManager()->getReader()->requestRefreshConfiguration(
1311 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1312}
1313
1314static void nativeReloadDeviceAliases(JNIEnv* env,
1315 jclass clazz, jint ptr) {
1316 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1317
1318 im->getInputManager()->getReader()->requestRefreshConfiguration(
1319 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001320}
1321
Jeff Brown4532e612012-04-05 14:27:12 -07001322static jstring nativeDump(JNIEnv* env, jclass clazz, jint ptr) {
1323 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001324
Jeff Brownb88102f2010-09-08 11:49:43 -07001325 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001326 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001327 return env->NewStringUTF(dump.string());
1328}
1329
Jeff Brown4532e612012-04-05 14:27:12 -07001330static void nativeMonitor(JNIEnv* env, jclass clazz, jint ptr) {
1331 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001332
Jeff Brown4532e612012-04-05 14:27:12 -07001333 im->getInputManager()->getReader()->monitor();
1334 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001335}
1336
Jeff Brown9c3cda02010-06-15 01:31:58 -07001337// ----------------------------------------------------------------------------
1338
Jeff Brown46b9ac02010-04-22 18:58:52 -07001339static JNINativeMethod gInputManagerMethods[] = {
1340 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001341 { "nativeInit",
1342 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)I",
1343 (void*) nativeInit },
1344 { "nativeStart", "(I)V",
1345 (void*) nativeStart },
1346 { "nativeSetDisplaySize", "(IIIIII)V",
1347 (void*) nativeSetDisplaySize },
1348 { "nativeSetDisplayOrientation", "(III)V",
1349 (void*) nativeSetDisplayOrientation },
1350 { "nativeGetScanCodeState", "(IIII)I",
1351 (void*) nativeGetScanCodeState },
1352 { "nativeGetKeyCodeState", "(IIII)I",
1353 (void*) nativeGetKeyCodeState },
1354 { "nativeGetSwitchState", "(IIII)I",
1355 (void*) nativeGetSwitchState },
1356 { "nativeHasKeys", "(III[I[Z)Z",
1357 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001358 { "nativeRegisterInputChannel",
Jeff Brown4532e612012-04-05 14:27:12 -07001359 "(ILandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
1360 (void*) nativeRegisterInputChannel },
1361 { "nativeUnregisterInputChannel", "(ILandroid/view/InputChannel;)V",
1362 (void*) nativeUnregisterInputChannel },
1363 { "nativeSetInputFilterEnabled", "(IZ)V",
1364 (void*) nativeSetInputFilterEnabled },
1365 { "nativeInjectInputEvent", "(ILandroid/view/InputEvent;IIIII)I",
1366 (void*) nativeInjectInputEvent },
1367 { "nativeSetInputWindows", "(I[Lcom/android/server/input/InputWindowHandle;)V",
1368 (void*) nativeSetInputWindows },
1369 { "nativeSetFocusedApplication", "(ILcom/android/server/input/InputApplicationHandle;)V",
1370 (void*) nativeSetFocusedApplication },
1371 { "nativeSetInputDispatchMode", "(IZZ)V",
1372 (void*) nativeSetInputDispatchMode },
1373 { "nativeSetSystemUiVisibility", "(II)V",
1374 (void*) nativeSetSystemUiVisibility },
Jeff Brown4532e612012-04-05 14:27:12 -07001375 { "nativeGetInputConfiguration", "(ILandroid/content/res/Configuration;)V",
1376 (void*) nativeGetInputConfiguration },
1377 { "nativeTransferTouchFocus", "(ILandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1378 (void*) nativeTransferTouchFocus },
1379 { "nativeSetPointerSpeed", "(II)V",
1380 (void*) nativeSetPointerSpeed },
1381 { "nativeSetShowTouches", "(IZ)V",
1382 (void*) nativeSetShowTouches },
Jeff Browna47425a2012-04-13 04:09:27 -07001383 { "nativeVibrate", "(II[JII)V",
1384 (void*) nativeVibrate },
1385 { "nativeCancelVibrate", "(III)V",
1386 (void*) nativeCancelVibrate },
Jeff Brown6ec6f792012-04-17 16:52:41 -07001387 { "nativeReloadKeyboardLayouts", "(I)V",
1388 (void*) nativeReloadKeyboardLayouts },
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001389 { "nativeReloadDeviceAliases", "(I)V",
1390 (void*) nativeReloadDeviceAliases },
Jeff Brown4532e612012-04-05 14:27:12 -07001391 { "nativeDump", "(I)Ljava/lang/String;",
1392 (void*) nativeDump },
1393 { "nativeMonitor", "(I)V",
1394 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001395};
1396
1397#define FIND_CLASS(var, className) \
1398 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001399 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001400
1401#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1402 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1403 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1404
1405#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1406 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1407 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1408
1409int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001410 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001411 gInputManagerMethods, NELEM(gInputManagerMethods));
1412 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1413
Jeff Brown9c3cda02010-06-15 01:31:58 -07001414 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001415
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001416 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001417 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001418
Jeff Brown4532e612012-04-05 14:27:12 -07001419 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001420 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001421
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001422 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1423 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1424
Jeff Brown4532e612012-04-05 14:27:12 -07001425 GET_METHOD_ID(gServiceClassInfo.notifyLidSwitchChanged, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001426 "notifyLidSwitchChanged", "(JZ)V");
1427
Jeff Brown4532e612012-04-05 14:27:12 -07001428 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1429 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001430
Jeff Brown4532e612012-04-05 14:27:12 -07001431 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001432 "notifyANR",
Jeff Brown4532e612012-04-05 14:27:12 -07001433 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001434
Jeff Brown4532e612012-04-05 14:27:12 -07001435 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001436 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1437
Jeff Brown4532e612012-04-05 14:27:12 -07001438 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001439 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001440
Jeff Brown4532e612012-04-05 14:27:12 -07001441 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001442 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001443 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1444
Jeff Brown4532e612012-04-05 14:27:12 -07001445 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001446 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001447 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001448
Jeff Brown4532e612012-04-05 14:27:12 -07001449 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001450 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001451 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001452
Jeff Brown4532e612012-04-05 14:27:12 -07001453 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001454 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001455
Jeff Brown4532e612012-04-05 14:27:12 -07001456 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001457 "getVirtualKeyQuietTimeMillis", "()I");
1458
Jeff Brown4532e612012-04-05 14:27:12 -07001459 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001460 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1461
Jeff Brown4532e612012-04-05 14:27:12 -07001462 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001463 "getKeyRepeatTimeout", "()I");
1464
Jeff Brown4532e612012-04-05 14:27:12 -07001465 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001466 "getKeyRepeatDelay", "()I");
1467
Jeff Brown4532e612012-04-05 14:27:12 -07001468 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001469 "getHoverTapTimeout", "()I");
1470
Jeff Brown4532e612012-04-05 14:27:12 -07001471 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001472 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001473
Jeff Brown4532e612012-04-05 14:27:12 -07001474 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001475 "getDoubleTapTimeout", "()I");
1476
Jeff Brown4532e612012-04-05 14:27:12 -07001477 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001478 "getLongPressTimeout", "()I");
1479
Jeff Brown4532e612012-04-05 14:27:12 -07001480 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001481 "getPointerLayer", "()I");
1482
Jeff Brown4532e612012-04-05 14:27:12 -07001483 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001484 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001485
Jeff Brown6ec6f792012-04-17 16:52:41 -07001486 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
1487 "getKeyboardLayoutOverlay", "(Ljava/lang/String;)[Ljava/lang/String;");
1488
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001489 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1490 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1491
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001492 // InputDevice
1493
1494 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1495 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1496
Jeff Brown6ec402b2010-07-28 15:48:59 -07001497 // KeyEvent
1498
1499 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001500 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1501
Jeff Brown8d608662010-08-30 03:02:23 -07001502 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001503
1504 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001505 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001506
Jeff Brown57c59372010-09-21 18:22:55 -07001507 // Configuration
1508
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001509 FIND_CLASS(clazz, "android/content/res/Configuration");
Jeff Brown57c59372010-09-21 18:22:55 -07001510
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001511 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001512 "touchscreen", "I");
1513
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001514 GET_FIELD_ID(gConfigurationClassInfo.keyboard, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001515 "keyboard", "I");
1516
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001517 GET_FIELD_ID(gConfigurationClassInfo.navigation, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001518 "navigation", "I");
1519
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520 return 0;
1521}
1522
Jeff Brown46b9ac02010-04-22 18:58:52 -07001523} /* namespace android */