blob: 3bb9c01d3c3e7cfe896f21a4989ccfb9553404d2 [file] [log] [blame]
Jeff Brownc5ed5912010-07-14 18:48:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jeff Browna47425a2012-04-13 04:09:27 -070019import android.content.Context;
Jeff Brownac143512012-04-05 18:57:33 -070020import android.hardware.input.InputManager;
Jeff Brown8d608662010-08-30 03:02:23 -070021import android.os.Parcel;
22import android.os.Parcelable;
Jeff Browna47425a2012-04-13 04:09:27 -070023import android.os.Vibrator;
24import android.os.NullVibrator;
Jeff Brownefd32662011-03-08 15:13:06 -080025
26import java.util.ArrayList;
27import java.util.List;
Jeff Brown8d608662010-08-30 03:02:23 -070028
Jeff Brownc5ed5912010-07-14 18:48:53 -070029/**
30 * Describes the capabilities of a particular input device.
31 * <p>
Jeff Brown9df6e7a2012-04-05 11:49:26 -070032 * Each input device may support multiple classes of input. For example, a multi-function
Jeff Brownc5ed5912010-07-14 18:48:53 -070033 * keyboard may compose the capabilities of a standard keyboard together with a track pad mouse
34 * or other pointing device.
35 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070036 * Some input devices present multiple distinguishable sources of input.
Jeff Brownc5ed5912010-07-14 18:48:53 -070037 * Applications can query the framework about the characteristics of each distinct source.
38 * </p><p>
39 * As a further wrinkle, different kinds of input sources uses different coordinate systems
40 * to describe motion events. Refer to the comments on the input source constants for
41 * the appropriate interpretation.
Jeff Brown6d0fec22010-07-23 21:28:06 -070042 * </p>
Jeff Brownc5ed5912010-07-14 18:48:53 -070043 */
Jeff Brown8d608662010-08-30 03:02:23 -070044public final class InputDevice implements Parcelable {
Jeff Brown9f25b7f2012-04-10 14:30:49 -070045 private final int mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070046 private final int mGeneration;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070047 private final String mName;
48 private final String mDescriptor;
Jeff Browndaa37532012-05-01 15:54:03 -070049 private final boolean mIsExternal;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070050 private final int mSources;
51 private final int mKeyboardType;
52 private final KeyCharacterMap mKeyCharacterMap;
Jeff Browna47425a2012-04-13 04:09:27 -070053 private final boolean mHasVibrator;
Jeff Brownefd32662011-03-08 15:13:06 -080054 private final ArrayList<MotionRange> mMotionRanges = new ArrayList<MotionRange>();
Jeff Brown91c69ab2011-02-14 17:03:18 -080055
Jeff Browna47425a2012-04-13 04:09:27 -070056 private Vibrator mVibrator; // guarded by mMotionRanges during initialization
57
Jeff Brownc5ed5912010-07-14 18:48:53 -070058 /**
59 * A mask for input source classes.
60 *
61 * Each distinct input source constant has one or more input source class bits set to
62 * specify the desired interpretation for its input events.
63 */
64 public static final int SOURCE_CLASS_MASK = 0x000000ff;
65
66 /**
67 * The input source has buttons or keys.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070068 * Examples: {@link #SOURCE_KEYBOARD}, {@link #SOURCE_DPAD}.
Jeff Brownc5ed5912010-07-14 18:48:53 -070069 *
70 * A {@link KeyEvent} should be interpreted as a button or key press.
71 *
Jeff Brown6eb5ac92010-08-30 23:29:07 -070072 * Use {@link #getKeyCharacterMap} to query the device's button and key mappings.
Jeff Brownc5ed5912010-07-14 18:48:53 -070073 */
74 public static final int SOURCE_CLASS_BUTTON = 0x00000001;
75
76 /**
77 * The input source is a pointing device associated with a display.
78 * Examples: {@link #SOURCE_TOUCHSCREEN}, {@link #SOURCE_MOUSE}.
79 *
80 * A {@link MotionEvent} should be interpreted as absolute coordinates in
81 * display units according to the {@link View} hierarchy. Pointer down/up indicated when
82 * the finger touches the display or when the selection button is pressed/released.
83 *
84 * Use {@link #getMotionRange} to query the range of the pointing device. Some devices permit
85 * touches outside the display area so the effective range may be somewhat smaller or larger
86 * than the actual display size.
87 */
88 public static final int SOURCE_CLASS_POINTER = 0x00000002;
89
90 /**
91 * The input source is a trackball navigation device.
92 * Examples: {@link #SOURCE_TRACKBALL}.
93 *
94 * A {@link MotionEvent} should be interpreted as relative movements in device-specific
95 * units used for navigation purposes. Pointer down/up indicates when the selection button
96 * is pressed/released.
97 *
98 * Use {@link #getMotionRange} to query the range of motion.
99 */
100 public static final int SOURCE_CLASS_TRACKBALL = 0x00000004;
101
102 /**
103 * The input source is an absolute positioning device not associated with a display
104 * (unlike {@link #SOURCE_CLASS_POINTER}).
105 *
106 * A {@link MotionEvent} should be interpreted as absolute coordinates in
107 * device-specific surface units.
108 *
109 * Use {@link #getMotionRange} to query the range of positions.
110 */
111 public static final int SOURCE_CLASS_POSITION = 0x00000008;
Jeff Browncb1404e2011-01-15 18:14:15 -0800112
113 /**
114 * The input source is a joystick.
115 *
116 * A {@link MotionEvent} should be interpreted as absolute joystick movements.
117 *
118 * Use {@link #getMotionRange} to query the range of positions.
119 */
120 public static final int SOURCE_CLASS_JOYSTICK = 0x00000010;
121
Jeff Brownc5ed5912010-07-14 18:48:53 -0700122 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700123 * The input source is unknown.
124 */
125 public static final int SOURCE_UNKNOWN = 0x00000000;
126
127 /**
128 * The input source is a keyboard.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700129 *
130 * This source indicates pretty much anything that has buttons. Use
131 * {@link #getKeyboardType()} to determine whether the keyboard has alphabetic keys
132 * and can be used to enter text.
133 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700134 * @see #SOURCE_CLASS_BUTTON
135 */
136 public static final int SOURCE_KEYBOARD = 0x00000100 | SOURCE_CLASS_BUTTON;
137
138 /**
139 * The input source is a DPad.
140 *
141 * @see #SOURCE_CLASS_BUTTON
142 */
143 public static final int SOURCE_DPAD = 0x00000200 | SOURCE_CLASS_BUTTON;
Jeff Browncb1404e2011-01-15 18:14:15 -0800144
145 /**
146 * The input source is a game pad.
147 * (It may also be a {@link #SOURCE_JOYSTICK}).
148 *
149 * @see #SOURCE_CLASS_BUTTON
150 */
151 public static final int SOURCE_GAMEPAD = 0x00000400 | SOURCE_CLASS_BUTTON;
152
Jeff Brownc5ed5912010-07-14 18:48:53 -0700153 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700154 * The input source is a touch screen pointing device.
155 *
156 * @see #SOURCE_CLASS_POINTER
157 */
158 public static final int SOURCE_TOUCHSCREEN = 0x00001000 | SOURCE_CLASS_POINTER;
159
160 /**
161 * The input source is a mouse pointing device.
162 * This code is also used for other mouse-like pointing devices such as trackpads
163 * and trackpoints.
164 *
165 * @see #SOURCE_CLASS_POINTER
166 */
167 public static final int SOURCE_MOUSE = 0x00002000 | SOURCE_CLASS_POINTER;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700168
169 /**
170 * The input source is a stylus pointing device.
Jeff Brown00710e92012-04-19 15:18:26 -0700171 * <p>
172 * Note that this bit merely indicates that an input device is capable of obtaining
173 * input from a stylus. To determine whether a given touch event was produced
174 * by a stylus, examine the tool type returned by {@link MotionEvent#getToolType(int)}
175 * for each individual pointer.
176 * </p><p>
177 * A single touch event may multiple pointers with different tool types,
178 * such as an event that has one pointer with tool type
179 * {@link MotionEvent#TOOL_TYPE_FINGER} and another pointer with tool type
180 * {@link MotionEvent#TOOL_TYPE_STYLUS}. So it is important to examine
181 * the tool type of each pointer, regardless of the source reported
182 * by {@link MotionEvent#getSource()}.
183 * </p>
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 *
185 * @see #SOURCE_CLASS_POINTER
186 */
187 public static final int SOURCE_STYLUS = 0x00004000 | SOURCE_CLASS_POINTER;
188
Jeff Brownc5ed5912010-07-14 18:48:53 -0700189 /**
190 * The input source is a trackball.
191 *
192 * @see #SOURCE_CLASS_TRACKBALL
193 */
194 public static final int SOURCE_TRACKBALL = 0x00010000 | SOURCE_CLASS_TRACKBALL;
195
196 /**
197 * The input source is a touch pad or digitizer tablet that is not
Jeff Browne33348b2010-07-15 23:54:05 -0700198 * associated with a display (unlike {@link #SOURCE_TOUCHSCREEN}).
Jeff Brownc5ed5912010-07-14 18:48:53 -0700199 *
200 * @see #SOURCE_CLASS_POSITION
201 */
202 public static final int SOURCE_TOUCHPAD = 0x00100000 | SOURCE_CLASS_POSITION;
Jeff Browncb1404e2011-01-15 18:14:15 -0800203
204 /**
205 * The input source is a joystick.
206 * (It may also be a {@link #SOURCE_GAMEPAD}).
207 *
208 * @see #SOURCE_CLASS_JOYSTICK
209 */
210 public static final int SOURCE_JOYSTICK = 0x01000000 | SOURCE_CLASS_JOYSTICK;
211
Jeff Brown6d0fec22010-07-23 21:28:06 -0700212 /**
213 * A special input source constant that is used when filtering input devices
214 * to match devices that provide any type of input source.
215 */
216 public static final int SOURCE_ANY = 0xffffff00;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700217
Jeff Browne33348b2010-07-15 23:54:05 -0700218 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800219 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_X}.
Jeff Browne33348b2010-07-15 23:54:05 -0700220 *
221 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800222 * @deprecated Use {@link MotionEvent#AXIS_X} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700223 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800224 @Deprecated
225 public static final int MOTION_RANGE_X = MotionEvent.AXIS_X;
226
Jeff Browne33348b2010-07-15 23:54:05 -0700227 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800228 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_Y}.
Jeff Browne33348b2010-07-15 23:54:05 -0700229 *
230 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800231 * @deprecated Use {@link MotionEvent#AXIS_Y} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700232 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800233 @Deprecated
234 public static final int MOTION_RANGE_Y = MotionEvent.AXIS_Y;
235
Jeff Browne33348b2010-07-15 23:54:05 -0700236 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800237 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_PRESSURE}.
Jeff Browne33348b2010-07-15 23:54:05 -0700238 *
239 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800240 * @deprecated Use {@link MotionEvent#AXIS_PRESSURE} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700241 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800242 @Deprecated
243 public static final int MOTION_RANGE_PRESSURE = MotionEvent.AXIS_PRESSURE;
244
Jeff Browne33348b2010-07-15 23:54:05 -0700245 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800246 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_SIZE}.
Jeff Browne33348b2010-07-15 23:54:05 -0700247 *
248 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800249 * @deprecated Use {@link MotionEvent#AXIS_SIZE} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700250 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800251 @Deprecated
252 public static final int MOTION_RANGE_SIZE = MotionEvent.AXIS_SIZE;
253
Jeff Browne33348b2010-07-15 23:54:05 -0700254 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800255 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MAJOR}.
Jeff Browne33348b2010-07-15 23:54:05 -0700256 *
257 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800258 * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MAJOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700259 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800260 @Deprecated
261 public static final int MOTION_RANGE_TOUCH_MAJOR = MotionEvent.AXIS_TOUCH_MAJOR;
262
Jeff Browne33348b2010-07-15 23:54:05 -0700263 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800264 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MINOR}.
Jeff Browne33348b2010-07-15 23:54:05 -0700265 *
266 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800267 * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MINOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700268 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800269 @Deprecated
270 public static final int MOTION_RANGE_TOUCH_MINOR = MotionEvent.AXIS_TOUCH_MINOR;
271
Jeff Browne33348b2010-07-15 23:54:05 -0700272 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800273 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MAJOR}.
Jeff Browne33348b2010-07-15 23:54:05 -0700274 *
275 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800276 * @deprecated Use {@link MotionEvent#AXIS_TOOL_MAJOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700277 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800278 @Deprecated
279 public static final int MOTION_RANGE_TOOL_MAJOR = MotionEvent.AXIS_TOOL_MAJOR;
280
Jeff Browne33348b2010-07-15 23:54:05 -0700281 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800282 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MINOR}.
Jeff Browne33348b2010-07-15 23:54:05 -0700283 *
284 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800285 * @deprecated Use {@link MotionEvent#AXIS_TOOL_MINOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700286 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800287 @Deprecated
288 public static final int MOTION_RANGE_TOOL_MINOR = MotionEvent.AXIS_TOOL_MINOR;
289
Jeff Browne33348b2010-07-15 23:54:05 -0700290 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800291 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_ORIENTATION}.
Jeff Browne33348b2010-07-15 23:54:05 -0700292 *
293 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800294 * @deprecated Use {@link MotionEvent#AXIS_ORIENTATION} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700295 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800296 @Deprecated
297 public static final int MOTION_RANGE_ORIENTATION = MotionEvent.AXIS_ORIENTATION;
Jeff Brown8d608662010-08-30 03:02:23 -0700298
Jeff Brown6d0fec22010-07-23 21:28:06 -0700299 /**
300 * There is no keyboard.
301 */
302 public static final int KEYBOARD_TYPE_NONE = 0;
303
304 /**
305 * The keyboard is not fully alphabetic. It may be a numeric keypad or an assortment
306 * of buttons that are not mapped as alphabetic keys suitable for text input.
307 */
308 public static final int KEYBOARD_TYPE_NON_ALPHABETIC = 1;
309
310 /**
311 * The keyboard supports a complement of alphabetic keys.
312 */
313 public static final int KEYBOARD_TYPE_ALPHABETIC = 2;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800314
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700315 public static final Parcelable.Creator<InputDevice> CREATOR =
316 new Parcelable.Creator<InputDevice>() {
317 public InputDevice createFromParcel(Parcel in) {
318 return new InputDevice(in);
319 }
320 public InputDevice[] newArray(int size) {
321 return new InputDevice[size];
322 }
323 };
324
Jeff Brown8d608662010-08-30 03:02:23 -0700325 // Called by native code.
Jeff Browndaa37532012-05-01 15:54:03 -0700326 private InputDevice(int id, int generation, String name, String descriptor,
327 boolean isExternal, int sources,
Jeff Browna47425a2012-04-13 04:09:27 -0700328 int keyboardType, KeyCharacterMap keyCharacterMap, boolean hasVibrator) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700329 mId = id;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700330 mGeneration = generation;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700331 mName = name;
332 mDescriptor = descriptor;
Jeff Browndaa37532012-05-01 15:54:03 -0700333 mIsExternal = isExternal;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700334 mSources = sources;
335 mKeyboardType = keyboardType;
336 mKeyCharacterMap = keyCharacterMap;
Jeff Browna47425a2012-04-13 04:09:27 -0700337 mHasVibrator = hasVibrator;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700338 }
339
340 private InputDevice(Parcel in) {
341 mId = in.readInt();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700342 mGeneration = in.readInt();
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700343 mName = in.readString();
344 mDescriptor = in.readString();
Jeff Browndaa37532012-05-01 15:54:03 -0700345 mIsExternal = in.readInt() != 0;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700346 mSources = in.readInt();
347 mKeyboardType = in.readInt();
348 mKeyCharacterMap = KeyCharacterMap.CREATOR.createFromParcel(in);
Jeff Browna47425a2012-04-13 04:09:27 -0700349 mHasVibrator = in.readInt() != 0;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700350
351 for (;;) {
352 int axis = in.readInt();
353 if (axis < 0) {
354 break;
355 }
356 addMotionRange(axis, in.readInt(),
357 in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat());
358 }
Jeff Brown8d608662010-08-30 03:02:23 -0700359 }
Jeff Browne33348b2010-07-15 23:54:05 -0700360
361 /**
362 * Gets information about the input device with the specified id.
363 * @param id The device id.
364 * @return The input device or null if not found.
365 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700366 public static InputDevice getDevice(int id) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700367 return InputManager.getInstance().getInputDevice(id);
Jeff Brown8d608662010-08-30 03:02:23 -0700368 }
369
370 /**
371 * Gets the ids of all input devices in the system.
372 * @return The input device ids.
373 */
374 public static int[] getDeviceIds() {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700375 return InputManager.getInstance().getInputDeviceIds();
Jeff Brown8d608662010-08-30 03:02:23 -0700376 }
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700377
Jeff Brown8d608662010-08-30 03:02:23 -0700378 /**
379 * Gets the input device id.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700380 * <p>
381 * Each input device receives a unique id when it is first configured
382 * by the system. The input device id may change when the system is restarted or if the
383 * input device is disconnected, reconnected or reconfigured at any time.
384 * If you require a stable identifier for a device that persists across
385 * boots and reconfigurations, use {@link #getDescriptor()}.
386 * </p>
387 *
Jeff Brown8d608662010-08-30 03:02:23 -0700388 * @return The input device id.
389 */
390 public int getId() {
391 return mId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700392 }
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700393
394 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700395 * Gets a generation number for this input device.
396 * The generation number is incremented whenever the device is reconfigured and its
397 * properties may have changed.
398 *
399 * @return The generation number.
400 *
401 * @hide
402 */
403 public int getGeneration() {
404 return mGeneration;
405 }
406
407 /**
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700408 * Gets the input device descriptor, which is a stable identifier for an input device.
409 * <p>
410 * An input device descriptor uniquely identifies an input device. Its value
411 * is intended to be persistent across system restarts, and should not change even
412 * if the input device is disconnected, reconnected or reconfigured at any time.
Jeff Browne38fdfa2012-04-06 14:51:01 -0700413 * </p><p>
414 * It is possible for there to be multiple {@link InputDevice} instances that have the
415 * same input device descriptor. This might happen in situations where a single
416 * human input device registers multiple {@link InputDevice} instances (HID collections)
417 * that describe separate features of the device, such as a keyboard that also
418 * has a trackpad. Alternately, it may be that the input devices are simply
419 * indistinguishable, such as two keyboards made by the same manufacturer.
420 * </p><p>
Jeff Browndaa37532012-05-01 15:54:03 -0700421 * The input device descriptor returned by {@link #getDescriptor} should only be
Jeff Browne38fdfa2012-04-06 14:51:01 -0700422 * used when an application needs to remember settings associated with a particular
423 * input device. For all other purposes when referring to a logical
424 * {@link InputDevice} instance at runtime use the id returned by {@link #getId()}.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700425 * </p>
426 *
427 * @return The input device descriptor.
428 */
429 public String getDescriptor() {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700430 return mDescriptor;
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700431 }
432
Jeff Brownc5ed5912010-07-14 18:48:53 -0700433 /**
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700434 * Returns true if the device is a virtual input device rather than a real one,
435 * such as the virtual keyboard (see {@link KeyCharacterMap#VIRTUAL_KEYBOARD}).
436 * <p>
437 * Virtual input devices are provided to implement system-level functionality
438 * and should not be seen or configured by users.
439 * </p>
440 *
441 * @return True if the device is virtual.
442 *
443 * @see KeyCharacterMap#VIRTUAL_KEYBOARD
444 */
445 public boolean isVirtual() {
446 return mId < 0;
447 }
448
449 /**
Jeff Browndaa37532012-05-01 15:54:03 -0700450 * Returns true if the device is external (connected to USB or Bluetooth or some other
451 * peripheral bus), otherwise it is built-in.
452 *
453 * @return True if the device is external.
454 *
455 * @hide
456 */
457 public boolean isExternal() {
458 return mIsExternal;
459 }
460
461 /**
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700462 * Returns true if the device is a full keyboard.
463 *
464 * @return True if the device is a full keyboard.
465 *
466 * @hide
467 */
468 public boolean isFullKeyboard() {
469 return (mSources & SOURCE_KEYBOARD) == SOURCE_KEYBOARD
470 && mKeyboardType == KEYBOARD_TYPE_ALPHABETIC;
471 }
472
473 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700474 * Gets the name of this input device.
475 * @return The input device name.
476 */
477 public String getName() {
478 return mName;
479 }
480
481 /**
482 * Gets the input sources supported by this input device as a combined bitfield.
483 * @return The supported input sources.
484 */
485 public int getSources() {
486 return mSources;
487 }
488
489 /**
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490 * Gets the keyboard type.
491 * @return The keyboard type.
492 */
493 public int getKeyboardType() {
494 return mKeyboardType;
495 }
496
497 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700498 * Gets the key character map associated with this input device.
499 * @return The key character map.
500 */
501 public KeyCharacterMap getKeyCharacterMap() {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700502 return mKeyCharacterMap;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800503 }
504
Jeff Browne33348b2010-07-15 23:54:05 -0700505 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800506 * Gets information about the range of values for a particular {@link MotionEvent} axis.
Jeff Brownefd32662011-03-08 15:13:06 -0800507 * If the device supports multiple sources, the same axis may have different meanings
508 * for each source. Returns information about the first axis found for any source.
509 * To obtain information about the axis for a specific source, use
510 * {@link #getMotionRange(int, int)}.
511 *
Jeff Brown91c69ab2011-02-14 17:03:18 -0800512 * @param axis The axis constant.
513 * @return The range of values, or null if the requested axis is not
Jeff Browne33348b2010-07-15 23:54:05 -0700514 * supported by the device.
Jeff Brown91c69ab2011-02-14 17:03:18 -0800515 *
516 * @see MotionEvent#AXIS_X
517 * @see MotionEvent#AXIS_Y
Jeff Brown6f2fba42011-02-19 01:08:02 -0800518 * @see #getSupportedAxes()
Jeff Browne33348b2010-07-15 23:54:05 -0700519 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800520 public MotionRange getMotionRange(int axis) {
Jeff Brownefd32662011-03-08 15:13:06 -0800521 final int numRanges = mMotionRanges.size();
522 for (int i = 0; i < numRanges; i++) {
523 final MotionRange range = mMotionRanges.get(i);
524 if (range.mAxis == axis) {
525 return range;
526 }
527 }
528 return null;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700529 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800530
Jeff Brown6f2fba42011-02-19 01:08:02 -0800531 /**
Jeff Brownefd32662011-03-08 15:13:06 -0800532 * Gets information about the range of values for a particular {@link MotionEvent} axis
533 * used by a particular source on the device.
534 * If the device supports multiple sources, the same axis may have different meanings
535 * for each source.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800536 *
Jeff Brownefd32662011-03-08 15:13:06 -0800537 * @param axis The axis constant.
538 * @param source The source for which to return information.
539 * @return The range of values, or null if the requested axis is not
540 * supported by the device.
541 *
542 * @see MotionEvent#AXIS_X
543 * @see MotionEvent#AXIS_Y
544 * @see #getSupportedAxes()
Jeff Brown6f2fba42011-02-19 01:08:02 -0800545 */
Jeff Brownefd32662011-03-08 15:13:06 -0800546 public MotionRange getMotionRange(int axis, int source) {
547 final int numRanges = mMotionRanges.size();
548 for (int i = 0; i < numRanges; i++) {
549 final MotionRange range = mMotionRanges.get(i);
550 if (range.mAxis == axis && range.mSource == source) {
551 return range;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800552 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800553 }
Jeff Brownefd32662011-03-08 15:13:06 -0800554 return null;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800555 }
556
Jeff Brownefd32662011-03-08 15:13:06 -0800557 /**
558 * Gets the ranges for all axes supported by the device.
559 * @return The motion ranges for the device.
560 *
561 * @see #getMotionRange(int, int)
562 */
563 public List<MotionRange> getMotionRanges() {
564 return mMotionRanges;
565 }
566
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700567 // Called from native code.
Jeff Brownefd32662011-03-08 15:13:06 -0800568 private void addMotionRange(int axis, int source,
569 float min, float max, float flat, float fuzz) {
570 mMotionRanges.add(new MotionRange(axis, source, min, max, flat, fuzz));
Jeff Brownc5ed5912010-07-14 18:48:53 -0700571 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800572
Jeff Browne33348b2010-07-15 23:54:05 -0700573 /**
Jeff Browna47425a2012-04-13 04:09:27 -0700574 * Gets the vibrator service associated with the device, if there is one.
575 * Even if the device does not have a vibrator, the result is never null.
576 * Use {@link Vibrator#hasVibrator} to determine whether a vibrator is
577 * present.
578 *
579 * Note that the vibrator associated with the device may be different from
580 * the system vibrator. To obtain an instance of the system vibrator instead, call
581 * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
582 *
583 * @return The vibrator service associated with the device, never null.
584 */
585 public Vibrator getVibrator() {
586 synchronized (mMotionRanges) {
587 if (mVibrator == null) {
588 if (mHasVibrator) {
589 mVibrator = InputManager.getInstance().getInputDeviceVibrator(mId);
590 } else {
591 mVibrator = NullVibrator.getInstance();
592 }
593 }
594 return mVibrator;
595 }
596 }
597
598 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800599 * Provides information about the range of values for a particular {@link MotionEvent} axis.
600 *
601 * @see InputDevice#getMotionRange(int)
Jeff Browne33348b2010-07-15 23:54:05 -0700602 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700603 public static final class MotionRange {
Jeff Brownefd32662011-03-08 15:13:06 -0800604 private int mAxis;
605 private int mSource;
Jeff Brown8d608662010-08-30 03:02:23 -0700606 private float mMin;
607 private float mMax;
608 private float mFlat;
609 private float mFuzz;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800610
Jeff Brownefd32662011-03-08 15:13:06 -0800611 private MotionRange(int axis, int source, float min, float max, float flat, float fuzz) {
612 mAxis = axis;
613 mSource = source;
Jeff Brown8d608662010-08-30 03:02:23 -0700614 mMin = min;
615 mMax = max;
616 mFlat = flat;
617 mFuzz = fuzz;
618 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800619
Jeff Browne33348b2010-07-15 23:54:05 -0700620 /**
Jeff Brownefd32662011-03-08 15:13:06 -0800621 * Gets the axis id.
622 * @return The axis id.
623 */
624 public int getAxis() {
625 return mAxis;
626 }
627
628 /**
629 * Gets the source for which the axis is defined.
630 * @return The source.
631 */
632 public int getSource() {
633 return mSource;
634 }
635
636 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800637 * Gets the inclusive minimum value for the axis.
638 * @return The inclusive minimum value.
Jeff Browne33348b2010-07-15 23:54:05 -0700639 */
640 public float getMin() {
Jeff Brown8d608662010-08-30 03:02:23 -0700641 return mMin;
Jeff Browne33348b2010-07-15 23:54:05 -0700642 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800643
Jeff Browne33348b2010-07-15 23:54:05 -0700644 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800645 * Gets the inclusive maximum value for the axis.
646 * @return The inclusive maximum value.
Jeff Browne33348b2010-07-15 23:54:05 -0700647 */
648 public float getMax() {
Jeff Brown8d608662010-08-30 03:02:23 -0700649 return mMax;
Jeff Browne33348b2010-07-15 23:54:05 -0700650 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800651
Jeff Browne33348b2010-07-15 23:54:05 -0700652 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800653 * Gets the range of the axis (difference between maximum and minimum).
Jeff Browne33348b2010-07-15 23:54:05 -0700654 * @return The range of values.
655 */
656 public float getRange() {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800657 return mMax - mMin;
Jeff Browne33348b2010-07-15 23:54:05 -0700658 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800659
Jeff Browne33348b2010-07-15 23:54:05 -0700660 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800661 * Gets the extent of the center flat position with respect to this axis.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800662 * <p>
Jeff Browne33348b2010-07-15 23:54:05 -0700663 * For example, a flat value of 8 means that the center position is between -8 and +8.
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700664 * This value is mainly useful for calibrating self-centering devices.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800665 * </p>
Jeff Browne33348b2010-07-15 23:54:05 -0700666 * @return The extent of the center flat position.
667 */
668 public float getFlat() {
Jeff Brown8d608662010-08-30 03:02:23 -0700669 return mFlat;
Jeff Browne33348b2010-07-15 23:54:05 -0700670 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800671
Jeff Browne33348b2010-07-15 23:54:05 -0700672 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800673 * Gets the error tolerance for input device measurements with respect to this axis.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800674 * <p>
Jeff Browne33348b2010-07-15 23:54:05 -0700675 * For example, a value of 2 indicates that the measured value may be up to +/- 2 units
676 * away from the actual value due to noise and device sensitivity limitations.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800677 * </p>
Jeff Browne33348b2010-07-15 23:54:05 -0700678 * @return The error tolerance.
679 */
680 public float getFuzz() {
Jeff Brown8d608662010-08-30 03:02:23 -0700681 return mFuzz;
682 }
683 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800684
Jeff Brown8d608662010-08-30 03:02:23 -0700685 @Override
686 public void writeToParcel(Parcel out, int flags) {
687 out.writeInt(mId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700688 out.writeInt(mGeneration);
Jeff Brown8d608662010-08-30 03:02:23 -0700689 out.writeString(mName);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700690 out.writeString(mDescriptor);
Jeff Browndaa37532012-05-01 15:54:03 -0700691 out.writeInt(mIsExternal ? 1 : 0);
Jeff Brown8d608662010-08-30 03:02:23 -0700692 out.writeInt(mSources);
693 out.writeInt(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700694 mKeyCharacterMap.writeToParcel(out, flags);
Jeff Browna47425a2012-04-13 04:09:27 -0700695 out.writeInt(mHasVibrator ? 1 : 0);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800696
Jeff Brownefd32662011-03-08 15:13:06 -0800697 final int numRanges = mMotionRanges.size();
698 for (int i = 0; i < numRanges; i++) {
699 MotionRange range = mMotionRanges.get(i);
700 out.writeInt(range.mAxis);
701 out.writeInt(range.mSource);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800702 out.writeFloat(range.mMin);
703 out.writeFloat(range.mMax);
704 out.writeFloat(range.mFlat);
705 out.writeFloat(range.mFuzz);
Jeff Brown8d608662010-08-30 03:02:23 -0700706 }
707 out.writeInt(-1);
708 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800709
Jeff Brown8d608662010-08-30 03:02:23 -0700710 @Override
711 public int describeContents() {
712 return 0;
713 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800714
Jeff Brown8d608662010-08-30 03:02:23 -0700715 @Override
716 public String toString() {
717 StringBuilder description = new StringBuilder();
718 description.append("Input Device ").append(mId).append(": ").append(mName).append("\n");
Jeff Browne38fdfa2012-04-06 14:51:01 -0700719 description.append(" Descriptor: ").append(mDescriptor).append("\n");
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700720 description.append(" Generation: ").append(mGeneration).append("\n");
Jeff Browndaa37532012-05-01 15:54:03 -0700721 description.append(" Location: ").append(mIsExternal ? "external" : "built-in").append("\n");
Jeff Browne38fdfa2012-04-06 14:51:01 -0700722
Jeff Brown8d608662010-08-30 03:02:23 -0700723 description.append(" Keyboard Type: ");
724 switch (mKeyboardType) {
725 case KEYBOARD_TYPE_NONE:
726 description.append("none");
727 break;
728 case KEYBOARD_TYPE_NON_ALPHABETIC:
729 description.append("non-alphabetic");
730 break;
731 case KEYBOARD_TYPE_ALPHABETIC:
732 description.append("alphabetic");
733 break;
734 }
735 description.append("\n");
Jeff Brown91c69ab2011-02-14 17:03:18 -0800736
Jeff Browna47425a2012-04-13 04:09:27 -0700737 description.append(" Has Vibrator: ").append(mHasVibrator).append("\n");
738
Jeff Brownefd32662011-03-08 15:13:06 -0800739 description.append(" Sources: 0x").append(Integer.toHexString(mSources)).append(" (");
Jeff Brown8d608662010-08-30 03:02:23 -0700740 appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard");
741 appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad");
Jeff Brown8d608662010-08-30 03:02:23 -0700742 appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHSCREEN, "touchscreen");
743 appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE, "mouse");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700744 appendSourceDescriptionIfApplicable(description, SOURCE_STYLUS, "stylus");
Jeff Brown8d608662010-08-30 03:02:23 -0700745 appendSourceDescriptionIfApplicable(description, SOURCE_TRACKBALL, "trackball");
746 appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHPAD, "touchpad");
Jeff Brown91c69ab2011-02-14 17:03:18 -0800747 appendSourceDescriptionIfApplicable(description, SOURCE_JOYSTICK, "joystick");
748 appendSourceDescriptionIfApplicable(description, SOURCE_GAMEPAD, "gamepad");
749 description.append(" )\n");
750
751 final int numAxes = mMotionRanges.size();
752 for (int i = 0; i < numAxes; i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800753 MotionRange range = mMotionRanges.get(i);
754 description.append(" ").append(MotionEvent.axisToString(range.mAxis));
755 description.append(": source=0x").append(Integer.toHexString(range.mSource));
756 description.append(" min=").append(range.mMin);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800757 description.append(" max=").append(range.mMax);
758 description.append(" flat=").append(range.mFlat);
759 description.append(" fuzz=").append(range.mFuzz);
760 description.append("\n");
761 }
Jeff Brown8d608662010-08-30 03:02:23 -0700762 return description.toString();
763 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800764
Jeff Brown8d608662010-08-30 03:02:23 -0700765 private void appendSourceDescriptionIfApplicable(StringBuilder description, int source,
766 String sourceName) {
767 if ((mSources & source) == source) {
768 description.append(" ");
769 description.append(sourceName);
770 }
771 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700772}