Debian Codegraph @

core/java/android/os/Vibrator.java

raw symbols SCIP
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
17 package android.os;
18
19 import android.annotation.CallbackExecutor;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SystemApi;
25 import android.annotation.SystemService;
26 import android.annotation.TestApi;
27 import android.app.ActivityThread;
28 import android.compat.annotation.UnsupportedAppUsage;
29 import android.content.Context;
30 import android.content.res.Resources;
31 import android.hardware.vibrator.IVibrator;
32 import android.media.AudioAttributes;
33 import android.os.vibrator.VibrationConfig;
34 import android.os.vibrator.VibratorFrequencyProfile;
35 import android.util.Log;
36
37 import java.lang.annotation.Retention;
38 import java.lang.annotation.RetentionPolicy;
39 import java.util.concurrent.Executor;
40
41 /**
42 * Class that operates the vibrator on the device.
43 * <p>
44 * If your process exits, any vibration you started will stop.
45 * </p>
46 */
47 @SystemService(Context.VIBRATOR_SERVICE)
48 public abstract class Vibrator {
49 private static final String TAG = "Vibrator";
50
51 /**
52 * Vibration intensity: no vibrations.
53 *
54 * @hide
55 */
56 @TestApi
57 public static final int VIBRATION_INTENSITY_OFF = 0;
58
59 /**
60 * Vibration intensity: low.
61 *
62 * @hide
63 */
64 @TestApi
65 public static final int VIBRATION_INTENSITY_LOW = 1;
66
67 /**
68 * Vibration intensity: medium.
69 *
70 * @hide
71 */
72 @TestApi
73 public static final int VIBRATION_INTENSITY_MEDIUM = 2;
74
75 /**
76 * Vibration intensity: high.
77 *
78 * @hide
79 */
80 @TestApi
81 public static final int VIBRATION_INTENSITY_HIGH = 3;
82
83 /**
84 * Vibration effect support: unknown
85 *
86 * <p>The hardware doesn't report its supported effects, so we can't determine whether the
87 * effect is supported or not.
88 */
89 public static final int VIBRATION_EFFECT_SUPPORT_UNKNOWN = 0;
90
91 /**
92 * Vibration effect support: supported
93 *
94 * <p>This effect is supported by the underlying hardware.
95 */
96 public static final int VIBRATION_EFFECT_SUPPORT_YES = 1;
97
98 /**
99 * Vibration effect support: unsupported
100 *
101 * <p>This effect is <b>not</b> natively supported by the underlying hardware, although
102 * the system may still play a fallback vibration.
103 */
104 public static final int VIBRATION_EFFECT_SUPPORT_NO = 2;
105
106 /** @hide */
107 @Retention(RetentionPolicy.SOURCE)
108 @IntDef(prefix = {"VIBRATION_EFFECT_SUPPORT_"}, value = {
109 VIBRATION_EFFECT_SUPPORT_UNKNOWN,
110 VIBRATION_EFFECT_SUPPORT_YES,
111 VIBRATION_EFFECT_SUPPORT_NO,
112 })
113 public @interface VibrationEffectSupport {}
114
115 /** @hide */
116 @Retention(RetentionPolicy.SOURCE)
117 @IntDef(prefix = {"VIBRATION_INTENSITY_"}, value = {
118 VIBRATION_INTENSITY_OFF,
119 VIBRATION_INTENSITY_LOW,
120 VIBRATION_INTENSITY_MEDIUM,
121 VIBRATION_INTENSITY_HIGH
122 })
123 public @interface VibrationIntensity {
124 }
125
126 private final String mPackageName;
127 @Nullable
128 private final Resources mResources;
129
130 // This is lazily loaded only for the few clients that need this (e. Settings app).
131 @Nullable
132 private volatile VibrationConfig mVibrationConfig;
133
134 /**
135 * @hide to prevent subclassing from outside of the framework
136 */
137 @UnsupportedAppUsage
138 public Vibrator() {
139 mPackageName = ActivityThread.currentPackageName();
140 mResources = null;
141 }
142
143 /**
144 * @hide to prevent subclassing from outside of the framework
145 */
146 protected Vibrator(Context context) {
147 mPackageName = context.getOpPackageName();
148 mResources = context.getResources();
149 }
150
151 /**
152 * Get the info describing this vibrator.
153 *
154 * @hide
155 */
156 protected VibratorInfo getInfo() {
157 return VibratorInfo.EMPTY_VIBRATOR_INFO;
158 }
159
160 /** Get the static vibrator configuration from config.xml. */
161 private VibrationConfig getConfig() {
162 if (mVibrationConfig == null) {
163 Resources resources = mResources;
164 if (resources == null) {
165 final Context ctx = ActivityThread.currentActivityThread().getSystemContext();
166 resources = ctx != null ? ctx.getResources() : null;
167 }
168 // This might be constructed more than once, but it only loads static config data from a
169 // xml file, so it would be ok.
170 mVibrationConfig = new VibrationConfig(resources);
171 }
172 return mVibrationConfig;
173 }
174
175 /**
176 * Get the default vibration intensity for given usage.
177 *
178 * @hide
179 */
180 @TestApi
181 @VibrationIntensity
182 public int getDefaultVibrationIntensity(@VibrationAttributes.Usage int usage) {
183 return getConfig().getDefaultVibrationIntensity(usage);
184 }
185
186 /**
187 * Return the ID of this vibrator.
188 *
189 * @return A non-negative integer representing the id of the vibrator controlled by this
190 * service, or -1 this service is not attached to any physical vibrator.
191 */
192 public int getId() {
193 return getInfo().getId();
194 }
195
196 /**
197 * Check whether the hardware has a vibrator.
198 *
199 * @return True if the hardware has a vibrator, else false.
200 */
201 public abstract boolean hasVibrator();
202
203 /**
204 * Check whether the vibrator has amplitude control.
205 *
206 * @return True if the hardware can control the amplitude of the vibrations, otherwise false.
207 */
208 public abstract boolean hasAmplitudeControl();
209
210 /**
211 * Check whether the vibrator has independent frequency control.
212 *
213 * @return True if the hardware can control the frequency of the vibrations independently of
214 * the vibration amplitude, false otherwise.
215 * @hide
216 */
217 @TestApi
218 public boolean hasFrequencyControl() {
219 // We currently can only control frequency of the vibration using the compose PWLE method.
220 return getInfo().hasCapability(
221 IVibrator.CAP_FREQUENCY_CONTROL | IVibrator.CAP_COMPOSE_PWLE_EFFECTS);
222 }
223
224 /**
225 * Check whether the vibrator can be controlled by an external service with the
226 * {@link IExternalVibratorService}.
227 *
228 * @return True if the hardware can be controlled by an external service, otherwise false.
229 * @hide
230 */
231 public boolean hasExternalControl() {
232 return getInfo().hasCapability(IVibrator.CAP_EXTERNAL_CONTROL);
233 }
234
235 /**
236 * Gets the resonant frequency of the vibrator, if applicable.
237 *
238 * @return the resonant frequency of the vibrator, or {@link Float#NaN NaN} if it's unknown, not
239 * applicable, or if this vibrator is a composite of multiple physical devices with different
240 * frequencies.
241 * @hide
242 */
243 @TestApi
244 public float getResonantFrequency() {
245 return getInfo().getResonantFrequencyHz();
246 }
247
248 /**
249 * Gets the <a href="https://en.wikipedia.org/wiki/Q_factor">Q factor</a> of the vibrator.
250 *
251 * @return the Q factor of the vibrator, or {@link Float#NaN NaN} if it's unknown, not
252 * applicable, or if this vibrator is a composite of multiple physical devices with different
253 * Q factors.
254 * @hide
255 */
256 @TestApi
257 public float getQFactor() {
258 return getInfo().getQFactor();
259 }
260
261 /**
262 * Gets the profile that describes the vibrator output across the supported frequency range.
263 *
264 * <p>The profile describes the relative output acceleration that the device can reach when it
265 * vibrates at different frequencies.
266 *
267 * @return The frequency profile for this vibrator, or null if the vibrator does not have
268 * frequency control. If this vibrator is a composite of multiple physical devices then this
269 * will return a profile supported in all devices, or null if the intersection is empty or not
270 * available.
271 * @hide
272 */
273 @TestApi
274 @Nullable
275 public VibratorFrequencyProfile getFrequencyProfile() {
276 VibratorInfo.FrequencyProfile frequencyProfile = getInfo().getFrequencyProfile();
277 if (frequencyProfile.isEmpty()) {
278 return null;
279 }
280 return new VibratorFrequencyProfile(frequencyProfile);
281 }
282
283 /**
284 * Return the maximum amplitude the vibrator can play using the audio haptic channels.
285 *
286 * <p>This is a positive value, or {@link Float#NaN NaN} if it's unknown. If this returns a
287 * positive value <code>maxAmplitude</code>, then the signals from the haptic channels of audio
288 * tracks should be in the range <code>[-maxAmplitude, maxAmplitude]</code>.
289 *
290 * @return a positive value representing the maximum absolute value the device can play signals
291 * from audio haptic channels, or {@link Float#NaN NaN} if it's unknown.
292 * @hide
293 */
294 public float getHapticChannelMaximumAmplitude() {
295 return getConfig().getHapticChannelMaximumAmplitude();
296 }
297
298 /**
299 * Configure an always-on haptics effect.
300 *
301 * @param alwaysOnId The board-specific always-on ID to configure.
302 * @param effect Vibration effect to assign to always-on id. Passing null will disable it.
303 * @param attributes {@link VibrationAttributes} corresponding to the vibration. For example,
304 * specify {@link VibrationAttributes#USAGE_ALARM} for alarm vibrations or
305 * {@link VibrationAttributes#USAGE_RINGTONE} for vibrations associated with
306 * incoming calls. May only be null when effect is null.
307 * @hide
308 */
309 @RequiresPermission(android.Manifest.permission.VIBRATE_ALWAYS_ON)
310 public boolean setAlwaysOnEffect(int alwaysOnId, @Nullable VibrationEffect effect,
311 @Nullable VibrationAttributes attributes) {
312 return setAlwaysOnEffect(Process.myUid(), mPackageName, alwaysOnId, effect, attributes);
313 }
314
315 /**
316 * @hide
317 */
318 @RequiresPermission(android.Manifest.permission.VIBRATE_ALWAYS_ON)
319 public boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId,
320 @Nullable VibrationEffect effect, @Nullable VibrationAttributes attributes) {
321 Log.w(TAG, "Always-on effects aren't supported");
322 return false;
323 }
324
325 /**
326 * Vibrate constantly for the specified period of time.
327 *
328 * <p>The app should be in the foreground for the vibration to happen.</p>
329 *
330 * @param milliseconds The number of milliseconds to vibrate.
331 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
332 */
333 @Deprecated
334 @RequiresPermission(android.Manifest.permission.VIBRATE)
335 public void vibrate(long milliseconds) {
336 vibrate(milliseconds, null);
337 }
338
339 /**
340 * Vibrate constantly for the specified period of time.
341 *
342 * <p>The app should be in the foreground for the vibration to happen. Background apps should
343 * specify a ringtone, notification or alarm usage in order to vibrate.</p>
344 *
345 * @param milliseconds The number of milliseconds to vibrate.
346 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
347 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
348 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
349 * vibrations associated with incoming calls.
350 * @deprecated Use {@link #vibrate(VibrationEffect, VibrationAttributes)} instead.
351 */
352 @Deprecated
353 @RequiresPermission(android.Manifest.permission.VIBRATE)
354 public void vibrate(long milliseconds, AudioAttributes attributes) {
355 try {
356 // This ignores all exceptions to stay compatible with pre-O implementations.
357 VibrationEffect effect =
358 VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE);
359 vibrate(effect, attributes);
360 } catch (IllegalArgumentException iae) {
361 Log.e(TAG, "Failed to create VibrationEffect", iae);
362 }
363 }
364
365 /**
366 * Vibrate with a given pattern.
367 *
368 * <p>
369 * Pass in an array of ints that are the durations for which to turn on or off
370 * the vibrator in milliseconds. The first value indicates the number of milliseconds
371 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
372 * for which to keep the vibrator on before turning it off. Subsequent values alternate
373 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
374 * </p><p>
375 * To cause the pattern to repeat, pass the index into the pattern array at which
376 * to start the repeat, or -1 to disable repeating.
377 * </p>
378 *
379 * <p>The app should be in the foreground for the vibration to happen.</p>
380 *
381 * @param pattern an array of longs of times for which to turn the vibrator on or off.
382 * @param repeat the index into pattern at which to repeat, or -1 if
383 * you don't want to repeat.
384 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
385 */
386 @Deprecated
387 @RequiresPermission(android.Manifest.permission.VIBRATE)
388 public void vibrate(long[] pattern, int repeat) {
389 vibrate(pattern, repeat, null);
390 }
391
392 /**
393 * Vibrate with a given pattern.
394 *
395 * <p>
396 * Pass in an array of ints that are the durations for which to turn on or off
397 * the vibrator in milliseconds. The first value indicates the number of milliseconds
398 * to wait before turning the vibrator on. The next value indicates the number of milliseconds
399 * for which to keep the vibrator on before turning it off. Subsequent values alternate
400 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
401 * </p><p>
402 * To cause the pattern to repeat, pass the index into the pattern array at which
403 * to start the repeat, or -1 to disable repeating.
404 * </p>
405 *
406 * <p>The app should be in the foreground for the vibration to happen. Background apps should
407 * specify a ringtone, notification or alarm usage in order to vibrate.</p>
408 *
409 * @param pattern an array of longs of times for which to turn the vibrator on or off.
410 * @param repeat the index into pattern at which to repeat, or -1 if
411 * you don't want to repeat.
412 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
413 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
414 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
415 * vibrations associated with incoming calls.
416 * @deprecated Use {@link #vibrate(VibrationEffect, VibrationAttributes)} instead.
417 */
418 @Deprecated
419 @RequiresPermission(android.Manifest.permission.VIBRATE)
420 public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
421 // This call needs to continue throwing ArrayIndexOutOfBoundsException but ignore all other
422 // exceptions for compatibility purposes
423 if (repeat < -1 || repeat >= pattern.length) {
424 Log.e(TAG, "vibrate called with repeat index out of bounds" +
425 " (pattern.length=" + pattern.length + ", index=" + repeat + ")");
426 throw new ArrayIndexOutOfBoundsException();
427 }
428
429 try {
430 vibrate(VibrationEffect.createWaveform(pattern, repeat), attributes);
431 } catch (IllegalArgumentException iae) {
432 Log.e(TAG, "Failed to create VibrationEffect", iae);
433 }
434 }
435
436 /**
437 * Vibrate with a given effect.
438 *
439 * <p>The app should be in the foreground for the vibration to happen.</p>
440 *
441 * @param vibe {@link VibrationEffect} describing the vibration to be performed.
442 */
443 @RequiresPermission(android.Manifest.permission.VIBRATE)
444 public void vibrate(VibrationEffect vibe) {
445 vibrate(vibe, new VibrationAttributes.Builder().build());
446 }
447
448 /**
449 * Vibrate with a given effect.
450 *
451 * <p>The app should be in the foreground for the vibration to happen. Background apps should
452 * specify a ringtone, notification or alarm usage in order to vibrate.</p>
453 *
454 * @param vibe {@link VibrationEffect} describing the vibration to be performed.
455 * @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
456 * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
457 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
458 * vibrations associated with incoming calls.
459 * @deprecated Use {@link #vibrate(VibrationEffect, VibrationAttributes)} instead.
460 */
461 @RequiresPermission(android.Manifest.permission.VIBRATE)
462 public void vibrate(VibrationEffect vibe, AudioAttributes attributes) {
463 vibrate(vibe,
464 attributes == null
465 ? new VibrationAttributes.Builder().build()
466 : new VibrationAttributes.Builder(attributes).build());
467 }
468
469 /**
470 * Vibrate with a given effect.
471 *
472 * <p>The app should be in the foreground for the vibration to happen. Background apps should
473 * specify a ringtone, notification or alarm usage in order to vibrate.</p>
474 *
475 * @param vibe {@link VibrationEffect} describing the vibration to be performed.
476 * @param attributes {@link VibrationAttributes} corresponding to the vibration. For example,
477 * specify {@link VibrationAttributes#USAGE_ALARM} for alarm vibrations or
478 * {@link VibrationAttributes#USAGE_RINGTONE} for vibrations associated with
479 * incoming calls.
480 */
481 @RequiresPermission(android.Manifest.permission.VIBRATE)
482 public void vibrate(@NonNull VibrationEffect vibe, @NonNull VibrationAttributes attributes) {
483 vibrate(Process.myUid(), mPackageName, vibe, null, attributes);
484 }
485
486 /**
487 * Like {@link #vibrate(VibrationEffect, VibrationAttributes)}, but allows the
488 * caller to specify the vibration is owned by someone else and set a reason for vibration.
489 *
490 * @hide
491 */
492 @RequiresPermission(android.Manifest.permission.VIBRATE)
493 public abstract void vibrate(int uid, String opPkg, @NonNull VibrationEffect vibe,
494 String reason, @NonNull VibrationAttributes attributes);
495
496 /**
497 * Query whether the vibrator natively supports the given effects.
498 *
499 * <p>If an effect is not supported, the system may still automatically fall back to playing
500 * a simpler vibration instead, which is not optimised for the specific device. This includes
501 * the unknown case, which can't be determined in advance, that will dynamically attempt to
502 * fall back if the optimised effect fails to play.
503 *
504 * <p>The returned array will be the same length as the query array and the value at a given
505 * index will contain {@link #VIBRATION_EFFECT_SUPPORT_YES} if the effect at that same index
506 * in the querying array is supported, {@link #VIBRATION_EFFECT_SUPPORT_NO} if it isn't
507 * supported, or {@link #VIBRATION_EFFECT_SUPPORT_UNKNOWN} if the system can't determine whether
508 * it's supported or not, as some hardware doesn't report its effect capabilities.
509 *
510 * <p>Use {@link #areAllEffectsSupported(int...)} to get a single combined result,
511 * or for convenience when querying exactly one effect.
512 *
513 * @param effectIds Which effects to query for.
514 * @return An array containing the systems current knowledge about whether the given effects
515 * are natively supported by the device, or not.
516 */
517 @NonNull
518 @VibrationEffectSupport
519 public int[] areEffectsSupported(
520 @NonNull @VibrationEffect.EffectType int... effectIds) {
521 VibratorInfo info = getInfo();
522 int[] supported = new int[effectIds.length];
523 for (int i = 0; i < effectIds.length; i++) {
524 supported[i] = info.isEffectSupported(effectIds[i]);
525 }
526 return supported;
527 }
528
529 /**
530 * Query whether the vibrator supports all the given effects. If no argument is provided this
531 * method will always return {@link #VIBRATION_EFFECT_SUPPORT_YES}.
532 *
533 * <p>If an effect is not supported, the system may still automatically fall back to a simpler
534 * vibration instead, which is not optimised for the specific device, however vibration isn't
535 * guaranteed in this case.
536 *
537 * <p>If the result is {@link #VIBRATION_EFFECT_SUPPORT_YES}, all effects in the query are
538 * supported by the hardware.
539 *
540 * <p>If the result is {@link #VIBRATION_EFFECT_SUPPORT_NO}, at least one of the effects in the
541 * query is not supported, and using them may fall back to an un-optimized vibration or no
542 * vibration.
543 *
544 * <p>If the result is {@link #VIBRATION_EFFECT_SUPPORT_UNKNOWN}, the system doesn't know
545 * whether all the effects are supported. It may support any or all of the queried effects,
546 * but there's no way to programmatically know whether a {@link #vibrate} call will successfully
547 * cause a vibration. It's guaranteed, however, that none of the queried effects are
548 * definitively unsupported by the hardware.
549 *
550 * <p>Use {@link #areEffectsSupported(int...)} to get individual results for each effect.
551 *
552 * @param effectIds Which effects to query for.
553 * @return Whether all specified effects are natively supported by the device. Empty query
554 * defaults to {@link #VIBRATION_EFFECT_SUPPORT_YES}.
555 */
556 @VibrationEffectSupport
557 public final int areAllEffectsSupported(
558 @NonNull @VibrationEffect.EffectType int... effectIds) {
559 VibratorInfo info = getInfo();
560 int allSupported = VIBRATION_EFFECT_SUPPORT_YES;
561 for (int effectId : effectIds) {
562 switch (info.isEffectSupported(effectId)) {
563 case VIBRATION_EFFECT_SUPPORT_NO:
564 return VIBRATION_EFFECT_SUPPORT_NO;
565 case VIBRATION_EFFECT_SUPPORT_YES:
566 continue;
567 default: // VIBRATION_EFFECT_SUPPORT_UNKNOWN
568 allSupported = VIBRATION_EFFECT_SUPPORT_UNKNOWN;
569 break;
570 }
571 }
572 return allSupported;
573 }
574
575 /**
576 * Query whether the vibrator supports the given primitives.
577 *
578 * The returned array will be the same length as the query array and the value at a given index
579 * will contain whether the effect at that same index in the querying array is supported or
580 * not.
581 *
582 * <p>If a primitive is not supported by the device, then <em>no vibration</em> will occur if
583 * it is played.
584 *
585 * <p>Use {@link #areAllPrimitivesSupported(int...)} to get a single combined result,
586 * or for convenience when querying exactly one primitive.
587 *
588 * @param primitiveIds Which primitives to query for.
589 * @return Whether the primitives are supported.
590 */
591 @NonNull
592 public boolean[] arePrimitivesSupported(
593 @NonNull @VibrationEffect.Composition.PrimitiveType int... primitiveIds) {
594 VibratorInfo info = getInfo();
595 boolean[] supported = new boolean[primitiveIds.length];
596 for (int i = 0; i < primitiveIds.length; i++) {
597 supported[i] = info.isPrimitiveSupported(primitiveIds[i]);
598 }
599 return supported;
600 }
601
602 /**
603 * Query whether the vibrator supports all of the given primitives. If no argument is provided
604 * this method will always return {@code true}.
605 *
606 * <p>If a primitive is not supported by the device, then <em>no vibration</em> will occur if
607 * it is played.
608 *
609 * <p>Use {@link #arePrimitivesSupported(int...)} to get individual results for each primitive.
610 *
611 * @param primitiveIds Which primitives to query for.
612 * @return Whether all specified primitives are supported. Empty query defaults to {@code true}.
613 */
614 public final boolean areAllPrimitivesSupported(
615 @NonNull @VibrationEffect.Composition.PrimitiveType int... primitiveIds) {
616 VibratorInfo info = getInfo();
617 for (int primitiveId : primitiveIds) {
618 if (!info.isPrimitiveSupported(primitiveId)) {
619 return false;
620 }
621 }
622 return true;
623 }
624
625 /**
626 * Query the estimated durations of the given primitives.
627 *
628 * <p>The returned array will be the same length as the query array and the value at a given
629 * index will contain the duration in milliseconds of the effect at the same index in the
630 * querying array.
631 *
632 * <p>The duration will be positive for primitives that are supported and zero for the
633 * unsupported ones, in correspondence with {@link #arePrimitivesSupported(int...)}.
634 *
635 * @param primitiveIds Which primitives to query for.
636 * @return The duration of each primitive, with zeroes for primitives that are not supported.
637 */
638 @NonNull
639 public int[] getPrimitiveDurations(
640 @NonNull @VibrationEffect.Composition.PrimitiveType int... primitiveIds) {
641 VibratorInfo info = getInfo();
642 int[] durations = new int[primitiveIds.length];
643 for (int i = 0; i < primitiveIds.length; i++) {
644 durations[i] = info.getPrimitiveDuration(primitiveIds[i]);
645 }
646 return durations;
647 }
648
649 /**
650 * Turn the vibrator off.
651 */
652 @RequiresPermission(android.Manifest.permission.VIBRATE)
653 public abstract void cancel();
654
655 /**
656 * Cancel specific types of ongoing vibrations.
657 *
658 * @param usageFilter The type of vibration to be cancelled, represented as a bitwise
659 * combination of {@link VibrationAttributes.Usage} values.
660 * @hide
661 */
662 @RequiresPermission(android.Manifest.permission.VIBRATE)
663 public abstract void cancel(int usageFilter);
664
665 /**
666 * Check whether the vibrator is vibrating.
667 *
668 * @return True if the hardware is vibrating, otherwise false.
669 * @hide
670 */
671 @SystemApi
672 @RequiresPermission(android.Manifest.permission.ACCESS_VIBRATOR_STATE)
673 public boolean isVibrating() {
674 return false;
675 }
676
677 /**
678 * Listener for when the vibrator state has changed.
679 *
680 * @see #addVibratorStateListener
681 * @see #removeVibratorStateListener
682 * @hide
683 */
684 @SystemApi
685 public interface OnVibratorStateChangedListener {
686 /**
687 * Called when the vibrator state has changed.
688 *
689 * @param isVibrating If true, the vibrator has started vibrating. If false,
690 * it's stopped vibrating.
691 */
692 void onVibratorStateChanged(boolean isVibrating);
693 }
694
695 /**
696 * Adds a listener for vibrator state changes. Callbacks will be executed on the main thread.
697 * If the listener was previously added and not removed, this call will be ignored.
698 *
699 * @param listener listener to be added
700 * @hide
701 */
702 @SystemApi
703 @RequiresPermission(android.Manifest.permission.ACCESS_VIBRATOR_STATE)
704 public void addVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) {
705 }
706
707 /**
708 * Adds a listener for vibrator state change. If the listener was previously added and not
709 * removed, this call will be ignored.
710 *
711 * @param listener listener to be added
712 * @param executor executor of listener
713 * @hide
714 */
715 @SystemApi
716 @RequiresPermission(android.Manifest.permission.ACCESS_VIBRATOR_STATE)
717 public void addVibratorStateListener(
718 @NonNull @CallbackExecutor Executor executor,
719 @NonNull OnVibratorStateChangedListener listener) {
720 }
721
722 /**
723 * Removes the listener for vibrator state changes. If the listener was not previously
724 * registered, this call will do nothing.
725 *
726 * @param listener listener to be removed
727 * @hide
728 */
729 @SystemApi
730 @RequiresPermission(android.Manifest.permission.ACCESS_VIBRATOR_STATE)
731 public void removeVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) {
732 }
733 }