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