diff options
| author | Jeff Brown <jeffbrown@google.com> | 2011-02-14 17:03:18 -0800 |
|---|---|---|
| committer | Jeff Brown <jeffbrown@google.com> | 2011-02-15 19:14:37 -0800 |
| commit | 91c69ab01539f7ba28708f41ec1835cc2920d0a0 (patch) | |
| tree | 04f5dc37a0fa3f18a18ba08916efe0c33a1de300 /core/java/android/view/InputDevice.java | |
| parent | c3451d4a4479b6244bd6d1dadf289a8d44bdcca2 (diff) | |
Add support for arbitrary axes in MotionEvents.
This change makes it possible to extend the set of axes that
are reported in MotionEvents by defining new axis constants.
The MotionEvent object is now backed by its C++ counterpart
to avoid having to maintain multiple representations of the
same data.
Change-Id: Ibe93c90d4b390d43c176cce48d558d20869ee608
Diffstat (limited to 'core/java/android/view/InputDevice.java')
| -rwxr-xr-x | core/java/android/view/InputDevice.java | 230 |
1 files changed, 115 insertions, 115 deletions
diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index e799f7696c65..7abbce6269c5 100755 --- a/core/java/android/view/InputDevice.java +++ b/core/java/android/view/InputDevice.java @@ -20,6 +20,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; import android.os.ServiceManager; +import android.util.SparseArray; /** * Describes the capabilities of a particular input device. @@ -41,9 +42,9 @@ public final class InputDevice implements Parcelable { private String mName; private int mSources; private int mKeyboardType; - - private MotionRange[] mMotionRanges; - + + private final SparseArray<MotionRange> mMotionRanges = new SparseArray<MotionRange>(); + /** * A mask for input source classes. * @@ -181,70 +182,85 @@ public final class InputDevice implements Parcelable { public static final int SOURCE_ANY = 0xffffff00; /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#x}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_X}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_X} instead. */ - public static final int MOTION_RANGE_X = 0; - + @Deprecated + public static final int MOTION_RANGE_X = MotionEvent.AXIS_X; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#y}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_Y}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_Y} instead. */ - public static final int MOTION_RANGE_Y = 1; - + @Deprecated + public static final int MOTION_RANGE_Y = MotionEvent.AXIS_Y; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#pressure}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_PRESSURE}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_PRESSURE} instead. */ - public static final int MOTION_RANGE_PRESSURE = 2; - + @Deprecated + public static final int MOTION_RANGE_PRESSURE = MotionEvent.AXIS_PRESSURE; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#size}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_SIZE}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_SIZE} instead. */ - public static final int MOTION_RANGE_SIZE = 3; - + @Deprecated + public static final int MOTION_RANGE_SIZE = MotionEvent.AXIS_SIZE; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#touchMajor}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MAJOR}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MAJOR} instead. */ - public static final int MOTION_RANGE_TOUCH_MAJOR = 4; - + @Deprecated + public static final int MOTION_RANGE_TOUCH_MAJOR = MotionEvent.AXIS_TOUCH_MAJOR; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#touchMinor}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOUCH_MINOR}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MINOR} instead. */ - public static final int MOTION_RANGE_TOUCH_MINOR = 5; - + @Deprecated + public static final int MOTION_RANGE_TOUCH_MINOR = MotionEvent.AXIS_TOUCH_MINOR; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#toolMajor}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MAJOR}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_TOOL_MAJOR} instead. */ - public static final int MOTION_RANGE_TOOL_MAJOR = 6; - + @Deprecated + public static final int MOTION_RANGE_TOOL_MAJOR = MotionEvent.AXIS_TOOL_MAJOR; + /** - * Constant for retrieving the range of values for {@link MotionEvent.PointerCoords#toolMinor}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_TOOL_MINOR}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_TOOL_MINOR} instead. */ - public static final int MOTION_RANGE_TOOL_MINOR = 7; - + @Deprecated + public static final int MOTION_RANGE_TOOL_MINOR = MotionEvent.AXIS_TOOL_MINOR; + /** - * Constant for retrieving the range of values for - * {@link MotionEvent.PointerCoords#orientation}. + * Constant for retrieving the range of values for {@link MotionEvent#AXIS_ORIENTATION}. * * @see #getMotionRange + * @deprecated Use {@link MotionEvent#AXIS_ORIENTATION} instead. */ - public static final int MOTION_RANGE_ORIENTATION = 8; - - private static final int MOTION_RANGE_LAST = MOTION_RANGE_ORIENTATION; + @Deprecated + public static final int MOTION_RANGE_ORIENTATION = MotionEvent.AXIS_ORIENTATION; /** * There is no keyboard. @@ -261,10 +277,9 @@ public final class InputDevice implements Parcelable { * The keyboard supports a complement of alphabetic keys. */ public static final int KEYBOARD_TYPE_ALPHABETIC = 2; - + // Called by native code. private InputDevice() { - mMotionRanges = new MotionRange[MOTION_RANGE_LAST + 1]; } /** @@ -335,72 +350,69 @@ public final class InputDevice implements Parcelable { public KeyCharacterMap getKeyCharacterMap() { return KeyCharacterMap.load(mId); } - + /** - * Gets information about the range of values for a particular {@link MotionEvent} - * coordinate. - * @param rangeType The motion range constant. - * @return The range of values, or null if the requested coordinate is not + * Gets information about the range of values for a particular {@link MotionEvent} axis. + * @param axis The axis constant. + * @return The range of values, or null if the requested axis is not * supported by the device. + * + * @see MotionEvent#AXIS_X + * @see MotionEvent#AXIS_Y */ - public MotionRange getMotionRange(int rangeType) { - if (rangeType < 0 || rangeType > MOTION_RANGE_LAST) { - throw new IllegalArgumentException("Requested range is out of bounds."); - } - - return mMotionRanges[rangeType]; + public MotionRange getMotionRange(int axis) { + return mMotionRanges.get(axis); } - - private void addMotionRange(int rangeType, float min, float max, float flat, float fuzz) { - if (rangeType >= 0 && rangeType <= MOTION_RANGE_LAST) { - MotionRange range = new MotionRange(min, max, flat, fuzz); - mMotionRanges[rangeType] = range; - } + + // Called by native code. + private void addMotionRange(int axis, float min, float max, float flat, float fuzz) { + mMotionRanges.append(axis, new MotionRange(min, max, flat, fuzz)); } - + /** - * Provides information about the range of values for a particular {@link MotionEvent} - * coordinate. + * Provides information about the range of values for a particular {@link MotionEvent} axis. + * + * @see InputDevice#getMotionRange(int) */ public static final class MotionRange { private float mMin; private float mMax; private float mFlat; private float mFuzz; - + private MotionRange(float min, float max, float flat, float fuzz) { mMin = min; mMax = max; mFlat = flat; mFuzz = fuzz; } - + /** - * Gets the minimum value for the coordinate. - * @return The minimum value. + * Gets the minimum value for the axis. + * @return The (inclusive) minimum value. */ public float getMin() { return mMin; } - + /** - * Gets the maximum value for the coordinate. - * @return The minimum value. + * Gets the maximum value for the axis. + * @return The (inclusive) maximum value. */ public float getMax() { return mMax; } - + /** - * Gets the range of the coordinate (difference between maximum and minimum). + * Gets the range of the axis (difference between maximum and minimum plus one). * @return The range of values. */ public float getRange() { - return mMax - mMin; + return mMax - mMin + 1; } - + /** - * Gets the extent of the center flat position with respect to this coordinate. + * Gets the extent of the center flat position with respect to this axis. * For example, a flat value of 8 means that the center position is between -8 and +8. * This value is mainly useful for calibrating self-centering devices. * @return The extent of the center flat position. @@ -408,9 +420,9 @@ public final class InputDevice implements Parcelable { public float getFlat() { return mFlat; } - + /** - * Gets the error tolerance for input device measurements with respect to this coordinate. + * Gets the error tolerance for input device measurements with respect to this axis. * For example, a value of 2 indicates that the measured value may be up to +/- 2 units * away from the actual value due to noise and device sensitivity limitations. * @return The error tolerance. @@ -419,7 +431,7 @@ public final class InputDevice implements Parcelable { return mFuzz; } } - + public static final Parcelable.Creator<InputDevice> CREATOR = new Parcelable.Creator<InputDevice>() { public InputDevice createFromParcel(Parcel in) { @@ -438,15 +450,13 @@ public final class InputDevice implements Parcelable { mName = in.readString(); mSources = in.readInt(); mKeyboardType = in.readInt(); - + for (;;) { - int rangeType = in.readInt(); - if (rangeType < 0) { + int axis = in.readInt(); + if (axis < 0) { break; } - - addMotionRange(rangeType, - in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat()); + addMotionRange(axis, in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat()); } } @@ -456,25 +466,25 @@ public final class InputDevice implements Parcelable { out.writeString(mName); out.writeInt(mSources); out.writeInt(mKeyboardType); - - for (int i = 0; i <= MOTION_RANGE_LAST; i++) { - MotionRange range = mMotionRanges[i]; - if (range != null) { - out.writeInt(i); - out.writeFloat(range.mMin); - out.writeFloat(range.mMax); - out.writeFloat(range.mFlat); - out.writeFloat(range.mFuzz); - } + + final int numAxes = mMotionRanges.size(); + for (int i = 0; i < numAxes; i++) { + int axis = mMotionRanges.keyAt(i); + MotionRange range = mMotionRanges.valueAt(i); + out.writeInt(axis); + out.writeFloat(range.mMin); + out.writeFloat(range.mMax); + out.writeFloat(range.mFlat); + out.writeFloat(range.mFuzz); } out.writeInt(-1); } - + @Override public int describeContents() { return 0; } - + @Override public String toString() { StringBuilder description = new StringBuilder(); @@ -493,29 +503,32 @@ public final class InputDevice implements Parcelable { break; } description.append("\n"); - - description.append(" Sources:"); + + description.append(" Sources: ").append(Integer.toHexString(mSources)).append(" ("); appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard"); appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad"); appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHSCREEN, "touchscreen"); appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE, "mouse"); appendSourceDescriptionIfApplicable(description, SOURCE_TRACKBALL, "trackball"); appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHPAD, "touchpad"); - description.append("\n"); - - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_X, "x"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_Y, "y"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_PRESSURE, "pressure"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_SIZE, "size"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_TOUCH_MAJOR, "touchMajor"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_TOUCH_MINOR, "touchMinor"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_TOOL_MAJOR, "toolMajor"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_TOOL_MINOR, "toolMinor"); - appendRangeDescriptionIfApplicable(description, MOTION_RANGE_ORIENTATION, "orientation"); - + appendSourceDescriptionIfApplicable(description, SOURCE_JOYSTICK, "joystick"); + appendSourceDescriptionIfApplicable(description, SOURCE_GAMEPAD, "gamepad"); + description.append(" )\n"); + + final int numAxes = mMotionRanges.size(); + for (int i = 0; i < numAxes; i++) { + int axis = mMotionRanges.keyAt(i); + MotionRange range = mMotionRanges.valueAt(i); + description.append(" ").append(MotionEvent.axisToString(axis)); + description.append(": min=").append(range.mMin); + description.append(" max=").append(range.mMax); + description.append(" flat=").append(range.mFlat); + description.append(" fuzz=").append(range.mFuzz); + description.append("\n"); + } return description.toString(); } - + private void appendSourceDescriptionIfApplicable(StringBuilder description, int source, String sourceName) { if ((mSources & source) == source) { @@ -523,17 +536,4 @@ public final class InputDevice implements Parcelable { description.append(sourceName); } } - - private void appendRangeDescriptionIfApplicable(StringBuilder description, - int rangeType, String rangeName) { - MotionRange range = mMotionRanges[rangeType]; - if (range != null) { - description.append(" Range[").append(rangeName); - description.append("]: min=").append(range.mMin); - description.append(" max=").append(range.mMax); - description.append(" flat=").append(range.mFlat); - description.append(" fuzz=").append(range.mFuzz); - description.append("\n"); - } - } } |
