1 /*
2  * Copyright (C) 2016 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.vehiclehal.test;
18 
19 import android.annotation.CheckResult;
20 import android.hardware.automotive.vehicle.V2_0.VehicleAreaConfig;
21 import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
22 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
23 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode;
24 
25 import java.util.Collection;
26 
27 /**
28  * A builder class for {@link android.hardware.automotive.vehicle.V2_0.VehiclePropConfig}
29  */
30 public class VehiclePropConfigBuilder {
31 
32     private final VehiclePropConfig mConfig;
33 
34     @CheckResult
newBuilder(int propId)35     public static VehiclePropConfigBuilder newBuilder(int propId) {
36         return new VehiclePropConfigBuilder(propId);
37     }
38 
VehiclePropConfigBuilder(int propId)39     private VehiclePropConfigBuilder(int propId) {
40         mConfig = new VehiclePropConfig();
41         mConfig.prop = propId;
42         mConfig.access = VehiclePropertyAccess.READ_WRITE;
43         mConfig.changeMode = VehiclePropertyChangeMode.ON_CHANGE;
44     }
45 
clone(VehiclePropConfig propConfig)46     private VehiclePropConfig clone(VehiclePropConfig propConfig) {
47         VehiclePropConfig newConfig = new VehiclePropConfig();
48 
49         newConfig.prop = propConfig.prop;
50         newConfig.access = propConfig.access;
51         newConfig.changeMode = propConfig.changeMode;
52         newConfig.configString = propConfig.configString;
53         newConfig.minSampleRate = propConfig.minSampleRate;
54         newConfig.maxSampleRate = propConfig.maxSampleRate;
55         newConfig.configArray.addAll(propConfig.configArray);
56         for (VehicleAreaConfig area : propConfig.areaConfigs) {
57             VehicleAreaConfig newArea = new VehicleAreaConfig();
58             newArea.areaId = area.areaId;
59             newArea.minInt32Value = area.minInt32Value;
60             newArea.maxInt32Value = area.maxInt32Value;
61             newArea.minInt64Value = area.minInt64Value;
62             newArea.maxInt64Value = area.maxInt64Value;
63             newArea.minFloatValue = area.minFloatValue;
64             newArea.maxFloatValue = area.maxFloatValue;
65             newConfig.areaConfigs.add(newArea);
66         }
67 
68         return newConfig;
69     }
70 
71     @CheckResult
setAccess(int access)72     public VehiclePropConfigBuilder setAccess(int access) {
73         mConfig.access = access;
74         return this;
75     }
76 
77     @CheckResult
setChangeMode(int changeMode)78     public VehiclePropConfigBuilder setChangeMode(int changeMode) {
79         mConfig.changeMode = changeMode;
80         return this;
81     }
82 
83     @CheckResult
setConfigString(String configString)84     public VehiclePropConfigBuilder setConfigString(String configString) {
85         mConfig.configString = configString;
86         return this;
87     }
88 
89 
90     @CheckResult
setConfigArray(Collection<Integer> configArray)91     public VehiclePropConfigBuilder setConfigArray(Collection<Integer> configArray) {
92         mConfig.configArray.clear();
93         mConfig.configArray.addAll(configArray);
94         return this;
95     }
96 
97     @CheckResult
addAreaConfig(int areaId)98     public  VehiclePropConfigBuilder addAreaConfig(int areaId) {
99         VehicleAreaConfig area = new VehicleAreaConfig();
100         area.areaId = areaId;
101         mConfig.areaConfigs.add(area);
102         return this;
103     }
104 
105     @CheckResult
addAreaConfig(int areaId, int minValue, int maxValue)106     public VehiclePropConfigBuilder addAreaConfig(int areaId, int minValue, int maxValue) {
107         VehicleAreaConfig area = new VehicleAreaConfig();
108         area.areaId = areaId;
109         area.minInt32Value = minValue;
110         area.maxInt32Value = maxValue;
111         mConfig.areaConfigs.add(area);
112         return this;
113     }
114 
115     @CheckResult
addAreaConfig(int areaId, float minValue, float maxValue)116     public VehiclePropConfigBuilder addAreaConfig(int areaId, float minValue, float maxValue) {
117         VehicleAreaConfig area = new VehicleAreaConfig();
118         area.areaId = areaId;
119         area.minFloatValue = minValue;
120         area.maxFloatValue = maxValue;
121         mConfig.areaConfigs.add(area);
122         return this;
123     }
124 
build()125     public VehiclePropConfig build() {
126         return clone(mConfig);
127     }
128 }
129