| 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 | /** |
| 25 | * Encapsulates all audio states during a call. |
| 26 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 27 | public final class AudioState implements Parcelable { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 28 | /** Direct the audio stream through the device's earpiece. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 29 | public static final int ROUTE_EARPIECE = 0x00000001; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 30 | |
| 31 | /** Direct the audio stream through Bluetooth. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 32 | public static final int ROUTE_BLUETOOTH = 0x00000002; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 33 | |
| 34 | /** Direct the audio stream through a wired headset. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 35 | public static final int ROUTE_WIRED_HEADSET = 0x00000004; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 36 | |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 37 | /** Direct the audio stream through the device's speakerphone. */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 38 | public static final int ROUTE_SPEAKER = 0x00000008; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Direct the audio stream through the device's earpiece or wired headset if one is |
| 42 | * connected. |
| 43 | */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 44 | 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] | 45 | |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 46 | /** Bit mask of all possible audio routes. |
| 47 | * |
| 48 | * @hide |
| 49 | */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 50 | public static final int ROUTE_ALL = ROUTE_EARPIECE | ROUTE_BLUETOOTH | ROUTE_WIRED_HEADSET | |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 51 | ROUTE_SPEAKER; |
| 52 | |
| 53 | /** True if the call is muted, false otherwise. */ |
| 54 | public final boolean isMuted; |
| 55 | |
| 56 | /** The route to use for the audio stream. */ |
| 57 | public final int route; |
| 58 | |
| 59 | /** Bit vector of all routes supported by this call. */ |
| 60 | public final int supportedRouteMask; |
| 61 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 62 | public AudioState(boolean isMuted, int route, int supportedRouteMask) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 63 | this.isMuted = isMuted; |
| 64 | this.route = route; |
| 65 | this.supportedRouteMask = supportedRouteMask; |
| 66 | } |
| 67 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 68 | public AudioState(AudioState state) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 69 | isMuted = state.isMuted; |
| 70 | route = state.route; |
| 71 | supportedRouteMask = state.supportedRouteMask; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean equals(Object obj) { |
| 76 | if (obj == null) { |
| 77 | return false; |
| 78 | } |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 79 | if (!(obj instanceof AudioState)) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 82 | AudioState state = (AudioState) obj; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 83 | return isMuted == state.isMuted && route == state.route && |
| 84 | supportedRouteMask == state.supportedRouteMask; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return String.format(Locale.US, |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 90 | "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]", |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 91 | isMuted, audioRouteToString(route), audioRouteToString(supportedRouteMask)); |
| 92 | } |
| 93 | |
| 94 | /** @hide */ |
| 95 | public static String audioRouteToString(int route) { |
| 96 | if (route == 0 || (route & ~ROUTE_ALL) != 0x0) { |
| 97 | return "UNKNOWN"; |
| 98 | } |
| 99 | |
| 100 | StringBuffer buffer = new StringBuffer(); |
| 101 | if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) { |
| 102 | listAppend(buffer, "EARPIECE"); |
| 103 | } |
| 104 | if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) { |
| 105 | listAppend(buffer, "BLUETOOTH"); |
| 106 | } |
| 107 | if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) { |
| 108 | listAppend(buffer, "WIRED_HEADSET"); |
| 109 | } |
| 110 | if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) { |
| 111 | listAppend(buffer, "SPEAKER"); |
| 112 | } |
| 113 | |
| 114 | return buffer.toString(); |
| 115 | } |
| 116 | |
| 117 | private static void listAppend(StringBuffer buffer, String str) { |
| 118 | if (buffer.length() > 0) { |
| 119 | buffer.append(", "); |
| 120 | } |
| 121 | buffer.append(str); |
| 122 | } |
| 123 | |
| 124 | /** |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 125 | * Responsible for creating AudioState objects for deserialized Parcels. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 126 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 127 | public static final Parcelable.Creator<AudioState> CREATOR = |
| 128 | new Parcelable.Creator<AudioState> () { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 129 | |
| 130 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 131 | public AudioState createFromParcel(Parcel source) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 132 | boolean isMuted = source.readByte() == 0 ? false : true; |
| 133 | int route = source.readInt(); |
| 134 | int supportedRouteMask = source.readInt(); |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 135 | return new AudioState(isMuted, route, supportedRouteMask); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 139 | public AudioState[] newArray(int size) { |
| 140 | return new AudioState[size]; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * {@inheritDoc} |
| 146 | */ |
| 147 | @Override |
| 148 | public int describeContents() { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /** |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 153 | * Writes AudioState object into a serializeable Parcel. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 154 | */ |
| 155 | @Override |
| 156 | public void writeToParcel(Parcel destination, int flags) { |
| 157 | destination.writeByte((byte) (isMuted ? 1 : 0)); |
| 158 | destination.writeInt(route); |
| 159 | destination.writeInt(supportedRouteMask); |
| 160 | } |
| 161 | } |