diff options
| author | Jeff Brown <jeffbrown@google.com> | 2012-04-13 04:09:27 -0700 |
|---|---|---|
| committer | Jeff Brown <jeffbrown@google.com> | 2012-04-13 17:01:15 -0700 |
| commit | a47425a13c19f95057df78b8bb65bb25657e8753 (patch) | |
| tree | 675c0d6bf611f2427bb3d11315d410bf9087b20a /core/java/android/view/InputDevice.java | |
| parent | c2346134bb519a54d50655cbef940fc3fdec60a9 (diff) | |
Add support for input devices that have vibrators.
Added a getVibrator() method to InputDevice which returns a Vibrator
associated with that input device. Its uses the same API as the
system vibrator which makes it easy for applications to be modified
to use one or the other.
Bug: 6334179
Change-Id: Ifc7f13dbcb778670f3f1c07ccc562334e6109d2e
Diffstat (limited to 'core/java/android/view/InputDevice.java')
| -rwxr-xr-x | core/java/android/view/InputDevice.java | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index 4ebb679c0a01..4848a7accf72 100755 --- a/core/java/android/view/InputDevice.java +++ b/core/java/android/view/InputDevice.java @@ -16,9 +16,12 @@ package android.view; +import android.content.Context; import android.hardware.input.InputManager; import android.os.Parcel; import android.os.Parcelable; +import android.os.Vibrator; +import android.os.NullVibrator; import java.util.ArrayList; import java.util.List; @@ -46,8 +49,11 @@ public final class InputDevice implements Parcelable { private final int mSources; private final int mKeyboardType; private final KeyCharacterMap mKeyCharacterMap; + private final boolean mHasVibrator; private final ArrayList<MotionRange> mMotionRanges = new ArrayList<MotionRange>(); + private Vibrator mVibrator; // guarded by mMotionRanges during initialization + /** * A mask for input source classes. * @@ -304,7 +310,7 @@ public final class InputDevice implements Parcelable { // Called by native code. private InputDevice(int id, int generation, String name, String descriptor, int sources, - int keyboardType, KeyCharacterMap keyCharacterMap) { + int keyboardType, KeyCharacterMap keyCharacterMap, boolean hasVibrator) { mId = id; mGeneration = generation; mName = name; @@ -312,6 +318,7 @@ public final class InputDevice implements Parcelable { mSources = sources; mKeyboardType = keyboardType; mKeyCharacterMap = keyCharacterMap; + mHasVibrator = hasVibrator; } private InputDevice(Parcel in) { @@ -322,6 +329,7 @@ public final class InputDevice implements Parcelable { mSources = in.readInt(); mKeyboardType = in.readInt(); mKeyCharacterMap = KeyCharacterMap.CREATOR.createFromParcel(in); + mHasVibrator = in.readInt() != 0; for (;;) { int axis = in.readInt(); @@ -522,6 +530,31 @@ public final class InputDevice implements Parcelable { } /** + * Gets the vibrator service associated with the device, if there is one. + * Even if the device does not have a vibrator, the result is never null. + * Use {@link Vibrator#hasVibrator} to determine whether a vibrator is + * present. + * + * Note that the vibrator associated with the device may be different from + * the system vibrator. To obtain an instance of the system vibrator instead, call + * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument. + * + * @return The vibrator service associated with the device, never null. + */ + public Vibrator getVibrator() { + synchronized (mMotionRanges) { + if (mVibrator == null) { + if (mHasVibrator) { + mVibrator = InputManager.getInstance().getInputDeviceVibrator(mId); + } else { + mVibrator = NullVibrator.getInstance(); + } + } + return mVibrator; + } + } + + /** * Provides information about the range of values for a particular {@link MotionEvent} axis. * * @see InputDevice#getMotionRange(int) @@ -617,6 +650,7 @@ public final class InputDevice implements Parcelable { out.writeInt(mSources); out.writeInt(mKeyboardType); mKeyCharacterMap.writeToParcel(out, flags); + out.writeInt(mHasVibrator ? 1 : 0); final int numRanges = mMotionRanges.size(); for (int i = 0; i < numRanges; i++) { @@ -657,6 +691,8 @@ public final class InputDevice implements Parcelable { } description.append("\n"); + description.append(" Has Vibrator: ").append(mHasVibrator).append("\n"); + description.append(" Sources: 0x").append(Integer.toHexString(mSources)).append(" ("); appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard"); appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad"); |
