1 /*
2  * Copyright (C) 2021 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 com.android.car.hal.test;
18 
19 import android.annotation.CheckResult;
20 import android.hardware.automotive.vehicle.VehicleAreaConfig;
21 import android.hardware.automotive.vehicle.VehiclePropConfig;
22 import android.hardware.automotive.vehicle.VehiclePropertyAccess;
23 import android.hardware.automotive.vehicle.VehiclePropertyChangeMode;
24 
25 import java.util.Collection;
26 
27 /**
28  * A builder class for {@link android.hardware.automotive.vehicle.VehiclePropConfig}
29  */
30 public final class AidlVehiclePropConfigBuilder {
31 
32     private final VehiclePropConfig mConfig;
33 
AidlVehiclePropConfigBuilder(VehiclePropConfig propConfig)34     private AidlVehiclePropConfigBuilder(VehiclePropConfig propConfig) {
35         mConfig = clone(propConfig);
36     }
37 
AidlVehiclePropConfigBuilder(int propId)38     private AidlVehiclePropConfigBuilder(int propId) {
39         mConfig = new VehiclePropConfig();
40         mConfig.prop = propId;
41         mConfig.access = VehiclePropertyAccess.READ_WRITE;
42         mConfig.changeMode = VehiclePropertyChangeMode.ON_CHANGE;
43         mConfig.configArray = new int[0];
44         mConfig.areaConfigs = new VehicleAreaConfig[0];
45     }
46 
clone(VehiclePropConfig propConfig)47     private VehiclePropConfig clone(VehiclePropConfig propConfig) {
48         VehiclePropConfig newConfig = new VehiclePropConfig();
49 
50         newConfig.prop = propConfig.prop;
51         newConfig.access = propConfig.access;
52         newConfig.changeMode = propConfig.changeMode;
53         newConfig.configString = propConfig.configString;
54         newConfig.minSampleRate = propConfig.minSampleRate;
55         newConfig.maxSampleRate = propConfig.maxSampleRate;
56         newConfig.configArray = propConfig.configArray.clone();
57         newConfig.areaConfigs = new VehicleAreaConfig[propConfig.areaConfigs.length];
58         newConfig.areaConfigs = duplicateAreaConfig(propConfig.areaConfigs);
59         return newConfig;
60     }
61 
addAreaConfig(VehicleAreaConfig config)62     private void addAreaConfig(VehicleAreaConfig config) {
63         int oldLength = mConfig.areaConfigs.length;
64         VehicleAreaConfig[] newConfigs = duplicateAreaConfig(mConfig.areaConfigs, oldLength + 1);
65         newConfigs[oldLength] = config;
66         mConfig.areaConfigs = newConfigs;
67     }
68 
69     @CheckResult
setAccess(int access)70     public AidlVehiclePropConfigBuilder setAccess(int access) {
71         mConfig.access = access;
72         for (VehicleAreaConfig areaConfig : mConfig.areaConfigs) {
73             areaConfig.access = access;
74         }
75         return this;
76     }
77 
78     @CheckResult
setAccessPerAreaId(int access, int areaId)79     public AidlVehiclePropConfigBuilder setAccessPerAreaId(int access, int areaId) {
80         for (VehicleAreaConfig areaConfig : mConfig.areaConfigs) {
81             if (areaConfig.areaId == areaId) {
82                 areaConfig.access = access;
83                 break;
84             }
85         }
86         return this;
87     }
88 
89     @CheckResult
setChangeMode(int changeMode)90     public AidlVehiclePropConfigBuilder setChangeMode(int changeMode) {
91         mConfig.changeMode = changeMode;
92         return this;
93     }
94 
95     @CheckResult
setMaxSampleRate(float maxSampleRate)96     public AidlVehiclePropConfigBuilder setMaxSampleRate(float maxSampleRate) {
97         mConfig.maxSampleRate = maxSampleRate;
98         return this;
99     }
100 
101     @CheckResult
setMinSampleRate(float minSampleRate)102     public AidlVehiclePropConfigBuilder setMinSampleRate(float minSampleRate) {
103         mConfig.minSampleRate = minSampleRate;
104         return this;
105     }
106 
107     @CheckResult
setConfigString(String configString)108     public AidlVehiclePropConfigBuilder setConfigString(String configString) {
109         mConfig.configString = configString;
110         return this;
111     }
112 
113 
114     @CheckResult
setConfigArray(Collection<Integer> configArray)115     public AidlVehiclePropConfigBuilder setConfigArray(Collection<Integer> configArray) {
116         mConfig.configArray = new int[configArray.size()];
117         int i = 0;
118         for (int value : configArray) {
119             mConfig.configArray[i] = value;
120             i++;
121         }
122         return this;
123     }
124 
125     @CheckResult
addAreaConfig(int areaId)126     public  AidlVehiclePropConfigBuilder addAreaConfig(int areaId) {
127         VehicleAreaConfig area = new VehicleAreaConfig();
128         area.areaId = areaId;
129         area.access = mConfig.access;
130         addAreaConfig(area);
131         return this;
132     }
133 
134     @CheckResult
addAreaConfig(int areaId, int minValue, int maxValue)135     public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, int minValue, int maxValue) {
136         VehicleAreaConfig area = new VehicleAreaConfig();
137         area.areaId = areaId;
138         area.access = mConfig.access;
139         area.minInt32Value = minValue;
140         area.maxInt32Value = maxValue;
141         addAreaConfig(area);
142         return this;
143     }
144 
145     @CheckResult
addAreaConfig(int areaId, float minValue, float maxValue)146     public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, float minValue, float maxValue) {
147         VehicleAreaConfig area = new VehicleAreaConfig();
148         area.areaId = areaId;
149         area.access = mConfig.access;
150         area.minFloatValue = minValue;
151         area.maxFloatValue = maxValue;
152         addAreaConfig(area);
153         return this;
154     }
155 
156     @CheckResult
addAreaConfig(int areaId, int access)157     public  AidlVehiclePropConfigBuilder addAreaConfig(int areaId, int access) {
158         VehicleAreaConfig area = new VehicleAreaConfig();
159         area.areaId = areaId;
160         area.access = access;
161         addAreaConfig(area);
162         return this;
163     }
164 
165     @CheckResult
addAreaConfig(int areaId, int access, int minValue, int maxValue)166     public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, int access,
167                                                       int minValue, int maxValue) {
168         VehicleAreaConfig area = new VehicleAreaConfig();
169         area.areaId = areaId;
170         area.access = access;
171         area.minInt32Value = minValue;
172         area.maxInt32Value = maxValue;
173         addAreaConfig(area);
174         return this;
175     }
176 
177     @CheckResult
addAreaConfig(int areaId, int access, float minValue, float maxValue)178     public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, int access,
179                                                       float minValue, float maxValue) {
180         VehicleAreaConfig area = new VehicleAreaConfig();
181         area.areaId = areaId;
182         area.access = access;
183         area.minFloatValue = minValue;
184         area.maxFloatValue = maxValue;
185         addAreaConfig(area);
186         return this;
187     }
188 
build()189     public VehiclePropConfig build() {
190         return clone(mConfig);
191     }
192 
duplicateAreaConfig(VehicleAreaConfig[] areaConfigs)193     private static VehicleAreaConfig[] duplicateAreaConfig(VehicleAreaConfig[] areaConfigs) {
194         return duplicateAreaConfig(areaConfigs, areaConfigs.length);
195     }
196 
duplicateAreaConfig(VehicleAreaConfig[] areaConfigs, int newSize)197     private static VehicleAreaConfig[] duplicateAreaConfig(VehicleAreaConfig[] areaConfigs,
198             int newSize) {
199         VehicleAreaConfig[] out = new VehicleAreaConfig[newSize];
200         int i = 0;
201         for (VehicleAreaConfig area : areaConfigs) {
202             VehicleAreaConfig newArea = new VehicleAreaConfig();
203             newArea.areaId = area.areaId;
204             newArea.access = area.access;
205             newArea.minInt32Value = area.minInt32Value;
206             newArea.maxInt32Value = area.maxInt32Value;
207             newArea.minInt64Value = area.minInt64Value;
208             newArea.maxInt64Value = area.maxInt64Value;
209             newArea.minFloatValue = area.minFloatValue;
210             newArea.maxFloatValue = area.maxFloatValue;
211             out[i] = newArea;
212             i++;
213         }
214         return out;
215     }
216 
217     @CheckResult
newBuilder(int propId)218     public static AidlVehiclePropConfigBuilder newBuilder(int propId) {
219         return new AidlVehiclePropConfigBuilder(propId);
220     }
221 
222     @CheckResult
newBuilder(VehiclePropConfig config)223     public static AidlVehiclePropConfigBuilder newBuilder(VehiclePropConfig config) {
224         return new AidlVehiclePropConfigBuilder(config);
225     }
226 }
227