1616
1717package android .os ;
1818
19- import android .util . Log ;
19+ import android .content . Context ;
2020
2121/**
2222 * Class that operates the vibrator on the device.
2323 * <p>
2424 * If your process exits, any vibration you started with will stop.
2525 * </p>
26+ *
27+ * To obtain an instance of the system vibrator, call
28+ * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
2629 */
27- public class Vibrator
28- {
29- private static final String TAG = "Vibrator" ;
30-
31- IVibratorService mService ;
32- private final Binder mToken = new Binder ();
33-
34- /** @hide */
35- public Vibrator ()
36- {
37- mService = IVibratorService .Stub .asInterface (
38- ServiceManager .getService ("vibrator" ));
30+ public abstract class Vibrator {
31+ /**
32+ * @hide to prevent subclassing from outside of the framework
33+ */
34+ public Vibrator () {
3935 }
4036
4137 /**
42- * Check whether the hardware has a vibrator. Returns true if a vibrator
43- * exists, else false.
38+ * Check whether the hardware has a vibrator.
39+ *
40+ * @return True if the hardware has a vibrator, else false.
4441 */
45- public boolean hasVibrator () {
46- if (mService == null ) {
47- Log .w (TAG , "Failed to vibrate; no vibrator service." );
48- return false ;
49- }
50- try {
51- return mService .hasVibrator ();
52- } catch (RemoteException e ) {
53- }
54- return false ;
55- }
42+ public abstract boolean hasVibrator ();
5643
5744 /**
58- * Turn the vibrator on .
45+ * Vibrate constantly for the specified period of time .
5946 *
6047 * @param milliseconds The number of milliseconds to vibrate.
6148 */
62- public void vibrate (long milliseconds )
63- {
64- if (mService == null ) {
65- Log .w (TAG , "Failed to vibrate; no vibrator service." );
66- return ;
67- }
68- try {
69- mService .vibrate (milliseconds , mToken );
70- } catch (RemoteException e ) {
71- Log .w (TAG , "Failed to vibrate." , e );
72- }
73- }
49+ public abstract void vibrate (long milliseconds );
7450
7551 /**
7652 * Vibrate with a given pattern.
@@ -90,38 +66,10 @@ public void vibrate(long milliseconds)
9066 * @param repeat the index into pattern at which to repeat, or -1 if
9167 * you don't want to repeat.
9268 */
93- public void vibrate (long [] pattern , int repeat )
94- {
95- if (mService == null ) {
96- Log .w (TAG , "Failed to vibrate; no vibrator service." );
97- return ;
98- }
99- // catch this here because the server will do nothing. pattern may
100- // not be null, let that be checked, because the server will drop it
101- // anyway
102- if (repeat < pattern .length ) {
103- try {
104- mService .vibratePattern (pattern , repeat , mToken );
105- } catch (RemoteException e ) {
106- Log .w (TAG , "Failed to vibrate." , e );
107- }
108- } else {
109- throw new ArrayIndexOutOfBoundsException ();
110- }
111- }
69+ public abstract void vibrate (long [] pattern , int repeat );
11270
11371 /**
11472 * Turn the vibrator off.
11573 */
116- public void cancel ()
117- {
118- if (mService == null ) {
119- return ;
120- }
121- try {
122- mService .cancelVibrate (mToken );
123- } catch (RemoteException e ) {
124- Log .w (TAG , "Failed to cancel vibration." , e );
125- }
126- }
74+ public abstract void cancel ();
12775}
0 commit comments