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