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