blob: 2a507d0edc276c98627388e31a2a8782da1776a1 [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
Tyler Gunn711d876fd2014-09-19 11:17:02 -070019import 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 Cordond3a0b162014-10-28 13:10:36 -070026 * Encapsulates the telecom audio state, including the current audio routing, supported audio
27 * routing and mute.
Tyler Gunn711d876fd2014-09-19 11:17:02 -070028 * @hide
Sailesh Nepal4cff3922014-03-19 10:15:37 -070029 */
Tyler Gunn711d876fd2014-09-19 11:17:02 -070030@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
Divya Sharma1cb8f692015-02-24 16:25:55 -080057 /** Note: Deprecated, please do not use if possible. */
58 @SystemApi public final boolean isMuted;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070059
Divya Sharma1cb8f692015-02-24 16:25:55 -080060 /** Note: Deprecated, please do not use if possible. */
61 @SystemApi public final int route;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070062
Divya Sharma1cb8f692015-02-24 16:25:55 -080063 /** Note: Deprecated, please do not use if possible. */
64 @SystemApi public final int supportedRouteMask;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070065
Ihab Awad4a0d2722014-11-12 13:41:16 -080066 public AudioState(boolean muted, int route, int supportedRouteMask) {
67 this.isMuted = muted;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070068 this.route = route;
69 this.supportedRouteMask = supportedRouteMask;
70 }
71
Ihab Awadb19a0bc2014-08-07 19:46:01 -070072 public AudioState(AudioState state) {
Ihab Awad4a0d2722014-11-12 13:41:16 -080073 isMuted = state.isMuted();
74 route = state.getRoute();
75 supportedRouteMask = state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070076 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (obj == null) {
81 return false;
82 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070083 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070084 return false;
85 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070086 AudioState state = (AudioState) obj;
Ihab Awad4a0d2722014-11-12 13:41:16 -080087 return isMuted() == state.isMuted() && getRoute() == state.getRoute() &&
88 getSupportedRouteMask() == state.getSupportedRouteMask();
Sailesh Nepal4cff3922014-03-19 10:15:37 -070089 }
90
91 @Override
92 public String toString() {
93 return String.format(Locale.US,
Ihab Awadb19a0bc2014-08-07 19:46:01 -070094 "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]",
Ihab Awad4a0d2722014-11-12 13:41:16 -080095 isMuted,
96 audioRouteToString(route),
97 audioRouteToString(supportedRouteMask));
Sailesh Nepal4cff3922014-03-19 10:15:37 -070098 }
99
100 /** @hide */
101 public static String audioRouteToString(int route) {
102 if (route == 0 || (route & ~ROUTE_ALL) != 0x0) {
103 return "UNKNOWN";
104 }
105
106 StringBuffer buffer = new StringBuffer();
107 if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) {
108 listAppend(buffer, "EARPIECE");
109 }
110 if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) {
111 listAppend(buffer, "BLUETOOTH");
112 }
113 if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) {
114 listAppend(buffer, "WIRED_HEADSET");
115 }
116 if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) {
117 listAppend(buffer, "SPEAKER");
118 }
119
120 return buffer.toString();
121 }
122
123 private static void listAppend(StringBuffer buffer, String str) {
124 if (buffer.length() > 0) {
125 buffer.append(", ");
126 }
127 buffer.append(str);
128 }
129
130 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700131 * Responsible for creating AudioState objects for deserialized Parcels.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700132 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700133 public static final Parcelable.Creator<AudioState> CREATOR =
134 new Parcelable.Creator<AudioState> () {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700135
136 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700137 public AudioState createFromParcel(Parcel source) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700138 boolean isMuted = source.readByte() == 0 ? false : true;
139 int route = source.readInt();
140 int supportedRouteMask = source.readInt();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700141 return new AudioState(isMuted, route, supportedRouteMask);
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700142 }
143
144 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700145 public AudioState[] newArray(int size) {
146 return new AudioState[size];
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700147 }
148 };
149
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 public int describeContents() {
155 return 0;
156 }
157
158 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700159 * Writes AudioState object into a serializeable Parcel.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700160 */
161 @Override
162 public void writeToParcel(Parcel destination, int flags) {
163 destination.writeByte((byte) (isMuted ? 1 : 0));
164 destination.writeInt(route);
165 destination.writeInt(supportedRouteMask);
166 }
Ihab Awad4a0d2722014-11-12 13:41:16 -0800167
168 /**
169 * @return {@code true} if the call is muted, false otherwise.
170 */
171 public boolean isMuted() {
172 return isMuted;
173 }
174
175 /**
176 * @return The current audio route being used.
177 */
178 public int getRoute() {
179 return route;
180 }
181
182 /**
183 * @return Bit mask of all routes supported by this call.
184 */
185 public int getSupportedRouteMask() {
186 return supportedRouteMask;
187 }
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700188}