| 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 |  | 
| Aurimas Liutikas | 00be951 | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; | 
 | 20 | import android.annotation.Nullable; | 
| Yorke Lee | 4af5935 | 2015-05-13 14:14:54 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; | 
| Artur Satayev | 2ebb31c | 2020-01-08 12:24:36 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; | 
| Tyler Gunn | 17933eb | 2019-03-05 13:58:45 -0800 | [diff] [blame] | 23 | import android.os.Build; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 24 | import android.os.Parcel; | 
 | 25 | import android.os.Parcelable; | 
 | 26 |  | 
 | 27 | import java.util.Locale; | 
 | 28 |  | 
 | 29 | /** | 
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 30 |  *  Encapsulates the telecom audio state, including the current audio routing, supported audio | 
 | 31 |  *  routing and mute. | 
| Yorke Lee | 4af5935 | 2015-05-13 14:14:54 -0700 | [diff] [blame] | 32 |  *  @deprecated - use {@link CallAudioState} instead. | 
 | 33 |  *  @hide | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 34 |  */ | 
| Yorke Lee | 4af5935 | 2015-05-13 14:14:54 -0700 | [diff] [blame] | 35 | @Deprecated | 
 | 36 | @SystemApi | 
 | 37 | public class AudioState implements Parcelable { | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 38 |     /** Direct the audio stream through the device's earpiece. */ | 
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 39 |     public static final int ROUTE_EARPIECE      = 0x00000001; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 40 |  | 
 | 41 |     /** Direct the audio stream through Bluetooth. */ | 
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 42 |     public static final int ROUTE_BLUETOOTH     = 0x00000002; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 43 |  | 
 | 44 |     /** Direct the audio stream through a wired headset. */ | 
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 45 |     public static final int ROUTE_WIRED_HEADSET = 0x00000004; | 
| 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 |     /** Direct the audio stream through the device's speakerphone. */ | 
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 48 |     public static final int ROUTE_SPEAKER       = 0x00000008; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 49 |  | 
 | 50 |     /** | 
 | 51 |      * Direct the audio stream through the device's earpiece or wired headset if one is | 
 | 52 |      * connected. | 
 | 53 |      */ | 
| Yorke Lee | 1426048 | 2014-08-20 16:16:26 -0700 | [diff] [blame] | 54 |     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] | 55 |  | 
| Jay Shrauner | 55b9752 | 2015-04-09 15:15:43 -0700 | [diff] [blame] | 56 |     /** Bit mask of all possible audio routes. */ | 
 | 57 |     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] | 58 |             ROUTE_SPEAKER; | 
 | 59 |  | 
| Tyler Gunn | 17933eb | 2019-03-05 13:58:45 -0800 | [diff] [blame] | 60 |     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) | 
| Jay Shrauner | 164a0ac | 2015-04-14 18:16:10 -0700 | [diff] [blame] | 61 |     private final boolean isMuted; | 
| Tyler Gunn | 17933eb | 2019-03-05 13:58:45 -0800 | [diff] [blame] | 62 |     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) | 
| Jay Shrauner | 164a0ac | 2015-04-14 18:16:10 -0700 | [diff] [blame] | 63 |     private final int route; | 
| Tyler Gunn | 17933eb | 2019-03-05 13:58:45 -0800 | [diff] [blame] | 64 |     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) | 
| Jay Shrauner | 164a0ac | 2015-04-14 18:16:10 -0700 | [diff] [blame] | 65 |     private final int supportedRouteMask; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 66 |  | 
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 67 |     public AudioState(boolean muted, int route, int supportedRouteMask) { | 
 | 68 |         this.isMuted = muted; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 69 |         this.route = route; | 
 | 70 |         this.supportedRouteMask = supportedRouteMask; | 
 | 71 |     } | 
 | 72 |  | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 73 |     public AudioState(AudioState state) { | 
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 74 |         isMuted = state.isMuted(); | 
 | 75 |         route = state.getRoute(); | 
 | 76 |         supportedRouteMask = state.getSupportedRouteMask(); | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 77 |     } | 
 | 78 |  | 
| Yorke Lee | 4af5935 | 2015-05-13 14:14:54 -0700 | [diff] [blame] | 79 |     public AudioState(CallAudioState state) { | 
 | 80 |         isMuted = state.isMuted(); | 
 | 81 |         route = state.getRoute(); | 
 | 82 |         supportedRouteMask = state.getSupportedRouteMask(); | 
 | 83 |     } | 
 | 84 |  | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 85 |     @Override | 
| Aurimas Liutikas | 00be951 | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 86 |     public boolean equals(@Nullable Object obj) { | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 87 |         if (obj == null) { | 
 | 88 |             return false; | 
 | 89 |         } | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 90 |         if (!(obj instanceof AudioState)) { | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 91 |             return false; | 
 | 92 |         } | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 93 |         AudioState state = (AudioState) obj; | 
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 94 |         return isMuted() == state.isMuted() && getRoute() == state.getRoute() && | 
 | 95 |                 getSupportedRouteMask() == state.getSupportedRouteMask(); | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 96 |     } | 
 | 97 |  | 
