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.telecom;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.compat.annotation.UnsupportedAppUsage;
23 import android.os.Build;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.util.Locale;
28 
29 /**
30  *  Encapsulates the telecom audio state, including the current audio routing, supported audio
31  *  routing and mute.
32  *  @deprecated - use {@link CallAudioState} instead.
33  *  @hide
34  */
35 @Deprecated
36 @SystemApi
37 public class AudioState implements Parcelable {
38     /** Direct the audio stream through the device's earpiece. */
39     public static final int ROUTE_EARPIECE      = 0x00000001;
40 
41     /** Direct the audio stream through Bluetooth. */
42     public static final int ROUTE_BLUETOOTH     = 0x00000002;
43 
44     /** Direct the audio stream through a wired headset. */
45     public static final int ROUTE_WIRED_HEADSET = 0x00000004;
46 
47     /** Direct the audio stream through the device's speakerphone. */
48     public static final int ROUTE_SPEAKER       = 0x00000008;
49 
50     /**
51      * Direct the audio stream through the device's earpiece or wired headset if one is
52      * connected.
53      */
54     public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET;
55 
56     /** Bit mask of all possible audio routes. */
57     private static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
58             ROUTE_SPEAKER;
59 
60     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
61     private final boolean isMuted;
62     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
63     private final int route;
64     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
65     private final int supportedRouteMask;
66 
AudioState(boolean muted, int route, int supportedRouteMask)67     public AudioState(boolean muted, int route, int supportedRouteMask) {
68         this.isMuted = muted;
69         this.route = route;
70         this.supportedRouteMask = supportedRouteMask;
71     }
72 
AudioState(AudioState state)73     public AudioState(AudioState state) {
74         isMuted = state.isMuted();
75         route = state.getRoute();
76         supportedRouteMask = state.getSupportedRouteMask();
77     }
78 
AudioState(CallAudioState state)79     public AudioState(CallAudioState state) {
80         isMuted = state.isMuted();
81         route = state.getRoute();
82         supportedRouteMask = state.getSupportedRouteMask();
83     }
84 
85     @Override
equals(@ullable Object obj)86     public boolean equals(@Nullable Object obj) {
87         if (obj == null) {
88             return false;
89         }
90         if (!(obj instanceof AudioState)) {
91             return false;
92         }
93         AudioState state = (AudioState) obj;
94         return isMuted() == state.isMuted() && getRoute() == state.getRoute() &&
95                 getSupportedRouteMask() == state.getSupportedRouteMask();
96     }
97 
98     @NonNull
99     @Override
toString()100     public String toString() {
101         return String.format(Locale.US,
102                 "[AudioState isMuted: %b, route: %s, supportedRouteMask: %s]",
103                 isMuted,
104                 audioRouteToString(route),
105                 audioRouteToString(supportedRouteMask));
106     }
107 
audioRouteToString(int route)108     public static String audioRouteToString(int route) {
109         if (route == 0 || (route & ~ROUTE_ALL) != 0x0) {
110             return "UNKNOWN";
111         }
112 
113         StringBuffer buffer = new StringBuffer();
114         if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) {
115             listAppend(buffer, "EARPIECE");
116         }
117         if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) {
118             listAppend(buffer, "BLUETOOTH");
119         }
120         if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) {
121             listAppend(buffer, "WIRED_HEADSET");
122         }
123         if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) {
124             listAppend(buffer, "SPEAKER");
125         }
126 
127         return buffer.toString();
128     }
129 
listAppend(StringBuffer buffer, String str)130     private static void listAppend(StringBuffer buffer, String str) {
131         if (buffer.length() > 0) {
132             buffer.append(", ");
133         }
134         buffer.append(str);
135     }
136 
137     /**
138      * Responsible for creating AudioState objects for deserialized Parcels.
139      */
140     public static final @android.annotation.NonNull Parcelable.Creator<AudioState> CREATOR =
141             new Parcelable.Creator<AudioState> () {
142 
143         @Override
144         public AudioState createFromParcel(Parcel source) {
145             boolean isMuted = source.readByte() == 0 ? false : true;
146             int route = source.readInt();
147             int supportedRouteMask = source.readInt();
148             return new AudioState(isMuted, route, supportedRouteMask);
149         }
150 
151         @Override
152         public AudioState[] newArray(int size) {
153             return new AudioState[size];
154         }
155     };
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
describeContents()161     public int describeContents() {
162         return 0;
163     }
164 
165     /**
166      * Writes AudioState object into a serializeable Parcel.
167      */
168     @Override
writeToParcel(Parcel destination, int flags)169     public void writeToParcel(Parcel destination, int flags) {
170         destination.writeByte((byte) (isMuted ? 1 : 0));
171         destination.writeInt(route);
172         destination.writeInt(supportedRouteMask);
173     }
174 
175     /**
176      * @return {@code true} if the call is muted, false otherwise.
177      */
isMuted()178     public boolean isMuted() {
179         return isMuted;
180     }
181 
182     /**
183      * @return The current audio route being used.
184      */
getRoute()185     public int getRoute() {
186         return route;
187     }
188 
189     /**
190      * @return Bit mask of all routes supported by this call.
191      */
getSupportedRouteMask()192     public int getSupportedRouteMask() {
193         return supportedRouteMask;
194     }
195 }
196