1616
1717package android .view ;
1818
19+ import android .content .Context ;
1920import android .hardware .input .InputManager ;
2021import android .os .Parcel ;
2122import android .os .Parcelable ;
23+ import android .os .Vibrator ;
24+ import android .os .NullVibrator ;
2225
2326import java .util .ArrayList ;
2427import java .util .List ;
@@ -46,8 +49,11 @@ public final class InputDevice implements Parcelable {
4649 private final int mSources ;
4750 private final int mKeyboardType ;
4851 private final KeyCharacterMap mKeyCharacterMap ;
52+ private final boolean mHasVibrator ;
4953 private final ArrayList <MotionRange > mMotionRanges = new ArrayList <MotionRange >();
5054
55+ private Vibrator mVibrator ; // guarded by mMotionRanges during initialization
56+
5157 /**
5258 * A mask for input source classes.
5359 *
@@ -304,14 +310,15 @@ public InputDevice[] newArray(int size) {
304310
305311 // Called by native code.
306312 private InputDevice (int id , int generation , String name , String descriptor , int sources ,
307- int keyboardType , KeyCharacterMap keyCharacterMap ) {
313+ int keyboardType , KeyCharacterMap keyCharacterMap , boolean hasVibrator ) {
308314 mId = id ;
309315 mGeneration = generation ;
310316 mName = name ;
311317 mDescriptor = descriptor ;
312318 mSources = sources ;
313319 mKeyboardType = keyboardType ;
314320 mKeyCharacterMap = keyCharacterMap ;
321+ mHasVibrator = hasVibrator ;
315322 }
316323
317324 private InputDevice (Parcel in ) {
@@ -322,6 +329,7 @@ private InputDevice(Parcel in) {
322329 mSources = in .readInt ();
323330 mKeyboardType = in .readInt ();
324331 mKeyCharacterMap = KeyCharacterMap .CREATOR .createFromParcel (in );
332+ mHasVibrator = in .readInt () != 0 ;
325333
326334 for (;;) {
327335 int axis = in .readInt ();
@@ -521,6 +529,31 @@ private void addMotionRange(int axis, int source,
521529 mMotionRanges .add (new MotionRange (axis , source , min , max , flat , fuzz ));
522530 }
523531
532+ /**
533+ * Gets the vibrator service associated with the device, if there is one.
534+ * Even if the device does not have a vibrator, the result is never null.
535+ * Use {@link Vibrator#hasVibrator} to determine whether a vibrator is
536+ * present.
537+ *
538+ * Note that the vibrator associated with the device may be different from
539+ * the system vibrator. To obtain an instance of the system vibrator instead, call
540+ * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
541+ *
542+ * @return The vibrator service associated with the device, never null.
543+ */
544+ public Vibrator getVibrator () {
545+ synchronized (mMotionRanges ) {
546+ if (mVibrator == null ) {
547+ if (mHasVibrator ) {
548+ mVibrator = InputManager .getInstance ().getInputDeviceVibrator (mId );
549+ } else {
550+ mVibrator = NullVibrator .getInstance ();
551+ }
552+ }
553+ return mVibrator ;
554+ }
555+ }
556+
524557 /**
525558 * Provides information about the range of values for a particular {@link MotionEvent} axis.
526559 *
@@ -617,6 +650,7 @@ public void writeToParcel(Parcel out, int flags) {
617650 out .writeInt (mSources );
618651 out .writeInt (mKeyboardType );
619652 mKeyCharacterMap .writeToParcel (out , flags );
653+ out .writeInt (mHasVibrator ? 1 : 0 );
620654
621655 final int numRanges = mMotionRanges .size ();
622656 for (int i = 0 ; i < numRanges ; i ++) {
@@ -657,6 +691,8 @@ public String toString() {
657691 }
658692 description .append ("\n " );
659693
694+ description .append (" Has Vibrator: " ).append (mHasVibrator ).append ("\n " );
695+
660696 description .append (" Sources: 0x" ).append (Integer .toHexString (mSources )).append (" (" );
661697 appendSourceDescriptionIfApplicable (description , SOURCE_KEYBOARD , "keyboard" );
662698 appendSourceDescriptionIfApplicable (description , SOURCE_DPAD , "dpad" );
0 commit comments