Debian Codegraph @

core/java/android/os/VibrationAttributes.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.TestApi;
23 import android.media.AudioAttributes;
24
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.util.Objects;
28
29 /**
30 * Encapsulates a collection of attributes describing information about a vibration.
31 */
32 public final class VibrationAttributes implements Parcelable {
33 private static final String TAG = "VibrationAttributes";
34
35 /** @hide */
36 @IntDef(prefix = { "USAGE_CLASS_" }, value = {
37 USAGE_CLASS_UNKNOWN,
38 USAGE_CLASS_ALARM,
39 USAGE_CLASS_FEEDBACK,
40 USAGE_CLASS_MEDIA,
41 })
42 @Retention(RetentionPolicy.SOURCE)
43 public @interface UsageClass {}
44
45 /** @hide */
46 @IntDef(prefix = { "USAGE_" }, value = {
47 USAGE_UNKNOWN,
48 USAGE_ACCESSIBILITY,
49 USAGE_ALARM,
50 USAGE_COMMUNICATION_REQUEST,
51 USAGE_HARDWARE_FEEDBACK,
52 USAGE_MEDIA,
53 USAGE_NOTIFICATION,
54 USAGE_PHYSICAL_EMULATION,
55 USAGE_RINGTONE,
56 USAGE_TOUCH,
57 })
58 @Retention(RetentionPolicy.SOURCE)
59 public @interface Usage {}
60
61 /**
62 * Vibration usage filter value to match all usages.
63 * @hide
64 */
65 public static final int USAGE_FILTER_MATCH_ALL = -1;
66 /**
67 * Vibration usage class value to use when the vibration usage class is unknown.
68 */
69 public static final int USAGE_CLASS_UNKNOWN = 0x0;
70 /**
71 * Vibration usage class value to use when the vibration is initiated to catch user's
72 * attention, such as alarm, ringtone, and notification vibrations.
73 */
74 public static final int USAGE_CLASS_ALARM = 0x1;
75 /**
76 * Vibration usage class value to use when the vibration is initiated as a response to user's
77 * actions, such as emulation of physical effects, and texting feedback vibration.
78 */
79 public static final int USAGE_CLASS_FEEDBACK = 0x2;
80 /**
81 * Vibration usage class value to use when the vibration is part of media, such as music, movie,
82 * soundtrack, game or animations.
83 */
84 public static final int USAGE_CLASS_MEDIA = 0x3;
85
86 /**
87 * Mask for vibration usage class value.
88 */
89 public static final int USAGE_CLASS_MASK = 0xF;
90
91 /**
92 * Usage value to use when usage is unknown.
93 */
94 public static final int USAGE_UNKNOWN = 0x0 | USAGE_CLASS_UNKNOWN;
95 /**
96 * Usage value to use for alarm vibrations.
97 */
98 public static final int USAGE_ALARM = 0x10 | USAGE_CLASS_ALARM;
99 /**
100 * Usage value to use for ringtone vibrations.
101 */
102 public static final int USAGE_RINGTONE = 0x20 | USAGE_CLASS_ALARM;
103 /**
104 * Usage value to use for notification vibrations.
105 */
106 public static final int USAGE_NOTIFICATION = 0x30 | USAGE_CLASS_ALARM;
107 /**
108 * Usage value to use for vibrations which mean a request to enter/end a
109 * communication with the user, such as a voice prompt.
110 */
111 public static final int USAGE_COMMUNICATION_REQUEST = 0x40 | USAGE_CLASS_ALARM;
112 /**
113 * Usage value to use for touch vibrations.
114 *
115 * <p>Most typical haptic feedback should be classed as <em>touch</em> feedback. Examples
116 * include vibrations for tap, long press, drag and scroll.
117 */
118 public static final int USAGE_TOUCH = 0x10 | USAGE_CLASS_FEEDBACK;
119 /**
120 * Usage value to use for vibrations which emulate physical hardware reactions,
121 * such as edge squeeze.
122 *
123 * <p>Note that normal screen-touch feedback "click" effects would typically be
124 * classed as {@link #USAGE_TOUCH}, and that on-screen "physical" animations
125 * like bouncing would be {@link #USAGE_MEDIA}.
126 */
127 public static final int USAGE_PHYSICAL_EMULATION = 0x20 | USAGE_CLASS_FEEDBACK;
128 /**
129 * Usage value to use for vibrations which provide a feedback for hardware
130 * component interaction, such as a fingerprint sensor.
131 */
132 public static final int USAGE_HARDWARE_FEEDBACK = 0x30 | USAGE_CLASS_FEEDBACK;
133 /**
134 * Usage value to use for accessibility vibrations, such as with a screen reader.
135 */
136 public static final int USAGE_ACCESSIBILITY = 0x40 | USAGE_CLASS_FEEDBACK;
137 /**
138 * Usage value to use for media vibrations, such as music, movie, soundtrack, animations, games,
139 * or any interactive media that isn't for touch feedback specifically.
140 */
141 public static final int USAGE_MEDIA = 0x10 | USAGE_CLASS_MEDIA;
142
143 /**
144 * @hide
145 */
146 @IntDef(prefix = { "FLAG_" }, flag = true, value = {
147 FLAG_BYPASS_INTERRUPTION_POLICY,
148 FLAG_BYPASS_USER_VIBRATION_INTENSITY_OFF
149 })
150 @Retention(RetentionPolicy.SOURCE)
151 public @interface Flag{}
152
153 /**
154 * Flag requesting vibration effect to be played even under limited interruptions.
155 *
156 * <p>Only privileged apps can ignore user settings that limit interruptions, and this
157 * flag will be ignored otherwise.
158 */
159 public static final int FLAG_BYPASS_INTERRUPTION_POLICY = 0x1;
160
161 /**
162 * Flag requesting vibration effect to be played even when user settings are disabling it.
163 *
164 * <p>Flag introduced to represent
165 * {@link android.view.HapticFeedbackConstants#FLAG_IGNORE_GLOBAL_SETTING} and
166 * {@link AudioAttributes#FLAG_BYPASS_MUTE}.
167 *
168 * @hide
169 */
170 public static final int FLAG_BYPASS_USER_VIBRATION_INTENSITY_OFF = 0x2;
171
172 /**
173 * Flag requesting vibration effect to be played with fresh user settings values.
174 *
175 * <p>This flag is not protected by any permission, but vibrations that use it require an extra
176 * query of user vibration intensity settings, ringer mode and other controls that affect the
177 * vibration effect playback, which can increase the latency for the overall request.
178 *
179 * <p>This is intended to be used on scenarios where the user settings might have changed
180 * recently, and needs to be applied to this vibration, like settings controllers that preview
181 * newly set intensities to the user.
182 *
183 * @hide
184 */
185 public static final int FLAG_INVALIDATE_SETTINGS_CACHE = 0x3;
186
187 /**
188 * All flags supported by vibrator service, update it when adding new flag.
189 * @hide
190 */
191 public static final int FLAG_ALL_SUPPORTED =
192 FLAG_BYPASS_INTERRUPTION_POLICY | FLAG_BYPASS_USER_VIBRATION_INTENSITY_OFF
193 | FLAG_INVALIDATE_SETTINGS_CACHE;
194
195 /** Creates a new {@link VibrationAttributes} instance with given usage. */
196 public static @NonNull VibrationAttributes createForUsage(@Usage int usage) {
197 return new VibrationAttributes.Builder().setUsage(usage).build();
198 }
199
200 private final int mUsage;
201 private final int mFlags;
202 private final int mOriginalAudioUsage;
203
204 private VibrationAttributes(@Usage int usage, @AudioAttributes.AttributeUsage int audioUsage,
205 @Flag int flags) {
206 mUsage = usage;
207 mOriginalAudioUsage = audioUsage;
208 mFlags = flags & FLAG_ALL_SUPPORTED;
209 }
210
211 /**
212 * Return the vibration usage class.
213 */
214 @UsageClass
215 public int getUsageClass() {
216 return mUsage & USAGE_CLASS_MASK;
217 }
218
219 /**
220 * Return the vibration usage.
221 */
222 @Usage
223 public int getUsage() {
224 return mUsage;
225 }
226
227 /**
228 * Return the flags.
229 * @return a combined mask of all flags
230 */
231 @Flag
232 public int getFlags() {
233 return mFlags;
234 }
235
236 /**
237 * Check whether a flag is set
238 * @return true if a flag is set and false otherwise
239 */
240 public boolean isFlagSet(@Flag int flag) {
241 return (mFlags & flag) > 0;
242 }
243
244 /**
245 * Return {@link AudioAttributes} usage equivalent to {@link #getUsage()}.
246 * @return one of {@link AudioAttributes#SDK_USAGES} that represents {@link #getUsage()}
247 * @hide
248 */
249 @TestApi
250 @AudioAttributes.AttributeUsage
251 public int getAudioUsage() {
252 if (mOriginalAudioUsage != AudioAttributes.USAGE_UNKNOWN) {
253 // Return same audio usage set in the Builder.
254 return mOriginalAudioUsage;
255 }
256 // Return correct audio usage based on the vibration usage set in the Builder.
257 switch (mUsage) {
258 case USAGE_NOTIFICATION:
259 return AudioAttributes.USAGE_NOTIFICATION;
260 case USAGE_COMMUNICATION_REQUEST:
261 return AudioAttributes.USAGE_VOICE_COMMUNICATION;
262 case USAGE_RINGTONE:
263 return AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
264 case USAGE_TOUCH:
265 return AudioAttributes.USAGE_ASSISTANCE_SONIFICATION;
266 case USAGE_ALARM:
267 return AudioAttributes.USAGE_ALARM;
268 case USAGE_ACCESSIBILITY:
269 return AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY;
270 case USAGE_MEDIA:
271 return AudioAttributes.USAGE_MEDIA;
272 default:
273 return AudioAttributes.USAGE_UNKNOWN;
274 }
275 }
276
277 @Override
278 public int describeContents() {
279 return 0;
280 }
281
282 @Override
283 public void writeToParcel(@NonNull Parcel dest, int flags) {
284 dest.writeInt(mUsage);
285 dest.writeInt(mOriginalAudioUsage);
286 dest.writeInt(mFlags);
287 }
288
289 private VibrationAttributes(Parcel src) {
290 mUsage = src.readInt();
291 mOriginalAudioUsage = src.readInt();
292 mFlags = src.readInt();
293 }
294
295 public static final @NonNull Parcelable.Creator<VibrationAttributes>
296 CREATOR = new Parcelable.Creator<VibrationAttributes>() {
297 public VibrationAttributes createFromParcel(Parcel p) {
298 return new VibrationAttributes(p);
299 }
300 public VibrationAttributes[] newArray(int size) {
301 return new VibrationAttributes[size];
302 }
303 };
304
305 @Override
306 public boolean equals(@Nullable Object o) {
307 if (this == o) {
308 return true;
309 }
310 if (o == null || getClass() != o.getClass()) {
311 return false;
312 }
313 VibrationAttributes rhs = (VibrationAttributes) o;
314 return mUsage == rhs.mUsage && mOriginalAudioUsage == rhs.mOriginalAudioUsage
315 && mFlags == rhs.mFlags;
316 }
317
318 @Override
319 public int hashCode() {
320 return Objects.hash(mUsage, mOriginalAudioUsage, mFlags);
321 }
322
323 @Override
324 public String toString() {
325 return "VibrationAttributes:"
326 + " Usage=" + usageToString()
327 + " Audio Usage= " + AudioAttributes.usageToString(mOriginalAudioUsage)
328 + " Flags=" + mFlags;
329 }
330
331 /** @hide */
332 public String usageToString() {
333 return usageToString(mUsage);
334 }
335
336 /** @hide */
337 public static String usageToString(@Usage int usage) {
338 switch (usage) {
339 case USAGE_UNKNOWN:
340 return "UNKNOWN";
341 case USAGE_ALARM:
342 return "ALARM";
343 case USAGE_ACCESSIBILITY:
344 return "ACCESSIBILITY";
345 case USAGE_RINGTONE:
346 return "RINGTONE";
347 case USAGE_NOTIFICATION:
348 return "NOTIFICATION";
349 case USAGE_COMMUNICATION_REQUEST:
350 return "COMMUNICATION_REQUEST";
351 case USAGE_MEDIA:
352 return "MEDIA";
353 case USAGE_TOUCH:
354 return "TOUCH";
355 case USAGE_PHYSICAL_EMULATION:
356 return "PHYSICAL_EMULATION";
357 case USAGE_HARDWARE_FEEDBACK:
358 return "HARDWARE_FEEDBACK";
359 default:
360 return "unknown usage " + usage;
361 }
362 }
363
364 /**
365 * Builder class for {@link VibrationAttributes} objects.
366 * By default, all information is set to UNKNOWN.
367 */
368 public static final class Builder {
369 private int mUsage = USAGE_UNKNOWN;
370 private int mOriginalAudioUsage = AudioAttributes.USAGE_UNKNOWN;
371 private int mFlags = 0x0;
372
373 /**
374 * Constructs a new Builder with the defaults.
375 */
376 public Builder() {
377 }
378
379 /**
380 * Constructs a new Builder from a given VibrationAttributes.
381 */
382 public Builder(@Nullable VibrationAttributes vib) {
383 if (vib != null) {
384 mUsage = vib.mUsage;
385 mOriginalAudioUsage = vib.mOriginalAudioUsage;
386 mFlags = vib.mFlags;
387 }
388 }
389
390 /**
391 * Constructs a new Builder from AudioAttributes.
392 */
393 public Builder(@NonNull AudioAttributes audio) {
394 setUsage(audio);
395 setFlags(audio);
396 }
397
398 private void setUsage(@NonNull AudioAttributes audio) {
399 mOriginalAudioUsage = audio.getUsage();
400 switch (audio.getUsage()) {
401 case AudioAttributes.USAGE_NOTIFICATION:
402 case AudioAttributes.USAGE_NOTIFICATION_EVENT:
403 case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
404 case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
405 case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
406 mUsage = USAGE_NOTIFICATION;
407 break;
408 case AudioAttributes.USAGE_VOICE_COMMUNICATION:
409 case AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING:
410 case AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
411 case AudioAttributes.USAGE_ASSISTANT:
412 mUsage = USAGE_COMMUNICATION_REQUEST;
413 break;
414 case AudioAttributes.USAGE_NOTIFICATION_RINGTONE:
415 mUsage = USAGE_RINGTONE;
416 break;
417 case AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY:
418 mUsage = USAGE_ACCESSIBILITY;
419 break;
420 case AudioAttributes.USAGE_ASSISTANCE_SONIFICATION:
421 mUsage = USAGE_TOUCH;
422 break;
423 case AudioAttributes.USAGE_ALARM:
424 mUsage = USAGE_ALARM;
425 break;
426 case AudioAttributes.USAGE_MEDIA:
427 case AudioAttributes.USAGE_GAME:
428 mUsage = USAGE_MEDIA;
429 break;
430 default:
431 mUsage = USAGE_UNKNOWN;
432 }
433 }
434
435 private void setFlags(@NonNull AudioAttributes audio) {
436 if ((audio.getAllFlags() & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0) {
437 mFlags |= FLAG_BYPASS_INTERRUPTION_POLICY;
438 }
439 if ((audio.getAllFlags() & AudioAttributes.FLAG_BYPASS_MUTE) != 0) {
440 // Muted audio stream translates to vibration usage having the value
441 // Vibrator.VIBRATION_INTENSITY_OFF set in the user setting.
442 mFlags |= FLAG_BYPASS_USER_VIBRATION_INTENSITY_OFF;
443 }
444 }
445
446 /**
447 * Combines all of the attributes that have been set and returns a new
448 * {@link VibrationAttributes} object.
449 * @return a new {@link VibrationAttributes} object
450 */
451 public @NonNull VibrationAttributes build() {
452 VibrationAttributes ans = new VibrationAttributes(mUsage, mOriginalAudioUsage, mFlags);
453 return ans;
454 }
455
456 /**
457 * Sets the attribute describing the type of the corresponding vibration.
458 * @param usage The type of usage for the vibration
459 * @return the same Builder instance.
460 */
461 public @NonNull Builder setUsage(@Usage int usage) {
462 mOriginalAudioUsage = AudioAttributes.USAGE_UNKNOWN;
463 mUsage = usage;
464 return this;
465 }
466
467 /**
468 * Sets only the flags specified in the bitmask, leaving the other supported flag values
469 * unchanged in the builder.
470 *
471 * @param flags Combination of flags to be set.
472 * @param mask Bit range that should be changed.
473 * @return the same Builder instance.
474 */
475 public @NonNull Builder setFlags(@Flag int flags, int mask) {
476 mask &= FLAG_ALL_SUPPORTED;
477 mFlags = (mFlags & ~mask) | (flags & mask);
478 return this;
479 }
480
481 /**
482 * Set all supported flags with given combination of flags, overriding any previous values
483 * set to this builder.
484 *
485 * @param flags combination of flags to be set.
486 * @return the same Builder instance.
487 *
488 * @hide
489 */
490 public @NonNull Builder setFlags(@Flag int flags) {
491 return setFlags(flags, FLAG_ALL_SUPPORTED);
492 }
493 }
494 }