1d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
2d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Copyright (C) 2010 The Android Open Source Project
3d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
4d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Licensed under the Apache License, Version 2.0 (the "License");
5d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * you may not use this file except in compliance with the License.
6d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * You may obtain a copy of the License at
7d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
8d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *      http://www.apache.org/licenses/LICENSE-2.0
9d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
10d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Unless required by applicable law or agreed to in writing, software
11d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * distributed under the License is distributed on an "AS IS" BASIS,
12d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * See the License for the specific language governing permissions and
14d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * limitations under the License.
15d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
16d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
17d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include "../InputReader.h"
18d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
19d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/List.h>
20d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <gtest/gtest.h>
21d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <math.h>
22d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
23d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightnamespace android {
24d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
25d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// An arbitrary time value.
26d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic const nsecs_t ARBITRARY_TIME = 1234;
27d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
28d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Arbitrary display properties.
29d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic const int32_t DISPLAY_ID = 0;
30d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic const int32_t DISPLAY_WIDTH = 480;
31d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic const int32_t DISPLAY_HEIGHT = 800;
32d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
33d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Error tolerance for floating point assertions.
34d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic const float EPSILON = 0.001f;
35d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
36d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrighttemplate<typename T>
37d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic inline T min(T a, T b) {
38d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return a < b ? a : b;
39d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
40d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
41d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic inline float avg(float x, float y) {
42d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return (x + y) / 2;
43d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
44d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
45d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
46d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// --- FakePointerController ---
47d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
48d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass FakePointerController : public PointerControllerInterface {
49d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mHaveBounds;
50d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float mMinX, mMinY, mMaxX, mMaxY;
51d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float mX, mY;
52d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mButtonState;
53d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
54d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
55d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~FakePointerController() { }
56d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
57d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
58d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    FakePointerController() :
59d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
60d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mButtonState(0) {
61d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
62d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
63d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setBounds(float minX, float minY, float maxX, float maxY) {
64d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mHaveBounds = true;
65d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mMinX = minX;
66d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mMinY = minY;
67d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mMaxX = maxX;
68d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mMaxY = maxY;
69d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
70d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
71d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setPosition(float x, float y) {
72d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mX = x;
73d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mY = y;
74d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
75d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
76d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setButtonState(int32_t buttonState) {
77d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mButtonState = buttonState;
78d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
79d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
80d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getButtonState() const {
81d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return mButtonState;
82d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
83d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
84d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getPosition(float* outX, float* outY) const {
85d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outX = mX;
86d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outY = mY;
87d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
88d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
89d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
90d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
91d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outMinX = mMinX;
92d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outMinY = mMinY;
93d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outMaxX = mMaxX;
94d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outMaxY = mMaxY;
95d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return mHaveBounds;
96d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
97d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
98d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void move(float deltaX, float deltaY) {
99d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mX += deltaX;
100d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mX < mMinX) mX = mMinX;
101d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mX > mMaxX) mX = mMaxX;
102d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mY += deltaY;
103d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mY < mMinY) mY = mMinY;
104d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mY > mMaxY) mY = mMaxY;
105d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
106d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
107d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void fade(Transition transition) {
108d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
109d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
110d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void unfade(Transition transition) {
111d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
112d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
113d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setPresentation(Presentation presentation) {
114d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
115d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
116d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setSpots(const PointerCoords* spotCoords,
117d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
118d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
119d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
120d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void clearSpots() {
121d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
122d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
123d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
124d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
125d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// --- FakeInputReaderPolicy ---
126d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
127d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass FakeInputReaderPolicy : public InputReaderPolicyInterface {
128d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderConfiguration mConfig;
129d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
130d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Vector<InputDeviceInfo> mInputDevices;
131489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke    TouchAffineTransformation transform;
132d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
133d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
134d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~FakeInputReaderPolicy() { }
135d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
136d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
137d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    FakeInputReaderPolicy() {
138d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
139d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
140d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
141d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Set the size of both the internal and external display at the same time.
142d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        bool isRotated = (orientation == DISPLAY_ORIENTATION_90
143d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                || orientation == DISPLAY_ORIENTATION_270);
144d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        DisplayViewport v;
145d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.displayId = displayId;
146d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.orientation = orientation;
147d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.logicalLeft = 0;
148d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.logicalTop = 0;
149d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.logicalRight = isRotated ? height : width;
150d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.logicalBottom = isRotated ? width : height;
151d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.physicalLeft = 0;
152d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.physicalTop = 0;
153d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.physicalRight = isRotated ? height : width;
154d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.physicalBottom = isRotated ? width : height;
155d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.deviceWidth = isRotated ? height : width;
156d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        v.deviceHeight = isRotated ? width : height;
157d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mConfig.setDisplayInfo(false /*external*/, v);
158d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mConfig.setDisplayInfo(true /*external*/, v);
159d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
160d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
161d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addExcludedDeviceName(const String8& deviceName) {
162d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mConfig.excludedDeviceNames.push(deviceName);
163d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
164d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
165d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
166d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mPointerControllers.add(deviceId, controller);
167d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
168d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
169d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    const InputReaderConfiguration* getReaderConfiguration() const {
170d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return &mConfig;
171d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
172d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
173d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    const Vector<InputDeviceInfo>& getInputDevices() const {
174d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return mInputDevices;
175d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
176d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
17771b16e81f9cbf2e288611f32c43ea7fb4a331fcfJason Gerecke    TouchAffineTransformation getTouchAffineTransformation(const String8& inputDeviceDescriptor,
17871b16e81f9cbf2e288611f32c43ea7fb4a331fcfJason Gerecke            int32_t surfaceRotation) {
179489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke        return transform;
180489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke    }
181489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke
182489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke    void setTouchAffineTransformation(const TouchAffineTransformation t) {
183489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke        transform = t;
18412d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke    }
18512d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke
186d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
187d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
188d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outConfig = mConfig;
189d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
190d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
191d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
192d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return mPointerControllers.valueFor(deviceId);
193d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
194d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
195d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
196d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mInputDevices = inputDevices;
197d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
198d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
199d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier) {
200d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return NULL;
201d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
202d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
203d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) {
204d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return String8::empty();
205d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
206d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
207d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
208d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
209d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// --- FakeInputListener ---
210d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
211d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass FakeInputListener : public InputListenerInterface {
212d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
213d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
214d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
215d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<NotifyKeyArgs> mNotifyKeyArgsQueue;
216d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<NotifyMotionArgs> mNotifyMotionArgsQueue;
217d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<NotifySwitchArgs> mNotifySwitchArgsQueue;
218d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
219d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
220d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~FakeInputListener() { }
221d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
222d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
223d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    FakeInputListener() {
224d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
225d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
226d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyConfigurationChangedWasCalled(
227d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            NotifyConfigurationChangedArgs* outEventArgs = NULL) {
228d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_FALSE(mNotifyConfigurationChangedArgsQueue.empty())
229d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyConfigurationChanged() to have been called.";
230d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (outEventArgs) {
231d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outEventArgs = *mNotifyConfigurationChangedArgsQueue.begin();
232d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
233d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin());
234d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
235d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
236d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyDeviceResetWasCalled(
237d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            NotifyDeviceResetArgs* outEventArgs = NULL) {
238d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty())
239d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyDeviceReset() to have been called.";
240d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (outEventArgs) {
241d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outEventArgs = *mNotifyDeviceResetArgsQueue.begin();
242d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
243d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin());
244d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
245d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
246d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) {
247d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_FALSE(mNotifyKeyArgsQueue.empty())
248d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyKey() to have been called.";
249d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (outEventArgs) {
250d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outEventArgs = *mNotifyKeyArgsQueue.begin();
251d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
252d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyKeyArgsQueue.erase(mNotifyKeyArgsQueue.begin());
253d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
254d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
255d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyKeyWasNotCalled() {
256d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_TRUE(mNotifyKeyArgsQueue.empty())
257d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyKey() to not have been called.";
258d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
259d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
260d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = NULL) {
261d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_FALSE(mNotifyMotionArgsQueue.empty())
262d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyMotion() to have been called.";
263d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (outEventArgs) {
264d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outEventArgs = *mNotifyMotionArgsQueue.begin();
265d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
266d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyMotionArgsQueue.erase(mNotifyMotionArgsQueue.begin());
267d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
268d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
269d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifyMotionWasNotCalled() {
270d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_TRUE(mNotifyMotionArgsQueue.empty())
271d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifyMotion() to not have been called.";
272d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
273d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
274d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = NULL) {
275d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_FALSE(mNotifySwitchArgsQueue.empty())
276d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected notifySwitch() to have been called.";
277d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (outEventArgs) {
278d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outEventArgs = *mNotifySwitchArgsQueue.begin();
279d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
280d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifySwitchArgsQueue.erase(mNotifySwitchArgsQueue.begin());
281d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
282d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
283d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
284d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
285d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyConfigurationChangedArgsQueue.push_back(*args);
286d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
287d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
288d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) {
289d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyDeviceResetArgsQueue.push_back(*args);
290d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
291d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
292d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyKey(const NotifyKeyArgs* args) {
293d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyKeyArgsQueue.push_back(*args);
294d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
295d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
296d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyMotion(const NotifyMotionArgs* args) {
297d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifyMotionArgsQueue.push_back(*args);
298d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
299d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
300d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifySwitch(const NotifySwitchArgs* args) {
301d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNotifySwitchArgsQueue.push_back(*args);
302d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
303d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
304d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
305d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
306d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// --- FakeEventHub ---
307d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
308d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass FakeEventHub : public EventHubInterface {
309d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    struct KeyInfo {
310d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        int32_t keyCode;
311d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        uint32_t flags;
312d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    };
313d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
314d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    struct Device {
315d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        InputDeviceIdentifier identifier;
316d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        uint32_t classes;
317d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        PropertyMap configuration;
318d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
319d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int, bool> relativeAxes;
320d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, int32_t> keyCodeStates;
321d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, int32_t> scanCodeStates;
322d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, int32_t> switchStates;
323d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, int32_t> absoluteAxisValue;
324d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, KeyInfo> keysByScanCode;
325d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, KeyInfo> keysByUsageCode;
326d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyedVector<int32_t, bool> leds;
327d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Vector<VirtualKeyDefinition> virtualKeys;
328d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
329d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device(uint32_t classes) :
330d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                classes(classes) {
331d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
332d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    };
333d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
334d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    KeyedVector<int32_t, Device*> mDevices;
335d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Vector<String8> mExcludedDevices;
336d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    List<RawEvent> mEvents;
337d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
338d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
339d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~FakeEventHub() {
340d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        for (size_t i = 0; i < mDevices.size(); i++) {
341d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            delete mDevices.valueAt(i);
342d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
343d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
344d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
345d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
346d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    FakeEventHub() { }
347d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
348d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
349d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = new Device(classes);
350d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->identifier.name = name;
351d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mDevices.add(deviceId, device);
352d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
353d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
354d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
355d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
356d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void removeDevice(int32_t deviceId) {
357d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        delete mDevices.valueFor(deviceId);
358d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mDevices.removeItem(deviceId);
359d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
360d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
361d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
362d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
363d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void finishDeviceScan() {
364d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
365d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
366d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
367d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
368d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
369d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->configuration.addProperty(key, value);
370d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
371d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
372d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
373d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
374d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->configuration.addAll(configuration);
375d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
376d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
377d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addAbsoluteAxis(int32_t deviceId, int axis,
378d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
379d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
380d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
381d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        RawAbsoluteAxisInfo info;
382d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.valid = true;
383d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.minValue = minValue;
384d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.maxValue = maxValue;
385d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.flat = flat;
386d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.fuzz = fuzz;
387d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.resolution = resolution;
388d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->absoluteAxes.add(axis, info);
389d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
390d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
391d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addRelativeAxis(int32_t deviceId, int32_t axis) {
392d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
393d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->relativeAxes.add(axis, true);
394d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
395d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
396d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
397d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
398d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->keyCodeStates.replaceValueFor(keyCode, state);
399d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
400d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
401d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
402d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
403d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->scanCodeStates.replaceValueFor(scanCode, state);
404d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
405d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
406d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
407d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
408d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->switchStates.replaceValueFor(switchCode, state);
409d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
410d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
411d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
412d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
413d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->absoluteAxisValue.replaceValueFor(axis, value);
414d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
415d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
416d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
417d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t keyCode, uint32_t flags) {
418d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
419d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyInfo info;
420d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.keyCode = keyCode;
421d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        info.flags = flags;
422d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (scanCode) {
423d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            device->keysByScanCode.add(scanCode, info);
424d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
425d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (usageCode) {
426d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            device->keysByUsageCode.add(usageCode, info);
427d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
428d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
429d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
430d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addLed(int32_t deviceId, int32_t led, bool initialState) {
431d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
432d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->leds.add(led, initialState);
433d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
434d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
435d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool getLedState(int32_t deviceId, int32_t led) {
436d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
437d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return device->leds.valueFor(led);
438d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
439d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
440d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Vector<String8>& getExcludedDevices() {
441d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return mExcludedDevices;
442d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
443d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
444d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
445d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
446d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        device->virtualKeys.push(definition);
447d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
448d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
449d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
450d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t code, int32_t value) {
451d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        RawEvent event;
452d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        event.when = when;
453d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        event.deviceId = deviceId;
454d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        event.type = type;
455d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        event.code = code;
456d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        event.value = value;
457d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mEvents.push_back(event);
458d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
459d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (type == EV_ABS) {
460d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            setAbsoluteAxisValue(deviceId, code, value);
461d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
462d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
463d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
464d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void assertQueueIsEmpty() {
465d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ASSERT_EQ(size_t(0), mEvents.size())
466d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                << "Expected the event queue to be empty (fully consumed).";
467d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
468d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
469d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
470d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Device* getDevice(int32_t deviceId) const {
471d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ssize_t index = mDevices.indexOfKey(deviceId);
472d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return index >= 0 ? mDevices.valueAt(index) : NULL;
473d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
474d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
475d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual uint32_t getDeviceClasses(int32_t deviceId) const {
476d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
477d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return device ? device->classes : 0;
478d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
479d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
480d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
481d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
482d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return device ? device->identifier : InputDeviceIdentifier();
483d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
484d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
485d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getDeviceControllerNumber(int32_t deviceId) const {
486d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return 0;
487d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
488d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
489d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
490d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
491d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
492d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            *outConfiguration = device->configuration;
493d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
494d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
495d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
496d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
497d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            RawAbsoluteAxisInfo* outAxisInfo) const {
498d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
499d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
500d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->absoluteAxes.indexOfKey(axis);
501d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
502d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                *outAxisInfo = device->absoluteAxes.valueAt(index);
503d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return OK;
504d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
505d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
506d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        outAxisInfo->clear();
507d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return -1;
508d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
509d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
510d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
511d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
512d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
513d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            return device->relativeAxes.indexOfKey(axis) >= 0;
514d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
515d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
516d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
517d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
518d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasInputProperty(int32_t deviceId, int property) const {
519d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
520d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
521d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
522d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
523d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t* outKeycode, uint32_t* outFlags) const {
524d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
525d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
526d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const KeyInfo* key = getKey(device, scanCode, usageCode);
527d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (key) {
528d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                if (outKeycode) {
529d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    *outKeycode = key->keyCode;
530d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
531d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                if (outFlags) {
532d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    *outFlags = key->flags;
533d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
534d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return OK;
535d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
536d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
537d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return NAME_NOT_FOUND;
538d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
539d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
540d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
541d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (usageCode) {
542d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
543d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
544d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return &device->keysByUsageCode.valueAt(index);
545d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
546d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
547d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (scanCode) {
548d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
549d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
550d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return &device->keysByScanCode.valueAt(index);
551d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
552d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
553d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return NULL;
554d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
555d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
556d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
557d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            AxisInfo* outAxisInfo) const {
558d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return NAME_NOT_FOUND;
559d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
560d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
561d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setExcludedDevices(const Vector<String8>& devices) {
562d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mExcludedDevices = devices;
563d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
564d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
565d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
566d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mEvents.empty()) {
567d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            return 0;
568d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
569d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
570d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *buffer = *mEvents.begin();
571d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mEvents.erase(mEvents.begin());
572d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return 1;
573d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
574d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
575d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
576d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
577d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
578d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
579d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
580d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return device->scanCodeStates.valueAt(index);
581d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
582d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
583d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return AKEY_STATE_UNKNOWN;
584d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
585d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
586d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
587d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
588d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
589d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
590d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
591d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return device->keyCodeStates.valueAt(index);
592d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
593d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
594d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return AKEY_STATE_UNKNOWN;
595d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
596d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
597d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
598d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
599d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
600d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->switchStates.indexOfKey(sw);
601d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
602d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return device->switchStates.valueAt(index);
603d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
604d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
605d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return AKEY_STATE_UNKNOWN;
606d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
607d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
608d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
609d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t* outValue) const {
610d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
611d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
612d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
613d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
614d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                *outValue = device->absoluteAxisValue.valueAt(index);
615d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return OK;
616d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
617d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
618d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *outValue = 0;
619d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return -1;
620d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
621d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
622d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
623d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            uint8_t* outFlags) const {
624d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        bool result = false;
625d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
626d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
627d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            for (size_t i = 0; i < numCodes; i++) {
628d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
629d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
630d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        outFlags[i] = 1;
631d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        result = true;
632d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    }
633d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
634d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
635d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
636d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        outFlags[i] = 1;
637d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        result = true;
638d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    }
639d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
640d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
641d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
642d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return result;
643d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
644d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
645d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
646d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
647d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
648d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
649d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            return index >= 0;
650d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
651d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
652d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
653d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
654d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasLed(int32_t deviceId, int32_t led) const {
655d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
656d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return device && device->leds.indexOfKey(led) >= 0;
657d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
658d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
659d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
660d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
661d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
662d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t index = device->leds.indexOfKey(led);
663d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (index >= 0) {
664d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                device->leds.replaceValueAt(led, on);
665d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            } else {
666d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                ADD_FAILURE()
667d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        << "Attempted to set the state of an LED that the EventHub declared "
668d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        "was not present.  led=" << led;
669d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
670d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
671d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
672d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
673d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getVirtualKeyDefinitions(int32_t deviceId,
674d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            Vector<VirtualKeyDefinition>& outVirtualKeys) const {
675d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        outVirtualKeys.clear();
676d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
677d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Device* device = getDevice(deviceId);
678d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (device) {
679d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            outVirtualKeys.appendVector(device->virtualKeys);
680d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
681d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
682d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
683d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const {
684d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return NULL;
685d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
686d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
687d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) {
688d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
689d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
690d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
691d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void vibrate(int32_t deviceId, nsecs_t duration) {
692d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
693d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
694d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void cancelVibrate(int32_t deviceId) {
695d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
696d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
697d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool isExternal(int32_t deviceId) const {
698d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
699d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
700d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
701d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void dump(String8& dump) {
702d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
703d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
704d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void monitor() {
705d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
706d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
707d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void requestReopenDevices() {
708d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
709