1 /* 2 * Copyright (C) 2020 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.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.ArrayList; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * A parcelable collection of buffer constraints by codec type. 32 * 33 * @hide 34 */ 35 @SystemApi 36 public final class BufferConstraints implements Parcelable { 37 private static final String TAG = BufferConstraints.class.getSimpleName(); 38 39 public static final int BUFFER_CODEC_MAX_NUM = 32; 40 41 private final Map<Integer, BufferConstraint> mBufferConstraints; 42 private final List<BufferConstraint> mBufferConstraintList; 43 BufferConstraints(@onNull List<BufferConstraint> bufferConstraintList)44 public BufferConstraints(@NonNull List<BufferConstraint> bufferConstraintList) { 45 46 mBufferConstraintList = new ArrayList<>(bufferConstraintList); 47 mBufferConstraints = new HashMap<>(); 48 for (int i = 0; i < BUFFER_CODEC_MAX_NUM; i++) { 49 mBufferConstraints.put(i, bufferConstraintList.get(i)); 50 } 51 } 52 BufferConstraints(Parcel in)53 BufferConstraints(Parcel in) { 54 mBufferConstraints = new HashMap<>(); 55 mBufferConstraintList = in.createTypedArrayList(BufferConstraint.CREATOR); 56 for (int i = 0; i < mBufferConstraintList.size(); i++) { 57 mBufferConstraints.put(i, mBufferConstraintList.get(i)); 58 } 59 } 60 61 public static final @NonNull Parcelable.Creator<BufferConstraints> CREATOR = 62 new Parcelable.Creator<BufferConstraints>() { 63 public BufferConstraints createFromParcel(Parcel in) { 64 return new BufferConstraints(in); 65 } 66 67 public BufferConstraints[] newArray(int size) { 68 return new BufferConstraints[size]; 69 } 70 }; 71 72 @Override writeToParcel(@onNull Parcel out, int flags)73 public void writeToParcel(@NonNull Parcel out, int flags) { 74 out.writeTypedList(mBufferConstraintList); 75 } 76 77 @Override describeContents()78 public int describeContents() { 79 return 0; 80 } 81 82 /** 83 * Get the buffer constraints by codec type. 84 * 85 * @param codec Audio codec 86 * @return buffer constraints by codec type. 87 * @hide 88 */ 89 @SystemApi forCodec(@luetoothCodecConfig.SourceCodecType int codec)90 public @Nullable BufferConstraint forCodec(@BluetoothCodecConfig.SourceCodecType int codec) { 91 return mBufferConstraints.get(codec); 92 } 93 } 94