blob: fc2fff4ace2e028b4b0ded8bfe341a18512aa1d5 [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/**
26 * Encapsulates all audio states during a call.
Tyler Gunn711d876fd2014-09-19 11:17:02 -070027 * @hide
Sailesh Nepal4cff3922014-03-19 10:15:37 -070028 */
Tyler Gunn711d876fd2014-09-19 11:17:02 -070029@SystemApi
Ihab Awadb19a0bc2014-08-07 19:46:01 -070030public final class AudioState implements Parcelable {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070031 /** Direct the audio stream through the device's earpiece. */
Yorke Lee14260482014-08-20 16:16:26 -070032 public static final int ROUTE_EARPIECE = 0x00000001;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070033
34 /** Direct the audio stream through Bluetooth. */
Yorke Lee14260482014-08-20 16:16:26 -070035 public static final int ROUTE_BLUETOOTH = 0x00000002;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070036
37 /** Direct the audio stream through a wired headset. */
Yorke Lee14260482014-08-20 16:16:26 -070038 public static final int ROUTE_WIRED_HEADSET = 0x00000004;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070039
Nancy Chenea38cca2014-09-05 16:38:49 -070040 /** Direct the audio stream through the device's speakerphone. */
Yorke Lee14260482014-08-20 16:16:26 -070041 public static final int ROUTE_SPEAKER = 0x00000008;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070042
43 /**
44 * Direct the audio stream through the device's earpiece or wired headset if one is
45 * connected.
46 */
Yorke Lee14260482014-08-20 16:16:26 -070047 public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070048
Nancy Chenea38cca2014-09-05 16:38:49 -070049 /** Bit mask of all possible audio routes.
50 *
51 * @hide
52 */
Yorke Lee14260482014-08-20 16:16:26 -070053 public static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
Sailesh Nepal4cff3922014-03-19 10:15:37 -070054 ROUTE_SPEAKER;
55
56 /** True if the call is muted, false otherwise. */
57 public final boolean isMuted;
58
59 /** The route to use for the audio stream. */
60 public final int route;
61
62 /** Bit vector of all routes supported by this call. */
63 public final int supportedRouteMask;
64
Ihab Awadb19a0bc2014-08-07 19:46:01 -070065 public AudioState(boolean isMuted, int route, int supportedRouteMask) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070066 this.isMuted = isMuted;
67 this.route = route;
68 this.supportedRouteMask = supportedRouteMask;
69 }
70
Ihab Awadb19a0bc2014-08-07 19:46:01 -070071 public AudioState(AudioState state) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070072 isMuted = state.isMuted;
73 route = state.route;
74 supportedRouteMask = state.supportedRouteMask;
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (obj == null) {
80 return false;
81 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070082 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070083 return false;
84 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070085 AudioState state = (AudioState) obj;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070086 return isMuted == state.isMuted && route == state.route &&
87 supportedRouteMask == state.supportedRouteMask;
88 }
89
90 @Override
91 public String toString() {
92 return String.format(Locale.US,
Ihab Awadb19a0bc2014-08-07 19:46:01 -070093 "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]",
Sailesh Nepal4cff3922014-03-19 10:15:37 -070094 isMuted, audioRouteToString(route), audioRouteToString(supportedRouteMask));
95 }
96
97 /** @hide */
98 public static String audioRouteToString(int route) {
99 if (route == 0 || (route & ~ROUTE_ALL) != 0x0) {
100 return "UNKNOWN";
101 }
102
103 StringBuffer buffer = new StringBuffer();
104 if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) {
105 listAppend(buffer, "EARPIECE");
106 }
107 if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) {
108 listAppend(buffer, "BLUETOOTH");
109 }
110 if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) {
111 listAppend(buffer, "WIRED_HEADSET");
112 }
113 if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) {
114 listAppend(buffer, "SPEAKER");
115 }
116
117 return buffer.toString();
118 }
119
120 private static void listAppend(StringBuffer buffer, String str) {
121 if (buffer.length() > 0) {
122 buffer.append(", ");
123 }
124 buffer.append(str);
125 }
126
127 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700128 * Responsible for creating AudioState objects for deserialized Parcels.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700129 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700130 public static final Parcelable.Creator<AudioState> CREATOR =
131 new Parcelable.Creator<AudioState> () {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700132
133 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700134 public AudioState createFromParcel(Parcel source) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700135 boolean isMuted = source.readByte() == 0 ? false : true;
136 int route = source.readInt();
137 int supportedRouteMask = source.readInt();
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700138 return new AudioState(isMuted, route, supportedRouteMask);
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700139 }
140
141 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700142 public AudioState[] newArray(int size) {
143 return new AudioState[size];
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700144 }
145 };
146
147 /**
148 * {@inheritDoc}
149 */
150 @Override
151 public int describeContents() {
152 return 0;
153 }
154
155 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700156 * Writes AudioState object into a serializeable Parcel.
Sailesh Nepal4cff3922014-03-19 10:15:37 -0700157 */
158 @Override
159 public void writeToParcel(Parcel destination, int flags) {
160 destination.writeByte((byte) (isMuted ? 1 : 0));
161 destination.writeInt(route);
162 destination.writeInt(supportedRouteMask);
163 }
164}