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 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 return this; 73 } 74 75 @CheckResult setChangeMode(int changeMode)76 public AidlVehiclePropConfigBuilder setChangeMode(int changeMode) { 77 mConfig.changeMode = changeMode; 78 return this; 79 } 80 81 @CheckResult setMaxSampleRate(float maxSampleRate)82 public AidlVehiclePropConfigBuilder setMaxSampleRate(float maxSampleRate) { 83 mConfig.maxSampleRate = maxSampleRate; 84 return this; 85 } 86 87 @CheckResult setMinSampleRate(float minSampleRate)88 public AidlVehiclePropConfigBuilder setMinSampleRate(float minSampleRate) { 89 mConfig.minSampleRate = minSampleRate; 90 return this; 91 } 92 93 @CheckResult setConfigString(String configString)94 public AidlVehiclePropConfigBuilder setConfigString(String configString) { 95 mConfig.configString = configString; 96 return this; 97 } 98 99 100 @CheckResult setConfigArray(Collection<Integer> configArray)101 public AidlVehiclePropConfigBuilder setConfigArray(Collection<Integer> configArray) { 102 mConfig.configArray = new int[configArray.size()]; 103 int i = 0; 104 for (int value : configArray) { 105 mConfig.configArray[i] = value; 106 i++; 107 } 108 return this; 109 } 110 111 @CheckResult addAreaConfig(int areaId)112 public AidlVehiclePropConfigBuilder addAreaConfig(int areaId) { 113 VehicleAreaConfig area = new VehicleAreaConfig(); 114 area.areaId = areaId; 115 addAreaConfig(area); 116 return this; 117 } 118 119 @CheckResult addAreaConfig(int areaId, int minValue, int maxValue)120 public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, int minValue, int maxValue) { 121 VehicleAreaConfig area = new VehicleAreaConfig(); 122 area.areaId = areaId; 123 area.minInt32Value = minValue; 124 area.maxInt32Value = maxValue; 125 addAreaConfig(area); 126 return this; 127 } 128 129 @CheckResult addAreaConfig(int areaId, float minValue, float maxValue)130 public AidlVehiclePropConfigBuilder addAreaConfig(int areaId, float minValue, float maxValue) { 131 VehicleAreaConfig area = new VehicleAreaConfig(); 132 area.areaId = areaId; 133 area.minFloatValue = minValue; 134 area.maxFloatValue = maxValue; 135 addAreaConfig(area); 136 return this; 137 } 138 build()139 public VehiclePropConfig build() { 140 return clone(mConfig); 141 } 142 duplicateAreaConfig(VehicleAreaConfig[] areaConfigs)143 private static VehicleAreaConfig[] duplicateAreaConfig(VehicleAreaConfig[] areaConfigs) { 144 return duplicateAreaConfig(areaConfigs, areaConfigs.length); 145 } 146 duplicateAreaConfig(VehicleAreaConfig[] areaConfigs, int newSize)147 private static VehicleAreaConfig[] duplicateAreaConfig(VehicleAreaConfig[] areaConfigs, 148 int newSize) { 149 VehicleAreaConfig[] out = new VehicleAreaConfig[newSize]; 150 int i = 0; 151 for (VehicleAreaConfig area : areaConfigs) { 152 VehicleAreaConfig newArea = new VehicleAreaConfig(); 153 newArea.areaId = area.areaId; 154 newArea.minInt32Value = area.minInt32Value; 155 newArea.maxInt32Value = area.maxInt32Value; 156 newArea.minInt64Value = area.minInt64Value; 157 newArea.maxInt64Value = area.maxInt64Value; 158 newArea.minFloatValue = area.minFloatValue; 159 newArea.maxFloatValue = area.maxFloatValue; 160 out[i] = newArea; 161 i++; 162 } 163 return out; 164 } 165 166 @CheckResult newBuilder(int propId)167 public static AidlVehiclePropConfigBuilder newBuilder(int propId) { 168 return new AidlVehiclePropConfigBuilder(propId); 169 } 170 171 @CheckResult newBuilder(VehiclePropConfig config)172 public static AidlVehiclePropConfigBuilder newBuilder(VehiclePropConfig config) { 173 return new AidlVehiclePropConfigBuilder(config); 174 } 175 } 176