19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.os;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
191af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlockimport android.app.ActivityThread;
20c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brownimport android.content.Context;
217b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlockimport android.media.AudioAttributes;
22e331644cb570e74a8739cb21ffcc5875663ffa58Brad Fitzpatrick
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Class that operates the vibrator on the device.
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>
260f49c28e7a3667e680002beb8c557d8b5e3ad1d3John Spurlock * If your process exits, any vibration you started will stop.
27d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown * </p>
28c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown *
29c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown * To obtain an instance of the system vibrator, call
300f49c28e7a3667e680002beb8c557d8b5e3ad1d3John Spurlock * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as the argument.
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
32c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brownpublic abstract class Vibrator {
331af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
341af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    private final String mPackageName;
351af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
36c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown    /**
37c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     * @hide to prevent subclassing from outside of the framework
38c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     */
39c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown    public Vibrator() {
401af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock        mPackageName = ActivityThread.currentPackageName();
411af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    }
421af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
431af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    /**
441af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * @hide to prevent subclassing from outside of the framework
451af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     */
461af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    protected Vibrator(Context context) {
471af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock        mPackageName = context.getOpPackageName();
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
51c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     * Check whether the hardware has a vibrator.
52c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     *
53c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     * @return True if the hardware has a vibrator, else false.
54ea9020e0854427d47e566a1394df6749f3265410Dianne Hackborn     */
55c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown    public abstract boolean hasVibrator();
561af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
57ea9020e0854427d47e566a1394df6749f3265410Dianne Hackborn    /**
58c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown     * Vibrate constantly for the specified period of time.
599530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * <p>This method requires the caller to hold the permission
609530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * {@link android.Manifest.permission#VIBRATE}.
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
62d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * @param milliseconds The number of milliseconds to vibrate.
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
641af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    public void vibrate(long milliseconds) {
657b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock        vibrate(milliseconds, null);
661af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    }
671af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
681af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    /**
691af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * Vibrate constantly for the specified period of time.
701af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * <p>This method requires the caller to hold the permission
711af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * {@link android.Manifest.permission#VIBRATE}.
721af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     *
731af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * @param milliseconds The number of milliseconds to vibrate.
747b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
757b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     *        specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
7689c3b29a9bfa0ae9858b913bc1ab6604c4613a15Jean-Michel Trivi     *        {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
777b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     *        vibrations associated with incoming calls.
781af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     */
797b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock    public void vibrate(long milliseconds, AudioAttributes attributes) {
807b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock        vibrate(Process.myUid(), mPackageName, milliseconds, attributes);
811af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    }
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate with a given pattern.
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
87d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * Pass in an array of ints that are the durations for which to turn on or off
88d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * the vibrator in milliseconds.  The first value indicates the number of milliseconds
89d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * to wait before turning the vibrator on.  The next value indicates the number of milliseconds
90d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * for which to keep the vibrator on before turning it off.  Subsequent values alternate
91d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
92d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * </p><p>
93d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * To cause the pattern to repeat, pass the index into the pattern array at which
94d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * to start the repeat, or -1 to disable repeating.
95d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * </p>
969530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * <p>This method requires the caller to hold the permission
979530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * {@link android.Manifest.permission#VIBRATE}.
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
99d10bfe185c336d813845c9beb1f4041c5daa1669Jeff Brown     * @param pattern an array of longs of times for which to turn the vibrator on or off.
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param repeat the index into pattern at which to repeat, or -1 if
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *        you don't want to repeat.
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1031af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    public void vibrate(long[] pattern, int repeat) {
1047b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock        vibrate(pattern, repeat, null);
1051af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    }
1061af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock
1071af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    /**
1081af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * Vibrate with a given pattern.
1091af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     *
1101af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * <p>
1111af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * Pass in an array of ints that are the durations for which to turn on or off
1121af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * the vibrator in milliseconds.  The first value indicates the number of milliseconds
1131af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * to wait before turning the vibrator on.  The next value indicates the number of milliseconds
1141af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * for which to keep the vibrator on before turning it off.  Subsequent values alternate
1151af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
1161af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * </p><p>
1171af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * To cause the pattern to repeat, pass the index into the pattern array at which
1181af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * to start the repeat, or -1 to disable repeating.
1191af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * </p>
1201af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * <p>This method requires the caller to hold the permission
1211af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * {@link android.Manifest.permission#VIBRATE}.
1221af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     *
1231af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * @param pattern an array of longs of times for which to turn the vibrator on or off.
1241af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     * @param repeat the index into pattern at which to repeat, or -1 if
1251af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     *        you don't want to repeat.
1267b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
1277b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     *        specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
12889c3b29a9bfa0ae9858b913bc1ab6604c4613a15Jean-Michel Trivi     *        {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
1297b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     *        vibrations associated with incoming calls.
1301af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock     */
1317b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock    public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
1327b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock        vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes);
1331af30c7ac480e5d335f267a3ac3b2e6c748ce240John Spurlock    }
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
136f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     * @hide
1377b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     * Like {@link #vibrate(long, AudioAttributes)}, but allowing the caller to specify that
138f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     * the vibration is owned by someone else.
139f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     */
1407b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock    public abstract void vibrate(int uid, String opPkg, long milliseconds,
1417b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock            AudioAttributes attributes);
142f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn
143f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn    /**
144f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     * @hide
1457b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock     * Like {@link #vibrate(long[], int, AudioAttributes)}, but allowing the caller to specify that
146f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     * the vibration is owned by someone else.
147f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn     */
1487b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock    public abstract void vibrate(int uid, String opPkg, long[] pattern, int repeat,
1497b41467704f941b11af6aace3e40993afc7f6c6fJohn Spurlock            AudioAttributes attributes);
150f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn
151f265ea9d8307282ff1da3915978625a94fc2859eDianne Hackborn    /**
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Turn the vibrator off.
1539530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * <p>This method requires the caller to hold the permission
1549530e3a22d5ffa2019d1a5177b6a441d4d6d048bNicolas Falliere     * {@link android.Manifest.permission#VIBRATE}.
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
156c2346134bb519a54d50655cbef940fc3fdec60a9Jeff Brown    public abstract void cancel();
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
158