1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package android.bluetooth;
19 
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.text.TextUtils;
25 
26 /**
27  * Represents the Hearing Access Profile preset.
28  *
29  * @hide
30  */
31 @SystemApi
32 public final class BluetoothHapPresetInfo implements Parcelable {
33     private int mPresetIndex;
34     private String mPresetName = "";
35     private boolean mIsWritable;
36     private boolean mIsAvailable;
37 
38     /**
39      * HapPresetInfo constructor
40      *
41      * @param presetIndex Preset index
42      * @param presetName Preset Name
43      * @param isWritable Is writable flag
44      * @param isAvailable Is available flag
45      */
BluetoothHapPresetInfo( int presetIndex, @NonNull String presetName, boolean isWritable, boolean isAvailable)46     /*package*/ BluetoothHapPresetInfo(
47             int presetIndex, @NonNull String presetName, boolean isWritable, boolean isAvailable) {
48         this.mPresetIndex = presetIndex;
49         this.mPresetName = presetName;
50         this.mIsWritable = isWritable;
51         this.mIsAvailable = isAvailable;
52     }
53 
54     /**
55      * HapPresetInfo constructor
56      *
57      * @param in HapPresetInfo parcel
58      */
BluetoothHapPresetInfo(@onNull Parcel in)59     private BluetoothHapPresetInfo(@NonNull Parcel in) {
60         mPresetIndex = in.readInt();
61         mPresetName = in.readString();
62         mIsWritable = in.readBoolean();
63         mIsAvailable = in.readBoolean();
64     }
65 
66     /**
67      * HapPresetInfo preset index
68      *
69      * @return Preset index
70      */
getIndex()71     public int getIndex() {
72         return mPresetIndex;
73     }
74 
75     /**
76      * HapPresetInfo preset name
77      *
78      * @return Preset name
79      */
getName()80     public @NonNull String getName() {
81         return mPresetName;
82     }
83 
84     /**
85      * HapPresetInfo preset writability
86      *
87      * @return If preset is writable
88      */
isWritable()89     public boolean isWritable() {
90         return mIsWritable;
91     }
92 
93     /**
94      * HapPresetInfo availability
95      *
96      * @return If preset is available
97      */
isAvailable()98     public boolean isAvailable() {
99         return mIsAvailable;
100     }
101 
102     /** HapPresetInfo array creator */
103     public static final @NonNull Creator<BluetoothHapPresetInfo> CREATOR =
104             new Creator<BluetoothHapPresetInfo>() {
105                 public BluetoothHapPresetInfo createFromParcel(@NonNull Parcel in) {
106                     return new BluetoothHapPresetInfo(in);
107                 }
108 
109                 public BluetoothHapPresetInfo[] newArray(int size) {
110                     return new BluetoothHapPresetInfo[size];
111                 }
112             };
113 
114     /** @hide */
115     @Override
describeContents()116     public int describeContents() {
117         return 0;
118     }
119 
120     @Override
writeToParcel(@onNull Parcel dest, int flags)121     public void writeToParcel(@NonNull Parcel dest, int flags) {
122         dest.writeInt(mPresetIndex);
123         dest.writeString(mPresetName);
124         dest.writeBoolean(mIsWritable);
125         dest.writeBoolean(mIsAvailable);
126     }
127 
128     /**
129      * Builder for {@link BluetoothHapPresetInfo}.
130      *
131      * <p>By default, the preset index will be set to {@link
132      * BluetoothHapClient#PRESET_INDEX_UNAVAILABLE}, the name to an empty string, writability and
133      * availability both to false.
134      *
135      * @hide
136      */
137     public static final class Builder {
138         private int mPresetIndex = BluetoothHapClient.PRESET_INDEX_UNAVAILABLE;
139         private String mPresetName = "";
140         private boolean mIsWritable = false;
141         private boolean mIsAvailable = false;
142 
143         /**
144          * Creates a new builder.
145          *
146          * @param index The preset index for HAP preset info
147          * @param name The preset name for HAP preset info
148          */
Builder(int index, @NonNull String name)149         public Builder(int index, @NonNull String name) {
150             if (TextUtils.isEmpty(name)) {
151                 throw new IllegalArgumentException(
152                         "The size of the preset name for HAP shall be at"
153                                 + " least one character long.");
154             }
155             if (index < 0) {
156                 throw new IllegalArgumentException(
157                         "Preset index for HAP shall be a non-negative value.");
158             }
159 
160             mPresetIndex = index;
161             mPresetName = name;
162         }
163 
164         /**
165          * Set preset writability for HAP preset info.
166          *
167          * @param isWritable whether preset is writable
168          * @return the same Builder instance
169          */
setWritable(boolean isWritable)170         public @NonNull Builder setWritable(boolean isWritable) {
171             mIsWritable = isWritable;
172             return this;
173         }
174 
175         /**
176          * Set preset availability for HAP preset info.
177          *
178          * @param isAvailable whether preset is currently available to select
179          * @return the same Builder instance
180          */
setAvailable(boolean isAvailable)181         public @NonNull Builder setAvailable(boolean isAvailable) {
182             mIsAvailable = isAvailable;
183             return this;
184         }
185 
186         /**
187          * Build {@link BluetoothHapPresetInfo}.
188          *
189          * @return new BluetoothHapPresetInfo built
190          */
build()191         public @NonNull BluetoothHapPresetInfo build() {
192             return new BluetoothHapPresetInfo(mPresetIndex, mPresetName, mIsWritable, mIsAvailable);
193         }
194     }
195 }
196