blob: 465c5f41a297766c37e3c4378ade2d3cefe9e4cc [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/**
Santos Cordond9e614f2014-10-28 13:10:36 -070025 * Encapsulates the telecom audio state, including the current audio routing, supported audio
26 * routing and mute.
Sailesh Nepal4cff3922014-03-19 10:15:37 -070027 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070028public final class AudioState implements Parcelable {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070029 /** Direct the audio stream through the device's earpiece. */
Yorke Lee14260482014-08-20 16:16:26 -070030 public static final int ROUTE_EARPIECE = 0x00000001;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070031
32 /** Direct the audio stream through Bluetooth. */
Yorke Lee14260482014-08-20 16:16:26 -070033 public static final int ROUTE_BLUETOOTH = 0x00000002;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070034
35 /** Direct the audio stream through a wired headset. */
Yorke Lee14260482014-08-20 16:16:26 -070036 public static final int ROUTE_WIRED_HEADSET = 0x00000004;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070037
Nancy Chenea38cca2014-09-05 16:38:49 -070038 /** Direct the audio stream through the device's speakerphone. */
Yorke Lee14260482014-08-20 16:16:26 -070039 public static final int ROUTE_SPEAKER = 0x00000008;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070040
41 /**
42 * Direct the audio stream through the device's earpiece or wired headset if one is
43 * connected.
44 */
Yorke Lee14260482014-08-20 16:16:26 -070045 public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070046
Jay Shrauner1cf9b6b2015-04-09 15:15:43 -070047 /** Bit mask of all possible audio routes. */
48 private static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
Sailesh Nepal4cff3922014-03-19 10:15:37 -070049 ROUTE_SPEAKER;
50
Jay Shrauner193de662015-04-14 18:16:10 -070051 private final boolean isMuted;
52 private final int route;
53 private final int supportedRouteMask;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070054
Ihab Awad5c9c86e2014-11-12 13:41:16 -080055 public AudioState(boolean muted, int route, int supportedRouteMask) {
56 this.isMuted = muted;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070057 this.route = route;
58 this.supportedRouteMask = supportedRouteMask;
59 }
60
Ihab Awadb19a0bc2014-08-07 19:46:01 -070061 public AudioState(AudioState state) {
Ihab Awad5c9c86e2014-11-12 13:41:16 -080062 isMuted = state.isMuted();
63 route = state.getRoute();
64 supportedRouteMask = state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070065 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (obj == null) {
70 return false;
71 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070072 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070073 return false;
74 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070075 AudioState state = (AudioState) obj;
Ihab Awad5c9c86e2014-11-12 13:41:16 -080076 return isMuted() == state.isMuted() && getRoute() == state.getRoute() &&
77 getSupportedRouteMask() == state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070078 }
79
80 @Override
81 public String toString() {
82 return String.format(Locale.US,
Nancy Chenddf15a12014-12-02 15:59:35 -080083 "[AudioState isMuted: %b, route: %s, supportedRouteMask: %s]",
Ihab Awad5c9c86e2014-11-12 13:41:16 -080084 isMuted,
85 audioRouteToString(route),
86 audioRouteToString(supportedRouteMask));
Sailesh Nepal4cff3922014-03-19 10:15:37 -070087 }
88
Sailesh Nepal4cff3922014-03-19 10:15:37 -070089 public static String audioRouteToString(int route) {
90 if (route == 0 || (route & ~ROUTE_ALL) != 0x0) {
91 return "UNKNOWN";
92 }
93
94 StringBuffer buffer = new StringBuffer();
95 if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) {
96 listAppend(buffer, "EARPIECE");
97 }
98 if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) {
99 listAppend(buffer, "BLUETOOTH");
100 }
101 if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) {
102 listAppend(buffer, "WIRED_HEADSET");
103 }
104 if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) {
105 listAppend(buffer, "SPEAKER");
106 }
107
108 return buffer.toString();
109 }
110
111 private static void listAppend(StringBuffer buffer, String str) {
112 if (buffer.length() > 0) {
113 buffer.append(", ");
114 }
115 buffer.append(str);
116 }
117
118 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700119 * Responsible for creating AudioState objects for deserialized Parcels.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700120 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700121 public static final Parcelable.Creator<AudioState> CREATOR =
122 new Parcelable.Creator<AudioState> () {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700123
124 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700125 public AudioState createFromParcel(Parcel source) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700126 boolean isMuted = source.readByte() == 0 ? false : true;
127 int route = source.readInt();
128 int supportedRouteMask = source.readInt();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700129 return new AudioState(isMuted, route, supportedRouteMask);
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700130 }
131
132 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700133 public AudioState[] newArray(int size) {
134 return new AudioState[size];
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700135 }
136 };
137
138 /**
139 * {@inheritDoc}
140 */
141 @Override
142 public int describeContents() {
143 return 0;
144 }
145
146 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700147 * Writes AudioState object into a serializeable Parcel.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700148 */
149 @Override
150 public void writeToParcel(Parcel destination, int flags) {
151 destination.writeByte((byte) (isMuted ? 1 : 0));
152 destination.writeInt(route);
153 destination.writeInt(supportedRouteMask);
154 }
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800155
156 /**
157 * @return {@code true} if the call is muted, false otherwise.
158 */
159 public boolean isMuted() {
160 return isMuted;
161 }
162
163 /**
164 * @return The current audio route being used.
165 */
166 public int getRoute() {
167 return route;
168 }
169
170 /**
171 * @return Bit mask of all routes supported by this call.
172 */
173 public int getSupportedRouteMask() {
174 return supportedRouteMask;
175 }
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700176}