blob: 0720df37f60f4a376b67d3609b8d9d459411b1b4 [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
Evan Charlton0e094d92014-11-08 15:49:16 -080019import android.annotation.SystemApi;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070020import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Locale;
24
25/**
Santos Cordond9e614f2014-10-28 13:10:36 -070026 * Encapsulates the telecom audio state, including the current audio routing, supported audio
27 * routing and mute.
Evan Charlton0e094d92014-11-08 15:49:16 -080028 * @hide
Sailesh Nepal4cff3922014-03-19 10:15:37 -070029 */
Evan Charlton0e094d92014-11-08 15:49:16 -080030@SystemApi
Ihab Awadb19a0bc2014-08-07 19:46:01 -070031public final class AudioState implements Parcelable {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070032 /** Direct the audio stream through the device's earpiece. */
Yorke Lee14260482014-08-20 16:16:26 -070033 public static final int ROUTE_EARPIECE = 0x00000001;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070034
35 /** Direct the audio stream through Bluetooth. */
Yorke Lee14260482014-08-20 16:16:26 -070036 public static final int ROUTE_BLUETOOTH = 0x00000002;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070037
38 /** Direct the audio stream through a wired headset. */
Yorke Lee14260482014-08-20 16:16:26 -070039 public static final int ROUTE_WIRED_HEADSET = 0x00000004;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070040
Nancy Chenea38cca2014-09-05 16:38:49 -070041 /** Direct the audio stream through the device's speakerphone. */
Yorke Lee14260482014-08-20 16:16:26 -070042 public static final int ROUTE_SPEAKER = 0x00000008;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070043
44 /**
45 * Direct the audio stream through the device's earpiece or wired headset if one is
46 * connected.
47 */
Yorke Lee14260482014-08-20 16:16:26 -070048 public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070049
Nancy Chenea38cca2014-09-05 16:38:49 -070050 /** Bit mask of all possible audio routes.
51 *
52 * @hide
53 */
Yorke Lee14260482014-08-20 16:16:26 -070054 public static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
Sailesh Nepal4cff3922014-03-19 10:15:37 -070055 ROUTE_SPEAKER;
56
Jay Shrauner193de662015-04-14 18:16:10 -070057 private final boolean isMuted;
58 private final int route;
59 private final int supportedRouteMask;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070060
Ihab Awad5c9c86e2014-11-12 13:41:16 -080061 public AudioState(boolean muted, int route, int supportedRouteMask) {
62 this.isMuted = muted;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070063 this.route = route;
64 this.supportedRouteMask = supportedRouteMask;
65 }
66
Ihab Awadb19a0bc2014-08-07 19:46:01 -070067 public AudioState(AudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -080068 isMuted = state.isMuted();
69 route = state.getRoute();
70 supportedRouteMask = state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070071 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (obj == null) {
76 return false;
77 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070078 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070079 return false;
80 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070081 AudioState state = (AudioState) obj;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080082 return isMuted() == state.isMuted() && getRoute() == state.getRoute() &&
83 getSupportedRouteMask() == state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070084 }
85
86 @Override
87 public String toString() {
88 return String.format(Locale.US,
Nancy Chenddf15a12014-12-02 15:59:35 -080089 "[AudioState isMuted: %b, route: %s, supportedRouteMask: %s]",
Ihab Awad5c9c86e2014-11-12 13:41:16 -080090 isMuted,
91 audioRouteToString(route),
92 audioRouteToString(supportedRouteMask));
Sailesh Nepal4cff3922014-03-19 10:15:37 -070093 }
94
95 /** @hide */
96 public static String audioRouteToString(int route) {
97 if (route == 0 || (route & ~ROUTE_ALL) != 0x0) {
98 return "UNKNOWN";
99 }
100
101 StringBuffer buffer = new StringBuffer();
102 if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) {
103 listAppend(buffer, "EARPIECE");
104 }
105 if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) {
106 listAppend(buffer, "BLUETOOTH");
107 }
108 if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) {
109 listAppend(buffer, "WIRED_HEADSET");
110 }
111 if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) {
112 listAppend(buffer, "SPEAKER");
113 }
114
115 return buffer.toString();
116 }
117
118 private static void listAppend(StringBuffer buffer, String str) {
119 if (buffer.length() > 0) {
120 buffer.append(", ");
121 }
122 buffer.append(str);
123 }
124
125 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700126 * Responsible for creating AudioState objects for deserialized Parcels.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700127 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700128 public static final Parcelable.Creator<AudioState> CREATOR =
129 new Parcelable.Creator<AudioState> () {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700130
131 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700132 public AudioState createFromParcel(Parcel source) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700133 boolean isMuted = source.readByte() == 0 ? false : true;
134 int route = source.readInt();
135 int supportedRouteMask = source.readInt();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700136 return new AudioState(isMuted, route, supportedRouteMask);
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700137 }
138
139 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700140 public AudioState[] newArray(int size) {
141 return new AudioState[size];
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700142 }
143 };
144
145 /**
146 * {@inheritDoc}
147 */
148 @Override
149 public int describeContents() {
150 return 0;
151 }
152
153 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700154 * Writes AudioState object into a serializeable Parcel.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700155 */
156 @Override
157 public void writeToParcel(Parcel destination, int flags) {
158 destination.writeByte((byte) (isMuted ? 1 : 0));
159 destination.writeInt(route);
160 destination.writeInt(supportedRouteMask);
161 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800162
163 /**
164 * @return {@code true} if the call is muted, false otherwise.
165 */
166 public boolean isMuted() {
167 return isMuted;
168 }
169
170 /**
171 * @return The current audio route being used.
172 */
173 public int getRoute() {
174 return route;
175 }
176
177 /**
178 * @return Bit mask of all routes supported by this call.
179 */
180 public int getSupportedRouteMask() {
181 return supportedRouteMask;
182 }
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700183}