| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
| 22 | import java.util.Locale; |
| 23 | |
| 24 | /** |
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 25 | * Encapsulates the telecom audio state, including the current audio routing, supported audio |
| 26 | * routing and mute. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 27 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 28 | public final class AudioState implements Parcelable { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 29 | /** Direct the audio stream through the device's earpiece. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 30 | public static final int ROUTE_EARPIECE = 0x00000001; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 31 | |
| 32 | /** Direct the audio stream through Bluetooth. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 33 | public static final int ROUTE_BLUETOOTH = 0x00000002; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 34 | |
| 35 | /** Direct the audio stream through a wired headset. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 36 | public static final int ROUTE_WIRED_HEADSET = 0x00000004; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 37 | |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 38 | /** Direct the audio stream through the device's speakerphone. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 39 | public static final int ROUTE_SPEAKER = 0x00000008; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Direct the audio stream through the device's earpiece or wired headset if one is |
| 43 | * connected. |
| 44 | */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 45 | public static final int ROUTE_WIRED_OR_EARPIECE = ROUTE_EARPIECE | ROUTE_WIRED_HEADSET; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 46 | |
| Jay Shrauner | 1cf9b6b | 2015-04-09 15:15:43 -0700 | [diff] [blame] | 47 | /** Bit mask of all possible audio routes. */ |
| 48 | private static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET | |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 49 | ROUTE_SPEAKER; |
| 50 | |
| Jay Shrauner | 193de66 | 2015-04-14 18:16:10 -0700 | [diff] [blame] | 51 | private final boolean isMuted; |
| 52 | private final int route; |
| 53 | private final int supportedRouteMask; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 54 | |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 55 | public AudioState(boolean muted, int route, int supportedRouteMask) { |
| 56 | this.isMuted = muted; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 57 | this.route = route; |
| 58 | this.supportedRouteMask = supportedRouteMask; |
| 59 | } |
| 60 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 61 | public AudioState(AudioState state) { |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 62 | isMuted = state.isMuted(); |
| 63 | route = state.getRoute(); |
| 64 | supportedRouteMask = state.getSupportedRouteMask(); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean equals(Object obj) { |
| 69 | if (obj == null) { |
| 70 | return false; |
| 71 | } |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 72 | if (!(obj instanceof AudioState)) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 75 | AudioState state = (AudioState) obj; |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 76 | return isMuted() == state.isMuted() && getRoute() == state.getRoute() && |
| 77 | getSupportedRouteMask() == state.getSupportedRouteMask(); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public String toString() { |
| 82 | return String.format(Locale.US, |
| Nancy Chen | ddf15a1 | 2014-12-02 15:59:35 -0800 | [diff] [blame] | 83 | "[AudioState isMuted: %b, route: %s, supportedRouteMask: %s]", |
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 84 | isMuted, |
| 85 | audioRouteToString(route), |
| 86 | audioRouteToString(supportedRouteMask)); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 89 | 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 119 | * Responsible for creating AudioState objects for deserialized Parcels. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 120 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 121 | public static final Parcelable.Creator<AudioState> CREATOR = |
| 122 | new Parcelable.Creator<AudioState> () { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 123 | |
| 124 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 125 | public AudioState createFromParcel(Parcel source) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 126 | boolean isMuted = source.readByte() == 0 ? false : true; |
| 127 | int route = source.readInt(); |
| 128 | int supportedRouteMask = source.readInt(); |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 129 | return new AudioState(isMuted, route, supportedRouteMask); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 133 | public AudioState[] newArray(int size) { |
| 134 | return new AudioState[size]; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * {@inheritDoc} |
| 140 | */ |
| 141 | @Override |
| 142 | public int describeContents() { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /** |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 147 | * Writes AudioState object into a serializeable Parcel. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 148 | */ |
| 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 Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 155 | |
| 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 Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 176 | } |