1 /*
2  * Copyright (C) 2022 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.car.hardware.property;
18 
19 import static android.car.feature.Flags.FLAG_AREA_ID_CONFIG_ACCESS;
20 import static android.car.feature.Flags.FLAG_VARIABLE_UPDATE_RATE;
21 
22 import android.annotation.FlaggedApi;
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.car.hardware.CarPropertyConfig;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 
30 import java.util.Collections;
31 import java.util.List;
32 
33 /**
34  * Represents area ID specific configuration information for a vehicle property.
35  *
36  * @param <T> matches the type for the {@link android.car.hardware.CarPropertyConfig}.
37  */
38 public final class AreaIdConfig<T> implements Parcelable {
39     @NonNull
40     public static final Parcelable.Creator<AreaIdConfig<Object>> CREATOR = getCreator();
41 
42     private final @CarPropertyConfig.VehiclePropertyAccessType int mAccess;
43     private final int mAreaId;
44     @Nullable private final T mMinValue;
45     @Nullable private final T mMaxValue;
46     private final List<T> mSupportedEnumValues;
47     private final boolean mSupportVariableUpdateRate;
48 
AreaIdConfig( int areaId, @Nullable T minValue, @Nullable T maxValue, List<T> supportedEnumValues, @CarPropertyConfig.VehiclePropertyAccessType int access, boolean supportVariableUpdateRate)49     private AreaIdConfig(
50             int areaId, @Nullable T minValue, @Nullable T maxValue, List<T> supportedEnumValues,
51             @CarPropertyConfig.VehiclePropertyAccessType int access,
52             boolean supportVariableUpdateRate) {
53         mAccess = access;
54         mAreaId = areaId;
55         mMinValue = minValue;
56         mMaxValue = maxValue;
57         mSupportedEnumValues = supportedEnumValues;
58         mSupportVariableUpdateRate = supportVariableUpdateRate;
59     }
60 
61     @SuppressWarnings("unchecked")
AreaIdConfig(Parcel in)62     private AreaIdConfig(Parcel in) {
63         mAccess = in.readInt();
64         mAreaId = in.readInt();
65         mMinValue = (T) in.readValue(getClass().getClassLoader());
66         mMaxValue = (T) in.readValue(getClass().getClassLoader());
67         mSupportedEnumValues = in.readArrayList(getClass().getClassLoader());
68         mSupportVariableUpdateRate = in.readBoolean();
69     }
70 
getCreator()71     private static <E> Parcelable.Creator<AreaIdConfig<E>> getCreator() {
72         return new Creator<AreaIdConfig<E>>() {
73             @Override
74             public AreaIdConfig<E> createFromParcel(Parcel source) {
75                 return new AreaIdConfig<>(source);
76             }
77 
78             @Override
79             @SuppressWarnings("unchecked")
80             public AreaIdConfig<E>[] newArray(int size) {
81                 AreaIdConfig<E>[] areaIdConfigs = new AreaIdConfig[size];
82                 for (int i = 0; i < size; i++) {
83                     areaIdConfigs[i] = null;
84                 }
85                 return areaIdConfigs;
86             }
87         };
88     }
89 
90     /**
91      * Return the access type of the car property at the current areaId.
92      * <p>The access type could be one of the following:
93      * <ul>
94      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_NONE}</li>
95      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_READ}</li>
96      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_WRITE}</li>
97      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_READ_WRITE}</li>
98      * </ul>
99      *
100      * @return the access type of the car property at the current areaId.
101      */
102     @FlaggedApi(FLAG_AREA_ID_CONFIG_ACCESS)
103     public @CarPropertyConfig.VehiclePropertyAccessType int getAccess() {
104         return mAccess;
105     }
106 
107     /**
108      * Returns the area ID for this configuration.
109      *
110      * @return area ID for this configuration.
111      */
112     public int getAreaId() {
113         return mAreaId;
114     }
115 
116     /**
117      * Returns the minimum value supported for the {@link #getAreaId()}.
118      *
119      * @return minimum value supported for the {@link #getAreaId()}. Will return {@code null} if no
120      *     minimum value supported.
121      */
122     @Nullable
123     public T getMinValue() {
124         return mMinValue;
125     }
126 
127     /**
128      * Returns the maximum value supported for the {@link #getAreaId()}.
129      *
130      * @return maximum value supported for the {@link #getAreaId()}. Will return {@code null} if no
131      *     maximum value supported.
132      */
133     @Nullable
134     public T getMaxValue() {
135         return mMaxValue;
136     }
137 
138     /**
139      * Returns whether variable update rate is supported.
140      *
141      * If this returns {@code false}, variable update rate is always disabled for this area ID.
142      *
143      * If this returns {@code true}, variable update rate will be disabled if client calls
144      * {@link Subscription.Builder#setVariableUpdateRateEnabled} with {@code false}, or enabled
145      * otherwise.
146      *
147      * @return whether variable update rate is supported.
148      */
149     @FlaggedApi(FLAG_VARIABLE_UPDATE_RATE)
150     public boolean isVariableUpdateRateSupported() {
151         return mSupportVariableUpdateRate;
152     }
153 
154     /**
155      * Returns the supported enum values for the {@link #getAreaId()}. If list is empty, the
156      * property does not support an enum.
157      */
158     @NonNull
159     public List<T> getSupportedEnumValues() {
160         return Collections.unmodifiableList(mSupportedEnumValues);
161     }
162 
163     @Override
164     public int describeContents() {
165         return 0;
166     }
167 
168     @Override
169     public void writeToParcel(@NonNull Parcel dest, int flags) {
170         dest.writeInt(mAccess);
171         dest.writeInt(mAreaId);
172         dest.writeValue(mMinValue);
173         dest.writeValue(mMaxValue);
174         dest.writeList(mSupportedEnumValues);
175         dest.writeBoolean(mSupportVariableUpdateRate);
176     }
177 
178     @Override
179     public String toString() {
180         StringBuilder sb = new StringBuilder();
181         sb.append("AreaIdConfig{")
182                 .append("mAccess=").append(mAccess)
183                 .append("mAreaId=").append(mAreaId);
184         if (mMinValue != null) {
185             sb.append(", mMinValue=").append(mMinValue);
186         }
187         if (mMaxValue != null) {
188             sb.append(", mMaxValue=").append(mMaxValue);
189         }
190         if (!mSupportedEnumValues.isEmpty()) {
191             sb.append(", mSupportedEnumValues=").append(mSupportedEnumValues);
192         }
193         return sb.append("}").toString();
194     }
195 
196     /**
197      * @param <T> matches the type for the {@link android.car.hardware.CarPropertyConfig}.
198      *
199      * This is supposed to be called by CarService only. For history reason, we exposed
200      * this as system API, however, client must not use this builder and should use the getXXX
201      * method in {@code AreaIdConfig}.
202      *
203      * @hide
204      * @deprecated marked as deprecated because clients should not have direct access to the
205      * AreaIdConfig.Builder class
206      */
207     @Deprecated
208     @SystemApi
209     public static final class Builder<T> {
210         private final @CarPropertyConfig.VehiclePropertyAccessType int mAccess;
211         private final int mAreaId;
212         private T mMinValue = null;
213         private T mMaxValue = null;
214         private List<T> mSupportedEnumValues = Collections.EMPTY_LIST;
215         private boolean mSupportVariableUpdateRate = false;
216 
217         public Builder(int areaId) {
218             mAccess = CarPropertyConfig.VEHICLE_PROPERTY_ACCESS_NONE;
219             mAreaId = areaId;
220         }
221 
222         @FlaggedApi(FLAG_AREA_ID_CONFIG_ACCESS)
223         public Builder(@CarPropertyConfig.VehiclePropertyAccessType int access, int areaId) {
224             mAccess = access;
225             mAreaId = areaId;
226         }
227 
228         /** Set the min value for the {@link AreaIdConfig}. */
229         @NonNull
230         public Builder<T> setMinValue(T minValue) {
231             mMinValue = minValue;
232             return this;
233         }
234 
235         /** Set the max value for the {@link AreaIdConfig}. */
236         @NonNull
237         public Builder<T> setMaxValue(T maxValue) {
238             mMaxValue = maxValue;
239             return this;
240         }
241 
242         /** Set the supported enum values for the {@link AreaIdConfig}. */
243         @NonNull
244         public Builder<T> setSupportedEnumValues(@NonNull List<T> supportedEnumValues) {
245             mSupportedEnumValues = supportedEnumValues;
246             return this;
247         }
248 
249         /**
250          * Sets whether variable update rate is supported.
251          *
252          * This is supposed to be called by CarService only.
253          *
254          * @hide
255          */
256         @NonNull
257         public Builder<T> setSupportVariableUpdateRate(boolean supportVariableUpdateRate) {
258             mSupportVariableUpdateRate = supportVariableUpdateRate;
259             return this;
260         }
261 
262         /** Builds a new {@link android.car.hardware.property.AreaIdConfig}. */
263         @NonNull
264         public AreaIdConfig<T> build() {
265             return new AreaIdConfig<>(mAreaId, mMinValue, mMaxValue, mSupportedEnumValues, mAccess,
266                     mSupportVariableUpdateRate);
267         }
268     }
269 }
270