| Aurimas Liutikas | 00be951 | 2019-08-28 13:01:05 -0700 | [diff] [blame] | 98 |     @NonNull | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 99 |     @Override | 
 | 100 |     public String toString() { | 
 | 101 |         return String.format(Locale.US, | 
| Nancy Chen | ddf15a1 | 2014-12-02 15:59:35 -0800 | [diff] [blame] | 102 |                 "[AudioState isMuted: %b, route: %s, supportedRouteMask: %s]", | 
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 103 |                 isMuted, | 
 | 104 |                 audioRouteToString(route), | 
 | 105 |                 audioRouteToString(supportedRouteMask)); | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 106 |     } | 
 | 107 |  | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 108 |     public static String audioRouteToString(int route) { | 
 | 109 |         if (route == 0 || (route & ~ROUTE_ALL) != 0x0) { | 
 | 110 |             return "UNKNOWN"; | 
 | 111 |         } | 
 | 112 |  | 
 | 113 |         StringBuffer buffer = new StringBuffer(); | 
 | 114 |         if ((route & ROUTE_EARPIECE) == ROUTE_EARPIECE) { | 
 | 115 |             listAppend(buffer, "EARPIECE"); | 
 | 116 |         } | 
 | 117 |         if ((route & ROUTE_BLUETOOTH) == ROUTE_BLUETOOTH) { | 
 | 118 |             listAppend(buffer, "BLUETOOTH"); | 
 | 119 |         } | 
 | 120 |         if ((route & ROUTE_WIRED_HEADSET) == ROUTE_WIRED_HEADSET) { | 
 | 121 |             listAppend(buffer, "WIRED_HEADSET"); | 
 | 122 |         } | 
 | 123 |         if ((route & ROUTE_SPEAKER) == ROUTE_SPEAKER) { | 
 | 124 |             listAppend(buffer, "SPEAKER"); | 
 | 125 |         } | 
 | 126 |  | 
 | 127 |         return buffer.toString(); | 
 | 128 |     } | 
 | 129 |  | 
 | 130 |     private static void listAppend(StringBuffer buffer, String str) { | 
 | 131 |         if (buffer.length() > 0) { | 
 | 132 |             buffer.append(", "); | 
 | 133 |         } | 
 | 134 |         buffer.append(str); | 
 | 135 |     } | 
 | 136 |  | 
 | 137 |     /** | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 138 |      * Responsible for creating AudioState objects for deserialized Parcels. | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 139 |      */ | 
| Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 140 |     public static final @android.annotation.NonNull Parcelable.Creator<AudioState> CREATOR = | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 141 |             new Parcelable.Creator<AudioState> () { | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 142 |  | 
 | 143 |         @Override | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 144 |         public AudioState createFromParcel(Parcel source) { | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 145 |             boolean isMuted = source.readByte() == 0 ? false : true; | 
 | 146 |             int route = source.readInt(); | 
 | 147 |             int supportedRouteMask = source.readInt(); | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 148 |             return new AudioState(isMuted, route, supportedRouteMask); | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 149 |         } | 
 | 150 |  | 
 | 151 |         @Override | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 152 |         public AudioState[] newArray(int size) { | 
 | 153 |             return new AudioState[size]; | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 154 |         } | 
 | 155 |     }; | 
 | 156 |  | 
 | 157 |     /** | 
 | 158 |      * {@inheritDoc} | 
 | 159 |      */ | 
 | 160 |     @Override | 
 | 161 |     public int describeContents() { | 
 | 162 |         return 0; | 
 | 163 |     } | 
 | 164 |  | 
 | 165 |     /** | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 166 |      * Writes AudioState object into a serializeable Parcel. | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 167 |      */ | 
 | 168 |     @Override | 
 | 169 |     public void writeToParcel(Parcel destination, int flags) { | 
 | 170 |         destination.writeByte((byte) (isMuted ? 1 : 0)); | 
 | 171 |         destination.writeInt(route); | 
 | 172 |         destination.writeInt(supportedRouteMask); | 
 | 173 |     } | 
| Ihab Awad | 5c9c86e | 2014-11-12 13:41:16 -0800 | [diff] [blame] | 174 |  | 
 | 175 |     /** | 
 | 176 |      * @return {@code true} if the call is muted, false otherwise. | 
 | 177 |      */ | 
 | 178 |     public boolean isMuted() { | 
 | 179 |         return isMuted; | 
 | 180 |     } | 
 | 181 |  | 
 | 182 |     /** | 
 | 183 |      * @return The current audio route being used. | 
 | 184 |      */ | 
 | 185 |     public int getRoute() { | 
 | 186 |         return route; | 
 | 187 |     } | 
 | 188 |  | 
 | 189 |     /** | 
 | 190 |      * @return Bit mask of all routes supported by this call. | 
 | 191 |      */ | 
 | 192 |     public int getSupportedRouteMask() { | 
 | 193 |         return supportedRouteMask; | 
 | 194 |     } | 
| Sailesh Nepal | 4cff392 | 2014-03-19 10:15:37 -0700 | [diff] [blame] | 195 | } |