blob: f9b7666b79f7a847133e3bf7d523a07996c04f07 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
John Spurlock1af30c72014-03-10 08:33:35 -040019import android.app.ActivityThread;
Jeff Brownc2346132012-04-13 01:55:38 -070020import android.content.Context;
John Spurlock7b414672014-07-18 13:02:39 -040021import android.media.AudioAttributes;
Brad Fitzpatricke3316442010-10-14 19:40:56 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
24 * Class that operates the vibrator on the device.
25 * <p>
John Spurlock0f49c282014-03-10 11:29:35 -040026 * If your process exits, any vibration you started will stop.
Jeff Brownd10bfe12010-12-14 13:23:10 -080027 * </p>
Jeff Brownc2346132012-04-13 01:55:38 -070028 *
29 * To obtain an instance of the system vibrator, call
John Spurlock0f49c282014-03-10 11:29:35 -040030 * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as the argument.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 */
Jeff Brownc2346132012-04-13 01:55:38 -070032public abstract class Vibrator {
John Spurlock1af30c72014-03-10 08:33:35 -040033
34 private final String mPackageName;
35
Jeff Brownc2346132012-04-13 01:55:38 -070036 /**
37 * @hide to prevent subclassing from outside of the framework
38 */
39 public Vibrator() {
John Spurlock1af30c72014-03-10 08:33:35 -040040 mPackageName = ActivityThread.currentPackageName();
41 }
42
43 /**
44 * @hide to prevent subclassing from outside of the framework
45 */
46 protected Vibrator(Context context) {
47 mPackageName = context.getOpPackageName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 }
49
50 /**
Jeff Brownc2346132012-04-13 01:55:38 -070051 * Check whether the hardware has a vibrator.
52 *
53 * @return True if the hardware has a vibrator, else false.
Dianne Hackbornea9020e2010-11-04 11:39:12 -070054 */
Jeff Brownc2346132012-04-13 01:55:38 -070055 public abstract boolean hasVibrator();
John Spurlock1af30c72014-03-10 08:33:35 -040056
Dianne Hackbornea9020e2010-11-04 11:39:12 -070057 /**
Jeff Brownc2346132012-04-13 01:55:38 -070058 * Vibrate constantly for the specified period of time.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -070059 * <p>This method requires the caller to hold the permission
60 * {@link android.Manifest.permission#VIBRATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 *
Jeff Brownd10bfe12010-12-14 13:23:10 -080062 * @param milliseconds The number of milliseconds to vibrate.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
John Spurlock1af30c72014-03-10 08:33:35 -040064 public void vibrate(long milliseconds) {
John Spurlock7b414672014-07-18 13:02:39 -040065 vibrate(milliseconds, null);
John Spurlock1af30c72014-03-10 08:33:35 -040066 }
67
68 /**
69 * Vibrate constantly for the specified period of time.
70 * <p>This method requires the caller to hold the permission
71 * {@link android.Manifest.permission#VIBRATE}.
72 *
73 * @param milliseconds The number of milliseconds to vibrate.
John Spurlock7b414672014-07-18 13:02:39 -040074 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
75 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -070076 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
John Spurlock7b414672014-07-18 13:02:39 -040077 * vibrations associated with incoming calls.
John Spurlock1af30c72014-03-10 08:33:35 -040078 */
John Spurlock7b414672014-07-18 13:02:39 -040079 public void vibrate(long milliseconds, AudioAttributes attributes) {
80 vibrate(Process.myUid(), mPackageName, milliseconds, attributes);
John Spurlock1af30c72014-03-10 08:33:35 -040081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 /**
84 * Vibrate with a given pattern.
85 *
86 * <p>
Jeff Brownd10bfe12010-12-14 13:23:10 -080087 * Pass in an array of ints that are the durations for which to turn on or off
88 * the vibrator in milliseconds. The first value indicates the number of milliseconds
89 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
90 * for which to keep the vibrator on before turning it off. Subsequent values alternate
91 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
92 * </p><p>
93 * To cause the pattern to repeat, pass the index into the pattern array at which
94 * to start the repeat, or -1 to disable repeating.
95 * </p>
Nicolas Falliere9530e3a2012-06-18 17:21:06 -070096 * <p>This method requires the caller to hold the permission
97 * {@link android.Manifest.permission#VIBRATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 *
Jeff Brownd10bfe12010-12-14 13:23:10 -080099 * @param pattern an array of longs of times for which to turn the vibrator on or off.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * @param repeat the index into pattern at which to repeat, or -1 if
101 * you don't want to repeat.
102 */
John Spurlock1af30c72014-03-10 08:33:35 -0400103 public void vibrate(long[] pattern, int repeat) {
John Spurlock7b414672014-07-18 13:02:39 -0400104 vibrate(pattern, repeat, null);
John Spurlock1af30c72014-03-10 08:33:35 -0400105 }
106
107 /**
108 * Vibrate with a given pattern.
109 *
110 * <p>
111 * Pass in an array of ints that are the durations for which to turn on or off
112 * the vibrator in milliseconds. The first value indicates the number of milliseconds
113 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
114 * for which to keep the vibrator on before turning it off. Subsequent values alternate
115 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
116 * </p><p>
117 * To cause the pattern to repeat, pass the index into the pattern array at which
118 * to start the repeat, or -1 to disable repeating.
119 * </p>
120 * <p>This method requires the caller to hold the permission
121 * {@link android.Manifest.permission#VIBRATE}.
122 *
123 * @param pattern an array of longs of times for which to turn the vibrator on or off.
124 * @param repeat the index into pattern at which to repeat, or -1 if
125 * you don't want to repeat.
John Spurlock7b414672014-07-18 13:02:39 -0400126 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
127 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700128 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
John Spurlock7b414672014-07-18 13:02:39 -0400129 * vibrations associated with incoming calls.
John Spurlock1af30c72014-03-10 08:33:35 -0400130 */
John Spurlock7b414672014-07-18 13:02:39 -0400131 public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
132 vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes);
John Spurlock1af30c72014-03-10 08:33:35 -0400133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 /**
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800136 * @hide
John Spurlock7b414672014-07-18 13:02:39 -0400137 * Like {@link #vibrate(long, AudioAttributes)}, but allowing the caller to specify that
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800138 * the vibration is owned by someone else.
139 */
John Spurlock7b414672014-07-18 13:02:39 -0400140 public abstract void vibrate(int uid, String opPkg, long milliseconds,
141 AudioAttributes attributes);
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800142
143 /**
144 * @hide
John Spurlock7b414672014-07-18 13:02:39 -0400145 * Like {@link #vibrate(long[], int, AudioAttributes)}, but allowing the caller to specify that
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800146 * the vibration is owned by someone else.
147 */
John Spurlock7b414672014-07-18 13:02:39 -0400148 public abstract void vibrate(int uid, String opPkg, long[] pattern, int repeat,
149 AudioAttributes attributes);
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800150
151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * Turn the vibrator off.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700153 * <p>This method requires the caller to hold the permission
154 * {@link android.Manifest.permission#VIBRATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
Jeff Brownc2346132012-04-13 01:55:38 -0700156 public abstract void cancel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}