| 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 | |
| Nancy Chen | ea38cca | 2014-09-05 16:38:49 -0700 | [diff] [blame] | 47 | /** Bit mask of all possible audio routes. |
| 48 | * |
| 49 | * @hide |
| 50 | */ |
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 51 | 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] | 52 | ROUTE_SPEAKER; |
| 53 | |
| 54 | /** True if the call is muted, false otherwise. */ |
| 55 | public final boolean isMuted; |
| 56 | |
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 57 | /** The current audio route being used. */ |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 58 | public final int route; |
| 59 | |
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 60 | /** Bit mask of all routes supported by this call. */ |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 61 | public final int supportedRouteMask; |
| 62 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 63 | public AudioState(boolean isMuted, int route, int supportedRouteMask) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 64 | this.isMuted = isMuted; |
| 65 | this.route = route; |
| 66 | this.supportedRouteMask = supportedRouteMask; |
| 67 | } |
| 68 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 69 | public AudioState(AudioState state) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 70 | 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 80 | if (!(obj instanceof AudioState)) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 83 | AudioState state = (AudioState) obj; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 84 | 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 91 | "[AudioState isMuted: %b, route; %s, supportedRouteMask: %s]", |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 92 | 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 126 | * Responsible for creating AudioState objects for deserialized Parcels. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 127 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 128 | public static final Parcelable.Creator<AudioState> CREATOR = |
| 129 | new Parcelable.Creator<AudioState> () { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 132 | public AudioState createFromParcel(Parcel source) { |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 133 | boolean isMuted = source.readByte() == 0 ? false : true; |
| 134 | int route = source.readInt(); |
| 135 | int supportedRouteMask = source.readInt(); |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 136 | return new AudioState(isMuted, route, supportedRouteMask); |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 140 | public AudioState[] newArray(int size) { |
| 141 | return new AudioState[size]; |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * {@inheritDoc} |
| 147 | */ |
| 148 | @Override |
| 149 | public int describeContents() { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /** |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 154 | * Writes AudioState object into a serializeable Parcel. |
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 155 | */ |
| 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 | } |