1 /*
2  * Copyright 2021 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 package android.media;
17 
18 import android.annotation.NonNull;
19 import android.annotation.SystemApi;
20 import android.bluetooth.BluetoothProfile;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * Contains information about Bluetooth profile connection state changed
26  * {@hide}
27  */
28 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
29 public final class BluetoothProfileConnectionInfo implements Parcelable {
30     private final int mProfile;
31     private final boolean mSupprNoisy;
32     private final int mVolume;
33     private final boolean mIsLeOutput;
34 
BluetoothProfileConnectionInfo(int profile, boolean suppressNoisyIntent, int volume, boolean isLeOutput)35     private BluetoothProfileConnectionInfo(int profile, boolean suppressNoisyIntent,
36             int volume, boolean isLeOutput) {
37         mProfile = profile;
38         mSupprNoisy = suppressNoisyIntent;
39         mVolume = volume;
40         mIsLeOutput = isLeOutput;
41     }
42 
43     /**
44      * Constructor used by BtHelper when a profile is connected
45      * {@hide}
46      */
BluetoothProfileConnectionInfo(int profile)47     public BluetoothProfileConnectionInfo(int profile) {
48         this(profile, false, -1, false);
49     }
50 
51     public static final @NonNull Parcelable.Creator<BluetoothProfileConnectionInfo> CREATOR =
52             new Parcelable.Creator<BluetoothProfileConnectionInfo>() {
53                 @Override
54                 public BluetoothProfileConnectionInfo createFromParcel(Parcel source) {
55                     return new BluetoothProfileConnectionInfo(source.readInt(),
56                             source.readBoolean(), source.readInt(), source.readBoolean());
57                 }
58 
59                 @Override
60                 public BluetoothProfileConnectionInfo[] newArray(int size) {
61                     return new BluetoothProfileConnectionInfo[size];
62                 }
63             };
64 
65     @Override
writeToParcel(@onNull Parcel dest, @WriteFlags int flags)66     public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
67         dest.writeInt(mProfile);
68         dest.writeBoolean(mSupprNoisy);
69         dest.writeInt(mVolume);
70         dest.writeBoolean(mIsLeOutput);
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     /**
79      * Constructor for A2dp info
80      *
81      * @param suppressNoisyIntent if true the {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY}
82      * intent will not be sent.
83      *
84      * @param volume of device -1 to ignore value
85      */
createA2dpInfo( boolean suppressNoisyIntent, int volume)86     public static @NonNull BluetoothProfileConnectionInfo createA2dpInfo(
87             boolean suppressNoisyIntent, int volume) {
88         return new BluetoothProfileConnectionInfo(BluetoothProfile.A2DP, suppressNoisyIntent,
89             volume, false);
90     }
91 
92     /**
93      * Constructor for A2dp sink info
94      * The {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY} intent will not be sent.
95      *
96      * @param volume of device -1 to ignore value
97      */
createA2dpSinkInfo(int volume)98     public static @NonNull BluetoothProfileConnectionInfo createA2dpSinkInfo(int volume) {
99         return new BluetoothProfileConnectionInfo(BluetoothProfile.A2DP_SINK, true, volume, false);
100     }
101 
102     /**
103      * Constructor for hearing aid info
104      *
105      * @param suppressNoisyIntent if true the {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY}
106      * intent will not be sent.
107      */
createHearingAidInfo( boolean suppressNoisyIntent)108     public static @NonNull BluetoothProfileConnectionInfo createHearingAidInfo(
109             boolean suppressNoisyIntent) {
110         return new BluetoothProfileConnectionInfo(BluetoothProfile.HEARING_AID, suppressNoisyIntent,
111             -1, false);
112     }
113 
114     /**
115      * Factory method for <code>BluetoothProfileConnectionInfo</code> for an LE device
116      * Use this method for an input device connection,
117      * or for an output device connection if the connection volume is unknown,
118      * otherwise use {@link #createLeAudioOutputInfo(boolean, int)}.
119      * @param suppressNoisyIntent if true the {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY}
120      * intent will not be sent.
121      *
122      * @param isLeOutput if true mean the device is an output device, if false it's an input device
123      */
createLeAudioInfo( boolean suppressNoisyIntent, boolean isLeOutput)124     public static @NonNull BluetoothProfileConnectionInfo createLeAudioInfo(
125             boolean suppressNoisyIntent, boolean isLeOutput) {
126         return new BluetoothProfileConnectionInfo(BluetoothProfile.LE_AUDIO, suppressNoisyIntent,
127             -1, isLeOutput);
128     }
129 
130     /**
131      * Factory method for <code>BluetoothProfileConnectionInfo</code> for an LE output device
132      * Use this method for an output device connection with a volume to be used at connection
133      * time.
134      * @param suppressNoisyIntent if true the {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY}
135      *     intent will not be sent.
136      * @param volume the volume index of the device, -1 if unknown or to be ignored
137      * @return an instance of BluetoothProfileConnectionInfo for the BLE output device that reflects
138      *     the given parameters
139      */
createLeAudioOutputInfo( boolean suppressNoisyIntent, int volume)140     public static @NonNull BluetoothProfileConnectionInfo createLeAudioOutputInfo(
141             boolean suppressNoisyIntent, int volume) {
142         return new BluetoothProfileConnectionInfo(BluetoothProfile.LE_AUDIO, suppressNoisyIntent,
143                 volume, /*isLeOutput*/ true);
144     }
145 
146     /**
147      * @return The profile connection
148      */
getProfile()149     public int getProfile() {
150         return mProfile;
151     }
152 
153     /**
154      * @return {@code true} if {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY} intent will not be
155      * sent
156      */
isSuppressNoisyIntent()157     public boolean isSuppressNoisyIntent() {
158         return mSupprNoisy;
159     }
160 
161     /**
162      * Only for {@link BluetoothProfile.A2DP} profile
163      * @return the volume of the connection or -1 if the value is ignored
164      */
getVolume()165     public int getVolume() {
166         return mVolume;
167     }
168 
169     /**
170      * Only for {@link BluetoothProfile.LE_AUDIO} profile
171      * @return {@code true} is the LE device is an output device, {@code false} if it's an input
172      * device
173      */
isLeOutput()174     public boolean isLeOutput() {
175         return mIsLeOutput;
176     }
177 }
178