1 /* 2 * Copyright (C) 2017 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 17 package android.bluetooth; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.util.Collections; 25 import java.util.List; 26 import java.util.Objects; 27 28 /** 29 * Represents the codec status (configuration and capability) for a Bluetooth 30 * A2DP source device. 31 * 32 * {@see BluetoothA2dp} 33 */ 34 public final class BluetoothCodecStatus implements Parcelable { 35 /** 36 * Extra for the codec configuration intents of the individual profiles. 37 * 38 * This extra represents the current codec status of the A2DP 39 * profile. 40 */ 41 public static final String EXTRA_CODEC_STATUS = 42 "android.bluetooth.extra.CODEC_STATUS"; 43 44 private final @Nullable BluetoothCodecConfig mCodecConfig; 45 private final @Nullable List<BluetoothCodecConfig> mCodecsLocalCapabilities; 46 private final @Nullable List<BluetoothCodecConfig> mCodecsSelectableCapabilities; 47 48 /** 49 * Creates a new BluetoothCodecStatus. 50 * 51 * @hide 52 */ BluetoothCodecStatus(@ullable BluetoothCodecConfig codecConfig, @Nullable List<BluetoothCodecConfig> codecsLocalCapabilities, @Nullable List<BluetoothCodecConfig> codecsSelectableCapabilities)53 public BluetoothCodecStatus(@Nullable BluetoothCodecConfig codecConfig, 54 @Nullable List<BluetoothCodecConfig> codecsLocalCapabilities, 55 @Nullable List<BluetoothCodecConfig> codecsSelectableCapabilities) { 56 mCodecConfig = codecConfig; 57 mCodecsLocalCapabilities = codecsLocalCapabilities; 58 mCodecsSelectableCapabilities = codecsSelectableCapabilities; 59 } 60 BluetoothCodecStatus(Parcel in)61 private BluetoothCodecStatus(Parcel in) { 62 mCodecConfig = in.readTypedObject(BluetoothCodecConfig.CREATOR); 63 mCodecsLocalCapabilities = in.createTypedArrayList(BluetoothCodecConfig.CREATOR); 64 mCodecsSelectableCapabilities = in.createTypedArrayList(BluetoothCodecConfig.CREATOR); 65 } 66 67 @Override equals(@ullable Object o)68 public boolean equals(@Nullable Object o) { 69 if (o instanceof BluetoothCodecStatus) { 70 BluetoothCodecStatus other = (BluetoothCodecStatus) o; 71 return (Objects.equals(other.mCodecConfig, mCodecConfig) 72 && sameCapabilities(other.mCodecsLocalCapabilities, mCodecsLocalCapabilities) 73 && sameCapabilities(other.mCodecsSelectableCapabilities, 74 mCodecsSelectableCapabilities)); 75 } 76 return false; 77 } 78 79 /** 80 * Checks whether two lists of capabilities contain same capabilities. 81 * The order of the capabilities in each list is ignored. 82 * 83 * @param c1 the first list of capabilities to compare 84 * @param c2 the second list of capabilities to compare 85 * @return {@code true} if both lists contain same capabilities 86 */ sameCapabilities(@ullable List<BluetoothCodecConfig> c1, @Nullable List<BluetoothCodecConfig> c2)87 private static boolean sameCapabilities(@Nullable List<BluetoothCodecConfig> c1, 88 @Nullable List<BluetoothCodecConfig> c2) { 89 if (c1 == null) { 90 return (c2 == null); 91 } 92 if (c2 == null) { 93 return false; 94 } 95 if (c1.size() != c2.size()) { 96 return false; 97 } 98 return c1.containsAll(c2); 99 } 100 101 /** 102 * Checks whether the codec config matches the selectable capabilities. 103 * Any parameters of the codec config with NONE value will be considered a wildcard matching. 104 * 105 * @param codecConfig the codec config to compare against 106 * @return {@code true} if the codec config matches, {@code false} otherwise 107 */ isCodecConfigSelectable(@ullable BluetoothCodecConfig codecConfig)108 public boolean isCodecConfigSelectable(@Nullable BluetoothCodecConfig codecConfig) { 109 if (codecConfig == null || !codecConfig.hasSingleSampleRate() 110 || !codecConfig.hasSingleBitsPerSample() || !codecConfig.hasSingleChannelMode()) { 111 return false; 112 } 113 for (BluetoothCodecConfig selectableConfig : mCodecsSelectableCapabilities) { 114 if (codecConfig.getCodecType() != selectableConfig.getCodecType()) { 115 continue; 116 } 117 int sampleRate = codecConfig.getSampleRate(); 118 if ((sampleRate & selectableConfig.getSampleRate()) == 0 119 && sampleRate != BluetoothCodecConfig.SAMPLE_RATE_NONE) { 120 continue; 121 } 122 int bitsPerSample = codecConfig.getBitsPerSample(); 123 if ((bitsPerSample & selectableConfig.getBitsPerSample()) == 0 124 && bitsPerSample != BluetoothCodecConfig.BITS_PER_SAMPLE_NONE) { 125 continue; 126 } 127 int channelMode = codecConfig.getChannelMode(); 128 if ((channelMode & selectableConfig.getChannelMode()) == 0 129 && channelMode != BluetoothCodecConfig.CHANNEL_MODE_NONE) { 130 continue; 131 } 132 return true; 133 } 134 return false; 135 } 136 137 /** 138 * Returns a hash based on the codec config and local capabilities. 139 */ 140 @Override hashCode()141 public int hashCode() { 142 return Objects.hash(mCodecConfig, mCodecsLocalCapabilities, 143 mCodecsLocalCapabilities); 144 } 145 146 /** 147 * Returns a {@link String} that describes each BluetoothCodecStatus parameter 148 * current value. 149 */ 150 @Override toString()151 public String toString() { 152 return "{mCodecConfig:" + mCodecConfig 153 + ",mCodecsLocalCapabilities:" + mCodecsLocalCapabilities 154 + ",mCodecsSelectableCapabilities:" + mCodecsSelectableCapabilities 155 + "}"; 156 } 157 158 /** 159 * @return 0 160 * @hide 161 */ 162 @Override describeContents()163 public int describeContents() { 164 return 0; 165 } 166 167 public static final @NonNull Creator<BluetoothCodecStatus> CREATOR = new Creator<>() { 168 public BluetoothCodecStatus createFromParcel(Parcel in) { 169 return new BluetoothCodecStatus(in); 170 } 171 172 public BluetoothCodecStatus[] newArray(int size) { 173 return new BluetoothCodecStatus[size]; 174 } 175 }; 176 177 /** 178 * Flattens the object to a parcel. 179 * 180 * @param out The Parcel in which the object should be written 181 * @param flags Additional flags about how the object should be written 182 */ 183 @Override writeToParcel(@onNull Parcel out, int flags)184 public void writeToParcel(@NonNull Parcel out, int flags) { 185 out.writeTypedObject(mCodecConfig, 0); 186 out.writeTypedList(mCodecsLocalCapabilities); 187 out.writeTypedList(mCodecsSelectableCapabilities); 188 } 189 190 /** 191 * Returns the current codec configuration. 192 */ getCodecConfig()193 public @Nullable BluetoothCodecConfig getCodecConfig() { 194 return mCodecConfig; 195 } 196 197 /** 198 * Returns the codecs local capabilities. 199 */ getCodecsLocalCapabilities()200 public @NonNull List<BluetoothCodecConfig> getCodecsLocalCapabilities() { 201 return (mCodecsLocalCapabilities == null) 202 ? Collections.emptyList() : mCodecsLocalCapabilities; 203 } 204 205 /** 206 * Returns the codecs selectable capabilities. 207 */ getCodecsSelectableCapabilities()208 public @NonNull List<BluetoothCodecConfig> getCodecsSelectableCapabilities() { 209 return (mCodecsSelectableCapabilities == null) 210 ? Collections.emptyList() : mCodecsSelectableCapabilities; 211 } 212 213 /** 214 * Builder for {@link BluetoothCodecStatus}. 215 */ 216 public static final class Builder { 217 private BluetoothCodecConfig mCodecConfig = null; 218 private List<BluetoothCodecConfig> mCodecsLocalCapabilities = null; 219 private List<BluetoothCodecConfig> mCodecsSelectableCapabilities = null; 220 221 /** 222 * Set Bluetooth codec config for this codec status. 223 * 224 * @param codecConfig of this codec status 225 * @return the same Builder instance 226 */ setCodecConfig(@onNull BluetoothCodecConfig codecConfig)227 public @NonNull Builder setCodecConfig(@NonNull BluetoothCodecConfig codecConfig) { 228 mCodecConfig = codecConfig; 229 return this; 230 } 231 232 /** 233 * Set codec local capabilities list for this codec status. 234 * 235 * @param codecsLocalCapabilities of this codec status 236 * @return the same Builder instance 237 */ setCodecsLocalCapabilities( @onNull List<BluetoothCodecConfig> codecsLocalCapabilities)238 public @NonNull Builder setCodecsLocalCapabilities( 239 @NonNull List<BluetoothCodecConfig> codecsLocalCapabilities) { 240 mCodecsLocalCapabilities = codecsLocalCapabilities; 241 return this; 242 } 243 244 /** 245 * Set codec selectable capabilities list for this codec status. 246 * 247 * @param codecsSelectableCapabilities of this codec status 248 * @return the same Builder instance 249 */ setCodecsSelectableCapabilities( @onNull List<BluetoothCodecConfig> codecsSelectableCapabilities)250 public @NonNull Builder setCodecsSelectableCapabilities( 251 @NonNull List<BluetoothCodecConfig> codecsSelectableCapabilities) { 252 mCodecsSelectableCapabilities = codecsSelectableCapabilities; 253 return this; 254 } 255 256 /** 257 * Build {@link BluetoothCodecStatus}. 258 * @return new BluetoothCodecStatus built 259 */ build()260 public @NonNull BluetoothCodecStatus build() { 261 return new BluetoothCodecStatus(mCodecConfig, mCodecsLocalCapabilities, 262 mCodecsSelectableCapabilities); 263 } 264 } 265 } 266