146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown/*
246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * Copyright (C) 2010 The Android Open Source Project
346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown *
446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * you may not use this file except in compliance with the License.
646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * You may obtain a copy of the License at
746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown *
846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown *
1046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * Unless required by applicable law or agreed to in writing, software
1146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
1246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * See the License for the specific language governing permissions and
1446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown * limitations under the License.
1546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown */
1646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#define LOG_TAG "InputManager-JNI"
1846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
199c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown//#define LOG_NDEBUG 0
209c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
219c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown// Log debug messages about InputReaderPolicy
22349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown#define DEBUG_INPUT_READER_POLICY 0
239c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
249c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown// Log debug messages about InputDispatcherPolicy
25349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown#define DEBUG_INPUT_DISPATCHER_POLICY 0
269c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
2783c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
2846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#include "JNIHelp.h"
2946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#include "jni.h"
30349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown#include <limits.h>
3146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#include <android_runtime/AndroidRuntime.h>
32b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
3346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#include <utils/Log.h>
3405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown#include <utils/Looper.h>
3546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown#include <utils/threads.h>
3683c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
37b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <input/InputManager.h>
38b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <input/PointerController.h>
395541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown#include <input/SpriteController.h>
40b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
4105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown#include <android_os_MessageQueue.h>
429f25b7fdf216c9ef0bd2322cd223eeaf0d60f77fJeff Brown#include <android_view_InputDevice.h>
43b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <android_view_KeyEvent.h>
44b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <android_view_MotionEvent.h>
45b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <android_view_InputChannel.h>
462352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown#include <android_view_PointerIcon.h>
47b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <android/graphics/GraphicsJNI.h>
48b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
496ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown#include <ScopedLocalRef.h>
506ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown#include <ScopedUtfChars.h>
516ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown
524f8ecd80296508a1dc69d3f3a23fd91e962c2784Jeff Brown#include "com_android_server_power_PowerManagerService.h"
534532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown#include "com_android_server_input_InputApplicationHandle.h"
544532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown#include "com_android_server_input_InputWindowHandle.h"
5546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
5646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brownnamespace android {
5746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
581a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown// The exponent used to calculate the pointer speed scaling factor.
591a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown// The scaling factor is calculated as 2 ^ (speed * exponent),
601a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown// where the speed ranges from -7 to + 7 and is supplied by the user.
61bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brownstatic const float POINTER_SPEED_EXPONENT = 1.0f / 4;
621a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
639c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brownstatic struct {
649c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    jmethodID notifyConfigurationChanged;
65af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    jmethodID notifyInputDevicesChanged;
665338428ddbe662283bf88171c7ca361d51d78da8Jeff Brown    jmethodID notifySwitch;
677fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    jmethodID notifyInputChannelBroken;
68349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    jmethodID notifyANR;
690029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    jmethodID filterInputEvent;
70349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    jmethodID interceptKeyBeforeQueueing;
7156194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    jmethodID interceptMotionBeforeQueueingWhenScreenOff;
72349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    jmethodID interceptKeyBeforeDispatching;
733915bb845b032dc184dba5e60970b803390ca3edJeff Brown    jmethodID dispatchUnhandledKey;
74349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    jmethodID checkInjectEventsPermission;
75fe50892af3b365806a767298dfd8e86447682581Jeff Brown    jmethodID getVirtualKeyQuietTimeMillis;
769c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    jmethodID getExcludedDeviceNames;
77a454767b09ecb7d25d00beae0e5a1fdd48605c63Jeff Brown    jmethodID getKeyRepeatTimeout;
78a454767b09ecb7d25d00beae0e5a1fdd48605c63Jeff Brown    jmethodID getKeyRepeatDelay;
79bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown    jmethodID getHoverTapTimeout;
80bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown    jmethodID getHoverTapSlop;
81214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    jmethodID getDoubleTapTimeout;
82214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    jmethodID getLongPressTimeout;
8383c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    jmethodID getPointerLayer;
84b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    jmethodID getPointerIcon;
856ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    jmethodID getKeyboardLayoutOverlay;
865bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    jmethodID getDeviceAlias;
874532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown} gServiceClassInfo;
889c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
899c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brownstatic struct {
909c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    jclass clazz;
91af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown} gInputDeviceClassInfo;
92af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
93af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brownstatic struct {
94af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    jclass clazz;
956ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown} gKeyEventClassInfo;
966ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown
976ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brownstatic struct {
986ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown    jclass clazz;
996ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown} gMotionEventClassInfo;
1006ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown
101928e054931d357326613c78e62f4d850b7c442ffJeff Brown
102928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- Global functions ---
103928e054931d357326613c78e62f4d850b7c442ffJeff Brown
104214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Browntemplate<typename T>
105214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Browninline static T min(const T& a, const T& b) {
106214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    return a < b ? a : b;
107214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown}
108214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown
109214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Browntemplate<typename T>
110214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Browninline static T max(const T& a, const T& b) {
111214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    return a > b ? a : b;
112214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown}
113214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown
114928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
115928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputApplicationHandle>& inputApplicationHandle) {
116928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (inputApplicationHandle == NULL) {
117928e054931d357326613c78e62f4d850b7c442ffJeff Brown        return NULL;
118928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
119928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
120928e054931d357326613c78e62f4d850b7c442ffJeff Brown            getInputApplicationHandleObjLocalRef(env);
121928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
122928e054931d357326613c78e62f4d850b7c442ffJeff Brown
123928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
124928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputWindowHandle>& inputWindowHandle) {
125928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (inputWindowHandle == NULL) {
126928e054931d357326613c78e62f4d850b7c442ffJeff Brown        return NULL;
127928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
128928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
129928e054931d357326613c78e62f4d850b7c442ffJeff Brown            getInputWindowHandleObjLocalRef(env);
130928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
131928e054931d357326613c78e62f4d850b7c442ffJeff Brown
1322352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownstatic void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
1332352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        SpriteIcon* outSpriteIcon) {
1342352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    PointerIcon pointerIcon;
1352352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    status_t status = android_view_PointerIcon_loadSystemIcon(env,
1362352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            contextObj, style, &pointerIcon);
1372352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (!status) {
1382352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
1392352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
1402352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
1412352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
1422352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
1432352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
144905805ad7ce18a386076fff99264f821bbad9f83Jeff Brownenum {
145905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown    WM_ACTION_PASS_TO_USER = 1,
1469630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown    WM_ACTION_WAKE_UP = 2,
147905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown    WM_ACTION_GO_TO_SLEEP = 4,
148905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown};
149905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown
150928e054931d357326613c78e62f4d850b7c442ffJeff Brown
151928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- NativeInputManager ---
15283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
1539c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brownclass NativeInputManager : public virtual RefBase,
1549c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    public virtual InputReaderPolicyInterface,
1552352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    public virtual InputDispatcherPolicyInterface,
1562352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    public virtual PointerControllerPolicyInterface {
1579c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brownprotected:
1589c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    virtual ~NativeInputManager();
1599c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
16046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brownpublic:
1614532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
1629c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
1639c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    inline sp<InputManager> getInputManager() const { return mInputManager; }
16446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
165b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    void dump(String8& dump);
166e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown
167d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    void setDisplayViewport(bool external, const DisplayViewport& viewport);
16846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1697fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
170928e054931d357326613c78e62f4d850b7c442ffJeff Brown            const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
1717fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
1727fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
1739302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
1749302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
175349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    void setInputDispatchMode(bool enabled, bool frozen);
17605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    void setSystemUiVisibility(int32_t visibility);
1771a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown    void setPointerSpeed(int32_t speed);
178daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown    void setShowTouches(bool enabled);
179349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
1809c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    /* --- InputReaderPolicyInterface implementation --- */
1819c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
182214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
18383c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
184af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
1856ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor);
1865bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
18746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1889c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    /* --- InputDispatcherPolicyInterface implementation --- */
18946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
190bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brown    virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
191e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown            uint32_t policyFlags);
1929c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    virtual void notifyConfigurationChanged(nsecs_t when);
193519e024d1e682ca458cc2dab743589a12992c0e1Jeff Brown    virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
194928e054931d357326613c78e62f4d850b7c442ffJeff Brown            const sp<InputWindowHandle>& inputWindowHandle);
195928e054931d357326613c78e62f4d850b7c442ffJeff Brown    virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
1960029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
197214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
198214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    virtual bool isKeyRepeatEnabled();
1991f2451007c660091b7b090c1ea332f9044515d2dJeff Brown    virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
20056194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
201905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown    virtual nsecs_t interceptKeyBeforeDispatching(
202905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown            const sp<InputWindowHandle>& inputWindowHandle,
203b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown            const KeyEvent* keyEvent, uint32_t policyFlags);
204928e054931d357326613c78e62f4d850b7c442ffJeff Brown    virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
20549ed71db425c5054e3ad9526496a7e116c89556bJeff Brown            const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
20601ce2e9eee41cc0c24b0d16465710a28ea337d5dJeff Brown    virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
207b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    virtual bool checkInjectEventsPermissionNonReentrant(
208b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown            int32_t injectorPid, int32_t injectorUid);
20946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2102352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    /* --- PointerControllerPolicyInterface implementation --- */
2112352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2122352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    virtual void loadPointerResources(PointerResources* outResources);
2132352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
21446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brownprivate:
2159c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    sp<InputManager> mInputManager;
21646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2172352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    jobject mContextObj;
2184532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jobject mServiceObj;
21905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    sp<Looper> mLooper;
22046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
22183c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    Mutex mLock;
22283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    struct Locked {
22383c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        // Display size information.
224d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        DisplayViewport internalViewport;
225d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        DisplayViewport externalViewport;
22683c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
22705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        // System UI visibility.
22805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        int32_t systemUiVisibility;
22905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
2301a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        // Pointer speed.
2311a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        int32_t pointerSpeed;
2321a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
233474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        // True if pointer gestures are enabled.
234474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        bool pointerGesturesEnabled;
235474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown
236daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        // Show touches feature enable/disable.
237daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        bool showTouches;
238daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
2395541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        // Sprite controller singleton, created on first use.
2405541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        sp<SpriteController> spriteController;
2415541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown
24283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        // Pointer controller singleton, created and destroyed as needed.
24383c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        wp<PointerController> pointerController;
24483c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    } mLocked;
24546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2462352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
24756194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
2485541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown    void ensureSpriteControllerLocked();
24905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
25000fa7bdd69f0868fd17ea7c881c771d785b2fbbdJeff Brown    // Power manager interactions.
2519c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    bool isScreenOn();
2529c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    bool isScreenBright();
2539c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
254b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
255a41ca77fabe1c7ad12ebb9b69b9e786c07d49fa0Jeff Brown
2569c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    static inline JNIEnv* jniEnv() {
25746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        return AndroidRuntime::getJNIEnv();
25846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
25946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown};
26046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
261928e054931d357326613c78e62f4d850b7c442ffJeff Brown
26246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2632352b978a3c94cd88f41d0d908f961333fdac1e9Jeff BrownNativeInputManager::NativeInputManager(jobject contextObj,
2644532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jobject serviceObj, const sp<Looper>& looper) :
265214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        mLooper(looper) {
2669c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    JNIEnv* env = jniEnv();
26746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2682352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mContextObj = env->NewGlobalRef(contextObj);
2694532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    mServiceObj = env->NewGlobalRef(serviceObj);
27046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
27183c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    {
27283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        AutoMutex _l(mLock);
27305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
2741a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        mLocked.pointerSpeed = 0;
275474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        mLocked.pointerGesturesEnabled = true;
276daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        mLocked.showTouches = false;
27783c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    }
27883c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
2799c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    sp<EventHub> eventHub = new EventHub();
2809c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    mInputManager = new InputManager(eventHub, this, this);
2819c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
28246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2839c3cda04d969912bc46184f2b326d1db95e0aba5Jeff BrownNativeInputManager::~NativeInputManager() {
2849c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    JNIEnv* env = jniEnv();
28546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2862352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    env->DeleteGlobalRef(mContextObj);
2874532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    env->DeleteGlobalRef(mServiceObj);
2889c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
28946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
290b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brownvoid NativeInputManager::dump(String8& dump) {
291b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    mInputManager->getReader()->dump(dump);
292b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    dump.append("\n");
2936d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown
294b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    mInputManager->getDispatcher()->dump(dump);
295b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    dump.append("\n");
2969c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
29746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
2987fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brownbool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
2999c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    if (env->ExceptionCheck()) {
3003762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("An exception was thrown by callback '%s'.", methodName);
3019c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown        LOGE_EX(env);
3029c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown        env->ExceptionClear();
3039c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown        return true;
3049c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    }
3059c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    return false;
3069c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
3079c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
308d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brownvoid NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
30965fd251c3913fc921468a3dad190810db19eb9dfJeff Brown    bool changed = false;
310d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    {
31183c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        AutoMutex _l(mLock);
31283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
313d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
314d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        if (v != viewport) {
31565fd251c3913fc921468a3dad190810db19eb9dfJeff Brown            changed = true;
316d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown            v = viewport;
317d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown
318d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown            if (!external) {
319d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                sp<PointerController> controller = mLocked.pointerController.promote();
320d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                if (controller != NULL) {
321d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                    controller->setDisplayViewport(
322d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                            viewport.logicalRight - viewport.logicalLeft,
323d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                            viewport.logicalBottom - viewport.logicalTop,
324d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                            viewport.orientation);
325d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                }
32683c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown            }
32783c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        }
3289c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    }
32965fd251c3913fc921468a3dad190810db19eb9dfJeff Brown
33065fd251c3913fc921468a3dad190810db19eb9dfJeff Brown    if (changed) {
33165fd251c3913fc921468a3dad190810db19eb9dfJeff Brown        mInputManager->getReader()->requestRefreshConfiguration(
33265fd251c3913fc921468a3dad190810db19eb9dfJeff Brown                InputReaderConfiguration::CHANGE_DISPLAY_INFO);
33365fd251c3913fc921468a3dad190810db19eb9dfJeff Brown    }
3349c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
3359c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
3367fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brownstatus_t NativeInputManager::registerInputChannel(JNIEnv* env,
337928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputChannel>& inputChannel,
338928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
339928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return mInputManager->getDispatcher()->registerInputChannel(
340928e054931d357326613c78e62f4d850b7c442ffJeff Brown            inputChannel, inputWindowHandle, monitor);
3417fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown}
3427fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
3437fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brownstatus_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
3447fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown        const sp<InputChannel>& inputChannel) {
345b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
3467fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown}
3477fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
348214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brownvoid NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
349214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    JNIEnv* env = jniEnv();
3509c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
3514532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
3524532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getVirtualKeyQuietTimeMillis);
353214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
354214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
355fe50892af3b365806a767298dfd8e86447682581Jeff Brown    }
3569c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
357214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    outConfig->excludedDeviceNames.clear();
3584532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
3594532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getExcludedDeviceNames));
360214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
361214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        jsize length = env->GetArrayLength(excludedDeviceNames);
3629c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown        for (jsize i = 0; i < length; i++) {
363214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown            jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
3649c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown            const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
365214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown            outConfig->excludedDeviceNames.add(String8(deviceNameChars));
3669c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown            env->ReleaseStringUTFChars(item, deviceNameChars);
3679c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown            env->DeleteLocalRef(item);
3689c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown        }
369214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        env->DeleteLocalRef(excludedDeviceNames);
370214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    }
371214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown
3724532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
3734532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getHoverTapTimeout);
374bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
3754532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
3764532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                gServiceClassInfo.getDoubleTapTimeout);
377214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
3784532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            jint longPressTimeout = env->CallIntMethod(mServiceObj,
3794532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                    gServiceClassInfo.getLongPressTimeout);
380214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown            if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
381bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown                outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
382214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown
383214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                // We must ensure that the tap-drag interval is significantly shorter than
384214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                // the long-press timeout because the tap is held down for the entire duration
385214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                // of the double-tap timeout.
386214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                jint tapDragInterval = max(min(longPressTimeout - 100,
387bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown                        doubleTapTimeout), hoverTapTimeout);
388214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                outConfig->pointerGestureTapDragInterval =
389214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown                        milliseconds_to_nanoseconds(tapDragInterval);
390214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown            }
391214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        }
392214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    }
393214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown
3944532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jint hoverTapSlop = env->CallIntMethod(mServiceObj,
3954532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getHoverTapSlop);
396bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
397bb3fcba0caf697f1d238a2cbefdf1efe06eded99Jeff Brown        outConfig->pointerGestureTapSlop = hoverTapSlop;
3989c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    }
3991a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
4001a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown    { // acquire lock
4011a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        AutoMutex _l(mLock);
4021a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
4031a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
4041a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown                * POINTER_SPEED_EXPONENT);
405474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
40665fd251c3913fc921468a3dad190810db19eb9dfJeff Brown
407daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        outConfig->showTouches = mLocked.showTouches;
408daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
409d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
410d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
4111a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown    } // release lock
4129c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
4139c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
41483c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brownsp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
41583c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    AutoMutex _l(mLock);
41683c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
41783c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    sp<PointerController> controller = mLocked.pointerController.promote();
41883c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    if (controller == NULL) {
4195541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        ensureSpriteControllerLocked();
42083c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
4212352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        controller = new PointerController(this, mLooper, mLocked.spriteController);
42283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown        mLocked.pointerController = controller;
42383c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
424d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        DisplayViewport& v = mLocked.internalViewport;
425d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        controller->setDisplayViewport(
426d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                v.logicalRight - v.logicalLeft,
427d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                v.logicalBottom - v.logicalTop,
428d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown                v.orientation);
429b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
4305541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        JNIEnv* env = jniEnv();
4314532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
4324532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                gServiceClassInfo.getPointerIcon);
4332352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
4342352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            PointerIcon pointerIcon;
4352352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            status_t status = android_view_PointerIcon_load(env, pointerIconObj,
4362352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                    mContextObj, &pointerIcon);
4372352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            if (!status && !pointerIcon.isNullIcon()) {
4382352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
4392352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                        pointerIcon.hotSpotX, pointerIcon.hotSpotY));
4402352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            } else {
4412352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                controller->setPointerIcon(SpriteIcon());
442b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            }
4432352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            env->DeleteLocalRef(pointerIconObj);
444b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
44505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
4462352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        updateInactivityTimeoutLocked(controller);
44783c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    }
44883c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown    return controller;
44983c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown}
45083c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
4515541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brownvoid NativeInputManager::ensureSpriteControllerLocked() {
4525541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown    if (mLocked.spriteController == NULL) {
4535541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        JNIEnv* env = jniEnv();
4544532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
4555541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
4565541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown            layer = -1;
4575541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        }
4585541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown        mLocked.spriteController = new SpriteController(mLooper, layer);
4595541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown    }
4605541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown}
4615541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown
462af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brownvoid NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
463af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    JNIEnv* env = jniEnv();
464af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
465af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    size_t count = inputDevices.size();
466af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    jobjectArray inputDevicesObjArray = env->NewObjectArray(
467af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            count, gInputDeviceClassInfo.clazz, NULL);
468af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    if (inputDevicesObjArray) {
469af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        bool error = false;
470af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        for (size_t i = 0; i < count; i++) {
471af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
472af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            if (!inputDeviceObj) {
473af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown                error = true;
474af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown                break;
475af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            }
476af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
477af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
478af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            env->DeleteLocalRef(inputDeviceObj);
479af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        }
480af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
481af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        if (!error) {
482af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown            env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
483af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown                    inputDevicesObjArray);
484af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        }
485af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
486af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown        env->DeleteLocalRef(inputDevicesObjArray);
487af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    }
488af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
489af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown    checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
490af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown}
491af9e8d38184c6ba4d2d3eb5bde7014a66dd8a78bJeff Brown
4926ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brownsp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
4936ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        const String8& inputDeviceDescriptor) {
4946ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    JNIEnv* env = jniEnv();
4956ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown
4966ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    sp<KeyCharacterMap> result;
4976ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
4986ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
4996ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown                gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get())));
5006ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    if (arrayObj.get()) {
5016ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        ScopedLocalRef<jstring> filenameObj(env,
5026ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown                jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
5036ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        ScopedLocalRef<jstring> contentsObj(env,
5046ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown                jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
5056ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        ScopedUtfChars filenameChars(env, filenameObj.get());
5066ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        ScopedUtfChars contentsChars(env, contentsObj.get());
5076ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown
5086ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown        KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
5096ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown                String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
5106ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    }
5116ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
5126ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown    return result;
5136ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown}
5146ec6f79e1ac1714e3b837796e99f07ff88f66601Jeff Brown
5155bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff BrownString8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
5165bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    JNIEnv* env = jniEnv();
5175bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown
5185bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
5195bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
5205bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown            gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
5215bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    String8 result;
5225bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    if (aliasObj.get()) {
5235bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown        ScopedUtfChars aliasChars(env, aliasObj.get());
5245bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown        result.setTo(aliasChars.c_str());
5255bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    }
5265bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    checkAndClearExceptionFromCallback(env, "getDeviceAlias");
5275bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown    return result;
5285bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown}
5295bbd4b4f5fc19302fa017ad6afee6eb2d489d91aJeff Brown
530bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brownvoid NativeInputManager::notifySwitch(nsecs_t when,
531bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brown        uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
532e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
533bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brown    ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
534bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brown            when, switchValues, switchMask, policyFlags);
535e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown#endif
536e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
537e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown    JNIEnv* env = jniEnv();
538e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
5395338428ddbe662283bf88171c7ca361d51d78da8Jeff Brown    env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
540bcc046af4ef171aa3aa3c6b64efb5cafc1e46cd3Jeff Brown            when, switchValues, switchMask);
5415338428ddbe662283bf88171c7ca361d51d78da8Jeff Brown    checkAndClearExceptionFromCallback(env, "notifySwitch");
542e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown}
543e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
5449c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brownvoid NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
5459c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
5465baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("notifyConfigurationChanged - when=%lld", when);
5479c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown#endif
5489c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
5499c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    JNIEnv* env = jniEnv();
5509c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
5514532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
5527fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
5539c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
5549c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
555519e024d1e682ca458cc2dab743589a12992c0e1Jeff Brownnsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
556928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputWindowHandle>& inputWindowHandle) {
557b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
5585baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("notifyANR");
559b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown#endif
560b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown
561b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    JNIEnv* env = jniEnv();
562b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown
563928e054931d357326613c78e62f4d850b7c442ffJeff Brown    jobject inputApplicationHandleObj =
564928e054931d357326613c78e62f4d850b7c442ffJeff Brown            getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
565928e054931d357326613c78e62f4d850b7c442ffJeff Brown    jobject inputWindowHandleObj =
566928e054931d357326613c78e62f4d850b7c442ffJeff Brown            getInputWindowHandleObjLocalRef(env, inputWindowHandle);
567b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown
5684532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jlong newTimeout = env->CallLongMethod(mServiceObj,
5694532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
570519e024d1e682ca458cc2dab743589a12992c0e1Jeff Brown    if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
571b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown        newTimeout = 0; // abort dispatch
572519e024d1e682ca458cc2dab743589a12992c0e1Jeff Brown    } else {
573519e024d1e682ca458cc2dab743589a12992c0e1Jeff Brown        assert(newTimeout >= 0);
574b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    }
575b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown
576928e054931d357326613c78e62f4d850b7c442ffJeff Brown    env->DeleteLocalRef(inputWindowHandleObj);
577928e054931d357326613c78e62f4d850b7c442ffJeff Brown    env->DeleteLocalRef(inputApplicationHandleObj);
578b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    return newTimeout;
579b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown}
580b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown
581928e054931d357326613c78e62f4d850b7c442ffJeff Brownvoid NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
5829c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
5835baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("notifyInputChannelBroken");
5849c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown#endif
5859c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
5867fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    JNIEnv* env = jniEnv();
5877fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
588928e054931d357326613c78e62f4d850b7c442ffJeff Brown    jobject inputWindowHandleObj =
589928e054931d357326613c78e62f4d850b7c442ffJeff Brown            getInputWindowHandleObjLocalRef(env, inputWindowHandle);
590928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (inputWindowHandleObj) {
5914532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
592928e054931d357326613c78e62f4d850b7c442ffJeff Brown                inputWindowHandleObj);
5937fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown        checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
5947fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
595928e054931d357326613c78e62f4d850b7c442ffJeff Brown        env->DeleteLocalRef(inputWindowHandleObj);
5967fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    }
5979c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
5989c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
599214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brownvoid NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
600214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    JNIEnv* env = jniEnv();
601a454767b09ecb7d25d00beae0e5a1fdd48605c63Jeff Brown
6024532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
6034532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getKeyRepeatTimeout);
604214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
605214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
6069c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown    }
6079c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
6084532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
6094532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.getKeyRepeatDelay);
610214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
611214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown        outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
612214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    }
613b21fb104cc95fe7e5daf3b3626241e525c39a3f1Jeff Brown}
614b21fb104cc95fe7e5daf3b3626241e525c39a3f1Jeff Brown
615214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brownbool NativeInputManager::isKeyRepeatEnabled() {
616214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    // Only enable automatic key repeating when the screen is on.
617214eaf48878bba00cbd5831871bcbd82632b6e34Jeff Brown    return isScreenOn();
618ae9fc03bdccda709101291bbcd3beaa5b6daebfcJeff Brown}
619ae9fc03bdccda709101291bbcd3beaa5b6daebfcJeff Brown
6209302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brownvoid NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
6219302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    Vector<sp<InputWindowHandle> > windowHandles;
6229c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
6239302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    if (windowHandleObjArray) {
6249302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        jsize length = env->GetArrayLength(windowHandleObjArray);
6259302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        for (jsize i = 0; i < length; i++) {
6269302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
6279302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            if (! windowHandleObj) {
6289302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown                break; // found null element indicating end of used portion of the array
6299302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            }
630349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
6319302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            sp<InputWindowHandle> windowHandle =
6329302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown                    android_server_InputWindowHandle_getHandle(env, windowHandleObj);
6339302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            if (windowHandle != NULL) {
6349302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown                windowHandles.push(windowHandle);
635474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown            }
6369302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            env->DeleteLocalRef(windowHandleObj);
637e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown        }
638b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    }
639349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
6409302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    mInputManager->getDispatcher()->setInputWindows(windowHandles);
6419302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
6429302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    // Do this after the dispatcher has updated the window handle state.
6439302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    bool newPointerGesturesEnabled = true;
6449302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    size_t numWindows = windowHandles.size();
6459302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    for (size_t i = 0; i < numWindows; i++) {
6469302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
647cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        const InputWindowInfo* windowInfo = windowHandle->getInfo();
648cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
649cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown                & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
6509302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            newPointerGesturesEnabled = false;
6519302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        }
6529302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    }
653474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown
654474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    uint32_t changes = 0;
655474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    { // acquire lock
656474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        AutoMutex _l(mLock);
657474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown
658474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
659474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown            mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
660474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown            changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
661474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        }
662474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    } // release lock
663474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown
664474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    if (changes) {
665474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        mInputManager->getReader()->requestRefreshConfiguration(changes);
666474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    }
667349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
668349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
6699302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brownvoid NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
6709302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    sp<InputApplicationHandle> applicationHandle =
6719302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
6729302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
6739c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown}
6749c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
675b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brownvoid NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
676b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
677349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
678349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
67905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brownvoid NativeInputManager::setSystemUiVisibility(int32_t visibility) {
68005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    AutoMutex _l(mLock);
68105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
68205dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    if (mLocked.systemUiVisibility != visibility) {
68305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        mLocked.systemUiVisibility = visibility;
68405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
68505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        sp<PointerController> controller = mLocked.pointerController.promote();
68605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        if (controller != NULL) {
6872352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            updateInactivityTimeoutLocked(controller);
68805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        }
68905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
69005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
69105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
6922352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
69305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
6942352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    controller->setInactivityTimeout(lightsOut
6952352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            ? PointerController::INACTIVITY_TIMEOUT_SHORT
6962352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            : PointerController::INACTIVITY_TIMEOUT_NORMAL);
69705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
69805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
6991a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brownvoid NativeInputManager::setPointerSpeed(int32_t speed) {
700474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    { // acquire lock
701474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        AutoMutex _l(mLock);
702474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown
703474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        if (mLocked.pointerSpeed == speed) {
704474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown            return;
705474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown        }
7061a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
7076215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        ALOGI("Setting pointer speed to %d.", speed);
7081a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown        mLocked.pointerSpeed = speed;
709474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    } // release lock
7101a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
711474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown    mInputManager->getReader()->requestRefreshConfiguration(
712474dcb5c3ddff737c4ac9fc44a1f7be569605e5fJeff Brown            InputReaderConfiguration::CHANGE_POINTER_SPEED);
7131a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown}
7141a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
715daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brownvoid NativeInputManager::setShowTouches(bool enabled) {
716daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown    { // acquire lock
717daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        AutoMutex _l(mLock);
718daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
719daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        if (mLocked.showTouches == enabled) {
720daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown            return;
721daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        }
722daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
7236215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
724daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown        mLocked.showTouches = enabled;
725daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown    } // release lock
726daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
727daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown    mInputManager->getReader()->requestRefreshConfiguration(
728daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown            InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
729daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown}
730daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
731e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brownbool NativeInputManager::isScreenOn() {
732e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown    return android_server_PowerManagerService_isScreenOn();
733e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown}
734e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
735e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brownbool NativeInputManager::isScreenBright() {
736e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown    return android_server_PowerManagerService_isScreenBright();
737e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown}
738e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
7390029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownbool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
7400029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    jobject inputEventObj;
7410029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
7420029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    JNIEnv* env = jniEnv();
7430029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    switch (inputEvent->getType()) {
7440029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    case AINPUT_EVENT_TYPE_KEY:
7450029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        inputEventObj = android_view_KeyEvent_fromNative(env,
7460029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                static_cast<const KeyEvent*>(inputEvent));
7470029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        break;
7480029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    case AINPUT_EVENT_TYPE_MOTION:
7490029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
7500029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                static_cast<const MotionEvent*>(inputEvent));
7510029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        break;
7520029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    default:
7530029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        return true; // dispatch the event normally
7540029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
7550029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
7560029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    if (!inputEventObj) {
7573762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Failed to obtain input event object for filterInputEvent.");
7580029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        return true; // dispatch the event normally
7590029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
7600029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
7610029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    // The callee is responsible for recycling the event.
7624532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
7630029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown            inputEventObj, policyFlags);
7640029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
7650029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        pass = true;
7660029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
7670029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    env->DeleteLocalRef(inputEventObj);
7680029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    return pass;
7690029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown}
7700029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
7711f2451007c660091b7b090c1ea332f9044515d2dJeff Brownvoid NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
7721f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        uint32_t& policyFlags) {
7733122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // Policy:
7743122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Ignore untrusted events and pass them along.
7753122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Ask the window manager what to do with normal events and trusted injected events.
7763122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - For normal events wake and brighten the screen if currently off or dim.
7773122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    if ((policyFlags & POLICY_FLAG_TRUSTED)) {
7781f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        nsecs_t when = keyEvent->getEventTime();
7793122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        bool isScreenOn = this->isScreenOn();
7803122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        bool isScreenBright = this->isScreenBright();
781e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
7823122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        JNIEnv* env = jniEnv();
7831f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
7841f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        jint wmActions;
7851f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        if (keyEventObj) {
7864532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            wmActions = env->CallIntMethod(mServiceObj,
7874532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                    gServiceClassInfo.interceptKeyBeforeQueueing,
7881f2451007c660091b7b090c1ea332f9044515d2dJeff Brown                    keyEventObj, policyFlags, isScreenOn);
7891f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
7901f2451007c660091b7b090c1ea332f9044515d2dJeff Brown                wmActions = 0;
7911f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            }
7921f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            android_view_KeyEvent_recycle(env, keyEventObj);
7931f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            env->DeleteLocalRef(keyEventObj);
7941f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        } else {
7953762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
7963122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            wmActions = 0;
797e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown        }
798e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
7991f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        if (!(policyFlags & POLICY_FLAG_INJECTED)) {
8003122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            if (!isScreenOn) {
8013122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown                policyFlags |= POLICY_FLAG_WOKE_HERE;
8023122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            }
8033122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown
8043122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            if (!isScreenBright) {
8053122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown                policyFlags |= POLICY_FLAG_BRIGHT_HERE;
8063122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            }
807e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown        }
808e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
80956194ebec6212e229f4ccdaa4b187166d20013efJeff Brown        handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
8103122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    } else {
811e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown        policyFlags |= POLICY_FLAG_PASS_TO_USER;
812e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown    }
813e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown}
814e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
81556194ebec6212e229f4ccdaa4b187166d20013efJeff Brownvoid NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
8163122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // Policy:
8173122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Ignore untrusted events and pass them along.
8183122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - No special filtering for injected events required at this time.
8193122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Filter normal events based on screen state.
8203122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - For normal events brighten (but do not wake) the screen if currently dim.
8213122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
8223122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        if (isScreenOn()) {
8233122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            policyFlags |= POLICY_FLAG_PASS_TO_USER;
8243122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown
8253122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            if (!isScreenBright()) {
8263122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown                policyFlags |= POLICY_FLAG_BRIGHT_HERE;
8273122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown            }
82856194ebec6212e229f4ccdaa4b187166d20013efJeff Brown        } else {
82956194ebec6212e229f4ccdaa4b187166d20013efJeff Brown            JNIEnv* env = jniEnv();
8304532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            jint wmActions = env->CallIntMethod(mServiceObj,
8314532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                        gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
83256194ebec6212e229f4ccdaa4b187166d20013efJeff Brown                        policyFlags);
83356194ebec6212e229f4ccdaa4b187166d20013efJeff Brown            if (checkAndClearExceptionFromCallback(env,
83456194ebec6212e229f4ccdaa4b187166d20013efJeff Brown                    "interceptMotionBeforeQueueingWhenScreenOff")) {
83556194ebec6212e229f4ccdaa4b187166d20013efJeff Brown                wmActions = 0;
83656194ebec6212e229f4ccdaa4b187166d20013efJeff Brown            }
83756194ebec6212e229f4ccdaa4b187166d20013efJeff Brown
83856194ebec6212e229f4ccdaa4b187166d20013efJeff Brown            policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
83956194ebec6212e229f4ccdaa4b187166d20013efJeff Brown            handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
840e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown        }
8413122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    } else {
8423122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        policyFlags |= POLICY_FLAG_PASS_TO_USER;
843e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown    }
844e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown}
845e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown
84656194ebec6212e229f4ccdaa4b187166d20013efJeff Brownvoid NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
84756194ebec6212e229f4ccdaa4b187166d20013efJeff Brown        uint32_t& policyFlags) {
84856194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    if (wmActions & WM_ACTION_GO_TO_SLEEP) {
8499267beb8689e6febe3dd23f2a3025736c50eef01Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
8505baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("handleInterceptActions: Going to sleep.");
85156194ebec6212e229f4ccdaa4b187166d20013efJeff Brown#endif
85256194ebec6212e229f4ccdaa4b187166d20013efJeff Brown        android_server_PowerManagerService_goToSleep(when);
85356194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    }
85456194ebec6212e229f4ccdaa4b187166d20013efJeff Brown
8559630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown    if (wmActions & WM_ACTION_WAKE_UP) {
8569267beb8689e6febe3dd23f2a3025736c50eef01Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
8579630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown        ALOGD("handleInterceptActions: Waking up.");
85856194ebec6212e229f4ccdaa4b187166d20013efJeff Brown#endif
8599630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown        android_server_PowerManagerService_wakeUp(when);
86056194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    }
86156194ebec6212e229f4ccdaa4b187166d20013efJeff Brown
86256194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    if (wmActions & WM_ACTION_PASS_TO_USER) {
86356194ebec6212e229f4ccdaa4b187166d20013efJeff Brown        policyFlags |= POLICY_FLAG_PASS_TO_USER;
86456194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    } else {
8659267beb8689e6febe3dd23f2a3025736c50eef01Jeff Brown#if DEBUG_INPUT_DISPATCHER_POLICY
8665baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("handleInterceptActions: Not passing key to user.");
86756194ebec6212e229f4ccdaa4b187166d20013efJeff Brown#endif
86856194ebec6212e229f4ccdaa4b187166d20013efJeff Brown    }
86956194ebec6212e229f4ccdaa4b187166d20013efJeff Brown}
87056194ebec6212e229f4ccdaa4b187166d20013efJeff Brown
871905805ad7ce18a386076fff99264f821bbad9f83Jeff Brownnsecs_t NativeInputManager::interceptKeyBeforeDispatching(
872928e054931d357326613c78e62f4d850b7c442ffJeff Brown        const sp<InputWindowHandle>& inputWindowHandle,
873e20c9e0264190f94324197a8271cf03811a4ca58Jeff Brown        const KeyEvent* keyEvent, uint32_t policyFlags) {
8743122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // Policy:
8753122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Ignore untrusted events and pass them along.
8763122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    // - Filter normal events and trusted injected events through the window manager policy to
8773122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    //   handle the HOME key and the like.
878905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown    nsecs_t result = 0;
8793122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    if (policyFlags & POLICY_FLAG_TRUSTED) {
8803122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown        JNIEnv* env = jniEnv();
8813122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown
882928e054931d357326613c78e62f4d850b7c442ffJeff Brown        // Note: inputWindowHandle may be null.
883928e054931d357326613c78e62f4d850b7c442ffJeff Brown        jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
8841f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
8851f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        if (keyEventObj) {
8864532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            jlong delayMillis = env->CallLongMethod(mServiceObj,
8874532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                    gServiceClassInfo.interceptKeyBeforeDispatching,
888928e054931d357326613c78e62f4d850b7c442ffJeff Brown                    inputWindowHandleObj, keyEventObj, policyFlags);
8891f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
8901f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            android_view_KeyEvent_recycle(env, keyEventObj);
8911f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            env->DeleteLocalRef(keyEventObj);
892905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown            if (!error) {
893905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown                if (delayMillis < 0) {
894905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown                    result = -1;
895905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown                } else if (delayMillis > 0) {
896905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown                    result = milliseconds_to_nanoseconds(delayMillis);
897905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown                }
898905805ad7ce18a386076fff99264f821bbad9f83Jeff Brown            }
8991f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        } else {
9003762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
9011f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        }
902928e054931d357326613c78e62f4d850b7c442ffJeff Brown        env->DeleteLocalRef(inputWindowHandleObj);
9033122e4488aa0749cbec9890ace22c366e35350c3Jeff Brown    }
9041f2451007c660091b7b090c1ea332f9044515d2dJeff Brown    return result;
905d0097871828bb7d5d6eec06cadd92c2e3358849bJeff Brown}
906d0097871828bb7d5d6eec06cadd92c2e3358849bJeff Brown
907928e054931d357326613c78e62f4d850b7c442ffJeff Brownbool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
90849ed71db425c5054e3ad9526496a7e116c89556bJeff Brown        const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
9093915bb845b032dc184dba5e60970b803390ca3edJeff Brown    // Policy:
9103915bb845b032dc184dba5e60970b803390ca3edJeff Brown    // - Ignore untrusted events and do not perform default handling.
91149ed71db425c5054e3ad9526496a7e116c89556bJeff Brown    bool result = false;
9123915bb845b032dc184dba5e60970b803390ca3edJeff Brown    if (policyFlags & POLICY_FLAG_TRUSTED) {
9133915bb845b032dc184dba5e60970b803390ca3edJeff Brown        JNIEnv* env = jniEnv();
9143915bb845b032dc184dba5e60970b803390ca3edJeff Brown
915928e054931d357326613c78e62f4d850b7c442ffJeff Brown        // Note: inputWindowHandle may be null.
916928e054931d357326613c78e62f4d850b7c442ffJeff Brown        jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
9171f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
9181f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        if (keyEventObj) {
9194532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
9204532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                    gServiceClassInfo.dispatchUnhandledKey,
921928e054931d357326613c78e62f4d850b7c442ffJeff Brown                    inputWindowHandleObj, keyEventObj, policyFlags);
922da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown            if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
923da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown                fallbackKeyEventObj = NULL;
924da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown            }
9251f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            android_view_KeyEvent_recycle(env, keyEventObj);
9261f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            env->DeleteLocalRef(keyEventObj);
92749ed71db425c5054e3ad9526496a7e116c89556bJeff Brown
92849ed71db425c5054e3ad9526496a7e116c89556bJeff Brown            if (fallbackKeyEventObj) {
92949ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                // Note: outFallbackKeyEvent may be the same object as keyEvent.
93049ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
93149ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                        outFallbackKeyEvent)) {
93249ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                    result = true;
93349ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                }
93449ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
93549ed71db425c5054e3ad9526496a7e116c89556bJeff Brown                env->DeleteLocalRef(fallbackKeyEventObj);
93649ed71db425c5054e3ad9526496a7e116c89556bJeff Brown            }
9371f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        } else {
9383762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
9391f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        }
940928e054931d357326613c78e62f4d850b7c442ffJeff Brown        env->DeleteLocalRef(inputWindowHandleObj);
9413915bb845b032dc184dba5e60970b803390ca3edJeff Brown    }
9421f2451007c660091b7b090c1ea332f9044515d2dJeff Brown    return result;
9433915bb845b032dc184dba5e60970b803390ca3edJeff Brown}
9443915bb845b032dc184dba5e60970b803390ca3edJeff Brown
94501ce2e9eee41cc0c24b0d16465710a28ea337d5dJeff Brownvoid NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
94601ce2e9eee41cc0c24b0d16465710a28ea337d5dJeff Brown    android_server_PowerManagerService_userActivity(eventTime, eventType);
9476d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown}
9486d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown
949349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
950b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brownbool NativeInputManager::checkInjectEventsPermissionNonReentrant(
951b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown        int32_t injectorPid, int32_t injectorUid) {
952b88102f5b7e51552a3576cf197b4c8cf96f193d1Jeff Brown    JNIEnv* env = jniEnv();
9534532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    jboolean result = env->CallBooleanMethod(mServiceObj,
9544532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown            gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
955da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown    if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
956da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown        result = false;
957da3d5a91b6b311ed77f2707d4456c1f18b84d73bJeff Brown    }
958349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown    return result;
959349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
960349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
9612352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid NativeInputManager::loadPointerResources(PointerResources* outResources) {
9622352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    JNIEnv* env = jniEnv();
9632352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
9642352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
9652352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            &outResources->spotHover);
9662352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
9672352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            &outResources->spotTouch);
9682352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
9692352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            &outResources->spotAnchor);
9702352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
9712352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
97283c09685f2e62bc3cf7e71bc61d903f4b9ccaeb4Jeff Brown
9739c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown// ----------------------------------------------------------------------------
9749c3cda04d969912bc46184f2b326d1db95e0aba5Jeff Brown
9754532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jint nativeInit(JNIEnv* env, jclass clazz,
9764532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
977603b44589682db3ff33ade172facb0c5e309f1beJeff Brown    sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
978603b44589682db3ff33ade172facb0c5e309f1beJeff Brown    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
979603b44589682db3ff33ade172facb0c5e309f1beJeff Brown            messageQueue->getLooper());
9804532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->incStrong(serviceObj);
9814532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    return reinterpret_cast<jint>(im);
98246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
98346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
9844532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeStart(JNIEnv* env, jclass clazz, jint ptr) {
9854532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
98646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
9874532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    status_t result = im->getInputManager()->start();
98846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    if (result) {
98946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        jniThrowRuntimeException(env, "Input manager could not be started.");
99046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
99146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
99246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
993d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brownstatic void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jint ptr, jboolean external,
994d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        jint displayId, jint orientation,
995d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
99683d616a9c7b9505153d258511eb5c16b552e268dJeff Brown        jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
99783d616a9c7b9505153d258511eb5c16b552e268dJeff Brown        jint deviceWidth, jint deviceHeight) {
9984532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
99946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1000d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    DisplayViewport v;
1001d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.displayId = displayId;
1002d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.orientation = orientation;
1003d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.logicalLeft = logicalLeft;
1004d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.logicalTop = logicalTop;
1005d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.logicalRight = logicalRight;
1006d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.logicalBottom = logicalBottom;
1007d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.physicalLeft = physicalLeft;
1008d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.physicalTop = physicalTop;
1009d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.physicalRight = physicalRight;
1010d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    v.physicalBottom = physicalBottom;
101183d616a9c7b9505153d258511eb5c16b552e268dJeff Brown    v.deviceWidth = deviceWidth;
101283d616a9c7b9505153d258511eb5c16b552e268dJeff Brown    v.deviceHeight = deviceHeight;
1013d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    im->setDisplayViewport(external, v);
101446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
101546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10164532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
10174532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jint deviceId, jint sourceMask, jint scanCode) {
10184532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
101946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10204532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    return im->getInputManager()->getReader()->getScanCodeState(
10216d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown            deviceId, uint32_t(sourceMask), scanCode);
102246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
102346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10244532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
10254532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jint deviceId, jint sourceMask, jint keyCode) {
10264532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
102746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10284532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    return im->getInputManager()->getReader()->getKeyCodeState(
10296d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown            deviceId, uint32_t(sourceMask), keyCode);
103046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
103146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10324532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
10334532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jint deviceId, jint sourceMask, jint sw) {
10344532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
103546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10364532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    return im->getInputManager()->getReader()->getSwitchState(
10376d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown            deviceId, uint32_t(sourceMask), sw);
103846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
103946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10404532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
10414532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
10424532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
104346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
104446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
104546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
104646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    jsize numCodes = env->GetArrayLength(keyCodes);
104746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    jboolean result;
10486d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown    if (numCodes == env->GetArrayLength(keyCodes)) {
10494532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        result = im->getInputManager()->getReader()->hasKeys(
10506d0fec2de3601821f4f44eeb7d7deedebb2b7117Jeff Brown                deviceId, uint32_t(sourceMask), numCodes, codes, flags);
105146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    } else {
105246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        result = JNI_FALSE;
105346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
105446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
105546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    env->ReleaseBooleanArrayElements(outFlags, flags, 0);
105646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    env->ReleaseIntArrayElements(keyCodes, codes, 0);
105746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    return result;
105846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
105946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
106046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brownstatic void throwInputChannelNotInitialized(JNIEnv* env) {
106146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    jniThrowException(env, "java/lang/IllegalStateException",
106246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown             "inputChannel is not initialized");
106346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
106446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10654532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void handleInputChannelDisposed(JNIEnv* env,
106646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
10674532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = static_cast<NativeInputManager*>(data);
10684532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown
10698564c8da817a845353d213acd8636b76f567b234Steve Block    ALOGW("Input channel object '%s' was disposed without first being unregistered with "
107046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown            "the input manager!", inputChannel->getName().string());
10714532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->unregisterInputChannel(env, inputChannel);
107246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
107346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
10744532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
10754532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
10764532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
107746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
107846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
107946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown            inputChannelObj);
108046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    if (inputChannel == NULL) {
108146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        throwInputChannelNotInitialized(env);
108246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        return;
108346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
108446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1085928e054931d357326613c78e62f4d850b7c442ffJeff Brown    sp<InputWindowHandle> inputWindowHandle =
1086928e054931d357326613c78e62f4d850b7c442ffJeff Brown            android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
10877fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
10884532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    status_t status = im->registerInputChannel(
1089928e054931d357326613c78e62f4d850b7c442ffJeff Brown            env, inputChannel, inputWindowHandle, monitor);
109046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    if (status) {
1091cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        String8 message;
1092cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        message.appendFormat("Failed to register input channel.  status=%d", status);
1093cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        jniThrowRuntimeException(env, message.string());
109446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        return;
109546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
109646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
1097a41ca77fabe1c7ad12ebb9b69b9e786c07d49fa0Jeff Brown    if (! monitor) {
1098a41ca77fabe1c7ad12ebb9b69b9e786c07d49fa0Jeff Brown        android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
10994532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown                handleInputChannelDisposed, im);
1100a41ca77fabe1c7ad12ebb9b69b9e786c07d49fa0Jeff Brown    }
110146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
110246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
11034532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
11044532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jobject inputChannelObj) {
11054532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
110646b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
110746b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
110846b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown            inputChannelObj);
110946b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    if (inputChannel == NULL) {
111046b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        throwInputChannelNotInitialized(env);
111146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown        return;
111246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
111346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
111446b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
111546b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
11164532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    status_t status = im->unregisterInputChannel(env, inputChannel);
1117cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown    if (status && status != BAD_VALUE) { // ignore already unregistered channel
1118cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        String8 message;
1119cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        message.appendFormat("Failed to unregister input channel.  status=%d", status);
1120cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        jniThrowRuntimeException(env, message.string());
112146b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown    }
112246b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown}
112346b9ac0ae2162309774a7478cd9d4e578747bfc2Jeff Brown
11244532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
11254532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jboolean enabled) {
11264532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
11270029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
11284532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
11290029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown}
11300029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
11314532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
11324532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
11330029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        jint syncMode, jint timeoutMillis, jint policyFlags) {
11344532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
11357fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
11366ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown    if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
11376ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown        KeyEvent keyEvent;
11381f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
11391f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        if (status) {
11401f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
11411f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            return INPUT_EVENT_INJECTION_FAILED;
11421f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        }
11437fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
11444532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        return im->getInputManager()->getDispatcher()->injectInputEvent(
11450029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
11460029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                uint32_t(policyFlags));
11476ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown    } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
11482ed2462aa29c564f5231f317c27b3188da875e52Jeff Brown        const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
11492ed2462aa29c564f5231f317c27b3188da875e52Jeff Brown        if (!motionEvent) {
11501f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
11511f2451007c660091b7b090c1ea332f9044515d2dJeff Brown            return INPUT_EVENT_INJECTION_FAILED;
11521f2451007c660091b7b090c1ea332f9044515d2dJeff Brown        }
11537fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
11544532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        return im->getInputManager()->getDispatcher()->injectInputEvent(
11550029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
11560029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown                uint32_t(policyFlags));
11576ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown    } else {
11586ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown        jniThrowRuntimeException(env, "Invalid input event type.");
11597fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown        return INPUT_EVENT_INJECTION_FAILED;
11607fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown    }
11617fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown}
11627fbdc84e87dd3a0e196b9803bb04495d11e9cb8aJeff Brown
11634532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetInputWindows(JNIEnv* env, jclass clazz,
11644532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jobjectArray windowHandleObjArray) {
11654532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1166349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11674532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setInputWindows(env, windowHandleObjArray);
1168349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
1169349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11704532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
11714532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jint ptr, jobject applicationHandleObj) {
11724532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1173349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11744532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setFocusedApplication(env, applicationHandleObj);
1175349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
1176349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11774532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetInputDispatchMode(JNIEnv* env,
11784532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jclass clazz, jint ptr, jboolean enabled, jboolean frozen) {
11794532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1180349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11814532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setInputDispatchMode(enabled, frozen);
1182349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown}
1183349703effce5acc53ed96f7ed8556131f0c65e18Jeff Brown
11844532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetSystemUiVisibility(JNIEnv* env,
11854532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jclass clazz, jint ptr, jint visibility) {
11864532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
118705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
11884532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setSystemUiVisibility(visibility);
118905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
119005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
11914532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic jboolean nativeTransferTouchFocus(JNIEnv* env,
11924532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jclass clazz, jint ptr, jobject fromChannelObj, jobject toChannelObj) {
11934532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1194e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown
1195e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown    sp<InputChannel> fromChannel =
1196e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown            android_view_InputChannel_getInputChannel(env, fromChannelObj);
1197e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown    sp<InputChannel> toChannel =
1198e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown            android_view_InputChannel_getInputChannel(env, toChannelObj);
1199e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown
1200e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown    if (fromChannel == NULL || toChannel == NULL) {
1201e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown        return false;
1202e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown    }
1203e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown
12044532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    return im->getInputManager()->getDispatcher()->
1205e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown            transferTouchFocus(fromChannel, toChannel);
1206e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown}
1207e65041225ec0bbc3d67a3b70cdc6d598a5760043Jeff Brown
12084532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetPointerSpeed(JNIEnv* env,
12094532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jclass clazz, jint ptr, jint speed) {
12104532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
12111a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
12124532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setPointerSpeed(speed);
12131a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown}
12141a84fd1fb7a51f3fe4f8865e1cdd09f3490f696cJeff Brown
12154532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brownstatic void nativeSetShowTouches(JNIEnv* env,
12164532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown        jclass clazz, jint ptr, jboolean enabled) {
12174532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1218daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
12194532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    im->setShowTouches(enabled);
1220daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown}
1221daf4a127ba2af82a3fb477044b872719a0ab1827Jeff Brown
1222a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brownstatic void nativeVibrate(JNIEnv* env,
1223a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown        jclass clazz, jint ptr, jint deviceId, jlongArray patternObj,
1224a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown        jint repeat, jint token) {
1225a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1226a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown
1227a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    size_t patternSize = env->GetArrayLength(patternObj);
1228a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1229a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown        ALOGI("Skipped requested vibration because the pattern size is %d "
1230a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown                "which is more than the maximum supported size of %d.",
1231a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown                patternSize, MAX_VIBRATE_PATTERN_SIZE);
1232a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown        return; // limit to reasonable size
1233a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    }
1234a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown
1235a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1236a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown            patternObj, NULL));
1237a47425a13c19f95057df78b8bb65bb25657e8753Jeff Brown    nsecs_t pattern[patternSize];
1238