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