blob: 7295259a0f89e516afcde86328e7c457cb91828e [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
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070019import android.annotation.RequiresPermission;
20import android.annotation.TestApi;
Mathew Inwooda570dee2018-08-17 14:56:00 +010021import android.annotation.UnsupportedAppUsage;
Jeff Browna47425a2012-04-13 04:09:27 -070022import android.content.Context;
RoboErikca9eef62013-12-16 11:27:55 -080023import android.hardware.input.InputDeviceIdentifier;
Jeff Brownac143512012-04-05 18:57:33 -070024import android.hardware.input.InputManager;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070025import android.os.NullVibrator;
Jeff Brown8d608662010-08-30 03:02:23 -070026import android.os.Parcel;
27import android.os.Parcelable;
Jeff Browna47425a2012-04-13 04:09:27 -070028import android.os.Vibrator;
Jeff Brownefd32662011-03-08 15:13:06 -080029
30import java.util.ArrayList;
31import java.util.List;
Jeff Brown8d608662010-08-30 03:02:23 -070032
Jeff Brownc5ed5912010-07-14 18:48:53 -070033/**
34 * Describes the capabilities of a particular input device.
35 * <p>
Jeff Brown9df6e7a2012-04-05 11:49:26 -070036 * Each input device may support multiple classes of input. For example, a multi-function
Jeff Brownc5ed5912010-07-14 18:48:53 -070037 * keyboard may compose the capabilities of a standard keyboard together with a track pad mouse
38 * or other pointing device.
39 * </p><p>
Jeff Browndc1ab4b2010-09-14 18:03:38 -070040 * Some input devices present multiple distinguishable sources of input.
Jeff Brownc5ed5912010-07-14 18:48:53 -070041 * Applications can query the framework about the characteristics of each distinct source.
42 * </p><p>
43 * As a further wrinkle, different kinds of input sources uses different coordinate systems
44 * to describe motion events. Refer to the comments on the input source constants for
45 * the appropriate interpretation.
Jeff Brown6d0fec22010-07-23 21:28:06 -070046 * </p>
Jeff Brownc5ed5912010-07-14 18:48:53 -070047 */
Jeff Brown8d608662010-08-30 03:02:23 -070048public final class InputDevice implements Parcelable {
Jeff Brown9f25b7f2012-04-10 14:30:49 -070049 private final int mId;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070050 private final int mGeneration;
Michael Wrightac6c78b2013-07-17 13:21:45 -070051 private final int mControllerNumber;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070052 private final String mName;
Michael Wright54e56942013-08-12 16:39:59 -070053 private final int mVendorId;
54 private final int mProductId;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070055 private final String mDescriptor;
RoboErikca9eef62013-12-16 11:27:55 -080056 private final InputDeviceIdentifier mIdentifier;
Mathew Inwooda570dee2018-08-17 14:56:00 +010057 @UnsupportedAppUsage
Jeff Browndaa37532012-05-01 15:54:03 -070058 private final boolean mIsExternal;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070059 private final int mSources;
60 private final int mKeyboardType;
61 private final KeyCharacterMap mKeyCharacterMap;
Jeff Browna47425a2012-04-13 04:09:27 -070062 private final boolean mHasVibrator;
Tim Kilbourn72285e52015-06-05 15:52:05 -070063 private final boolean mHasMicrophone;
Michael Wright7ddd1102013-05-20 15:04:55 -070064 private final boolean mHasButtonUnderPad;
Jeff Brownefd32662011-03-08 15:13:06 -080065 private final ArrayList<MotionRange> mMotionRanges = new ArrayList<MotionRange>();
Jeff Brown91c69ab2011-02-14 17:03:18 -080066
Jeff Browna47425a2012-04-13 04:09:27 -070067 private Vibrator mVibrator; // guarded by mMotionRanges during initialization
68
Jeff Brownc5ed5912010-07-14 18:48:53 -070069 /**
70 * A mask for input source classes.
RoboErikca9eef62013-12-16 11:27:55 -080071 *
Jeff Brownc5ed5912010-07-14 18:48:53 -070072 * Each distinct input source constant has one or more input source class bits set to
73 * specify the desired interpretation for its input events.
74 */
75 public static final int SOURCE_CLASS_MASK = 0x000000ff;
Michael Wrighte7a9ae82013-03-08 15:19:19 -080076
77 /**
78 * The input source has no class.
79 *
80 * It is up to the application to determine how to handle the device based on the device type.
81 */
82 public static final int SOURCE_CLASS_NONE = 0x00000000;
83
Jeff Brownc5ed5912010-07-14 18:48:53 -070084 /**
85 * The input source has buttons or keys.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070086 * Examples: {@link #SOURCE_KEYBOARD}, {@link #SOURCE_DPAD}.
RoboErikca9eef62013-12-16 11:27:55 -080087 *
Jeff Brownc5ed5912010-07-14 18:48:53 -070088 * A {@link KeyEvent} should be interpreted as a button or key press.
RoboErikca9eef62013-12-16 11:27:55 -080089 *
Jeff Brown6eb5ac92010-08-30 23:29:07 -070090 * Use {@link #getKeyCharacterMap} to query the device's button and key mappings.
Jeff Brownc5ed5912010-07-14 18:48:53 -070091 */
92 public static final int SOURCE_CLASS_BUTTON = 0x00000001;
RoboErikca9eef62013-12-16 11:27:55 -080093
Jeff Brownc5ed5912010-07-14 18:48:53 -070094 /**
95 * The input source is a pointing device associated with a display.
96 * Examples: {@link #SOURCE_TOUCHSCREEN}, {@link #SOURCE_MOUSE}.
RoboErikca9eef62013-12-16 11:27:55 -080097 *
Jeff Brownc5ed5912010-07-14 18:48:53 -070098 * A {@link MotionEvent} should be interpreted as absolute coordinates in
99 * display units according to the {@link View} hierarchy. Pointer down/up indicated when
100 * the finger touches the display or when the selection button is pressed/released.
RoboErikca9eef62013-12-16 11:27:55 -0800101 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700102 * Use {@link #getMotionRange} to query the range of the pointing device. Some devices permit
103 * touches outside the display area so the effective range may be somewhat smaller or larger
104 * than the actual display size.
105 */
106 public static final int SOURCE_CLASS_POINTER = 0x00000002;
RoboErikca9eef62013-12-16 11:27:55 -0800107
Jeff Brownc5ed5912010-07-14 18:48:53 -0700108 /**
109 * The input source is a trackball navigation device.
110 * Examples: {@link #SOURCE_TRACKBALL}.
RoboErikca9eef62013-12-16 11:27:55 -0800111 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700112 * A {@link MotionEvent} should be interpreted as relative movements in device-specific
113 * units used for navigation purposes. Pointer down/up indicates when the selection button
114 * is pressed/released.
RoboErikca9eef62013-12-16 11:27:55 -0800115 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700116 * Use {@link #getMotionRange} to query the range of motion.
117 */
118 public static final int SOURCE_CLASS_TRACKBALL = 0x00000004;
RoboErikca9eef62013-12-16 11:27:55 -0800119
Jeff Brownc5ed5912010-07-14 18:48:53 -0700120 /**
121 * The input source is an absolute positioning device not associated with a display
122 * (unlike {@link #SOURCE_CLASS_POINTER}).
RoboErikca9eef62013-12-16 11:27:55 -0800123 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700124 * A {@link MotionEvent} should be interpreted as absolute coordinates in
125 * device-specific surface units.
RoboErikca9eef62013-12-16 11:27:55 -0800126 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700127 * Use {@link #getMotionRange} to query the range of positions.
128 */
129 public static final int SOURCE_CLASS_POSITION = 0x00000008;
Jeff Browncb1404e2011-01-15 18:14:15 -0800130
131 /**
132 * The input source is a joystick.
133 *
134 * A {@link MotionEvent} should be interpreted as absolute joystick movements.
135 *
136 * Use {@link #getMotionRange} to query the range of positions.
137 */
138 public static final int SOURCE_CLASS_JOYSTICK = 0x00000010;
139
Jeff Brownc5ed5912010-07-14 18:48:53 -0700140 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700141 * The input source is unknown.
142 */
143 public static final int SOURCE_UNKNOWN = 0x00000000;
RoboErikca9eef62013-12-16 11:27:55 -0800144
Jeff Brownc5ed5912010-07-14 18:48:53 -0700145 /**
146 * The input source is a keyboard.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700147 *
148 * This source indicates pretty much anything that has buttons. Use
149 * {@link #getKeyboardType()} to determine whether the keyboard has alphabetic keys
150 * and can be used to enter text.
151 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700152 * @see #SOURCE_CLASS_BUTTON
153 */
154 public static final int SOURCE_KEYBOARD = 0x00000100 | SOURCE_CLASS_BUTTON;
RoboErikca9eef62013-12-16 11:27:55 -0800155
Jeff Brownc5ed5912010-07-14 18:48:53 -0700156 /**
157 * The input source is a DPad.
RoboErikca9eef62013-12-16 11:27:55 -0800158 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700159 * @see #SOURCE_CLASS_BUTTON
160 */
161 public static final int SOURCE_DPAD = 0x00000200 | SOURCE_CLASS_BUTTON;
Jeff Browncb1404e2011-01-15 18:14:15 -0800162
163 /**
164 * The input source is a game pad.
165 * (It may also be a {@link #SOURCE_JOYSTICK}).
166 *
167 * @see #SOURCE_CLASS_BUTTON
168 */
169 public static final int SOURCE_GAMEPAD = 0x00000400 | SOURCE_CLASS_BUTTON;
170
Jeff Brownc5ed5912010-07-14 18:48:53 -0700171 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700172 * The input source is a touch screen pointing device.
RoboErikca9eef62013-12-16 11:27:55 -0800173 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700174 * @see #SOURCE_CLASS_POINTER
175 */
176 public static final int SOURCE_TOUCHSCREEN = 0x00001000 | SOURCE_CLASS_POINTER;
RoboErikca9eef62013-12-16 11:27:55 -0800177
Jeff Brownc5ed5912010-07-14 18:48:53 -0700178 /**
179 * The input source is a mouse pointing device.
180 * This code is also used for other mouse-like pointing devices such as trackpads
181 * and trackpoints.
RoboErikca9eef62013-12-16 11:27:55 -0800182 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700183 * @see #SOURCE_CLASS_POINTER
184 */
185 public static final int SOURCE_MOUSE = 0x00002000 | SOURCE_CLASS_POINTER;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186
187 /**
188 * The input source is a stylus pointing device.
Jeff Brown00710e92012-04-19 15:18:26 -0700189 * <p>
190 * Note that this bit merely indicates that an input device is capable of obtaining
191 * input from a stylus. To determine whether a given touch event was produced
192 * by a stylus, examine the tool type returned by {@link MotionEvent#getToolType(int)}
193 * for each individual pointer.
194 * </p><p>
195 * A single touch event may multiple pointers with different tool types,
196 * such as an event that has one pointer with tool type
197 * {@link MotionEvent#TOOL_TYPE_FINGER} and another pointer with tool type
198 * {@link MotionEvent#TOOL_TYPE_STYLUS}. So it is important to examine
199 * the tool type of each pointer, regardless of the source reported
200 * by {@link MotionEvent#getSource()}.
201 * </p>
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700202 *
203 * @see #SOURCE_CLASS_POINTER
204 */
205 public static final int SOURCE_STYLUS = 0x00004000 | SOURCE_CLASS_POINTER;
206
Jeff Brownc5ed5912010-07-14 18:48:53 -0700207 /**
Michael Wrightf18920b2015-06-12 15:24:38 +0100208 * The input device is a Bluetooth stylus.
209 * <p>
210 * Note that this bit merely indicates that an input device is capable of
211 * obtaining input from a Bluetooth stylus. To determine whether a given
212 * touch event was produced by a stylus, examine the tool type returned by
213 * {@link MotionEvent#getToolType(int)} for each individual pointer.
214 * </p><p>
215 * A single touch event may multiple pointers with different tool types,
216 * such as an event that has one pointer with tool type
217 * {@link MotionEvent#TOOL_TYPE_FINGER} and another pointer with tool type
218 * {@link MotionEvent#TOOL_TYPE_STYLUS}. So it is important to examine
219 * the tool type of each pointer, regardless of the source reported
220 * by {@link MotionEvent#getSource()}.
221 * </p><p>
222 * A bluetooth stylus generally receives its pressure and button state
223 * information from the stylus itself, and derives the rest from another
224 * source. For example, a Bluetooth stylus used in conjunction with a
225 * touchscreen would derive its contact position and pointer size from the
226 * touchscreen and may not be any more accurate than other tools such as
227 * fingers.
228 * </p>
229 *
230 * @see #SOURCE_STYLUS
231 * @see #SOURCE_CLASS_POINTER
232 */
233 public static final int SOURCE_BLUETOOTH_STYLUS =
234 0x00008000 | SOURCE_STYLUS;
235
236 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700237 * The input source is a trackball.
RoboErikca9eef62013-12-16 11:27:55 -0800238 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700239 * @see #SOURCE_CLASS_TRACKBALL
240 */
241 public static final int SOURCE_TRACKBALL = 0x00010000 | SOURCE_CLASS_TRACKBALL;
RoboErikca9eef62013-12-16 11:27:55 -0800242
Jeff Brownc5ed5912010-07-14 18:48:53 -0700243 /**
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800244 * The input source is a mouse device whose relative motions should be interpreted as
245 * navigation events.
246 *
247 * @see #SOURCE_CLASS_TRACKBALL
248 */
249 public static final int SOURCE_MOUSE_RELATIVE = 0x00020000 | SOURCE_CLASS_TRACKBALL;
250
251 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700252 * The input source is a touch pad or digitizer tablet that is not
Jeff Browne33348b2010-07-15 23:54:05 -0700253 * associated with a display (unlike {@link #SOURCE_TOUCHSCREEN}).
RoboErikca9eef62013-12-16 11:27:55 -0800254 *
Jeff Brownc5ed5912010-07-14 18:48:53 -0700255 * @see #SOURCE_CLASS_POSITION
256 */
257 public static final int SOURCE_TOUCHPAD = 0x00100000 | SOURCE_CLASS_POSITION;
Jeff Browncb1404e2011-01-15 18:14:15 -0800258
259 /**
Michael Wrighte7a9ae82013-03-08 15:19:19 -0800260 * The input source is a touch device whose motions should be interpreted as navigation events.
261 *
262 * For example, an upward swipe should be as an upward focus traversal in the same manner as
263 * pressing up on a D-Pad would be. Swipes to the left, right and down should be treated in a
264 * similar manner.
265 *
266 * @see #SOURCE_CLASS_NONE
267 */
268 public static final int SOURCE_TOUCH_NAVIGATION = 0x00200000 | SOURCE_CLASS_NONE;
269
270 /**
Prashant Malani67322b12015-08-25 17:41:34 -0700271 * The input source is a rotating encoder device whose motions should be interpreted as akin to
272 * those of a scroll wheel.
273 *
274 * @see #SOURCE_CLASS_NONE
Prashant Malani67322b12015-08-25 17:41:34 -0700275 */
276 public static final int SOURCE_ROTARY_ENCODER = 0x00400000 | SOURCE_CLASS_NONE;
277
278 /**
Jeff Browncb1404e2011-01-15 18:14:15 -0800279 * The input source is a joystick.
280 * (It may also be a {@link #SOURCE_GAMEPAD}).
281 *
282 * @see #SOURCE_CLASS_JOYSTICK
283 */
284 public static final int SOURCE_JOYSTICK = 0x01000000 | SOURCE_CLASS_JOYSTICK;
285
Jeff Brown6d0fec22010-07-23 21:28:06 -0700286 /**
Jinsuk Kim96658f72014-05-14 15:33:43 +0900287 * The input source is a device connected through HDMI-based bus.
288 *
289 * The key comes in through HDMI-CEC or MHL signal line, and is treated as if it were
290 * generated by a locally connected DPAD or keyboard.
291 */
292 public static final int SOURCE_HDMI = 0x02000000 | SOURCE_CLASS_BUTTON;
293
294 /**
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295 * A special input source constant that is used when filtering input devices
296 * to match devices that provide any type of input source.
297 */
298 public static final int SOURCE_ANY = 0xffffff00;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700299
Jeff Browne33348b2010-07-15 23:54:05 -0700300 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800301 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_X}.
RoboErikca9eef62013-12-16 11:27:55 -0800302 *
Jeff Browne33348b2010-07-15 23:54:05 -0700303 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800304 * @deprecated Use {@link MotionEvent#AXIS_X} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700305 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800306 @Deprecated
307 public static final int MOTION_RANGE_X = MotionEvent.AXIS_X;
308
Jeff Browne33348b2010-07-15 23:54:05 -0700309 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800310 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_Y}.
RoboErikca9eef62013-12-16 11:27:55 -0800311 *
Jeff Browne33348b2010-07-15 23:54:05 -0700312 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800313 * @deprecated Use {@link MotionEvent#AXIS_Y} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700314 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800315 @Deprecated
316 public static final int MOTION_RANGE_Y = MotionEvent.AXIS_Y;
317
Jeff Browne33348b2010-07-15 23:54:05 -0700318 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800319 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_PRESSURE}.
RoboErikca9eef62013-12-16 11:27:55 -0800320 *
Jeff Browne33348b2010-07-15 23:54:05 -0700321 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800322 * @deprecated Use {@link MotionEvent#AXIS_PRESSURE} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700323 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800324 @Deprecated
325 public static final int MOTION_RANGE_PRESSURE = MotionEvent.AXIS_PRESSURE;
326
Jeff Browne33348b2010-07-15 23:54:05 -0700327 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800328 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_SIZE}.
RoboErikca9eef62013-12-16 11:27:55 -0800329 *
Jeff Browne33348b2010-07-15 23:54:05 -0700330 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800331 * @deprecated Use {@link MotionEvent#AXIS_SIZE} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700332 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800333 @Deprecated
334 public static final int MOTION_RANGE_SIZE = MotionEvent.AXIS_SIZE;
335
Jeff Browne33348b2010-07-15 23:54:05 -0700336 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800337 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MAJOR}.
RoboErikca9eef62013-12-16 11:27:55 -0800338 *
Jeff Browne33348b2010-07-15 23:54:05 -0700339 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800340 * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MAJOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700341 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800342 @Deprecated
343 public static final int MOTION_RANGE_TOUCH_MAJOR = MotionEvent.AXIS_TOUCH_MAJOR;
344
Jeff Browne33348b2010-07-15 23:54:05 -0700345 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800346 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MINOR}.
RoboErikca9eef62013-12-16 11:27:55 -0800347 *
Jeff Browne33348b2010-07-15 23:54:05 -0700348 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800349 * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MINOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700350 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800351 @Deprecated
352 public static final int MOTION_RANGE_TOUCH_MINOR = MotionEvent.AXIS_TOUCH_MINOR;
353
Jeff Browne33348b2010-07-15 23:54:05 -0700354 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800355 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MAJOR}.
RoboErikca9eef62013-12-16 11:27:55 -0800356 *
Jeff Browne33348b2010-07-15 23:54:05 -0700357 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800358 * @deprecated Use {@link MotionEvent#AXIS_TOOL_MAJOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700359 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800360 @Deprecated
361 public static final int MOTION_RANGE_TOOL_MAJOR = MotionEvent.AXIS_TOOL_MAJOR;
362
Jeff Browne33348b2010-07-15 23:54:05 -0700363 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800364 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MINOR}.
RoboErikca9eef62013-12-16 11:27:55 -0800365 *
Jeff Browne33348b2010-07-15 23:54:05 -0700366 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800367 * @deprecated Use {@link MotionEvent#AXIS_TOOL_MINOR} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700368 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800369 @Deprecated
370 public static final int MOTION_RANGE_TOOL_MINOR = MotionEvent.AXIS_TOOL_MINOR;
371
Jeff Browne33348b2010-07-15 23:54:05 -0700372 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800373 * Constant for retrieving the range of values for {@link MotionEvent#AXIS_ORIENTATION}.
RoboErikca9eef62013-12-16 11:27:55 -0800374 *
Jeff Browne33348b2010-07-15 23:54:05 -0700375 * @see #getMotionRange
Jeff Brown91c69ab2011-02-14 17:03:18 -0800376 * @deprecated Use {@link MotionEvent#AXIS_ORIENTATION} instead.
Jeff Browne33348b2010-07-15 23:54:05 -0700377 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800378 @Deprecated
379 public static final int MOTION_RANGE_ORIENTATION = MotionEvent.AXIS_ORIENTATION;
RoboErikca9eef62013-12-16 11:27:55 -0800380
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 /**
382 * There is no keyboard.
383 */
384 public static final int KEYBOARD_TYPE_NONE = 0;
RoboErikca9eef62013-12-16 11:27:55 -0800385
Jeff Brown6d0fec22010-07-23 21:28:06 -0700386 /**
387 * The keyboard is not fully alphabetic. It may be a numeric keypad or an assortment
388 * of buttons that are not mapped as alphabetic keys suitable for text input.
389 */
390 public static final int KEYBOARD_TYPE_NON_ALPHABETIC = 1;
RoboErikca9eef62013-12-16 11:27:55 -0800391
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 /**
393 * The keyboard supports a complement of alphabetic keys.
394 */
395 public static final int KEYBOARD_TYPE_ALPHABETIC = 2;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800396
Kristian Monsen1f9dc882016-03-25 16:42:15 -0700397 private static final int MAX_RANGES = 1000;
398
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700399 public static final Parcelable.Creator<InputDevice> CREATOR =
400 new Parcelable.Creator<InputDevice>() {
401 public InputDevice createFromParcel(Parcel in) {
402 return new InputDevice(in);
403 }
404 public InputDevice[] newArray(int size) {
405 return new InputDevice[size];
406 }
407 };
408
Jeff Brown8d608662010-08-30 03:02:23 -0700409 // Called by native code.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100410 @UnsupportedAppUsage
Michael Wright54e56942013-08-12 16:39:59 -0700411 private InputDevice(int id, int generation, int controllerNumber, String name, int vendorId,
Tim Kilbourn6d85cf22015-04-08 10:23:35 -0700412 int productId, String descriptor, boolean isExternal, int sources, int keyboardType,
Tim Kilbourn72285e52015-06-05 15:52:05 -0700413 KeyCharacterMap keyCharacterMap, boolean hasVibrator, boolean hasMicrophone,
Tim Kilbourn415b4502015-03-19 16:02:02 -0700414 boolean hasButtonUnderPad) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700415 mId = id;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700416 mGeneration = generation;
Michael Wrightac6c78b2013-07-17 13:21:45 -0700417 mControllerNumber = controllerNumber;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700418 mName = name;
Michael Wright54e56942013-08-12 16:39:59 -0700419 mVendorId = vendorId;
420 mProductId = productId;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700421 mDescriptor = descriptor;
Jeff Browndaa37532012-05-01 15:54:03 -0700422 mIsExternal = isExternal;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700423 mSources = sources;
424 mKeyboardType = keyboardType;
425 mKeyCharacterMap = keyCharacterMap;
Jeff Browna47425a2012-04-13 04:09:27 -0700426 mHasVibrator = hasVibrator;
Tim Kilbourn72285e52015-06-05 15:52:05 -0700427 mHasMicrophone = hasMicrophone;
Michael Wright7ddd1102013-05-20 15:04:55 -0700428 mHasButtonUnderPad = hasButtonUnderPad;
RoboErikca9eef62013-12-16 11:27:55 -0800429 mIdentifier = new InputDeviceIdentifier(descriptor, vendorId, productId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700430 }
431
432 private InputDevice(Parcel in) {
433 mId = in.readInt();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700434 mGeneration = in.readInt();
Michael Wrightac6c78b2013-07-17 13:21:45 -0700435 mControllerNumber = in.readInt();
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700436 mName = in.readString();
Michael Wright54e56942013-08-12 16:39:59 -0700437 mVendorId = in.readInt();
438 mProductId = in.readInt();
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700439 mDescriptor = in.readString();
Jeff Browndaa37532012-05-01 15:54:03 -0700440 mIsExternal = in.readInt() != 0;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700441 mSources = in.readInt();
442 mKeyboardType = in.readInt();
443 mKeyCharacterMap = KeyCharacterMap.CREATOR.createFromParcel(in);
Jeff Browna47425a2012-04-13 04:09:27 -0700444 mHasVibrator = in.readInt() != 0;
Tim Kilbourn72285e52015-06-05 15:52:05 -0700445 mHasMicrophone = in.readInt() != 0;
Michael Wright7ddd1102013-05-20 15:04:55 -0700446 mHasButtonUnderPad = in.readInt() != 0;
RoboErikca9eef62013-12-16 11:27:55 -0800447 mIdentifier = new InputDeviceIdentifier(mDescriptor, mVendorId, mProductId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700448
Kristian Monsen1f9dc882016-03-25 16:42:15 -0700449 int numRanges = in.readInt();
450 if (numRanges > MAX_RANGES) {
451 numRanges = MAX_RANGES;
452 }
453
454 for (int i = 0; i < numRanges; i++) {
455 addMotionRange(in.readInt(), in.readInt(), in.readFloat(), in.readFloat(),
456 in.readFloat(), in.readFloat(), in.readFloat());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700457 }
Jeff Brown8d608662010-08-30 03:02:23 -0700458 }
Jeff Browne33348b2010-07-15 23:54:05 -0700459
460 /**
461 * Gets information about the input device with the specified id.
462 * @param id The device id.
463 * @return The input device or null if not found.
464 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700465 public static InputDevice getDevice(int id) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700466 return InputManager.getInstance().getInputDevice(id);
Jeff Brown8d608662010-08-30 03:02:23 -0700467 }
RoboErikca9eef62013-12-16 11:27:55 -0800468
Jeff Brown8d608662010-08-30 03:02:23 -0700469 /**
470 * Gets the ids of all input devices in the system.
471 * @return The input device ids.
472 */
473 public static int[] getDeviceIds() {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700474 return InputManager.getInstance().getInputDeviceIds();
Jeff Brown8d608662010-08-30 03:02:23 -0700475 }
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700476
Jeff Brown8d608662010-08-30 03:02:23 -0700477 /**
478 * Gets the input device id.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700479 * <p>
480 * Each input device receives a unique id when it is first configured
481 * by the system. The input device id may change when the system is restarted or if the
482 * input device is disconnected, reconnected or reconfigured at any time.
483 * If you require a stable identifier for a device that persists across
484 * boots and reconfigurations, use {@link #getDescriptor()}.
485 * </p>
486 *
Jeff Brown8d608662010-08-30 03:02:23 -0700487 * @return The input device id.
488 */
489 public int getId() {
490 return mId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700491 }
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700492
493 /**
Michael Wrightac6c78b2013-07-17 13:21:45 -0700494 * The controller number for a given input device.
495 * <p>
Michael Wright10fac452013-09-03 12:37:12 -0700496 * Each gamepad or joystick is given a unique, positive controller number when initially
497 * configured by the system. This number may change due to events such as device disconnects /
498 * reconnects or user initiated reassignment. Any change in number will trigger an event that
499 * can be observed by registering an {@link InputManager.InputDeviceListener}.
500 * </p>
501 * <p>
502 * All input devices which are not gamepads or joysticks will be assigned a controller number
Michael Wrightac6c78b2013-07-17 13:21:45 -0700503 * of 0.
504 * </p>
Michael Wright10fac452013-09-03 12:37:12 -0700505 *
506 * @return The controller number of the device.
Michael Wrightac6c78b2013-07-17 13:21:45 -0700507 */
508 public int getControllerNumber() {
509 return mControllerNumber;
510 }
511
512 /**
RoboErikca9eef62013-12-16 11:27:55 -0800513 * The set of identifying information for type of input device. This
514 * information can be used by the system to configure appropriate settings
515 * for the device.
516 *
517 * @return The identifier object for this device
518 * @hide
519 */
520 public InputDeviceIdentifier getIdentifier() {
521 return mIdentifier;
522 }
523
524 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700525 * Gets a generation number for this input device.
526 * The generation number is incremented whenever the device is reconfigured and its
527 * properties may have changed.
528 *
529 * @return The generation number.
530 *
531 * @hide
532 */
533 public int getGeneration() {
534 return mGeneration;
535 }
536
537 /**
Michael Wright54e56942013-08-12 16:39:59 -0700538 * Gets the vendor id for the given device, if available.
539 * <p>
540 * A vendor id uniquely identifies the company who manufactured the device. A value of 0 will
541 * be assigned where a vendor id is not available.
542 * </p>
543 *
544 * @return The vendor id of a given device
545 */
546 public int getVendorId() {
547 return mVendorId;
548 }
549
550 /**
551 * Gets the product id for the given device, if available.
552 * <p>
553 * A product id uniquely identifies which product within the address space of a given vendor,
554 * identified by the device's vendor id. A value of 0 will be assigned where a product id is
555 * not available.
556 * </p>
557 *
558 * @return The product id of a given device
559 */
560 public int getProductId() {
561 return mProductId;
562 }
563
564 /**
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700565 * Gets the input device descriptor, which is a stable identifier for an input device.
566 * <p>
567 * An input device descriptor uniquely identifies an input device. Its value
568 * is intended to be persistent across system restarts, and should not change even
569 * if the input device is disconnected, reconnected or reconfigured at any time.
Jeff Browne38fdfa2012-04-06 14:51:01 -0700570 * </p><p>
571 * It is possible for there to be multiple {@link InputDevice} instances that have the
572 * same input device descriptor. This might happen in situations where a single
573 * human input device registers multiple {@link InputDevice} instances (HID collections)
574 * that describe separate features of the device, such as a keyboard that also
575 * has a trackpad. Alternately, it may be that the input devices are simply
576 * indistinguishable, such as two keyboards made by the same manufacturer.
577 * </p><p>
Jeff Browndaa37532012-05-01 15:54:03 -0700578 * The input device descriptor returned by {@link #getDescriptor} should only be
Jeff Browne38fdfa2012-04-06 14:51:01 -0700579 * used when an application needs to remember settings associated with a particular
580 * input device. For all other purposes when referring to a logical
581 * {@link InputDevice} instance at runtime use the id returned by {@link #getId()}.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700582 * </p>
583 *
584 * @return The input device descriptor.
585 */
586 public String getDescriptor() {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700587 return mDescriptor;
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700588 }
589
Jeff Brownc5ed5912010-07-14 18:48:53 -0700590 /**
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700591 * Returns true if the device is a virtual input device rather than a real one,
592 * such as the virtual keyboard (see {@link KeyCharacterMap#VIRTUAL_KEYBOARD}).
593 * <p>
594 * Virtual input devices are provided to implement system-level functionality
595 * and should not be seen or configured by users.
596 * </p>
597 *
598 * @return True if the device is virtual.
599 *
600 * @see KeyCharacterMap#VIRTUAL_KEYBOARD
601 */
602 public boolean isVirtual() {
603 return mId < 0;
604 }
605
606 /**
Jeff Browndaa37532012-05-01 15:54:03 -0700607 * Returns true if the device is external (connected to USB or Bluetooth or some other
608 * peripheral bus), otherwise it is built-in.
609 *
610 * @return True if the device is external.
611 *
612 * @hide
613 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100614 @UnsupportedAppUsage
Jeff Browndaa37532012-05-01 15:54:03 -0700615 public boolean isExternal() {
616 return mIsExternal;
617 }
618
619 /**
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700620 * Returns true if the device is a full keyboard.
621 *
622 * @return True if the device is a full keyboard.
623 *
624 * @hide
625 */
626 public boolean isFullKeyboard() {
627 return (mSources & SOURCE_KEYBOARD) == SOURCE_KEYBOARD
628 && mKeyboardType == KEYBOARD_TYPE_ALPHABETIC;
629 }
630
631 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700632 * Gets the name of this input device.
633 * @return The input device name.
634 */
635 public String getName() {
636 return mName;
637 }
RoboErikca9eef62013-12-16 11:27:55 -0800638
Jeff Brownc5ed5912010-07-14 18:48:53 -0700639 /**
640 * Gets the input sources supported by this input device as a combined bitfield.
641 * @return The supported input sources.
642 */
643 public int getSources() {
644 return mSources;
645 }
RoboErikca9eef62013-12-16 11:27:55 -0800646
Jeff Brownc5ed5912010-07-14 18:48:53 -0700647 /**
Michael Wrightd08c8642014-03-28 12:59:34 -0700648 * Determines whether the input device supports the given source or sources.
649 *
650 * @param source The input source or sources to check against. This can be a generic device
651 * type such as {@link InputDevice#SOURCE_MOUSE}, a more generic device class, such as
652 * {@link InputDevice#SOURCE_CLASS_POINTER}, or a combination of sources bitwise ORed together.
653 * @return Whether the device can produce all of the given sources.
654 */
655 public boolean supportsSource(int source) {
656 return (mSources & source) == source;
657 }
658
659 /**
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660 * Gets the keyboard type.
661 * @return The keyboard type.
662 */
663 public int getKeyboardType() {
664 return mKeyboardType;
665 }
RoboErikca9eef62013-12-16 11:27:55 -0800666
Jeff Brown6d0fec22010-07-23 21:28:06 -0700667 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -0700668 * Gets the key character map associated with this input device.
669 * @return The key character map.
670 */
671 public KeyCharacterMap getKeyCharacterMap() {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700672 return mKeyCharacterMap;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800673 }
674
Jeff Browne33348b2010-07-15 23:54:05 -0700675 /**
Michael Wrightb7b2d4b2013-08-19 15:55:38 -0700676 * Gets whether the device is capable of producing the list of keycodes.
677 * @param keys The list of android keycodes to check for.
678 * @return An array of booleans where each member specifies whether the device is capable of
679 * generating the keycode given by the corresponding value at the same index in the keys array.
680 */
681 public boolean[] hasKeys(int... keys) {
682 return InputManager.getInstance().deviceHasKeys(mId, keys);
683 }
684
685 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800686 * Gets information about the range of values for a particular {@link MotionEvent} axis.
Jeff Brownefd32662011-03-08 15:13:06 -0800687 * If the device supports multiple sources, the same axis may have different meanings
688 * for each source. Returns information about the first axis found for any source.
689 * To obtain information about the axis for a specific source, use
690 * {@link #getMotionRange(int, int)}.
691 *
Jeff Brown91c69ab2011-02-14 17:03:18 -0800692 * @param axis The axis constant.
693 * @return The range of values, or null if the requested axis is not
Jeff Browne33348b2010-07-15 23:54:05 -0700694 * supported by the device.
Jeff Brown91c69ab2011-02-14 17:03:18 -0800695 *
696 * @see MotionEvent#AXIS_X
697 * @see MotionEvent#AXIS_Y
Jeff Browne33348b2010-07-15 23:54:05 -0700698 */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800699 public MotionRange getMotionRange(int axis) {
Jeff Brownefd32662011-03-08 15:13:06 -0800700 final int numRanges = mMotionRanges.size();
701 for (int i = 0; i < numRanges; i++) {
702 final MotionRange range = mMotionRanges.get(i);
703 if (range.mAxis == axis) {
704 return range;
705 }
706 }
707 return null;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700708 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800709
Jeff Brown6f2fba42011-02-19 01:08:02 -0800710 /**
Jeff Brownefd32662011-03-08 15:13:06 -0800711 * Gets information about the range of values for a particular {@link MotionEvent} axis
712 * used by a particular source on the device.
713 * If the device supports multiple sources, the same axis may have different meanings
714 * for each source.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800715 *
Jeff Brownefd32662011-03-08 15:13:06 -0800716 * @param axis The axis constant.
717 * @param source The source for which to return information.
718 * @return The range of values, or null if the requested axis is not
719 * supported by the device.
720 *
721 * @see MotionEvent#AXIS_X
722 * @see MotionEvent#AXIS_Y
Jeff Brown6f2fba42011-02-19 01:08:02 -0800723 */
Jeff Brownefd32662011-03-08 15:13:06 -0800724 public MotionRange getMotionRange(int axis, int source) {
725 final int numRanges = mMotionRanges.size();
726 for (int i = 0; i < numRanges; i++) {
727 final MotionRange range = mMotionRanges.get(i);
728 if (range.mAxis == axis && range.mSource == source) {
729 return range;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800730 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800731 }
Jeff Brownefd32662011-03-08 15:13:06 -0800732 return null;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800733 }
734
Jeff Brownefd32662011-03-08 15:13:06 -0800735 /**
736 * Gets the ranges for all axes supported by the device.
737 * @return The motion ranges for the device.
738 *
739 * @see #getMotionRange(int, int)
740 */
741 public List<MotionRange> getMotionRanges() {
742 return mMotionRanges;
743 }
744
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700745 // Called from native code.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100746 @UnsupportedAppUsage
Jeff Brownefd32662011-03-08 15:13:06 -0800747 private void addMotionRange(int axis, int source,
Michael Wrightc6091c62013-04-01 20:56:04 -0700748 float min, float max, float flat, float fuzz, float resolution) {
749 mMotionRanges.add(new MotionRange(axis, source, min, max, flat, fuzz, resolution));
Jeff Brownc5ed5912010-07-14 18:48:53 -0700750 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800751
Jeff Browne33348b2010-07-15 23:54:05 -0700752 /**
Jeff Browna47425a2012-04-13 04:09:27 -0700753 * Gets the vibrator service associated with the device, if there is one.
754 * Even if the device does not have a vibrator, the result is never null.
755 * Use {@link Vibrator#hasVibrator} to determine whether a vibrator is
756 * present.
757 *
758 * Note that the vibrator associated with the device may be different from
759 * the system vibrator. To obtain an instance of the system vibrator instead, call
760 * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
761 *
762 * @return The vibrator service associated with the device, never null.
763 */
764 public Vibrator getVibrator() {
765 synchronized (mMotionRanges) {
766 if (mVibrator == null) {
767 if (mHasVibrator) {
768 mVibrator = InputManager.getInstance().getInputDeviceVibrator(mId);
769 } else {
770 mVibrator = NullVibrator.getInstance();
771 }
772 }
773 return mVibrator;
774 }
775 }
776
777 /**
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700778 * Returns true if input device is enabled.
779 * @return Whether the input device is enabled.
780 */
781 public boolean isEnabled() {
782 return InputManager.getInstance().isInputDeviceEnabled(mId);
783 }
784
785 /**
786 * Enables the input device.
787 *
788 * @hide
789 */
790 @RequiresPermission(android.Manifest.permission.DISABLE_INPUT_DEVICE)
791 @TestApi
792 public void enable() {
793 InputManager.getInstance().enableInputDevice(mId);
794 }
795
796 /**
797 * Disables the input device.
798 *
799 * @hide
800 */
801 @RequiresPermission(android.Manifest.permission.DISABLE_INPUT_DEVICE)
802 @TestApi
803 public void disable() {
804 InputManager.getInstance().disableInputDevice(mId);
805 }
806
807 /**
Tim Kilbourn6d85cf22015-04-08 10:23:35 -0700808 * Reports whether the device has a built-in microphone.
809 * @return Whether the device has a built-in microphone.
810 */
Tim Kilbourn72285e52015-06-05 15:52:05 -0700811 public boolean hasMicrophone() {
812 return mHasMicrophone;
Tim Kilbourn6d85cf22015-04-08 10:23:35 -0700813 }
814
815 /**
Michael Wright7ddd1102013-05-20 15:04:55 -0700816 * Reports whether the device has a button under its touchpad
817 * @return Whether the device has a button under its touchpad
818 * @hide
819 */
820 public boolean hasButtonUnderPad() {
821 return mHasButtonUnderPad;
822 }
823
824 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100825 * Sets the current pointer type.
826 * @param pointerType the type of the pointer icon.
Jun Mukai1db53972015-09-11 18:08:31 -0700827 * @hide
828 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100829 public void setPointerType(int pointerType) {
830 InputManager.getInstance().setPointerIconType(pointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700831 }
832
833 /**
Jun Mukaid4eaef72015-10-30 15:54:33 -0700834 * Specifies the current custom pointer.
835 * @param icon the icon data.
836 * @hide
837 */
838 public void setCustomPointerIcon(PointerIcon icon) {
839 InputManager.getInstance().setCustomPointerIcon(icon);
840 }
841
842 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800843 * Provides information about the range of values for a particular {@link MotionEvent} axis.
844 *
845 * @see InputDevice#getMotionRange(int)
Jeff Browne33348b2010-07-15 23:54:05 -0700846 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700847 public static final class MotionRange {
Jeff Brownefd32662011-03-08 15:13:06 -0800848 private int mAxis;
849 private int mSource;
Jeff Brown8d608662010-08-30 03:02:23 -0700850 private float mMin;
851 private float mMax;
852 private float mFlat;
853 private float mFuzz;
Michael Wrightc6091c62013-04-01 20:56:04 -0700854 private float mResolution;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800855
Michael Wrightc6091c62013-04-01 20:56:04 -0700856 private MotionRange(int axis, int source, float min, float max, float flat, float fuzz,
857 float resolution) {
Jeff Brownefd32662011-03-08 15:13:06 -0800858 mAxis = axis;
859 mSource = source;
Jeff Brown8d608662010-08-30 03:02:23 -0700860 mMin = min;
861 mMax = max;
862 mFlat = flat;
863 mFuzz = fuzz;
Michael Wrightc6091c62013-04-01 20:56:04 -0700864 mResolution = resolution;
Jeff Brown8d608662010-08-30 03:02:23 -0700865 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800866
Jeff Browne33348b2010-07-15 23:54:05 -0700867 /**
Jeff Brownefd32662011-03-08 15:13:06 -0800868 * Gets the axis id.
869 * @return The axis id.
870 */
871 public int getAxis() {
872 return mAxis;
873 }
874
875 /**
876 * Gets the source for which the axis is defined.
877 * @return The source.
878 */
879 public int getSource() {
880 return mSource;
881 }
882
Michael Wright74e41562013-03-08 14:58:14 -0800883
884 /**
885 * Determines whether the event is from the given source.
886 *
887 * @param source The input source to check against. This can be a specific device type,
888 * such as {@link InputDevice#SOURCE_TOUCH_NAVIGATION}, or a more generic device class,
889 * such as {@link InputDevice#SOURCE_CLASS_POINTER}.
890 * @return Whether the event is from the given source.
891 */
892 public boolean isFromSource(int source) {
893 return (getSource() & source) == source;
894 }
895
Jeff Brownefd32662011-03-08 15:13:06 -0800896 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800897 * Gets the inclusive minimum value for the axis.
898 * @return The inclusive minimum value.
Jeff Browne33348b2010-07-15 23:54:05 -0700899 */
900 public float getMin() {
Jeff Brown8d608662010-08-30 03:02:23 -0700901 return mMin;
Jeff Browne33348b2010-07-15 23:54:05 -0700902 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800903
Jeff Browne33348b2010-07-15 23:54:05 -0700904 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800905 * Gets the inclusive maximum value for the axis.
906 * @return The inclusive maximum value.
Jeff Browne33348b2010-07-15 23:54:05 -0700907 */
908 public float getMax() {
Jeff Brown8d608662010-08-30 03:02:23 -0700909 return mMax;
Jeff Browne33348b2010-07-15 23:54:05 -0700910 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800911
Jeff Browne33348b2010-07-15 23:54:05 -0700912 /**
Jeff Brown6f2fba42011-02-19 01:08:02 -0800913 * Gets the range of the axis (difference between maximum and minimum).
Jeff Browne33348b2010-07-15 23:54:05 -0700914 * @return The range of values.
915 */
916 public float getRange() {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800917 return mMax - mMin;
Jeff Browne33348b2010-07-15 23:54:05 -0700918 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800919
Jeff Browne33348b2010-07-15 23:54:05 -0700920 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800921 * Gets the extent of the center flat position with respect to this axis.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800922 * <p>
Jeff Browne33348b2010-07-15 23:54:05 -0700923 * For example, a flat value of 8 means that the center position is between -8 and +8.
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700924 * This value is mainly useful for calibrating self-centering devices.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800925 * </p>
Jeff Browne33348b2010-07-15 23:54:05 -0700926 * @return The extent of the center flat position.
927 */
928 public float getFlat() {
Jeff Brown8d608662010-08-30 03:02:23 -0700929 return mFlat;
Jeff Browne33348b2010-07-15 23:54:05 -0700930 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800931
Jeff Browne33348b2010-07-15 23:54:05 -0700932 /**
Jeff Brown91c69ab2011-02-14 17:03:18 -0800933 * Gets the error tolerance for input device measurements with respect to this axis.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800934 * <p>
Jeff Browne33348b2010-07-15 23:54:05 -0700935 * For example, a value of 2 indicates that the measured value may be up to +/- 2 units
936 * away from the actual value due to noise and device sensitivity limitations.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800937 * </p>
Jeff Browne33348b2010-07-15 23:54:05 -0700938 * @return The error tolerance.
939 */
940 public float getFuzz() {
Jeff Brown8d608662010-08-30 03:02:23 -0700941 return mFuzz;
942 }
Michael Wrightc6091c62013-04-01 20:56:04 -0700943
944 /**
945 * Gets the resolution for input device measurements with respect to this axis.
946 * @return The resolution in units per millimeter, or units per radian for rotational axes.
947 */
948 public float getResolution() {
949 return mResolution;
950 }
Jeff Brown8d608662010-08-30 03:02:23 -0700951 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800952
Jeff Brown8d608662010-08-30 03:02:23 -0700953 @Override
954 public void writeToParcel(Parcel out, int flags) {
955 out.writeInt(mId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700956 out.writeInt(mGeneration);
Michael Wrightac6c78b2013-07-17 13:21:45 -0700957 out.writeInt(mControllerNumber);
Jeff Brown8d608662010-08-30 03:02:23 -0700958 out.writeString(mName);
Michael Wright54e56942013-08-12 16:39:59 -0700959 out.writeInt(mVendorId);
960 out.writeInt(mProductId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700961 out.writeString(mDescriptor);
Jeff Browndaa37532012-05-01 15:54:03 -0700962 out.writeInt(mIsExternal ? 1 : 0);
Jeff Brown8d608662010-08-30 03:02:23 -0700963 out.writeInt(mSources);
964 out.writeInt(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700965 mKeyCharacterMap.writeToParcel(out, flags);
Jeff Browna47425a2012-04-13 04:09:27 -0700966 out.writeInt(mHasVibrator ? 1 : 0);
Tim Kilbourn72285e52015-06-05 15:52:05 -0700967 out.writeInt(mHasMicrophone ? 1 : 0);
Michael Wright7ddd1102013-05-20 15:04:55 -0700968 out.writeInt(mHasButtonUnderPad ? 1 : 0);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800969
Jeff Brownefd32662011-03-08 15:13:06 -0800970 final int numRanges = mMotionRanges.size();
Kristian Monsen1f9dc882016-03-25 16:42:15 -0700971 out.writeInt(numRanges);
Jeff Brownefd32662011-03-08 15:13:06 -0800972 for (int i = 0; i < numRanges; i++) {
973 MotionRange range = mMotionRanges.get(i);
974 out.writeInt(range.mAxis);
975 out.writeInt(range.mSource);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800976 out.writeFloat(range.mMin);
977 out.writeFloat(range.mMax);
978 out.writeFloat(range.mFlat);
979 out.writeFloat(range.mFuzz);
Michael Wrightc6091c62013-04-01 20:56:04 -0700980 out.writeFloat(range.mResolution);
Jeff Brown8d608662010-08-30 03:02:23 -0700981 }
Jeff Brown8d608662010-08-30 03:02:23 -0700982 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800983
Jeff Brown8d608662010-08-30 03:02:23 -0700984 @Override
985 public int describeContents() {
986 return 0;
987 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800988
Jeff Brown8d608662010-08-30 03:02:23 -0700989 @Override
990 public String toString() {
991 StringBuilder description = new StringBuilder();
992 description.append("Input Device ").append(mId).append(": ").append(mName).append("\n");
Jeff Browne38fdfa2012-04-06 14:51:01 -0700993 description.append(" Descriptor: ").append(mDescriptor).append("\n");
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700994 description.append(" Generation: ").append(mGeneration).append("\n");
Jeff Browndaa37532012-05-01 15:54:03 -0700995 description.append(" Location: ").append(mIsExternal ? "external" : "built-in").append("\n");
Jeff Browne38fdfa2012-04-06 14:51:01 -0700996
Jeff Brown8d608662010-08-30 03:02:23 -0700997 description.append(" Keyboard Type: ");
998 switch (mKeyboardType) {
999 case KEYBOARD_TYPE_NONE:
1000 description.append("none");
1001 break;
1002 case KEYBOARD_TYPE_NON_ALPHABETIC:
1003 description.append("non-alphabetic");
1004 break;
1005 case KEYBOARD_TYPE_ALPHABETIC:
1006 description.append("alphabetic");
1007 break;
1008 }
1009 description.append("\n");
Jeff Brown91c69ab2011-02-14 17:03:18 -08001010
Jeff Browna47425a2012-04-13 04:09:27 -07001011 description.append(" Has Vibrator: ").append(mHasVibrator).append("\n");
1012
Tim Kilbourn72285e52015-06-05 15:52:05 -07001013 description.append(" Has mic: ").append(mHasMicrophone).append("\n");
Tim Kilbourn6d85cf22015-04-08 10:23:35 -07001014
Jeff Brownefd32662011-03-08 15:13:06 -08001015 description.append(" Sources: 0x").append(Integer.toHexString(mSources)).append(" (");
Jeff Brown8d608662010-08-30 03:02:23 -07001016 appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard");
1017 appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad");
Jeff Brown8d608662010-08-30 03:02:23 -07001018 appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHSCREEN, "touchscreen");
1019 appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE, "mouse");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001020 appendSourceDescriptionIfApplicable(description, SOURCE_STYLUS, "stylus");
Jeff Brown8d608662010-08-30 03:02:23 -07001021 appendSourceDescriptionIfApplicable(description, SOURCE_TRACKBALL, "trackball");
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001022 appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE_RELATIVE, "mouse_relative");
Jeff Brown8d608662010-08-30 03:02:23 -07001023 appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHPAD, "touchpad");
Jeff Brown91c69ab2011-02-14 17:03:18 -08001024 appendSourceDescriptionIfApplicable(description, SOURCE_JOYSTICK, "joystick");
1025 appendSourceDescriptionIfApplicable(description, SOURCE_GAMEPAD, "gamepad");
1026 description.append(" )\n");
1027
1028 final int numAxes = mMotionRanges.size();
1029 for (int i = 0; i < numAxes; i++) {
Jeff Brownefd32662011-03-08 15:13:06 -08001030 MotionRange range = mMotionRanges.get(i);
1031 description.append(" ").append(MotionEvent.axisToString(range.mAxis));
1032 description.append(": source=0x").append(Integer.toHexString(range.mSource));
1033 description.append(" min=").append(range.mMin);
Jeff Brown91c69ab2011-02-14 17:03:18 -08001034 description.append(" max=").append(range.mMax);
1035 description.append(" flat=").append(range.mFlat);
1036 description.append(" fuzz=").append(range.mFuzz);
Michael Wrightc6091c62013-04-01 20:56:04 -07001037 description.append(" resolution=").append(range.mResolution);
Jeff Brown91c69ab2011-02-14 17:03:18 -08001038 description.append("\n");
1039 }
Jeff Brown8d608662010-08-30 03:02:23 -07001040 return description.toString();
1041 }
Jeff Brown91c69ab2011-02-14 17:03:18 -08001042
Jeff Brown8d608662010-08-30 03:02:23 -07001043 private void appendSourceDescriptionIfApplicable(StringBuilder description, int source,
1044 String sourceName) {
1045 if ((mSources & source) == source) {
1046 description.append(" ");
1047 description.append(sourceName);
1048 }
1049 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07001050}