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 com.android.car.hal.test; 18 19 import android.annotation.CheckResult; 20 import android.hardware.automotive.vehicle.RawPropValues; 21 import android.hardware.automotive.vehicle.VehiclePropValue; 22 import android.hardware.automotive.vehicle.VehiclePropertyStatus; 23 import android.os.SystemClock; 24 25 /** A builder class for {@link VehiclePropValue} */ 26 public final class AidlVehiclePropValueBuilder { 27 private final VehiclePropValue mPropValue; 28 29 /** 30 * Get a new builder based on the property ID. 31 */ newBuilder(int propId)32 public static AidlVehiclePropValueBuilder newBuilder(int propId) { 33 return new AidlVehiclePropValueBuilder(propId); 34 } 35 36 /** 37 * Get a new builder based on the {@link VehiclePropValue}. 38 */ newBuilder(VehiclePropValue propValue)39 public static AidlVehiclePropValueBuilder newBuilder(VehiclePropValue propValue) { 40 return new AidlVehiclePropValueBuilder(propValue); 41 } 42 AidlVehiclePropValueBuilder(int propId)43 private AidlVehiclePropValueBuilder(int propId) { 44 mPropValue = new VehiclePropValue(); 45 mPropValue.value = new RawPropValues(); 46 mPropValue.value.int32Values = new int[0]; 47 mPropValue.value.floatValues = new float[0]; 48 mPropValue.value.int64Values = new long[0]; 49 mPropValue.value.byteValues = new byte[0]; 50 mPropValue.value.stringValue = new String(); 51 mPropValue.prop = propId; 52 } 53 AidlVehiclePropValueBuilder(VehiclePropValue propValue)54 private AidlVehiclePropValueBuilder(VehiclePropValue propValue) { 55 mPropValue = clone(propValue); 56 } 57 clone(VehiclePropValue propValue)58 private VehiclePropValue clone(VehiclePropValue propValue) { 59 VehiclePropValue newValue = new VehiclePropValue(); 60 61 newValue.prop = propValue.prop; 62 newValue.areaId = propValue.areaId; 63 newValue.status = propValue.status; 64 newValue.timestamp = propValue.timestamp; 65 newValue.value = new RawPropValues(); 66 newValue.value.stringValue = propValue.value.stringValue; 67 if (propValue.value.int32Values != null) { 68 newValue.value.int32Values = propValue.value.int32Values.clone(); 69 } else { 70 newValue.value.int32Values = new int[0]; 71 } 72 if (propValue.value.floatValues != null) { 73 newValue.value.floatValues = propValue.value.floatValues.clone(); 74 } else { 75 newValue.value.floatValues = new float[0]; 76 } 77 if (propValue.value.int64Values != null) { 78 newValue.value.int64Values = propValue.value.int64Values.clone(); 79 } else { 80 newValue.value.int64Values = new long[0]; 81 } 82 if (propValue.value.byteValues != null) { 83 newValue.value.byteValues = propValue.value.byteValues.clone(); 84 } else { 85 newValue.value.byteValues = new byte[0]; 86 } 87 88 return newValue; 89 } 90 91 /** 92 * Set the area ID. 93 */ 94 @CheckResult setAreaId(int areaId)95 public AidlVehiclePropValueBuilder setAreaId(int areaId) { 96 mPropValue.areaId = areaId; 97 return this; 98 } 99 100 /** 101 * Set the status 102 */ 103 @CheckResult setStatus(@ehiclePropertyStatus int status)104 public AidlVehiclePropValueBuilder setStatus(@VehiclePropertyStatus int status) { 105 mPropValue.status = status; 106 return this; 107 } 108 109 /** 110 * Set the timestamp. 111 */ 112 @CheckResult setTimestamp(long timestamp)113 public AidlVehiclePropValueBuilder setTimestamp(long timestamp) { 114 mPropValue.timestamp = timestamp; 115 return this; 116 } 117 118 /** 119 * Set the timestamp to the current time. 120 */ 121 @CheckResult setCurrentTimestamp()122 public AidlVehiclePropValueBuilder setCurrentTimestamp() { 123 mPropValue.timestamp = SystemClock.elapsedRealtimeNanos(); 124 return this; 125 } 126 127 /** 128 * Add int32 values. 129 */ 130 @CheckResult addIntValues(int... values)131 public AidlVehiclePropValueBuilder addIntValues(int... values) { 132 int oldSize = mPropValue.value.int32Values.length; 133 int newSize = oldSize + values.length; 134 int[] newValues = new int[newSize]; 135 for (int i = 0; i < oldSize; i++) { 136 newValues[i] = mPropValue.value.int32Values[i]; 137 } 138 for (int i = 0; i < values.length; i++) { 139 newValues[oldSize + i] = values[i]; 140 } 141 mPropValue.value.int32Values = newValues; 142 return this; 143 } 144 145 /** 146 * Add float values. 147 */ 148 @CheckResult addFloatValues(float... values)149 public AidlVehiclePropValueBuilder addFloatValues(float... values) { 150 int oldSize = mPropValue.value.floatValues.length; 151 int newSize = oldSize + values.length; 152 float[] newValues = new float[newSize]; 153 for (int i = 0; i < oldSize; i++) { 154 newValues[i] = mPropValue.value.floatValues[i]; 155 } 156 for (int i = 0; i < values.length; i++) { 157 newValues[oldSize + i] = values[i]; 158 } 159 mPropValue.value.floatValues = newValues; 160 return this; 161 } 162 163 /** 164 * Add byte values. 165 */ 166 @CheckResult addByteValues(byte... values)167 public AidlVehiclePropValueBuilder addByteValues(byte... values) { 168 int oldSize = mPropValue.value.byteValues.length; 169 int newSize = oldSize + values.length; 170 byte[] newValues = new byte[newSize]; 171 for (int i = 0; i < oldSize; i++) { 172 newValues[i] = mPropValue.value.byteValues[i]; 173 } 174 for (int i = 0; i < values.length; i++) { 175 newValues[oldSize + i] = values[i]; 176 } 177 mPropValue.value.byteValues = newValues; 178 return this; 179 } 180 181 /** 182 * Add int64 values. 183 */ 184 @CheckResult addInt64Values(long... values)185 public AidlVehiclePropValueBuilder addInt64Values(long... values) { 186 int oldSize = mPropValue.value.int64Values.length; 187 int newSize = oldSize + values.length; 188 long[] newValues = new long[newSize]; 189 for (int i = 0; i < oldSize; i++) { 190 newValues[i] = mPropValue.value.int64Values[i]; 191 } 192 for (int i = 0; i < values.length; i++) { 193 newValues[oldSize + i] = values[i]; 194 } 195 mPropValue.value.int64Values = newValues; 196 return this; 197 } 198 199 /** 200 * Set boolean value. 201 */ 202 @CheckResult setBooleanValue(boolean value)203 public AidlVehiclePropValueBuilder setBooleanValue(boolean value) { 204 mPropValue.value.int32Values = new int[1]; 205 mPropValue.value.int32Values[0] = value ? 1 : 0; 206 return this; 207 } 208 209 /** 210 * Set string value. 211 */ 212 @CheckResult setStringValue(String val)213 public AidlVehiclePropValueBuilder setStringValue(String val) { 214 mPropValue.value.stringValue = val; 215 return this; 216 } 217 218 /** 219 * Build the {@link VehiclePropValue}. 220 */ build()221 public VehiclePropValue build() { 222 return clone(mPropValue); 223 } 224 } 225