blob: b361a267f472542c428f69fc281deae82da950b0 [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 Brown4532e612012-04-05 14:27:12 -070086} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070087
88static struct {
89 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070090} gInputDeviceClassInfo;
91
92static struct {
93 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070094} gKeyEventClassInfo;
95
96static struct {
97 jclass clazz;
98} gMotionEventClassInfo;
99
Jeff Brown8d608662010-08-30 03:02:23 -0700100static struct {
Jeff Brown57c59372010-09-21 18:22:55 -0700101 jfieldID touchscreen;
102 jfieldID keyboard;
103 jfieldID navigation;
104} gConfigurationClassInfo;
105
Jeff Brown928e0542011-01-10 11:17:36 -0800106
107// --- Global functions ---
108
Jeff Brown214eaf42011-05-26 19:17:02 -0700109template<typename T>
110inline static T min(const T& a, const T& b) {
111 return a < b ? a : b;
112}
113
114template<typename T>
115inline static T max(const T& a, const T& b) {
116 return a > b ? a : b;
117}
118
Jeff Brown928e0542011-01-10 11:17:36 -0800119static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
120 const sp<InputApplicationHandle>& inputApplicationHandle) {
121 if (inputApplicationHandle == NULL) {
122 return NULL;
123 }
124 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
125 getInputApplicationHandleObjLocalRef(env);
126}
127
128static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
129 const sp<InputWindowHandle>& inputWindowHandle) {
130 if (inputWindowHandle == NULL) {
131 return NULL;
132 }
133 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
134 getInputWindowHandleObjLocalRef(env);
135}
136
Jeff Brown2352b972011-04-12 22:39:53 -0700137static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
138 SpriteIcon* outSpriteIcon) {
139 PointerIcon pointerIcon;
140 status_t status = android_view_PointerIcon_loadSystemIcon(env,
141 contextObj, style, &pointerIcon);
142 if (!status) {
143 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
144 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
145 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
146 }
147}
148
Jeff Brown905805a2011-10-12 13:57:59 -0700149enum {
150 WM_ACTION_PASS_TO_USER = 1,
151 WM_ACTION_POKE_USER_ACTIVITY = 2,
152 WM_ACTION_GO_TO_SLEEP = 4,
153};
154
Jeff Brown928e0542011-01-10 11:17:36 -0800155
156// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800157
Jeff Brown9c3cda02010-06-15 01:31:58 -0700158class NativeInputManager : public virtual RefBase,
159 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700160 public virtual InputDispatcherPolicyInterface,
161 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162protected:
163 virtual ~NativeInputManager();
164
165public:
Jeff Brown4532e612012-04-05 14:27:12 -0700166 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167
168 inline sp<InputManager> getInputManager() const { return mInputManager; }
169
Jeff Brownb88102f2010-09-08 11:49:43 -0700170 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700171
Jeff Brownbc68a592011-07-25 12:58:12 -0700172 void setDisplaySize(int32_t displayId, int32_t width, int32_t height,
173 int32_t externalWidth, int32_t externalHeight);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700174 void setDisplayOrientation(int32_t displayId, int32_t orientation);
175
Jeff Brown7fbdc842010-06-17 20:52:56 -0700176 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800177 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700178 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
179
Jeff Brown9302c872011-07-13 22:51:29 -0700180 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
181 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700182 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800183 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700184 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700185 void setShowTouches(bool enabled);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700186 void reloadKeyboardLayouts();
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 Brown9c3cda02010-06-15 01:31:58 -0700194
195 /* --- InputDispatcherPolicyInterface implementation --- */
196
Jeff Browne20c9e02010-10-11 14:20:19 -0700197 virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
198 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700199 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700200 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800201 const sp<InputWindowHandle>& inputWindowHandle);
202 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700203 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700204 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
205 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800206 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800207 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700208 virtual nsecs_t interceptKeyBeforeDispatching(
209 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700210 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800211 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800212 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700213 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700214 virtual bool checkInjectEventsPermissionNonReentrant(
215 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700216
Jeff Brown2352b972011-04-12 22:39:53 -0700217 /* --- PointerControllerPolicyInterface implementation --- */
218
219 virtual void loadPointerResources(PointerResources* outResources);
220
Jeff Brown9c3cda02010-06-15 01:31:58 -0700221private:
222 sp<InputManager> mInputManager;
223
Jeff Brown2352b972011-04-12 22:39:53 -0700224 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700225 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800226 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700227
Jeff Brown83c09682010-12-23 17:50:18 -0800228 Mutex mLock;
229 struct Locked {
230 // Display size information.
Jeff Brownbc68a592011-07-25 12:58:12 -0700231 int32_t displayWidth, displayHeight; // -1 when not initialized
232 int32_t displayExternalWidth, displayExternalHeight; // -1 when not initialized
Jeff Brown83c09682010-12-23 17:50:18 -0800233 int32_t displayOrientation;
234
Jeff Brown05dc66a2011-03-02 14:41:58 -0800235 // System UI visibility.
236 int32_t systemUiVisibility;
237
Jeff Brown1a84fd12011-06-02 01:26:32 -0700238 // Pointer speed.
239 int32_t pointerSpeed;
240
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 // True if pointer gestures are enabled.
242 bool pointerGesturesEnabled;
243
Jeff Browndaf4a122011-08-26 17:14:14 -0700244 // Show touches feature enable/disable.
245 bool showTouches;
246
Jeff Brown5541de92011-04-11 11:54:25 -0700247 // Sprite controller singleton, created on first use.
248 sp<SpriteController> spriteController;
249
Jeff Brown83c09682010-12-23 17:50:18 -0800250 // Pointer controller singleton, created and destroyed as needed.
251 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800252 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700253
Jeff Brown2352b972011-04-12 22:39:53 -0700254 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800255 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700256 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800257
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700258 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700259 bool isScreenOn();
260 bool isScreenBright();
261
Jeff Brownb88102f2010-09-08 11:49:43 -0700262 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700263
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264 static inline JNIEnv* jniEnv() {
265 return AndroidRuntime::getJNIEnv();
266 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700267};
268
Jeff Brown928e0542011-01-10 11:17:36 -0800269
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270
Jeff Brown2352b972011-04-12 22:39:53 -0700271NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700272 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700273 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274 JNIEnv* env = jniEnv();
275
Jeff Brown2352b972011-04-12 22:39:53 -0700276 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700277 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700278
Jeff Brown83c09682010-12-23 17:50:18 -0800279 {
280 AutoMutex _l(mLock);
281 mLocked.displayWidth = -1;
282 mLocked.displayHeight = -1;
Jeff Brownbc68a592011-07-25 12:58:12 -0700283 mLocked.displayExternalWidth = -1;
284 mLocked.displayExternalHeight = -1;
Jeff Brown65fd2512011-08-18 11:20:58 -0700285 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800286
287 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700288 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700289 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700290 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800291 }
292
Jeff Brown9c3cda02010-06-15 01:31:58 -0700293 sp<EventHub> eventHub = new EventHub();
294 mInputManager = new InputManager(eventHub, this, this);
295}
296
297NativeInputManager::~NativeInputManager() {
298 JNIEnv* env = jniEnv();
299
Jeff Brown2352b972011-04-12 22:39:53 -0700300 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700301 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700302}
303
Jeff Brownb88102f2010-09-08 11:49:43 -0700304void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700305 mInputManager->getReader()->dump(dump);
306 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700307
Jeff Brownb88102f2010-09-08 11:49:43 -0700308 mInputManager->getDispatcher()->dump(dump);
309 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310}
311
Jeff Brown7fbdc842010-06-17 20:52:56 -0700312bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000314 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700315 LOGE_EX(env);
316 env->ExceptionClear();
317 return true;
318 }
319 return false;
320}
321
Jeff Brownbc68a592011-07-25 12:58:12 -0700322void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height,
323 int32_t externalWidth, int32_t externalHeight) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700324 bool changed = false;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325 if (displayId == 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700326 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700327
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
329 changed = true;
330 mLocked.displayWidth = width;
331 mLocked.displayHeight = height;
Jeff Brownbc68a592011-07-25 12:58:12 -0700332
Jeff Brown65fd2512011-08-18 11:20:58 -0700333 sp<PointerController> controller = mLocked.pointerController.promote();
334 if (controller != NULL) {
335 controller->setDisplaySize(width, height);
Jeff Brown2352b972011-04-12 22:39:53 -0700336 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700337 }
Jeff Brown2352b972011-04-12 22:39:53 -0700338
Jeff Brown65fd2512011-08-18 11:20:58 -0700339 if (mLocked.displayExternalWidth != externalWidth
340 || mLocked.displayExternalHeight != externalHeight) {
341 changed = true;
Jeff Brownbc68a592011-07-25 12:58:12 -0700342 mLocked.displayExternalWidth = externalWidth;
343 mLocked.displayExternalHeight = externalHeight;
Jeff Brown65fd2512011-08-18 11:20:58 -0700344 }
345 }
346
347 if (changed) {
348 mInputManager->getReader()->requestRefreshConfiguration(
349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700350 }
351}
352
353void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700354 bool changed = false;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700355 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800356 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700357
Jeff Brown83c09682010-12-23 17:50:18 -0800358 if (mLocked.displayOrientation != orientation) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700359 changed = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800360 mLocked.displayOrientation = orientation;
361
362 sp<PointerController> controller = mLocked.pointerController.promote();
363 if (controller != NULL) {
364 controller->setDisplayOrientation(orientation);
365 }
366 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700367 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700368
369 if (changed) {
370 mInputManager->getReader()->requestRefreshConfiguration(
371 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
372 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700373}
374
Jeff Brown7fbdc842010-06-17 20:52:56 -0700375status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800376 const sp<InputChannel>& inputChannel,
377 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
378 return mInputManager->getDispatcher()->registerInputChannel(
379 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700380}
381
382status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
383 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700384 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700385}
386
Jeff Brown214eaf42011-05-26 19:17:02 -0700387void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700388 JNIEnv* env = jniEnv();
389
Jeff Brown4532e612012-04-05 14:27:12 -0700390 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
391 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700392 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
393 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
394 }
395
396 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700397 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
398 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700399 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
400 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700401 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700402 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700403 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700404 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700405 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700406 env->DeleteLocalRef(item);
407 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700408 env->DeleteLocalRef(excludedDeviceNames);
409 }
410
Jeff Brown4532e612012-04-05 14:27:12 -0700411 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
412 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700413 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700414 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
415 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700416 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700417 jint longPressTimeout = env->CallIntMethod(mServiceObj,
418 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700419 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700420 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700421
422 // We must ensure that the tap-drag interval is significantly shorter than
423 // the long-press timeout because the tap is held down for the entire duration
424 // of the double-tap timeout.
425 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700426 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700427 outConfig->pointerGestureTapDragInterval =
428 milliseconds_to_nanoseconds(tapDragInterval);
429 }
430 }
431 }
432
Jeff Brown4532e612012-04-05 14:27:12 -0700433 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
434 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700435 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
436 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700438
439 { // acquire lock
440 AutoMutex _l(mLock);
441
442 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
443 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700444 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700445
Jeff Browndaf4a122011-08-26 17:14:14 -0700446 outConfig->showTouches = mLocked.showTouches;
447
Jeff Brown65fd2512011-08-18 11:20:58 -0700448 outConfig->setDisplayInfo(0, false /*external*/,
449 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);
450 outConfig->setDisplayInfo(0, true /*external*/,
451 mLocked.displayExternalWidth, mLocked.displayExternalHeight,
452 mLocked.displayOrientation);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700453 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700454}
455
Jeff Brown83c09682010-12-23 17:50:18 -0800456sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
457 AutoMutex _l(mLock);
458
459 sp<PointerController> controller = mLocked.pointerController.promote();
460 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700461 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800462
Jeff Brown2352b972011-04-12 22:39:53 -0700463 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800464 mLocked.pointerController = controller;
465
466 controller->setDisplaySize(mLocked.displayWidth, mLocked.displayHeight);
467 controller->setDisplayOrientation(mLocked.displayOrientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800468
Jeff Brown5541de92011-04-11 11:54:25 -0700469 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700470 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
471 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700472 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
473 PointerIcon pointerIcon;
474 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
475 mContextObj, &pointerIcon);
476 if (!status && !pointerIcon.isNullIcon()) {
477 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
478 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
479 } else {
480 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800481 }
Jeff Brown2352b972011-04-12 22:39:53 -0700482 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800483 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800484
Jeff Brown2352b972011-04-12 22:39:53 -0700485 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800486 }
487 return controller;
488}
489
Jeff Brown5541de92011-04-11 11:54:25 -0700490void NativeInputManager::ensureSpriteControllerLocked() {
491 if (mLocked.spriteController == NULL) {
492 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700493 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700494 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
495 layer = -1;
496 }
497 mLocked.spriteController = new SpriteController(mLooper, layer);
498 }
499}
500
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700501void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
502 JNIEnv* env = jniEnv();
503
504 size_t count = inputDevices.size();
505 jobjectArray inputDevicesObjArray = env->NewObjectArray(
506 count, gInputDeviceClassInfo.clazz, NULL);
507 if (inputDevicesObjArray) {
508 bool error = false;
509 for (size_t i = 0; i < count; i++) {
510 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
511 if (!inputDeviceObj) {
512 error = true;
513 break;
514 }
515
516 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
517 env->DeleteLocalRef(inputDeviceObj);
518 }
519
520 if (!error) {
521 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
522 inputDevicesObjArray);
523 }
524
525 env->DeleteLocalRef(inputDevicesObjArray);
526 }
527
528 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
529}
530
Jeff Brown6ec6f792012-04-17 16:52:41 -0700531sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
532 const String8& inputDeviceDescriptor) {
533 JNIEnv* env = jniEnv();
534
535 sp<KeyCharacterMap> result;
536 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
537 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
538 gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get())));
539 if (arrayObj.get()) {
540 ScopedLocalRef<jstring> filenameObj(env,
541 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
542 ScopedLocalRef<jstring> contentsObj(env,
543 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
544 ScopedUtfChars filenameChars(env, filenameObj.get());
545 ScopedUtfChars contentsChars(env, contentsObj.get());
546
547 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
548 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
549 }
550 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
551 return result;
552}
553
Jeff Browne20c9e02010-10-11 14:20:19 -0700554void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
555 int32_t switchValue, uint32_t policyFlags) {
556#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000557 ALOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
Jeff Browne20c9e02010-10-11 14:20:19 -0700558 when, switchCode, switchValue, policyFlags);
559#endif
560
561 JNIEnv* env = jniEnv();
562
563 switch (switchCode) {
564 case SW_LID:
Jeff Brown27fd3422012-04-09 11:05:16 -0700565 // When switch value is set indicates lid is closed.
Jeff Brown4532e612012-04-05 14:27:12 -0700566 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyLidSwitchChanged,
Jeff Brown27fd3422012-04-09 11:05:16 -0700567 when, switchValue == 0 /*lidOpen*/);
Jeff Browne20c9e02010-10-11 14:20:19 -0700568 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
569 break;
570 }
571}
572
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
574#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000575 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700576#endif
577
578 JNIEnv* env = jniEnv();
579
Jeff Brown4532e612012-04-05 14:27:12 -0700580 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700581 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700582}
583
Jeff Brown519e0242010-09-15 15:18:56 -0700584nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800585 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700586#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000587 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700588#endif
589
590 JNIEnv* env = jniEnv();
591
Jeff Brown928e0542011-01-10 11:17:36 -0800592 jobject inputApplicationHandleObj =
593 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
594 jobject inputWindowHandleObj =
595 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700596
Jeff Brown4532e612012-04-05 14:27:12 -0700597 jlong newTimeout = env->CallLongMethod(mServiceObj,
598 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700599 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
600 newTimeout = 0; // abort dispatch
601 } else {
602 assert(newTimeout >= 0);
603 }
604
Jeff Brown928e0542011-01-10 11:17:36 -0800605 env->DeleteLocalRef(inputWindowHandleObj);
606 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700607 return newTimeout;
608}
609
Jeff Brown928e0542011-01-10 11:17:36 -0800610void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700611#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000612 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700613#endif
614
Jeff Brown7fbdc842010-06-17 20:52:56 -0700615 JNIEnv* env = jniEnv();
616
Jeff Brown928e0542011-01-10 11:17:36 -0800617 jobject inputWindowHandleObj =
618 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
619 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700620 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800621 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700622 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
623
Jeff Brown928e0542011-01-10 11:17:36 -0800624 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700625 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700626}
627
Jeff Brown214eaf42011-05-26 19:17:02 -0700628void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
629 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800630
Jeff Brown4532e612012-04-05 14:27:12 -0700631 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
632 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700633 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
634 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
635 }
Jeff Browna4547672011-03-02 21:38:11 -0800636
Jeff Brown4532e612012-04-05 14:27:12 -0700637 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
638 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700639 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
640 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
641 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700642}
643
Jeff Brown214eaf42011-05-26 19:17:02 -0700644bool NativeInputManager::isKeyRepeatEnabled() {
645 // Only enable automatic key repeating when the screen is on.
646 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700647}
648
Jeff Brown9302c872011-07-13 22:51:29 -0700649void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
650 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700651
Jeff Brown9302c872011-07-13 22:51:29 -0700652 if (windowHandleObjArray) {
653 jsize length = env->GetArrayLength(windowHandleObjArray);
654 for (jsize i = 0; i < length; i++) {
655 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
656 if (! windowHandleObj) {
657 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700658 }
Jeff Brown9302c872011-07-13 22:51:29 -0700659
660 sp<InputWindowHandle> windowHandle =
661 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
662 if (windowHandle != NULL) {
663 windowHandles.push(windowHandle);
664 }
665 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700666 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700667 }
Jeff Brown349703e2010-06-22 01:27:15 -0700668
Jeff Brown9302c872011-07-13 22:51:29 -0700669 mInputManager->getDispatcher()->setInputWindows(windowHandles);
670
671 // Do this after the dispatcher has updated the window handle state.
672 bool newPointerGesturesEnabled = true;
673 size_t numWindows = windowHandles.size();
674 for (size_t i = 0; i < numWindows; i++) {
675 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700676 const InputWindowInfo* windowInfo = windowHandle->getInfo();
677 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
678 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700679 newPointerGesturesEnabled = false;
680 }
681 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700682
683 uint32_t changes = 0;
684 { // acquire lock
685 AutoMutex _l(mLock);
686
687 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
688 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
689 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
690 }
691 } // release lock
692
693 if (changes) {
694 mInputManager->getReader()->requestRefreshConfiguration(changes);
695 }
Jeff Brown349703e2010-06-22 01:27:15 -0700696}
697
Jeff Brown9302c872011-07-13 22:51:29 -0700698void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
699 sp<InputApplicationHandle> applicationHandle =
700 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
701 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700702}
703
704void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700705 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700706}
707
Jeff Brown05dc66a2011-03-02 14:41:58 -0800708void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
709 AutoMutex _l(mLock);
710
711 if (mLocked.systemUiVisibility != visibility) {
712 mLocked.systemUiVisibility = visibility;
713
714 sp<PointerController> controller = mLocked.pointerController.promote();
715 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700716 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800717 }
718 }
719}
720
Jeff Brown2352b972011-04-12 22:39:53 -0700721void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800722 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700723 controller->setInactivityTimeout(lightsOut
724 ? PointerController::INACTIVITY_TIMEOUT_SHORT
725 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800726}
727
Jeff Brown1a84fd12011-06-02 01:26:32 -0700728void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700729 { // acquire lock
730 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700731
Jeff Brown474dcb52011-06-14 20:22:50 -0700732 if (mLocked.pointerSpeed == speed) {
733 return;
734 }
735
Steve Block6215d3f2012-01-04 20:05:49 +0000736 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700737 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700738 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700739
Jeff Brown474dcb52011-06-14 20:22:50 -0700740 mInputManager->getReader()->requestRefreshConfiguration(
741 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700742}
743
Jeff Browndaf4a122011-08-26 17:14:14 -0700744void NativeInputManager::setShowTouches(bool enabled) {
745 { // acquire lock
746 AutoMutex _l(mLock);
747
748 if (mLocked.showTouches == enabled) {
749 return;
750 }
751
Steve Block6215d3f2012-01-04 20:05:49 +0000752 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700753 mLocked.showTouches = enabled;
754 } // release lock
755
756 mInputManager->getReader()->requestRefreshConfiguration(
757 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
758}
759
Jeff Brown6ec6f792012-04-17 16:52:41 -0700760void NativeInputManager::reloadKeyboardLayouts() {
761 mInputManager->getReader()->requestRefreshConfiguration(
762 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
763}
764
Jeff Browne20c9e02010-10-11 14:20:19 -0700765bool NativeInputManager::isScreenOn() {
766 return android_server_PowerManagerService_isScreenOn();
767}
768
769bool NativeInputManager::isScreenBright() {
770 return android_server_PowerManagerService_isScreenBright();
771}
772
Jeff Brown0029c662011-03-30 02:25:18 -0700773bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
774 jobject inputEventObj;
775
776 JNIEnv* env = jniEnv();
777 switch (inputEvent->getType()) {
778 case AINPUT_EVENT_TYPE_KEY:
779 inputEventObj = android_view_KeyEvent_fromNative(env,
780 static_cast<const KeyEvent*>(inputEvent));
781 break;
782 case AINPUT_EVENT_TYPE_MOTION:
783 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
784 static_cast<const MotionEvent*>(inputEvent));
785 break;
786 default:
787 return true; // dispatch the event normally
788 }
789
790 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000791 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700792 return true; // dispatch the event normally
793 }
794
795 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700796 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700797 inputEventObj, policyFlags);
798 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
799 pass = true;
800 }
801 env->DeleteLocalRef(inputEventObj);
802 return pass;
803}
804
Jeff Brown1f245102010-11-18 20:53:46 -0800805void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
806 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700807 // Policy:
808 // - Ignore untrusted events and pass them along.
809 // - Ask the window manager what to do with normal events and trusted injected events.
810 // - For normal events wake and brighten the screen if currently off or dim.
811 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800812 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700813 bool isScreenOn = this->isScreenOn();
814 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700815
Jeff Brown3122e442010-10-11 23:32:49 -0700816 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800817 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
818 jint wmActions;
819 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700820 wmActions = env->CallIntMethod(mServiceObj,
821 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800822 keyEventObj, policyFlags, isScreenOn);
823 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
824 wmActions = 0;
825 }
826 android_view_KeyEvent_recycle(env, keyEventObj);
827 env->DeleteLocalRef(keyEventObj);
828 } else {
Steve Block3762c312012-01-06 19:20:56 +0000829 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700830 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700831 }
832
Jeff Brown1f245102010-11-18 20:53:46 -0800833 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700834 if (!isScreenOn) {
835 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700836 }
837
838 if (!isScreenBright) {
839 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
840 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700841 }
842
Jeff Brown56194eb2011-03-02 19:23:13 -0800843 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700844 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700845 policyFlags |= POLICY_FLAG_PASS_TO_USER;
846 }
847}
848
Jeff Brown56194eb2011-03-02 19:23:13 -0800849void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700850 // Policy:
851 // - Ignore untrusted events and pass them along.
852 // - No special filtering for injected events required at this time.
853 // - Filter normal events based on screen state.
854 // - For normal events brighten (but do not wake) the screen if currently dim.
855 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
856 if (isScreenOn()) {
857 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700858
Jeff Brown3122e442010-10-11 23:32:49 -0700859 if (!isScreenBright()) {
860 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
861 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800862 } else {
863 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700864 jint wmActions = env->CallIntMethod(mServiceObj,
865 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown56194eb2011-03-02 19:23:13 -0800866 policyFlags);
867 if (checkAndClearExceptionFromCallback(env,
868 "interceptMotionBeforeQueueingWhenScreenOff")) {
869 wmActions = 0;
870 }
871
872 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
873 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700874 }
Jeff Brown3122e442010-10-11 23:32:49 -0700875 } else {
876 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700877 }
878}
879
Jeff Brown56194eb2011-03-02 19:23:13 -0800880void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
881 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800882 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800883#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000884 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800885#endif
886 android_server_PowerManagerService_goToSleep(when);
887 }
888
889 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800890#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000891 ALOGD("handleInterceptActions: Poking user activity.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800892#endif
893 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
894 }
895
896 if (wmActions & WM_ACTION_PASS_TO_USER) {
897 policyFlags |= POLICY_FLAG_PASS_TO_USER;
898 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800899#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000900 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800901#endif
902 }
903}
904
Jeff Brown905805a2011-10-12 13:57:59 -0700905nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800906 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700907 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700908 // Policy:
909 // - Ignore untrusted events and pass them along.
910 // - Filter normal events and trusted injected events through the window manager policy to
911 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700912 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700913 if (policyFlags & POLICY_FLAG_TRUSTED) {
914 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700915
Jeff Brown928e0542011-01-10 11:17:36 -0800916 // Note: inputWindowHandle may be null.
917 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800918 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
919 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700920 jlong delayMillis = env->CallLongMethod(mServiceObj,
921 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800922 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800923 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
924 android_view_KeyEvent_recycle(env, keyEventObj);
925 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700926 if (!error) {
927 if (delayMillis < 0) {
928 result = -1;
929 } else if (delayMillis > 0) {
930 result = milliseconds_to_nanoseconds(delayMillis);
931 }
932 }
Jeff Brown1f245102010-11-18 20:53:46 -0800933 } else {
Steve Block3762c312012-01-06 19:20:56 +0000934 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800935 }
Jeff Brown928e0542011-01-10 11:17:36 -0800936 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700937 }
Jeff Brown1f245102010-11-18 20:53:46 -0800938 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700939}
940
Jeff Brown928e0542011-01-10 11:17:36 -0800941bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800942 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700943 // Policy:
944 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800945 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700946 if (policyFlags & POLICY_FLAG_TRUSTED) {
947 JNIEnv* env = jniEnv();
948
Jeff Brown928e0542011-01-10 11:17:36 -0800949 // Note: inputWindowHandle may be null.
950 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800951 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
952 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700953 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
954 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800955 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700956 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
957 fallbackKeyEventObj = NULL;
958 }
Jeff Brown1f245102010-11-18 20:53:46 -0800959 android_view_KeyEvent_recycle(env, keyEventObj);
960 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800961
962 if (fallbackKeyEventObj) {
963 // Note: outFallbackKeyEvent may be the same object as keyEvent.
964 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
965 outFallbackKeyEvent)) {
966 result = true;
967 }
968 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
969 env->DeleteLocalRef(fallbackKeyEventObj);
970 }
Jeff Brown1f245102010-11-18 20:53:46 -0800971 } else {
Steve Block3762c312012-01-06 19:20:56 +0000972 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800973 }
Jeff Brown928e0542011-01-10 11:17:36 -0800974 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700975 }
Jeff Brown1f245102010-11-18 20:53:46 -0800976 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700977}
978
Jeff Brown01ce2e92010-09-26 22:20:12 -0700979void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
980 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700981}
982
Jeff Brown349703e2010-06-22 01:27:15 -0700983
Jeff Brownb88102f2010-09-08 11:49:43 -0700984bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
985 int32_t injectorPid, int32_t injectorUid) {
986 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700987 jboolean result = env->CallBooleanMethod(mServiceObj,
988 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700989 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
990 result = false;
991 }
Jeff Brown349703e2010-06-22 01:27:15 -0700992 return result;
993}
994
Jeff Brown2352b972011-04-12 22:39:53 -0700995void NativeInputManager::loadPointerResources(PointerResources* outResources) {
996 JNIEnv* env = jniEnv();
997
998 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
999 &outResources->spotHover);
1000 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1001 &outResources->spotTouch);
1002 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1003 &outResources->spotAnchor);
1004}
1005
Jeff Brown83c09682010-12-23 17:50:18 -08001006
Jeff Brown9c3cda02010-06-15 01:31:58 -07001007// ----------------------------------------------------------------------------
1008
Jeff Brown4532e612012-04-05 14:27:12 -07001009static jint nativeInit(JNIEnv* env, jclass clazz,
1010 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001011 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
1012 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1013 messageQueue->getLooper());
Jeff Brown4532e612012-04-05 14:27:12 -07001014 im->incStrong(serviceObj);
1015 return reinterpret_cast<jint>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001016}
1017
Jeff Brown4532e612012-04-05 14:27:12 -07001018static void nativeStart(JNIEnv* env, jclass clazz, jint ptr) {
1019 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001020
Jeff Brown4532e612012-04-05 14:27:12 -07001021 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001022 if (result) {
1023 jniThrowRuntimeException(env, "Input manager could not be started.");
1024 }
1025}
1026
Jeff Brown4532e612012-04-05 14:27:12 -07001027static void nativeSetDisplaySize(JNIEnv* env, jclass clazz, jint ptr,
Jeff Brownbc68a592011-07-25 12:58:12 -07001028 jint displayId, jint width, jint height, jint externalWidth, jint externalHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001029 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001030
1031 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
1032 // to be passed in like this, not sure which is better but leaving it like this
1033 // keeps the window manager in direct control of when display transitions propagate down
1034 // to the input dispatcher
Jeff Brown4532e612012-04-05 14:27:12 -07001035 im->setDisplaySize(displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001036}
1037
Jeff Brown4532e612012-04-05 14:27:12 -07001038static void nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
1039 jint ptr, jint displayId, jint orientation) {
1040 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001041
Jeff Brown4532e612012-04-05 14:27:12 -07001042 im->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001043}
1044
Jeff Brown4532e612012-04-05 14:27:12 -07001045static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
1046 jint ptr, jint deviceId, jint sourceMask, jint scanCode) {
1047 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001048
Jeff Brown4532e612012-04-05 14:27:12 -07001049 return im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001050 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001051}
1052
Jeff Brown4532e612012-04-05 14:27:12 -07001053static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
1054 jint ptr, jint deviceId, jint sourceMask, jint keyCode) {
1055 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001056
Jeff Brown4532e612012-04-05 14:27:12 -07001057 return im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001059}
1060
Jeff Brown4532e612012-04-05 14:27:12 -07001061static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
1062 jint ptr, jint deviceId, jint sourceMask, jint sw) {
1063 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001064
Jeff Brown4532e612012-04-05 14:27:12 -07001065 return im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001066 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001067}
1068
Jeff Brown4532e612012-04-05 14:27:12 -07001069static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
1070 jint ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
1071 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001072
1073 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1074 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1075 jsize numCodes = env->GetArrayLength(keyCodes);
1076 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001077 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brown4532e612012-04-05 14:27:12 -07001078 result = im->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001080 } else {
1081 result = JNI_FALSE;
1082 }
1083
1084 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1085 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1086 return result;
1087}
1088
1089static void throwInputChannelNotInitialized(JNIEnv* env) {
1090 jniThrowException(env, "java/lang/IllegalStateException",
1091 "inputChannel is not initialized");
1092}
1093
Jeff Brown4532e612012-04-05 14:27:12 -07001094static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001095 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001096 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1097
Steve Block8564c8d2012-01-05 23:22:43 +00001098 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001099 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001100 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101}
1102
Jeff Brown4532e612012-04-05 14:27:12 -07001103static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
1104 jint ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
1105 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001106
1107 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1108 inputChannelObj);
1109 if (inputChannel == NULL) {
1110 throwInputChannelNotInitialized(env);
1111 return;
1112 }
1113
Jeff Brown928e0542011-01-10 11:17:36 -08001114 sp<InputWindowHandle> inputWindowHandle =
1115 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001116
Jeff Brown4532e612012-04-05 14:27:12 -07001117 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001118 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001119 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001120 String8 message;
1121 message.appendFormat("Failed to register input channel. status=%d", status);
1122 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001123 return;
1124 }
1125
Jeff Browna41ca772010-08-11 14:46:32 -07001126 if (! monitor) {
1127 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001128 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001129 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001130}
1131
Jeff Brown4532e612012-04-05 14:27:12 -07001132static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1133 jint ptr, jobject inputChannelObj) {
1134 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001135
1136 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1137 inputChannelObj);
1138 if (inputChannel == NULL) {
1139 throwInputChannelNotInitialized(env);
1140 return;
1141 }
1142
1143 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1144
Jeff Brown4532e612012-04-05 14:27:12 -07001145 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001146 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1147 String8 message;
1148 message.appendFormat("Failed to unregister input channel. status=%d", status);
1149 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001150 }
1151}
1152
Jeff Brown4532e612012-04-05 14:27:12 -07001153static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
1154 jint ptr, jboolean enabled) {
1155 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001156
Jeff Brown4532e612012-04-05 14:27:12 -07001157 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001158}
1159
Jeff Brown4532e612012-04-05 14:27:12 -07001160static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1161 jint ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001162 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001163 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001164
Jeff Brown6ec402b2010-07-28 15:48:59 -07001165 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1166 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001167 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1168 if (status) {
1169 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1170 return INPUT_EVENT_INJECTION_FAILED;
1171 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001172
Jeff Brown4532e612012-04-05 14:27:12 -07001173 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001174 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1175 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001176 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001177 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1178 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001179 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1180 return INPUT_EVENT_INJECTION_FAILED;
1181 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001182
Jeff Brown4532e612012-04-05 14:27:12 -07001183 return im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001184 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1185 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001186 } else {
1187 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001188 return INPUT_EVENT_INJECTION_FAILED;
1189 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001190}
1191
Jeff Brown4532e612012-04-05 14:27:12 -07001192static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
1193 jint ptr, jobjectArray windowHandleObjArray) {
1194 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001195
Jeff Brown4532e612012-04-05 14:27:12 -07001196 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001197}
1198
Jeff Brown4532e612012-04-05 14:27:12 -07001199static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1200 jint ptr, jobject applicationHandleObj) {
1201 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001202
Jeff Brown4532e612012-04-05 14:27:12 -07001203 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001204}
1205
Jeff Brown4532e612012-04-05 14:27:12 -07001206static void nativeSetInputDispatchMode(JNIEnv* env,
1207 jclass clazz, jint ptr, jboolean enabled, jboolean frozen) {
1208 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001209
Jeff Brown4532e612012-04-05 14:27:12 -07001210 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001211}
1212
Jeff Brown4532e612012-04-05 14:27:12 -07001213static void nativeSetSystemUiVisibility(JNIEnv* env,
1214 jclass clazz, jint ptr, jint visibility) {
1215 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001216
Jeff Brown4532e612012-04-05 14:27:12 -07001217 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001218}
1219
Jeff Brown4532e612012-04-05 14:27:12 -07001220static void nativeGetInputConfiguration(JNIEnv* env,
1221 jclass clazz, jint ptr, jobject configObj) {
1222 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown57c59372010-09-21 18:22:55 -07001223
1224 InputConfiguration config;
Jeff Brown4532e612012-04-05 14:27:12 -07001225 im->getInputManager()->getReader()->getInputConfiguration(& config);
Jeff Brown57c59372010-09-21 18:22:55 -07001226
1227 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1228 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1229 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1230}
1231
Jeff Brown4532e612012-04-05 14:27:12 -07001232static jboolean nativeTransferTouchFocus(JNIEnv* env,
1233 jclass clazz, jint ptr, jobject fromChannelObj, jobject toChannelObj) {
1234 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001235
1236 sp<InputChannel> fromChannel =
1237 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1238 sp<InputChannel> toChannel =
1239 android_view_InputChannel_getInputChannel(env, toChannelObj);
1240
1241 if (fromChannel == NULL || toChannel == NULL) {
1242 return false;
1243 }
1244
Jeff Brown4532e612012-04-05 14:27:12 -07001245 return im->getInputManager()->getDispatcher()->
Jeff Browne6504122010-09-27 14:52:15 -07001246 transferTouchFocus(fromChannel, toChannel);
1247}
1248
Jeff Brown4532e612012-04-05 14:27:12 -07001249static void nativeSetPointerSpeed(JNIEnv* env,
1250 jclass clazz, jint ptr, jint speed) {
1251 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001252
Jeff Brown4532e612012-04-05 14:27:12 -07001253 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001254}
1255
Jeff Brown4532e612012-04-05 14:27:12 -07001256static void nativeSetShowTouches(JNIEnv* env,
1257 jclass clazz, jint ptr, jboolean enabled) {
1258 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001259
Jeff Brown4532e612012-04-05 14:27:12 -07001260 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001261}
1262
Jeff Browna47425a2012-04-13 04:09:27 -07001263static void nativeVibrate(JNIEnv* env,
1264 jclass clazz, jint ptr, jint deviceId, jlongArray patternObj,
1265 jint repeat, jint token) {
1266 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1267
1268 size_t patternSize = env->GetArrayLength(patternObj);
1269 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1270 ALOGI("Skipped requested vibration because the pattern size is %d "
1271 "which is more than the maximum supported size of %d.",
1272 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1273 return; // limit to reasonable size
1274 }
1275
1276 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1277 patternObj, NULL));
1278 nsecs_t pattern[patternSize];
1279 for (size_t i = 0; i < patternSize; i++) {
1280 pattern[i] = max(jlong(0), min(patternMillis[i],
1281 MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL)) * 1000000LL;
1282 }
1283 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1284
1285 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1286}
1287
1288static void nativeCancelVibrate(JNIEnv* env,
1289 jclass clazz, jint ptr, jint deviceId, jint token) {
1290 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1291
1292 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1293}
1294
Jeff Brown6ec6f792012-04-17 16:52:41 -07001295static void nativeReloadKeyboardLayouts(JNIEnv* env,
1296 jclass clazz, jint ptr) {
1297 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1298
1299 im->reloadKeyboardLayouts();
1300}
1301
Jeff Brown4532e612012-04-05 14:27:12 -07001302static jstring nativeDump(JNIEnv* env, jclass clazz, jint ptr) {
1303 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001304
Jeff Brownb88102f2010-09-08 11:49:43 -07001305 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001306 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001307 return env->NewStringUTF(dump.string());
1308}
1309
Jeff Brown4532e612012-04-05 14:27:12 -07001310static void nativeMonitor(JNIEnv* env, jclass clazz, jint ptr) {
1311 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001312
Jeff Brown4532e612012-04-05 14:27:12 -07001313 im->getInputManager()->getReader()->monitor();
1314 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001315}
1316
Jeff Brown9c3cda02010-06-15 01:31:58 -07001317// ----------------------------------------------------------------------------
1318
Jeff Brown46b9ac02010-04-22 18:58:52 -07001319static JNINativeMethod gInputManagerMethods[] = {
1320 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001321 { "nativeInit",
1322 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)I",
1323 (void*) nativeInit },
1324 { "nativeStart", "(I)V",
1325 (void*) nativeStart },
1326 { "nativeSetDisplaySize", "(IIIIII)V",
1327 (void*) nativeSetDisplaySize },
1328 { "nativeSetDisplayOrientation", "(III)V",
1329 (void*) nativeSetDisplayOrientation },
1330 { "nativeGetScanCodeState", "(IIII)I",
1331 (void*) nativeGetScanCodeState },
1332 { "nativeGetKeyCodeState", "(IIII)I",
1333 (void*) nativeGetKeyCodeState },
1334 { "nativeGetSwitchState", "(IIII)I",
1335 (void*) nativeGetSwitchState },
1336 { "nativeHasKeys", "(III[I[Z)Z",
1337 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001338 { "nativeRegisterInputChannel",
Jeff Brown4532e612012-04-05 14:27:12 -07001339 "(ILandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
1340 (void*) nativeRegisterInputChannel },
1341 { "nativeUnregisterInputChannel", "(ILandroid/view/InputChannel;)V",
1342 (void*) nativeUnregisterInputChannel },
1343 { "nativeSetInputFilterEnabled", "(IZ)V",
1344 (void*) nativeSetInputFilterEnabled },
1345 { "nativeInjectInputEvent", "(ILandroid/view/InputEvent;IIIII)I",
1346 (void*) nativeInjectInputEvent },
1347 { "nativeSetInputWindows", "(I[Lcom/android/server/input/InputWindowHandle;)V",
1348 (void*) nativeSetInputWindows },
1349 { "nativeSetFocusedApplication", "(ILcom/android/server/input/InputApplicationHandle;)V",
1350 (void*) nativeSetFocusedApplication },
1351 { "nativeSetInputDispatchMode", "(IZZ)V",
1352 (void*) nativeSetInputDispatchMode },
1353 { "nativeSetSystemUiVisibility", "(II)V",
1354 (void*) nativeSetSystemUiVisibility },
Jeff Brown4532e612012-04-05 14:27:12 -07001355 { "nativeGetInputConfiguration", "(ILandroid/content/res/Configuration;)V",
1356 (void*) nativeGetInputConfiguration },
1357 { "nativeTransferTouchFocus", "(ILandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1358 (void*) nativeTransferTouchFocus },
1359 { "nativeSetPointerSpeed", "(II)V",
1360 (void*) nativeSetPointerSpeed },
1361 { "nativeSetShowTouches", "(IZ)V",
1362 (void*) nativeSetShowTouches },
Jeff Browna47425a2012-04-13 04:09:27 -07001363 { "nativeVibrate", "(II[JII)V",
1364 (void*) nativeVibrate },
1365 { "nativeCancelVibrate", "(III)V",
1366 (void*) nativeCancelVibrate },
Jeff Brown6ec6f792012-04-17 16:52:41 -07001367 { "nativeReloadKeyboardLayouts", "(I)V",
1368 (void*) nativeReloadKeyboardLayouts },
Jeff Brown4532e612012-04-05 14:27:12 -07001369 { "nativeDump", "(I)Ljava/lang/String;",
1370 (void*) nativeDump },
1371 { "nativeMonitor", "(I)V",
1372 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001373};
1374
1375#define FIND_CLASS(var, className) \
1376 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001377 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001378
1379#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1380 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1381 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1382
1383#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1384 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1385 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1386
1387int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001388 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001389 gInputManagerMethods, NELEM(gInputManagerMethods));
1390 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1391
Jeff Brown9c3cda02010-06-15 01:31:58 -07001392 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001393
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001394 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001395 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001396
Jeff Brown4532e612012-04-05 14:27:12 -07001397 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001398 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001399
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001400 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1401 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1402
Jeff Brown4532e612012-04-05 14:27:12 -07001403 GET_METHOD_ID(gServiceClassInfo.notifyLidSwitchChanged, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001404 "notifyLidSwitchChanged", "(JZ)V");
1405
Jeff Brown4532e612012-04-05 14:27:12 -07001406 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1407 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001408
Jeff Brown4532e612012-04-05 14:27:12 -07001409 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001410 "notifyANR",
Jeff Brown4532e612012-04-05 14:27:12 -07001411 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001412
Jeff Brown4532e612012-04-05 14:27:12 -07001413 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001414 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1415
Jeff Brown4532e612012-04-05 14:27:12 -07001416 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001417 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001418
Jeff Brown4532e612012-04-05 14:27:12 -07001419 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001420 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001421 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1422
Jeff Brown4532e612012-04-05 14:27:12 -07001423 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001424 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001425 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001426
Jeff Brown4532e612012-04-05 14:27:12 -07001427 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001428 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001429 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001430
Jeff Brown4532e612012-04-05 14:27:12 -07001431 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001432 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001433
Jeff Brown4532e612012-04-05 14:27:12 -07001434 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001435 "getVirtualKeyQuietTimeMillis", "()I");
1436
Jeff Brown4532e612012-04-05 14:27:12 -07001437 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001438 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1439
Jeff Brown4532e612012-04-05 14:27:12 -07001440 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001441 "getKeyRepeatTimeout", "()I");
1442
Jeff Brown4532e612012-04-05 14:27:12 -07001443 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001444 "getKeyRepeatDelay", "()I");
1445
Jeff Brown4532e612012-04-05 14:27:12 -07001446 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001447 "getHoverTapTimeout", "()I");
1448
Jeff Brown4532e612012-04-05 14:27:12 -07001449 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001450 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001451
Jeff Brown4532e612012-04-05 14:27:12 -07001452 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001453 "getDoubleTapTimeout", "()I");
1454
Jeff Brown4532e612012-04-05 14:27:12 -07001455 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001456 "getLongPressTimeout", "()I");
1457
Jeff Brown4532e612012-04-05 14:27:12 -07001458 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001459 "getPointerLayer", "()I");
1460
Jeff Brown4532e612012-04-05 14:27:12 -07001461 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001462 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001463
Jeff Brown6ec6f792012-04-17 16:52:41 -07001464 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
1465 "getKeyboardLayoutOverlay", "(Ljava/lang/String;)[Ljava/lang/String;");
1466
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001467 // InputDevice
1468
1469 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1470 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1471
Jeff Brown6ec402b2010-07-28 15:48:59 -07001472 // KeyEvent
1473
1474 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001475 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1476
Jeff Brown8d608662010-08-30 03:02:23 -07001477 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001478
1479 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001480 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001481
Jeff Brown57c59372010-09-21 18:22:55 -07001482 // Configuration
1483
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001484 FIND_CLASS(clazz, "android/content/res/Configuration");
Jeff Brown57c59372010-09-21 18:22:55 -07001485
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001486 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001487 "touchscreen", "I");
1488
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001489 GET_FIELD_ID(gConfigurationClassInfo.keyboard, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001490 "keyboard", "I");
1491
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001492 GET_FIELD_ID(gConfigurationClassInfo.navigation, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001493 "navigation", "I");
1494
Jeff Brown46b9ac02010-04-22 18:58:52 -07001495 return 0;
1496}
1497
Jeff Brown46b9ac02010-04-22 18:58:52 -07001498} /* namespace android */