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