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