blob: 43da38f3648cc7714ae0b666cb6674ee11348035 [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
Nancy Chenea38cca2014-09-05 16:38:49 -070047 /** Bit mask of all possible audio routes.
48 *
49 * @hide
50 */
Yorke Lee14260482014-08-20 16:16:26 -070051 public static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET |
Sailesh Nepal4cff3922014-03-19 10:15:37 -070052 ROUTE_SPEAKER;
53
54 /** True if the call is muted, false otherwise. */
55 public final boolean isMuted;
56
Santos Cordond9e614f2014-10-28 13:10:36 -070057 /** The current audio route being used. */
Sailesh Nepal4cff3922014-03-19 10:15:37 -070058 public final int route;
59
Santos Cordond9e614f2014-10-28 13:10:36 -070060 /** Bit mask of all routes supported by this call. */
Sailesh Nepal4cff3922014-03-19 10:15:37 -070061 public final int supportedRouteMask;
62
Ihab Awadb19a0bc2014-08-07 19:46:01 -070063 public AudioState(boolean isMuted, int route, int supportedRouteMask) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070064 this.isMuted = isMuted;
65 this.route = route;
66 this.supportedRouteMask = supportedRouteMask;
67 }
68
Ihab Awadb19a0bc2014-08-07 19:46:01 -070069 public AudioState(AudioState state) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070070 isMuted = state.isMuted;
71 route = state.route;
72 supportedRouteMask = state.supportedRouteMask;
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (obj == null) {
78 return false;
79 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070080 if (!(obj instanceof AudioState)) {
Sailesh Nepal4cff3922014-03-19 10:15:37 -070081 return false;
82 }
Ihab Awadb19a0bc2014-08-07 19:46:01 -070083 AudioState state = (AudioState) obj;
Sailesh Nepal4cff3922014-03-19 10:15:37 -070084 return isMuted == state.isMuted && route == state.route &&
85 supportedRouteMask == state.supportedRouteMask;
86 }
87
88 @Override
89 public String toString() {
90 return String.format(Locale.US,
Ihab Awadb19a0bc2014-08-07 19:46:01 -070091 "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]",
Sailesh Nepal4cff3922014-03-19 10:15:37 -070092 isMuted, audioRouteToString(route), audioRouteToString(supportedRouteMask));
93 }
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 }
162}