blob: d0e28604246160313111b9e5fbc87b30eb471d88 [file] [log] [blame]
Sailesh Nepal4cff3922014-03-19 10:15:37 -07001/*
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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070018
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Locale;
23
24/**
25 * Encapsulates all audio states during a call.
26 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070027public final class AudioState implements Parcelable {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070028 /** Direct the audio stream through the device's earpiece. */
Yorke Lee14260482014-08-20 16:16:26 -070029 public static final int ROUTE_EARPIECE = 0x00000001;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070030
31 /** Direct the audio stream through Bluetooth. */
Yorke Lee14260482014-08-20 16:16:26 -070032 public static final int ROUTE_BLUETOOTH = 0x00000002;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070033
34 /** Direct the audio stream through a wired headset. */
Yorke Lee14260482014-08-20 16:16:26 -070035 public static final int ROUTE_WIRED_HEADSET = 0x00000004;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070036
Nancy Chenea38cca2014-09-05 16:38:49 -070037 /** Direct the audio stream through the device's speakerphone. */
Yorke Lee14260482014-08-20 16:16:26 -070038 public static final int ROUTE_SPEAKER = 0x00000008;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070039
40 /**
41 * Direct the audio stream through the device's earpiece or wired headset if one is
42 * connected.
43 */
Yorke Lee14260482014-08-20 16:16:26 -070044 public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070045
Nancy Chenea38cca2014-09-05 16:38:49 -070046 /** Bit mask of all possible audio routes.
47 *
48 * @hide
49 */
Yorke Lee14260482014-08-20 16:16:26 -070050 public static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
Sailesh Nepal4cff3922014-03-19 10:15:37 -070051 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
Ihab Awadb19a0bc2014-08-07 19:46:01 -070062 public AudioState(boolean isMuted, int route, int supportedRouteMask) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070063 this.isMuted = isMuted;
64 this.route = route;
65 this.supportedRouteMask = supportedRouteMask;
66 }
67
Ihab Awadb19a0bc2014-08-07 19:46:01 -070068 public AudioState(AudioState state) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070069 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 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070079 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070080 return false;
81 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070082 AudioState state = (AudioState) obj;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070083 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,
Ihab Awadb19a0bc2014-08-07 19:46:01 -070090 "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]",
Sailesh Nepal4cff3922014-03-19 10:15:37 -070091 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 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700125 * Responsible for creating AudioState objects for deserialized Parcels.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700126 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700127 public static final Parcelable.Creator<AudioState> CREATOR =
128 new Parcelable.Creator<AudioState> () {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700129
130 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700131 public AudioState createFromParcel(Parcel source) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700132 boolean isMuted = source.readByte() == 0 ? false : true;
133 int route = source.readInt();
134 int supportedRouteMask = source.readInt();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700135 return new AudioState(isMuted, route, supportedRouteMask);
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700136 }
137
138 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700139 public AudioState[] newArray(int size) {
140 return new AudioState[size];
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700141 }
142 };
143
144 /**
145 * {@inheritDoc}
146 */
147 @Override
148 public int describeContents() {
149 return 0;
150 }
151
152 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700153 * Writes AudioState object into a serializeable Parcel.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700154 */
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}