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