Pavlin Radoslavov | b37f181 | 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 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | import java.util.Arrays; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Represents the codec status (configuration and capability) for a Bluetooth |
| 28 | * A2DP source device. |
| 29 | * |
| 30 | * {@see BluetoothA2dp} |
| 31 | * |
| 32 | * {@hide} |
| 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 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 41 | @UnsupportedAppUsage |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 42 | public static final String EXTRA_CODEC_STATUS = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 43 | "android.bluetooth.codec.extra.CODEC_STATUS"; |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 44 | |
| 45 | private final BluetoothCodecConfig mCodecConfig; |
| 46 | private final BluetoothCodecConfig[] mCodecsLocalCapabilities; |
| 47 | private final BluetoothCodecConfig[] mCodecsSelectableCapabilities; |
| 48 | |
| 49 | public BluetoothCodecStatus(BluetoothCodecConfig codecConfig, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 50 | BluetoothCodecConfig[] codecsLocalCapabilities, |
| 51 | BluetoothCodecConfig[] codecsSelectableCapabilities) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 52 | mCodecConfig = codecConfig; |
| 53 | mCodecsLocalCapabilities = codecsLocalCapabilities; |
| 54 | mCodecsSelectableCapabilities = codecsSelectableCapabilities; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object o) { |
| 59 | if (o instanceof BluetoothCodecStatus) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 60 | BluetoothCodecStatus other = (BluetoothCodecStatus) o; |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 61 | return (Objects.equals(other.mCodecConfig, mCodecConfig) |
Pavlin Radoslavov | 9d36e6b | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 62 | && sameCapabilities(other.mCodecsLocalCapabilities, mCodecsLocalCapabilities) |
| 63 | && sameCapabilities(other.mCodecsSelectableCapabilities, |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 64 | mCodecsSelectableCapabilities)); |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
Pavlin Radoslavov | 9d36e6b | 2018-04-19 14:16:15 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Checks whether two arrays of capabilities contain same capabilities. |
| 71 | * The order of the capabilities in each array is ignored. |
| 72 | * |
| 73 | * @param c1 the first array of capabilities to compare |
| 74 | * @param c2 the second array of capabilities to compare |
| 75 | * @return true if both arrays contain same capabilities |
| 76 | */ |
| 77 | private static boolean sameCapabilities(BluetoothCodecConfig[] c1, |
| 78 | BluetoothCodecConfig[] c2) { |
| 79 | if (c1 == null) { |
| 80 | return (c2 == null); |
| 81 | } |
| 82 | if (c2 == null) { |
| 83 | return false; |
| 84 | } |
| 85 | if (c1.length != c2.length) { |
| 86 | return false; |
| 87 | } |
| 88 | return Arrays.asList(c1).containsAll(Arrays.asList(c2)); |
| 89 | } |
| 90 | |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 91 | @Override |
| 92 | public int hashCode() { |
| 93 | return Objects.hash(mCodecConfig, mCodecsLocalCapabilities, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 94 | mCodecsLocalCapabilities); |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 99 | return "{mCodecConfig:" + mCodecConfig |
| 100 | + ",mCodecsLocalCapabilities:" + Arrays.toString(mCodecsLocalCapabilities) |
| 101 | + ",mCodecsSelectableCapabilities:" + Arrays.toString(mCodecsSelectableCapabilities) |
| 102 | + "}"; |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 105 | @Override |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 106 | public int describeContents() { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | public static final Parcelable.Creator<BluetoothCodecStatus> CREATOR = |
| 111 | new Parcelable.Creator<BluetoothCodecStatus>() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 112 | public BluetoothCodecStatus createFromParcel(Parcel in) { |
| 113 | final BluetoothCodecConfig codecConfig = in.readTypedObject( |
| 114 | BluetoothCodecConfig.CREATOR); |
| 115 | final BluetoothCodecConfig[] codecsLocalCapabilities = in.createTypedArray( |
| 116 | BluetoothCodecConfig.CREATOR); |
| 117 | final BluetoothCodecConfig[] codecsSelectableCapabilities = in.createTypedArray( |
| 118 | BluetoothCodecConfig.CREATOR); |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 119 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 120 | return new BluetoothCodecStatus(codecConfig, |
| 121 | codecsLocalCapabilities, |
| 122 | codecsSelectableCapabilities); |
| 123 | } |
| 124 | |
| 125 | public BluetoothCodecStatus[] newArray(int size) { |
| 126 | return new BluetoothCodecStatus[size]; |
| 127 | } |
| 128 | }; |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 129 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 130 | @Override |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 131 | public void writeToParcel(Parcel out, int flags) { |
| 132 | out.writeTypedObject(mCodecConfig, 0); |
| 133 | out.writeTypedArray(mCodecsLocalCapabilities, 0); |
| 134 | out.writeTypedArray(mCodecsSelectableCapabilities, 0); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Gets the current codec configuration. |
| 139 | * |
| 140 | * @return the current codec configuration |
| 141 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 142 | @UnsupportedAppUsage |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 143 | public BluetoothCodecConfig getCodecConfig() { |
| 144 | return mCodecConfig; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Gets the codecs local capabilities. |
| 149 | * |
| 150 | * @return an array with the codecs local capabilities |
| 151 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 152 | @UnsupportedAppUsage |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 153 | public BluetoothCodecConfig[] getCodecsLocalCapabilities() { |
| 154 | return mCodecsLocalCapabilities; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Gets the codecs selectable capabilities. |
| 159 | * |
| 160 | * @return an array with the codecs selectable capabilities |
| 161 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 162 | @UnsupportedAppUsage |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 163 | public BluetoothCodecConfig[] getCodecsSelectableCapabilities() { |
| 164 | return mCodecsSelectableCapabilities; |
| 165 | } |
| 166 | } |