1 /* 2 * Copyright (C) 2014 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.media; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.annotation.UnsupportedAppUsage; 23 import android.media.audiopolicy.AudioProductStrategy; 24 import android.os.Build; 25 import android.os.Bundle; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 import android.text.TextUtils; 29 import android.util.Log; 30 import android.util.SparseIntArray; 31 import android.util.proto.ProtoOutputStream; 32 33 import java.lang.annotation.Retention; 34 import java.lang.annotation.RetentionPolicy; 35 import java.util.Collections; 36 import java.util.HashSet; 37 import java.util.Objects; 38 import java.util.Set; 39 40 /** 41 * A class to encapsulate a collection of attributes describing information about an audio 42 * stream. 43 * <p><code>AudioAttributes</code> supersede the notion of stream types (see for instance 44 * {@link AudioManager#STREAM_MUSIC} or {@link AudioManager#STREAM_ALARM}) for defining the 45 * behavior of audio playback. Attributes allow an application to specify more information than is 46 * conveyed in a stream type by allowing the application to define: 47 * <ul> 48 * <li>usage: "why" you are playing a sound, what is this sound used for. This is achieved with 49 * the "usage" information. Examples of usage are {@link #USAGE_MEDIA} and {@link #USAGE_ALARM}. 50 * These two examples are the closest to stream types, but more detailed use cases are 51 * available. Usage information is more expressive than a stream type, and allows certain 52 * platforms or routing policies to use this information for more refined volume or routing 53 * decisions. Usage is the most important information to supply in <code>AudioAttributes</code> 54 * and it is recommended to build any instance with this information supplied, see 55 * {@link AudioAttributes.Builder} for exceptions.</li> 56 * <li>content type: "what" you are playing. The content type expresses the general category of 57 * the content. This information is optional. But in case it is known (for instance 58 * {@link #CONTENT_TYPE_MOVIE} for a movie streaming service or {@link #CONTENT_TYPE_MUSIC} for 59 * a music playback application) this information might be used by the audio framework to 60 * selectively configure some audio post-processing blocks.</li> 61 * <li>flags: "how" is playback to be affected, see the flag definitions for the specific playback 62 * behaviors they control. </li> 63 * </ul> 64 * <p><code>AudioAttributes</code> are used for example in one of the {@link AudioTrack} 65 * constructors (see {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}), 66 * to configure a {@link MediaPlayer} 67 * (see {@link MediaPlayer#setAudioAttributes(AudioAttributes)} or a 68 * {@link android.app.Notification} (see {@link android.app.Notification#audioAttributes}). An 69 * <code>AudioAttributes</code> instance is built through its builder, 70 * {@link AudioAttributes.Builder}. 71 */ 72 public final class AudioAttributes implements Parcelable { 73 private final static String TAG = "AudioAttributes"; 74 75 /** 76 * Content type value to use when the content type is unknown, or other than the ones defined. 77 */ 78 public final static int CONTENT_TYPE_UNKNOWN = 0; 79 /** 80 * Content type value to use when the content type is speech. 81 */ 82 public final static int CONTENT_TYPE_SPEECH = 1; 83 /** 84 * Content type value to use when the content type is music. 85 */ 86 public final static int CONTENT_TYPE_MUSIC = 2; 87 /** 88 * Content type value to use when the content type is a soundtrack, typically accompanying 89 * a movie or TV program. 90 */ 91 public final static int CONTENT_TYPE_MOVIE = 3; 92 /** 93 * Content type value to use when the content type is a sound used to accompany a user 94 * action, such as a beep or sound effect expressing a key click, or event, such as the 95 * type of a sound for a bonus being received in a game. These sounds are mostly synthesized 96 * or short Foley sounds. 97 */ 98 public final static int CONTENT_TYPE_SONIFICATION = 4; 99 100 /** 101 * Usage value to use when the usage is unknown. 102 */ 103 public final static int USAGE_UNKNOWN = 0; 104 /** 105 * Usage value to use when the usage is media, such as music, or movie 106 * soundtracks. 107 */ 108 public final static int USAGE_MEDIA = 1; 109 /** 110 * Usage value to use when the usage is voice communications, such as telephony 111 * or VoIP. 112 */ 113 public final static int USAGE_VOICE_COMMUNICATION = 2; 114 /** 115 * Usage value to use when the usage is in-call signalling, such as with 116 * a "busy" beep, or DTMF tones. 117 */ 118 public final static int USAGE_VOICE_COMMUNICATION_SIGNALLING = 3; 119 /** 120 * Usage value to use when the usage is an alarm (e.g. wake-up alarm). 121 */ 122 public final static int USAGE_ALARM = 4; 123 /** 124 * Usage value to use when the usage is notification. See other 125 * notification usages for more specialized uses. 126 */ 127 public final static int USAGE_NOTIFICATION = 5; 128 /** 129 * Usage value to use when the usage is telephony ringtone. 130 */ 131 public final static int USAGE_NOTIFICATION_RINGTONE = 6; 132 /** 133 * Usage value to use when the usage is a request to enter/end a 134 * communication, such as a VoIP communication or video-conference. 135 */ 136 public final static int USAGE_NOTIFICATION_COMMUNICATION_REQUEST = 7; 137 /** 138 * Usage value to use when the usage is notification for an "instant" 139 * communication such as a chat, or SMS. 140 */ 141 public final static int USAGE_NOTIFICATION_COMMUNICATION_INSTANT = 8; 142 /** 143 * Usage value to use when the usage is notification for a 144 * non-immediate type of communication such as e-mail. 145 */ 146 public final static int USAGE_NOTIFICATION_COMMUNICATION_DELAYED = 9; 147 /** 148 * Usage value to use when the usage is to attract the user's attention, 149 * such as a reminder or low battery warning. 150 */ 151 public final static int USAGE_NOTIFICATION_EVENT = 10; 152 /** 153 * Usage value to use when the usage is for accessibility, such as with 154 * a screen reader. 155 */ 156 public final static int USAGE_ASSISTANCE_ACCESSIBILITY = 11; 157 /** 158 * Usage value to use when the usage is driving or navigation directions. 159 */ 160 public final static int USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12; 161 /** 162 * Usage value to use when the usage is sonification, such as with user 163 * interface sounds. 164 */ 165 public final static int USAGE_ASSISTANCE_SONIFICATION = 13; 166 /** 167 * Usage value to use when the usage is for game audio. 168 */ 169 public final static int USAGE_GAME = 14; 170 /** 171 * @hide 172 * Usage value to use when feeding audio to the platform and replacing "traditional" audio 173 * source, such as audio capture devices. 174 */ 175 public final static int USAGE_VIRTUAL_SOURCE = 15; 176 /** 177 * Usage value to use for audio responses to user queries, audio instructions or help 178 * utterances. 179 */ 180 public final static int USAGE_ASSISTANT = 16; 181 182 /** 183 * IMPORTANT: when adding new usage types, add them to SDK_USAGES and update SUPPRESSIBLE_USAGES 184 * if applicable, as well as audioattributes.proto. 185 * Also consider adding them to <aaudio/AAudio.h> for the NDK. 186 * Also consider adding them to UsageTypeConverter for service dump and etc. 187 */ 188 189 /** 190 * @hide 191 * Denotes a usage for notifications that do not expect immediate intervention from the user, 192 * will be muted when the Zen mode disables notifications 193 * @see #SUPPRESSIBLE_USAGES 194 */ 195 public final static int SUPPRESSIBLE_NOTIFICATION = 1; 196 /** 197 * @hide 198 * Denotes a usage for notifications that do expect immediate intervention from the user, 199 * will be muted when the Zen mode disables calls 200 * @see #SUPPRESSIBLE_USAGES 201 */ 202 public final static int SUPPRESSIBLE_CALL = 2; 203 /** 204 * @hide 205 * Denotes a usage that is never going to be muted, even in Total Silence. 206 * @see #SUPPRESSIBLE_USAGES 207 */ 208 public final static int SUPPRESSIBLE_NEVER = 3; 209 /** 210 * @hide 211 * Denotes a usage for alarms, 212 * will be muted when the Zen mode priority doesn't allow alarms or in Alarms Only Mode 213 * @see #SUPPRESSIBLE_USAGES 214 */ 215 public final static int SUPPRESSIBLE_ALARM = 4; 216 /** 217 * @hide 218 * Denotes a usage for media, game, assistant, and navigation 219 * will be muted when the Zen priority mode doesn't allow media 220 * @see #SUPPRESSIBLE_USAGES 221 */ 222 public final static int SUPPRESSIBLE_MEDIA = 5; 223 /** 224 * @hide 225 * Denotes a usage for sounds not caught in SUPPRESSIBLE_NOTIFICATION, 226 * SUPPRESSIBLE_CALL,SUPPRESSIBLE_NEVER, SUPPRESSIBLE_ALARM or SUPPRESSIBLE_MEDIA. 227 * This includes sonification sounds. 228 * These will be muted when the Zen priority mode doesn't allow system sounds 229 * @see #SUPPRESSIBLE_USAGES 230 */ 231 public final static int SUPPRESSIBLE_SYSTEM = 6; 232 233 /** 234 * @hide 235 * Array of all usage types for calls and notifications to assign the suppression behavior, 236 * used by the Zen mode restrictions. 237 * @see com.android.server.notification.ZenModeHelper 238 */ 239 public static final SparseIntArray SUPPRESSIBLE_USAGES; 240 241 static { 242 SUPPRESSIBLE_USAGES = new SparseIntArray(); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION, SUPPRESSIBLE_NOTIFICATION)243 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION, SUPPRESSIBLE_NOTIFICATION); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_RINGTONE, SUPPRESSIBLE_CALL)244 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_RINGTONE, SUPPRESSIBLE_CALL); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_REQUEST,SUPPRESSIBLE_CALL)245 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_REQUEST,SUPPRESSIBLE_CALL); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_INSTANT,SUPPRESSIBLE_NOTIFICATION)246 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_INSTANT,SUPPRESSIBLE_NOTIFICATION); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_DELAYED,SUPPRESSIBLE_NOTIFICATION)247 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_DELAYED,SUPPRESSIBLE_NOTIFICATION); SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_EVENT, SUPPRESSIBLE_NOTIFICATION)248 SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_EVENT, SUPPRESSIBLE_NOTIFICATION); SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_ACCESSIBILITY, SUPPRESSIBLE_NEVER)249 SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_ACCESSIBILITY, SUPPRESSIBLE_NEVER); SUPPRESSIBLE_USAGES.put(USAGE_VOICE_COMMUNICATION, SUPPRESSIBLE_NEVER)250 SUPPRESSIBLE_USAGES.put(USAGE_VOICE_COMMUNICATION, SUPPRESSIBLE_NEVER); SUPPRESSIBLE_USAGES.put(USAGE_VOICE_COMMUNICATION_SIGNALLING, SUPPRESSIBLE_NEVER)251 SUPPRESSIBLE_USAGES.put(USAGE_VOICE_COMMUNICATION_SIGNALLING, SUPPRESSIBLE_NEVER); SUPPRESSIBLE_USAGES.put(USAGE_ALARM, SUPPRESSIBLE_ALARM)252 SUPPRESSIBLE_USAGES.put(USAGE_ALARM, SUPPRESSIBLE_ALARM); SUPPRESSIBLE_USAGES.put(USAGE_MEDIA, SUPPRESSIBLE_MEDIA)253 SUPPRESSIBLE_USAGES.put(USAGE_MEDIA, SUPPRESSIBLE_MEDIA); SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, SUPPRESSIBLE_MEDIA)254 SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, SUPPRESSIBLE_MEDIA); SUPPRESSIBLE_USAGES.put(USAGE_GAME, SUPPRESSIBLE_MEDIA)255 SUPPRESSIBLE_USAGES.put(USAGE_GAME, SUPPRESSIBLE_MEDIA); SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANT, SUPPRESSIBLE_MEDIA)256 SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANT, SUPPRESSIBLE_MEDIA); 257 /** default volume assignment is STREAM_MUSIC, handle unknown usage as media */ SUPPRESSIBLE_USAGES.put(USAGE_UNKNOWN, SUPPRESSIBLE_MEDIA)258 SUPPRESSIBLE_USAGES.put(USAGE_UNKNOWN, SUPPRESSIBLE_MEDIA); SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_SONIFICATION, SUPPRESSIBLE_SYSTEM)259 SUPPRESSIBLE_USAGES.put(USAGE_ASSISTANCE_SONIFICATION, SUPPRESSIBLE_SYSTEM); 260 } 261 262 /** 263 * @hide 264 * Array of all usage types exposed in the SDK that applications can use. 265 */ 266 public final static int[] SDK_USAGES = { 267 USAGE_UNKNOWN, 268 USAGE_MEDIA, 269 USAGE_VOICE_COMMUNICATION, 270 USAGE_VOICE_COMMUNICATION_SIGNALLING, 271 USAGE_ALARM, 272 USAGE_NOTIFICATION, 273 USAGE_NOTIFICATION_RINGTONE, 274 USAGE_NOTIFICATION_COMMUNICATION_REQUEST, 275 USAGE_NOTIFICATION_COMMUNICATION_INSTANT, 276 USAGE_NOTIFICATION_COMMUNICATION_DELAYED, 277 USAGE_NOTIFICATION_EVENT, 278 USAGE_ASSISTANCE_ACCESSIBILITY, 279 USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, 280 USAGE_ASSISTANCE_SONIFICATION, 281 USAGE_GAME, 282 USAGE_ASSISTANT, 283 }; 284 285 /** 286 * Flag defining a behavior where the audibility of the sound will be ensured by the system. 287 */ 288 public final static int FLAG_AUDIBILITY_ENFORCED = 0x1 << 0; 289 /** 290 * @hide 291 * Flag defining a behavior where the playback of the sound is ensured without 292 * degradation only when going to a secure sink. 293 */ 294 // FIXME not guaranteed yet 295 // TODO add in FLAG_ALL_PUBLIC when supported and in public API 296 public final static int FLAG_SECURE = 0x1 << 1; 297 /** 298 * @hide 299 * Flag to enable when the stream is associated with SCO usage. 300 * Internal use only for dealing with legacy STREAM_BLUETOOTH_SCO 301 */ 302 public final static int FLAG_SCO = 0x1 << 2; 303 /** 304 * @hide 305 * Flag defining a behavior where the system ensures that the playback of the sound will 306 * be compatible with its use as a broadcast for surrounding people and/or devices. 307 * Ensures audibility with no or minimal post-processing applied. 308 */ 309 @SystemApi 310 public final static int FLAG_BEACON = 0x1 << 3; 311 312 /** 313 * Flag requesting the use of an output stream supporting hardware A/V synchronization. 314 */ 315 public final static int FLAG_HW_AV_SYNC = 0x1 << 4; 316 317 /** 318 * @hide 319 * Flag requesting capture from the source used for hardware hotword detection. 320 * To be used with capture preset MediaRecorder.AudioSource.HOTWORD or 321 * MediaRecorder.AudioSource.VOICE_RECOGNITION. 322 */ 323 @SystemApi 324 public final static int FLAG_HW_HOTWORD = 0x1 << 5; 325 326 /** 327 * @hide 328 * Flag requesting audible playback even under limited interruptions. 329 */ 330 @SystemApi 331 public final static int FLAG_BYPASS_INTERRUPTION_POLICY = 0x1 << 6; 332 333 /** 334 * @hide 335 * Flag requesting audible playback even when the underlying stream is muted. 336 */ 337 @SystemApi 338 public final static int FLAG_BYPASS_MUTE = 0x1 << 7; 339 340 /** 341 * Flag requesting a low latency path when creating an AudioTrack. 342 * When using this flag, the sample rate must match the native sample rate 343 * of the device. Effects processing is also unavailable. 344 * 345 * Note that if this flag is used without specifying a bufferSizeInBytes then the 346 * AudioTrack's actual buffer size may be too small. It is recommended that a fairly 347 * large buffer should be specified when the AudioTrack is created. 348 * Then the actual size can be reduced by calling 349 * {@link AudioTrack#setBufferSizeInFrames(int)}. The buffer size can be optimized 350 * by lowering it after each write() call until the audio glitches, which is detected by calling 351 * {@link AudioTrack#getUnderrunCount()}. Then the buffer size can be increased 352 * until there are no glitches. 353 * This tuning step should be done while playing silence. 354 * This technique provides a compromise between latency and glitch rate. 355 * 356 * @deprecated Use {@link AudioTrack.Builder#setPerformanceMode(int)} with 357 * {@link AudioTrack#PERFORMANCE_MODE_LOW_LATENCY} to control performance. 358 */ 359 public final static int FLAG_LOW_LATENCY = 0x1 << 8; 360 361 /** 362 * @hide 363 * Flag requesting a deep buffer path when creating an {@code AudioTrack}. 364 * 365 * A deep buffer path, if available, may consume less power and is 366 * suitable for media playback where latency is not a concern. 367 * Use {@link AudioTrack.Builder#setPerformanceMode(int)} with 368 * {@link AudioTrack#PERFORMANCE_MODE_POWER_SAVING} to enable. 369 */ 370 public final static int FLAG_DEEP_BUFFER = 0x1 << 9; 371 372 /** 373 * @hide 374 * Flag specifying that the audio shall not be captured by third-party apps 375 * with a MediaProjection. 376 */ 377 public static final int FLAG_NO_MEDIA_PROJECTION = 0x1 << 10; 378 379 /** 380 * @hide 381 * Flag indicating force muting haptic channels. 382 */ 383 public static final int FLAG_MUTE_HAPTIC = 0x1 << 11; 384 385 /** 386 * @hide 387 * Flag specifying that the audio shall not be captured by any apps, not even system apps. 388 */ 389 public static final int FLAG_NO_SYSTEM_CAPTURE = 0x1 << 12; 390 391 // Note that even though FLAG_MUTE_HAPTIC is stored as a flag bit, it is not here since 392 // it is known as a boolean value outside of AudioAttributes. 393 private static final int FLAG_ALL = FLAG_AUDIBILITY_ENFORCED | FLAG_SECURE | FLAG_SCO 394 | FLAG_BEACON | FLAG_HW_AV_SYNC | FLAG_HW_HOTWORD | FLAG_BYPASS_INTERRUPTION_POLICY 395 | FLAG_BYPASS_MUTE | FLAG_LOW_LATENCY | FLAG_DEEP_BUFFER | FLAG_NO_MEDIA_PROJECTION 396 | FLAG_NO_SYSTEM_CAPTURE; 397 private final static int FLAG_ALL_PUBLIC = FLAG_AUDIBILITY_ENFORCED | 398 FLAG_HW_AV_SYNC | FLAG_LOW_LATENCY; 399 400 /** 401 * Indicates that the audio may be captured by any app. 402 * 403 * For privacy, the following usages cannot be recorded: VOICE_COMMUNICATION*, 404 * USAGE_NOTIFICATION*, USAGE_ASSISTANCE* and USAGE_ASSISTANT. 405 * 406 * On {@link android.os.Build.VERSION_CODES#Q}, this means only {@link #USAGE_UNKNOWN}, 407 * {@link #USAGE_MEDIA} and {@link #USAGE_GAME} may be captured. 408 * 409 * See {@link android.media.projection.MediaProjection} and 410 * {@link Builder#setAllowedCapturePolicy}. 411 */ 412 public static final int ALLOW_CAPTURE_BY_ALL = 1; 413 /** 414 * Indicates that the audio may only be captured by system apps. 415 * 416 * System apps can capture for many purposes like accessibility, live captions, user guidance... 417 * but abide to the following restrictions: 418 * - the audio cannot leave the device 419 * - the audio cannot be passed to a third party app 420 * - the audio cannot be recorded at a higher quality than 16kHz 16bit mono 421 * 422 * See {@link Builder#setAllowedCapturePolicy}. 423 */ 424 public static final int ALLOW_CAPTURE_BY_SYSTEM = 2; 425 /** 426 * Indicates that the audio is not to be recorded by any app, even if it is a system app. 427 * 428 * It is encouraged to use {@link #ALLOW_CAPTURE_BY_SYSTEM} instead of this value as system apps 429 * provide significant and useful features for the user (such as live captioning 430 * and accessibility). 431 * 432 * See {@link Builder#setAllowedCapturePolicy}. 433 */ 434 public static final int ALLOW_CAPTURE_BY_NONE = 3; 435 436 /** @hide */ 437 @IntDef({ 438 ALLOW_CAPTURE_BY_ALL, 439 ALLOW_CAPTURE_BY_SYSTEM, 440 ALLOW_CAPTURE_BY_NONE, 441 }) 442 @Retention(RetentionPolicy.SOURCE) 443 public @interface CapturePolicy {} 444 445 @UnsupportedAppUsage 446 private int mUsage = USAGE_UNKNOWN; 447 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 448 private int mContentType = CONTENT_TYPE_UNKNOWN; 449 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 450 private int mSource = MediaRecorder.AudioSource.AUDIO_SOURCE_INVALID; 451 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 452 private int mFlags = 0x0; 453 private HashSet<String> mTags; 454 @UnsupportedAppUsage 455 private String mFormattedTags; 456 private Bundle mBundle; // lazy-initialized, may be null 457 AudioAttributes()458 private AudioAttributes() { 459 } 460 461 /** 462 * Return the content type. 463 * @return one of the values that can be set in {@link Builder#setContentType(int)} 464 */ getContentType()465 public int getContentType() { 466 return mContentType; 467 } 468 469 /** 470 * Return the usage. 471 * @return one of the values that can be set in {@link Builder#setUsage(int)} 472 */ getUsage()473 public int getUsage() { 474 return mUsage; 475 } 476 477 /** 478 * @hide 479 * Return the capture preset. 480 * @return one of the values that can be set in {@link Builder#setCapturePreset(int)} or a 481 * negative value if none has been set. 482 */ 483 @SystemApi getCapturePreset()484 public int getCapturePreset() { 485 return mSource; 486 } 487 488 /** 489 * Return the flags. 490 * @return a combined mask of all flags 491 */ getFlags()492 public int getFlags() { 493 // only return the flags that are public 494 return (mFlags & (FLAG_ALL_PUBLIC)); 495 } 496 497 /** 498 * @hide 499 * Return all the flags, even the non-public ones. 500 * Internal use only 501 * @return a combined mask of all flags 502 */ 503 @SystemApi getAllFlags()504 public int getAllFlags() { 505 return (mFlags & FLAG_ALL); 506 } 507 508 /** 509 * @hide 510 * Return the Bundle of data. 511 * @return a copy of the Bundle for this instance, may be null. 512 */ 513 @SystemApi getBundle()514 public Bundle getBundle() { 515 if (mBundle == null) { 516 return mBundle; 517 } else { 518 return new Bundle(mBundle); 519 } 520 } 521 522 /** 523 * @hide 524 * Return the set of tags. 525 * @return a read-only set of all tags stored as strings. 526 */ getTags()527 public Set<String> getTags() { 528 return Collections.unmodifiableSet(mTags); 529 } 530 531 /** 532 * Return if haptic channels are muted. 533 * @return {@code true} if haptic channels are muted, {@code false} otherwise. 534 */ areHapticChannelsMuted()535 public boolean areHapticChannelsMuted() { 536 return (mFlags & FLAG_MUTE_HAPTIC) != 0; 537 } 538 539 /** 540 * Return the capture policy. 541 * @return the capture policy set by {@link Builder#setAllowedCapturePolicy(int)} or 542 * the default if it was not called. 543 */ 544 @CapturePolicy getAllowedCapturePolicy()545 public int getAllowedCapturePolicy() { 546 if ((mFlags & FLAG_NO_SYSTEM_CAPTURE) == FLAG_NO_SYSTEM_CAPTURE) { 547 return ALLOW_CAPTURE_BY_NONE; 548 } 549 if ((mFlags & FLAG_NO_MEDIA_PROJECTION) == FLAG_NO_MEDIA_PROJECTION) { 550 return ALLOW_CAPTURE_BY_SYSTEM; 551 } 552 return ALLOW_CAPTURE_BY_ALL; 553 } 554 555 556 /** 557 * Builder class for {@link AudioAttributes} objects. 558 * <p> Here is an example where <code>Builder</code> is used to define the 559 * {@link AudioAttributes} to be used by a new <code>AudioTrack</code> instance: 560 * 561 * <pre class="prettyprint"> 562 * AudioTrack myTrack = new AudioTrack( 563 * new AudioAttributes.Builder() 564 * .setUsage(AudioAttributes.USAGE_MEDIA) 565 * .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 566 * .build(), 567 * myFormat, myBuffSize, AudioTrack.MODE_STREAM, mySession); 568 * </pre> 569 * 570 * <p>By default all types of information (usage, content type, flags) conveyed by an 571 * <code>AudioAttributes</code> instance are set to "unknown". Unknown information will be 572 * interpreted as a default value that is dependent on the context of use, for instance a 573 * {@link MediaPlayer} will use a default usage of {@link AudioAttributes#USAGE_MEDIA}. 574 */ 575 public static class Builder { 576 private int mUsage = USAGE_UNKNOWN; 577 private int mContentType = CONTENT_TYPE_UNKNOWN; 578 private int mSource = MediaRecorder.AudioSource.AUDIO_SOURCE_INVALID; 579 private int mFlags = 0x0; 580 private boolean mMuteHapticChannels = true; 581 private HashSet<String> mTags = new HashSet<String>(); 582 private Bundle mBundle; 583 584 /** 585 * Constructs a new Builder with the defaults. 586 * By default, usage and content type are respectively {@link AudioAttributes#USAGE_UNKNOWN} 587 * and {@link AudioAttributes#CONTENT_TYPE_UNKNOWN}, and flags are 0. It is recommended to 588 * configure the usage (with {@link #setUsage(int)}) or deriving attributes from a legacy 589 * stream type (with {@link #setLegacyStreamType(int)}) before calling {@link #build()} 590 * to override any default playback behavior in terms of routing and volume management. 591 */ Builder()592 public Builder() { 593 } 594 595 /** 596 * Constructs a new Builder from a given AudioAttributes 597 * @param aa the AudioAttributes object whose data will be reused in the new Builder. 598 */ 599 @SuppressWarnings("unchecked") // for cloning of mTags Builder(AudioAttributes aa)600 public Builder(AudioAttributes aa) { 601 mUsage = aa.mUsage; 602 mContentType = aa.mContentType; 603 mFlags = aa.getAllFlags(); 604 mTags = (HashSet<String>) aa.mTags.clone(); 605 mMuteHapticChannels = aa.areHapticChannelsMuted(); 606 } 607 608 /** 609 * Combines all of the attributes that have been set and return a new 610 * {@link AudioAttributes} object. 611 * @return a new {@link AudioAttributes} object 612 */ 613 @SuppressWarnings("unchecked") // for cloning of mTags build()614 public AudioAttributes build() { 615 AudioAttributes aa = new AudioAttributes(); 616 aa.mContentType = mContentType; 617 aa.mUsage = mUsage; 618 aa.mSource = mSource; 619 aa.mFlags = mFlags; 620 if (mMuteHapticChannels) { 621 aa.mFlags |= FLAG_MUTE_HAPTIC; 622 } 623 aa.mTags = (HashSet<String>) mTags.clone(); 624 aa.mFormattedTags = TextUtils.join(";", mTags); 625 if (mBundle != null) { 626 aa.mBundle = new Bundle(mBundle); 627 } 628 return aa; 629 } 630 631 /** 632 * Sets the attribute describing what is the intended use of the the audio signal, 633 * such as alarm or ringtone. 634 * @param usage one of {@link AudioAttributes#USAGE_UNKNOWN}, 635 * {@link AudioAttributes#USAGE_MEDIA}, 636 * {@link AudioAttributes#USAGE_VOICE_COMMUNICATION}, 637 * {@link AudioAttributes#USAGE_VOICE_COMMUNICATION_SIGNALLING}, 638 * {@link AudioAttributes#USAGE_ALARM}, {@link AudioAttributes#USAGE_NOTIFICATION}, 639 * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE}, 640 * {@link AudioAttributes#USAGE_NOTIFICATION_COMMUNICATION_REQUEST}, 641 * {@link AudioAttributes#USAGE_NOTIFICATION_COMMUNICATION_INSTANT}, 642 * {@link AudioAttributes#USAGE_NOTIFICATION_COMMUNICATION_DELAYED}, 643 * {@link AudioAttributes#USAGE_NOTIFICATION_EVENT}, 644 * {@link AudioAttributes#USAGE_ASSISTANT}, 645 * {@link AudioAttributes#USAGE_ASSISTANCE_ACCESSIBILITY}, 646 * {@link AudioAttributes#USAGE_ASSISTANCE_NAVIGATION_GUIDANCE}, 647 * {@link AudioAttributes#USAGE_ASSISTANCE_SONIFICATION}, 648 * {@link AudioAttributes#USAGE_GAME}. 649 * @return the same Builder instance. 650 */ setUsage(@ttributeUsage int usage)651 public Builder setUsage(@AttributeUsage int usage) { 652 switch (usage) { 653 case USAGE_UNKNOWN: 654 case USAGE_MEDIA: 655 case USAGE_VOICE_COMMUNICATION: 656 case USAGE_VOICE_COMMUNICATION_SIGNALLING: 657 case USAGE_ALARM: 658 case USAGE_NOTIFICATION: 659 case USAGE_NOTIFICATION_RINGTONE: 660 case USAGE_NOTIFICATION_COMMUNICATION_REQUEST: 661 case USAGE_NOTIFICATION_COMMUNICATION_INSTANT: 662 case USAGE_NOTIFICATION_COMMUNICATION_DELAYED: 663 case USAGE_NOTIFICATION_EVENT: 664 case USAGE_ASSISTANCE_ACCESSIBILITY: 665 case USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: 666 case USAGE_ASSISTANCE_SONIFICATION: 667 case USAGE_GAME: 668 case USAGE_VIRTUAL_SOURCE: 669 case USAGE_ASSISTANT: 670 mUsage = usage; 671 break; 672 default: 673 mUsage = USAGE_UNKNOWN; 674 } 675 return this; 676 } 677 678 /** 679 * Sets the attribute describing the content type of the audio signal, such as speech, 680 * or music. 681 * @param contentType the content type values, one of 682 * {@link AudioAttributes#CONTENT_TYPE_MOVIE}, 683 * {@link AudioAttributes#CONTENT_TYPE_MUSIC}, 684 * {@link AudioAttributes#CONTENT_TYPE_SONIFICATION}, 685 * {@link AudioAttributes#CONTENT_TYPE_SPEECH}, 686 * {@link AudioAttributes#CONTENT_TYPE_UNKNOWN}. 687 * @return the same Builder instance. 688 */ setContentType(@ttributeContentType int contentType)689 public Builder setContentType(@AttributeContentType int contentType) { 690 switch (contentType) { 691 case CONTENT_TYPE_UNKNOWN: 692 case CONTENT_TYPE_MOVIE: 693 case CONTENT_TYPE_MUSIC: 694 case CONTENT_TYPE_SONIFICATION: 695 case CONTENT_TYPE_SPEECH: 696 mContentType = contentType; 697 break; 698 default: 699 mContentType = CONTENT_TYPE_UNKNOWN; 700 } 701 return this; 702 } 703 704 /** 705 * Sets the combination of flags. 706 * 707 * This is a bitwise OR with the existing flags. 708 * @param flags a combination of {@link AudioAttributes#FLAG_AUDIBILITY_ENFORCED}, 709 * {@link AudioAttributes#FLAG_HW_AV_SYNC}. 710 * @return the same Builder instance. 711 */ setFlags(int flags)712 public Builder setFlags(int flags) { 713 flags &= AudioAttributes.FLAG_ALL; 714 mFlags |= flags; 715 return this; 716 } 717 718 /** 719 * Specifies whether the audio may or may not be captured by other apps or the system. 720 * 721 * The default is {@link AudioAttributes#ALLOW_CAPTURE_BY_ALL}. 722 * 723 * There are multiple ways to set this policy: 724 * <ul> 725 * <li> for each track independently, with this method </li> 726 * <li> application-wide at runtime, with 727 * {@link AudioManager#setAllowedCapturePolicy(int)} </li> 728 * <li> application-wide at build time, see {@code allowAudioPlaybackCapture} in the 729 * application manifest. </li> 730 * </ul> 731 * The most restrictive policy is always applied. 732 * 733 * See {@link AudioPlaybackCaptureConfiguration} for more details on 734 * which audio signals can be captured. 735 * 736 * @return the same Builder instance 737 * @throws IllegalArgumentException if the argument is not a valid value. 738 */ setAllowedCapturePolicy(@apturePolicy int capturePolicy)739 public @NonNull Builder setAllowedCapturePolicy(@CapturePolicy int capturePolicy) { 740 mFlags = capturePolicyToFlags(capturePolicy, mFlags); 741 return this; 742 } 743 744 /** 745 * @hide 746 * Replaces flags. 747 * @param flags any combination of {@link AudioAttributes#FLAG_ALL}. 748 * @return the same Builder instance. 749 */ replaceFlags(int flags)750 public Builder replaceFlags(int flags) { 751 mFlags = flags & AudioAttributes.FLAG_ALL; 752 return this; 753 } 754 755 /** 756 * @hide 757 * Adds a Bundle of data 758 * @param bundle a non-null Bundle 759 * @return the same builder instance 760 */ 761 @SystemApi addBundle(@onNull Bundle bundle)762 public Builder addBundle(@NonNull Bundle bundle) { 763 if (bundle == null) { 764 throw new IllegalArgumentException("Illegal null bundle"); 765 } 766 if (mBundle == null) { 767 mBundle = new Bundle(bundle); 768 } else { 769 mBundle.putAll(bundle); 770 } 771 return this; 772 } 773 774 /** 775 * @hide 776 * Add a custom tag stored as a string 777 * @param tag 778 * @return the same Builder instance. 779 */ 780 @UnsupportedAppUsage addTag(String tag)781 public Builder addTag(String tag) { 782 mTags.add(tag); 783 return this; 784 } 785 786 /** 787 * Sets attributes as inferred from the legacy stream types. 788 * Warning: do not use this method in combination with setting any other attributes such as 789 * usage, content type, flags or haptic control, as this method will overwrite (the more 790 * accurate) information describing the use case previously set in the <code>Builder</code>. 791 * In general, avoid using it and prefer setting usage and content type directly 792 * with {@link #setUsage(int)} and {@link #setContentType(int)}. 793 * <p>Use this method when building an {@link AudioAttributes} instance to initialize some 794 * of the attributes by information derived from a legacy stream type. 795 * @param streamType one of {@link AudioManager#STREAM_VOICE_CALL}, 796 * {@link AudioManager#STREAM_SYSTEM}, {@link AudioManager#STREAM_RING}, 797 * {@link AudioManager#STREAM_MUSIC}, {@link AudioManager#STREAM_ALARM}, 798 * or {@link AudioManager#STREAM_NOTIFICATION}. 799 * @return the same Builder instance. 800 */ setLegacyStreamType(int streamType)801 public Builder setLegacyStreamType(int streamType) { 802 if (streamType == AudioManager.STREAM_ACCESSIBILITY) { 803 throw new IllegalArgumentException("STREAM_ACCESSIBILITY is not a legacy stream " 804 + "type that was used for audio playback"); 805 } 806 setInternalLegacyStreamType(streamType); 807 return this; 808 } 809 810 /** 811 * @hide 812 * For internal framework use only, enables building from hidden stream types. 813 * @param streamType 814 * @return the same Builder instance. 815 */ 816 @UnsupportedAppUsage setInternalLegacyStreamType(int streamType)817 public Builder setInternalLegacyStreamType(int streamType) { 818 mContentType = CONTENT_TYPE_UNKNOWN; 819 mUsage = USAGE_UNKNOWN; 820 if (AudioProductStrategy.getAudioProductStrategies().size() > 0) { 821 AudioAttributes attributes = 822 AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType( 823 streamType); 824 if (attributes != null) { 825 mUsage = attributes.mUsage; 826 mContentType = attributes.mContentType; 827 mFlags = attributes.mFlags; 828 mMuteHapticChannels = attributes.areHapticChannelsMuted(); 829 mTags = attributes.mTags; 830 mBundle = attributes.mBundle; 831 mSource = attributes.mSource; 832 } 833 } 834 if (mContentType == CONTENT_TYPE_UNKNOWN) { 835 switch (streamType) { 836 case AudioSystem.STREAM_VOICE_CALL: 837 mContentType = CONTENT_TYPE_SPEECH; 838 break; 839 case AudioSystem.STREAM_SYSTEM_ENFORCED: 840 mFlags |= FLAG_AUDIBILITY_ENFORCED; 841 // intended fall through, attributes in common with STREAM_SYSTEM 842 case AudioSystem.STREAM_SYSTEM: 843 mContentType = CONTENT_TYPE_SONIFICATION; 844 break; 845 case AudioSystem.STREAM_RING: 846 mContentType = CONTENT_TYPE_SONIFICATION; 847 break; 848 case AudioSystem.STREAM_MUSIC: 849 mContentType = CONTENT_TYPE_MUSIC; 850 break; 851 case AudioSystem.STREAM_ALARM: 852 mContentType = CONTENT_TYPE_SONIFICATION; 853 break; 854 case AudioSystem.STREAM_NOTIFICATION: 855 mContentType = CONTENT_TYPE_SONIFICATION; 856 break; 857 case AudioSystem.STREAM_BLUETOOTH_SCO: 858 mContentType = CONTENT_TYPE_SPEECH; 859 mFlags |= FLAG_SCO; 860 break; 861 case AudioSystem.STREAM_DTMF: 862 mContentType = CONTENT_TYPE_SONIFICATION; 863 break; 864 case AudioSystem.STREAM_TTS: 865 mContentType = CONTENT_TYPE_SONIFICATION; 866 mFlags |= FLAG_BEACON; 867 break; 868 case AudioSystem.STREAM_ACCESSIBILITY: 869 mContentType = CONTENT_TYPE_SPEECH; 870 break; 871 default: 872 Log.e(TAG, "Invalid stream type " + streamType + " for AudioAttributes"); 873 } 874 } 875 if (mUsage == USAGE_UNKNOWN) { 876 mUsage = usageForStreamType(streamType); 877 } 878 return this; 879 } 880 881 /** 882 * @hide 883 * Sets the capture preset. 884 * Use this audio attributes configuration method when building an {@link AudioRecord} 885 * instance with {@link AudioRecord#AudioRecord(AudioAttributes, AudioFormat, int)}. 886 * @param preset one of {@link MediaRecorder.AudioSource#DEFAULT}, 887 * {@link MediaRecorder.AudioSource#MIC}, {@link MediaRecorder.AudioSource#CAMCORDER}, 888 * {@link MediaRecorder.AudioSource#VOICE_RECOGNITION}, 889 * {@link MediaRecorder.AudioSource#VOICE_COMMUNICATION}, 890 * {@link MediaRecorder.AudioSource#UNPROCESSED} or 891 * {@link MediaRecorder.AudioSource#VOICE_PERFORMANCE} 892 * @return the same Builder instance. 893 */ 894 @SystemApi setCapturePreset(int preset)895 public Builder setCapturePreset(int preset) { 896 switch (preset) { 897 case MediaRecorder.AudioSource.DEFAULT: 898 case MediaRecorder.AudioSource.MIC: 899 case MediaRecorder.AudioSource.CAMCORDER: 900 case MediaRecorder.AudioSource.VOICE_RECOGNITION: 901 case MediaRecorder.AudioSource.VOICE_COMMUNICATION: 902 case MediaRecorder.AudioSource.UNPROCESSED: 903 case MediaRecorder.AudioSource.VOICE_PERFORMANCE: 904 mSource = preset; 905 break; 906 default: 907 Log.e(TAG, "Invalid capture preset " + preset + " for AudioAttributes"); 908 } 909 return this; 910 } 911 912 /** 913 * @hide 914 * Same as {@link #setCapturePreset(int)} but authorizes the use of HOTWORD, 915 * REMOTE_SUBMIX, RADIO_TUNER, VOICE_DOWNLINK, VOICE_UPLINK, VOICE_CALL and ECHO_REFERENCE. 916 * @param preset 917 * @return the same Builder instance. 918 */ 919 @SystemApi setInternalCapturePreset(int preset)920 public Builder setInternalCapturePreset(int preset) { 921 if ((preset == MediaRecorder.AudioSource.HOTWORD) 922 || (preset == MediaRecorder.AudioSource.REMOTE_SUBMIX) 923 || (preset == MediaRecorder.AudioSource.RADIO_TUNER) 924 || (preset == MediaRecorder.AudioSource.VOICE_DOWNLINK) 925 || (preset == MediaRecorder.AudioSource.VOICE_UPLINK) 926 || (preset == MediaRecorder.AudioSource.VOICE_CALL) 927 || (preset == MediaRecorder.AudioSource.ECHO_REFERENCE)) { 928 mSource = preset; 929 } else { 930 setCapturePreset(preset); 931 } 932 return this; 933 } 934 935 /** 936 * Specifying if haptic should be muted or not when playing audio-haptic coupled data. 937 * By default, haptic channels are disabled. 938 * @param muted true to force muting haptic channels. 939 * @return the same Builder instance. 940 */ setHapticChannelsMuted(boolean muted)941 public @NonNull Builder setHapticChannelsMuted(boolean muted) { 942 mMuteHapticChannels = muted; 943 return this; 944 } 945 }; 946 947 @Override describeContents()948 public int describeContents() { 949 return 0; 950 } 951 952 /** 953 * @hide 954 * Used to indicate that when parcelling, the tags should be parcelled through the flattened 955 * formatted string, not through the array of strings. 956 * Keep in sync with frameworks/av/media/libmediaplayerservice/MediaPlayerService.cpp 957 * see definition of kAudioAttributesMarshallTagFlattenTags 958 */ 959 public final static int FLATTEN_TAGS = 0x1; 960 961 private final static int ATTR_PARCEL_IS_NULL_BUNDLE = -1977; 962 private final static int ATTR_PARCEL_IS_VALID_BUNDLE = 1980; 963 964 /** 965 * When adding tags for writeToParcel(Parcel, int), add them in the list of flags (| NEW_FLAG) 966 */ 967 private final static int ALL_PARCEL_FLAGS = FLATTEN_TAGS; 968 @Override writeToParcel(Parcel dest, int flags)969 public void writeToParcel(Parcel dest, int flags) { 970 dest.writeInt(mUsage); 971 dest.writeInt(mContentType); 972 dest.writeInt(mSource); 973 dest.writeInt(mFlags); 974 dest.writeInt(flags & ALL_PARCEL_FLAGS); 975 if ((flags & FLATTEN_TAGS) == 0) { 976 String[] tagsArray = new String[mTags.size()]; 977 mTags.toArray(tagsArray); 978 dest.writeStringArray(tagsArray); 979 } else if ((flags & FLATTEN_TAGS) == FLATTEN_TAGS) { 980 dest.writeString(mFormattedTags); 981 } 982 if (mBundle == null) { 983 dest.writeInt(ATTR_PARCEL_IS_NULL_BUNDLE); 984 } else { 985 dest.writeInt(ATTR_PARCEL_IS_VALID_BUNDLE); 986 dest.writeBundle(mBundle); 987 } 988 } 989 AudioAttributes(Parcel in)990 private AudioAttributes(Parcel in) { 991 mUsage = in.readInt(); 992 mContentType = in.readInt(); 993 mSource = in.readInt(); 994 mFlags = in.readInt(); 995 boolean hasFlattenedTags = ((in.readInt() & FLATTEN_TAGS) == FLATTEN_TAGS); 996 mTags = new HashSet<String>(); 997 if (hasFlattenedTags) { 998 mFormattedTags = new String(in.readString()); 999 mTags.add(mFormattedTags); 1000 } else { 1001 String[] tagsArray = in.readStringArray(); 1002 for (int i = tagsArray.length - 1 ; i >= 0 ; i--) { 1003 mTags.add(tagsArray[i]); 1004 } 1005 mFormattedTags = TextUtils.join(";", mTags); 1006 } 1007 switch (in.readInt()) { 1008 case ATTR_PARCEL_IS_NULL_BUNDLE: 1009 mBundle = null; 1010 break; 1011 case ATTR_PARCEL_IS_VALID_BUNDLE: 1012 mBundle = new Bundle(in.readBundle()); 1013 break; 1014 default: 1015 Log.e(TAG, "Illegal value unmarshalling AudioAttributes, can't initialize bundle"); 1016 } 1017 } 1018 1019 public static final @android.annotation.NonNull Parcelable.Creator<AudioAttributes> CREATOR 1020 = new Parcelable.Creator<AudioAttributes>() { 1021 /** 1022 * Rebuilds an AudioAttributes previously stored with writeToParcel(). 1023 * @param p Parcel object to read the AudioAttributes from 1024 * @return a new AudioAttributes created from the data in the parcel 1025 */ 1026 public AudioAttributes createFromParcel(Parcel p) { 1027 return new AudioAttributes(p); 1028 } 1029 public AudioAttributes[] newArray(int size) { 1030 return new AudioAttributes[size]; 1031 } 1032 }; 1033 1034 @Override equals(Object o)1035 public boolean equals(Object o) { 1036 if (this == o) return true; 1037 if (o == null || getClass() != o.getClass()) return false; 1038 1039 AudioAttributes that = (AudioAttributes) o; 1040 1041 return ((mContentType == that.mContentType) 1042 && (mFlags == that.mFlags) 1043 && (mSource == that.mSource) 1044 && (mUsage == that.mUsage) 1045 //mFormattedTags is never null due to assignment in Builder or unmarshalling 1046 && (mFormattedTags.equals(that.mFormattedTags))); 1047 } 1048 1049 @Override hashCode()1050 public int hashCode() { 1051 return Objects.hash(mContentType, mFlags, mSource, mUsage, mFormattedTags, mBundle); 1052 } 1053 1054 @Override toString()1055 public String toString () { 1056 return new String("AudioAttributes:" 1057 + " usage=" + usageToString() 1058 + " content=" + contentTypeToString() 1059 + " flags=0x" + Integer.toHexString(mFlags).toUpperCase() 1060 + " tags=" + mFormattedTags 1061 + " bundle=" + (mBundle == null ? "null" : mBundle.toString())); 1062 } 1063 1064 /** @hide */ writeToProto(ProtoOutputStream proto, long fieldId)1065 public void writeToProto(ProtoOutputStream proto, long fieldId) { 1066 final long token = proto.start(fieldId); 1067 1068 proto.write(AudioAttributesProto.USAGE, mUsage); 1069 proto.write(AudioAttributesProto.CONTENT_TYPE, mContentType); 1070 proto.write(AudioAttributesProto.FLAGS, mFlags); 1071 // mFormattedTags is never null due to assignment in Builder or unmarshalling. 1072 for (String t : mFormattedTags.split(";")) { 1073 t = t.trim(); 1074 if (t != "") { 1075 proto.write(AudioAttributesProto.TAGS, t); 1076 } 1077 } 1078 // TODO: is the data in mBundle useful for debugging? 1079 1080 proto.end(token); 1081 } 1082 1083 /** @hide */ usageToString()1084 public String usageToString() { 1085 return usageToString(mUsage); 1086 } 1087 1088 /** @hide */ usageToString(int usage)1089 public static String usageToString(int usage) { 1090 switch(usage) { 1091 case USAGE_UNKNOWN: 1092 return new String("USAGE_UNKNOWN"); 1093 case USAGE_MEDIA: 1094 return new String("USAGE_MEDIA"); 1095 case USAGE_VOICE_COMMUNICATION: 1096 return new String("USAGE_VOICE_COMMUNICATION"); 1097 case USAGE_VOICE_COMMUNICATION_SIGNALLING: 1098 return new String("USAGE_VOICE_COMMUNICATION_SIGNALLING"); 1099 case USAGE_ALARM: 1100 return new String("USAGE_ALARM"); 1101 case USAGE_NOTIFICATION: 1102 return new String("USAGE_NOTIFICATION"); 1103 case USAGE_NOTIFICATION_RINGTONE: 1104 return new String("USAGE_NOTIFICATION_RINGTONE"); 1105 case USAGE_NOTIFICATION_COMMUNICATION_REQUEST: 1106 return new String("USAGE_NOTIFICATION_COMMUNICATION_REQUEST"); 1107 case USAGE_NOTIFICATION_COMMUNICATION_INSTANT: 1108 return new String("USAGE_NOTIFICATION_COMMUNICATION_INSTANT"); 1109 case USAGE_NOTIFICATION_COMMUNICATION_DELAYED: 1110 return new String("USAGE_NOTIFICATION_COMMUNICATION_DELAYED"); 1111 case USAGE_NOTIFICATION_EVENT: 1112 return new String("USAGE_NOTIFICATION_EVENT"); 1113 case USAGE_ASSISTANCE_ACCESSIBILITY: 1114 return new String("USAGE_ASSISTANCE_ACCESSIBILITY"); 1115 case USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: 1116 return new String("USAGE_ASSISTANCE_NAVIGATION_GUIDANCE"); 1117 case USAGE_ASSISTANCE_SONIFICATION: 1118 return new String("USAGE_ASSISTANCE_SONIFICATION"); 1119 case USAGE_GAME: 1120 return new String("USAGE_GAME"); 1121 case USAGE_ASSISTANT: 1122 return new String("USAGE_ASSISTANT"); 1123 default: 1124 return new String("unknown usage " + usage); 1125 } 1126 } 1127 1128 /** @hide */ contentTypeToString()1129 public String contentTypeToString() { 1130 switch(mContentType) { 1131 case CONTENT_TYPE_UNKNOWN: 1132 return new String("CONTENT_TYPE_UNKNOWN"); 1133 case CONTENT_TYPE_SPEECH: return new String("CONTENT_TYPE_SPEECH"); 1134 case CONTENT_TYPE_MUSIC: return new String("CONTENT_TYPE_MUSIC"); 1135 case CONTENT_TYPE_MOVIE: return new String("CONTENT_TYPE_MOVIE"); 1136 case CONTENT_TYPE_SONIFICATION: return new String("CONTENT_TYPE_SONIFICATION"); 1137 default: return new String("unknown content type " + mContentType); 1138 } 1139 } 1140 usageForStreamType(int streamType)1141 private static int usageForStreamType(int streamType) { 1142 switch(streamType) { 1143 case AudioSystem.STREAM_VOICE_CALL: 1144 return USAGE_VOICE_COMMUNICATION; 1145 case AudioSystem.STREAM_SYSTEM_ENFORCED: 1146 case AudioSystem.STREAM_SYSTEM: 1147 return USAGE_ASSISTANCE_SONIFICATION; 1148 case AudioSystem.STREAM_RING: 1149 return USAGE_NOTIFICATION_RINGTONE; 1150 case AudioSystem.STREAM_MUSIC: 1151 return USAGE_MEDIA; 1152 case AudioSystem.STREAM_ALARM: 1153 return USAGE_ALARM; 1154 case AudioSystem.STREAM_NOTIFICATION: 1155 return USAGE_NOTIFICATION; 1156 case AudioSystem.STREAM_BLUETOOTH_SCO: 1157 return USAGE_VOICE_COMMUNICATION; 1158 case AudioSystem.STREAM_DTMF: 1159 return USAGE_VOICE_COMMUNICATION_SIGNALLING; 1160 case AudioSystem.STREAM_ACCESSIBILITY: 1161 return USAGE_ASSISTANCE_ACCESSIBILITY; 1162 case AudioSystem.STREAM_TTS: 1163 default: 1164 return USAGE_UNKNOWN; 1165 } 1166 } 1167 1168 /** 1169 * Returns the stream type matching this {@code AudioAttributes} instance for volume control. 1170 * Use this method to derive the stream type needed to configure the volume 1171 * control slider in an {@link android.app.Activity} with 1172 * {@link android.app.Activity#setVolumeControlStream(int)} for playback conducted with these 1173 * attributes. 1174 * <BR>Do not use this method to set the stream type on an audio player object 1175 * (e.g. {@link AudioTrack}, {@link MediaPlayer}) as this is deprecated, 1176 * use {@code AudioAttributes} instead. 1177 * @return a valid stream type for {@code Activity} or stream volume control that matches 1178 * the attributes, or {@link AudioManager#USE_DEFAULT_STREAM_TYPE} if there isn't a direct 1179 * match. Note that {@code USE_DEFAULT_STREAM_TYPE} is not a valid value 1180 * for {@link AudioManager#setStreamVolume(int, int, int)}. 1181 */ getVolumeControlStream()1182 public int getVolumeControlStream() { 1183 return toVolumeStreamType(true /*fromGetVolumeControlStream*/, this); 1184 } 1185 1186 /** 1187 * @hide 1188 * Only use to get which stream type should be used for volume control, NOT for audio playback 1189 * (all audio playback APIs are supposed to take AudioAttributes as input parameters) 1190 * @param aa non-null AudioAttributes. 1191 * @return a valid stream type for volume control that matches the attributes. 1192 */ 1193 @UnsupportedAppUsage toLegacyStreamType(@onNull AudioAttributes aa)1194 public static int toLegacyStreamType(@NonNull AudioAttributes aa) { 1195 return toVolumeStreamType(false /*fromGetVolumeControlStream*/, aa); 1196 } 1197 toVolumeStreamType(boolean fromGetVolumeControlStream, AudioAttributes aa)1198 private static int toVolumeStreamType(boolean fromGetVolumeControlStream, AudioAttributes aa) { 1199 // flags to stream type mapping 1200 if ((aa.getFlags() & FLAG_AUDIBILITY_ENFORCED) == FLAG_AUDIBILITY_ENFORCED) { 1201 return fromGetVolumeControlStream ? 1202 AudioSystem.STREAM_SYSTEM : AudioSystem.STREAM_SYSTEM_ENFORCED; 1203 } 1204 if ((aa.getAllFlags() & FLAG_SCO) == FLAG_SCO) { 1205 return fromGetVolumeControlStream ? 1206 AudioSystem.STREAM_VOICE_CALL : AudioSystem.STREAM_BLUETOOTH_SCO; 1207 } 1208 if ((aa.getAllFlags() & FLAG_BEACON) == FLAG_BEACON) { 1209 return fromGetVolumeControlStream ? 1210 AudioSystem.STREAM_MUSIC : AudioSystem.STREAM_TTS; 1211 } 1212 1213 if (AudioProductStrategy.getAudioProductStrategies().size() > 0) { 1214 return AudioProductStrategy.getLegacyStreamTypeForStrategyWithAudioAttributes(aa); 1215 } 1216 // usage to stream type mapping 1217 switch (aa.getUsage()) { 1218 case USAGE_MEDIA: 1219 case USAGE_GAME: 1220 case USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: 1221 case USAGE_ASSISTANT: 1222 return AudioSystem.STREAM_MUSIC; 1223 case USAGE_ASSISTANCE_SONIFICATION: 1224 return AudioSystem.STREAM_SYSTEM; 1225 case USAGE_VOICE_COMMUNICATION: 1226 return AudioSystem.STREAM_VOICE_CALL; 1227 case USAGE_VOICE_COMMUNICATION_SIGNALLING: 1228 return fromGetVolumeControlStream ? 1229 AudioSystem.STREAM_VOICE_CALL : AudioSystem.STREAM_DTMF; 1230 case USAGE_ALARM: 1231 return AudioSystem.STREAM_ALARM; 1232 case USAGE_NOTIFICATION_RINGTONE: 1233 return AudioSystem.STREAM_RING; 1234 case USAGE_NOTIFICATION: 1235 case USAGE_NOTIFICATION_COMMUNICATION_REQUEST: 1236 case USAGE_NOTIFICATION_COMMUNICATION_INSTANT: 1237 case USAGE_NOTIFICATION_COMMUNICATION_DELAYED: 1238 case USAGE_NOTIFICATION_EVENT: 1239 return AudioSystem.STREAM_NOTIFICATION; 1240 case USAGE_ASSISTANCE_ACCESSIBILITY: 1241 return AudioSystem.STREAM_ACCESSIBILITY; 1242 case USAGE_UNKNOWN: 1243 return AudioSystem.STREAM_MUSIC; 1244 default: 1245 if (fromGetVolumeControlStream) { 1246 throw new IllegalArgumentException("Unknown usage value " + aa.getUsage() + 1247 " in audio attributes"); 1248 } else { 1249 return AudioSystem.STREAM_MUSIC; 1250 } 1251 } 1252 } 1253 capturePolicyToFlags(@apturePolicy int capturePolicy, int flags)1254 static int capturePolicyToFlags(@CapturePolicy int capturePolicy, int flags) { 1255 switch (capturePolicy) { 1256 case ALLOW_CAPTURE_BY_NONE: 1257 flags |= FLAG_NO_MEDIA_PROJECTION | FLAG_NO_SYSTEM_CAPTURE; 1258 break; 1259 case ALLOW_CAPTURE_BY_SYSTEM: 1260 flags |= FLAG_NO_MEDIA_PROJECTION; 1261 flags &= ~FLAG_NO_SYSTEM_CAPTURE; 1262 break; 1263 case ALLOW_CAPTURE_BY_ALL: 1264 flags &= ~FLAG_NO_SYSTEM_CAPTURE & ~FLAG_NO_MEDIA_PROJECTION; 1265 break; 1266 default: 1267 throw new IllegalArgumentException("Unknown allow playback capture policy"); 1268 } 1269 return flags; 1270 } 1271 1272 /** @hide */ 1273 @IntDef({ 1274 USAGE_UNKNOWN, 1275 USAGE_MEDIA, 1276 USAGE_VOICE_COMMUNICATION, 1277 USAGE_VOICE_COMMUNICATION_SIGNALLING, 1278 USAGE_ALARM, 1279 USAGE_NOTIFICATION, 1280 USAGE_NOTIFICATION_RINGTONE, 1281 USAGE_NOTIFICATION_COMMUNICATION_REQUEST, 1282 USAGE_NOTIFICATION_COMMUNICATION_INSTANT, 1283 USAGE_NOTIFICATION_COMMUNICATION_DELAYED, 1284 USAGE_NOTIFICATION_EVENT, 1285 USAGE_ASSISTANCE_ACCESSIBILITY, 1286 USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, 1287 USAGE_ASSISTANCE_SONIFICATION, 1288 USAGE_GAME, 1289 USAGE_ASSISTANT, 1290 }) 1291 @Retention(RetentionPolicy.SOURCE) 1292 public @interface AttributeUsage {} 1293 1294 /** @hide */ 1295 @IntDef({ 1296 CONTENT_TYPE_UNKNOWN, 1297 CONTENT_TYPE_SPEECH, 1298 CONTENT_TYPE_MUSIC, 1299 CONTENT_TYPE_MOVIE, 1300 CONTENT_TYPE_SONIFICATION 1301 }) 1302 @Retention(RetentionPolicy.SOURCE) 1303 public @interface AttributeContentType {} 1304 } 1305