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