blob: ccbb0f191f6f8bbcf1762550f4d55eb6b93e4532 [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
Michael Wright35a0c672018-01-24 00:32:53 +000019import android.annotation.IntDef;
Harpreet \"Eli\" Sangha45d53ee2019-09-25 17:43:24 +090020import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060021import android.annotation.RequiresPermission;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060022import android.annotation.SystemService;
John Spurlock1af30c72014-03-10 08:33:35 -040023import android.app.ActivityThread;
Artur Satayevafdb23a2019-12-10 17:47:53 +000024import android.compat.annotation.UnsupportedAppUsage;
Jeff Brownc2346132012-04-13 01:55:38 -070025import android.content.Context;
John Spurlock7b414672014-07-18 13:02:39 -040026import android.media.AudioAttributes;
Michael Wright6a407132017-04-05 14:13:19 +010027import android.util.Log;
Brad Fitzpatricke3316442010-10-14 19:40:56 -070028
Michael Wright35a0c672018-01-24 00:32:53 +000029import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * Class that operates the vibrator on the device.
34 * <p>
John Spurlock0f49c282014-03-10 11:29:35 -040035 * If your process exits, any vibration you started will stop.
Jeff Brownd10bfe12010-12-14 13:23:10 -080036 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060038@SystemService(Context.VIBRATOR_SERVICE)
Jeff Brownc2346132012-04-13 01:55:38 -070039public abstract class Vibrator {
Michael Wright6a407132017-04-05 14:13:19 +010040 private static final String TAG = "Vibrator";
John Spurlock1af30c72014-03-10 08:33:35 -040041
Michael Wright35a0c672018-01-24 00:32:53 +000042 /**
43 * Vibration intensity: no vibrations.
44 * @hide
45 */
46 public static final int VIBRATION_INTENSITY_OFF = 0;
47
48 /**
49 * Vibration intensity: low.
50 * @hide
51 */
52 public static final int VIBRATION_INTENSITY_LOW = 1;
53
54 /**
55 * Vibration intensity: medium.
56 * @hide
57 */
58 public static final int VIBRATION_INTENSITY_MEDIUM = 2;
59
60 /**
61 * Vibration intensity: high.
62 * @hide
63 */
64 public static final int VIBRATION_INTENSITY_HIGH = 3;
65
66 /** @hide */
67 @Retention(RetentionPolicy.SOURCE)
68 @IntDef(prefix = { "VIBRATION_INTENSITY_" }, value = {
69 VIBRATION_INTENSITY_OFF,
70 VIBRATION_INTENSITY_LOW,
71 VIBRATION_INTENSITY_MEDIUM,
72 VIBRATION_INTENSITY_HIGH
73 })
74 public @interface VibrationIntensity{}
75
John Spurlock1af30c72014-03-10 08:33:35 -040076 private final String mPackageName;
Michael Wright0dbb5162018-05-25 15:13:36 +010077 // The default vibration intensity level for haptic feedback.
78 @VibrationIntensity
Alexey Kuzminccdaebb2018-12-10 12:02:51 +000079 private int mDefaultHapticFeedbackIntensity;
Michael Wright0dbb5162018-05-25 15:13:36 +010080 // The default vibration intensity level for notifications.
81 @VibrationIntensity
Alexey Kuzminccdaebb2018-12-10 12:02:51 +000082 private int mDefaultNotificationVibrationIntensity;
83 // The default vibration intensity level for ringtones.
84 @VibrationIntensity
85 private int mDefaultRingVibrationIntensity;
John Spurlock1af30c72014-03-10 08:33:35 -040086
Jeff Brownc2346132012-04-13 01:55:38 -070087 /**
88 * @hide to prevent subclassing from outside of the framework
89 */
Andrei Onea24ec3212019-03-15 17:35:05 +000090 @UnsupportedAppUsage
Jeff Brownc2346132012-04-13 01:55:38 -070091 public Vibrator() {
John Spurlock1af30c72014-03-10 08:33:35 -040092 mPackageName = ActivityThread.currentPackageName();
Michael Wright0dbb5162018-05-25 15:13:36 +010093 final Context ctx = ActivityThread.currentActivityThread().getSystemContext();
Alexey Kuzminccdaebb2018-12-10 12:02:51 +000094 loadVibrationIntensities(ctx);
John Spurlock1af30c72014-03-10 08:33:35 -040095 }
96
97 /**
98 * @hide to prevent subclassing from outside of the framework
99 */
100 protected Vibrator(Context context) {
101 mPackageName = context.getOpPackageName();
Alexey Kuzminccdaebb2018-12-10 12:02:51 +0000102 loadVibrationIntensities(context);
103 }
104
105 private void loadVibrationIntensities(Context context) {
Michael Wright0dbb5162018-05-25 15:13:36 +0100106 mDefaultHapticFeedbackIntensity = loadDefaultIntensity(context,
107 com.android.internal.R.integer.config_defaultHapticFeedbackIntensity);
108 mDefaultNotificationVibrationIntensity = loadDefaultIntensity(context,
109 com.android.internal.R.integer.config_defaultNotificationVibrationIntensity);
Alexey Kuzminccdaebb2018-12-10 12:02:51 +0000110 mDefaultRingVibrationIntensity = loadDefaultIntensity(context,
111 com.android.internal.R.integer.config_defaultRingVibrationIntensity);
Michael Wright0dbb5162018-05-25 15:13:36 +0100112 }
113
114 private int loadDefaultIntensity(Context ctx, int resId) {
115 return ctx != null ? ctx.getResources().getInteger(resId) : VIBRATION_INTENSITY_MEDIUM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
118 /**
Michael Wright35a0c672018-01-24 00:32:53 +0000119 * Get the default vibration intensity for haptic feedback.
120 * @hide
121 */
122 public int getDefaultHapticFeedbackIntensity() {
Michael Wright0dbb5162018-05-25 15:13:36 +0100123 return mDefaultHapticFeedbackIntensity;
Michael Wright35a0c672018-01-24 00:32:53 +0000124 }
125
126 /**
Alexey Kuzminccdaebb2018-12-10 12:02:51 +0000127 * Get the default vibration intensity for notifications.
Michael Wright35a0c672018-01-24 00:32:53 +0000128 * @hide
129 */
130 public int getDefaultNotificationVibrationIntensity() {
Michael Wright0dbb5162018-05-25 15:13:36 +0100131 return mDefaultNotificationVibrationIntensity;
Michael Wright35a0c672018-01-24 00:32:53 +0000132 }
133
Alexey Kuzminccdaebb2018-12-10 12:02:51 +0000134 /** Get the default vibration intensity for ringtones.
135 * @hide
136 */
137 public int getDefaultRingVibrationIntensity() {
138 return mDefaultRingVibrationIntensity;
139 }
140
Michael Wright35a0c672018-01-24 00:32:53 +0000141 /**
Jeff Brownc2346132012-04-13 01:55:38 -0700142 * Check whether the hardware has a vibrator.
143 *
144 * @return True if the hardware has a vibrator, else false.
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700145 */
Jeff Brownc2346132012-04-13 01:55:38 -0700146 public abstract boolean hasVibrator();
John Spurlock1af30c72014-03-10 08:33:35 -0400147
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700148 /**
Michael Wright71216972017-01-31 18:33:54 +0000149 * Check whether the vibrator has amplitude control.
150 *
151 * @return True if the hardware can control the amplitude of the vibrations, otherwise false.
152 */
153 public abstract boolean hasAmplitudeControl();
154
155 /**
Harpreet \"Eli\" Sangha45d53ee2019-09-25 17:43:24 +0900156 * Configure an always-on haptics effect.
157 *
158 * @param id The board-specific always-on ID to configure.
159 * @param effect Vibration effect to assign to always-on id. Passing null will disable it.
160 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
161 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
162 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
163 * vibrations associated with incoming calls. May only be null when effect is null.
164 * @hide
165 */
166 @RequiresPermission(android.Manifest.permission.VIBRATE_ALWAYS_ON)
167 public boolean setAlwaysOnEffect(int id, @Nullable VibrationEffect effect,
168 @Nullable AudioAttributes attributes) {
169 Log.w(TAG, "Always-on effects aren't supported");
170 return false;
171 }
172
173 /**
Jeff Brownc2346132012-04-13 01:55:38 -0700174 * Vibrate constantly for the specified period of time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 *
Jeff Brownd10bfe12010-12-14 13:23:10 -0800176 * @param milliseconds The number of milliseconds to vibrate.
Michael Wright71216972017-01-31 18:33:54 +0000177 *
178 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 */
Michael Wright71216972017-01-31 18:33:54 +0000180 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600181 @RequiresPermission(android.Manifest.permission.VIBRATE)
John Spurlock1af30c72014-03-10 08:33:35 -0400182 public void vibrate(long milliseconds) {
John Spurlock7b414672014-07-18 13:02:39 -0400183 vibrate(milliseconds, null);
John Spurlock1af30c72014-03-10 08:33:35 -0400184 }
185
186 /**
187 * Vibrate constantly for the specified period of time.
John Spurlock1af30c72014-03-10 08:33:35 -0400188 *
189 * @param milliseconds The number of milliseconds to vibrate.
John Spurlock7b414672014-07-18 13:02:39 -0400190 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
191 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700192 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
John Spurlock7b414672014-07-18 13:02:39 -0400193 * vibrations associated with incoming calls.
Michael Wright71216972017-01-31 18:33:54 +0000194 *
195 * @deprecated Use {@link #vibrate(VibrationEffect, AudioAttributes)} instead.
John Spurlock1af30c72014-03-10 08:33:35 -0400196 */
Michael Wright71216972017-01-31 18:33:54 +0000197 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600198 @RequiresPermission(android.Manifest.permission.VIBRATE)
John Spurlock7b414672014-07-18 13:02:39 -0400199 public void vibrate(long milliseconds, AudioAttributes attributes) {
Michael Wright6a407132017-04-05 14:13:19 +0100200 try {
201 // This ignores all exceptions to stay compatible with pre-O implementations.
202 VibrationEffect effect =
203 VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE);
204 vibrate(effect, attributes);
205 } catch (IllegalArgumentException iae) {
206 Log.e(TAG, "Failed to create VibrationEffect", iae);
207 }
John Spurlock1af30c72014-03-10 08:33:35 -0400208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 /**
211 * Vibrate with a given pattern.
212 *
213 * <p>
Jeff Brownd10bfe12010-12-14 13:23:10 -0800214 * Pass in an array of ints that are the durations for which to turn on or off
215 * the vibrator in milliseconds. The first value indicates the number of milliseconds
216 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
217 * for which to keep the vibrator on before turning it off. Subsequent values alternate
218 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
219 * </p><p>
220 * To cause the pattern to repeat, pass the index into the pattern array at which
221 * to start the repeat, or -1 to disable repeating.
222 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 *
Jeff Brownd10bfe12010-12-14 13:23:10 -0800224 * @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 -0800225 * @param repeat the index into pattern at which to repeat, or -1 if
226 * you don't want to repeat.
Michael Wright71216972017-01-31 18:33:54 +0000227 *
228 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Michael Wright71216972017-01-31 18:33:54 +0000230 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600231 @RequiresPermission(android.Manifest.permission.VIBRATE)
John Spurlock1af30c72014-03-10 08:33:35 -0400232 public void vibrate(long[] pattern, int repeat) {
John Spurlock7b414672014-07-18 13:02:39 -0400233 vibrate(pattern, repeat, null);
John Spurlock1af30c72014-03-10 08:33:35 -0400234 }
235
236 /**
237 * Vibrate with a given pattern.
238 *
239 * <p>
240 * Pass in an array of ints that are the durations for which to turn on or off
241 * the vibrator in milliseconds. The first value indicates the number of milliseconds
242 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
243 * for which to keep the vibrator on before turning it off. Subsequent values alternate
244 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
245 * </p><p>
246 * To cause the pattern to repeat, pass the index into the pattern array at which
247 * to start the repeat, or -1 to disable repeating.
248 * </p>
John Spurlock1af30c72014-03-10 08:33:35 -0400249 *
250 * @param pattern an array of longs of times for which to turn the vibrator on or off.
251 * @param repeat the index into pattern at which to repeat, or -1 if
252 * you don't want to repeat.
John Spurlock7b414672014-07-18 13:02:39 -0400253 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
254 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700255 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
John Spurlock7b414672014-07-18 13:02:39 -0400256 * vibrations associated with incoming calls.
Michael Wright71216972017-01-31 18:33:54 +0000257 *
258 * @deprecated Use {@link #vibrate(VibrationEffect, AudioAttributes)} instead.
John Spurlock1af30c72014-03-10 08:33:35 -0400259 */
Michael Wright71216972017-01-31 18:33:54 +0000260 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600261 @RequiresPermission(android.Manifest.permission.VIBRATE)
John Spurlock7b414672014-07-18 13:02:39 -0400262 public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
Michael Wright6a407132017-04-05 14:13:19 +0100263 // This call needs to continue throwing ArrayIndexOutOfBoundsException but ignore all other
264 // exceptions for compatibility purposes
Michael Wright71216972017-01-31 18:33:54 +0000265 if (repeat < -1 || repeat >= pattern.length) {
Michael Wright0ef6edd2017-04-05 20:57:39 +0100266 Log.e(TAG, "vibrate called with repeat index out of bounds" +
267 " (pattern.length=" + pattern.length + ", index=" + repeat + ")");
Michael Wright71216972017-01-31 18:33:54 +0000268 throw new ArrayIndexOutOfBoundsException();
269 }
Michael Wright6a407132017-04-05 14:13:19 +0100270
271 try {
272 vibrate(VibrationEffect.createWaveform(pattern, repeat), attributes);
273 } catch (IllegalArgumentException iae) {
274 Log.e(TAG, "Failed to create VibrationEffect", iae);
275 }
Michael Wright71216972017-01-31 18:33:54 +0000276 }
277
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600278 @RequiresPermission(android.Manifest.permission.VIBRATE)
Michael Wright71216972017-01-31 18:33:54 +0000279 public void vibrate(VibrationEffect vibe) {
280 vibrate(vibe, null);
281 }
282
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600283 @RequiresPermission(android.Manifest.permission.VIBRATE)
Michael Wright71216972017-01-31 18:33:54 +0000284 public void vibrate(VibrationEffect vibe, AudioAttributes attributes) {
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100285 vibrate(Process.myUid(), mPackageName, vibe, null, attributes);
John Spurlock1af30c72014-03-10 08:33:35 -0400286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
288 /**
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100289 * Like {@link #vibrate(int, String, VibrationEffect, AudioAttributes)}, but allows the
290 * caller to specify the vibration is owned by someone else and set reason for vibration.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800291 * @hide
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800292 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600293 @RequiresPermission(android.Manifest.permission.VIBRATE)
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100294 public abstract void vibrate(int uid, String opPkg, VibrationEffect vibe,
295 String reason, AudioAttributes attributes);
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800296
297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 * Turn the vibrator off.
299 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600300 @RequiresPermission(android.Manifest.permission.VIBRATE)
Jeff Brownc2346132012-04-13 01:55:38 -0700301 public abstract void cancel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}