Vibrator.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 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.os;
18
19/**
20 * Class that operates the vibrator on the device.
21 * <p>
22 * If your process exits, any vibration you started with will stop.
23 */
24public class Vibrator
25{
26    IHardwareService mService;
27
28    /** @hide */
29    public Vibrator()
30    {
31        mService = IHardwareService.Stub.asInterface(
32                ServiceManager.getService("hardware"));
33    }
34
35    /**
36     * Turn the vibrator on.
37     *
38     * @param milliseconds How long to vibrate for.
39     */
40    public void vibrate(long milliseconds)
41    {
42        try {
43            mService.vibrate(milliseconds);
44        } catch (RemoteException e) {
45        }
46    }
47
48    /**
49     * Vibrate with a given pattern.
50     *
51     * <p>
52     * Pass in an array of ints that are the times at which to turn on or off
53     * the vibrator.  The first one is how long to wait before turning it on,
54     * and then after that it alternates.  If you want to repeat, pass the
55     * index into the pattern at which to start the repeat.
56     *
57     * @param pattern an array of longs of times to turn the vibrator on or off.
58     * @param repeat the index into pattern at which to repeat, or -1 if
59     *        you don't want to repeat.
60     */
61    public void vibrate(long[] pattern, int repeat)
62    {
63        // catch this here because the server will do nothing.  pattern may
64        // not be null, let that be checked, because the server will drop it
65        // anyway
66        if (repeat < pattern.length) {
67            try {
68                mService.vibratePattern(pattern, repeat, new Binder());
69            } catch (RemoteException e) {
70            }
71        } else {
72            throw new ArrayIndexOutOfBoundsException();
73        }
74    }
75
76    /**
77     * Turn the vibrator off.
78     */
79    public void cancel()
80    {
81        try {
82            mService.cancelVibrate();
83        } catch (RemoteException e) {
84        }
85    }
86}
87