| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 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 | |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| Jean-Michel Trivi | dfd389c | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 24 | import java.util.Collections; |
| 25 | import java.util.List; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 29 | * Represents the codec status (configuration and capability) for a Bluetooth A2DP source device. |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 30 | * |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame] | 31 | * @see BluetoothA2dp |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 32 | */ |
| 33 | public final class BluetoothCodecStatus implements Parcelable { |
| 34 | /** |
| 35 | * Extra for the codec configuration intents of the individual profiles. |
| 36 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 37 | * <p>This extra represents the current codec status of the A2DP profile. |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 38 | */ |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 39 | public static final String EXTRA_CODEC_STATUS = "android.bluetooth.extra.CODEC_STATUS"; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 40 | |
| Jean-Michel Trivi | dfd389c | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 41 | private final @Nullable BluetoothCodecConfig mCodecConfig; |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 42 | private final @Nullable List<BluetoothCodecConfig> mCodecsLocalCapabilities; |
| 43 | private final @Nullable List<BluetoothCodecConfig> mCodecsSelectableCapabilities; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 44 | |
| Etienne Ruffieux | d3c62fd | 2022-03-08 13:37:12 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Creates a new BluetoothCodecStatus. |
| 47 | * |
| 48 | * @hide |
| 49 | */ |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 50 | public BluetoothCodecStatus( |
| 51 | @Nullable BluetoothCodecConfig codecConfig, |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 52 | @Nullable List<BluetoothCodecConfig> codecsLocalCapabilities, |
| 53 | @Nullable List<BluetoothCodecConfig> codecsSelectableCapabilities) { |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 54 | mCodecConfig = codecConfig; |
| 55 | mCodecsLocalCapabilities = codecsLocalCapabilities; |
| 56 | mCodecsSelectableCapabilities = codecsSelectableCapabilities; |
| 57 | } |
| 58 | |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 59 | private BluetoothCodecStatus(Parcel in) { |
| 60 | mCodecConfig = in.readTypedObject(BluetoothCodecConfig.CREATOR); |
| 61 | mCodecsLocalCapabilities = in.createTypedArrayList(BluetoothCodecConfig.CREATOR); |
| 62 | mCodecsSelectableCapabilities = in.createTypedArrayList(BluetoothCodecConfig.CREATOR); |
| 63 | } |
| 64 | |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 65 | @Override |
| Roman Kalukiewicz | 2883f7a | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 66 | public boolean equals(@Nullable Object o) { |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 67 | if (o instanceof BluetoothCodecStatus) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 68 | BluetoothCodecStatus other = (BluetoothCodecStatus) o; |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 69 | return (Objects.equals(other.mCodecConfig, mCodecConfig) |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 70 | && sameCapabilities(other.mCodecsLocalCapabilities, mCodecsLocalCapabilities) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 71 | && sameCapabilities( |
| 72 | other.mCodecsSelectableCapabilities, mCodecsSelectableCapabilities)); |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 77 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 78 | * Checks whether two lists of capabilities contain same capabilities. The order of the |
| 79 | * capabilities in each list is ignored. |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 80 | * |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 81 | * @param c1 the first list of capabilities to compare |
| 82 | * @param c2 the second list of capabilities to compare |
| 83 | * @return {@code true} if both lists contain same capabilities |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 84 | */ |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 85 | private static boolean sameCapabilities( |
| 86 | @Nullable List<BluetoothCodecConfig> c1, @Nullable List<BluetoothCodecConfig> c2) { |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 87 | if (c1 == null) { |
| 88 | return (c2 == null); |
| 89 | } |
| 90 | if (c2 == null) { |
| 91 | return false; |
| 92 | } |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 93 | if (c1.size() != c2.size()) { |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 96 | return c1.containsAll(c2); |
| Pavlin Radoslavov | 93e6d42 | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| Cheney Ni | 38a8716 | 2019-07-18 21:45:10 +0800 | [diff] [blame] | 99 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 100 | * Checks whether the codec config matches the selectable capabilities. Any parameters of the |
| 101 | * codec config with NONE value will be considered a wildcard matching. |
| Cheney Ni | 38a8716 | 2019-07-18 21:45:10 +0800 | [diff] [blame] | 102 | * |
| 103 | * @param codecConfig the codec config to compare against |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 104 | * @return {@code true} if the codec config matches, {@code false} otherwise |
| Cheney Ni | 38a8716 | 2019-07-18 21:45:10 +0800 | [diff] [blame] | 105 | */ |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 106 | public boolean isCodecConfigSelectable(@Nullable BluetoothCodecConfig codecConfig) { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 107 | if (codecConfig == null |
| 108 | || !codecConfig.hasSingleSampleRate() |
| 109 | || !codecConfig.hasSingleBitsPerSample() |
| 110 | || !codecConfig.hasSingleChannelMode()) { |
| Cheney Ni | 38a8716 | 2019-07-18 21:45:10 +0800 | [diff] [blame] | 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 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 137 | /** Returns a hash based on the codec config and local capabilities. */ |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 138 | @Override |
| 139 | public int hashCode() { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 140 | return Objects.hash(mCodecConfig, mCodecsLocalCapabilities, mCodecsLocalCapabilities); |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 143 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 144 | * Returns a {@link String} that describes each BluetoothCodecStatus parameter current value. |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 145 | */ |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 146 | @Override |
| 147 | public String toString() { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 148 | return "{mCodecConfig:" |
| 149 | + mCodecConfig |
| 150 | + ",mCodecsLocalCapabilities:" |
| 151 | + mCodecsLocalCapabilities |
| 152 | + ",mCodecsSelectableCapabilities:" |
| 153 | + mCodecsSelectableCapabilities |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 154 | + "}"; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| Rahul Sabnis | 4f8a2b3 | 2019-11-15 16:08:54 -0800 | [diff] [blame] | 157 | /** |
| Rahul Sabnis | 4f8a2b3 | 2019-11-15 16:08:54 -0800 | [diff] [blame] | 158 | * @return 0 |
| 159 | * @hide |
| 160 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 161 | @Override |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 162 | public int describeContents() { |
| 163 | return 0; |
| 164 | } |
| 165 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 166 | public static final @NonNull Creator<BluetoothCodecStatus> CREATOR = |
| 167 | new Creator<>() { |
| 168 | public BluetoothCodecStatus createFromParcel(Parcel in) { |
| 169 | return new BluetoothCodecStatus(in); |
| 170 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 171 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 172 | public BluetoothCodecStatus[] newArray(int size) { |
| 173 | return new BluetoothCodecStatus[size]; |
| 174 | } |
| 175 | }; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 176 | |
| Rahul Sabnis | 4f8a2b3 | 2019-11-15 16:08:54 -0800 | [diff] [blame] | 177 | /** |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 178 | * Flattens the object to a parcel. |
| Rahul Sabnis | 4f8a2b3 | 2019-11-15 16:08:54 -0800 | [diff] [blame] | 179 | * |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 180 | * @param out The Parcel in which the object should be written |
| 181 | * @param flags Additional flags about how the object should be written |
| Rahul Sabnis | 4f8a2b3 | 2019-11-15 16:08:54 -0800 | [diff] [blame] | 182 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 183 | @Override |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 184 | public void writeToParcel(@NonNull Parcel out, int flags) { |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 185 | out.writeTypedObject(mCodecConfig, 0); |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 186 | out.writeTypedList(mCodecsLocalCapabilities); |
| 187 | out.writeTypedList(mCodecsSelectableCapabilities); |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 190 | /** Returns the current codec configuration. */ |
| Jean-Michel Trivi | dfd389c | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 191 | public @Nullable BluetoothCodecConfig getCodecConfig() { |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 192 | return mCodecConfig; |
| 193 | } |
| 194 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 195 | /** Returns the codecs local capabilities. */ |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 196 | public @NonNull List<BluetoothCodecConfig> getCodecsLocalCapabilities() { |
| 197 | return (mCodecsLocalCapabilities == null) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 198 | ? Collections.emptyList() |
| 199 | : mCodecsLocalCapabilities; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 202 | /** Returns the codecs selectable capabilities. */ |
| Etienne Ruffieux | 55a0386 | 2021-10-05 17:11:45 +0000 | [diff] [blame] | 203 | public @NonNull List<BluetoothCodecConfig> getCodecsSelectableCapabilities() { |
| 204 | return (mCodecsSelectableCapabilities == null) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 205 | ? Collections.emptyList() |
| 206 | : mCodecsSelectableCapabilities; |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 207 | } |
| Etienne Ruffieux | d3c62fd | 2022-03-08 13:37:12 +0000 | [diff] [blame] | 208 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 209 | /** Builder for {@link BluetoothCodecStatus}. */ |
| Etienne Ruffieux | d3c62fd | 2022-03-08 13:37:12 +0000 | [diff] [blame] | 210 | public static final class Builder { |
| 211 | private BluetoothCodecConfig mCodecConfig = null; |
| 212 | private List<BluetoothCodecConfig> mCodecsLocalCapabilities = null; |
| 213 | private List<BluetoothCodecConfig> mCodecsSelectableCapabilities = null; |
| 214 | |
| 215 | /** |
| 216 | * Set Bluetooth codec config for this codec status. |
| 217 | * |
| 218 | * @param codecConfig of this codec status |
| 219 | * @return the same Builder instance |
| 220 | */ |
| 221 | public @NonNull Builder setCodecConfig(@NonNull BluetoothCodecConfig codecConfig) { |
| 222 | mCodecConfig = codecConfig; |
| 223 | return this; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Set codec local capabilities list for this codec status. |
| 228 | * |
| 229 | * @param codecsLocalCapabilities of this codec status |
| 230 | * @return the same Builder instance |
| 231 | */ |
| 232 | public @NonNull Builder setCodecsLocalCapabilities( |
| 233 | @NonNull List<BluetoothCodecConfig> codecsLocalCapabilities) { |
| 234 | mCodecsLocalCapabilities = codecsLocalCapabilities; |
| 235 | return this; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Set codec selectable capabilities list for this codec status. |
| 240 | * |
| 241 | * @param codecsSelectableCapabilities of this codec status |
| 242 | * @return the same Builder instance |
| 243 | */ |
| 244 | public @NonNull Builder setCodecsSelectableCapabilities( |
| 245 | @NonNull List<BluetoothCodecConfig> codecsSelectableCapabilities) { |
| 246 | mCodecsSelectableCapabilities = codecsSelectableCapabilities; |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Build {@link BluetoothCodecStatus}. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 252 | * |
| Etienne Ruffieux | d3c62fd | 2022-03-08 13:37:12 +0000 | [diff] [blame] | 253 | * @return new BluetoothCodecStatus built |
| 254 | */ |
| 255 | public @NonNull BluetoothCodecStatus build() { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 256 | return new BluetoothCodecStatus( |
| 257 | mCodecConfig, mCodecsLocalCapabilities, mCodecsSelectableCapabilities); |
| Etienne Ruffieux | d3c62fd | 2022-03-08 13:37:12 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| Pavlin Radoslavov | f6d543a | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 260 | } |